PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/third_party/readline/example/fileman.c

http://shell-simulator.googlecode.com/
C | 426 lines | 284 code | 78 blank | 64 comment | 47 complexity | 705e257b2e78f07095080828eb52503b MD5 | raw file
Possible License(s): GPL-3.0, CPL-1.0
  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. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <sys/file.h>
  7. #include <sys/stat.h>
  8. #include <sys/errno.h>
  9. #include <stdlib.h>
  10. #include <readline/readline.h>
  11. #include <readline/history.h>
  12. extern char *getwd ();
  13. extern char *xmalloc ();
  14. /* The names of functions that actually do the manipulation. */
  15. int com_list (), com_view (), com_rename (), com_stat (), com_pwd ();
  16. int com_delete (), com_help (), com_cd (), com_quit ();
  17. /* A structure which contains information on the commands this program
  18. can understand. */
  19. typedef struct {
  20. char *name; /* User printable name of the function. */
  21. Function *func; /* Function to call to do the job. */
  22. char *doc; /* Documentation for this function. */
  23. } COMMAND;
  24. COMMAND commands[] = {
  25. { "cd", com_cd, "Change to directory DIR" },
  26. { "delete", com_delete, "Delete FILE" },
  27. { "help", com_help, "Display this text" },
  28. { "?", com_help, "Synonym for `help'" },
  29. { "list", com_list, "List files in DIR" },
  30. { "ls", com_list, "Synonym for `list'" },
  31. { "pwd", com_pwd, "Print the current working directory" },
  32. { "quit", com_quit, "Quit using Fileman" },
  33. { "rename", com_rename, "Rename FILE to NEWNAME" },
  34. { "stat", com_stat, "Print out statistics on FILE" },
  35. { "view", com_view, "View the contents of FILE" },
  36. { (char *)NULL, (Function *)NULL, (char *)NULL }
  37. };
  38. /* Forward declarations. */
  39. char *stripwhite ();
  40. COMMAND *find_command ();
  41. /* The name of this program, as taken from argv[0]. */
  42. char *progname;
  43. /* When non-zero, this global means the user is done using this program. */
  44. int done;
  45. char *
  46. dupstr (s)
  47. char *s;
  48. {
  49. char *r;
  50. r = xmalloc (strlen (s) + 1);
  51. strcpy (r, s);
  52. return (r);
  53. }
  54. main (argc, argv)
  55. int argc;
  56. char **argv;
  57. {
  58. char *line, *s;
  59. progname = argv[0];
  60. initialize_readline (); /* Bind our completer. */
  61. /* Loop reading and executing lines until the user quits. */
  62. for ( ; done == 0; )
  63. {
  64. line = readline (" ");
  65. if (!line)
  66. break;
  67. /* Remove leading and trailing whitespace from the line.
  68. Then, if there is anything left, add it to the history list
  69. and execute it. */
  70. s = stripwhite (line);
  71. if (*s)
  72. {
  73. add_history (s);
  74. execute_line (s);
  75. }
  76. free (line);
  77. }
  78. exit (0);
  79. }
  80. /* Execute a command line. */
  81. int
  82. execute_line (line)
  83. char *line;
  84. {
  85. register int i;
  86. COMMAND *command;
  87. char *word;
  88. /* Isolate the command word. */
  89. i = 0;
  90. while (line[i] && whitespace (line[i]))
  91. i++;
  92. word = line + i;
  93. while (line[i] && !whitespace (line[i]))
  94. i++;
  95. if (line[i])
  96. line[i++] = '\0';
  97. command = find_command (word);
  98. if (!command)
  99. {
  100. fprintf (stderr, "%s: No such command for FileMan.\n", word);
  101. return (-1);
  102. }
  103. /* Get argument to command, if any. */
  104. while (whitespace (line[i]))
  105. i++;
  106. word = line + i;
  107. /* Call the function. */
  108. return ((*(command->func)) (word));
  109. }
  110. /* Look up NAME as the name of a command, and return a pointer to that
  111. command. Return a NULL pointer if NAME isn't a command name. */
  112. COMMAND *
  113. find_command (name)
  114. char *name;
  115. {
  116. register int i;
  117. for (i = 0; commands[i].name; i++)
  118. if (strcmp (name, commands[i].name) == 0)
  119. return (&commands[i]);
  120. return ((COMMAND *)NULL);
  121. }
  122. /* Strip whitespace from the start and end of STRING. Return a pointer
  123. into STRING. */
  124. char *
  125. stripwhite (string)
  126. char *string;
  127. {
  128. register char *s, *t;
  129. for (s = string; whitespace (*s); s++)
  130. ;
  131. if (*s == 0)
  132. return (s);
  133. t = s + strlen (s) - 1;
  134. while (t > s && whitespace (*t))
  135. t--;
  136. *++t = '\0';
  137. return s;
  138. }
  139. /* **************************************************************** */
  140. /* */
  141. /* Interface to Readline Completion */
  142. /* */
  143. /* **************************************************************** */
  144. char *command_generator ();
  145. char **fileman_completion ();
  146. /* Tell the GNU Readline library how to complete. We want to try to complete
  147. on command names if this is the first word in the line, or on filenames
  148. if not. */
  149. initialize_readline ()
  150. {
  151. /* Allow conditional parsing of the ~/.inputrc file. */
  152. //rl_readline_name = "FileMan";
  153. /* Tell the completer that we want a crack first. */
  154. rl_attempted_completion_function = (CPPFunction *)fileman_completion;
  155. }
  156. /* Attempt to complete on the contents of TEXT. START and END show the
  157. region of TEXT that contains the word to complete. We can use the
  158. entire line in case we want to do some simple parsing. Return the
  159. array of matches, or NULL if there aren't any. */
  160. char **
  161. fileman_completion (text, start, end)
  162. char *text;
  163. int start, end;
  164. {
  165. char **matches;
  166. matches = (char **)NULL;
  167. /* If this word is at the start of the line, then it is a command
  168. to complete. Otherwise it is the name of a file in the current
  169. directory. */
  170. if (start == 0)
  171. matches = completion_matches (text, command_generator);
  172. return (matches);
  173. }
  174. /* Generator function for command completion. STATE lets us know whether
  175. to start from scratch; without any state (i.e. STATE == 0), then we
  176. start at the top of the list. */
  177. char *
  178. command_generator (text, state)
  179. char *text;
  180. int state;
  181. {
  182. static int list_index, len;
  183. char *name;
  184. /* If this is a new word to complete, initialize now. This includes
  185. saving the length of TEXT for efficiency, and initializing the index
  186. variable to 0. */
  187. if (!state)
  188. {
  189. list_index = 0;
  190. len = strlen (text);
  191. }
  192. /* Return the next name which partially matches from the command list. */
  193. while (name = commands[list_index].name)
  194. {
  195. list_index++;
  196. if (strncmp (name, text, len) == 0)
  197. return (dupstr(name));
  198. }
  199. /* If no names matched, then return NULL. */
  200. return ((char *)NULL);
  201. }
  202. /* **************************************************************** */
  203. /* */
  204. /* FileMan Commands */
  205. /* */
  206. /* **************************************************************** */
  207. /* String to pass to system (). This is for the LIST, VIEW and RENAME
  208. commands. */
  209. static char syscom[1024];
  210. /* List the file(s) named in arg. */
  211. com_list (arg)
  212. char *arg;
  213. {
  214. if (!arg)
  215. arg = "";
  216. sprintf (syscom, "ls -FClg %s", arg);
  217. return (system (syscom));
  218. }
  219. com_view (arg)
  220. char *arg;
  221. {
  222. if (!valid_argument ("view", arg))
  223. return 1;
  224. sprintf (syscom, "more %s", arg);
  225. return (system (syscom));
  226. }
  227. com_rename (arg)
  228. char *arg;
  229. {
  230. too_dangerous ("rename");
  231. return (1);
  232. }
  233. com_stat (arg)
  234. char *arg;
  235. {
  236. struct stat finfo;
  237. if (!valid_argument ("stat", arg))
  238. return (1);
  239. if (stat (arg, &finfo) == -1)
  240. {
  241. perror (arg);
  242. return (1);
  243. }
  244. printf ("Statistics for `%s':\n", arg);
  245. printf ("%s has %d link%s, and is %d byte%s in length.\n", arg,
  246. finfo.st_nlink,
  247. (finfo.st_nlink == 1) ? "" : "s",
  248. finfo.st_size,
  249. (finfo.st_size == 1) ? "" : "s");
  250. printf ("Inode Last Change at: %s", ctime (&finfo.st_ctime));
  251. printf (" Last access at: %s", ctime (&finfo.st_atime));
  252. printf (" Last modified at: %s", ctime (&finfo.st_mtime));
  253. return (0);
  254. }
  255. com_delete (arg)
  256. char *arg;
  257. {
  258. too_dangerous ("delete");
  259. return (1);
  260. }
  261. /* Print out help for ARG, or for all of the commands if ARG is
  262. not present. */
  263. com_help (arg)
  264. char *arg;
  265. {
  266. register int i;
  267. int printed = 0;
  268. for (i = 0; commands[i].name; i++)
  269. {
  270. if (!*arg || (strcmp (arg, commands[i].name) == 0))
  271. {
  272. printf ("%s\t\t%s.\n", commands[i].name, commands[i].doc);
  273. printed++;
  274. }
  275. }
  276. if (!printed)
  277. {
  278. printf ("No commands match `%s'. Possibilties are:\n", arg);
  279. for (i = 0; commands[i].name; i++)
  280. {
  281. /* Print in six columns. */
  282. if (printed == 6)
  283. {
  284. printed = 0;
  285. printf ("\n");
  286. }
  287. printf ("%s\t", commands[i].name);
  288. printed++;
  289. }
  290. if (printed)
  291. printf ("\n");
  292. }
  293. return (0);
  294. }
  295. /* Change to the directory ARG. */
  296. com_cd (arg)
  297. char *arg;
  298. {
  299. if (chdir (arg) == -1)
  300. {
  301. perror (arg);
  302. return 1;
  303. }
  304. com_pwd ("");
  305. return (0);
  306. }
  307. /* Print out the current working directory. */
  308. com_pwd (ignore)
  309. char *ignore;
  310. {
  311. char dir[1024], *s;
  312. s = getwd (dir);
  313. if (s == 0)
  314. {
  315. printf ("Error getting pwd: %s\n", dir);
  316. return 1;
  317. }
  318. printf ("Current directory is %s\n", dir);
  319. return 0;
  320. }
  321. /* The user wishes to quit using this program. Just set DONE non-zero. */
  322. com_quit (arg)
  323. char *arg;
  324. {
  325. done = 1;
  326. return (0);
  327. }
  328. /* Function which tells you that you can't do this. */
  329. too_dangerous (caller)
  330. char *caller;
  331. {
  332. fprintf (stderr,
  333. "%s: Too dangerous for me to distribute. Write it yourself.\n",
  334. caller);
  335. }
  336. /* Return non-zero if ARG is a valid argument for CALLER, else print
  337. an error message and return zero. */
  338. int
  339. valid_argument (caller, arg)
  340. char *caller, *arg;
  341. {
  342. if (!arg || !*arg)
  343. {
  344. fprintf (stderr, "%s: Argument required.\n", caller);
  345. return (0);
  346. }
  347. return (1);
  348. }