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