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. */
10 #ifdef HAVE_SYS_FILE_H
11 # include <sys/file.h>
23 #if defined (HAVE_STRING_H)
25 #else /* !HAVE_STRING_H */
27 #endif /* !HAVE_STRING_H */
33 #ifdef READLINE_LIBRARY
34 # include "readline.h"
37 # include <readline/readline.h>
38 # include <readline/history.h>
41 extern char *xmalloc ();
43 /* The names of functions that actually do the manipulation. */
44 int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
45 int com_delete (), com_help (), com_cd (), com_quit ();
47 /* A structure which contains information on the commands this program
51 char *name
; /* User printable name of the function. */
52 Function
*func
; /* Function to call to do the job. */
53 char *doc
; /* Documentation for this function. */
56 COMMAND commands
[] = {
57 { "cd", com_cd
, "Change to directory DIR" },
58 { "delete", com_delete
, "Delete FILE" },
59 { "help", com_help
, "Display this text" },
60 { "?", com_help
, "Synonym for `help'" },
61 { "list", com_list
, "List files in DIR" },
62 { "ls", com_list
, "Synonym for `list'" },
63 { "pwd", com_pwd
, "Print the current working directory" },
64 { "quit", com_quit
, "Quit using Fileman" },
65 { "rename", com_rename
, "Rename FILE to NEWNAME" },
66 { "stat", com_stat
, "Print out statistics on FILE" },
67 { "view", com_view
, "View the contents of FILE" },
68 { (char *)NULL
, (Function
*)NULL
, (char *)NULL
}
71 /* Forward declarations. */
73 COMMAND
*find_command ();
75 /* The name of this program, as taken from argv[0]. */
78 /* When non-zero, this global means the user is done using this program. */
87 r
= xmalloc (strlen (s
) + 1);
100 initialize_readline (); /* Bind our completer. */
102 /* Loop reading and executing lines until the user quits. */
105 line
= readline ("FileMan: ");
110 /* Remove leading and trailing whitespace from the line.
111 Then, if there is anything left, add it to the history list
113 s
= stripwhite (line
);
126 /* Execute a command line. */
135 /* Isolate the command word. */
137 while (line
[i
] && whitespace (line
[i
]))
141 while (line
[i
] && !whitespace (line
[i
]))
147 command
= find_command (word
);
151 fprintf (stderr
, "%s: No such command for FileMan.\n", word
);
155 /* Get argument to command, if any. */
156 while (whitespace (line
[i
]))
161 /* Call the function. */
162 return ((*(command
->func
)) (word
));
165 /* Look up NAME as the name of a command, and return a pointer to that
166 command. Return a NULL pointer if NAME isn't a command name. */
173 for (i
= 0; commands
[i
].name
; i
++)
174 if (strcmp (name
, commands
[i
].name
) == 0)
175 return (&commands
[i
]);
177 return ((COMMAND
*)NULL
);
180 /* Strip whitespace from the start and end of STRING. Return a pointer
186 register char *s
, *t
;
188 for (s
= string
; whitespace (*s
); s
++)
194 t
= s
+ strlen (s
) - 1;
195 while (t
> s
&& whitespace (*t
))
202 /* **************************************************************** */
204 /* Interface to Readline Completion */
206 /* **************************************************************** */
208 char *command_generator ();
209 char **fileman_completion ();
211 /* Tell the GNU Readline library how to complete. We want to try to complete
212 on command names if this is the first word in the line, or on filenames
214 initialize_readline ()
216 /* Allow conditional parsing of the ~/.inputrc file. */
217 rl_readline_name
= "FileMan";
219 /* Tell the completer that we want a crack first. */
220 rl_attempted_completion_function
= (CPPFunction
*)fileman_completion
;
223 /* Attempt to complete on the contents of TEXT. START and END bound the
224 region of rl_line_buffer that contains the word to complete. TEXT is
225 the word to complete. We can use the entire contents of rl_line_buffer
226 in case we want to do some simple parsing. Return the array of matches,
227 or NULL if there aren't any. */
229 fileman_completion (text
, start
, end
)
235 matches
= (char **)NULL
;
237 /* If this word is at the start of the line, then it is a command
238 to complete. Otherwise it is the name of a file in the current
241 matches
= completion_matches (text
, command_generator
);
246 /* Generator function for command completion. STATE lets us know whether
247 to start from scratch; without any state (i.e. STATE == 0), then we
248 start at the top of the list. */
250 command_generator (text
, state
)
254 static int list_index
, len
;
257 /* If this is a new word to complete, initialize now. This includes
258 saving the length of TEXT for efficiency, and initializing the index
266 /* Return the next name which partially matches from the command list. */
267 while (name
= commands
[list_index
].name
)
271 if (strncmp (name
, text
, len
) == 0)
272 return (dupstr(name
));
275 /* If no names matched, then return NULL. */
276 return ((char *)NULL
);
279 /* **************************************************************** */
281 /* FileMan Commands */
283 /* **************************************************************** */
285 /* String to pass to system (). This is for the LIST, VIEW and RENAME
287 static char syscom
[1024];
289 /* List the file(s) named in arg. */
296 sprintf (syscom
, "ls -FClg %s", arg
);
297 return (system (syscom
));
303 if (!valid_argument ("view", arg
))
306 #if defined (__MSDOS__)
307 /* more.com doesn't grok slashes in pathnames */
308 sprintf (syscom
, "less %s", arg
);
310 sprintf (syscom
, "more %s", arg
);
312 return (system (syscom
));
318 too_dangerous ("rename");
327 if (!valid_argument ("stat", arg
))
330 if (stat (arg
, &finfo
) == -1)
336 printf ("Statistics for `%s':\n", arg
);
338 printf ("%s has %d link%s, and is %d byte%s in length.\n",
341 (finfo
.st_nlink
== 1) ? "" : "s",
343 (finfo
.st_size
== 1) ? "" : "s");
344 printf ("Inode Last Change at: %s", ctime (&finfo
.st_ctime
));
345 printf (" Last access at: %s", ctime (&finfo
.st_atime
));
346 printf (" Last modified at: %s", ctime (&finfo
.st_mtime
));
353 too_dangerous ("delete");
357 /* Print out help for ARG, or for all of the commands if ARG is
365 for (i
= 0; commands
[i
].name
; i
++)
367 if (!*arg
|| (strcmp (arg
, commands
[i
].name
) == 0))
369 printf ("%s\t\t%s.\n", commands
[i
].name
, commands
[i
].doc
);
376 printf ("No commands match `%s'. Possibilties are:\n", arg
);
378 for (i
= 0; commands
[i
].name
; i
++)
380 /* Print in six columns. */
387 printf ("%s\t", commands
[i
].name
);
397 /* Change to the directory ARG. */
401 if (chdir (arg
) == -1)
411 /* Print out the current working directory. */
417 s
= getcwd (dir
, sizeof(dir
) - 1);
420 printf ("Error getting pwd: %s\n", dir
);
424 printf ("Current directory is %s\n", dir
);
428 /* The user wishes to quit using this program. Just set DONE non-zero. */
436 /* Function which tells you that you can't do this. */
437 too_dangerous (caller
)
441 "%s: Too dangerous for me to distribute. Write it yourself.\n",
445 /* Return non-zero if ARG is a valid argument for CALLER, else print
446 an error message and return zero. */
448 valid_argument (caller
, arg
)
453 fprintf (stderr
, "%s: Argument required.\n", caller
);