FreeCalypso > hg > freecalypso-hwlab
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 84:1d7d8615d628 | 85:b57cf64ece29 |
|---|---|
| 1 /* | |
| 2 * This module implements the command dispatch for fc-simtool | |
| 3 */ | |
| 4 | |
| 5 #include <sys/types.h> | |
| 6 #include <ctype.h> | |
| 7 #include <stdio.h> | |
| 8 #include <string.h> | |
| 9 #include <strings.h> | |
| 10 #include <stdlib.h> | |
| 11 #include <pcsclite.h> | |
| 12 #include <winscard.h> | |
| 13 #include "globals.h" | |
| 14 | |
| 15 extern int cmd_select(); | |
| 16 | |
| 17 cmd_exit() | |
| 18 { | |
| 19 SCardDisconnect(hCard, SCARD_UNPOWER_CARD); | |
| 20 SCardReleaseContext(hContext); | |
| 21 exit(0); | |
| 22 } | |
| 23 | |
| 24 static struct cmdtab { | |
| 25 char *cmd; | |
| 26 int minargs; | |
| 27 int maxargs; | |
| 28 int (*func)(); | |
| 29 } cmdtab[] = { | |
| 30 {"exit", 0, 0, cmd_exit}, | |
| 31 {"quit", 0, 0, cmd_exit}, | |
| 32 {"select", 1, 1, cmd_select}, | |
| 33 {0, 0, 0, 0} | |
| 34 }; | |
| 35 | |
| 36 simtool_dispatch_cmd(cmd) | |
| 37 char *cmd; | |
| 38 { | |
| 39 char *argv[10]; | |
| 40 char *cp, **ap; | |
| 41 struct cmdtab *tp; | |
| 42 | |
| 43 for (cp = cmd; isspace(*cp); cp++) | |
| 44 ; | |
| 45 if (!*cp || *cp == '#') | |
| 46 return(0); | |
| 47 argv[0] = cp; | |
| 48 while (*cp && !isspace(*cp)) | |
| 49 cp++; | |
| 50 if (*cp) | |
| 51 *cp++ = '\0'; | |
| 52 for (tp = cmdtab; tp->cmd; tp++) | |
| 53 if (!strcmp(tp->cmd, argv[0])) | |
| 54 break; | |
| 55 if (!tp->func) { | |
| 56 fprintf(stderr, "error: no such command\n"); | |
| 57 return(-1); | |
| 58 } | |
| 59 for (ap = argv + 1; ; ) { | |
| 60 while (isspace(*cp)) | |
| 61 cp++; | |
| 62 if (!*cp || *cp == '#') | |
| 63 break; | |
| 64 if (ap - argv - 1 >= tp->maxargs) { | |
| 65 fprintf(stderr, "error: too many arguments\n"); | |
| 66 return(-1); | |
| 67 } | |
| 68 *ap++ = cp; | |
| 69 while (*cp && !isspace(*cp)) | |
| 70 cp++; | |
| 71 if (*cp) | |
| 72 *cp++ = '\0'; | |
| 73 } | |
| 74 if (ap - argv - 1 < tp->minargs) { | |
| 75 fprintf(stderr, "error: too few arguments\n"); | |
| 76 return(-1); | |
| 77 } | |
| 78 *ap = 0; | |
| 79 return tp->func(ap - argv, argv); | |
| 80 } |
