fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
getoptb.c
Go to the documentation of this file.
1#include "buffer.h"
2#include "getoptb.h"
3
11#define optind subgetoptind
12#define optproblem subgetoptproblem
13
14int opterr = 1;
15char *optprogname = 0;
16
17int getoptb(int argc,char **argv,char *opts)
18{
19 int c;
20 char *s;
21
22 if (!optprogname) {
23 optprogname = *argv;
24 if (!optprogname) optprogname = "";
25 for (s = optprogname;*s;++s) if (*s == '/') optprogname = s + 1;
26 }
27 c = subgetopt(argc,argv,opts);
28 if (opterr)
29 if (c == '?') {
30 char chp[2]; chp[0] = optproblem; chp[1] = '\n';
32 if (argv[optind] && (optind < argc))
33 buffer_puts(buffer_2,": illegal option -- ");
34 else
35 buffer_puts(buffer_2,": option requires an argument -- ");
36 buffer_put(buffer_2,chp,2);
38 }
39 return c;
40}
41
42#define optpos subgetoptpos
43#define optarg subgetoptarg
44#define optdone subgetoptdone
45
46int optind = 1;
47int optpos = 0;
48char *optarg = 0;
49int optproblem = 0;
51
52int subgetopt(int argc,char **argv,char *opts)
53{
54 int c;
55 char *s;
56
57 optarg = 0;
58 if (!argv || (optind >= argc) || !argv[optind]) return optdone;
59 if (optpos && !argv[optind][optpos]) {
60 ++optind;
61 optpos = 0;
62 if ((optind >= argc) || !argv[optind]) return optdone;
63 }
64 if (!optpos) {
65 if (argv[optind][0] != '-') return optdone;
66 ++optpos;
67 c = argv[optind][1];
68 if ((c == '-') || (c == 0)) {
69 if (c) ++optind;
70 optpos = 0;
71 return optdone;
72 }
73 /* otherwise c is reassigned below */
74 }
75 c = argv[optind][optpos];
76 ++optpos;
77 s = opts;
78 while (*s) {
79 if (c == *s) {
80 if (s[1] == ':') {
81 optarg = argv[optind] + optpos;
82 ++optind;
83 optpos = 0;
84 if (!*optarg) {
85 optarg = argv[optind];
86 if ((optind >= argc) || !optarg) { /* argument past end */
87 optproblem = c;
88 return '?';
89 }
90 ++optind;
91 }
92 }
93 return c;
94 }
95 ++s;
96 if (*s == ':') ++s;
97 }
98 optproblem = c;
99 return '?';
100}
char * optprogname
Definition: getoptb.c:15
int getoptb(int argc, char **argv, char *opts)
Definition: getoptb.c:17
#define optpos
Definition: getoptb.c:42
int subgetopt(int argc, char **argv, char *opts)
Definition: getoptb.c:52
#define optarg
Definition: getoptb.c:43
#define optproblem
Definition: getoptb.c:12
int opterr
Definition: getoptb.c:14
#define optind
Definition: getoptb.c:11
#define optdone
Definition: getoptb.c:44
int buffer_puts(buffer *, const char *)
Definition: buffer.c:218
int buffer_put(buffer *, const char *, size_t)
Definition: buffer.c:185
buffer * buffer_2
Definition: buffer.c:48
int buffer_flush(buffer *)
Definition: buffer.c:161
#define SUBGETOPTDONE
Definition: getoptb.h:18