annotate libcommon/dispatch.c @ 78:f6d5cff989d6

fc-uicc-tool batch stdin mode implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 09 Apr 2021 02:47:21 +0000
parents b7ee2e85686b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
14
b7ee2e85686b command dispatch and scripting factored out into libcommon
Mychaela Falconia <falcon@freecalypso.org>
parents: 10
diff changeset
2 * This module implements common command dispatch for our SIM tools.
10
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <ctype.h>
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <string.h>
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <strings.h>
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
14
b7ee2e85686b command dispatch and scripting factored out into libcommon
Mychaela Falconia <falcon@freecalypso.org>
parents: 10
diff changeset
10 #include "cmdtab.h"
10
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
14
b7ee2e85686b command dispatch and scripting factored out into libcommon
Mychaela Falconia <falcon@freecalypso.org>
parents: 10
diff changeset
12 extern struct cmdtab cmdtab[];
10
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 static FILE *
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 handle_output_redir(str)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 char *str;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 char *cp, *fn;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 FILE *outf;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 for (cp = str; isspace(*cp); cp++)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 ;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 if (!*cp || *cp == '#') {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 fprintf(stderr, "error: no filename after '>'\n");
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 return(0);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 for (fn = cp; *cp && !isspace(*cp); cp++)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 ;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (*cp)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 *cp++ = '\0';
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 while (isspace(*cp))
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 cp++;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (*cp && *cp != '#') {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 fprintf(stderr, "error: invalid syntax after '>'\n");
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 return(0);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 outf = fopen(fn, "w");
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 if (!outf)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 perror(fn);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 return outf;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 simtool_dispatch_cmd(cmd, is_script)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 char *cmd;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 char *argv[20];
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 char *cp, **ap;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 struct cmdtab *tp;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 FILE *outf;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 int rc;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 for (cp = cmd; isspace(*cp); cp++)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 ;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (!*cp || *cp == '#')
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 return(0);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (is_script)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 printf("Script command: %s\n", cp);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 if (*cp == '!')
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 return system(cp + 1);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 argv[0] = cp;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 while (*cp && !isspace(*cp))
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 cp++;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if (*cp)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 *cp++ = '\0';
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 for (tp = cmdtab; tp->cmd; tp++)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 if (!strcmp(tp->cmd, argv[0]))
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 break;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 if (!tp->func) {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 fprintf(stderr, "error: no such command\n");
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 return(-1);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 for (ap = argv + 1; ; ) {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 while (isspace(*cp))
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 cp++;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (!*cp || *cp == '#' || *cp == '>')
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 break;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 if (ap - argv - 1 >= tp->maxargs) {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 fprintf(stderr, "error: too many arguments\n");
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 return(-1);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 if (*cp == '"') {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 *ap++ = ++cp;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 for (;;) {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 if (!*cp) {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 unterm_qstring: fprintf(stderr,
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 "error: unterminated quoted string\n");
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 return(-1);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 if (*cp == '"')
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 break;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 if (*cp++ == '\\') {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 if (!*cp)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 goto unterm_qstring;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 cp++;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 *cp++ = '\0';
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 } else {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 *ap++ = cp;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 while (*cp && !isspace(*cp))
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 cp++;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 if (*cp)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 *cp++ = '\0';
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 if (ap - argv - 1 < tp->minargs) {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 fprintf(stderr, "error: too few arguments\n");
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 return(-1);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 *ap = 0;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 if (*cp == '>') {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 if (!tp->allow_redir) {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 fprintf(stderr,
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 "error: command does not support output redirection\n");
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 return(-1);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 outf = handle_output_redir(cp + 1);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 if (!outf)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 return(-1);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 } else
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 outf = stdout;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 rc = tp->func(ap - argv, argv, outf);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 if (outf != stdout)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 fclose(outf);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 return rc;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 dispatch_ready_argv(argc, argv)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 char **argv;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 struct cmdtab *tp;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 for (tp = cmdtab; tp->cmd; tp++)
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 if (!strcmp(tp->cmd, argv[0]))
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 break;
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 if (!tp->func) {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 fprintf(stderr, "error: no such command\n");
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 return(-1);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 if (argc - 1 > tp->maxargs) {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 fprintf(stderr, "error: too many arguments\n");
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 return(-1);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 if (argc - 1 < tp->minargs) {
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 fprintf(stderr, "error: too few arguments\n");
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 return(-1);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 }
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 return tp->func(argc, argv, stdout);
ddd767f6e15b fc-simtool ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 }