3 * TOPS-20 style command parser
9 #include <sys/signal.h>
10 #include <X11/Intrinsic.h>
15 /* trivial absolute-value macro */
16 #define abs(i) ((i) < 0 ? -(i) : (i))
18 /* message used when end of parse tree is reached */
19 char eolmsg[] = "return to execute command";
22 /* main entry point. Takes the prompt and top node of the parse tree, and
23 * will collect user's input, providing help and completion. When entry
24 * is complete (<return> is pressed), the actions called for by each node
25 * in the parse tree which is traversed will be executed.
28 int parser(prompt, nd)
30 struct parse_node *nd;
38 write(1, prompt, strlen(prompt));
39 for (c = (getchar() & 0x7F); 1; c = (getchar() & 0x7F)) {
49 if (*(--p) == 'I' - '@') {
51 printf("\r%s%s", prompt, line);
60 write(1, " \007ABORT\r\n", 9);
67 printf("\b^%c", c + '@');
76 while ((p >= &line[0]) && isspace(*p)) {
80 while ((p >= &line[0]) && !isspace(*p)) {
94 do_help(&line[0], nd, prompt);
95 p = &line[strlen(line)];
96 printf("\n\r%s%s", prompt, line);
102 do_complete(&line[0], nd, prompt);
103 p = &line[strlen(line)];
107 kill(getpid(), SIGSTOP);
108 /* when continued, fall through to */
111 printf("\r\n%s%s", prompt, line);
115 while (p-- > &line[0])
116 write(1, "\b \b", 3);
118 printf("\r%s", prompt);
123 if ((val = do_parse(line, nd, prompt)) != ERROR) {
127 p = &line[strlen(line)];
129 printf("\r\n%s%s", prompt, line);
141 /* called when a ? is typed. This parses the line as far as possible, then
142 * displays possible completions and help strings.
145 do_help(line, nod, prompt)
147 struct parse_node *nod;
151 struct parse_node *n, *nd, *last;
152 int kw, state, nomatch;
153 struct parse_node *best, *ambig;
157 for (ln = line; n && state == MATCH; best && (n = best->p_next)) {
159 state = single_parse(&ln, n, &best, &ambig, &nomatch);
161 if (*ln && ((best && best->p_menu) || n == NULNODE))
166 if (last && last->p_menu && *line)
167 printf("Carriage return, or ");
172 write(1, "\r\nNOT a valid command line", 26);
175 write(1, "one of the following:\r\n ", 26);
176 for (; ambig; ambig = ambig->p_link)
177 printf("%s ", ambig->p_word);
181 printf("one of the following:\r\n %s", best->p_word);
185 write(1, eolmsg, strlen(eolmsg));
189 for (nd = n; nd; nd = nd->p_peer) {
191 write(1, "one of the following:\r\n ", 26);
194 if (kw + strlen(nd->p_word) > 72) {
195 printf("\r\n %s ", nd->p_word);
197 kw = 4 + strlen(nd->p_word);
199 printf("%s ", nd->p_word);
201 kw += 1 + strlen(nd->p_word);
207 /* Do escape processing. If a unique completion exists, use it. Otherwise,
211 do_complete(line, nod, prompt)
213 struct parse_node *nod;
216 struct parse_node *n, *nd;
220 struct parse_node *best, *ambig;
225 while (state == MATCH) {
227 state = single_parse(&ln, nd, &best, &ambig, &nomatch);
233 /* printf("ln %X, tmp %X, ln - tmp %d, nomatch %d\r\n", ln, tmp, ln-tmp, nomatch);
234 if (ln - tmp < nomatch) {
235 printf("attempting partial complete\r\n");
238 while ((ln > line) && !isspace(*ln))
247 /* fall through to: */
249 if (!(nd) || (nd->p_peer)) {
251 do_help(line, nod, prompt);
252 printf("\r\n%s%s", prompt, line);
258 /* fall through to incomplete case */
267 } while (best->p_next && !best->p_next->p_peer &&
268 !best->p_menu && (best = best->p_next));
269 printf("\r%s%s", prompt, line);
273 write(1, "We shouldn't get here (parser error)\r\n", 38);
278 /* Single parse parses through a single level of the parse tree.
279 * There are 4 possible outcomes:
280 * an exact match is made: the matching node is returned, ambig = 0
281 * an incomplete match: the matching node is returned, ambig = node
282 * ambiguous: nothing is returned, ambig = list of completions
283 * no matches: nothing is returned, ambig = 0
286 int single_parse(line, nd, best, ambig, nomatch)
288 struct parse_node *nd;
289 struct parse_node **best;
290 struct parse_node **ambig;
294 char c; /* char we're working on (from line) */
295 struct parse_node *n; /* node loop counter */
296 struct parse_node *tail; /* tmp used to build chains */
297 int i; /* loop counter */
298 int match; /* how many chars have we matched? */
299 int len; /* length of this keyword */
302 printf("single_parse(\"%s\") -> ", *line);
304 *ambig = tail = *best = NULNODE;
305 match = *nomatch = 0;
306 /* skip leading whitespace */
307 while (isspace(**line))
309 /* step through each node */
310 for (n = nd; n; n = n->p_peer) {
311 len = strlen(n->p_word);
312 /* step through each character in line */
313 for (i = 0; 1; i++) {
314 /* if at end of word on line */
315 if (isspace((*line)[i]) || (*line)[i] == 0) {
316 /* another ambiguous match */
317 if (i == match && i) {
325 *best = tail = *ambig = n;
330 if (isupper(c = (*line)[i]))
332 if (c != n->p_word[i]) {
341 if (tail != *ambig) {
344 if (isspace(**line)) {
356 if (isspace(**line)) {
358 while (isspace(**line))
370 *ambig = tail = *best = NULNODE;
378 /* execute the line. First check to see that the line is legal. If not,
379 * do_help the line & return ERROR. If so, execute each node passed through,
380 * and return OK (or EXIT if an EXIT node was encountered).
383 int do_parse(line, nod, prompt)
385 struct parse_node *nod;
388 struct parse_node *n, *nd, *last;
391 struct parse_node *best, *ambig;
399 while (n && ((state == MATCH) || (state == INCOMP))) {
401 state = single_parse(&ln, n, &best, &ambig, &nomatch);
403 printf("best = %s, best->next = 0x%x\r\n", best ? best->p_word : "",
404 best ? best->p_next : 0);
406 if ((state == NOMATCH) && (!*ln) && (n == nod))
408 if ((state == MATCH) || (state == INCOMP)) {
412 if (((state == AMBIG) || (state == NOMATCH)) && !*ln)
413 for (; n; n = n->p_peer)
414 if (last && last->p_menu) {
419 if (state == NOMATCH && !*ln) {
420 while (last && last->p_next && !last->p_next->p_peer && !last->p_menu)
422 if (last && last->p_menu) {
428 if ((state == NOMATCH) || (state == AMBIG)) {
429 write(1, " BAD command, ", 16);
430 do_help(line, nod, prompt);
437 for (i = 0; line[i] && !isspace(line[i]); i++);
438 if (!strncmp("help", line, i))
439 help(best->p_menu->form);
441 MoiraMenuRequest(best->p_menu);