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.
4
5   NOTE: this was taken from the GNU Readline documentation and ported
6   to libedit. A commad to output the history list was added.
7
8   */
9
10#include <stdio.h>
11#include <sys/types.h>
12#include <sys/file.h>
13#include <sys/stat.h>
14#include <sys/errno.h>
15#include <ctype.h>
16#include <string.h>
17#include <stdlib.h>
18#include <unistd.h>
19#include <locale.h>
20#include <time.h>
21
22/* GNU readline
23#include <readline/readline.h>
24#include <readline/history.h>
25*/
26#include <editline/readline.h>
27
28void * xmalloc (size_t size);
29void too_dangerous (char *caller);
30void initialize_readline ();
31int execute_line (char *line);
32int valid_argument (char *caller, char *arg);
33
34typedef int rl_icpfunc_t (char *);
35
36/* The names of functions that actually do the manipulation. */
37int com_list (char *);
38int com_view (char *);
39int com_history (char *);
40int com_rename(char *);
41int com_stat(char *);
42int com_pwd(char *);
43int com_delete(char *);
44int com_help(char *);
45int com_cd(char *);
46int com_quit(char *);
47
48/* A structure which contains information on the commands this program
49   can understand. */
50
51typedef struct {
52   char *name;                   /* User printable name of the function. */
53   rl_icpfunc_t *func;           /* Function to call to do the job. */
54   char *doc;                    /* Documentation for this function.  */
55} COMMAND;
56
57COMMAND commands[] = {
58   { "cd", com_cd, "Change to directory DIR" },
59   { "delete", com_delete, "Delete FILE" },
60   { "help", com_help, "Display this text" },
61   { "?", com_help, "Synonym for `help'" },
62   { "list", com_list, "List files in DIR" },
63   { "ls", com_list, "Synonym for `list'" },
64   { "pwd", com_pwd, "Print the current working directory" },
65   { "quit", com_quit, "Quit using Fileman" },
66   { "rename", com_rename, "Rename FILE to NEWNAME" },
67   { "stat", com_stat, "Print out statistics on FILE" },
68   { "view", com_view, "View the contents of FILE" },
69   { "history", com_history, "List editline history" },
70   { (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL }
71};
72
73/* Forward declarations. */
74char *stripwhite ();
75COMMAND *find_command ();
76
77/* The name of this program, as taken from argv[0]. */
78char *progname;
79
80/* When non-zero, this means the user is done using this program. */
81int done;
82
83char *
84dupstr (char* s)
85{
86   char *r;
87
88   r = xmalloc (strlen (s) + 1);
89   strcpy (r, s);
90   return (r);
91}
92
93int
94main (int argc, char **argv)
95{
96   char *line, *s;
97
98   progname = argv[0];
99
100   setlocale(LC_CTYPE, "");
101
102   initialize_readline();       /* Bind our completer. */
103
104   stifle_history(7);
105
106   /* Loop reading and executing lines until the user quits. */
107   for ( ; done == 0; )
108   {
109      line = readline ("FileMan: ");
110
111      if (!line)
112         break;
113
114      /* Remove leading and trailing whitespace from the line.
115         Then, if there is anything left, add it to the history list
116         and execute it. */
117      s = stripwhite(line);
118
119      if (*s) {
120
121         char* expansion;
122         int result;
123
124         result = history_expand(s, &expansion);
125
126         if (result < 0 || result == 2) {
127            fprintf(stderr, "%s\n", expansion);
128         } else {
129            add_history(expansion);
130            execute_line(expansion);
131         }
132         free(expansion);
133      }
134
135      free(line);
136   }
137   exit (0);
138
139   return 0;
140}
141
142/* Execute a command line. */
143int
144execute_line (char *line)
145{
146   register int i;
147   COMMAND *command;
148   char *word;
149
150   /* Isolate the command word. */
151   i = 0;
152   while (line[i] && isspace (line[i]))
153      i++;
154   word = line + i;
155
156   while (line[i] && !isspace (line[i]))
157      i++;
158
159   if (line[i])
160      line[i++] = '\0';
161
162   command = find_command (word);
163
164   if (!command)
165   {
166      fprintf (stderr, "%s: No such command for FileMan.\n", word);
167      return (-1);
168   }
169
170   /* Get argument to command, if any. */
171   while (isspace (line[i]))
172      i++;
173
174   word = line + i;
175
176   /* Call the function. */
177   return ((*(command->func)) (word));
178}
179
180/* Look up NAME as the name of a command, and return a pointer to that
181   command.  Return a NULL pointer if NAME isn't a command name. */
182COMMAND *
183find_command (char *name)
184{
185   register int i;
186
187   for (i = 0; commands[i].name; i++)
188      if (strcmp (name, commands[i].name) == 0)
189         return (&commands[i]);
190
191   return ((COMMAND *)NULL);
192}
193
194/* Strip whitespace from the start and end of STRING.  Return a pointer
195   into STRING. */
196char *
197stripwhite (char *string)
198{
199   register char *s, *t;
200
201   for (s = string; isspace (*s); s++)
202      ;
203
204   if (*s == 0)
205      return (s);
206
207   t = s + strlen (s) - 1;
208   while (t > s && isspace (*t))
209      t--;
210   *++t = '\0';
211
212   return s;
213}
214
215/* **************************************************************** */
216/*                                                                  */
217/*                  Interface to Readline Completion                */
218/*                                                                  */
219/* **************************************************************** */
220
221char *command_generator(const char *, int);
222char **fileman_completion(const char *, int, int);
223
224/* Tell the GNU Readline library how to complete.  We want to try to
225   complete on command names if this is the first word in the line, or
226   on filenames if not. */
227void
228initialize_readline ()
229{
230   /* Allow conditional parsing of the ~/.inputrc file. */
231   rl_readline_name = "FileMan";
232
233   /* Tell the completer that we want a crack first. */
234   rl_attempted_completion_function = fileman_completion;
235}
236
237/* Attempt to complete on the contents of TEXT.  START and END
238   bound the region of rl_line_buffer that contains the word to
239   complete.  TEXT is the word to complete.  We can use the entire
240   contents of rl_line_buffer in case we want to do some simple
241   parsing.  Returnthe array of matches, or NULL if there aren't any. */
242char **
243fileman_completion (const char* text, int start, int end)
244{
245   char **matches;
246
247   matches = (char **)NULL;
248
249   /* If this word is at the start of the line, then it is a command
250      to complete.  Otherwise it is the name of a file in the current
251      directory. */
252   if (start == 0)
253      /* TODO */
254      matches = completion_matches (text, command_generator);
255      /* matches = rl_completion_matches (text, command_generator); */
256
257   return (matches);
258}
259
260/* Generator function for command completion.  STATE lets us
261   know whether to start from scratch; without any state
262   (i.e. STATE == 0), then we start at the top of the list. */
263char *
264command_generator (text, state)
265   const char *text;
266   int state;
267{
268   static int list_index, len;
269   char *name;
270
271   /* If this is a new word to complete, initialize now.  This
272      includes saving the length of TEXT for efficiency, and
273      initializing the index variable to 0. */
274   if (!state)
275   {
276      list_index = 0;
277      len = strlen (text);
278   }
279
280   /* Return the next name which partially matches from the
281      command list. */
282   while (name = commands[list_index].name)
283   {
284      list_index++;
285
286      if (strncmp (name, text, len) == 0)
287         return (dupstr(name));
288   }
289
290   /* If no names matched, then return NULL. */
291   return ((char *)NULL);
292}
293
294/* **************************************************************** */
295/*                                                                  */
296/*                       FileMan Commands                           */
297/*                                                                  */
298/* **************************************************************** */
299
300/* String to pass to system ().  This is for the LIST, VIEW and RENAME
301   commands. */
302static char syscom[1024];
303
304/* List the file(s) named in arg. */
305int
306com_list (char *arg)
307{
308   if (!arg)
309      arg = "";
310
311   sprintf (syscom, "ls -FClg %s", arg);
312   return (system (syscom));
313}
314
315int
316com_view (char *arg)
317{
318   if (!valid_argument ("view", arg))
319      return 1;
320
321   sprintf (syscom, "more %s", arg);
322   return (system (syscom));
323}
324
325int
326com_history(char* arg)
327{
328   HIST_ENTRY *he;
329
330   /* rewind history */
331   while (next_history())
332      ;
333
334   for (he = current_history(); he != NULL; he = previous_history()) {
335      //printf("%5d  %s\n", *((int*)he->data) - 1, he->line);
336      printf("%s\n", he->line);
337   }
338
339   return 0;
340}
341
342int
343com_rename (char *arg)
344{
345   too_dangerous ("rename");
346   return (1);
347}
348
349int
350com_stat (char *arg)
351{
352   struct stat finfo;
353
354   if (!valid_argument ("stat", arg))
355      return (1);
356
357   if (stat (arg, &finfo) == -1)
358   {
359      perror (arg);
360      return (1);
361   }
362
363   printf ("Statistics for `%s':\n", arg);
364
365   printf ("%s has %ld link%s, and is %lld byte%s in length.\n", arg,
366         (long) finfo.st_nlink,
367         (finfo.st_nlink == 1) ? "" : "s",
368         (long long) finfo.st_size,
369         (finfo.st_size == 1) ? "" : "s");
370   printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime));
371   printf ("      Last access at: %s", ctime (&finfo.st_atime));
372   printf ("    Last modified at: %s", ctime (&finfo.st_mtime));
373   return (0);
374}
375
376int
377com_delete (char *arg)
378{
379   too_dangerous ("delete");
380   return (1);
381}
382
383/* Print out help for ARG, or for all of the commands if ARG is
384   not present. */
385int
386com_help (char *arg)
387{
388   register int i;
389   int printed = 0;
390
391   for (i = 0; commands[i].name; i++)
392   {
393      if (!*arg || (strcmp (arg, commands[i].name) == 0))
394      {
395         printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
396         printed++;
397      }
398   }
399
400   if (!printed)
401   {
402      printf ("No commands match `%s'.  Possibilties are:\n", arg);
403
404      for (i = 0; commands[i].name; i++)
405      {
406         /* Print in six columns. */
407         if (printed == 6)
408         {
409            printed = 0;
410            printf ("\n");
411         }
412
413         printf ("%s\t", commands[i].name);
414         printed++;
415      }
416
417      if (printed)
418         printf ("\n");
419   }
420   return (0);
421}
422
423/* Change to the directory ARG. */
424int
425com_cd (char *arg)
426{
427   if (chdir (arg) == -1)
428   {
429      perror (arg);
430      return 1;
431   }
432
433   com_pwd ("");
434   return (0);
435}
436
437/* Print out the current working directory. */
438int
439com_pwd (char* ignore)
440{
441   char dir[1024], *s;
442
443   s = (char*)getcwd(dir, sizeof(dir) - 1);
444   if (s == 0)
445   {
446      printf ("Error getting pwd: %s\n", dir);
447      return 1;
448   }
449
450   printf ("Current directory is %s\n", dir);
451   return 0;
452}
453
454/* The user wishes to quit using this program.  Just set DONE
455   non-zero. */
456int
457com_quit (char *arg)
458{
459   done = 1;
460   return (0);
461}
462
463/* Function which tells you that you can't do this. */
464void
465too_dangerous (char *caller)
466{
467   fprintf (stderr,
468         "%s: Too dangerous for me to distribute.\n",
469         caller);
470   fprintf (stderr, "Write it yourself.\n");
471}
472
473/* Return non-zero if ARG is a valid argument for CALLER,
474   else print an error message and return zero. */
475int
476valid_argument (char *caller, char *arg)
477{
478   if (!arg || !*arg)
479   {
480      fprintf (stderr, "%s: Argument required.\n", caller);
481      return (0);
482   }
483
484   return (1);
485}
486
487void *
488xmalloc (size_t size)
489{
490   register void *value = (void*)malloc(size);
491   if (value == 0)
492      fprintf(stderr, "virtual memory exhausted");
493   return value;
494}
495
496
497