comparison simtool/dispatch.c @ 1:2071b28cd0c7

simtool: first refactored version
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 11 Feb 2021 23:04:28 +0000
parents
children d368051576d0
comparison
equal deleted inserted replaced
0:f7145c77b7fb 1:2071b28cd0c7
1 /*
2 * This module implements the command dispatch for fc-simtool
3 */
4
5 #include <ctype.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <strings.h>
9 #include <stdlib.h>
10
11 extern int cmd_a38();
12 extern int cmd_change_chv();
13 extern int cmd_disable_chv();
14 extern int cmd_enable_chv();
15 extern int cmd_exec();
16 extern int cmd_fix_sysmo_msisdn();
17 extern int cmd_grcard1_set_adm();
18 extern int cmd_grcard1_set_ki();
19 extern int cmd_grcard1_set_pin();
20 extern int cmd_grcard2_set_adm();
21 extern int cmd_grcard2_set_pin();
22 extern int cmd_grcard2_set_puk();
23 extern int cmd_grcard2_set_super();
24 extern int cmd_iccid();
25 extern int cmd_imsi();
26 extern int cmd_pb_dump();
27 extern int cmd_pb_dump_rec();
28 extern int cmd_pb_erase();
29 extern int cmd_pb_erase_one();
30 extern int cmd_pb_erase_range();
31 extern int cmd_pb_update();
32 extern int cmd_pb_update_imm();
33 extern int cmd_pb_update_imm_hex();
34 extern int cmd_readbin();
35 extern int cmd_readef();
36 extern int cmd_readrec();
37 extern int cmd_restore_file();
38 extern int cmd_savebin();
39 extern int cmd_save_sms_bin();
40 extern int cmd_select();
41 extern int cmd_spn();
42 extern int cmd_telecom_sum();
43 extern int cmd_uicc_dir();
44 extern int cmd_unblock_chv();
45 extern int cmd_update_bin();
46 extern int cmd_update_bin_imm();
47 extern int cmd_update_rec();
48 extern int cmd_verify_chv();
49 extern int cmd_verify_ext();
50
51 extern int display_sim_resp_in_hex();
52 extern int good_exit();
53
54 static struct cmdtab {
55 char *cmd;
56 int minargs;
57 int maxargs;
58 int (*func)();
59 } cmdtab[] = {
60 {"a38", 1, 1, cmd_a38},
61 {"change-chv1", 2, 2, cmd_change_chv},
62 {"change-chv2", 2, 2, cmd_change_chv},
63 {"change-pin1", 2, 2, cmd_change_chv},
64 {"change-pin2", 2, 2, cmd_change_chv},
65 {"disable-chv", 1, 1, cmd_disable_chv},
66 {"disable-pin", 1, 1, cmd_disable_chv},
67 {"enable-chv", 1, 1, cmd_enable_chv},
68 {"enable-pin", 1, 1, cmd_enable_chv},
69 {"exec", 1, 1, cmd_exec},
70 {"exit", 0, 0, good_exit},
71 {"fix-sysmo-msisdn", 0, 0, cmd_fix_sysmo_msisdn},
72 {"grcard1-set-adm1", 2, 2, cmd_grcard1_set_adm},
73 {"grcard1-set-adm2", 2, 2, cmd_grcard1_set_adm},
74 {"grcard1-set-ki", 1, 1, cmd_grcard1_set_ki},
75 {"grcard1-set-pin1", 2, 2, cmd_grcard1_set_pin},
76 {"grcard1-set-pin2", 2, 2, cmd_grcard1_set_pin},
77 {"grcard2-set-adm", 1, 1, cmd_grcard2_set_adm},
78 {"grcard2-set-pin1", 1, 1, cmd_grcard2_set_pin},
79 {"grcard2-set-pin2", 1, 1, cmd_grcard2_set_pin},
80 {"grcard2-set-puk1", 1, 1, cmd_grcard2_set_puk},
81 {"grcard2-set-puk2", 1, 1, cmd_grcard2_set_puk},
82 {"grcard2-set-super", 1, 1, cmd_grcard2_set_super},
83 {"iccid", 0, 0, cmd_iccid},
84 {"imsi", 0, 0, cmd_imsi},
85 {"pb-dump", 1, 2, cmd_pb_dump},
86 {"pb-dump-rec", 2, 3, cmd_pb_dump_rec},
87 {"pb-erase", 1, 1, cmd_pb_erase},
88 {"pb-erase-one", 2, 2, cmd_pb_erase_one},
89 {"pb-erase-range", 3, 3, cmd_pb_erase_range},
90 {"pb-update", 2, 2, cmd_pb_update},
91 {"pb-update-imm", 3, 4, cmd_pb_update_imm},
92 {"pb-update-imm-hex", 4, 4, cmd_pb_update_imm_hex},
93 {"quit", 0, 0, good_exit},
94 {"readbin", 2, 2, cmd_readbin},
95 {"readef", 1, 1, cmd_readef},
96 {"readrec", 1, 2, cmd_readrec},
97 {"restore-file", 2, 2, cmd_restore_file},
98 {"savebin", 2, 2, cmd_savebin},
99 {"save-sms-bin", 1, 1, cmd_save_sms_bin},
100 {"select", 1, 1, cmd_select},
101 {"sim-resp", 0, 0, display_sim_resp_in_hex},
102 {"spn", 0, 0, cmd_spn},
103 {"telecom-sum", 0, 0, cmd_telecom_sum},
104 {"uicc-dir", 0, 0, cmd_uicc_dir},
105 {"unblock-chv1", 2, 2, cmd_unblock_chv},
106 {"unblock-chv2", 2, 2, cmd_unblock_chv},
107 {"unblock-pin1", 2, 2, cmd_unblock_chv},
108 {"unblock-pin2", 2, 2, cmd_unblock_chv},
109 {"update-bin", 2, 2, cmd_update_bin},
110 {"update-bin-imm", 2, 2, cmd_update_bin_imm},
111 {"update-rec", 2, 2, cmd_update_rec},
112 {"verify-chv1", 1, 1, cmd_verify_chv},
113 {"verify-chv2", 1, 1, cmd_verify_chv},
114 {"verify-ext", 2, 2, cmd_verify_ext},
115 {"verify-pin1", 1, 1, cmd_verify_chv},
116 {"verify-pin2", 1, 1, cmd_verify_chv},
117 {0, 0, 0, 0}
118 };
119
120 simtool_dispatch_cmd(cmd, is_script)
121 char *cmd;
122 {
123 char *argv[10];
124 char *cp, **ap;
125 struct cmdtab *tp;
126
127 for (cp = cmd; isspace(*cp); cp++)
128 ;
129 if (!*cp || *cp == '#')
130 return(0);
131 if (is_script)
132 printf("Script command: %s\n", cp);
133 argv[0] = cp;
134 while (*cp && !isspace(*cp))
135 cp++;
136 if (*cp)
137 *cp++ = '\0';
138 for (tp = cmdtab; tp->cmd; tp++)
139 if (!strcmp(tp->cmd, argv[0]))
140 break;
141 if (!tp->func) {
142 fprintf(stderr, "error: no such command\n");
143 return(-1);
144 }
145 for (ap = argv + 1; ; ) {
146 while (isspace(*cp))
147 cp++;
148 if (!*cp || *cp == '#')
149 break;
150 if (ap - argv - 1 >= tp->maxargs) {
151 fprintf(stderr, "error: too many arguments\n");
152 return(-1);
153 }
154 if (*cp == '"') {
155 *ap++ = ++cp;
156 for (;;) {
157 if (!*cp) {
158 unterm_qstring: fprintf(stderr,
159 "error: unterminated quoted string\n");
160 return(-1);
161 }
162 if (*cp == '"')
163 break;
164 if (*cp++ == '\\') {
165 if (!*cp)
166 goto unterm_qstring;
167 cp++;
168 }
169 }
170 *cp++ = '\0';
171 } else {
172 *ap++ = cp;
173 while (*cp && !isspace(*cp))
174 cp++;
175 if (*cp)
176 *cp++ = '\0';
177 }
178 }
179 if (ap - argv - 1 < tp->minargs) {
180 fprintf(stderr, "error: too few arguments\n");
181 return(-1);
182 }
183 *ap = 0;
184 return tp->func(ap - argv, argv);
185 }
186
187 dispatch_ready_argv(argc, argv)
188 char **argv;
189 {
190 struct cmdtab *tp;
191
192 for (tp = cmdtab; tp->cmd; tp++)
193 if (!strcmp(tp->cmd, argv[0]))
194 break;
195 if (!tp->func) {
196 fprintf(stderr, "error: no such command\n");
197 return(-1);
198 }
199 if (argc - 1 > tp->maxargs) {
200 fprintf(stderr, "error: too many arguments\n");
201 return(-1);
202 }
203 if (argc - 1 < tp->minargs) {
204 fprintf(stderr, "error: too few arguments\n");
205 return(-1);
206 }
207 return tp->func(argc, argv);
208 }