optparser.c

Mon, 17 Jan 2005 22:02:09 +0100

author
tuomov
date
Mon, 17 Jan 2005 22:02:09 +0100
changeset 85
9f94b2e96e3b
parent 79
81e9a32b1c0c
child 116
5f66d3bb1c68
permissions
-rw-r--r--

trunk: changeset 1934
Fixed everything that requires locale stuff to check CF_NO_LOCALE.

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 *
79
81e9a32b1c0c trunk: changeset 1689
tuomov
parents: 70
diff changeset
4 * Copyright (c) Tuomo Valkonen 1999-2004.
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
70
e866dc825481 trunk: changeset 1408
tuomov
parents: 62
diff changeset
13 #include "util.h"
e866dc825481 trunk: changeset 1408
tuomov
parents: 62
diff changeset
14 #include "misc.h"
e866dc825481 trunk: changeset 1408
tuomov
parents: 62
diff changeset
15 #include "optparser.h"
e866dc825481 trunk: changeset 1408
tuomov
parents: 62
diff changeset
16 #include "output.h"
79
81e9a32b1c0c trunk: changeset 1689
tuomov
parents: 70
diff changeset
17 #include "private.h"
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
18
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
19
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
20 #define O_ARGS(o) (o->flags&OPT_OPT_ARG)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
21 #define O_ARG(o) (o->flasg&OPT_ARG)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
22 #define O_OPT_ARG(o) (O_ARGS(o)==OPT_OPT_ARG)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
23 #define O_ID(o) (o->optid)
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
24
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
25
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
26 static const OptParserOpt *o_opts=NULL;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
27 static char *const *o_current=NULL;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
28 static int o_left=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
29 static const char* o_chain_ptr=NULL;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
30 static int o_args_left=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
31 static const char*o_tmp=NULL;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
32 static int o_error=0;
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 0
diff changeset
33 static int o_mode=OPTP_CHAIN;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
34
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
35
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
36 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
37
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
38
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 0
diff changeset
39 void optparser_init(int argc, char *const argv[], int mode,
79
81e9a32b1c0c trunk: changeset 1689
tuomov
parents: 70
diff changeset
40 const OptParserOpt *opts)
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
41 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
42 o_mode=mode;
79
81e9a32b1c0c trunk: changeset 1689
tuomov
parents: 70
diff changeset
43 o_opts=opts;
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
44 o_current=argv+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
45 o_left=argc-1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
46 o_chain_ptr=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
47 o_args_left=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
48 o_tmp=NULL;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
49 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
50
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
51
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
52 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
53
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
54
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
55 static const OptParserOpt *find_chain_opt(char p, const OptParserOpt *o)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
56 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
57 for(;O_ID(o);o++){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
58 if((O_ID(o)&~OPT_ID_RESERVED_FLAG)==p)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
59 return o;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
60 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
61 return NULL;
0
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
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
65 static bool is_option(const char *p)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
66 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
67 if(p==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
68 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
69 if(*p++!='-')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
70 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
71 if(*p++!='-')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
72 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
73 if(*p=='\0')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
74 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
75 return TRUE;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
76 }
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
77
0
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
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 0
diff changeset
81 enum{
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
82 SHORT, MIDLONG, LONG
3
b1fbfab67908 trunk: changeset 6
tuomov
parents: 0
diff changeset
83 };
b1fbfab67908 trunk: changeset 6
tuomov
parents: 0
diff changeset
84
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
85
79
81e9a32b1c0c trunk: changeset 1689
tuomov
parents: 70
diff changeset
86 int optparser_get_opt()
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
87 {
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
88 #define RET(X) return o_tmp=p, o_error=X
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
89 const char *p, *p2=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
90 bool dash=TRUE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
91 int type=SHORT;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
92 const OptParserOpt *o;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
93 int l;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
94
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
95 while(o_args_left)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
96 optparser_get_arg();
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
97
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
98 o_tmp=NULL;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
99
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
100 /* Are we doing a chain (i.e. opt. of style 'tar xzf')? */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
101 if(o_chain_ptr!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
102 p=o_chain_ptr++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
103 if(!*o_chain_ptr)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
104 o_chain_ptr=NULL;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
105
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
106 o=find_chain_opt(*p, o_opts);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
107
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
108 if(o==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
109 RET(E_OPT_INVALID_CHAIN_OPTION);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
110
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
111 goto do_arg;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
112 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
113
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
114 if(o_left<1)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
115 return OPT_ID_END;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
116
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
117 o_left--;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
118 p=*o_current++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
119
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
120 if(*p!='-'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
121 dash=FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
122 if(o_mode!=OPTP_NO_DASH)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
123 RET(OPT_ID_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
124 p2=p;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
125 }else if(*(p+1)=='-'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
126 /* --foo */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
127 if(*(p+2)=='\0'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
128 /* -- arguments */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
129 o_args_left=o_left;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
130 RET(OPT_ID_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
131 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
132 type=LONG;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
133 p2=p+2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
134 }else{
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
135 /* -foo */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
136 if(*(p+1)=='\0'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
137 /* - */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
138 o_args_left=1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
139 RET(OPT_ID_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
140 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
141 if(*(p+2)!='\0' && o_mode==OPTP_MIDLONG)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
142 type=MIDLONG;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
143
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
144 p2=p+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
145 }
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
146
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
147 o=o_opts;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
148
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
149 for(; O_ID(o); o++){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
150 if(type==LONG){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
151 /* Do long option (--foo=bar) */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
152 if(o->longopt==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
153 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
154 l=strlen(o->longopt);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
155 if(strncmp(p2, o->longopt, l)!=0)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
156 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
157
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
158 if(p2[l]=='\0'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
159 if(O_ARGS(o)==OPT_ARG)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
160 RET(E_OPT_MISSING_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
161 return O_ID(o);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
162 }else if(p2[l]=='='){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
163 if(!O_ARGS(o))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
164 RET(E_OPT_UNEXPECTED_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
165 if(p2[l+1]=='\0')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
166 RET(E_OPT_MISSING_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
167 o_tmp=p2+l+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
168 o_args_left=1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
169 return O_ID(o);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
170 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
171 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
172 }else if(type==MIDLONG){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
173 if(o->longopt==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
174 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
175
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
176 if(strcmp(p2, o->longopt)!=0)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
177 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
178 }else{ /* type==SHORT */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
179 if(*p2!=(O_ID(o)&~OPT_ID_RESERVED_FLAG))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
180 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
181
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
182 if(*(p2+1)!='\0'){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
183 if(o_mode==OPTP_CHAIN || o_mode==OPTP_NO_DASH){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
184 /*valid_chain(p2+1, o_opts)*/
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
185 o_chain_ptr=p2+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
186 p++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
187 }else if(o_mode==OPTP_IMMEDIATE){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
188 if(!O_ARGS(o)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
189 if(*(p2+1)!='\0')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
190 RET(E_OPT_UNEXPECTED_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
191 }else{
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
192 if(*(p2+1)=='\0')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
193 RET(E_OPT_MISSING_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
194 o_tmp=p2+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
195 o_args_left=1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
196 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
197 return O_ID(o);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
198 }else{
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
199 RET(E_OPT_SYNTAX_ERROR);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
200 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
201 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
202 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
203
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
204 do_arg:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
205
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
206 if(!O_ARGS(o))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
207 return O_ID(o);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
208
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
209 if(!o_left || is_option(*o_current)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
210 if(O_ARGS(o)==OPT_OPT_ARG)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
211 return O_ID(o);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
212 RET(E_OPT_MISSING_ARGUMENT);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
213 }
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
214
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
215 o_args_left=1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
216 return O_ID(o);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
217 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
218
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
219 if(dash)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
220 RET(E_OPT_INVALID_OPTION);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
221
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
222 RET(OPT_ID_ARGUMENT);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
223 #undef RET
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
224 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
225
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
226
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
227 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
228
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
229
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
230 const char* optparser_get_arg()
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
231 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
232 const char *p;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
233
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
234 if(o_tmp!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
235 /* If o_args_left==0, then were returning an invalid option
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
236 * otherwise an immediate argument (e.g. -funsigned-char
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
237 * where '-f' is the option and 'unsigned-char' the argument)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
238 */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
239 if(o_args_left>0)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
240 o_args_left--;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
241 p=o_tmp;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
242 o_tmp=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
243 return p;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
244 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
245
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
246 if(o_args_left<1 || o_left<1)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
247 return NULL;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
248
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
249 o_left--;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
250 o_args_left--;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
251 return *o_current++;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
252 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
253
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
254
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
255 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
256
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
257 static void warn_arg(const char *e)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
258 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
259 const char *p=optparser_get_arg();
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
260
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
261 if(p==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
262 warn("%s (null)", e);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
263 else
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
264 warn("%s \'%s\'", e, p);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
265 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
266
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
267
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
268 static void warn_opt(const char *e)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
269 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
270 if(o_tmp!=NULL && o_chain_ptr!=NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
271 warn("%s \'-%c\'", e, *o_tmp);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
272 else
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
273 warn_arg(e);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
274 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
275
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
276
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
277 void optparser_print_error()
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
278 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
279 switch(o_error){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
280 case E_OPT_INVALID_OPTION:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
281 case E_OPT_INVALID_CHAIN_OPTION:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
282 warn_opt(TR("Invalid option"));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
283 break;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
284
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
285 case E_OPT_SYNTAX_ERROR:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
286 warn_arg(TR("Syntax error while parsing"));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
287 break;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
288
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
289 case E_OPT_MISSING_ARGUMENT:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
290 warn_opt(TR("Missing argument to"));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
291 break;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
292
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
293 case E_OPT_UNEXPECTED_ARGUMENT:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
294 warn_opt(TR("No argument expected:"));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
295 break;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
296
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
297 case OPT_ID_ARGUMENT:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
298 warn(TR("Unexpected argument"));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
299 break;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
300
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
301 default:
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
302 warn(TR("(unknown error)"));
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
303 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
304
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
305 o_tmp=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
306 o_error=0;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
307 }
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
308
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
309
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
310 /* */
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
311
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
312
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
313 static uint opt_w(const OptParserOpt *opt, bool midlong)
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
314 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
315 uint w=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
316
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
317 if((opt->optid&OPT_ID_NOSHORT_FLAG)==0){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
318 w+=2; /* "-o" */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
319 if(opt->longopt!=NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
320 w+=2; /* ", " */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
321 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
322
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
323 if(opt->longopt!=NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
324 w+=strlen(opt->longopt)+(midlong ? 1 : 2);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
325
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
326 if(O_ARGS(opt)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
327 if(opt->argname==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
328 w+=4;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
329 else
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
330 w+=1+strlen(opt->argname); /* "=ARG" or " ARG" */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
331
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
332 if(O_OPT_ARG(opt))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
333 w+=2; /* [ARG] */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
334 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
335
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
336 return w;
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
337 }
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
338
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
339
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
340 #define TERM_W 80
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
341 #define OFF1 2
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
342 #define OFF2 2
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
343 #define SPACER1 " "
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
344 #define SPACER2 " "
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
345
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
346 static void print_opt(const OptParserOpt *opt, bool midlong,
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
347 uint maxw, uint tw)
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
348 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
349 FILE *f=stdout;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
350 const char *p, *p2, *p3;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
351 uint w=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
352
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
353 fprintf(f, SPACER1);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
354
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
355 /* short opt */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
356
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
357 if((O_ID(opt)&OPT_ID_NOSHORT_FLAG)==0){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
358 fprintf(f, "-%c", O_ID(opt)&~OPT_ID_RESERVED_FLAG);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
359 w+=2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
360
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
361 if(opt->longopt!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
362 fprintf(f, ", ");
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
363 w+=2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
364 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
365 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
366
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
367 /* long opt */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
368
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
369 if(opt->longopt!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
370 if(midlong){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
371 w++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
372 fprintf(f, "-%s", opt->longopt);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
373 }else{
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
374 w+=2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
375 fprintf(f, "--%s", opt->longopt);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
376 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
377 w+=strlen(opt->longopt);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
378 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
379
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
380 /* arg */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
381
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
382 if(O_ARGS(opt)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
383 w++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
384 if(opt->longopt!=NULL && !midlong)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
385 putc('=', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
386 else
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
387 putc(' ', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
388
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
389 if(O_OPT_ARG(opt)){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
390 w+=2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
391 putc('[', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
392 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
393
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
394 if(opt->argname!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
395 fprintf(f, "%s", opt->argname);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
396 w+=strlen(opt->argname);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
397 }else{
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
398 w+=3;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
399 fprintf(f, "ARG");
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
400 }
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
401
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
402 if(O_OPT_ARG(opt))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
403 putc(']', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
404 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
405
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
406 while(w++<maxw)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
407 putc(' ', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
408
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
409 /* descr */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
410
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
411 p=p2=opt->descr;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
412
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
413 if(p==NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
414 putc('\n', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
415 return;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
416 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
417
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
418 fprintf(f, SPACER2);
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
419
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
420 maxw+=OFF1+OFF2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
421 tw-=maxw;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
422
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
423 while(strlen(p)>tw){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
424 p3=p2=p+tw-2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
425
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
426 while(*p2!=' ' && p2!=p)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
427 p2--;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
428
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
429 while(*p3!=' ' && *p3!='\0')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
430 p3++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
431
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
432 if((uint)(p3-p2)>tw){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
433 /* long word - just wrap */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
434 p2=p+tw-2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
435 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
436
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
437 writef(f, p, p2-p);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
438 if(*p2==' ')
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
439 putc('\n', f);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
440 else
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
441 fprintf(f, "\\\n");
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
442
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
443 p=p2+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
444
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
445 w=maxw;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
446 while(w--)
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
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
450 fprintf(f, "%s\n", p);
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
451 }
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
452
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
453
79
81e9a32b1c0c trunk: changeset 1689
tuomov
parents: 70
diff changeset
454 void optparser_printhelp(int mode, const OptParserOpt *opts)
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
455 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
456 uint w, maxw=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
457 const OptParserOpt *o;
79
81e9a32b1c0c trunk: changeset 1689
tuomov
parents: 70
diff changeset
458 bool midlong=mode&OPTP_MIDLONG;
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
459
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
460 o=opts;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
461 for(; O_ID(o); o++){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
462 w=opt_w(o, midlong);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
463 if(w>maxw)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
464 maxw=w;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
465 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
466
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
467 o=opts;
79
81e9a32b1c0c trunk: changeset 1689
tuomov
parents: 70
diff changeset
468
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
469 for(; O_ID(o); o++)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 53
diff changeset
470 print_opt(o, midlong, maxw, TERM_W);
12
5fd153b29d40 trunk: changeset 15
tuomov
parents: 9
diff changeset
471 }

mercurial