1 /* $Id: pilotmain.c,v 1.7 2001/10/02 16:24:09 rees Exp $ */
2
3 /*
4 * Smartcard commander.
5 * Written by Jim Rees and others at the
6 * Center for Information Technology Integration (CITI)
7 * University of Michigan.
8 */
9
10 /*
11 copyright 2001
12 the regents of the university of michigan
13 all rights reserved
14
15 permission is granted to use, copy, create derivative works
16 and redistribute this software and such derivative works
17 for any purpose, so long as the name of the university of
18 michigan is not used in any advertising or publicity
19 pertaining to the use or distribution of this software
20 without specific, written prior authorization. if the
21 above copyright notice or any other identification of the
22 university of michigan is included in any copy of any
23 portion of this software, then the disclaimer below must
24 also be included.
25
26 this software is provided as is, without representation
27 from the university of michigan as to its fitness for any
28 purpose, and without warranty by the university of
29 michigan of any kind, either express or implied, including
30 without limitation the implied warranties of
31 merchantability and fitness for a particular purpose. the
32 regents of the university of michigan shall not be liable
33 for any damages, including special, indirect, incidental, or
34 consequential damages, with respect to any claim arising
35 out of or in connection with the use of the software, even
36 if it has been or is hereafter advised of the possibility of
37 such damages.
38 */
39
40 #pragma pack(2)
41
42 #include <Common.h>
43 #include <System/SysAll.h>
44 #include <UI/UIAll.h>
45 #include <System/Unix/sys_types.h>
46 #include <System/Unix/unix_stdlib.h>
47 #include <System/Unix/unix_string.h>
48 #include <string.h>
49
50 #include "sectok.h"
51 #include "field.h"
52 #include "resource.h"
53 #include "sc.h"
54
55 #define MAXTOKENS 300
56 #define MAXCOMMANDS 12
57
58 int port, fd = -1, cla;
59 FieldPtr cmdfield, printfield;
60 char *__progname = "sectok";
61
62 extern void IO_Close();
63 int StartApplication();
64 int StopApplication();
65 int EventLoop();
66 static Boolean sectok(EventPtr event);
67 void display_cmd_list();
68 int parse_and_dispatch();
69 int just_dispatch(char *cmd);
70
71 static char *lv1[] = {"apdu", "fid", "isearch", "csearch", "class", "read", "write", "challenge", NULL};
72 static char *lv2[] = {"acl", "create", "delete", "jdefault", "jatr", "jdata", "login",
73 "junload", NULL};
74 static char **lva[] = {lv1, lv2, NULL};
75
76 DWord
PilotMain(Word cmd,Ptr cmdPBP,Word launchFlags)77 PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
78 {
79 int err;
80
81 if (cmd == sysAppLaunchCmdNormalLaunch) {
82 err = StartApplication();
83 if (err)
84 return err;
85 EventLoop();
86 StopApplication();
87 return 0;
88 } else
89 return sysErrParamErr;
90 }
91
92 int
StartApplication()93 StartApplication()
94 {
95 fd = -1;
96 FrmGotoForm(formID_sectok);
97 return 0;
98 }
99
100 int
StopApplication()101 StopApplication()
102 {
103 dclose(0, NULL);
104 IO_Close();
105 FrmCloseAllForms();
106 return 0;
107 }
108
109 int
EventLoop()110 EventLoop()
111 {
112 short err;
113 int formID;
114 FormPtr form;
115 EventType event;
116
117 do {
118 EvtGetEvent(&event, 200);
119
120 if (SysHandleEvent(&event))
121 continue;
122 if (MenuHandleEvent((void *)0, &event, &err))
123 continue;
124
125 if (event.eType == frmLoadEvent) {
126 formID = event.data.frmLoad.formID;
127 form = FrmInitForm(formID);
128 FrmSetActiveForm(form);
129 switch (formID) {
130 case formID_sectok:
131 FrmSetEventHandler(form, (FormEventHandlerPtr) sectok);
132 break;
133 }
134 }
135 FrmDispatchEvent(&event);
136 } while (event.eType != appStopEvent);
137 return 0;
138 }
139
140 static Boolean
sectok(EventPtr event)141 sectok(EventPtr event)
142 {
143 FormPtr form = FrmGetActiveForm();
144 Word item;
145 FieldPtr field;
146 int id, handled;
147 char *cmd, cmdname[32], *av[2];
148
149 item = FrmGetFocus(form);
150 if (item == noFocus)
151 field = printfield;
152 else
153 field = FrmGetObjectPtr(form, item);
154
155 switch (event->eType) {
156 case frmOpenEvent:
157 switch (event->data.frmOpen.formID) {
158 case formID_sectok:
159 cmdfield = FrmGetObjectPtr(form, FrmGetObjectIndex(form, fieldID_cmd));
160 printfield = FrmGetObjectPtr(form, FrmGetObjectIndex(form, fieldID_msg));
161 display_cmd_list();
162 FrmDrawForm(form);
163 break;
164 default:
165 break;
166 }
167 handled = 1;
168 break;
169
170 case ctlSelectEvent:
171 id = event->data.ctlEnter.controlID;
172 handled = 1;
173 switch (id) {
174 case buttonID_Go:
175 parse_and_dispatch();
176 break;
177 case buttonID_Clear:
178 setfield(cmdfield, NULL);
179 FrmSetFocus(form, FrmGetObjectIndex(form, fieldID_cmd));
180 break;
181 case buttonID_Reset:
182 just_dispatch("reset -v");
183 break;
184 case buttonID_ls:
185 just_dispatch("ls -l");
186 break;
187 case buttonID_Close:
188 dclose(0, NULL);
189 break;
190 default:
191 handled = 0;
192 break;
193 }
194 break;
195
196 case popSelectEvent:
197 cmd = lva[event->data.popSelect.listID - listID_cmd][event->data.popSelect.selection];
198 av[0] = "help";
199 av[1] = cmd;
200 help(2, av);
201 sprintf(cmdname, "%s ", cmd);
202 setfield(cmdfield, cmdname);
203 FrmSetFocus(form, FrmGetObjectIndex(form, fieldID_cmd));
204 handled = 1;
205 break;
206
207 case keyDownEvent:
208 if (field == cmdfield && event->data.keyDown.chr == '\n') {
209 parse_and_dispatch();
210 handled = 1;
211 } else
212 handled = scrollfield(field, event->data.keyDown.chr);
213 break;
214
215 case menuEvent:
216 switch (event->data.menu.itemID) {
217 case menuitemID_help:
218 FrmHelp(textID_help);
219 break;
220 case menuitemID_about:
221 FrmAlert(alertID_about);
222 break;
223 case menuitemID_cut:
224 FldCut(field);
225 break;
226 case menuitemID_copy:
227 FldCopy(field);
228 break;
229 case menuitemID_paste:
230 FldPaste(field);
231 break;
232 default:
233 break;
234 }
235 handled = 1;
236 break;
237
238 default:
239 handled = 0;
240 break;
241
242 }
243 return handled;
244 }
245
246 void
display_cmd_list()247 display_cmd_list()
248 {
249 FormPtr form = FrmGetActiveForm();
250 ListPtr lp;
251 int li, lc;
252
253 /* For each list */
254 for (li = 0; lva[li]; li++) {
255 /* Count how many entries in this list */
256 for (lc = 0; lva[li][lc]; )
257 lc++;
258 lp = (ListPtr) FrmGetObjectPtr(form, FrmGetObjectIndex(form, li + listID_cmd));
259 LstSetListChoices(lp, lva[li], lc);
260 }
261 }
262
263 int
parse_and_dispatch()264 parse_and_dispatch()
265 {
266 if (fd < 0) {
267 printf("reader not open\n");
268 return -1;
269 }
270
271 if (FldGetTextLength(cmdfield) <= 0)
272 return -1;
273
274 return just_dispatch(FldGetTextPtr(cmdfield));
275 }
276
277 int
just_dispatch(char * cmd)278 just_dispatch(char *cmd)
279 {
280 int sw, tc;
281 char *s, *tp, *tv[MAXTOKENS];
282
283 s = strdup(cmd);
284
285 for (tc = 0, tp = s; *tp; ) {
286 /* skip blanks */
287 while (*tp && *tp == ' ')
288 *tp++ = '\0';
289 if (!*tp)
290 break;
291 tv[tc++] = tp;
292 /* skip non-blanks */
293 while (*tp && *tp != ' ')
294 tp++;
295 }
296 tv[tc] = NULL;
297
298 sw = dispatch(tc, tv);
299 free(s);
300 return sw;
301 }
302