File: options.cxx

package info (click to toggle)
curves 0.8.7
  • links: PTS
  • area: main
  • in suites: slink
  • size: 704 kB
  • ctags: 1,001
  • sloc: cpp: 6,197; ansic: 519; makefile: 270; sh: 192; fortran: 149
file content (335 lines) | stat: -rw-r--r-- 8,360 bytes parent folder | download | duplicates (2)
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
/* options.cxx
     $Id: options.cxx,v 1.4 1996/11/15 01:33:51 elf Exp $

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

   Copyright (C) 1996 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.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.

*/

  /* ----- Includes */

#include "std.h"
#include "options.h"


#define USE_PARTIALS		// Permit partial matches for long 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);


  /* ----- 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;

  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 OPTION_ERR_OK;
	  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 OPTION_ERR_UNRECOGNIZED;

	  if (pOption->flags & OPTION_F_ARGUMENT) {
	    if (*(pch + 1)) {
	      pchArgument = pch + 1;
	      if (*pchArgument == '=')
		++pchArgument;
	    }
	    else if (argc > 1) {
	      pchArgument = argv[1];
	      --argc;
	      ++argv;
	      ++*pargc_used;
	    }  /* else */
	    else
	      return OPTION_ERR_NOARGUMENT;
	  }  /* if */

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

	  if (pchArgument) {
	    pch += strlen (pch) - 1;
	    state = STATE_0;
	  }  /* if */
	  break;

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

	  cch = strlen (pch);
	  if (pOption->flags & OPTION_F_ARGUMENT) {
	    int cchOption = strcspn (pch, "=");
	    if (pch[cchOption] == '=') {
	      pchArgument = pch + cchOption + 1;
	      cch = cchOption;
	    }
	    else if (argc > 1) {
	      pchArgument = argv[1];
	      --argc;
	      ++argv;
	      ++*pargc_used;
	    }  /* else */
	    else
	      return OPTION_ERR_NOARGUMENT;
	  }  /* if */

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

	  pch += strlen (pch) - 1;

	  state = STATE_0;
	  break;
      }  /* switch */
    }  /* for */
  }  /* for */
  return OPTION_ERR_OK;
}  /* parse_options */


/* 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));
  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 & OPTION_F_DEFAULT)
	&& !pOptionDefault)
      pOptionDefault = pOption;

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

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

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

    if (cch == 1 && mode != FETCH_COMMAND) {
      if (!pOption->sz[1])
	return pOption;
      continue;
    }  /* if */
    if (strncmp (pch, pOption->sz, cch))
      continue;
    if (!pOption->sz[cch])
      return pOption;
#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 & OPTION_F_DEFAULT) {
      if (cch == 1) {
	sz[0] = *pch;
	sz[1] = 0;
	pchArgument = sz;
      }  /* if */
      else
	pchArgument = pch;
    }  /* if */
    return pOption->pfn (pOption, pchArgument)
      ? OPTION_ERR_FAIL
      : OPTION_ERR_OK;
  }  /* if */

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

  if (pOption->flags & OPTION_F_NONOPTION)
    return OPTION_ERR_EXIT;

  if ((pOption->flags & OPTION_F_SET_MASK) && pOption->pv) {
    long value = 1;
    if (pchArgument && !(pOption->flags & OPTION_F_SET_STRING))
      value = strtol (pchArgument, NULL, 0);
    else if (pOption->flags & OPTION_F_CLEAR)
      value = 0;

    switch (pOption->flags & OPTION_F_SET_TYPE) {
      default:
	return OPTION_ERR_BADOPTION;
      case OPTION_F_SET_INT:
	*(int*)   pOption->pv = int   (value);
	break;
      case OPTION_F_SET_SHORT:
	*(short*) pOption->pv = short (value);
	break;
      case OPTION_F_SET_LONG:
	*(long*)  pOption->pv = long  (value);
	break;
      case OPTION_F_SET_STRING:
	*(char**) pOption->pv = pchArgument;
	break;
    }  /* switch */

    return OPTION_ERR_OK;
  }  /* if */
  else
    return OPTION_ERR_BADOPTION;
}  /* eval_option */