diff options
Diffstat (limited to 'gnu/lib/libreadline/examples/fileman.c')
-rw-r--r-- | gnu/lib/libreadline/examples/fileman.c | 204 |
1 files changed, 117 insertions, 87 deletions
diff --git a/gnu/lib/libreadline/examples/fileman.c b/gnu/lib/libreadline/examples/fileman.c index d1e72c5..3ecb9f1 100644 --- a/gnu/lib/libreadline/examples/fileman.c +++ b/gnu/lib/libreadline/examples/fileman.c @@ -3,13 +3,17 @@ to manipulate files and their modes. */ #include <stdio.h> -#include <readline/readline.h> -#include <readline/history.h> #include <sys/types.h> #include <sys/file.h> #include <sys/stat.h> #include <sys/errno.h> +#include <readline/readline.h> +#include <readline/history.h> + +extern char *getwd (); +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 (); @@ -38,65 +42,78 @@ COMMAND commands[] = { { (char *)NULL, (Function *)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) + int 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 +123,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 +133,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 +151,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; + register char *s, *t; - while (whitespace (string[i])) - i++; - - if (i) - strcpy (string, string + i); + for (s = string; whitespace (*s); s++) + ; + + if (*s == 0) + return (s); - i = strlen (string) - 1; + t = s + strlen (s) - 1; + while (t > s && whitespace (*t)) + t--; + *++t = '\0'; - while (i > 0 && whitespace (string[i])) - i--; - - string[++i] = '\0'; + return s; } /* **************************************************************** */ @@ -160,18 +179,19 @@ stripwhite (string) /* */ /* **************************************************************** */ +char *command_generator (); +char **fileman_completion (); + /* 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 = (CPPFunction *)fileman_completion; } /* Attempt to complete on the contents of TEXT. START and END show the @@ -184,7 +204,6 @@ fileman_completion (text, start, end) int start, end; { char **matches; - char *command_generator (); matches = (char **)NULL; @@ -223,7 +242,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 +264,27 @@ 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; + return 1; - sprintf (syscom, "cat %s | more", arg); - system (syscom); + sprintf (syscom, "more %s", arg); + return (system (syscom)); } com_rename (arg) char *arg; { too_dangerous ("rename"); + return (1); } com_stat (arg) @@ -273,27 +293,32 @@ 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 +332,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 +343,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 +366,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 = getwd (dir); + 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 +397,7 @@ com_quit (arg) char *arg; { done = 1; + return (0); } /* Function which tells you that you can't do this. */ @@ -368,8 +405,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 +423,3 @@ valid_argument (caller, arg) return (1); } - - -/* - * Local variables: - * compile-command: "cc -g -I../.. -L.. -o fileman fileman.c -lreadline -ltermcap" - * end: - */ |