fileman.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* fileman.c - file manager example for readline library. */
  2. /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
  3. This file is part of the GNU Readline Library (Readline), a library for
  4. reading lines of text with interactive input and history editing.
  5. Readline is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Readline is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Readline. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /* fileman.c -- A tiny application which demonstrates how to use the
  17. GNU Readline library. This application interactively allows users
  18. to manipulate files and their modes. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <sys/types.h>
  23. #ifdef HAVE_SYS_FILE_H
  24. # include <sys/file.h>
  25. #endif
  26. #include <sys/stat.h>
  27. #ifdef HAVE_UNISTD_H
  28. # include <unistd.h>
  29. #endif
  30. #include <fcntl.h>
  31. #include <stdio.h>
  32. #include <errno.h>
  33. #if defined (HAVE_STRING_H)
  34. # include <string.h>
  35. #else /* !HAVE_STRING_H */
  36. # include <strings.h>
  37. #endif /* !HAVE_STRING_H */
  38. #ifdef HAVE_STDLIB_H
  39. # include <stdlib.h>
  40. #endif
  41. #include <time.h>
  42. #ifdef READLINE_LIBRARY
  43. # include "readline.h"
  44. # include "history.h"
  45. #else
  46. # include <readline/readline.h>
  47. # include <readline/history.h>
  48. #endif
  49. extern char *xmalloc PARAMS((size_t));
  50. /* The names of functions that actually do the manipulation. */
  51. int com_list PARAMS((char *));
  52. int com_view PARAMS((char *));
  53. int com_rename PARAMS((char *));
  54. int com_stat PARAMS((char *));
  55. int com_pwd PARAMS((char *));
  56. int com_delete PARAMS((char *));
  57. int com_help PARAMS((char *));
  58. int com_cd PARAMS((char *));
  59. int com_quit PARAMS((char *));
  60. /* A structure which contains information on the commands this program
  61. can understand. */
  62. typedef struct {
  63. char *name; /* User printable name of the function. */
  64. rl_icpfunc_t *func; /* Function to call to do the job. */
  65. char *doc; /* Documentation for this function. */
  66. } COMMAND;
  67. COMMAND commands[] = {
  68. { "cd", com_cd, "Change to directory DIR" },
  69. { "delete", com_delete, "Delete FILE" },
  70. { "help", com_help, "Display this text" },
  71. { "?", com_help, "Synonym for `help'" },
  72. { "list", com_list, "List files in DIR" },
  73. { "ls", com_list, "Synonym for `list'" },
  74. { "pwd", com_pwd, "Print the current working directory" },
  75. { "quit", com_quit, "Quit using Fileman" },
  76. { "rename", com_rename, "Rename FILE to NEWNAME" },
  77. { "stat", com_stat, "Print out statistics on FILE" },
  78. { "view", com_view, "View the contents of FILE" },
  79. { (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL }
  80. };
  81. /* Forward declarations. */
  82. char *stripwhite ();
  83. COMMAND *find_command ();
  84. /* The name of this program, as taken from argv[0]. */
  85. char *progname;
  86. /* When non-zero, this global means the user is done using this program. */
  87. int done;
  88. char *
  89. dupstr (s)
  90. char *s;
  91. {
  92. char *r;
  93. r = xmalloc (strlen (s) + 1);
  94. strcpy (r, s);
  95. return (r);
  96. }
  97. main (argc, argv)
  98. int argc;
  99. char **argv;
  100. {
  101. char *line, *s;
  102. progname = argv[0];
  103. initialize_readline (); /* Bind our completer. */
  104. /* Loop reading and executing lines until the user quits. */
  105. for ( ; done == 0; )
  106. {
  107. line = readline ("FileMan: ");
  108. if (!line)
  109. break;
  110. /* Remove leading and trailing whitespace from the line.
  111. Then, if there is anything left, add it to the history list
  112. and execute it. */
  113. s = stripwhite (line);
  114. if (*s)
  115. {
  116. add_history (s);
  117. execute_line (s);
  118. }
  119. free (line);
  120. }
  121. exit (0);
  122. }
  123. /* Execute a command line. */
  124. int
  125. execute_line (line)
  126. char *line;
  127. {
  128. register int i;
  129. COMMAND *command;
  130. char *word;
  131. /* Isolate the command word. */
  132. i = 0;
  133. while (line[i] && whitespace (line[i]))
  134. i++;
  135. word = line + i;
  136. while (line[i] && !whitespace (line[i]))
  137. i++;
  138. if (line[i])
  139. line[i++] = '\0';
  140. command = find_command (word);
  141. if (!command)
  142. {
  143. fprintf (stderr, "%s: No such command for FileMan.\n", word);
  144. return (-1);
  145. }
  146. /* Get argument to command, if any. */
  147. while (whitespace (line[i]))
  148. i++;
  149. word = line + i;
  150. /* Call the function. */
  151. return ((*(command->func)) (word));
  152. }
  153. /* Look up NAME as the name of a command, and return a pointer to that
  154. command. Return a NULL pointer if NAME isn't a command name. */
  155. COMMAND *
  156. find_command (name)
  157. char *name;
  158. {
  159. register int i;
  160. for (i = 0; commands[i].name; i++)
  161. if (strcmp (name, commands[i].name) == 0)
  162. return (&commands[i]);
  163. return ((COMMAND *)NULL);
  164. }
  165. /* Strip whitespace from the start and end of STRING. Return a pointer
  166. into STRING. */
  167. char *
  168. stripwhite (string)
  169. char *string;
  170. {
  171. register char *s, *t;
  172. for (s = string; whitespace (*s); s++)
  173. ;
  174. if (*s == 0)
  175. return (s);
  176. t = s + strlen (s) - 1;
  177. while (t > s && whitespace (*t))
  178. t--;
  179. *++t = '\0';
  180. return s;
  181. }
  182. /* **************************************************************** */
  183. /* */
  184. /* Interface to Readline Completion */
  185. /* */
  186. /* **************************************************************** */
  187. char *command_generator PARAMS((const char *, int));
  188. char **fileman_completion PARAMS((const char *, int, int));
  189. /* Tell the GNU Readline library how to complete. We want to try to complete
  190. on command names if this is the first word in the line, or on filenames
  191. if not. */
  192. initialize_readline ()
  193. {
  194. /* Allow conditional parsing of the ~/.inputrc file. */
  195. rl_readline_name = "FileMan";
  196. /* Tell the completer that we want a crack first. */
  197. rl_attempted_completion_function = fileman_completion;
  198. }
  199. /* Attempt to complete on the contents of TEXT. START and END bound the
  200. region of rl_line_buffer that contains the word to complete. TEXT is
  201. the word to complete. We can use the entire contents of rl_line_buffer
  202. in case we want to do some simple parsing. Return the array of matches,
  203. or NULL if there aren't any. */
  204. char **
  205. fileman_completion (text, start, end)
  206. const char *text;
  207. int start, end;
  208. {
  209. char **matches;
  210. matches = (char **)NULL;
  211. /* If this word is at the start of the line, then it is a command
  212. to complete. Otherwise it is the name of a file in the current
  213. directory. */
  214. if (start == 0)
  215. matches = rl_completion_matches (text, command_generator);
  216. return (matches);
  217. }
  218. /* Generator function for command completion. STATE lets us know whether
  219. to start from scratch; without any state (i.e. STATE == 0), then we
  220. start at the top of the list. */
  221. char *
  222. command_generator (text, state)
  223. const char *text;
  224. int state;
  225. {
  226. static int list_index, len;
  227. char *name;
  228. /* If this is a new word to complete, initialize now. This includes
  229. saving the length of TEXT for efficiency, and initializing the index
  230. variable to 0. */
  231. if (!state)
  232. {
  233. list_index = 0;
  234. len = strlen (text);
  235. }
  236. /* Return the next name which partially matches from the command list. */
  237. while (name = commands[list_index].name)
  238. {
  239. list_index++;
  240. if (strncmp (name, text, len) == 0)
  241. return (dupstr(name));
  242. }
  243. /* If no names matched, then return NULL. */
  244. return ((char *)NULL);
  245. }
  246. /* **************************************************************** */
  247. /* */
  248. /* FileMan Commands */
  249. /* */
  250. /* **************************************************************** */
  251. /* String to pass to system (). This is for the LIST, VIEW and RENAME
  252. commands. */
  253. static char syscom[1024];
  254. /* List the file(s) named in arg. */
  255. com_list (arg)
  256. char *arg;
  257. {
  258. if (!arg)
  259. arg = "";
  260. sprintf (syscom, "ls -FClg %s", arg);
  261. return (system (syscom));
  262. }
  263. com_view (arg)
  264. char *arg;
  265. {
  266. if (!valid_argument ("view", arg))
  267. return 1;
  268. #if defined (__MSDOS__)
  269. /* more.com doesn't grok slashes in pathnames */
  270. sprintf (syscom, "less %s", arg);
  271. #else
  272. sprintf (syscom, "more %s", arg);
  273. #endif
  274. return (system (syscom));
  275. }
  276. com_rename (arg)
  277. char *arg;
  278. {
  279. too_dangerous ("rename");
  280. return (1);
  281. }
  282. com_stat (arg)
  283. char *arg;
  284. {
  285. struct stat finfo;
  286. if (!valid_argument ("stat", arg))
  287. return (1);
  288. if (stat (arg, &finfo) == -1)
  289. {
  290. perror (arg);
  291. return (1);
  292. }
  293. printf ("Statistics for `%s':\n", arg);
  294. printf ("%s has %d link%s, and is %d byte%s in length.\n",
  295. arg,
  296. finfo.st_nlink,
  297. (finfo.st_nlink == 1) ? "" : "s",
  298. finfo.st_size,
  299. (finfo.st_size == 1) ? "" : "s");
  300. printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime));
  301. printf (" Last access at: %s", ctime (&finfo.st_atime));
  302. printf (" Last modified at: %s", ctime (&finfo.st_mtime));
  303. return (0);
  304. }
  305. com_delete (arg)
  306. char *arg;
  307. {
  308. too_dangerous ("delete");
  309. return (1);
  310. }
  311. /* Print out help for ARG, or for all of the commands if ARG is
  312. not present. */
  313. com_help (arg)
  314. char *arg;
  315. {
  316. register int i;
  317. int printed = 0;
  318. for (i = 0; commands[i].name; i++)
  319. {
  320. if (!*arg || (strcmp (arg, commands[i].name) == 0))
  321. {
  322. printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
  323. printed++;
  324. }
  325. }
  326. if (!printed)
  327. {
  328. printf ("No commands match `%s'. Possibilties are:\n", arg);
  329. for (i = 0; commands[i].name; i++)
  330. {
  331. /* Print in six columns. */
  332. if (printed == 6)
  333. {
  334. printed = 0;
  335. printf ("\n");
  336. }
  337. printf ("%s\t", commands[i].name);
  338. printed++;
  339. }
  340. if (printed)
  341. printf ("\n");
  342. }
  343. return (0);
  344. }
  345. /* Change to the directory ARG. */
  346. com_cd (arg)
  347. char *arg;
  348. {
  349. if (chdir (arg) == -1)
  350. {
  351. perror (arg);
  352. return 1;
  353. }
  354. com_pwd ("");
  355. return (0);
  356. }
  357. /* Print out the current working directory. */
  358. com_pwd (ignore)
  359. char *ignore;
  360. {
  361. char dir[1024], *s;
  362. s = getcwd (dir, sizeof(dir) - 1);
  363. if (s == 0)
  364. {
  365. printf ("Error getting pwd: %s\n", dir);
  366. return 1;
  367. }
  368. printf ("Current directory is %s\n", dir);
  369. return 0;
  370. }
  371. /* The user wishes to quit using this program. Just set DONE non-zero. */
  372. com_quit (arg)
  373. char *arg;
  374. {
  375. done = 1;
  376. return (0);
  377. }
  378. /* Function which tells you that you can't do this. */
  379. too_dangerous (caller)
  380. char *caller;
  381. {
  382. fprintf (stderr,
  383. "%s: Too dangerous for me to distribute. Write it yourself.\n",
  384. caller);
  385. }
  386. /* Return non-zero if ARG is a valid argument for CALLER, else print
  387. an error message and return zero. */
  388. int
  389. valid_argument (caller, arg)
  390. char *caller, *arg;
  391. {
  392. if (!arg || !*arg)
  393. {
  394. fprintf (stderr, "%s: Argument required.\n", caller);
  395. return (0);
  396. }
  397. return (1);
  398. }