xref: /aoo42x/main/soltools/cpp/_getopt.c (revision cdf0e10c)
1*cdf0e10cSrcweir #include    <stdio.h>
2*cdf0e10cSrcweir #include    <string.h>
3*cdf0e10cSrcweir 
4*cdf0e10cSrcweir #define EPR                 fprintf(stderr,
5*cdf0e10cSrcweir #define ERR(str, chr)       if(opterr) { EPR "%s%c\n", str, chr); }
6*cdf0e10cSrcweir 
7*cdf0e10cSrcweir int opterr = 1;
8*cdf0e10cSrcweir int optind = 1;
9*cdf0e10cSrcweir int optopt;
10*cdf0e10cSrcweir char *optarg;
11*cdf0e10cSrcweir 
12*cdf0e10cSrcweir int
13*cdf0e10cSrcweir     stgetopt(int argc, char *const argv[], const char *opts)
14*cdf0e10cSrcweir {
15*cdf0e10cSrcweir     static int sp = 1;
16*cdf0e10cSrcweir     register int c;
17*cdf0e10cSrcweir     register char *cp;
18*cdf0e10cSrcweir 
19*cdf0e10cSrcweir     if (sp == 1)
20*cdf0e10cSrcweir     {
21*cdf0e10cSrcweir         if (optind >= argc ||
22*cdf0e10cSrcweir             argv[optind][0] != '-' || argv[optind][1] == '\0')
23*cdf0e10cSrcweir             return -1;
24*cdf0e10cSrcweir         else if (strcmp(argv[optind], "--") == 0)
25*cdf0e10cSrcweir             {
26*cdf0e10cSrcweir                 optind++;
27*cdf0e10cSrcweir                 return -1;
28*cdf0e10cSrcweir             }
29*cdf0e10cSrcweir         else if (strcmp(argv[optind], "-isysroot") == 0)
30*cdf0e10cSrcweir             {
31*cdf0e10cSrcweir                 // skip Mac OS X SDK selection flags
32*cdf0e10cSrcweir                 optind++; optind++;
33*cdf0e10cSrcweir             }
34*cdf0e10cSrcweir     }
35*cdf0e10cSrcweir     optopt = c = argv[optind][sp];
36*cdf0e10cSrcweir     if (c == ':' || (cp = strchr(opts, c)) == 0)
37*cdf0e10cSrcweir     {
38*cdf0e10cSrcweir         ERR(": illegal option -- ", c);
39*cdf0e10cSrcweir         if (argv[optind][++sp] == '\0')
40*cdf0e10cSrcweir         {
41*cdf0e10cSrcweir             optind++;
42*cdf0e10cSrcweir             sp = 1;
43*cdf0e10cSrcweir         }
44*cdf0e10cSrcweir         return '?';
45*cdf0e10cSrcweir     }
46*cdf0e10cSrcweir     if (*++cp == ':')
47*cdf0e10cSrcweir     {
48*cdf0e10cSrcweir         if (argv[optind][sp + 1] != '\0')
49*cdf0e10cSrcweir             optarg = &argv[optind++][sp + 1];
50*cdf0e10cSrcweir         else
51*cdf0e10cSrcweir             if (++optind >= argc)
52*cdf0e10cSrcweir             {
53*cdf0e10cSrcweir                 ERR(": option requires an argument -- ", c);
54*cdf0e10cSrcweir                 sp = 1;
55*cdf0e10cSrcweir                 return '?';
56*cdf0e10cSrcweir             }
57*cdf0e10cSrcweir             else
58*cdf0e10cSrcweir                 optarg = argv[optind++];
59*cdf0e10cSrcweir         sp = 1;
60*cdf0e10cSrcweir     }
61*cdf0e10cSrcweir     else
62*cdf0e10cSrcweir     {
63*cdf0e10cSrcweir         if (argv[optind][++sp] == '\0')
64*cdf0e10cSrcweir         {
65*cdf0e10cSrcweir             sp = 1;
66*cdf0e10cSrcweir             optind++;
67*cdf0e10cSrcweir         }
68*cdf0e10cSrcweir         optarg = 0;
69*cdf0e10cSrcweir     }
70*cdf0e10cSrcweir     return c;
71*cdf0e10cSrcweir }
72