annotate simtool/dispatch.c @ 85:b57cf64ece29

fc-simtool project started
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 24 Jan 2021 00:32:10 +0000
parents
children 91486a77643e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
85
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements the command dispatch for fc-simtool
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <ctype.h>
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <string.h>
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <strings.h>
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <pcsclite.h>
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <winscard.h>
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include "globals.h"
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 extern int cmd_select();
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 cmd_exit()
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 {
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 SCardReleaseContext(hContext);
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 exit(0);
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 }
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 static struct cmdtab {
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 char *cmd;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 int minargs;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 int maxargs;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 int (*func)();
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 } cmdtab[] = {
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 {"exit", 0, 0, cmd_exit},
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 {"quit", 0, 0, cmd_exit},
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 {"select", 1, 1, cmd_select},
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 {0, 0, 0, 0}
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 };
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 simtool_dispatch_cmd(cmd)
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 char *cmd;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 {
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 char *argv[10];
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 char *cp, **ap;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 struct cmdtab *tp;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 for (cp = cmd; isspace(*cp); cp++)
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 ;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 if (!*cp || *cp == '#')
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 return(0);
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 argv[0] = cp;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 while (*cp && !isspace(*cp))
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 cp++;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (*cp)
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 *cp++ = '\0';
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 for (tp = cmdtab; tp->cmd; tp++)
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 if (!strcmp(tp->cmd, argv[0]))
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 break;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 if (!tp->func) {
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 fprintf(stderr, "error: no such command\n");
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 return(-1);
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 }
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 for (ap = argv + 1; ; ) {
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 while (isspace(*cp))
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 cp++;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 if (!*cp || *cp == '#')
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 break;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 if (ap - argv - 1 >= tp->maxargs) {
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 fprintf(stderr, "error: too many arguments\n");
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 return(-1);
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 }
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 *ap++ = cp;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 while (*cp && !isspace(*cp))
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 cp++;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 if (*cp)
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 *cp++ = '\0';
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 }
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 if (ap - argv - 1 < tp->minargs) {
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 fprintf(stderr, "error: too few arguments\n");
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 return(-1);
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 }
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 *ap = 0;
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 return tp->func(ap - argv, argv);
b57cf64ece29 fc-simtool project started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 }