File: options.cxx

package info (click to toggle)
buici-clock 0.3.7
  • links: PTS
  • area: main
  • in suites: slink
  • size: 736 kB
  • ctags: 840
  • sloc: cpp: 4,551; makefile: 397; sh: 224; yacc: 175; lex: 127
file content (424 lines) | stat: -rw-r--r-- 10,581 bytes parent folder | download | duplicates (5)
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* options.cxx
     $Id: options.cxx,v 1.4 1998/10/14 00:40:07 elf Exp $

   written by Marc Singer
   20 April 1996
   
   This file is part of the project Buici.  See the file README for
   more information.

   Copyright (C) 1996-1997 Marc Singer

   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
   in a file called COPYING along with this program; if not, write to
   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
   02139, USA.

   ---------------------
   ABBREVIATED CHANGELOG
   ---------------------

   Option parsing and value storage.

   Version 0.4 (13 October 1997)
     Allow long options to be prefixed by a single dash.  This is for
     X-Windows compatibility.  This entails some fancy footwork for
     option parsing and the addition of an option flag to permit
     recognition of long options when looking for short ones.
   Version 0.3 (14 November 1996)
     Permit option arguments to be prefixed by '='.
   Version 0.2 (14 May 1996)
     Long option names prefixed by a period are interpreted without a
     dash.  This allows command parsing as well as option parsing.
   Version 0.1
     Short and long options.  Default and error option codes.  Long
     option prefix matching.

   -----
   NOTES
   -----

   - We should detect ambiguous partials.  We need to change
     fetch_option to return the error code and not the OPTION pointer.

*/

  /* ----- Includes */

#include "standard.h"
#include "options.h"


#define USE_PARTIALS		// Permit partial matches for long options
//#define TEST_OPTIONS


  /* ----- Class Globals/Statics */

typedef enum {
  STATE_0			= 0,
  STATE_FOUND_DASH		= 1,
  STATE_FOUND_DASHDASH		= 2,
} STATE_PARSE;

typedef enum {
  FETCH_DEFAULT			= 0,
  FETCH_SHORT			= 1,
  FETCH_LONG			= 2,
  FETCH_COMMAND			= 3,
} FETCH_MODE;


static int eval_option (char* pch, int cch, char* pchArgument, 
			OPTION* pOptions);
static OPTION* fetch_option (char* pch, FETCH_MODE mode, OPTION* rgOptions);
int fetch_argument (OPTION* pOption, char* pch, int argc, char** argv, 
		    char** ppchArgument);


  /* ----- Methods */

char* parse_application (char* pch)
{
  char* pchSep    = rindex (pch, '\\');
  char* pchSepAlt = rindex (pch, '/');
  char* pchColon  = rindex (pch, ':');
  char* pchDot    = rindex (pch, '.');

  if (pchSepAlt > pchSep)
    pchSep = pchSepAlt;
  if (pchColon > pchSep)
    pchSep = pchColon;
  pch = pchSep ? pchSep + 1 : pch;

  if (pchDot && strcasecmp (pch, ".exe"))
    *pchDot = 0;
  return pch;
}  /* parse_applications */


/* parse_options

   accepts the argument vector for the application and an option
   description array and parses the command line arguments.  The
   return value is zero if the parse succeeds or non-zero if there
   was an error.

*/

int parse_options (int argc, char** argv, OPTION* rgOptions, int* pargc_used)
{
  int argc_used;
  if (!pargc_used)
    pargc_used = &argc_used;
  *pargc_used = 0;

  int result = 0;

  int state = STATE_0;

  for (; argc; --argc, ++argv, ++*pargc_used) {
    char* pch;
    for (pch = *argv; *pch; ++pch) {
      OPTION* pOption;
      char* pchArgument = NULL;
      int cch;			// General purpose length storage

      switch (state) {

	case STATE_0:
	  if (*pch == '-') {
	    state = STATE_FOUND_DASH;
	    continue;
	  }  /* if */

	  if ((pOption = fetch_option (pch, FETCH_COMMAND, rgOptions))) {
	    if ((result = eval_option (pch, strlen (pch), NULL, pOption)))
	      return result;
	  }  /* if */
	  else if ((pOption = fetch_option (NULL, FETCH_DEFAULT, rgOptions))) {
	    if ((result = eval_option (NULL, 0, pch, pOption)))
	      return result;
	  }  /* else-if */
	  else
	    return OptErrOk;
	  pch += strlen (pch) - 1;
	  break;

	case STATE_FOUND_DASH:
	  if (*pch == '-') {
	    state = STATE_FOUND_DASHDASH;
	    continue;
	  }  /* if */

	  pOption = fetch_option (pch, FETCH_SHORT, rgOptions);
	  if (!pOption)
	    return OptErrUnrecognized;

	  if (result
	      = fetch_argument (pOption, pch, argc, argv, &pchArgument))
	    return result;
	  cch = (pOption->sz && pOption->sz[1] ? strlen (pch) : 1);

	  if ((result = eval_option (pch, cch, pchArgument, pOption)))
	    return result;

	  if (pchArgument) {
	    if (argc && argv[1] == pchArgument) {
	      --argc;
	      ++argv;
	      ++*pargc_used;
	    }
	    else
	      cch = strlen (pch);
	    state = STATE_0;
	  }
	  pch += cch - 1;
	  break;

	case STATE_FOUND_DASHDASH:
	  pOption = fetch_option (pch, FETCH_LONG, rgOptions);
	  if (!pOption)
	    return OptErrUnrecognized;

	  if (result
	      = fetch_argument (pOption, pch, argc, argv, &pchArgument))
	    return result;
	  cch = (pOption->sz && pOption->sz[1] ? strlen (pch) : 1);

	  if ((result = eval_option (pch, cch, pchArgument, pOption)))
	    return result;

	  if (pchArgument && (argc && argv[1] == pchArgument)) {
	    --argc;
	    ++argv;
	    ++*pargc_used;
	  }
	  pch += cch - 1;
	  state = STATE_0;
	  break;
      }  /* switch */
    }  /* for */
  }  /* for */
  return OptErrOk;
}  /* parse_options */


/* fetch_argument

   handle the complexity of argument fetching.  It accepts the parsed
   option and the argument pointers, argc and argv, and returns a
   pointer to the argument, if there is one.  

*/

int fetch_argument (OPTION* pOption, char* pch, int argc, char** argv, 
		    char** ppchArgument)
{
  *ppchArgument = NULL;
  if (!(pOption->flags & OptArg))
    return OptErrOk;
  
  if (!(pOption->sz && pOption->sz[0] && pOption->sz[1])) {
    if (pch[1]) {
      *ppchArgument = pch + 1;
      if (**ppchArgument == '=')
	++*ppchArgument;
    }
    else if (argc > 1)
      *ppchArgument = argv[1];
  }
  else {
    int cch = strcspn (pch, "=");
    if (pch[cch] == '=') {
      *ppchArgument = pch + cch + 1;
      pch[cch] = 0;		// Important for OptDefault?  Not really.
    }
    else if (argc > 1)
      *ppchArgument = argv[1];
  }
  if (!*ppchArgument || !**ppchArgument)
    return OptErrNoArgument;
  return OptErrOk;
}


/* fetch_option

   accepts a pointer into a word within the command line, usually
   after either a single or double dash, and a mode specifier that
   determines which types of options it can match.  It then tries to
   match that option string in the option data.  Long options and
   command options will match partials if the USE_PARTIALS macro is
   defined. 

 */

OPTION* fetch_option (char* pch, FETCH_MODE mode, OPTION* rgOptions)
{
  //  int cch = ((mode == FETCH_SHORT) ? 1 : (pch ? strlen (pch) : 0));
  int cch = pch ? strlen (pch) : 0;
  if (cch) {
    int cchOption = strcspn (pch, "=");
    if (cchOption < cch)
      cch = cchOption;
  }

  OPTION* pOptionDefault = NULL;
  OPTION* pOptionPartial = NULL;

  for (OPTION* pOption = rgOptions; pOption && pOption->sz; ++pOption) {
    if (   (pOption->flags & OptDefault)
	&& !pOptionDefault)
      pOptionDefault = pOption;

    if (!pch) {
      if (pOption->flags & OptNonoption)
	return pOption;
      continue;
    }  /* if */

    if (*pOption->sz != *pch)
      continue;

    bool fLongOption = pOption->sz && pOption->sz[0] && pOption->sz[1];

    if (   (mode == FETCH_COMMAND && !(pOption->flags & OptCommand))
	|| (mode != FETCH_COMMAND &&  (pOption->flags & OptCommand)))
      continue;

    if (mode == FETCH_SHORT) {
      if (!fLongOption)
	return pOption;
      if (!(pOption->flags & OptAllDash))
	continue;
    }
    if (strncmp (pch, pOption->sz, cch))
      continue;
    if (!pOption->sz[cch])
      return pOption;
    if (mode == FETCH_SHORT && fLongOption) // No partials for OptAllDash
      continue;
#if defined USE_PARTIALS
    pOptionPartial = pOptionPartial ? (OPTION*) -1 : pOption;
#endif
  }  /* for */

  return (pOptionPartial && pOptionPartial != (OPTION*) -1)
      ? pOptionPartial
      : pOptionDefault;
}  /* fetch_option */


/* eval_option

   accepts a pointer to the user's option string, a pointer to the
   option argument, and the fetched option pointer.

 */

int eval_option (char* pch, int cch, char* pchArgument, OPTION* pOption)
{
  if (pOption->pfn) {
    char sz[2];
    if (pOption->flags & OptDefault) {
      if (cch == 1) {
	sz[0] = *pch;
	sz[1] = 0;
	pchArgument = sz;
      }  /* if */
      else
	pchArgument = pch;
    }  /* if */
    return pOption->pfn (pOption, pchArgument)
      ? OptErrFail
      : OptErrOk;
  }  /* if */

  if (pOption->flags & OptDefault) {
    printf ("Unrecognized option '%.*s'\n", cch, pch);
    return OptErrUnrecognized;
  }

  if (pOption->flags & OptNonoption)
    return OptErrExit;

  if ((pOption->flags & OptSetMask) && pOption->pv) {
    long value = 1;
    if (pchArgument && !(pOption->flags & OptSetString))
      value = strtol (pchArgument, NULL, 0);
    else if (pOption->flags & OptClear)
      value = 0;

    switch (pOption->flags & OptSetType) {
      default:
	return OptErrBadOption;
      case OptSetInt:
	*(int*)   pOption->pv = int   (value);
	break;
      case OptSetShort:
	*(short*) pOption->pv = short (value);
	break;
      case OptSetLong:
	*(long*)  pOption->pv = long  (value);
	break;
      case OptSetString:
	*(char**) pOption->pv = pchArgument;
	break;
    }  /* switch */

    return OptErrOk;
  }  /* if */

    return OptErrBadOption;
}  /* eval_option */

#if defined (TEST_OPTIONS)

char* g_szOptS;

int do_a (OPTION*, char*)
{
  printf ("option a\n");
  return 0;
}

int do_unrecognized (OPTION*, char* sz)
{
  printf ("unrecognized option '%s'\n", sz);
}

OPTION rgOptions[] = 
{
  { "s",	OptArg | OptSetString, &g_szOptS			},
  { "string",	OptArg | OptSetString, &g_szOptS			},
  { "geom",	OptArg | OptSetString | OptAllDash, &g_szOptS		},
  { "garage",	OptArg | OptSetString | OptAllDash, &g_szOptS		},
  { "a",	0, NULL, do_a						},
  { "",		OptDefault, NULL, do_unrecognized			},
  { NULL								},
};

int main (int argc, char** argv)
{
  int argc_used;
  int result = parse_options (argc - 1, argv + 1, rgOptions, &argc_used);

  if (g_szOptS)
    printf ("option s '%s'\n", g_szOptS);
  printf ("result %d  argc_used %d\n", result, argc_used);

  return 0;
}

#endif