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
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.
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 ***** */
40 ** Description: utilities to parse argc/argv
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
;
88 PR_SetError(PR_INVALID_ARGUMENT_ERROR
, 0);
92 opt
= PR_NEWZAP(PLOptState
);
95 PR_SetError(PR_OUT_OF_MEMORY_ERROR
, 0);
99 internal
= PR_NEW(PLOptionInternal
);
100 if (NULL
== internal
)
103 PR_SetError(PR_OUT_OF_MEMORY_ERROR
, 0);
109 opt
->internal
= internal
;
111 opt
->longOptIndex
= -1;
113 internal
->argc
= argc
;
114 internal
->argv
= argv
;
116 internal
->xargv
= &static_Nul
;
118 internal
->options
= options
;
119 internal
->longOpts
= longOpts
;
120 internal
->endOfOpts
= PR_FALSE
;
121 internal
->optionsLen
= PL_strlen(options
);
124 } /* PL_CreateLongOptState */
127 ** Destroy object created by CreateOptState()
129 PR_IMPLEMENT(void) PL_DestroyOptState(PLOptState
*opt
)
131 PR_DELETE(opt
->internal
);
133 } /* PL_DestroyOptState */
135 PR_IMPLEMENT(PLOptStatus
) PL_GetNextOpt(PLOptState
*opt
)
137 PLOptionInternal
*internal
= opt
->internal
;
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
)
157 internal
->xargv
= internal
->argv
[internal
->xargc
];
159 if (!internal
->endOfOpts
&& ('-' == *internal
->xargv
))
162 internal
->xargv
++; /* and consume */
163 if ('-' == *internal
->xargv
&& internal
->longOpts
)
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
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
;
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
;
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 */
211 internal
->xargv
= &static_Nul
; /* consume this */
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
229 if (':' == internal
->options
[cop
+ 1])
231 if (0 != *internal
->xargv
)
233 opt
->value
= internal
->argv
[++(internal
->xargc
)];
234 internal
->xargv
= &static_Nul
;
242 internal
->xargv
+= 1; /* consume that option */
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
;
252 } /* PL_GetNextOpt */