FreeCalypso > hg > fc-sim-tools
annotate libcommon/globalopts.c @ 97:9f1cc3174e45
grcard2-set-comp128: allow optional setting of upper 6 bits
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Wed, 05 May 2021 04:05:57 +0000 |
parents | 9eb5460f51a6 |
children |
rev | line source |
---|---|
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module implements parsing of global command line options. |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
45
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
5 #include <ctype.h> |
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdio.h> |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdlib.h> |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <unistd.h> |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 |
45
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
10 unsigned calypso_fd, pcsc_reader_num, serial_spenh; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
11 int use_pcsc; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
12 char *serial_device, *serial_baud; |
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 parse_global_options(argc, argv) |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 char **argv; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 { |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 extern char *optarg; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 int c; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 |
45
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
20 while ((c = getopt(argc, argv, "+b:C:d:e:p:")) != EOF) { |
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 switch (c) { |
45
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
22 case 'b': |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
23 serial_baud = optarg; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
24 continue; |
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 case 'C': |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 calypso_fd = atoi(optarg); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 continue; |
45
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
28 case 'd': |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
29 serial_device = optarg; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
30 continue; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
31 case 'e': |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
32 if (!isdigit(optarg[0]) || optarg[1]) { |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
33 bad_minus_e: fprintf(stderr, "error: bad -e argument\n"); |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
34 exit(1); |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
35 } |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
36 serial_spenh = optarg[0] - '0'; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
37 switch (serial_spenh) { |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
38 case 1: |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
39 case 2: |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
40 case 4: |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
41 case 8: |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
42 break; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
43 default: |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
44 goto bad_minus_e; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
45 } |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
46 continue; |
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 case 'p': |
45
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
48 use_pcsc = 1; |
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 pcsc_reader_num = atoi(optarg); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 continue; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 case '?': |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 default: |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 /* error msg already printed */ |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 exit(1); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 } |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 } |
45
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
57 if (serial_device && use_pcsc) { |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
58 fprintf(stderr, |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
59 "error: -d and -p target selection options are mutually exclusive\n"); |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
60 exit(1); |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
61 } |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
62 if (!serial_device && (serial_baud || serial_spenh)) { |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
63 fprintf(stderr, |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
64 "error: -b and -e options are meaningless without -d\n"); |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
65 exit(1); |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
66 } |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
67 if (serial_baud && serial_spenh) { |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
68 fprintf(stderr, |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
69 "error: -b and -e options are mutually exclusive\n"); |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
70 exit(1); |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
71 } |
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 return(0); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 } |