comparison uicc/dispatch.c @ 22:1b1468869ccf

new trimmed fc-uicc-tool is here
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 12 Feb 2021 04:34:53 +0000
parents
children d368051576d0
comparison
equal deleted inserted replaced
21:d4dc86195382 22:1b1468869ccf
1 /*
2 * This module implements the command dispatch for fc-uicc-tool
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_dir();
12 extern int cmd_exec();
13 extern int cmd_iccid();
14 extern int cmd_readbin();
15 extern int cmd_readef();
16 extern int cmd_readrec();
17 extern int cmd_select();
18 extern int cmd_select_aid();
19 extern int cmd_select_isim();
20 extern int cmd_select_usim();
21 extern int cmd_update_bin();
22 extern int cmd_update_bin_imm();
23 extern int cmd_update_rec();
24
25 extern int display_sim_resp_in_hex();
26 extern int good_exit();
27
28 static struct cmdtab {
29 char *cmd;
30 int minargs;
31 int maxargs;
32 int (*func)();
33 } cmdtab[] = {
34 {"dir", 0, 0, cmd_dir},
35 {"exec", 1, 1, cmd_exec},
36 {"exit", 0, 0, good_exit},
37 {"iccid", 0, 0, cmd_iccid},
38 {"quit", 0, 0, good_exit},
39 {"readbin", 2, 2, cmd_readbin},
40 {"readef", 1, 1, cmd_readef},
41 {"readrec", 1, 2, cmd_readrec},
42 {"select", 1, 1, cmd_select},
43 {"select-aid", 1, 1, cmd_select_aid},
44 {"select-isim", 0, 0, cmd_select_isim},
45 {"select-usim", 0, 0, cmd_select_usim},
46 {"sim-resp", 0, 0, display_sim_resp_in_hex},
47 {"update-bin", 2, 2, cmd_update_bin},
48 {"update-bin-imm", 2, 2, cmd_update_bin_imm},
49 {"update-rec", 2, 2, cmd_update_rec},
50 {0, 0, 0, 0}
51 };
52
53 simtool_dispatch_cmd(cmd, is_script)
54 char *cmd;
55 {
56 char *argv[10];
57 char *cp, **ap;
58 struct cmdtab *tp;
59
60 for (cp = cmd; isspace(*cp); cp++)
61 ;
62 if (!*cp || *cp == '#')
63 return(0);
64 if (is_script)
65 printf("Script command: %s\n", cp);
66 argv[0] = cp;
67 while (*cp && !isspace(*cp))
68 cp++;
69 if (*cp)
70 *cp++ = '\0';
71 for (tp = cmdtab; tp->cmd; tp++)
72 if (!strcmp(tp->cmd, argv[0]))
73 break;
74 if (!tp->func) {
75 fprintf(stderr, "error: no such command\n");
76 return(-1);
77 }
78 for (ap = argv + 1; ; ) {
79 while (isspace(*cp))
80 cp++;
81 if (!*cp || *cp == '#')
82 break;
83 if (ap - argv - 1 >= tp->maxargs) {
84 fprintf(stderr, "error: too many arguments\n");
85 return(-1);
86 }
87 if (*cp == '"') {
88 *ap++ = ++cp;
89 for (;;) {
90 if (!*cp) {
91 unterm_qstring: fprintf(stderr,
92 "error: unterminated quoted string\n");
93 return(-1);
94 }
95 if (*cp == '"')
96 break;
97 if (*cp++ == '\\') {
98 if (!*cp)
99 goto unterm_qstring;
100 cp++;
101 }
102 }
103 *cp++ = '\0';
104 } else {
105 *ap++ = cp;
106 while (*cp && !isspace(*cp))
107 cp++;
108 if (*cp)
109 *cp++ = '\0';
110 }
111 }
112 if (ap - argv - 1 < tp->minargs) {
113 fprintf(stderr, "error: too few arguments\n");
114 return(-1);
115 }
116 *ap = 0;
117 return tp->func(ap - argv, argv);
118 }
119
120 dispatch_ready_argv(argc, argv)
121 char **argv;
122 {
123 struct cmdtab *tp;
124
125 for (tp = cmdtab; tp->cmd; tp++)
126 if (!strcmp(tp->cmd, argv[0]))
127 break;
128 if (!tp->func) {
129 fprintf(stderr, "error: no such command\n");
130 return(-1);
131 }
132 if (argc - 1 > tp->maxargs) {
133 fprintf(stderr, "error: too many arguments\n");
134 return(-1);
135 }
136 if (argc - 1 < tp->minargs) {
137 fprintf(stderr, "error: too few arguments\n");
138 return(-1);
139 }
140 return tp->func(argc, argv);
141 }