3 * Copyright 1992 by the Massachusetts Institute of Technology.
5 * For further information on copyright and distribution
6 * see the file mit-copyright.h
9 #include <mit-copyright.h>
12 #include <sys/types.h>
13 #include <sys/signal.h>
19 #include <sys/ioctl.h>
21 #include <X11/Intrinsic.h>
26 static char rcsid[] = "$Header$";
29 struct parse_node *TtyCommands = NULL, *TtyRoot = NULL;
32 extern MenuItem MenuRoot;
33 char prompt[] = "moira> ";
36 static struct termios otty, ntty;
38 static struct sgttyb otty, ntty;
43 char buf[1024], cbuf[2], c, *p;
46 if (TtyCommands == NULL)
50 print_parse_tree(TtyRoot, 0);
58 ntty.c_lflag &= ~(ICANON|ECHO);
62 ioctl(0, TIOCFLUSH, &arg);
63 ioctl(0, TIOCGETP, &otty);
66 ntty.sg_flags &= ~ECHO;
71 parser(prompt, TtyRoot);
78 tcsetattr (0, TCSANOW, &otty);
80 ioctl(0, TIOCSETP, &otty);
87 tcsetattr (0, TCSANOW, &ntty);
89 ioctl(0, TIOCSETP, &ntty);
98 for (ret = 1; *s; s++)
107 struct parse_node *p;
109 TtyCommands = (struct parse_node *)malloc(sizeof(struct parse_node) *
111 memset(TtyCommands, 0, sizeof(struct parse_node) * NumMenus * 3);
113 parse_menu_recursive(&MenuRoot, "", &arg);
114 TtyCommands[arg].p_word = "help";
115 TtyCommands[arg].p_next = TtyRoot;
116 for (p = TtyRoot; p; p = p->p_peer)
117 if (strcmp(p->p_peer->p_word, "help") > 0)
119 TtyCommands[arg].p_peer = p->p_peer->p_peer;
120 p->p_peer = &TtyCommands[arg];
125 parse_menu_recursive(m, parent, i)
130 char buf[64], cmd[64], *word, *s;
131 struct parse_node *p, **prev;
134 if (m->submenu == NULL) {
135 if (!strcmp(parent, "file") ||
136 !strcmp(parent, "misc"))
138 if (NumWords(m->label) > 2 || !*parent)
139 strcpy(cmd, m->label);
141 sprintf(cmd, "%s %s", m->label, parent);
142 /* insert command into parse tree */
144 prev = (struct parse_node **) &TtyRoot;
145 for (word = cmd; word; word = strchr(word, ' ')) {
148 s = strchr(word, ' ');
154 if (!p || !p->p_word ||
155 (j = strncmp(p->p_word, word, len)) > 0) {
159 printf("word: %s\n", buf);
161 TtyCommands[*i].p_word = strsave(buf);
162 if (len == strlen(word))
163 TtyCommands[*i].p_menu = m;
164 TtyCommands[*i].p_peer = p;
166 *prev = &TtyCommands[*i];
167 TtyCommands[*i].p_next = (struct parse_node *) NULL;
168 TtyCommands[*i].p_link = (struct parse_node *) NULL;
169 prev = &(TtyCommands[*i].p_next);
170 p = (struct parse_node *) NULL;
178 p = (struct parse_node *) NULL;
188 if (!strcmp(m->label, "?help")) return;
189 for (j = 0; m->submenu[j]; j++) {
192 else if (strlen(parent))
193 sprintf(buf, "%s %s", parent, m->label);
195 strcpy(buf, m->label);
196 parse_menu_recursive(m->submenu[j], buf, i);
204 int i, j, done, best, choice;
208 printf("%s\r\n", f->instructions);
210 for (i = 0, p = f->inputlines[i]; p = f->inputlines[i]; i++)
218 for (i = 0, p = f->inputlines[i]; p = f->inputlines[i]; i++) {
220 printf(" %s\r\n", buf);
221 sprintf(buf, "%d: %s", ++j, p->prompt);
222 } else if (*buf == 0) {
223 sprintf(buf, "%d: %s", ++j, p->prompt);
226 strcat(buf, p->prompt);
228 if (buf[strlen(buf)-1] == ' ') buf[strlen(buf)-1] = 0;
230 printf(" %s\r\nSelect by [1]: ", buf);
231 if (mgets(buf, sizeof(buf)))
233 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
234 if (strlen(buf) == 0)
239 for (i = 0, p = f->inputlines[i]; p = f->inputlines[i]; i++) {
240 if (choice > 0 && p->choice) choice--;
241 if (choice != -1 && choice != 1) continue;
242 if (p->insensitive == True) continue;
248 printf("%s(T/F) [%c]: ", p->prompt, boolval(f, i) ? 'T' : 'F');
250 if (mgets(buf, sizeof(buf)))
252 if (buf[0] == 'T' || buf[0] == 't' ||
253 buf[0] == 'Y' || buf[0] == 'y')
254 p->returnvalue.booleanvalue = True;
255 else if (buf[0] == 'F' || buf[0] == 'f' ||
256 buf[0] == 'N' || buf[0] == 'n')
257 p->returnvalue.booleanvalue = False;
258 else if (buf[0] != 0) {
260 printf("Please answer True or False\r\n");
265 if (*StringValue(f, i))
266 printf("%s[%s]: ", p->prompt, StringValue(f, i));
268 printf("%s: ", p->prompt);
270 if (mgets(buf, sizeof(buf)))
272 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
273 if (strlen(buf) != 0)
274 StoreField(f, i, buf);
275 if (!strcmp(buf, "\"\""))
276 StoreField(f, i, "");
279 k = strchr(p->prompt, '|');
283 if (*StringValue(f, i))
284 printf("%s[%s]: ", p->prompt, StringValue(f, i));
286 printf("%s: ", p->prompt);
288 if (mgets(buf, sizeof(buf)))
290 if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
291 if (strlen(buf) == 0)
292 strcpy(buf, StringValue(f, i));
294 for (j = 0; p->keywords[j]; j++)
295 if (!strncasecmp(p->keywords[j], buf, strlen(buf)))
298 printf("You must enter one of these keywords:\r\n");
299 for (j = 0; p->keywords[j]; j++) {
300 printf(" %s\r\n", p->keywords[j]);
303 StoreField(f, i, p->keywords[best]);
308 process_form(f, TRUE);
312 int mgets(line, linelen)
321 for (c = (getchar() & 0x7f); 1; c = (getchar() & 0x7f)) {
322 if (c == 0) continue;
331 write(1, "\b \b", 3);
342 printf("\b^%c", c + '@');
351 while ((p >= &line[0]) && isspace(*p)) {
352 write(1, "\b \b", 3);
355 while ((p >= &line[0]) && !isspace(*p)) {
356 write(1, "\b \b", 3);
368 kill(getpid(), SIGSTOP);
369 /* when continued, fall through to */
372 printf("\r\n%s", line);
376 while (p-- > &line[0])
377 write(1, "\b \b", 3);
396 print_parse_tree(n, l)
397 struct parse_node *n;
402 for (i = l; i > 0; i--) putchar(' ');
403 printf("%s%c\n", n->p_word, n->p_menu ? '*' : ' ');
405 print_parse_tree(n->p_next, l+1);
407 return(print_parse_tree(n->p_peer, l));