1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
/* LIBDGL -- a Directed Graph Library implementation
* Copyright (C) 2002 Roberto Micarelli
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* best view tabstop=4
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "opt.h"
static int _ParseLongOption(GnoOption_s * pOpt, char *pszArg)
{
char *pszLong;
char *pszPar;
char *pszMatch = NULL;
int nret;
if (pOpt->pszLong == NULL) {
return 0;
}
pszLong = strdup(pOpt->pszLong);
if ((pszPar = strchr(pszArg, '=')) != NULL) {
*pszPar = 0;
}
pszMatch = strdup(pszArg);
if (pszPar)
*pszPar++ = '=';
if (strcmp(pszLong, pszMatch + 2) == 0) {
/* * mandatory parameter not found
* */
if (pszPar == NULL) {
nret = -1;
goto free_and_exit;
}
if (pOpt->ppszValue) {
if (pOpt->ppszValue[0])
free(pOpt->ppszValue[0]);
pOpt->ppszValue[0] = strdup(pszPar);
}
nret = 1;
goto free_and_exit;
}
nret = 0;
free_and_exit:
free(pszLong);
free(pszMatch);
return nret;
}
static int _ParseLongSwitch(GnoOption_s * pOpt, char *pszArg)
{
if (pOpt->pszLong == NULL) {
return 0;
}
if (strcmp(pOpt->pszLong, pszArg + 2) == 0) {
if (pOpt->pfValue)
*pOpt->pfValue = True;
return 1;
}
return 0;
}
static int _ParseShortOption(GnoOption_s * pOpt, char *pszArg, char *pszPar)
{
char *pszShort;
int ich;
if (pOpt->pszShort == NULL)
return 0;
pszShort = strdup(pOpt->pszShort);
for (ich = 1; pszArg[ich]; ich++) {
if (pszShort[0] == pszArg[ich]) {
if (pszPar == NULL || pszPar[0] == 0) {
free(pszShort);
return -1;
}
if (pszPar[0] == '-' && pszPar[1] != 0) {
free(pszShort);
return -1;
}
if (pOpt->ppszValue) {
if (pOpt->ppszValue[0])
free(pOpt->ppszValue[0]);
pOpt->ppszValue[0] = strdup(pszPar);
}
free(pszShort);
return 2;
}
}
free(pszShort);
return 0;
}
static int _ParseShortSwitch(GnoOption_s * pOpt, char *pszArg)
{
int ich;
if (pOpt->pszShort == NULL)
return 0;
for (ich = 1; pszArg[ich]; ich++) {
if (pOpt->pszShort[0] == pszArg[ich]) {
if (pOpt->pfValue)
*pOpt->pfValue = True;
return 1;
}
}
return 0;
}
/***********************************************************************
* CALLBACKS
**********************************************************************/
/***********************************************************************
* PUBLIC FUNCTIONS
**********************************************************************/
/*@*--------------------------------------------------------------------
* @func: GnoParse()
* @descr: Parse argc, argv against the option array and setup option
* values in the array.
*
* @args: I: argc = count of argv entries
* I: argv -> array of pointer to string
* I: pOpt -> option array pointer
*
* @ret: The number of 'orphan' entries found in the argv.
* @see: GnoOption_s
*
* @notes: The argv array will be modified: each argv entry that contains a
* recognized option ( '-.' or '--...' ) or each entry recognized as
* a parametric option parameter, will be set to NULL.
* Thus, at the function return the argv entries not set to NULL are
* those of orphan entries (those not related to any option).
* The user can then scan argv to find out orphans.
* However the number of argv entries will not be altered.
*
*--------------------------------------------------------------------*/
int GnoParse(int argc, char **argv, GnoOption_s * pOpt)
{
char *pszArgv;
char *pszArgvNxt;
int iArg, iOpt, cOrphan = 0;
int nret, cret;
Boolean fParseError = False;
/* * this first loop setup default values
* * strdup is used for non-switch options
* * to make life easier when freeing the field
* */
for (iOpt = 0; pOpt[iOpt].pszShort || pOpt[iOpt].pszLong; iOpt++) {
if (pOpt[iOpt].nFlg & GNO_FLG_SWITCH) {
if (pOpt[iOpt].pfValue) {
pOpt[iOpt].pfValue[0] = pOpt[iOpt].fDef;
}
}
else {
if (pOpt[iOpt].pszDef) {
if (pOpt[iOpt].ppszValue) {
pOpt[iOpt].ppszValue[0] = strdup(pOpt[iOpt].pszDef);
}
}
else {
if (pOpt[iOpt].ppszValue) {
pOpt[iOpt].ppszValue[0] = NULL;
}
}
}
}
/* * for each arg in argv lookup the matching options
* */
for (iArg = 0, pszArgv = NULL;
iArg < argc && (pszArgv = strdup(argv[iArg])) != NULL;
iArg++, free(pszArgv), pszArgv = NULL) {
if (pszArgv[0] == '-' && pszArgv[1] == '-' && pszArgv[2]) { /* long style */
for (iOpt = 0;
(pOpt[iOpt].pszShort || pOpt[iOpt].pszLong) && argv[iArg];
iOpt++) {
if (pOpt[iOpt].pszLong) {
if (pOpt[iOpt].nFlg & GNO_FLG_SWITCH) {
nret = _ParseLongSwitch(&pOpt[iOpt], pszArgv);
}
else {
nret = _ParseLongOption(&pOpt[iOpt], pszArgv);
}
if (nret < 0) {
fprintf(stderr,
"parse option: syntax error at <%s>\n",
pszArgv);
fParseError = True;
}
if (nret == 1) {
argv[iArg] = NULL;
}
}
}
if (argv[iArg]) {
fprintf(stderr, "parse option: <%s> is out of scope\n",
pszArgv);
fParseError = True;
}
}
else if (argv[iArg][0] == '-' && argv[iArg][1]) { /* short style */
if (iArg + 1 < argc) {
pszArgvNxt = strdup(argv[iArg + 1]);
}
else {
pszArgvNxt = NULL;
}
for (cret = iOpt = 0;
pOpt[iOpt].pszShort || pOpt[iOpt].pszLong; iOpt++) {
if (pOpt[iOpt].pszShort) {
if (pOpt[iOpt].nFlg & GNO_FLG_SWITCH) {
nret = _ParseShortSwitch(&pOpt[iOpt], pszArgv);
}
else {
nret =
_ParseShortOption(&pOpt[iOpt], pszArgv,
pszArgvNxt);
}
if (nret < 0) {
fprintf(stderr,
"parse option: syntax error at <%s>\n",
pszArgv);
fParseError = True;
}
else {
cret = (nret > cret) ? nret : cret;
}
}
}
if (pszArgvNxt) {
free(pszArgvNxt);
}
if (cret == 1) {
argv[iArg] = NULL;
}
else if (cret == 2) {
argv[iArg++] = NULL;
argv[iArg] = NULL;
}
}
else {
cOrphan++;
}
}
if (pszArgv)
free(pszArgv);
return (fParseError == True) ? -1 : cOrphan;
}
/*@*--------------------------------------------------------------------
* @func: GnoFree()
* @descr: Free resource previously created with a call to GnoParse()
*
* @args: I: pOpt -> option array pointer
*
* @see: GnoOption_s, GnoParse()
*
*--------------------------------------------------------------------*/
void GnoFree(GnoOption_s * pOpt)
{
int iOpt;
for (iOpt = 0; pOpt[iOpt].pszShort || pOpt[iOpt].pszLong; iOpt++) {
if (pOpt[iOpt].ppszValue) {
if (pOpt[iOpt].ppszValue[0]) {
free(pOpt[iOpt].ppszValue[0]);
pOpt[iOpt].ppszValue[0] = NULL;
}
}
}
}
/*@*--------------------------------------------------------------------
* @func: GnoHelp()
* @descr: Print a brief option's help on the standard error
*
* @args: I: pszHead -> help header string
*
* @args: I: pOpt -> option array pointer
*
* @see: GnoOption_s
*
*--------------------------------------------------------------------*/
void GnoHelp(char *pszHead, GnoOption_s * pOpt)
{
int iOpt;
fprintf(stderr, "%s\n", (pszHead) ? pszHead : "options");
for (iOpt = 0; pOpt[iOpt].pszShort || pOpt[iOpt].pszLong; iOpt++) {
if (pOpt[iOpt].nFlg & GNO_FLG_SWITCH) {
if (pOpt[iOpt].pszShort) {
fprintf(stderr, "-%s ", pOpt[iOpt].pszShort);
}
if (pOpt[iOpt].pszLong) {
fprintf(stderr, "--%s", pOpt[iOpt].pszLong);
}
fprintf(stderr, "\n\t%s\n", (pOpt[iOpt].pszDescr)
? pOpt[iOpt].pszDescr : "No description available.");
}
else {
if (pOpt[iOpt].pszShort) {
fprintf(stderr, "-%s ", pOpt[iOpt].pszShort);
fprintf(stderr, "<value> ");
}
if (pOpt[iOpt].pszLong) {
fprintf(stderr, "--%s", pOpt[iOpt].pszLong);
fprintf(stderr, "=<value>");
}
fprintf(stderr, "\n\t%s\n", (pOpt[iOpt].pszDescr)
? pOpt[iOpt].pszDescr : "No description available.");
}
}
}
/******************************* END OF FILE **************************/
|