Back to index

lightning-sunbird  0.9+nobinonly
plgetopt.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** File:          plgetopt.c
00040 ** Description:   utilities to parse argc/argv
00041 */
00042 
00043 #include "prmem.h"
00044 #include "prlog.h"
00045 #include "prerror.h"
00046 #include "plstr.h"
00047 #include "plgetopt.h"
00048 
00049 #include <string.h>
00050 
00051 static char static_Nul = 0;
00052 
00053 struct PLOptionInternal
00054 {
00055     const char *options;        /* client options list specification */
00056     PRIntn argc;                /* original number of arguments */
00057     char **argv;                /* vector of pointers to arguments */
00058     PRIntn xargc;               /* which one we're processing now */
00059     const char *xargv;          /* where within *argv[xargc] */
00060     PRBool minus;               /* do we already have the '-'? */
00061 };
00062 
00063 /*
00064 ** Create the state in which to parse the tokens.
00065 **
00066 ** argc        the sum of the number of options and their values
00067 ** argv        the options and their values
00068 ** options    vector of single character options w/ | w/o ':
00069 */
00070 PR_IMPLEMENT(PLOptState*) PL_CreateOptState(
00071     PRIntn argc, char **argv, const char *options)
00072 {
00073     PLOptState *opt = NULL;
00074     if (NULL == options)
00075         PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
00076     else
00077     {
00078         opt = PR_NEWZAP(PLOptState);
00079         if (NULL == opt)
00080             PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
00081         else
00082         {
00083             PLOptionInternal *internal = PR_NEW(PLOptionInternal);
00084             if (NULL == internal)
00085             {
00086                 PR_DELETE(opt);
00087                 PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
00088             }
00089             else
00090             {
00091                             opt->option = 0;
00092                             opt->value = NULL;
00093                             opt->internal = internal;
00094 
00095                 internal->argc = argc;
00096                 internal->argv = argv;
00097                 internal->xargc = 0;
00098                 internal->xargv = &static_Nul;
00099                 internal->minus = PR_FALSE;
00100                 internal->options = options;
00101             }
00102         }
00103     }
00104     return opt;
00105 }  /* PL_CreateOptState */
00106 
00107 /*
00108 ** Destroy object created by CreateOptState()
00109 */
00110 PR_IMPLEMENT(void) PL_DestroyOptState(PLOptState *opt)
00111 {
00112     PR_DELETE(opt->internal);
00113     PR_DELETE(opt);
00114 }  /* PL_DestroyOptState */
00115 
00116 PR_IMPLEMENT(PLOptStatus) PL_GetNextOpt(PLOptState *opt)
00117 {
00118     PLOptionInternal *internal = opt->internal;
00119     PRIntn cop, eoo = PL_strlen(internal->options);
00120 
00121     /*
00122     ** If the current xarg points to nul, advance to the next
00123     ** element of the argv vector. If the vector index is equal
00124     ** to argc, we're out of arguments, so return an EOL.
00125        ** Note whether the first character of the new argument is
00126        ** a '-' and skip by it if it is.
00127     */
00128     while (0 == *internal->xargv)
00129     {
00130         internal->xargc += 1;
00131         if (internal->xargc >= internal->argc)
00132               {
00133                      opt->option = 0;
00134                      opt->value = NULL;
00135                      return PL_OPT_EOL;
00136               }
00137         internal->xargv = internal->argv[internal->xargc];
00138               internal->minus = ('-' == *internal->xargv ? PR_TRUE : PR_FALSE);  /* not it */
00139               if (internal->minus) internal->xargv += 1;  /* and consume */
00140     }
00141 
00142     /*
00143     ** If we already have a '-' in hand, xargv points to the next
00144     ** option. See if we can find a match in the list of possible
00145     ** options supplied.
00146     */
00147 
00148     if (internal->minus)
00149     {
00150         for (cop = 0; cop < eoo; ++cop)
00151         {
00152             if (internal->options[cop] == *internal->xargv)
00153             {
00154                 opt->option = *internal->xargv;
00155                 internal->xargv += 1;
00156                 /*
00157                 ** if options indicates that there's an associated
00158                             ** value, this argv is finished and the next is the
00159                             ** option's value.
00160                 */
00161                 if (':' == internal->options[cop + 1])
00162                 {
00163                     if (0 != *internal->xargv) return PL_OPT_BAD;
00164                     opt->value = internal->argv[++(internal->xargc)];
00165                     internal->xargv = &static_Nul;
00166                     internal->minus = PR_FALSE;
00167                 }
00168                             else opt->value = NULL;
00169                 return PL_OPT_OK;
00170             }
00171         }
00172         internal->xargv += 1;  /* consume that option */
00173         return PL_OPT_BAD;
00174     }
00175     /*
00176     ** No '-', so it must be a standalone value. The option is nul.
00177     */
00178     opt->value = internal->argv[internal->xargc];
00179     internal->xargv = &static_Nul;
00180     opt->option = 0;
00181     return PL_OPT_OK;
00182 }  /* PL_GetNextOpt */
00183 
00184 /* plgetopt.c */