command.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* Library for reading command lines and decoding commands.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. NO WARRANTY
  4. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  5. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  6. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  7. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  8. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  9. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  10. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  11. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  12. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  13. CORRECTION.
  14. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  15. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  16. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  17. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  18. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  19. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  20. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  21. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  22. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  23. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  24. GENERAL PUBLIC LICENSE TO COPY
  25. 1. You may copy and distribute verbatim copies of this source file
  26. as you receive it, in any medium, provided that you conspicuously and
  27. appropriately publish on each copy a valid copyright notice "Copyright
  28. (C) 1986 Free Software Foundation, Inc."; and include following the
  29. copyright notice a verbatim copy of the above disclaimer of warranty
  30. and of this License. You may charge a distribution fee for the
  31. physical act of transferring a copy.
  32. 2. You may modify your copy or copies of this source file or
  33. any portion of it, and copy and distribute such modifications under
  34. the terms of Paragraph 1 above, provided that you also do the following:
  35. a) cause the modified files to carry prominent notices stating
  36. that you changed the files and the date of any change; and
  37. b) cause the whole of any work that you distribute or publish,
  38. that in whole or in part contains or is a derivative of this
  39. program or any part thereof, to be licensed at no charge to all
  40. third parties on terms identical to those contained in this
  41. License Agreement (except that you may choose to grant more
  42. extensive warranty protection to third parties, at your option).
  43. c) You may charge a distribution fee for the physical act of
  44. transferring a copy, and you may at your option offer warranty
  45. protection in exchange for a fee.
  46. 3. You may copy and distribute this program or any portion of it in
  47. compiled, executable or object code form under the terms of Paragraphs
  48. 1 and 2 above provided that you do the following:
  49. a) cause each such copy to be accompanied by the
  50. corresponding machine-readable source code, which must
  51. be distributed under the terms of Paragraphs 1 and 2 above; or,
  52. b) cause each such copy to be accompanied by a
  53. written offer, with no time limit, to give any third party
  54. free (except for a nominal shipping charge) a machine readable
  55. copy of the corresponding source code, to be distributed
  56. under the terms of Paragraphs 1 and 2 above; or,
  57. c) in the case of a recipient of this program in compiled, executable
  58. or object code form (without the corresponding source code) you
  59. shall cause copies you distribute to be accompanied by a copy
  60. of the written offer of source code which you received along
  61. with the copy you received.
  62. 4. You may not copy, sublicense, distribute or transfer this program
  63. except as expressly provided under this License Agreement. Any attempt
  64. otherwise to copy, sublicense, distribute or transfer this program is void and
  65. your rights to use the program under this License agreement shall be
  66. automatically terminated. However, parties who have received computer
  67. software programs from you with this License Agreement will not have
  68. their licenses terminated so long as such parties remain in full compliance.
  69. 5. If you wish to incorporate parts of this program into other free
  70. programs whose distribution conditions are different, write to the Free
  71. Software Foundation at 1000 Mass Ave, Cambridge, MA 02138. We have not yet
  72. worked out a simple rule that can be stated here, but we will often permit
  73. this. We will be guided by the two goals of preserving the free status of
  74. all derivatives our free software and of promoting the sharing and reuse of
  75. software.
  76. In other words, you are welcome to use, share and improve this program.
  77. You are forbidden to forbid anyone else to use, share and improve
  78. what you give them. Help stamp out software-hoarding! */
  79. #include "command.h"
  80. #include <stdio.h>
  81. static char *savestring ();
  82. /* Add element named NAME to command list *LIST.
  83. FUN should be the function to execute the command;
  84. it will get a character string as argument, with leading
  85. and trailing blanks already eliminated.
  86. DOC is a documentation string for the command.
  87. Its first line should be a complete sentence.
  88. It should start with ? for a command that is an abbreviation
  89. or with * for a command that most users don't need to know about. */
  90. struct cmd_list_element *
  91. add_cmd (name, class, fun, doc, list)
  92. char *name;
  93. int class;
  94. void (*fun) ();
  95. char *doc;
  96. struct cmd_list_element **list;
  97. {
  98. register struct cmd_list_element *c = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
  99. delete_cmd (name, list);
  100. c->next = *list;
  101. c->name = savestring (name, strlen (name));
  102. c->class = class;
  103. c->function = fun;
  104. c->doc = doc;
  105. c->prefixlist = 0;
  106. c->allow_unknown = 0;
  107. c->abbrev_flag = 0;
  108. c->aux = 0;
  109. *list = c;
  110. return c;
  111. }
  112. struct cmd_list_element *
  113. add_alias_cmd (name, oldname, class, abbrev_flag, list)
  114. char *name;
  115. char *oldname;
  116. int class;
  117. int abbrev_flag;
  118. struct cmd_list_element **list;
  119. {
  120. register struct cmd_list_element *old
  121. = lookup_cmd (&oldname, *list, 0, 1);
  122. register struct cmd_list_element *c;
  123. if (old == 0)
  124. {
  125. delete_cmd (name, list);
  126. return 0;
  127. }
  128. c = add_cmd (name, class, old->function, old->doc, list);
  129. c->prefixlist = old->prefixlist;
  130. c->prefixname = old->prefixname;
  131. c->allow_unknown = old->allow_unknown;
  132. c->abbrev_flag = abbrev_flag;
  133. c->aux = old->aux;
  134. return c;
  135. }
  136. /* Like add_prefix_cmd but adds an element for a command prefix:
  137. a name that should be followed by a subcommand to be looked up
  138. in another command list. PREFIXLIST should be the address
  139. of the variable containing that list. */
  140. struct cmd_list_element *
  141. add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
  142. allow_unknown, list)
  143. char *name;
  144. int class;
  145. void (*fun) ();
  146. char *doc;
  147. struct cmd_list_element **prefixlist;
  148. char *prefixname;
  149. int allow_unknown;
  150. struct cmd_list_element **list;
  151. {
  152. register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
  153. c->prefixlist = prefixlist;
  154. c->prefixname = prefixname;
  155. c->allow_unknown = allow_unknown;
  156. return c;
  157. }
  158. /* Remove the command named NAME from the command list. */
  159. void
  160. delete_cmd (name, list)
  161. char *name;
  162. struct cmd_list_element **list;
  163. {
  164. register struct cmd_list_element *c;
  165. while (*list && !strcmp ((*list)->name, name))
  166. {
  167. *list = (*list)->next;
  168. }
  169. if (*list)
  170. for (c = *list; c->next;)
  171. {
  172. if (!strcmp (c->next->name, name))
  173. c->next = c->next->next;
  174. else
  175. c = c->next;
  176. }
  177. }
  178. /* Implement a help command on command list LIST.
  179. COMMAND is the argument given (a command from the list to document)
  180. or zero for no arg (describe briefly all the commands in the list).
  181. CMDTYPE is a string to use in the error message if command COMMAND
  182. is not found in the list. */
  183. /* CLASS should be -1 to list all commands in LIST,
  184. or a nonnegative class number value to list just commands in that class,
  185. or -2 to list the classes themselves. */
  186. void
  187. help_cmd (command, list, cmdtype, class, stream)
  188. char *command;
  189. struct cmd_list_element *list;
  190. char *cmdtype;
  191. int class;
  192. FILE *stream;
  193. {
  194. register struct cmd_list_element *c;
  195. register char *p;
  196. register int ncmds;
  197. struct cmdvec { struct cmd_list_element *cmd; int class; };
  198. register struct cmdvec *cmdvec;
  199. char *cmdtype1, *cmdtype2;
  200. int len;
  201. if (command)
  202. {
  203. c = lookup_cmd (&command, list, cmdtype, 0);
  204. if (c == 0)
  205. return;
  206. /* There are three cases here.
  207. If c->prefixlist is nonzer, we have a prefix command.
  208. Print its documentation, then list its subcommands.
  209. If c->function is nonzero, we really have a command.
  210. Print its documentation and return.
  211. If c->function is zero, we have a class name.
  212. Print its documentation (as if it were a command)
  213. and then set class to he number of this class
  214. so that the commands in the class will be listed. */
  215. p = c->doc;
  216. fprintf (stream, "%s\n", p);
  217. if (c->function != 0 && c->prefixlist == 0)
  218. return;
  219. fputc ('\n', stream);
  220. if (c->prefixlist)
  221. {
  222. list = *c->prefixlist;
  223. class = 0;
  224. cmdtype = c->prefixname;
  225. }
  226. else
  227. class = c->class;
  228. }
  229. /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
  230. len = strlen (cmdtype);
  231. cmdtype1 = (char *) alloca (len + 1);
  232. cmdtype1[0] = 0;
  233. cmdtype2 = (char *) alloca (len + 4);
  234. cmdtype2[0] = 0;
  235. if (len)
  236. {
  237. cmdtype1[0] = ' ';
  238. strncpy (cmdtype1 + 1, cmdtype, len - 1);
  239. cmdtype1[len] = 0;
  240. strncpy (cmdtype2, cmdtype, len - 1);
  241. strcpy (cmdtype2 + len - 1, " sub");
  242. }
  243. if (class == -2)
  244. fprintf (stream, "List of classes of %scommands:\n\n", cmdtype2);
  245. else
  246. fprintf (stream, "List of %scommands:\n\n", cmdtype2);
  247. for (c = list; c; c = c->next)
  248. {
  249. if (c->abbrev_flag == 0
  250. && (class == -1 /* Listing all */
  251. || (c->class == class && c->function != 0) /* Listing one class */
  252. || (class == -2 && c->function == 0))) /* Listing the classes */
  253. {
  254. fprintf (stream, "%s -- ", c->name);
  255. /* Print just first line of documentation. */
  256. p = c->doc;
  257. while (*p && *p != '\n') p++;
  258. fwrite (c->doc, 1, p - c->doc, stream);
  259. fputc ('\n', stream);
  260. }
  261. }
  262. if (class == -2)
  263. fprintf (stream, "\n\
  264. Type \"help%s\" followed by a class name for a list of commands in that class.",
  265. cmdtype1);
  266. fprintf (stream, "\n\
  267. Type \"help%s\" followed by %scommand name for full documentation.\n\
  268. Command name abbreviations are allowed if unambiguous.\n",
  269. cmdtype1, cmdtype2);
  270. }
  271. /* Look up the contents of *LINE as a command in the command list LIST.
  272. LIST is a chain of struct cmd_list_element's.
  273. If it is found, return the struct cmd_list_element for that command
  274. and update *LINE to point after the command name, at the first argument.
  275. If not found, call error if ALLOW_UNKNOWN is zero
  276. otherwise (or if error returns) return zero.
  277. Call error if specified command is ambiguous,
  278. unless ALLOW_UNKNOWN is negative.
  279. CMDTYPE precedes the word "command" in the error message. */
  280. struct cmd_list_element *
  281. lookup_cmd (line, list, cmdtype, allow_unknown)
  282. char **line;
  283. struct cmd_list_element *list;
  284. char *cmdtype;
  285. int allow_unknown;
  286. {
  287. register char *p;
  288. register struct cmd_list_element *c, *found;
  289. int nfound;
  290. char ambbuf[100];
  291. /* Skip leading whitespace. */
  292. while (**line == ' ' || **line == '\t')
  293. (*line)++;
  294. /* Clear out trailing whitespace. */
  295. p = *line + strlen (*line);
  296. while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
  297. p--;
  298. *p = 0;
  299. /* Find end of command name. */
  300. p = *line;
  301. while (*p == '-'
  302. || (*p >= 'a' && *p <= 'z')
  303. || (*p >= 'A' && *p <= 'Z')
  304. || (*p >= '1' && *p <= '9'))
  305. {
  306. if (*p >= 'A' && *p <= 'Z')
  307. *p += 'a' - 'A';
  308. p++;
  309. }
  310. /* Look up the command name.
  311. If exact match, keep that.
  312. Otherwise, take command abbreviated, if unique. */
  313. found = 0;
  314. nfound = 0;
  315. for (c = list; c; c = c->next)
  316. {
  317. if (!strncmp (*line, c->name, p - *line))
  318. {
  319. found = c;
  320. nfound++;
  321. if (c->name[p - *line] == 0)
  322. {
  323. nfound = 1;
  324. break;
  325. }
  326. }
  327. }
  328. /* Report error for undefined command name. */
  329. if (nfound != 1)
  330. {
  331. if (nfound > 1 && allow_unknown >= 0)
  332. {
  333. *p = 0;
  334. ambbuf[0] = 0;
  335. for (c = list; c; c = c->next)
  336. if (!strncmp (*line, c->name, p - *line))
  337. {
  338. if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
  339. {
  340. if (strlen (ambbuf))
  341. strcat (ambbuf, ", ");
  342. strcat (ambbuf, c->name);
  343. }
  344. else
  345. {
  346. strcat (ambbuf, "..");
  347. break;
  348. }
  349. }
  350. error ("Ambiguous %scommand \"%s\": %s.", cmdtype, *line, ambbuf);
  351. }
  352. else if (!allow_unknown)
  353. {
  354. *p = 0;
  355. error ("Undefined %scommand: \"%s\".", cmdtype, *line);
  356. }
  357. return 0;
  358. }
  359. /* Skip whitespace before the argument. */
  360. while (*p == ' ' || *p == '\t') p++;
  361. *line = p;
  362. if (found->prefixlist && *p)
  363. {
  364. c = lookup_cmd (line, *found->prefixlist, found->prefixname,
  365. found->allow_unknown);
  366. if (c)
  367. return c;
  368. }
  369. return found;
  370. }
  371. /* Make a copy of the string at PTR with SIZE characters
  372. (and add a null character at the end in the copy).
  373. Uses malloc to get the space. Returns the address of the copy. */
  374. static char *
  375. savestring (ptr, size)
  376. char *ptr;
  377. int size;
  378. {
  379. register char *p = (char *) xmalloc (size + 1);
  380. bcopy (ptr, p, size);
  381. p[size] = 0;
  382. return p;
  383. }