optparser.c

Mon, 16 Feb 2004 18:50:28 +0100

author
tuomov
date
Mon, 16 Feb 2004 18:50:28 +0100
changeset 62
aae5facf9fc5
parent 53
f8f9366b359c
child 70
e866dc825481
permissions
-rw-r--r--

trunk: changeset 1318
Switched to using spaces only for indentation. Simple automatic "\t"
-> " " conversion; may need tuning later.

0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
1 /*
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
2 * libtu/optparser.c
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
3 *
36
63cd573ffbcf trunk: changeset 39
tuomov
parents: 19
diff changeset
4 * Copyright (c) Tuomo Valkonen 1999-2002.
53
f8f9366b359c trunk: changeset 57
tuomov
parents: 36
diff changeset
5 *
f8f9366b359c trunk: changeset 57
tuomov
parents: 36
diff changeset
6 * You may distribute and modify this library under the terms of either
f8f9366b359c trunk: changeset 57
tuomov
parents: 36
diff changeset
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
8 */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
9
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
10 #include <string.h>
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
11 #include <stdlib.h>
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
12
5
f878a9ffa3e0 trunk: changeset 8
tuomov
parents: 3
diff changeset
13 #include <libtu/util.h>
f878a9ffa3e0 trunk: changeset 8
tuomov
parents: 3
diff changeset
14 #include <libtu/misc.h>
f878a9ffa3e0 trunk: changeset 8
tuomov
parents: 3
diff changeset
15 #include <libtu/optparser.h>
f878a9ffa3e0 trunk: changeset 8
tuomov
parents: 3
diff changeset
16 #include <libtu/output.h>
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
17
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
18
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
19 #define O_ARGS(o) (o->flags&OPT_OPT_ARG)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
20 #define O_ARG(o) (o->flasg&OPT_ARG)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
21 #define O_OPT_ARG(o) (O_ARGS(o)==OPT_OPT_ARG)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
22 #define O_ID(o) (o->optid)
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
23
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
24 #define OPT_ID_HELP OPT_ID_RESERVED('h')
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
25 #define OPT_ID_VERSION OPT_ID_RESERVED('V')
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
26 #define OPT_ID_ABOUT OPT_ID_RESERVED('a'|OPT_ID_NOSHORT_FLAG)
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
27
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
28
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
29 static OptParserOpt common_opts[]={
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
30 {OPT_ID_HELP, "help", 0, NULL, DUMMY_TR("Show this help")},
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
31 {OPT_ID_VERSION, "version", 0, NULL, DUMMY_TR("Show program version")},
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
32 {OPT_ID_ABOUT, "about", 0, NULL, DUMMY_TR("Show about text")},
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
33 {0, NULL, 0, NULL, NULL}
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
34 };
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
35
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
36
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
37 static OptParserCommonInfo dummy_cinfo[]={
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
38 NULL, /* version */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
39 "Usage: $p\n", /* usage_tmpl */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
40 NULL /* about */
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
41 };
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
42
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
43
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
44 static const OptParserOpt *o_opts=NULL;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
45 static char *const *o_current=NULL;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
46 static int o_left=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
47 static const char* o_chain_ptr=NULL;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
48 static int o_args_left=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
49 static const char*o_tmp=NULL;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
50 static int o_error=0;
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 0
diff changeset
51 static int o_mode=OPTP_CHAIN;
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
52 static const OptParserCommonInfo *o_cinfo=NULL;
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
53
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
54
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
55 /* */
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
56
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
57
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
58 static void print_help(const OptParserOpt *opts, bool midlong,
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
59 const OptParserCommonInfo *cinfo);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
60
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
61
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
62 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
63
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
64
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 0
diff changeset
65 void optparser_init(int argc, char *const argv[], int mode,
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
66 const OptParserOpt *opts, const OptParserCommonInfo *cinfo)
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
67 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
68 o_mode=mode;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
69 o_opts=(opts==NULL ? common_opts : opts);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
70 o_current=argv+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
71 o_left=argc-1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
72 o_chain_ptr=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
73 o_args_left=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
74 o_tmp=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
75 o_cinfo=(cinfo==NULL ? dummy_cinfo : cinfo);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
76 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
77
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
78
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
79 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
80
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
81
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
82 static const OptParserOpt *find_chain_opt(char p, const OptParserOpt *o)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
83 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
84 for(;O_ID(o);o++){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
85 if((O_ID(o)&~OPT_ID_RESERVED_FLAG)==p)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
86 return o;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
87 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
88 return NULL;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
89 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
90
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
91
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
92 static bool is_option(const char *p)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
93 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
94 if(p==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
95 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
96 if(*p++!='-')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
97 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
98 if(*p++!='-')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
99 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
100 if(*p=='\0')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
101 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
102 return TRUE;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
103 }
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
104
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
105
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
106 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
107
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 0
diff changeset
108 enum{
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
109 SHORT, MIDLONG, LONG
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 0
diff changeset
110 };
b1fbfab67908 trunk: changeset 6
tuomov
parents: 0
diff changeset
111
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
112
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
113 static int optparser_do_get_opt()
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
114 {
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
115 #define RET(X) return o_tmp=p, o_error=X
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
116 const char *p, *p2=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
117 bool dash=TRUE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
118 int type=SHORT;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
119 const OptParserOpt *o;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
120 int l;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
121
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
122 while(o_args_left)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
123 optparser_get_arg();
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
124
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
125 o_tmp=NULL;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
126
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
127 /* Are we doing a chain (i.e. opt. of style 'tar xzf')? */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
128 if(o_chain_ptr!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
129 p=o_chain_ptr++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
130 if(!*o_chain_ptr)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
131 o_chain_ptr=NULL;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
132
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
133 o=find_chain_opt(*p, o_opts);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
134
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
135 if(o==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
136 RET(E_OPT_INVALID_CHAIN_OPTION);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
137
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
138 goto do_arg;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
139 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
140
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
141 if(o_left<1)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
142 return OPT_ID_END;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
143
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
144 o_left--;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
145 p=*o_current++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
146
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
147 if(*p!='-'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
148 dash=FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
149 if(o_mode!=OPTP_NO_DASH)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
150 RET(OPT_ID_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
151 p2=p;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
152 }else if(*(p+1)=='-'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
153 /* --foo */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
154 if(*(p+2)=='\0'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
155 /* -- arguments */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
156 o_args_left=o_left;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
157 RET(OPT_ID_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
158 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
159 type=LONG;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
160 p2=p+2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
161 }else{
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
162 /* -foo */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
163 if(*(p+1)=='\0'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
164 /* - */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
165 o_args_left=1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
166 RET(OPT_ID_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
167 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
168 if(*(p+2)!='\0' && o_mode==OPTP_MIDLONG)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
169 type=MIDLONG;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
170
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
171 p2=p+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
172 }
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
173
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
174 o=o_opts;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
175
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
176 again:
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
177 for(; O_ID(o); o++){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
178 if(type==LONG){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
179 /* Do long option (--foo=bar) */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
180 if(o->longopt==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
181 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
182 l=strlen(o->longopt);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
183 if(strncmp(p2, o->longopt, l)!=0)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
184 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
185
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
186 if(p2[l]=='\0'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
187 if(O_ARGS(o)==OPT_ARG)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
188 RET(E_OPT_MISSING_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
189 return O_ID(o);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
190 }else if(p2[l]=='='){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
191 if(!O_ARGS(o))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
192 RET(E_OPT_UNEXPECTED_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
193 if(p2[l+1]=='\0')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
194 RET(E_OPT_MISSING_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
195 o_tmp=p2+l+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
196 o_args_left=1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
197 return O_ID(o);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
198 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
199 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
200 }else if(type==MIDLONG){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
201 if(o->longopt==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
202 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
203
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
204 if(strcmp(p2, o->longopt)!=0)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
205 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
206 }else{ /* type==SHORT */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
207 if(*p2!=(O_ID(o)&~OPT_ID_RESERVED_FLAG))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
208 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
209
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
210 if(*(p2+1)!='\0'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
211 if(o_mode==OPTP_CHAIN || o_mode==OPTP_NO_DASH){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
212 /*valid_chain(p2+1, o_opts)*/
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
213 o_chain_ptr=p2+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
214 p++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
215 }else if(o_mode==OPTP_IMMEDIATE){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
216 if(!O_ARGS(o)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
217 if(*(p2+1)!='\0')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
218 RET(E_OPT_UNEXPECTED_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
219 }else{
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
220 if(*(p2+1)=='\0')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
221 RET(E_OPT_MISSING_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
222 o_tmp=p2+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
223 o_args_left=1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
224 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
225 return O_ID(o);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
226 }else{
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
227 RET(E_OPT_SYNTAX_ERROR);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
228 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
229 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
230 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
231
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
232 do_arg:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
233
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
234 if(!O_ARGS(o))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
235 return O_ID(o);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
236
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
237 if(!o_left || is_option(*o_current)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
238 if(O_ARGS(o)==OPT_OPT_ARG)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
239 return O_ID(o);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
240 RET(E_OPT_MISSING_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
241 }
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
242
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
243 o_args_left=1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
244 return O_ID(o);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
245 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
246
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
247 if(o!=&(common_opts[3])){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
248 o=common_opts;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
249 goto again;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
250 }
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
251
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
252 if(dash)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
253 RET(E_OPT_INVALID_OPTION);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
254
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
255 RET(OPT_ID_ARGUMENT);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
256 #undef RET
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
257 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
258
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
259
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
260 int optparser_get_opt()
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
261 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
262 int oid=optparser_do_get_opt();
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
263
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
264 if(oid<=0 || (oid&OPT_ID_RESERVED_FLAG)==0)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
265 return oid;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
266
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
267 switch(oid){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
268 case OPT_ID_ABOUT:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
269 if(o_cinfo->about!=NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
270 printf("%s", o_cinfo->about);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
271 break;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
272
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
273 case OPT_ID_VERSION:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
274 if(o_cinfo->version!=NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
275 printf("%s\n", o_cinfo->version);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
276 break;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
277
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
278 case OPT_ID_HELP:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
279 print_help(o_opts, o_mode==OPTP_MIDLONG, o_cinfo);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
280 break;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
281 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
282
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
283 exit(EXIT_SUCCESS);
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
284 }
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
285
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
286
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
287 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
288
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
289
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
290 const char* optparser_get_arg()
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
291 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
292 const char *p;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
293
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
294 if(o_tmp!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
295 /* If o_args_left==0, then were returning an invalid option
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
296 * otherwise an immediate argument (e.g. -funsigned-char
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
297 * where '-f' is the option and 'unsigned-char' the argument)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
298 */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
299 if(o_args_left>0)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
300 o_args_left--;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
301 p=o_tmp;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
302 o_tmp=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
303 return p;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
304 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
305
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
306 if(o_args_left<1 || o_left<1)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
307 return NULL;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
308
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
309 o_left--;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
310 o_args_left--;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
311 return *o_current++;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
312 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
313
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
314
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
315 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
316
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
317 static void warn_arg(const char *e)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
318 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
319 const char *p=optparser_get_arg();
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
320
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
321 if(p==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
322 warn("%s (null)", e);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
323 else
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
324 warn("%s \'%s\'", e, p);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
325 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
326
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
327
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
328 static void warn_opt(const char *e)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
329 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
330 if(o_tmp!=NULL && o_chain_ptr!=NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
331 warn("%s \'-%c\'", e, *o_tmp);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
332 else
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
333 warn_arg(e);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
334 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
335
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
336
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
337 void optparser_print_error()
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
338 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
339 switch(o_error){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
340 case E_OPT_INVALID_OPTION:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
341 case E_OPT_INVALID_CHAIN_OPTION:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
342 warn_opt(TR("Invalid option"));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
343 break;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
344
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
345 case E_OPT_SYNTAX_ERROR:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
346 warn_arg(TR("Syntax error while parsing"));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
347 break;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
348
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
349 case E_OPT_MISSING_ARGUMENT:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
350 warn_opt(TR("Missing argument to"));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
351 break;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
352
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
353 case E_OPT_UNEXPECTED_ARGUMENT:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
354 warn_opt(TR("No argument expected:"));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
355 break;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
356
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
357 case OPT_ID_ARGUMENT:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
358 warn(TR("Unexpected argument"));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
359 break;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
360
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
361 default:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
362 warn(TR("(unknown error)"));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
363 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
364
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
365 o_tmp=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
366 o_error=0;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
367 }
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
368
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
369
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
370 /* */
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
371
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
372
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
373 static uint opt_w(const OptParserOpt *opt, bool midlong)
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
374 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
375 uint w=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
376
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
377 if((opt->optid&OPT_ID_NOSHORT_FLAG)==0){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
378 w+=2; /* "-o" */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
379 if(opt->longopt!=NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
380 w+=2; /* ", " */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
381 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
382
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
383 if(opt->longopt!=NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
384 w+=strlen(opt->longopt)+(midlong ? 1 : 2);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
385
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
386 if(O_ARGS(opt)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
387 if(opt->argname==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
388 w+=4;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
389 else
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
390 w+=1+strlen(opt->argname); /* "=ARG" or " ARG" */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
391
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
392 if(O_OPT_ARG(opt))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
393 w+=2; /* [ARG] */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
394 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
395
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
396 return w;
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
397 }
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
398
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
399
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
400 #define TERM_W 80
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
401 #define OFF1 2
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
402 #define OFF2 2
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
403 #define SPACER1 " "
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
404 #define SPACER2 " "
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
405
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
406 static void print_opt(const OptParserOpt *opt, bool midlong,
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
407 uint maxw, uint tw)
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
408 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
409 FILE *f=stdout;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
410 const char *p, *p2, *p3;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
411 uint w=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
412
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
413 fprintf(f, SPACER1);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
414
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
415 /* short opt */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
416
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
417 if((O_ID(opt)&OPT_ID_NOSHORT_FLAG)==0){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
418 fprintf(f, "-%c", O_ID(opt)&~OPT_ID_RESERVED_FLAG);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
419 w+=2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
420
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
421 if(opt->longopt!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
422 fprintf(f, ", ");
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
423 w+=2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
424 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
425 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
426
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
427 /* long opt */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
428
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
429 if(opt->longopt!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
430 if(midlong){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
431 w++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
432 fprintf(f, "-%s", opt->longopt);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
433 }else{
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
434 w+=2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
435 fprintf(f, "--%s", opt->longopt);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
436 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
437 w+=strlen(opt->longopt);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
438 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
439
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
440 /* arg */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
441
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
442 if(O_ARGS(opt)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
443 w++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
444 if(opt->longopt!=NULL && !midlong)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
445 putc('=', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
446 else
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
447 putc(' ', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
448
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
449 if(O_OPT_ARG(opt)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
450 w+=2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
451 putc('[', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
452 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
453
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
454 if(opt->argname!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
455 fprintf(f, "%s", opt->argname);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
456 w+=strlen(opt->argname);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
457 }else{
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
458 w+=3;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
459 fprintf(f, "ARG");
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
460 }
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
461
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
462 if(O_OPT_ARG(opt))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
463 putc(']', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
464 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
465
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
466 while(w++<maxw)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
467 putc(' ', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
468
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
469 /* descr */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
470
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
471 p=p2=opt->descr;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
472
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
473 if(p==NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
474 putc('\n', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
475 return;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
476 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
477
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
478 fprintf(f, SPACER2);
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
479
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
480 maxw+=OFF1+OFF2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
481 tw-=maxw;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
482
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
483 while(strlen(p)>tw){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
484 p3=p2=p+tw-2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
485
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
486 while(*p2!=' ' && p2!=p)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
487 p2--;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
488
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
489 while(*p3!=' ' && *p3!='\0')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
490 p3++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
491
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
492 if((uint)(p3-p2)>tw){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
493 /* long word - just wrap */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
494 p2=p+tw-2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
495 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
496
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
497 writef(f, p, p2-p);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
498 if(*p2==' ')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
499 putc('\n', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
500 else
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
501 fprintf(f, "\\\n");
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
502
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
503 p=p2+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
504
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
505 w=maxw;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
506 while(w--)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
507 putc(' ', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
508 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
509
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
510 fprintf(f, "%s\n", p);
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
511 }
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
512
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
513
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
514 static void print_opts(const OptParserOpt *opts, bool midlong,
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
515 const OptParserCommonInfo *cinfo)
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
516 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
517 uint w, maxw=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
518 const OptParserOpt *o;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
519
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
520 o=opts;
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
521 again:
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
522 for(; O_ID(o); o++){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
523 w=opt_w(o, midlong);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
524 if(w>maxw)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
525 maxw=w;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
526 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
527
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
528 if(o!=&(common_opts[3])){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
529 o=common_opts;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
530 goto again;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
531 }
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
532
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
533 o=opts;
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
534 again2:
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
535 for(; O_ID(o); o++)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
536 print_opt(o, midlong, maxw, TERM_W);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
537
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
538 if(o!=&(common_opts[3])){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
539 printf("\n");
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
540 o=common_opts;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
541 goto again2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
542 }
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
543 }
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
544
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
545
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
546 static void print_help(const OptParserOpt *opts, bool midlong,
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
547 const OptParserCommonInfo *cinfo)
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
548 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
549 const char *tmp, *p=cinfo->usage_tmpl;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
550 size_t len;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
551 size_t start;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
552
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
553 while(1){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
554 tmp=strchr(p, '$');
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
555
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
556 if(tmp==NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
557 writef(stdout, p, strlen(p));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
558 return;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
559 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
560
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
561 if(tmp!=p)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
562 writef(stdout, p, tmp-p);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
563
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
564 p=tmp+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
565
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
566 if(*p=='p'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
567 tmp=prog_execname();
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
568 writef(stdout, tmp, strlen(tmp));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
569 }else if(*p=='o'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
570 print_opts(opts, midlong, cinfo);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
571 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
572 p++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
573 }
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
574 }
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
575

mercurial