PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/freebsd/contrib/libreadline/examples/fileman.c

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