comparison uicc/dispatch.c @ 130:f691a19f191d

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