Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* |
2 | * getlopt.c |
||
3 | * |
||
4 | * Oliver Fromme <oliver.fromme@heim3.tu-clausthal.de> |
||
5 | * Tue Apr 8 07:15:13 MET DST 1997 |
||
6 | */ |
||
7 | |||
8 | #include "getlopt.h" |
||
9 | |||
10 | int loptind = 1; /* index in argv[] */ |
||
11 | int loptchr = 0; /* index in argv[loptind] */ |
||
12 | char *loptarg; /* points to argument if present, else to option */ |
||
13 | |||
14 | #if defined(ultrix) || defined(ULTRIX) |
||
15 | char *strdup (char *src) |
||
16 | { |
||
17 | char *dest; |
||
18 | |||
19 | if (!(dest = (char *) malloc(strlen(src)+1))) |
||
20 | return (NULL); |
||
21 | return (strcpy(dest, src)); |
||
22 | } |
||
23 | #endif |
||
24 | |||
25 | topt *findopt (int islong, char *opt, topt *opts) |
||
26 | { |
||
27 | if (!opts) |
||
28 | return (0); |
||
29 | while (opts->lname) { |
||
30 | if (islong) { |
||
31 | if (!strcmp(opts->lname, opt)) |
||
32 | return (opts); |
||
33 | } |
||
34 | else |
||
35 | if (opts->sname == *opt) |
||
36 | return (opts); |
||
37 | opts++; |
||
38 | } |
||
39 | return (0); |
||
40 | } |
||
41 | |||
42 | int performoption (int argc, char *argv[], topt *opt) |
||
43 | { |
||
44 | int result = GLO_CONTINUE; |
||
45 | |||
46 | if (!(opt->flags & 1)) /* doesn't take argument */ |
||
47 | if (opt->var) |
||
48 | if (opt->flags & 2) /* var is *char */ |
||
49 | *((char *) opt->var) = (char) opt->value; |
||
50 | else |
||
51 | *((int *) opt->var) = opt->value; |
||
52 | else |
||
53 | result = opt->value ? opt->value : opt->sname; |
||
54 | else { /* requires argument */ |
||
55 | if (loptind >= argc) |
||
56 | return (GLO_NOARG); |
||
57 | loptarg = argv[loptind++]+loptchr; |
||
58 | loptchr = 0; |
||
59 | if (opt->var) |
||
60 | if (opt->flags & 2) /* var is *char */ |
||
61 | *((char **) opt->var) = strdup(loptarg); |
||
62 | else |
||
63 | *((int *) opt->var) = atoi(loptarg); |
||
64 | else |
||
65 | result = opt->value ? opt->value : opt->sname; |
||
66 | } |
||
67 | if (opt->func) |
||
68 | opt->func(loptarg); |
||
69 | return (result); |
||
70 | } |
||
71 | |||
72 | int getsingleopt (int argc, char *argv[], topt *opts) |
||
73 | { |
||
74 | char *thisopt; |
||
75 | topt *opt; |
||
76 | static char shortopt[2] = {0, 0}; |
||
77 | |||
78 | if (loptind >= argc) |
||
79 | return (GLO_END); |
||
80 | thisopt = argv[loptind]; |
||
81 | if (!loptchr) { /* start new option string */ |
||
82 | if (thisopt[0] != '-' || !thisopt[1]) /* no more options */ |
||
83 | return (GLO_END); |
||
84 | if (thisopt[1] == '-') /* "--" */ |
||
85 | if (thisopt[2]) { /* long option */ |
||
86 | loptarg = thisopt+2; |
||
87 | loptind++; |
||
88 | if (!(opt = findopt(1, thisopt+2, opts))) |
||
89 | return (GLO_UNKNOWN); |
||
90 | else |
||
91 | return (performoption(argc, argv, opt)); |
||
92 | } |
||
93 | else { /* "--" == end of options */ |
||
94 | loptind++; |
||
95 | return (GLO_END); |
||
96 | } |
||
97 | else /* start short option(s) */ |
||
98 | loptchr = 1; |
||
99 | } |
||
100 | shortopt[0] = thisopt[loptchr]; |
||
101 | loptarg = shortopt; |
||
102 | opt = findopt(0, thisopt+(loptchr++), opts); |
||
103 | if (!thisopt[loptchr]) { |
||
104 | loptind++; |
||
105 | loptchr = 0; |
||
106 | } |
||
107 | if (!opt) |
||
108 | return (GLO_UNKNOWN); |
||
109 | else |
||
110 | return (performoption(argc, argv, opt)); |
||
111 | } |
||
112 | |||
113 | int getlopt (int argc, char *argv[], topt *opts) |
||
114 | { |
||
115 | int result; |
||
116 | |||
117 | while ((result = getsingleopt(argc, argv, opts)) == GLO_CONTINUE); |
||
118 | return (result); |
||
119 | } |
||
120 | |||
121 | /* EOF */ |