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