xref: /aoo41x/main/soltools/cpp/_getopt.c (revision 7ce20373)
1*7ce20373SAndrew Rist /**************************************************************
2*7ce20373SAndrew Rist  *
3*7ce20373SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*7ce20373SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*7ce20373SAndrew Rist  * distributed with this work for additional information
6*7ce20373SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*7ce20373SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*7ce20373SAndrew Rist  * "License"); you may not use this file except in compliance
9*7ce20373SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*7ce20373SAndrew Rist  *
11*7ce20373SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*7ce20373SAndrew Rist  *
13*7ce20373SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*7ce20373SAndrew Rist  * software distributed under the License is distributed on an
15*7ce20373SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*7ce20373SAndrew Rist  * KIND, either express or implied.  See the License for the
17*7ce20373SAndrew Rist  * specific language governing permissions and limitations
18*7ce20373SAndrew Rist  * under the License.
19*7ce20373SAndrew Rist  *
20*7ce20373SAndrew Rist  *************************************************************/
21*7ce20373SAndrew Rist 
22cdf0e10cSrcweir #include    <stdio.h>
23cdf0e10cSrcweir #include    <string.h>
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #define EPR                 fprintf(stderr,
26cdf0e10cSrcweir #define ERR(str, chr)       if(opterr) { EPR "%s%c\n", str, chr); }
27cdf0e10cSrcweir 
28cdf0e10cSrcweir int opterr = 1;
29cdf0e10cSrcweir int optind = 1;
30cdf0e10cSrcweir int optopt;
31cdf0e10cSrcweir char *optarg;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir int
stgetopt(int argc,char * const argv[],const char * opts)34cdf0e10cSrcweir     stgetopt(int argc, char *const argv[], const char *opts)
35cdf0e10cSrcweir {
36cdf0e10cSrcweir     static int sp = 1;
37cdf0e10cSrcweir     register int c;
38cdf0e10cSrcweir     register char *cp;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir     if (sp == 1)
41cdf0e10cSrcweir     {
42cdf0e10cSrcweir         if (optind >= argc ||
43cdf0e10cSrcweir             argv[optind][0] != '-' || argv[optind][1] == '\0')
44cdf0e10cSrcweir             return -1;
45cdf0e10cSrcweir         else if (strcmp(argv[optind], "--") == 0)
46cdf0e10cSrcweir             {
47cdf0e10cSrcweir                 optind++;
48cdf0e10cSrcweir                 return -1;
49cdf0e10cSrcweir             }
50cdf0e10cSrcweir         else if (strcmp(argv[optind], "-isysroot") == 0)
51cdf0e10cSrcweir             {
52cdf0e10cSrcweir                 // skip Mac OS X SDK selection flags
53cdf0e10cSrcweir                 optind++; optind++;
54cdf0e10cSrcweir             }
55cdf0e10cSrcweir     }
56cdf0e10cSrcweir     optopt = c = argv[optind][sp];
57cdf0e10cSrcweir     if (c == ':' || (cp = strchr(opts, c)) == 0)
58cdf0e10cSrcweir     {
59cdf0e10cSrcweir         ERR(": illegal option -- ", c);
60cdf0e10cSrcweir         if (argv[optind][++sp] == '\0')
61cdf0e10cSrcweir         {
62cdf0e10cSrcweir             optind++;
63cdf0e10cSrcweir             sp = 1;
64cdf0e10cSrcweir         }
65cdf0e10cSrcweir         return '?';
66cdf0e10cSrcweir     }
67cdf0e10cSrcweir     if (*++cp == ':')
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         if (argv[optind][sp + 1] != '\0')
70cdf0e10cSrcweir             optarg = &argv[optind++][sp + 1];
71cdf0e10cSrcweir         else
72cdf0e10cSrcweir             if (++optind >= argc)
73cdf0e10cSrcweir             {
74cdf0e10cSrcweir                 ERR(": option requires an argument -- ", c);
75cdf0e10cSrcweir                 sp = 1;
76cdf0e10cSrcweir                 return '?';
77cdf0e10cSrcweir             }
78cdf0e10cSrcweir             else
79cdf0e10cSrcweir                 optarg = argv[optind++];
80cdf0e10cSrcweir         sp = 1;
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir     else
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir         if (argv[optind][++sp] == '\0')
85cdf0e10cSrcweir         {
86cdf0e10cSrcweir             sp = 1;
87cdf0e10cSrcweir             optind++;
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir         optarg = 0;
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir     return c;
92cdf0e10cSrcweir }
93