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)];
108 kill(getpid(), SIGSTOP);
110 /* when continued, fall through to */
113 printf("\r\n%s%s", prompt, line);
117 while (p-- > &line[0])
118 write(1, "\b \b", 3);
120 printf("\r%s", prompt);
125 if ((val = do_parse(line, nd, prompt)) != ERROR) {
129 p = &line[strlen(line)];
131 printf("\r\n%s%s", prompt, line);
143 /* called when a ? is typed. This parses the line as far as possible, then
144 * displays possible completions and help strings.
147 do_help(line, nod, prompt)
149 struct parse_node *nod;
153 struct parse_node *n, *nd, *last;
154 int kw, state, nomatch;
155 struct parse_node *best, *ambig;
160 for (ln = line; n && state == MATCH; best && (n = best->p_next)) {
162 state = single_parse(&ln, n, &best, &ambig, &nomatch);
164 if (*ln && ((best && best->p_menu) || n == NULNODE))
169 if (last && last->p_menu && *line)
170 printf("Carriage return, or ");
175 write(1, "\r\nNOT a valid command line", 26);
178 write(1, "one of the following:\r\n ", 26);
179 for (; ambig; ambig = ambig->p_link)
180 printf("%s ", ambig->p_word);
184 printf("one of the following:\r\n %s", best->p_word);
188 write(1, eolmsg, strlen(eolmsg));
192 for (nd = n; nd; nd = nd->p_peer) {
194 write(1, "one of the following:\r\n ", 26);
197 if (kw + strlen(nd->p_word) > 72) {
198 printf("\r\n %s ", nd->p_word);
200 kw = 4 + strlen(nd->p_word);
202 printf("%s ", nd->p_word);
204 kw += 1 + strlen(nd->p_word);
210 /* Do escape processing. If a unique completion exists, use it. Otherwise,
214 do_complete(line, nod, prompt)
216 struct parse_node *nod;
219 struct parse_node *n, *nd;
223 struct parse_node *best, *ambig;
228 while (state == MATCH) {
230 state = single_parse(&ln, nd, &best, &ambig, &nomatch);
236 /* printf("ln %X, tmp %X, ln - tmp %d, nomatch %d\r\n", ln, tmp, ln-tmp, nomatch);
237 if (ln - tmp < nomatch) {
238 printf("attempting partial complete\r\n");
241 while ((ln > line) && !isspace(*ln))
250 /* fall through to: */
252 if (!(nd) || (nd->p_peer)) {
254 do_help(line, nod, prompt);
255 printf("\r\n%s%s", prompt, line);
261 /* fall through to incomplete case */
270 } while (best->p_next && !best->p_next->p_peer &&
271 !best->p_menu && (best = best->p_next));
272 printf("\r%s%s", prompt, line);
276 write(1, "We shouldn't get here (parser error)\r\n", 38);
281 /* Single parse parses through a single level of the parse tree.
282 * There are 4 possible outcomes:
283 * an exact match is made: the matching node is returned, ambig = 0
284 * an incomplete match: the matching node is returned, ambig = node
285 * ambiguous: nothing is returned, ambig = list of completions
286 * no matches: nothing is returned, ambig = 0
289 int single_parse(line, nd, best, ambig, nomatch)
291 struct parse_node *nd;
292 struct parse_node **best;
293 struct parse_node **ambig;
297 char c; /* char we're working on (from line) */
298 struct parse_node *n; /* node loop counter */
299 struct parse_node *tail; /* tmp used to build chains */
300 int i; /* loop counter */
301 int match; /* how many chars have we matched? */
302 int len; /* length of this keyword */
305 printf("single_parse(\"%s\") -> ", *line);
307 *ambig = tail = *best = NULNODE;
308 match = *nomatch = 0;
309 /* skip leading whitespace */
310 while (isspace(**line))
312 /* step through each node */
313 for (n = nd; n; n = n->p_peer) {
314 len = strlen(n->p_word);
315 /* step through each character in line */
316 for (i = 0; 1; i++) {
317 /* if at end of word on line */
318 if (isspace((*line)[i]) || (*line)[i] == 0) {
319 /* another ambiguous match */
320 if (i == match && i) {
328 *best = tail = *ambig = n;
333 if (isupper(c = (*line)[i]))
335 if (c != n->p_word[i]) {
344 if (tail != *ambig) {
347 if (isspace(**line)) {
359 if (isspace(**line)) {
361 while (isspace(**line))
373 *ambig = tail = *best = NULNODE;
381 /* execute the line. First check to see that the line is legal. If not,
382 * do_help the line & return ERROR. If so, execute each node passed through,
383 * and return OK (or EXIT if an EXIT node was encountered).
386 int do_parse(line, nod, prompt)
388 struct parse_node *nod;
391 struct parse_node *n, *nd, *last;
394 struct parse_node *best, *ambig;
402 while (n && ((state == MATCH) || (state == INCOMP))) {
404 state = single_parse(&ln, n, &best, &ambig, &nomatch);
406 printf("best = %s, best->next = 0x%x\r\n", best ? best->p_word : "",
407 best ? best->p_next : 0);
409 if ((state == NOMATCH) && (!*ln) && (n == nod))
411 if ((state == MATCH) || (state == INCOMP)) {
415 if (((state == AMBIG) || (state == NOMATCH)) && !*ln)
416 for (; n; n = n->p_peer)
417 if (last && last->p_menu) {
422 if (state == NOMATCH && !*ln) {
423 while (last && last->p_next && !last->p_next->p_peer && !last->p_menu)
425 if (last && last->p_menu) {
431 if ((state == NOMATCH) || (state == AMBIG)) {
432 write(1, " BAD command, ", 16);
433 do_help(line, nod, prompt);
440 for (i = 0; line[i] && !isspace(line[i]); i++);
441 if (!strncmp("help", line, i))
442 help(best->p_menu->form);
444 MoiraMenuRequest(best->p_menu);