]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/dev/acpica/common/getopt.c
Merge ACPICA 20120320.
[FreeBSD/FreeBSD.git] / sys / contrib / dev / acpica / common / getopt.c
1
2 /******************************************************************************
3  *
4  * Module Name: getopt
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2012, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44
45
46 #include <stdio.h>
47 #include <string.h>
48 #include <contrib/dev/acpica/include/acpi.h>
49 #include <contrib/dev/acpica/include/accommon.h>
50 #include <contrib/dev/acpica/include/acapps.h>
51
52 #define ACPI_OPTION_ERROR(msg, badchar) \
53     if (AcpiGbl_Opterr) {fprintf (stderr, "%s%c\n", msg, badchar);}
54
55
56 int   AcpiGbl_Opterr = 1;
57 int   AcpiGbl_Optind = 1;
58 char  *AcpiGbl_Optarg;
59
60
61 /*******************************************************************************
62  *
63  * FUNCTION:    AcpiGetopt
64  *
65  * PARAMETERS:  argc, argv          - from main
66  *              opts                - options info list
67  *
68  * RETURN:      Option character or EOF
69  *
70  * DESCRIPTION: Get the next option
71  *
72  ******************************************************************************/
73
74 int
75 AcpiGetopt(
76     int                     argc,
77     char                    **argv,
78     char                    *opts)
79 {
80     static int              CurrentCharPtr = 1;
81     int                     CurrentChar;
82     char                    *OptsPtr;
83
84
85     if (CurrentCharPtr == 1)
86     {
87         if (AcpiGbl_Optind >= argc ||
88             argv[AcpiGbl_Optind][0] != '-' ||
89             argv[AcpiGbl_Optind][1] == '\0')
90         {
91             return (EOF);
92         }
93         else if (strcmp (argv[AcpiGbl_Optind], "--") == 0)
94         {
95             AcpiGbl_Optind++;
96             return (EOF);
97         }
98     }
99
100     /* Get the option */
101
102     CurrentChar = argv[AcpiGbl_Optind][CurrentCharPtr];
103
104     /* Make sure that the option is legal */
105
106     if (CurrentChar == ':' ||
107        (OptsPtr = strchr (opts, CurrentChar)) == NULL)
108     {
109         ACPI_OPTION_ERROR ("Illegal option: -", CurrentChar);
110
111         if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
112         {
113             AcpiGbl_Optind++;
114             CurrentCharPtr = 1;
115         }
116
117         return ('?');
118     }
119
120     /* Option requires an argument? */
121
122     if (*++OptsPtr == ':')
123     {
124         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
125         {
126             AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
127         }
128         else if (++AcpiGbl_Optind >= argc)
129         {
130             ACPI_OPTION_ERROR ("Option requires an argument: -", CurrentChar);
131
132             CurrentCharPtr = 1;
133             return ('?');
134         }
135         else
136         {
137             AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
138         }
139
140         CurrentCharPtr = 1;
141     }
142
143     /* Option has optional single-char arguments? */
144
145     else if (*OptsPtr == '^')
146     {
147         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
148         {
149             AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
150         }
151         else
152         {
153             AcpiGbl_Optarg = "^";
154         }
155
156         AcpiGbl_Optind++;
157         CurrentCharPtr = 1;
158     }
159
160     /* Option has a required single-char argument? */
161
162     else if (*OptsPtr == '|')
163     {
164         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
165         {
166             AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
167         }
168         else
169         {
170             ACPI_OPTION_ERROR ("Option requires a single-character suboption: -", CurrentChar);
171
172             CurrentCharPtr = 1;
173             return ('?');
174         }
175
176         AcpiGbl_Optind++;
177         CurrentCharPtr = 1;
178     }
179
180     /* Option with no arguments */
181
182     else
183     {
184         if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
185         {
186             CurrentCharPtr = 1;
187             AcpiGbl_Optind++;
188         }
189
190         AcpiGbl_Optarg = NULL;
191     }
192
193     return (CurrentChar);
194 }