Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / nsprpub / lib / libc / src / plgetopt.c
blob0fe4fafe5ba008bda26aaf91ccc84a90ad5870b2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 ** File: plgetopt.c
40 ** Description: utilities to parse argc/argv
43 #include "prmem.h"
44 #include "prlog.h"
45 #include "prerror.h"
46 #include "plstr.h"
47 #include "plgetopt.h"
49 #include <string.h>
51 static char static_Nul = 0;
53 struct PLOptionInternal
55 const char *options; /* client options list specification */
56 PRIntn argc; /* original number of arguments */
57 char **argv; /* vector of pointers to arguments */
58 PRIntn xargc; /* which one we're processing now */
59 const char *xargv; /* where within *argv[xargc] */
60 PRIntn minus; /* do we already have the '-'? */
61 const PLLongOpt *longOpts; /* Caller's array */
62 PRBool endOfOpts; /* have reached a "--" argument */
63 PRIntn optionsLen; /* is strlen(options) */
67 ** Create the state in which to parse the tokens.
69 ** argc the sum of the number of options and their values
70 ** argv the options and their values
71 ** options vector of single character options w/ | w/o ':
73 PR_IMPLEMENT(PLOptState*) PL_CreateOptState(
74 PRIntn argc, char **argv, const char *options)
76 return PL_CreateLongOptState( argc, argv, options, NULL);
77 } /* PL_CreateOptState */
79 PR_IMPLEMENT(PLOptState*) PL_CreateLongOptState(
80 PRIntn argc, char **argv, const char *options,
81 const PLLongOpt *longOpts)
83 PLOptState *opt = NULL;
84 PLOptionInternal *internal;
86 if (NULL == options)
88 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
89 return opt;
92 opt = PR_NEWZAP(PLOptState);
93 if (NULL == opt)
95 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
96 return opt;
99 internal = PR_NEW(PLOptionInternal);
100 if (NULL == internal)
102 PR_DELETE(opt);
103 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
104 return NULL;
107 opt->option = 0;
108 opt->value = NULL;
109 opt->internal = internal;
110 opt->longOption = 0;
111 opt->longOptIndex = -1;
113 internal->argc = argc;
114 internal->argv = argv;
115 internal->xargc = 0;
116 internal->xargv = &static_Nul;
117 internal->minus = 0;
118 internal->options = options;
119 internal->longOpts = longOpts;
120 internal->endOfOpts = PR_FALSE;
121 internal->optionsLen = PL_strlen(options);
123 return opt;
124 } /* PL_CreateLongOptState */
127 ** Destroy object created by CreateOptState()
129 PR_IMPLEMENT(void) PL_DestroyOptState(PLOptState *opt)
131 PR_DELETE(opt->internal);
132 PR_DELETE(opt);
133 } /* PL_DestroyOptState */
135 PR_IMPLEMENT(PLOptStatus) PL_GetNextOpt(PLOptState *opt)
137 PLOptionInternal *internal = opt->internal;
139 opt->longOption = 0;
140 opt->longOptIndex = -1;
142 ** If the current xarg points to nul, advance to the next
143 ** element of the argv vector. If the vector index is equal
144 ** to argc, we're out of arguments, so return an EOL.
145 ** Note whether the first character of the new argument is
146 ** a '-' and skip by it if it is.
148 while (0 == *internal->xargv)
150 internal->xargc += 1;
151 if (internal->xargc >= internal->argc)
153 opt->option = 0;
154 opt->value = NULL;
155 return PL_OPT_EOL;
157 internal->xargv = internal->argv[internal->xargc];
158 internal->minus = 0;
159 if (!internal->endOfOpts && ('-' == *internal->xargv))
161 internal->minus++;
162 internal->xargv++; /* and consume */
163 if ('-' == *internal->xargv && internal->longOpts)
165 internal->minus++;
166 internal->xargv++;
167 if (0 == *internal->xargv)
169 internal->endOfOpts = PR_TRUE;
176 ** If we already have a '-' or '--' in hand, xargv points to the next
177 ** option. See if we can find a match in the list of possible
178 ** options supplied.
181 if (internal->minus == 2)
183 char * foundEqual = strchr(internal->xargv,'=');
184 PRIntn optNameLen = foundEqual ? (foundEqual - internal->xargv) :
185 strlen(internal->xargv);
186 const PLLongOpt *longOpt = internal->longOpts;
188 opt->option = 0;
189 opt->value = NULL;
191 for (; longOpt->longOptName; ++longOpt)
193 if (strncmp(longOpt->longOptName, internal->xargv, optNameLen))
194 continue; /* not a possible match */
195 if (strlen(longOpt->longOptName) != optNameLen)
196 continue; /* not a match */
197 /* option name match */
198 opt->longOptIndex = longOpt - internal->longOpts;
199 opt->longOption = longOpt->longOption;
200 if (foundEqual)
202 opt->value = foundEqual[1] ? foundEqual + 1 : NULL;
204 else if (longOpt->valueRequired)
206 opt->value = internal->argv[++(internal->xargc)];
208 internal->xargv = &static_Nul; /* consume this */
209 return PL_OPT_OK;
211 internal->xargv = &static_Nul; /* consume this */
212 return PL_OPT_BAD;
214 if (internal->minus)
216 PRIntn cop;
217 PRIntn eoo = internal->optionsLen;
218 for (cop = 0; cop < eoo; ++cop)
220 if (internal->options[cop] == *internal->xargv)
222 opt->option = *internal->xargv++;
223 opt->longOption = opt->option & 0xff;
225 ** if options indicates that there's an associated
226 ** value, this argv is finished and the next is the
227 ** option's value.
229 if (':' == internal->options[cop + 1])
231 if (0 != *internal->xargv)
232 return PL_OPT_BAD;
233 opt->value = internal->argv[++(internal->xargc)];
234 internal->xargv = &static_Nul;
235 internal->minus = 0;
237 else
238 opt->value = NULL;
239 return PL_OPT_OK;
242 internal->xargv += 1; /* consume that option */
243 return PL_OPT_BAD;
246 ** No '-', so it must be a standalone value. The option is nul.
248 opt->value = internal->argv[internal->xargc];
249 internal->xargv = &static_Nul;
250 opt->option = 0;
251 return PL_OPT_OK;
252 } /* PL_GetNextOpt */
254 /* plgetopt.c */