chiark / gitweb /
Merge branches 'idx/verh' and 'idx/qmqpc'
[qmail] / subgetopt.3
CommitLineData
2117e02e
MW
1.TH subgetopt 3
2.SH NAME
3subgetopt \- get option character from command line
4.SH SYNTAX
5.B #include <subgetopt.h>
6
7char *\fBsgoptarg\fP;
8.br
9int \fBsgoptind\fP;
10.br
11int \fBsgoptpos\fP;
12.br
13int \fBsgoptdone\fP;
14.br
15int \fBsgoptproblem\fP;
16
17int \fBsgopt(\fP\fIargc,argv,opts\fR\fB)\fP;
18
19int \fIargc\fR;
20.br
21char **\fIargv\fR;
22.br
23char *\fIopts\fR;
24.SH DESCRIPTION
25.B sgopt
26returns the next valid command-line option character
27from
28.IR argv .
29
30Valid option characters are listed in the
31.I opts
32string.
33.I opts
34may be empty.
35A character in
36.I opts
37may be followed by a colon,
38in which case it
39takes an
40.I option argument\fR.
41Avoid using the characters ?, :, and \- as option characters.
42
43Below
44.I option argument
45is abbreviated
46as
47.I optarg
48and
49.I command-line argument
50is abbreviated as
51.IR cmdarg .
52
53Options are listed in cmdargs which begin with
54a minus sign.
55Several options which do not take optargs may be combined
56into one cmdarg.
57
58An option which takes an optarg may be handled in two ways.
59If it appears at the very end of a cmdarg,
60then the entire next cmdarg is the optarg.
61But if there are any characters in the cmdarg
62after the option character,
63then those characters form the optarg.
64The optarg is returned in
65.BR sgoptarg .
66Next time
67.B sgopt
68looks at the cmdarg which follows the optarg.
69
70If a cmdarg does not begin with a hyphen,
71or if it is a lone hyphen not followed by any characters,
72or if it begins with two hyphens,
73then it terminates option processing,
74and
75.B sgopt
76returns an appropriate code.
77If there are two hyphens,
78.B sgopt
79will advance attention to the next cmdarg,
80so it can be called again to read further options.
81.SH "PROPER USAGE"
82.B sgoptproblem
83should be used only when
84.B sgopt
85returns ?.
86.B sgoptind
87and
88.B sgoptpos
89are defined all the time.
90.B sgoptarg
91is defined all the time;
92it is null unless
93.B sgopt
94has just returned an option with optarg.
95
96.B sgopt
97is typically used as follows.
98
99.EX
100#include <subgetopt.h>
101
102main(argc,argv) int argc; char **argv; { int opt;
103
104while ((opt = sgopt(argc,argv,"a:s")) != sgoptdone)
105.br
106 switch(opt) {
107.br
108 case 'a':
109.br
110 printf("opt a with optarg %s\\n",sgoptarg); break;
111.br
112 case 's':
113.br
114 printf("opt s with no optarg\\n"); break;
115.br
116 case '?':
117.br
118 if (argv[sgoptind] && (sgoptind < argc))
119.br
120 printf("illegal opt %c\\n",sgoptproblem);
121.br
122 else
123.br
124 printf("missing arg, opt %c\\n",sgoptproblem);
125.br
126 exit(1);
127.br
128 }
129
130argv += sgoptind;
131.br
132while (*argv) printf("argument %s\\n",*argv++);
133.br
134exit(0);
135.br
136}
137.EE
138
139The end of the command line is
140marked by either
141.IR argc ,
142or a null pointer in
143.IR argv ,
144whichever comes first.
145Normally
146these two markers coincide,
147so it is redundant
148to test for
149both
150.I argv\fB[sgoptind]
151and
152.B sgoptind < \fIargc\fR.
153The above code shows both tests as an illustration.
154
155.B Multiple option sets:
156One useful technique is to call
157.B sgopt
158with a primary
159.I opts
160until it returns EOF,
161then call
162.B sgopt
163with a secondary
164.I opts
165until it returns EOF.
166The user can provide primary options, then a double hyphen,
167and then secondary options.
168No special handling is needed if some or all of the options are
169omitted.
170The same technique can be used for any number of option sets
171in series.
172
173.B Multiple command lines:
174Before parsing a new
175.BR argv ,
176make sure to
177set
178.B sgoptind
179and
180.B sgoptpos
181back to
1821 and 0.
183.SH "PARSING STAGES"
184.B sgopt
185keeps track of its position in
186.I argv
187with
188.B sgoptind
189and
190.BR sgoptpos ,
191which are initialized to 1 and 0.
192It looks at
193.I argv\fB[sgoptind][sgoptpos]
194and following characters.
195
196.B sgopt
197indicates
198that no more options are available by
199returning
200.BR sgoptdone ,
201which is initialized to
202.BR SUBGETOPTDONE ,
203which is defined as \-1.
204
205.B sgopt
206begins by setting
207.B optarg
208to null.
209
210.B Ending conditions:
211If
212.I argv
213is null, or
214.B sgoptind
215is larger than
216.IR argc ,
217or the current cmdarg
218.I argv\fB[sgoptind]
219is null,
220then
221.B sgopt
222returns
223.BR optdone .
224
225.B Stage one:
226If the current character
227is zero,
228.B sgopt
229moves to the beginning of the next cmdarg.
230It then checks the ending conditions again.
231
232.B Stage two:
233If
234the current position is the begining of the cmdarg,
235.B sgopt
236checks whether
237the current character
238is a minus sign.
239If not it returns
240.BR optdone .
241It then
242moves
243to the next character.
244If that character is zero,
245.B sgopt
246moves
247back to the beginning of the cmdarg,
248and returns
249.BR sgoptdone .
250If the character is a minus sign,
251.B sgopt
252moves to the beginning of the next cmdarg,
253and returns
254.BR sgoptdone .
255
256.B Stage three:
257.B sgopt
258records the current character,
259.IR c ,
260and moves to the next character.
261There are three possibilities:
262(1)
263.I c
264is an option character without optarg in
265.IR opts ,
266or
267(2)
268.I c
269is an option character with optarg in
270.IR opts ,
271or
272(3)
273.I c
274does not appear in
275.IR opts .
276
277(1)
278If
279.I c
280appears as an option character without optarg in
281.IR opts ,
282.B sgopt
283returns
284.IR c .
285
286(2)
287If
288.I c
289appears as an option character with optarg in
290.IR opts ,
291.B sgopt
292sets
293.B sgoptarg
294to the current position,
295and moves to the next cmdarg.
296If
297.B sgoptarg
298is nonempty,
299.B sgopt
300returns
301.IR c .
302
303Then
304.B sgopt
305sets
306.B sgoptarg
307to
308the current cmdarg.
309If
310the current cmdarg is null,
311or past
312.IR argc ,
313.B sgopt
314sets
315.B sgoptproblem
316to
317.I c
318and returns ?.
319Otherwise
320.B sgopt
321moves to the next
322argument
323and returns
324.IR c .
325
326(2)
327If
328.I c
329does not appear in
330.IR opts ,
331.B sgopt
332sets
333.B sgoptproblem
334to
335.I c
336and returns ?.
337.SH "SYNTAX NOTE"
338.B sgopt
339is actually a macro abbreviation for
340.BR subgetopt .
341The external
342.B sg
343variables are also macros
344for
345.BR subget .
346These macros are defined in
347.BR <subgetopt.h> ,
348unless
349.B SUBGETOPTNOSHORT
350is defined
351when
352.B <subgetopt.h>
353is included.
354.SH VERSION
355subgetopt version 0.9, 931129.
356.SH AUTHOR
357Placed into the public domain by Daniel J. Bernstein.