2005-02-07 Andrew Cagney <cagney@gnu.org>
[binutils-gdb.git] / readline / examples / fileman.c
index d1e72c5d5311bbea504088e4c3710a799332f383..340eee739f602c99e4738ebc093930c9bb5d9ba8 100644 (file)
@@ -1,25 +1,82 @@
+/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
+
+   This file is part of the GNU Readline Library, a library for
+   reading lines of text with interactive input and history editing.
+
+   The GNU Readline Library is free software; you can redistribute it
+   and/or modify it under the terms of the GNU General Public License
+   as published by the Free Software Foundation; either version 2, or
+   (at your option) any later version.
+
+   The GNU Readline Library is distributed in the hope that it will be
+   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
+   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   The GNU General Public License is often shipped with GNU software, and
+   is generally kept in a file called COPYING or LICENSE.  If you do not
+   have a copy of the license, write to the Free Software Foundation,
+   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+
 /* fileman.c -- A tiny application which demonstrates how to use the
    GNU Readline library.  This application interactively allows users
    to manipulate files and their modes. */
 
-#include <stdio.h>
-#include <readline/readline.h>
-#include <readline/history.h>
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include <sys/types.h>
-#include <sys/file.h>
+#ifdef HAVE_SYS_FILE_H
+#  include <sys/file.h>
+#endif
 #include <sys/stat.h>
-#include <sys/errno.h>
+
+#ifdef HAVE_UNISTD_H
+#  include <unistd.h>
+#endif
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <errno.h>
+
+#if defined (HAVE_STRING_H)
+#  include <string.h>
+#else /* !HAVE_STRING_H */
+#  include <strings.h>
+#endif /* !HAVE_STRING_H */
+
+#ifdef HAVE_STDLIB_H
+#  include <stdlib.h>
+#endif
+
+#ifdef READLINE_LIBRARY
+#  include "readline.h"
+#  include "history.h"
+#else
+#  include <readline/readline.h>
+#  include <readline/history.h>
+#endif
+
+extern char *xmalloc ();
 
 /* The names of functions that actually do the manipulation. */
-int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
-int com_delete (), com_help (), com_cd (), com_quit ();
+int com_list PARAMS((char *));
+int com_view PARAMS((char *));
+int com_rename PARAMS((char *));
+int com_stat PARAMS((char *));
+int com_pwd PARAMS((char *));
+int com_delete PARAMS((char *));
+int com_help PARAMS((char *));
+int com_cd PARAMS((char *));
+int com_quit PARAMS((char *));
 
 /* A structure which contains information on the commands this program
    can understand. */
 
 typedef struct {
   char *name;                  /* User printable name of the function. */
-  Function *func;              /* Function to call to do the job. */
+  rl_icpfunc_t *func;          /* Function to call to do the job. */
   char *doc;                   /* Documentation for this function.  */
 } COMMAND;
 
@@ -35,68 +92,81 @@ COMMAND commands[] = {
   { "rename", com_rename, "Rename FILE to NEWNAME" },
   { "stat", com_stat, "Print out statistics on FILE" },
   { "view", com_view, "View the contents of FILE" },
-  { (char *)NULL, (Function *)NULL, (char *)NULL }
+  { (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL }
 };
 
+/* Forward declarations. */
+char *stripwhite ();
+COMMAND *find_command ();
+
 /* The name of this program, as taken from argv[0]. */
 char *progname;
 
 /* When non-zero, this global means the user is done using this program. */
-int done = 0;
+int done;
+
+char *
+dupstr (s)
+     char *s;
+{
+  char *r;
+
+  r = xmalloc (strlen (s) + 1);
+  strcpy (r, s);
+  return (r);
+}
 
 main (argc, argv)
      int argc;
      char **argv;
 {
+  char *line, *s;
+
   progname = argv[0];
 
   initialize_readline ();      /* Bind our completer. */
 
   /* Loop reading and executing lines until the user quits. */
-  while (!done)
+  for ( ; done == 0; )
     {
-      char *line;
-
       line = readline ("FileMan: ");
 
       if (!line)
-       {
-         done = 1;             /* Encountered EOF at top level. */
-       }
-      else
-       {
-         /* Remove leading and trailing whitespace from the line.
-            Then, if there is anything left, add it to the history list
-            and execute it. */
-         stripwhite (line);
-
-         if (*line)
-           {
-             add_history (line);
-             execute_line (line);
-           }
-       }
-
-      if (line)
-       free (line);
+        break;
+
+      /* Remove leading and trailing whitespace from the line.
+         Then, if there is anything left, add it to the history list
+         and execute it. */
+      s = stripwhite (line);
+
+      if (*s)
+        {
+          add_history (s);
+          execute_line (s);
+        }
+
+      free (line);
     }
   exit (0);
 }
 
 /* Execute a command line. */
+int
 execute_line (line)
      char *line;
 {
   register int i;
-  COMMAND *find_command (), *command;
+  COMMAND *command;
   char *word;
 
   /* Isolate the command word. */
   i = 0;
-  while (line[i] && !whitespace (line[i]))
+  while (line[i] && whitespace (line[i]))
     i++;
+  word = line + i;
 
-  word = line;
+  while (line[i] && !whitespace (line[i]))
+    i++;
 
   if (line[i])
     line[i++] = '\0';
@@ -106,7 +176,7 @@ execute_line (line)
   if (!command)
     {
       fprintf (stderr, "%s: No such command for FileMan.\n", word);
-      return;
+      return (-1);
     }
 
   /* Get argument to command, if any. */
@@ -116,7 +186,7 @@ execute_line (line)
   word = line + i;
 
   /* Call the function. */
-  (*(command->func)) (word);
+  return ((*(command->func)) (word));
 }
 
 /* Look up NAME as the name of a command, and return a pointer to that
@@ -134,24 +204,26 @@ find_command (name)
   return ((COMMAND *)NULL);
 }
 
-/* Strip whitespace from the start and end of STRING. */
+/* Strip whitespace from the start and end of STRING.  Return a pointer
+   into STRING. */
+char *
 stripwhite (string)
      char *string;
 {
-  register int i = 0;
-
-  while (whitespace (string[i]))
-    i++;
-
-  if (i)
-    strcpy (string, string + i);
+  register char *s, *t;
 
-  i = strlen (string) - 1;
+  for (s = string; whitespace (*s); s++)
+    ;
+    
+  if (*s == 0)
+    return (s);
 
-  while (i > 0 && whitespace (string[i]))
-    i--;
+  t = s + strlen (s) - 1;
+  while (t > s && whitespace (*t))
+    t--;
+  *++t = '\0';
 
-  string[++i] = '\0';
+  return s;
 }
 
 /* **************************************************************** */
@@ -160,31 +232,32 @@ stripwhite (string)
 /*                                                                  */
 /* **************************************************************** */
 
+char *command_generator PARAMS((const char *, int));
+char **fileman_completion PARAMS((const char *, int, int));
+
 /* Tell the GNU Readline library how to complete.  We want to try to complete
    on command names if this is the first word in the line, or on filenames
    if not. */
 initialize_readline ()
 {
-  char **fileman_completion ();
-
   /* Allow conditional parsing of the ~/.inputrc file. */
   rl_readline_name = "FileMan";
 
   /* Tell the completer that we want a crack first. */
-  rl_attempted_completion_function = (Function *)fileman_completion;
+  rl_attempted_completion_function = fileman_completion;
 }
 
-/* Attempt to complete on the contents of TEXT.  START and END show the
-   region of TEXT that contains the word to complete.  We can use the
-   entire line in case we want to do some simple parsing.  Return the
-   array of matches, or NULL if there aren't any. */
+/* Attempt to complete on the contents of TEXT.  START and END bound the
+   region of rl_line_buffer that contains the word to complete.  TEXT is
+   the word to complete.  We can use the entire contents of rl_line_buffer
+   in case we want to do some simple parsing.  Return the array of matches,
+   or NULL if there aren't any. */
 char **
 fileman_completion (text, start, end)
-     char *text;
+     const char *text;
      int start, end;
 {
   char **matches;
-  char *command_generator ();
 
   matches = (char **)NULL;
 
@@ -192,7 +265,7 @@ fileman_completion (text, start, end)
      to complete.  Otherwise it is the name of a file in the current
      directory. */
   if (start == 0)
-    matches = completion_matches (text, command_generator);
+    matches = rl_completion_matches (text, command_generator);
 
   return (matches);
 }
@@ -202,7 +275,7 @@ fileman_completion (text, start, end)
    start at the top of the list. */
 char *
 command_generator (text, state)
-     char *text;
+     const char *text;
      int state;
 {
   static int list_index, len;
@@ -223,7 +296,7 @@ command_generator (text, state)
       list_index++;
 
       if (strncmp (name, text, len) == 0)
-       return (name);
+        return (dupstr(name));
     }
 
   /* If no names matched, then return NULL. */
@@ -245,26 +318,32 @@ com_list (arg)
      char *arg;
 {
   if (!arg)
-    arg = "*";
+    arg = "";
 
   sprintf (syscom, "ls -FClg %s", arg);
-  system (syscom);
+  return (system (syscom));
 }
 
 com_view (arg)
      char *arg;
 {
   if (!valid_argument ("view", arg))
-    return;
-
-  sprintf (syscom, "cat %s | more", arg);
-  system (syscom);
+    return 1;
+
+#if defined (__MSDOS__)
+  /* more.com doesn't grok slashes in pathnames */
+  sprintf (syscom, "less %s", arg);
+#else
+  sprintf (syscom, "more %s", arg);
+#endif
+  return (system (syscom));
 }
 
 com_rename (arg)
      char *arg;
 {
   too_dangerous ("rename");
+  return (1);
 }
 
 com_stat (arg)
@@ -273,27 +352,33 @@ com_stat (arg)
   struct stat finfo;
 
   if (!valid_argument ("stat", arg))
-    return;
+    return (1);
 
   if (stat (arg, &finfo) == -1)
     {
       perror (arg);
-      return;
+      return (1);
     }
 
   printf ("Statistics for `%s':\n", arg);
 
-  printf ("%s has %d link%s, and is %d bytes in length.\n", arg,
-         finfo.st_nlink, (finfo.st_nlink == 1) ? "" : "s",  finfo.st_size);
-  printf ("      Created on: %s", ctime (&finfo.st_ctime));
-  printf ("  Last access at: %s", ctime (&finfo.st_atime));
-  printf ("Last modified at: %s", ctime (&finfo.st_mtime));
+  printf ("%s has %d link%s, and is %d byte%s in length.\n",
+         arg,
+          finfo.st_nlink,
+          (finfo.st_nlink == 1) ? "" : "s",
+          finfo.st_size,
+          (finfo.st_size == 1) ? "" : "s");
+  printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime));
+  printf ("      Last access at: %s", ctime (&finfo.st_atime));
+  printf ("    Last modified at: %s", ctime (&finfo.st_mtime));
+  return (0);
 }
 
 com_delete (arg)
      char *arg;
 {
   too_dangerous ("delete");
+  return (1);
 }
 
 /* Print out help for ARG, or for all of the commands if ARG is
@@ -307,10 +392,10 @@ com_help (arg)
   for (i = 0; commands[i].name; i++)
     {
       if (!*arg || (strcmp (arg, commands[i].name) == 0))
-       {
-         printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
-         printed++;
-       }
+        {
+          printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
+          printed++;
+        }
     }
 
   if (!printed)
@@ -318,21 +403,22 @@ com_help (arg)
       printf ("No commands match `%s'.  Possibilties are:\n", arg);
 
       for (i = 0; commands[i].name; i++)
-       {
-         /* Print in six columns. */
-         if (printed == 6)
-           {
-             printed = 0;
-             printf ("\n");
-           }
-
-         printf ("%s\t", commands[i].name);
-         printed++;
-       }
+        {
+          /* Print in six columns. */
+          if (printed == 6)
+            {
+              printed = 0;
+              printf ("\n");
+            }
+
+          printf ("%s\t", commands[i].name);
+          printed++;
+        }
 
       if (printed)
-       printf ("\n");
+        printf ("\n");
     }
+  return (0);
 }
 
 /* Change to the directory ARG. */
@@ -340,20 +426,30 @@ com_cd (arg)
      char *arg;
 {
   if (chdir (arg) == -1)
-    perror (arg);
+    {
+      perror (arg);
+      return 1;
+    }
 
   com_pwd ("");
+  return (0);
 }
 
 /* Print out the current working directory. */
 com_pwd (ignore)
      char *ignore;
 {
-  char dir[1024];
+  char dir[1024], *s;
 
-  (void) getwd (dir);
+  s = getcwd (dir, sizeof(dir) - 1);
+  if (s == 0)
+    {
+      printf ("Error getting pwd: %s\n", dir);
+      return 1;
+    }
 
   printf ("Current directory is %s\n", dir);
+  return 0;
 }
 
 /* The user wishes to quit using this program.  Just set DONE non-zero. */
@@ -361,6 +457,7 @@ com_quit (arg)
      char *arg;
 {
   done = 1;
+  return (0);
 }
 
 /* Function which tells you that you can't do this. */
@@ -368,8 +465,8 @@ too_dangerous (caller)
      char *caller;
 {
   fprintf (stderr,
-          "%s: Too dangerous for me to distribute.  Write it yourself.\n",
-          caller);
+           "%s: Too dangerous for me to distribute.  Write it yourself.\n",
+           caller);
 }
 
 /* Return non-zero if ARG is a valid argument for CALLER, else print
@@ -386,10 +483,3 @@ valid_argument (caller, arg)
 
   return (1);
 }
-
-\f
-/*
- * Local variables:
- * compile-command: "cc -g -I../.. -L.. -o fileman fileman.c -lreadline -ltermcap"
- * end:
- */