annotate uicc/dispatch.c @ 131:d0929cb42e1c

fc-uicc-tool: GET RESPONSE after SELECT
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 04 Feb 2021 00:23:33 +0000
parents f691a19f191d
children f3bdefbeae38
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
130
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements the command dispatch for fc-uicc-tool
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <ctype.h>
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <string.h>
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <strings.h>
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 extern int cmd_exec();
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 extern int cmd_select();
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 extern int display_sim_resp_in_hex();
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 extern int good_exit();
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 static struct cmdtab {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 char *cmd;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 int minargs;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 int maxargs;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 int (*func)();
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 } cmdtab[] = {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 {"exec", 1, 1, cmd_exec},
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 {"exit", 0, 0, good_exit},
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 {"quit", 0, 0, good_exit},
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 {"select", 1, 1, cmd_select},
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 {"sim-resp", 0, 0, display_sim_resp_in_hex},
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 {0, 0, 0, 0}
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 };
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 simtool_dispatch_cmd(cmd, is_script)
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 char *cmd;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 char *argv[10];
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 char *cp, **ap;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 struct cmdtab *tp;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 for (cp = cmd; isspace(*cp); cp++)
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 ;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 if (!*cp || *cp == '#')
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 return(0);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 if (is_script)
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 printf("Script command: %s\n", cp);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 argv[0] = cp;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 while (*cp && !isspace(*cp))
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 cp++;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 if (*cp)
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 *cp++ = '\0';
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 for (tp = cmdtab; tp->cmd; tp++)
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (!strcmp(tp->cmd, argv[0]))
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 break;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (!tp->func) {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 fprintf(stderr, "error: no such command\n");
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 return(-1);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 for (ap = argv + 1; ; ) {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 while (isspace(*cp))
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 cp++;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 if (!*cp || *cp == '#')
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 break;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 if (ap - argv - 1 >= tp->maxargs) {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 fprintf(stderr, "error: too many arguments\n");
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 return(-1);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 }
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 if (*cp == '"') {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 *ap++ = ++cp;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 for (;;) {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 if (!*cp) {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 unterm_qstring: fprintf(stderr,
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 "error: unterminated quoted string\n");
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 return(-1);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 if (*cp == '"')
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 break;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (*cp++ == '\\') {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 if (!*cp)
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 goto unterm_qstring;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 cp++;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 }
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 *cp++ = '\0';
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 } else {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 *ap++ = cp;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 while (*cp && !isspace(*cp))
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 cp++;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 if (*cp)
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 *cp++ = '\0';
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 }
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 }
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 if (ap - argv - 1 < tp->minargs) {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 fprintf(stderr, "error: too few arguments\n");
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 return(-1);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 }
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 *ap = 0;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 return tp->func(ap - argv, argv);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 }
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 dispatch_ready_argv(argc, argv)
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 char **argv;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 struct cmdtab *tp;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 for (tp = cmdtab; tp->cmd; tp++)
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 if (!strcmp(tp->cmd, argv[0]))
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 break;
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 if (!tp->func) {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 fprintf(stderr, "error: no such command\n");
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 return(-1);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 if (argc - 1 > tp->maxargs) {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 fprintf(stderr, "error: too many arguments\n");
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 return(-1);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 }
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 if (argc - 1 < tp->minargs) {
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 fprintf(stderr, "error: too few arguments\n");
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 return(-1);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 }
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 return tp->func(argc, argv);
f691a19f191d fc-uicc-tool skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 }