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 command 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 <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
28 void * xmalloc (size_t size);
29 void too_dangerous (char *caller);
30 void initialize_readline ();
31 int execute_line (char *line);
32 int valid_argument (char *caller, char *arg);
33
34 typedef int rl_icpfunc_t (char *);
35
36 /* The names of functions that actually do the manipulation. */
37 int com_list (char *);
38 int com_view (char *);
39 int com_history (char *);
40 int com_rename(char *);
41 int com_stat(char *);
42 int com_pwd(char *);
43 int com_delete(char *);
44 int com_help(char *);
45 int com_cd(char *);
46 int com_quit(char *);
47
48 /* A structure which contains information on the commands this program
49 can understand. */
50
51 typedef 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
57 COMMAND 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. */
74 char * stripwhite (char *string);
75 COMMAND * find_command (char *name);
76
77 /* The name of this program, as taken from argv[0]. */
78 char *progname;
79
80 /* When non-zero, this means the user is done using this program. */
81 int done;
82
83 char *
dupstr(char * s)84 dupstr (char* s)
85 {
86 char *r;
87
88 r = xmalloc (strlen (s) + 1);
89 strcpy (r, s);
90 return (r);
91 }
92
93 int
main(int argc,char ** argv)94 main (int argc __attribute__((__unused__)), 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. */
143 int
execute_line(char * line)144 execute_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. */
182 COMMAND *
find_command(char * name)183 find_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. */
196 char *
stripwhite(char * string)197 stripwhite (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
221 char *command_generator(const char *, int);
222 char **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. */
227 void
initialize_readline()228 initialize_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. */
242 char **
fileman_completion(const char * text,int start,int end)243 fileman_completion (const char* text, int start, int end __attribute__((__unused__)))
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 ((char*)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. */
263 char *
command_generator(const char * text,int state)264 command_generator (const char *text, int state)
265 {
266 static int list_index, len;
267 char *name;
268
269 /* If this is a new word to complete, initialize now. This
270 includes saving the length of TEXT for efficiency, and
271 initializing the index variable to 0. */
272 if (!state)
273 {
274 list_index = 0;
275 len = strlen (text);
276 }
277
278 /* Return the next name which partially matches from the
279 command list. */
280 while ((name = commands[list_index].name))
281 {
282 list_index++;
283
284 if (strncmp (name, text, len) == 0)
285 return (dupstr(name));
286 }
287
288 /* If no names matched, then return NULL. */
289 return ((char *)NULL);
290 }
291
292 /* **************************************************************** */
293 /* */
294 /* FileMan Commands */
295 /* */
296 /* **************************************************************** */
297
298 /* String to pass to system (). This is for the LIST, VIEW and RENAME
299 commands. */
300 static char syscom[1024];
301
302 /* List the file(s) named in arg. */
303 int
com_list(char * arg)304 com_list (char *arg)
305 {
306 if (!arg)
307 arg = "";
308
309 sprintf (syscom, "ls -FClg %s", arg);
310 return (system (syscom));
311 }
312
313 int
com_view(char * arg)314 com_view (char *arg)
315 {
316 if (!valid_argument ("view", arg))
317 return 1;
318
319 sprintf (syscom, "more %s", arg);
320 return (system (syscom));
321 }
322
323 int
com_history(char * arg)324 com_history(char* arg __attribute__((__unused__)))
325 {
326 HIST_ENTRY *he;
327
328 /* rewind history */
329 while (previous_history())
330 ;
331
332 for (he = current_history(); he != NULL; he = next_history()) {
333 //printf("%5d %s\n", *((int*)he->data) - 1, he->line);
334 printf("%s\n", he->line);
335 }
336
337 return 0;
338 }
339
340 int
com_rename(char * arg)341 com_rename (char *arg __attribute__((__unused__)))
342 {
343 too_dangerous ("rename");
344 return (1);
345 }
346
347 int
com_stat(char * arg)348 com_stat (char *arg)
349 {
350 struct stat finfo;
351
352 if (!valid_argument ("stat", arg))
353 return (1);
354
355 if (stat (arg, &finfo) == -1)
356 {
357 perror (arg);
358 return (1);
359 }
360
361 printf ("Statistics for `%s':\n", arg);
362
363 printf ("%s has %ld link%s, and is %lld byte%s in length.\n", arg,
364 (long) finfo.st_nlink,
365 (finfo.st_nlink == 1) ? "" : "s",
366 (long long) finfo.st_size,
367 (finfo.st_size == 1) ? "" : "s");
368 printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime));
369 printf (" Last access at: %s", ctime (&finfo.st_atime));
370 printf (" Last modified at: %s", ctime (&finfo.st_mtime));
371 return (0);
372 }
373
374 int
com_delete(char * arg)375 com_delete (char *arg __attribute__((__unused__)))
376 {
377 too_dangerous ("delete");
378 return (1);
379 }
380
381 /* Print out help for ARG, or for all of the commands if ARG is
382 not present. */
383 int
com_help(char * arg)384 com_help (char *arg)
385 {
386 register int i;
387 int printed = 0;
388
389 for (i = 0; commands[i].name; i++)
390 {
391 if (!*arg || (strcmp (arg, commands[i].name) == 0))
392 {
393 printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
394 printed++;
395 }
396 }
397
398 if (!printed)
399 {
400 printf ("No commands match `%s'. Possibilties are:\n", arg);
401
402 for (i = 0; commands[i].name; i++)
403 {
404 /* Print in six columns. */
405 if (printed == 6)
406 {
407 printed = 0;
408 printf ("\n");
409 }
410
411 printf ("%s\t", commands[i].name);
412 printed++;
413 }
414
415 if (printed)
416 printf ("\n");
417 }
418 return (0);
419 }
420
421 /* Change to the directory ARG. */
422 int
com_cd(char * arg)423 com_cd (char *arg)
424 {
425 if (chdir (arg) == -1)
426 {
427 perror (arg);
428 return 1;
429 }
430
431 com_pwd ("");
432 return (0);
433 }
434
435 /* Print out the current working directory. */
436 int
com_pwd(char * ignore)437 com_pwd (char* ignore __attribute__((__unused__)))
438 {
439 char dir[1024], *s;
440
441 s = (char*)getcwd(dir, sizeof(dir) - 1);
442 if (s == 0)
443 {
444 printf ("Error getting pwd: %s\n", dir);
445 return 1;
446 }
447
448 printf ("Current directory is %s\n", dir);
449 return 0;
450 }
451
452 /* The user wishes to quit using this program. Just set DONE
453 non-zero. */
454 int
com_quit(char * arg)455 com_quit (char *arg __attribute__((__unused__)))
456 {
457 done = 1;
458 return (0);
459 }
460
461 /* Function which tells you that you can't do this. */
462 void
too_dangerous(char * caller)463 too_dangerous (char *caller)
464 {
465 fprintf (stderr,
466 "%s: Too dangerous for me to distribute.\n",
467 caller);
468 fprintf (stderr, "Write it yourself.\n");
469 }
470
471 /* Return non-zero if ARG is a valid argument for CALLER,
472 else print an error message and return zero. */
473 int
valid_argument(char * caller,char * arg)474 valid_argument (char *caller, char *arg)
475 {
476 if (!arg || !*arg)
477 {
478 fprintf (stderr, "%s: Argument required.\n", caller);
479 return (0);
480 }
481
482 return (1);
483 }
484
485 void *
xmalloc(size_t size)486 xmalloc (size_t size)
487 {
488 register void *value = (void*)malloc(size);
489 if (value == 0)
490 fprintf(stderr, "virtual memory exhausted");
491 return value;
492 }
493
494
495