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