comparison nspr/lib/libc/src/plgetopt.c @ 0:1e5118fa0cb1

This is NSS with a Cmake Buildsyste To compile a static NSS library for Windows we've used the Chromium-NSS fork and added a Cmake buildsystem to compile it statically for Windows. See README.chromium for chromium changes and README.trustbridge for our modifications.
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 28 Jul 2014 10:47:06 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e5118fa0cb1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /*
7 ** File: plgetopt.c
8 ** Description: utilities to parse argc/argv
9 */
10
11 #include "prmem.h"
12 #include "prlog.h"
13 #include "prerror.h"
14 #include "plstr.h"
15 #include "plgetopt.h"
16
17 #include <string.h>
18
19 static char static_Nul = 0;
20
21 struct PLOptionInternal
22 {
23 const char *options; /* client options list specification */
24 PRIntn argc; /* original number of arguments */
25 char **argv; /* vector of pointers to arguments */
26 PRIntn xargc; /* which one we're processing now */
27 const char *xargv; /* where within *argv[xargc] */
28 PRIntn minus; /* do we already have the '-'? */
29 const PLLongOpt *longOpts; /* Caller's array */
30 PRBool endOfOpts; /* have reached a "--" argument */
31 PRIntn optionsLen; /* is strlen(options) */
32 };
33
34 /*
35 ** Create the state in which to parse the tokens.
36 **
37 ** argc the sum of the number of options and their values
38 ** argv the options and their values
39 ** options vector of single character options w/ | w/o ':
40 */
41 PR_IMPLEMENT(PLOptState*) PL_CreateOptState(
42 PRIntn argc, char **argv, const char *options)
43 {
44 return PL_CreateLongOptState( argc, argv, options, NULL);
45 } /* PL_CreateOptState */
46
47 PR_IMPLEMENT(PLOptState*) PL_CreateLongOptState(
48 PRIntn argc, char **argv, const char *options,
49 const PLLongOpt *longOpts)
50 {
51 PLOptState *opt = NULL;
52 PLOptionInternal *internal;
53
54 if (NULL == options)
55 {
56 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
57 return opt;
58 }
59
60 opt = PR_NEWZAP(PLOptState);
61 if (NULL == opt)
62 {
63 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
64 return opt;
65 }
66
67 internal = PR_NEW(PLOptionInternal);
68 if (NULL == internal)
69 {
70 PR_DELETE(opt);
71 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
72 return NULL;
73 }
74
75 opt->option = 0;
76 opt->value = NULL;
77 opt->internal = internal;
78 opt->longOption = 0;
79 opt->longOptIndex = -1;
80
81 internal->argc = argc;
82 internal->argv = argv;
83 internal->xargc = 0;
84 internal->xargv = &static_Nul;
85 internal->minus = 0;
86 internal->options = options;
87 internal->longOpts = longOpts;
88 internal->endOfOpts = PR_FALSE;
89 internal->optionsLen = PL_strlen(options);
90
91 return opt;
92 } /* PL_CreateLongOptState */
93
94 /*
95 ** Destroy object created by CreateOptState()
96 */
97 PR_IMPLEMENT(void) PL_DestroyOptState(PLOptState *opt)
98 {
99 PR_DELETE(opt->internal);
100 PR_DELETE(opt);
101 } /* PL_DestroyOptState */
102
103 PR_IMPLEMENT(PLOptStatus) PL_GetNextOpt(PLOptState *opt)
104 {
105 PLOptionInternal *internal = opt->internal;
106
107 opt->longOption = 0;
108 opt->longOptIndex = -1;
109 /*
110 ** If the current xarg points to nul, advance to the next
111 ** element of the argv vector. If the vector index is equal
112 ** to argc, we're out of arguments, so return an EOL.
113 ** Note whether the first character of the new argument is
114 ** a '-' and skip by it if it is.
115 */
116 while (0 == *internal->xargv)
117 {
118 internal->xargc += 1;
119 if (internal->xargc >= internal->argc)
120 {
121 opt->option = 0;
122 opt->value = NULL;
123 return PL_OPT_EOL;
124 }
125 internal->xargv = internal->argv[internal->xargc];
126 internal->minus = 0;
127 if (!internal->endOfOpts && ('-' == *internal->xargv))
128 {
129 internal->minus++;
130 internal->xargv++; /* and consume */
131 if ('-' == *internal->xargv && internal->longOpts)
132 {
133 internal->minus++;
134 internal->xargv++;
135 if (0 == *internal->xargv)
136 {
137 internal->endOfOpts = PR_TRUE;
138 }
139 }
140 }
141 }
142
143 /*
144 ** If we already have a '-' or '--' in hand, xargv points to the next
145 ** option. See if we can find a match in the list of possible
146 ** options supplied.
147 */
148 if (internal->minus == 2)
149 {
150 char * foundEqual = strchr(internal->xargv,'=');
151 PRIntn optNameLen = foundEqual ? (foundEqual - internal->xargv) :
152 strlen(internal->xargv);
153 const PLLongOpt *longOpt = internal->longOpts;
154 PLOptStatus result = PL_OPT_BAD;
155
156 opt->option = 0;
157 opt->value = NULL;
158
159 for (; longOpt->longOptName; ++longOpt)
160 {
161 if (strncmp(longOpt->longOptName, internal->xargv, optNameLen))
162 continue; /* not a possible match */
163 if (strlen(longOpt->longOptName) != optNameLen)
164 continue; /* not a match */
165 /* option name match */
166 opt->longOptIndex = longOpt - internal->longOpts;
167 opt->longOption = longOpt->longOption;
168 /* value is part of the current argv[] element if = was found */
169 /* note: this sets value even for long options that do not
170 * require option if specified as --long=value */
171 if (foundEqual)
172 {
173 opt->value = foundEqual + 1;
174 }
175 else if (longOpt->valueRequired)
176 {
177 /* value is the next argv[] element, if any */
178 if (internal->xargc + 1 < internal->argc)
179 {
180 opt->value = internal->argv[++(internal->xargc)];
181 }
182 /* missing value */
183 else
184 {
185 break; /* return PL_OPT_BAD */
186 }
187 }
188 result = PL_OPT_OK;
189 break;
190 }
191 internal->xargv = &static_Nul; /* consume this */
192 return result;
193 }
194 if (internal->minus)
195 {
196 PRIntn cop;
197 PRIntn eoo = internal->optionsLen;
198 for (cop = 0; cop < eoo; ++cop)
199 {
200 if (internal->options[cop] == *internal->xargv)
201 {
202 opt->option = *internal->xargv++;
203 opt->longOption = opt->option & 0xff;
204 /*
205 ** if options indicates that there's an associated
206 ** value, it must be provided, either as part of this
207 ** argv[] element or as the next one
208 */
209 if (':' == internal->options[cop + 1])
210 {
211 /* value is part of the current argv[] element */
212 if (0 != *internal->xargv)
213 {
214 opt->value = internal->xargv;
215 }
216 /* value is the next argv[] element, if any */
217 else if (internal->xargc + 1 < internal->argc)
218 {
219 opt->value = internal->argv[++(internal->xargc)];
220 }
221 /* missing value */
222 else
223 {
224 return PL_OPT_BAD;
225 }
226
227 internal->xargv = &static_Nul;
228 internal->minus = 0;
229 }
230 else
231 opt->value = NULL;
232 return PL_OPT_OK;
233 }
234 }
235 internal->xargv += 1; /* consume that option */
236 return PL_OPT_BAD;
237 }
238
239 /*
240 ** No '-', so it must be a standalone value. The option is nul.
241 */
242 opt->value = internal->argv[internal->xargc];
243 internal->xargv = &static_Nul;
244 opt->option = 0;
245 return PL_OPT_OK;
246 } /* PL_GetNextOpt */
247
248 /* plgetopt.c */
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)