1: /*
2: * Generalized argument parser for use by people who are tired of writing
3: * and rewriting their own parsers. This is general enough for my use
4: * but others might find the conventions it imposes to be too restrictive.
5: *
6: * Written 3-Dec-81 by Michael Toy
7: */
8:
9: #include <stdio.h>
10: #include "args.h"
11: char _bf[52];
12: char *_sf[52];
13: char **_argspace, **_ap;
14: int nargs = 0;
15:
16: parse_args(ac, av, bopts, fopts)
17: int ac;
18: char **av, *bopts, *fopts;
19: {
20: int argc = ac, errcnt = 0;
21: char **argv = av, *pname, **ap, *cp;
22:
23: pname = argv[0]; /* Saved for error messages */
24: _ap = _argspace = ap = (char **) calloc(argc, sizeof (char *));
25: /*
26: * Loop once through the argument list. Record boolean and string
27: * flags and count the number of arguments, saving each normal arg.
28: */
29: while (--argc) {
30: cp = *++argv;
31: /*
32: * A flag string begins with a - unless it is a -- which is
33: * a dash escape.
34: */
35: if (*cp == '-' && cp[1] != '-') {
36: /*
37: * Process each flag in the string. Set boolean flags and
38: * save string flags
39: */
40: while(*++cp) {
41: if (index(bopts, *cp) != 0)
42: bools(*cp) = 1; /* Set bool */
43: else if (index(fopts, *cp))
44: if (argc > 1) {
45: argc--;
46: strings(*cp) = newstr(*++argv); /* Save string */
47: } else {
48: fprintf(stderr,
49: "%s: Argument for %c flag not specified\n",
50: pname, *cp);
51: errcnt++;
52: }
53: else {
54: fprintf(stderr, "%s: No such flag as %c\n", pname, *cp);
55: errcnt++;
56: }
57: }
58: } else {
59: /*
60: * It is a regular argument, just save it
61: */
62: *ap++ = newstr(cp);
63: nargs++;
64: }
65: }
66: *ap++ = (char *) 0;
67: return errcnt;
68: }
69:
70: char *newstr(s)
71: char *s;
72: {
73: char *cp;
74:
75: cp = (char *) calloc(1, strlen(s)+1);
76: strcpy(cp, s);
77: return cp;
78: }
Defined functions
Defined variables
_ap
defined in line
13; used 3 times
_bf
defined in line
11; used 1 times
_sf
defined in line
12; used 1 times
nargs
defined in line
14; used 1 times