|
|
1.1 paf 1: #include <stdio.h>
2: #include <string.h>
3: #include <assert.h>
4: #include <stdlib.h>
5:
6: #ifdef WIN32
7:
8: #define OPTERRCOLON (1)
9: #define OPTERRNF (2)
10: #define OPTERRARG (3)
11:
12: char *optarg;
13: int optind = 1;
14: int opterr = 1;
15: int optopt;
16:
17: static int
1.4 paf 18: optiserr(int /*argc*/, char * const *argv, int oint, const char* /*optstr*/,
1.1 paf 19: int optchr, int err)
20: {
21: if(opterr)
22: {
23: fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1);
24: switch(err)
25: {
26: case OPTERRCOLON:
27: fprintf(stderr, ": in flags\n");
28: break;
29: case OPTERRNF:
30: fprintf(stderr, "option not found %c\n", argv[oint][optchr]);
31: break;
32: case OPTERRARG:
33: fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]);
34: break;
35: default:
36: fprintf(stderr, "unknown\n");
37: break;
38: }
39: }
40: optopt = argv[oint][optchr];
41: return('?');
42: }
43:
44:
45:
46: int
1.3 paf 47: getopt(int argc, char* const *argv, const char* optstr)
1.1 paf 48: {
49: static int optchr = 0;
50: static int dash = 0; /* have already seen the - */
51:
52: if(optind >= argc)
53: return(EOF);
54: if(!dash && (argv[optind][0] != '-'))
55: return(EOF);
56: if(!dash && (argv[optind][0] == '-') && !argv[optind][1])
57: {
58: /*
59: * use to specify stdin. Need to let pgm process this and
60: * the following args
61: */
62: return(EOF);
63: }
64: if((argv[optind][0] == '-') && (argv[optind][1] == '-'))
65: {
66: /* -- indicates end of args */
67: optind++;
68: return(EOF);
69: }
70: if(!dash)
71: {
72: assert((argv[optind][0] == '-') && argv[optind][1]);
73: dash = 1;
74: optchr = 1;
75: }
76:
77: /* Check if the guy tries to do a -: kind of flag */
78: assert(dash);
79: if(argv[optind][optchr] == ':')
80: {
81: dash = 0;
82: optind++;
83: return(optiserr(argc, argv, optind-1, optstr, optchr, OPTERRCOLON));
84: }
1.6 ! misha 85: const char *cp = strchr(optstr, argv[optind][optchr]);
! 86: if(!cp)
1.1 paf 87: {
88: int errind = optind;
89: int errchr = optchr;
90:
91: if(!argv[optind][optchr+1])
92: {
93: dash = 0;
94: optind++;
95: }
96: else
97: optchr++;
98: return(optiserr(argc, argv, errind, optstr, errchr, OPTERRNF));
99: }
100: if(cp[1] == ':')
101: {
102: dash = 0;
103: optind++;
104: if(optind == argc)
105: return(optiserr(argc, argv, optind-1, optstr, optchr, OPTERRARG));
106: optarg = argv[optind++];
107: return(*cp);
108: }
109: else
110: {
111: if(!argv[optind][optchr+1])
112: {
113: dash = 0;
114: optind++;
115: }
116: else
117: optchr++;
118: return(*cp);
119: }
120: assert(0);
121: return(0);
122: }
123:
124: #endif /* WIN32 */
125:
126: #ifdef TESTGETOPT
127: int
128: main (int argc, char **argv)
129: {
130: int c;
131: extern char *optarg;
132: extern int optind;
133: int aflg = 0;
134: int bflg = 0;
135: int errflg = 0;
136: char *ofile = NULL;
137:
138: while ((c = getopt(argc, argv, "abo:")) != EOF)
139: switch (c) {
140: case 'a':
141: if (bflg)
142: errflg++;
143: else
144: aflg++;
145: break;
146: case 'b':
147: if (aflg)
148: errflg++;
149: else
150: bflg++;
151: break;
152: case 'o':
153: ofile = optarg;
154: (void)printf("ofile = %s\n", ofile);
155: break;
156: case '?':
157: errflg++;
158: }
159: if (errflg) {
160: (void)fprintf(stderr,
161: "usage: cmd [-a|-b] [-o <filename>] files...\n");
162: exit (2);
163: }
164: for ( ; optind < argc; optind++)
165: (void)printf("%s\n", argv[optind]);
166: return 0;
167: }
168:
1.2 paf 169: #endif /* TESTGETOPT */