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