comparison sw/mcsi-rxtx/usercmd.c @ 7:8a386263dd51

fc-mcsi-rxtx skeleton put together
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 28 Oct 2024 01:44:28 +0000
parents
children c1d9b5d128f5
comparison
equal deleted inserted replaced
6:a10657f8024e 7:8a386263dd51
1 /*
2 * This module implements fc-mcsi-rxtx user command dispatch.
3 */
4
5 #include <sys/types.h>
6 #include <ctype.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <strings.h>
10 #include <stdlib.h>
11
12 extern char usercmd[];
13
14 static void
15 cmd_exit()
16 {
17 tty_cleanup();
18 exit(0);
19 }
20
21 static struct cmdtab {
22 char *cmd;
23 int minargs;
24 int maxargs;
25 void (*func)();
26 } cmdtab[] = {
27 {"exit", 0, 0, cmd_exit},
28 {"quit", 0, 0, cmd_exit},
29 {0, 0, 0, 0}
30 };
31
32 void
33 dispatch_user_cmd()
34 {
35 char *argv[10];
36 char *cp, **ap;
37 struct cmdtab *tp;
38
39 for (cp = usercmd; isspace(*cp); cp++)
40 ;
41 if (!*cp || *cp == '#')
42 return;
43 argv[0] = cp;
44 while (*cp && !isspace(*cp))
45 cp++;
46 if (*cp)
47 *cp++ = '\0';
48 for (tp = cmdtab; tp->cmd; tp++)
49 if (!strcmp(tp->cmd, argv[0]))
50 break;
51 if (!tp->func) {
52 printf("error: no such command\n");
53 return;
54 }
55 for (ap = argv + 1; ; ) {
56 while (isspace(*cp))
57 cp++;
58 if (!*cp || *cp == '#')
59 break;
60 if (ap - argv - 1 >= tp->maxargs) {
61 printf("error: too many arguments\n");
62 return;
63 }
64 if (*cp == '"') {
65 *ap++ = ++cp;
66 while (*cp && *cp != '"')
67 cp++;
68 if (*cp != '"') {
69 printf("error: unterminated quoted string\n");
70 return;
71 }
72 *cp++ = '\0';
73 } else {
74 *ap++ = cp;
75 while (*cp && !isspace(*cp))
76 cp++;
77 if (*cp)
78 *cp++ = '\0';
79 }
80 }
81 if (ap - argv - 1 < tp->minargs) {
82 printf("error: too few arguments\n");
83 return;
84 }
85 *ap = 0;
86 tp->func(ap - argv, argv);
87 }