parse.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* $NetBSD: parse.c,v 1.20 2003/12/05 13:37:48 lukem Exp $ */
  2. /*-
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Christos Zoulas of Cornell University.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include "config.h"
  34. #if !defined(lint) && !defined(SCCSID)
  35. #if 0
  36. static char sccsid[] = "@(#)parse.c 8.1 (Berkeley) 6/4/93";
  37. #else
  38. __RCSID("$NetBSD: parse.c,v 1.20 2003/12/05 13:37:48 lukem Exp $");
  39. #endif
  40. #endif /* not lint && not SCCSID */
  41. /*
  42. * parse.c: parse an editline extended command
  43. *
  44. * commands are:
  45. *
  46. * bind
  47. * echotc
  48. * edit
  49. * gettc
  50. * history
  51. * settc
  52. * setty
  53. */
  54. #include "el.h"
  55. #include <stdlib.h>
  56. private const struct {
  57. const char *name;
  58. int (*func)(EditLine *, int, const char **);
  59. } cmds[] = {
  60. { "bind", map_bind },
  61. { "echotc", term_echotc },
  62. { "edit", el_editmode },
  63. { "history", hist_command },
  64. { "telltc", term_telltc },
  65. { "settc", term_settc },
  66. { "setty", tty_stty },
  67. { NULL, NULL }
  68. };
  69. /* parse_line():
  70. * Parse a line and dispatch it
  71. */
  72. protected int
  73. parse_line(EditLine *el, const char *line)
  74. {
  75. const char **argv;
  76. int argc;
  77. Tokenizer *tok;
  78. tok = tok_init(NULL);
  79. tok_str(tok, line, &argc, &argv);
  80. argc = el_parse(el, argc, argv);
  81. tok_end(tok);
  82. return (argc);
  83. }
  84. /* el_parse():
  85. * Command dispatcher
  86. */
  87. public int
  88. el_parse(EditLine *el, int argc, const char *argv[])
  89. {
  90. const char *ptr;
  91. int i;
  92. if (argc < 1)
  93. return (-1);
  94. ptr = strchr(argv[0], ':');
  95. if (ptr != NULL) {
  96. char *tprog;
  97. size_t l;
  98. if (ptr == argv[0])
  99. return (0);
  100. l = ptr - argv[0] - 1;
  101. tprog = (char *) el_malloc(l + 1);
  102. if (tprog == NULL)
  103. return (0);
  104. (void) strncpy(tprog, argv[0], l);
  105. tprog[l] = '\0';
  106. ptr++;
  107. l = el_match(el->el_prog, tprog);
  108. el_free(tprog);
  109. if (!l)
  110. return (0);
  111. } else
  112. ptr = argv[0];
  113. for (i = 0; cmds[i].name != NULL; i++)
  114. if (strcmp(cmds[i].name, ptr) == 0) {
  115. i = (*cmds[i].func) (el, argc, argv);
  116. return (-i);
  117. }
  118. return (-1);
  119. }
  120. /* parse__escape():
  121. * Parse a string of the form ^<char> \<odigit> \<char> and return
  122. * the appropriate character or -1 if the escape is not valid
  123. */
  124. protected int
  125. parse__escape(const char **const ptr)
  126. {
  127. const char *p;
  128. int c;
  129. p = *ptr;
  130. if (p[1] == 0)
  131. return (-1);
  132. if (*p == '\\') {
  133. p++;
  134. switch (*p) {
  135. case 'a':
  136. c = '\007'; /* Bell */
  137. break;
  138. case 'b':
  139. c = '\010'; /* Backspace */
  140. break;
  141. case 't':
  142. c = '\011'; /* Horizontal Tab */
  143. break;
  144. case 'n':
  145. c = '\012'; /* New Line */
  146. break;
  147. case 'v':
  148. c = '\013'; /* Vertical Tab */
  149. break;
  150. case 'f':
  151. c = '\014'; /* Form Feed */
  152. break;
  153. case 'r':
  154. c = '\015'; /* Carriage Return */
  155. break;
  156. case 'e':
  157. c = '\033'; /* Escape */
  158. break;
  159. case '0':
  160. case '1':
  161. case '2':
  162. case '3':
  163. case '4':
  164. case '5':
  165. case '6':
  166. case '7':
  167. {
  168. int cnt, ch;
  169. for (cnt = 0, c = 0; cnt < 3; cnt++) {
  170. ch = *p++;
  171. if (ch < '0' || ch > '7') {
  172. p--;
  173. break;
  174. }
  175. c = (c << 3) | (ch - '0');
  176. }
  177. if ((c & 0xffffff00) != 0)
  178. return (-1);
  179. --p;
  180. break;
  181. }
  182. default:
  183. c = *p;
  184. break;
  185. }
  186. } else if (*p == '^') {
  187. p++;
  188. c = (*p == '?') ? '\177' : (*p & 0237);
  189. } else
  190. c = *p;
  191. *ptr = ++p;
  192. return (c);
  193. }
  194. /* parse__string():
  195. * Parse the escapes from in and put the raw string out
  196. */
  197. protected char *
  198. parse__string(char *out, const char *in)
  199. {
  200. char *rv = out;
  201. int n;
  202. for (;;)
  203. switch (*in) {
  204. case '\0':
  205. *out = '\0';
  206. return (rv);
  207. case '\\':
  208. case '^':
  209. if ((n = parse__escape(&in)) == -1)
  210. return (NULL);
  211. *out++ = n;
  212. break;
  213. case 'M':
  214. if (in[1] == '-' && in[2] != '\0') {
  215. *out++ = '\033';
  216. in += 2;
  217. break;
  218. }
  219. /*FALLTHROUGH*/
  220. default:
  221. *out++ = *in++;
  222. break;
  223. }
  224. }
  225. /* parse_cmd():
  226. * Return the command number for the command string given
  227. * or -1 if one is not found
  228. */
  229. protected int
  230. parse_cmd(EditLine *el, const char *cmd)
  231. {
  232. el_bindings_t *b;
  233. for (b = el->el_map.help; b->name != NULL; b++)
  234. if (strcmp(b->name, cmd) == 0)
  235. return (b->func);
  236. return (-1);
  237. }