comparison uicc/dispatch.c @ 105:16481db543e8

fc-uicc-tool: infrastructure for output redirection
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 19 Feb 2021 06:07:49 +0000
parents d368051576d0
children dcfec53643c5
comparison
equal deleted inserted replaced
104:4709cff90a58 105:16481db543e8
28 28
29 static struct cmdtab { 29 static struct cmdtab {
30 char *cmd; 30 char *cmd;
31 int minargs; 31 int minargs;
32 int maxargs; 32 int maxargs;
33 int allow_redir;
33 int (*func)(); 34 int (*func)();
34 } cmdtab[] = { 35 } cmdtab[] = {
35 {"atr", 0, 0, retrieve_atr}, 36 {"atr", 0, 0, 0, retrieve_atr},
36 {"dir", 0, 0, cmd_dir}, 37 {"dir", 0, 0, 0, cmd_dir},
37 {"exec", 1, 1, cmd_exec}, 38 {"exec", 1, 1, 0, cmd_exec},
38 {"exit", 0, 0, good_exit}, 39 {"exit", 0, 0, 0, good_exit},
39 {"iccid", 0, 0, cmd_iccid}, 40 {"iccid", 0, 0, 0, cmd_iccid},
40 {"quit", 0, 0, good_exit}, 41 {"quit", 0, 0, 0, good_exit},
41 {"readbin", 2, 2, cmd_readbin}, 42 {"readbin", 2, 2, 0, cmd_readbin},
42 {"readef", 1, 1, cmd_readef}, 43 {"readef", 1, 1, 0, cmd_readef},
43 {"readrec", 1, 2, cmd_readrec}, 44 {"readrec", 1, 2, 0, cmd_readrec},
44 {"select", 1, 1, cmd_select}, 45 {"select", 1, 1, 0, cmd_select},
45 {"select-aid", 1, 1, cmd_select_aid}, 46 {"select-aid", 1, 1, 0, cmd_select_aid},
46 {"select-isim", 0, 0, cmd_select_isim}, 47 {"select-isim", 0, 0, 0, cmd_select_isim},
47 {"select-usim", 0, 0, cmd_select_usim}, 48 {"select-usim", 0, 0, 0, cmd_select_usim},
48 {"sim-resp", 0, 0, display_sim_resp_in_hex}, 49 {"sim-resp", 0, 0, 0, display_sim_resp_in_hex},
49 {"update-bin", 2, 2, cmd_update_bin}, 50 {"update-bin", 2, 2, 0, cmd_update_bin},
50 {"update-bin-imm", 2, 2, cmd_update_bin_imm}, 51 {"update-bin-imm", 2, 2, 0, cmd_update_bin_imm},
51 {"update-rec", 2, 2, cmd_update_rec}, 52 {"update-rec", 2, 2, 0, cmd_update_rec},
52 {0, 0, 0, 0} 53 {0, 0, 0, 0, 0}
53 }; 54 };
55
56 static FILE *
57 handle_output_redir(str)
58 char *str;
59 {
60 char *cp, *fn;
61 FILE *outf;
62
63 for (cp = str; isspace(*cp); cp++)
64 ;
65 if (!*cp || *cp == '#') {
66 fprintf(stderr, "error: no filename after '>'\n");
67 return(0);
68 }
69 for (fn = cp; *cp && !isspace(*cp); cp++)
70 ;
71 if (*cp)
72 *cp++ = '\0';
73 while (isspace(*cp))
74 cp++;
75 if (*cp && *cp != '#') {
76 fprintf(stderr, "error: invalid syntax after '>'\n");
77 return(0);
78 }
79 outf = fopen(fn, "w");
80 if (!outf)
81 perror(fn);
82 return outf;
83 }
54 84
55 simtool_dispatch_cmd(cmd, is_script) 85 simtool_dispatch_cmd(cmd, is_script)
56 char *cmd; 86 char *cmd;
57 { 87 {
58 char *argv[10]; 88 char *argv[10];
59 char *cp, **ap; 89 char *cp, **ap;
60 struct cmdtab *tp; 90 struct cmdtab *tp;
91 FILE *outf;
92 int rc;
61 93
62 for (cp = cmd; isspace(*cp); cp++) 94 for (cp = cmd; isspace(*cp); cp++)
63 ; 95 ;
64 if (!*cp || *cp == '#') 96 if (!*cp || *cp == '#')
65 return(0); 97 return(0);
78 return(-1); 110 return(-1);
79 } 111 }
80 for (ap = argv + 1; ; ) { 112 for (ap = argv + 1; ; ) {
81 while (isspace(*cp)) 113 while (isspace(*cp))
82 cp++; 114 cp++;
83 if (!*cp || *cp == '#') 115 if (!*cp || *cp == '#' || *cp == '>')
84 break; 116 break;
85 if (ap - argv - 1 >= tp->maxargs) { 117 if (ap - argv - 1 >= tp->maxargs) {
86 fprintf(stderr, "error: too many arguments\n"); 118 fprintf(stderr, "error: too many arguments\n");
87 return(-1); 119 return(-1);
88 } 120 }
114 if (ap - argv - 1 < tp->minargs) { 146 if (ap - argv - 1 < tp->minargs) {
115 fprintf(stderr, "error: too few arguments\n"); 147 fprintf(stderr, "error: too few arguments\n");
116 return(-1); 148 return(-1);
117 } 149 }
118 *ap = 0; 150 *ap = 0;
119 return tp->func(ap - argv, argv); 151 if (*cp == '>') {
152 if (!tp->allow_redir) {
153 fprintf(stderr,
154 "error: command does not support output redirection\n");
155 return(-1);
156 }
157 outf = handle_output_redir(cp + 1);
158 if (!outf)
159 return(-1);
160 } else
161 outf = stdout;
162 rc = tp->func(ap - argv, argv, outf);
163 if (outf != stdout)
164 fclose(outf);
165 return rc;
120 } 166 }
121 167
122 dispatch_ready_argv(argc, argv) 168 dispatch_ready_argv(argc, argv)
123 char **argv; 169 char **argv;
124 { 170 {
137 } 183 }
138 if (argc - 1 < tp->minargs) { 184 if (argc - 1 < tp->minargs) {
139 fprintf(stderr, "error: too few arguments\n"); 185 fprintf(stderr, "error: too few arguments\n");
140 return(-1); 186 return(-1);
141 } 187 }
142 return tp->func(argc, argv); 188 return tp->func(argc, argv, stdout);
143 } 189 }