1 /* fileman.c -- A tiny application which demonstrates how to use the
2 GNU Readline library. This application interactively allows users
3 to manipulate files and their modes. */
5 * Remove the next line if you're compiling this against an installed
8 #define READLINE_LIBRARY
14 #include <sys/types.h>
15 #ifdef HAVE_SYS_FILE_H
23 #if defined (HAVE_STRING_H)
25 #else /* !HAVE_STRING_H */
27 #endif /* !HAVE_STRING_H */
29 #ifdef READLINE_LIBRARY
30 # include "readline.h"
33 # include <readline/readline.h>
34 # include <readline/history.h>
37 extern char *getwd ();
38 extern char *xmalloc ();
40 /* The names of functions that actually do the manipulation. */
41 int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
42 int com_delete (), com_help (), com_cd (), com_quit ();
44 /* A structure which contains information on the commands this program
48 char *name; /* User printable name of the function. */
49 Function *func; /* Function to call to do the job. */
50 char *doc; /* Documentation for this function. */
53 COMMAND commands[] = {
54 { "cd", com_cd, "Change to directory DIR" },
55 { "delete", com_delete, "Delete FILE" },
56 { "help", com_help, "Display this text" },
57 { "?", com_help, "Synonym for `help'" },
58 { "list", com_list, "List files in DIR" },
59 { "ls", com_list, "Synonym for `list'" },
60 { "pwd", com_pwd, "Print the current working directory" },
61 { "quit", com_quit, "Quit using Fileman" },
62 { "rename", com_rename, "Rename FILE to NEWNAME" },
63 { "stat", com_stat, "Print out statistics on FILE" },
64 { "view", com_view, "View the contents of FILE" },
65 { (char *)NULL, (Function *)NULL, (char *)NULL }
68 /* Forward declarations. */
70 COMMAND *find_command ();
72 /* The name of this program, as taken from argv[0]. */
75 /* When non-zero, this global means the user is done using this program. */
84 r = xmalloc (strlen (s) + 1);
97 initialize_readline (); /* Bind our completer. */
99 /* Loop reading and executing lines until the user quits. */
102 line = readline ("FileMan: ");
107 /* Remove leading and trailing whitespace from the line.
108 Then, if there is anything left, add it to the history list
110 s = stripwhite (line);
123 /* Execute a command line. */
132 /* Isolate the command word. */
134 while (line[i] && whitespace (line[i]))
138 while (line[i] && !whitespace (line[i]))
144 command = find_command (word);
148 fprintf (stderr, "%s: No such command for FileMan.\n", word);
152 /* Get argument to command, if any. */
153 while (whitespace (line[i]))
158 /* Call the function. */
159 return ((*(command->func)) (word));
162 /* Look up NAME as the name of a command, and return a pointer to that
163 command. Return a NULL pointer if NAME isn't a command name. */
170 for (i = 0; commands[i].name; i++)
171 if (strcmp (name, commands[i].name) == 0)
172 return (&commands[i]);
174 return ((COMMAND *)NULL);
177 /* Strip whitespace from the start and end of STRING. Return a pointer
183 register char *s, *t;
185 for (s = string; whitespace (*s); s++)
191 t = s + strlen (s) - 1;
192 while (t > s && whitespace (*t))
199 /* **************************************************************** */
201 /* Interface to Readline Completion */
203 /* **************************************************************** */
205 char *command_generator ();
206 char **fileman_completion ();
208 /* Tell the GNU Readline library how to complete. We want to try to complete
209 on command names if this is the first word in the line, or on filenames
211 initialize_readline ()
213 /* Allow conditional parsing of the ~/.inputrc file. */
214 rl_readline_name = "FileMan";
216 /* Tell the completer that we want a crack first. */
217 rl_attempted_completion_function = (CPPFunction *)fileman_completion;
220 /* Attempt to complete on the contents of TEXT. START and END bound the
221 region of rl_line_buffer that contains the word to complete. TEXT is
222 the word to complete. We can use the entire contents of rl_line_buffer
223 in case we want to do some simple parsing. Return the array of matches,
224 or NULL if there aren't any. */
226 fileman_completion (text, start, end)
232 matches = (char **)NULL;
234 /* If this word is at the start of the line, then it is a command
235 to complete. Otherwise it is the name of a file in the current
238 matches = completion_matches (text, command_generator);
243 /* Generator function for command completion. STATE lets us know whether
244 to start from scratch; without any state (i.e. STATE == 0), then we
245 start at the top of the list. */
247 command_generator (text, state)
251 static int list_index, len;
254 /* If this is a new word to complete, initialize now. This includes
255 saving the length of TEXT for efficiency, and initializing the index
263 /* Return the next name which partially matches from the command list. */
264 while (name = commands[list_index].name)
268 if (strncmp (name, text, len) == 0)
269 return (dupstr(name));
272 /* If no names matched, then return NULL. */
273 return ((char *)NULL);
276 /* **************************************************************** */
278 /* FileMan Commands */
280 /* **************************************************************** */
282 /* String to pass to system (). This is for the LIST, VIEW and RENAME
284 static char syscom[1024];
286 /* List the file(s) named in arg. */
293 sprintf (syscom, "ls -FClg %s", arg);
294 return (system (syscom));
300 if (!valid_argument ("view", arg))
303 sprintf (syscom, "more %s", arg);
304 return (system (syscom));
310 too_dangerous ("rename");
319 if (!valid_argument ("stat", arg))
322 if (stat (arg, &finfo) == -1)
328 printf ("Statistics for `%s':\n", arg);
330 printf ("%s has %d link%s, and is %d byte%s in length.\n",
333 (finfo.st_nlink == 1) ? "" : "s",
335 (finfo.st_size == 1) ? "" : "s");
336 printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime));
337 printf (" Last access at: %s", ctime (&finfo.st_atime));
338 printf (" Last modified at: %s", ctime (&finfo.st_mtime));
345 too_dangerous ("delete");
349 /* Print out help for ARG, or for all of the commands if ARG is
357 for (i = 0; commands[i].name; i++)
359 if (!*arg || (strcmp (arg, commands[i].name) == 0))
361 printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
368 printf ("No commands match `%s'. Possibilties are:\n", arg);
370 for (i = 0; commands[i].name; i++)
372 /* Print in six columns. */
379 printf ("%s\t", commands[i].name);
389 /* Change to the directory ARG. */
393 if (chdir (arg) == -1)
403 /* Print out the current working directory. */
412 printf ("Error getting pwd: %s\n", dir);
416 printf ("Current directory is %s\n", dir);
420 /* The user wishes to quit using this program. Just set DONE non-zero. */
428 /* Function which tells you that you can't do this. */
429 too_dangerous (caller)
433 "%s: Too dangerous for me to distribute. Write it yourself.\n",
437 /* Return non-zero if ARG is a valid argument for CALLER, else print
438 an error message and return zero. */
440 valid_argument (caller, arg)
445 fprintf (stderr, "%s: Argument required.\n", caller);