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. */
11 #include <readline/readline.h>
12 #include <readline/history.h>
14 extern char *getwd ();
15 extern char *xmalloc ();
17 /* The names of functions that actually do the manipulation. */
18 int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
19 int com_delete (), com_help (), com_cd (), com_quit ();
21 /* A structure which contains information on the commands this program
25 char *name; /* User printable name of the function. */
26 Function *func; /* Function to call to do the job. */
27 char *doc; /* Documentation for this function. */
30 COMMAND commands[] = {
31 { "cd", com_cd, "Change to directory DIR" },
32 { "delete", com_delete, "Delete FILE" },
33 { "help", com_help, "Display this text" },
34 { "?", com_help, "Synonym for `help'" },
35 { "list", com_list, "List files in DIR" },
36 { "ls", com_list, "Synonym for `list'" },
37 { "pwd", com_pwd, "Print the current working directory" },
38 { "quit", com_quit, "Quit using Fileman" },
39 { "rename", com_rename, "Rename FILE to NEWNAME" },
40 { "stat", com_stat, "Print out statistics on FILE" },
41 { "view", com_view, "View the contents of FILE" },
42 { (char *)NULL, (Function *)NULL, (char *)NULL }
45 /* Forward declarations. */
47 COMMAND *find_command ();
49 /* The name of this program, as taken from argv[0]. */
52 /* When non-zero, this global means the user is done using this program. */
61 r = xmalloc (strlen (s) + 1);
74 initialize_readline (); /* Bind our completer. */
76 /* Loop reading and executing lines until the user quits. */
79 line = readline ("FileMan: ");
84 /* Remove leading and trailing whitespace from the line.
85 Then, if there is anything left, add it to the history list
87 s = stripwhite (line);
100 /* Execute a command line. */
109 /* Isolate the command word. */
111 while (line[i] && whitespace (line[i]))
115 while (line[i] && !whitespace (line[i]))
121 command = find_command (word);
125 fprintf (stderr, "%s: No such command for FileMan.\n", word);
129 /* Get argument to command, if any. */
130 while (whitespace (line[i]))
135 /* Call the function. */
136 return ((*(command->func)) (word));
139 /* Look up NAME as the name of a command, and return a pointer to that
140 command. Return a NULL pointer if NAME isn't a command name. */
147 for (i = 0; commands[i].name; i++)
148 if (strcmp (name, commands[i].name) == 0)
149 return (&commands[i]);
151 return ((COMMAND *)NULL);
154 /* Strip whitespace from the start and end of STRING. Return a pointer
160 register char *s, *t;
162 for (s = string; whitespace (*s); s++)
168 t = s + strlen (s) - 1;
169 while (t > s && whitespace (*t))
176 /* **************************************************************** */
178 /* Interface to Readline Completion */
180 /* **************************************************************** */
182 char *command_generator ();
183 char **fileman_completion ();
185 /* Tell the GNU Readline library how to complete. We want to try to complete
186 on command names if this is the first word in the line, or on filenames
188 initialize_readline ()
190 /* Allow conditional parsing of the ~/.inputrc file. */
191 rl_readline_name = "FileMan";
193 /* Tell the completer that we want a crack first. */
194 rl_attempted_completion_function = (CPPFunction *)fileman_completion;
197 /* Attempt to complete on the contents of TEXT. START and END show the
198 region of TEXT that contains the word to complete. We can use the
199 entire line in case we want to do some simple parsing. Return the
200 array of matches, or NULL if there aren't any. */
202 fileman_completion (text, start, end)
208 matches = (char **)NULL;
210 /* If this word is at the start of the line, then it is a command
211 to complete. Otherwise it is the name of a file in the current
214 matches = completion_matches (text, command_generator);
219 /* Generator function for command completion. STATE lets us know whether
220 to start from scratch; without any state (i.e. STATE == 0), then we
221 start at the top of the list. */
223 command_generator (text, state)
227 static int list_index, len;
230 /* If this is a new word to complete, initialize now. This includes
231 saving the length of TEXT for efficiency, and initializing the index
239 /* Return the next name which partially matches from the command list. */
240 while (name = commands[list_index].name)
244 if (strncmp (name, text, len) == 0)
245 return (dupstr(name));
248 /* If no names matched, then return NULL. */
249 return ((char *)NULL);
252 /* **************************************************************** */
254 /* FileMan Commands */
256 /* **************************************************************** */
258 /* String to pass to system (). This is for the LIST, VIEW and RENAME
260 static char syscom[1024];
262 /* List the file(s) named in arg. */
269 sprintf (syscom, "ls -FClg %s", arg);
270 return (system (syscom));
276 if (!valid_argument ("view", arg))
279 sprintf (syscom, "more %s", arg);
280 return (system (syscom));
286 too_dangerous ("rename");
295 if (!valid_argument ("stat", arg))
298 if (stat (arg, &finfo) == -1)
304 printf ("Statistics for `%s':\n", arg);
306 printf ("%s has %d link%s, and is %d byte%s in length.\n", arg,
308 (finfo.st_nlink == 1) ? "" : "s",
310 (finfo.st_size == 1) ? "" : "s");
311 printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime));
312 printf (" Last access at: %s", ctime (&finfo.st_atime));
313 printf (" Last modified at: %s", ctime (&finfo.st_mtime));
320 too_dangerous ("delete");
324 /* Print out help for ARG, or for all of the commands if ARG is
332 for (i = 0; commands[i].name; i++)
334 if (!*arg || (strcmp (arg, commands[i].name) == 0))
336 printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
343 printf ("No commands match `%s'. Possibilties are:\n", arg);
345 for (i = 0; commands[i].name; i++)
347 /* Print in six columns. */
354 printf ("%s\t", commands[i].name);
364 /* Change to the directory ARG. */
368 if (chdir (arg) == -1)
378 /* Print out the current working directory. */
387 printf ("Error getting pwd: %s\n", dir);
391 printf ("Current directory is %s\n", dir);
395 /* The user wishes to quit using this program. Just set DONE non-zero. */
403 /* Function which tells you that you can't do this. */
404 too_dangerous (caller)
408 "%s: Too dangerous for me to distribute. Write it yourself.\n",
412 /* Return non-zero if ARG is a valid argument for CALLER, else print
413 an error message and return zero. */
415 valid_argument (caller, arg)
420 fprintf (stderr, "%s: Argument required.\n", caller);