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