1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/lib/libc/include/plgetopt.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,125 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 +** File: plgetopt.h 1.11 +** Description: utilities to parse argc/argv 1.12 +*/ 1.13 + 1.14 +#if defined(PLGETOPT_H_) 1.15 +#else 1.16 +#define PLGETOPT_H_ 1.17 + 1.18 +#include "prtypes.h" 1.19 + 1.20 +PR_BEGIN_EXTERN_C 1.21 + 1.22 +typedef struct PLOptionInternal PLOptionInternal; 1.23 + 1.24 +typedef enum 1.25 +{ 1.26 + PL_OPT_OK, /* all's well with the option */ 1.27 + PL_OPT_EOL, /* end of options list */ 1.28 + PL_OPT_BAD /* invalid option (and value) */ 1.29 +} PLOptStatus; 1.30 + 1.31 +typedef struct PLLongOpt 1.32 +{ 1.33 + const char * longOptName; /* long option name string */ 1.34 + PRIntn longOption; /* value put in PLOptState for this option. */ 1.35 + PRBool valueRequired; /* If option name not followed by '=', */ 1.36 + /* value is the next argument from argv. */ 1.37 +} PLLongOpt; 1.38 + 1.39 +typedef struct PLOptState 1.40 +{ 1.41 + char option; /* the name of the option */ 1.42 + const char *value; /* the value of that option | NULL */ 1.43 + 1.44 + PLOptionInternal *internal; /* private processing state */ 1.45 + 1.46 + PRIntn longOption; /* value from PLLongOpt put here */ 1.47 + PRIntn longOptIndex; /* index into caller's array of PLLongOpts */ 1.48 +} PLOptState; 1.49 + 1.50 +/* 1.51 + * PL_CreateOptState 1.52 + * 1.53 + * The argument "options" points to a string of single-character option 1.54 + * names. Option names that may have an option argument value must be 1.55 + * followed immediately by a ':' character. 1.56 + */ 1.57 +PR_EXTERN(PLOptState*) PL_CreateOptState( 1.58 + PRIntn argc, char **argv, const char *options); 1.59 + 1.60 +/* 1.61 + * PL_CreateLongOptState 1.62 + * 1.63 + * Alternative to PL_CreateOptState. 1.64 + * Allows caller to specify BOTH a string of single-character option names, 1.65 + * AND an array of structures describing "long" (keyword) option names. 1.66 + * The array is terminated by a structure in which longOptName is NULL. 1.67 + * Long option values (arguments) may always be given as "--name=value". 1.68 + * If PLLongOpt.valueRequired is not PR_FALSE, and the option name was not 1.69 + * followed by '=' then the next argument from argv is taken as the value. 1.70 + */ 1.71 +PR_EXTERN(PLOptState*) PL_CreateLongOptState( 1.72 + PRIntn argc, char **argv, const char *options, 1.73 + const PLLongOpt *longOpts); 1.74 +/* 1.75 + * PL_DestroyOptState 1.76 + * 1.77 + * Call this to destroy the PLOptState returned from PL_CreateOptState or 1.78 + * PL_CreateLongOptState. 1.79 + */ 1.80 +PR_EXTERN(void) PL_DestroyOptState(PLOptState *opt); 1.81 + 1.82 +/* 1.83 + * PL_GetNextOpt 1.84 + * 1.85 + * When this function returns PL_OPT_OK, 1.86 + * - opt->option will hold the single-character option name that was parsed, 1.87 + * or zero. 1.88 + * When opt->option is zero, the token parsed was either a "long" (keyword) 1.89 + * option or a positional parameter. 1.90 + * For a positional parameter, 1.91 + * - opt->longOptIndex will contain -1, and 1.92 + * - opt->value will point to the positional parameter string. 1.93 + * For a long option name, 1.94 + * - opt->longOptIndex will contain the non-negative index of the 1.95 + * PLLongOpt structure in the caller's array of PLLongOpt structures 1.96 + * corresponding to the long option name, and 1.97 + * For a single-character or long option, 1.98 + * - opt->longOption will contain the value of the single-character option 1.99 + * name, or the value of the longOption from the PLLongOpt structure 1.100 + * for that long option. See notes below. 1.101 + * - opt->value will point to the argument option string, or will 1.102 + * be NULL if option does not require argument. If option requires 1.103 + * argument but it is not provided, PL_OPT_BAD is returned. 1.104 + * When opt->option is non-zero, 1.105 + * - opt->longOptIndex will be -1 1.106 + * When this function returns PL_OPT_EOL, or PL_OPT_BAD, the contents of 1.107 + * opt are undefined. 1.108 + * 1.109 + * Notes: It is possible to ignore opt->option, and always look at 1.110 + * opt->longOption instead. opt->longOption will contain the same value 1.111 + * as opt->option for single-character option names, and will contain the 1.112 + * value of longOption from the PLLongOpt structure for long option names. 1.113 + * This means that it is possible to equivalence long option names to 1.114 + * single character names by giving the longOption in the PLLongOpt struct 1.115 + * the same value as the single-character option name. 1.116 + * For long options that are NOT intended to be equivalent to any single- 1.117 + * character option, the longOption value should be chosen to not match 1.118 + * any possible single character name. It might be advisable to choose 1.119 + * longOption values greater than 0xff for such long options. 1.120 + */ 1.121 +PR_EXTERN(PLOptStatus) PL_GetNextOpt(PLOptState *opt); 1.122 + 1.123 +PR_END_EXTERN_C 1.124 + 1.125 +#endif /* defined(PLGETOPT_H_) */ 1.126 + 1.127 +/* plgetopt.h */ 1.128 +