source: buchla-68k/orig/DOC/SCANARGS.TXT@ 4aad7a7

Last change on this file since 4aad7a7 was 3ae31e9, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Imported original source code.

  • Property mode set to 100755
File size: 13.1 KB
RevLine 
[3ae31e9]1
2
3
4SCANARGS(3) UNIX Programmer's Manual SCANARGS(3)
5
6
7
8NAME
9 scanargs, qscanargs - formatted conversion from command
10 argument list
11
12SYNOPSIS
13 #include <stdio.h>
14
15 scanargs(argc, argv, format [, pointer]... )
16 int argc;
17 char *argv[];
18 char *format;
19
20DESCRIPTION
21 Scanargs reads argc arguments from an argument list pointed
22 to by argv. It converts the argument list according to the
23 format string, and stores the results of the conversions in
24 its parameters. Qscanargs is a smaller and less powerful
25 version which does not understand floating point.
26
27 Scanargs expects as its parameters an argument count argc, a
28 pointer to an argument list argv (see exec(2)), a control
29 string format, described below, and a set of pointer argu-
30 ments indicating where the converted output should be
31 stored.
32
33 The control string contains specifications, which are used
34 to direct interpretation of argument sequences. It contains
35 the necessary information to describe an acceptable syntax
36 for the argument list, and the expected meaning of each
37 argument.
38
39 If the scanning fails it will print a cryptic message tel-
40 ling why it failed, and generate a usage message from the
41 control string.
42
43 The control string is composed of two parts:
44
45 Name: The first characters in the string are assumed to be
46 the calling name of the program being executed. This is used
47 for generation of usage messages, but is otherwise ignored.
48 If this field is a % sign, it is replaced with the contents
49 of argv[0] in the message.
50
51 Conversions: Following the name, an optional list of
52 conversion specifications is given, with separating spaces.
53 The structure of a conversion specification:
54
55 label_key_conversion
56
57 consists of a label which is a string of non-space charac-
58 ters describing the acceptable argument, a key which may be
59 either of
60
61
62
63Printed 10/6/84 AMPEX CORP. 1
64
65
66
67
68
69
70SCANARGS(3) UNIX Programmer's Manual SCANARGS(3)
71
72
73
74 % The argument is optional. Its absence is ignored.
75
76 ! A required argument. If absent, an error return ensues.
77
78 and a conversion character which indicates the interpreta-
79 tion of the argument; the corresponding pointer parameter
80 must be of a restricted type.
81
82 The following conversion characters are supported:
83
84 d D a decimal integer is expected; the corresponding parame-
85 ter should be an int or a long (if D is specified)
86 pointer.
87
88 o O an octal integer is expected; the corresponding parame-
89 ter should be an int or a long pointer.
90
91 x X a hexadecimal integer is expected; the corresponding
92 parameter should be an int or a long pointer.
93
94 n N an integer numeric conversion using C language syntax.
95 Numbers beginning 0x are hexadecimal, numbers beginning
96 0 are octal, and other numbers are decimal. Negative
97 hex numbers must have the minus sign following the 0x,
98 i.e. negative 0xa would be given as 0x-a. The
99 corresponding pointer should point to an int or a long.
100
101 f F a floating point number is expected; the corresponding
102 parameter should be a pointer to a float or a double.
103 Not available in qscanargs.
104
105 x X d D o O f F
106 all numeric types supported by scanf(3S) are legal in
107 scanargs; qscanargs supports all but f and F formats,
108 and avoids including the large size of the scanf rou-
109 tine.
110
111 s a character string is expected; the corresponding param-
112 eter should be the address of a pointer to char.
113
114 - a single character flag is expected; the corresponding
115 parameter should be an int pointer. The occurrence of a
116 - followed by the character specified in the label will
117 cause the setting of the least significant bit of the
118 integer pointed to by the corresponding parameter. The
119 label may consist of up to sixteen (32 on a VAX) option
120 characters, in which case one of the bits of the integer
121 is independantly set to reflect which one of the flags
122 was present. (The right most character corresponds to
123 the LSB of the integer) Only one option may be chosen
124 from each conversion specification. The bits which are
125 not set will remain in their previous state. For
126
127
128
129Printed 10/6/84 AMPEX CORP. 2
130
131
132
133
134
135
136SCANARGS(3) UNIX Programmer's Manual SCANARGS(3)
137
138
139
140 example, a specification of abc%- would match one of -a
141 -b or -c in the argument list. -c would cause the
142 corresponding variable to be set to 1, -b to 2, and -a
143 to 4. (Actually, these bits would be ored in, but
144 assuming an intial value of 0, this is true).
145
146 The - may be followed immediately by more
147 label_key_conversion specifications. These should not
148 be separated by blanks and should not contain any -
149 specifications. They will be processed only if the flag
150 argument is scanned. This allows optional specification
151 of parameters corresponding to a flag (e.g. -f file ).
152 Corresponding arguments on the command line must appear
153 between the flag which introduces them and the next flag
154 in the command line.
155
156 $ This may appear only as the last specifier in the format
157 string, and is used to "eat up" the rest of the command
158 arguments. The corresponding function argument is an
159 int pointer. An index into argv to the dividing point
160 between the arguments which have been used, and those
161 which have not is returned. This index points to the
162 first unused command argument. If there is no such
163 dividing point, an error will be generated.
164
165 A string or numeric conversion character may be preceded by
166 a * or a , to indicate that a list of such arguments is
167 expected. If , is used, then the AT&T proposed argument
168 standard is followed, and a single string is expected, with
169 the individual list elements separated by commas or spaces.
170 Two commas in a row will produce a null entry (0 if numeric,
171 zero-length string if string conversion), but multiple
172 spaces, and spaces following a comma, are taken as a single
173 separator. If * is specified, then multiple arguments are
174 parsed to produce the list. A format specifier with a * or
175 a , takes two arguments. The first is an int pointer, the
176 number of items in the list is returned here. The second is
177 a pointer to pointer to the correct data type for the format
178 specifier. A pointer to the list of arguments is returned
179 here.
180
181 The scanner will process the control string from left to
182 right, and where there are multiple conversions of the same
183 type, they will be assigned one to one with their order of
184 occurrence in the argument list. Where the order of the
185 arguments is not ambiguous in the control string, they may
186 occur in any order in the argument list. (ie. A decimal
187 number will not be confused with a flag, but may be confused
188 with an octal number or another decimal number. So if an
189 octal and a decimal number are to be arguments, their order
190 will determine their conversion, while a decimal number and
191 a flag as arguments may occur in any order and still be
192
193
194
195Printed 10/6/84 AMPEX CORP. 3
196
197
198
199
200
201
202SCANARGS(3) UNIX Programmer's Manual SCANARGS(3)
203
204
205
206 converted correctly.)
207
208 An argument list that does not match the requirements of the
209 control string will cause the printing of a short message
210 telling why, and a message telling what the correct usage
211 is. This usage is gleaned from the control string, and the
212 labels are used directly. The labels should be both terse
213 and descriptive! Spaces, tabs, and newlines in the format
214 string will be reproduced in the usage message, and can be
215 used for effective prettyprinting. A single tab (following
216 a newline) will indent the line directly under the command
217 name in the usage message.
218
219 The scanargs function returns 1 when the argument list
220 matched the requirements of the control string, and returns
221 0 if there was a failure. Parameters for any conversions
222 not matched are left untouched.
223 For example, the call
224
225 int i; double x; char *name;
226 scanargs(argc, argv, "program decimal%d floating%F
227 file%s"
228 , &i, &x, &name );
229
230 in a C program executed by the shell command
231
232 % program 10 3.5397 inputfile
233
234 will assign to i the value 10, x the value 3.5397, and name
235 will point to the string "inputfile".
236
237 If the program was executed by the shell command
238
239 % program 3.4 .7 inputfile
240
241 the following would be printed on the standard error:
242
243 extra arguments not processed
244 usage : program [decimal] [floating] [file]
245
246 because 3.4 matches the type of 'floating' and inputfile
247 matches the type of 'file', leaving .7 unmatched (it is con-
248 sidered a string by scanargs, to be considered a number, it
249 must begin with a digit).
250
251 Finally, executing the command
252 % program 10
253 would assign 10 to i, leaving x and name unaffected.
254
255 This call could be used for the diff(1) command
256
257
258
259
260
261Printed 10/6/84 AMPEX CORP. 4
262
263
264
265
266
267
268SCANARGS(3) UNIX Programmer's Manual SCANARGS(3)
269
270
271
272 int blanks; int flags; char *file1; char *file2;
273 scanargs(argc, argv, "diff b%- efh%- file1!s file2!s"
274 , &blanks, &flags, &file1, &file2 );
275
276 and would only allow one of either -e -f or -h to be chosen
277 optionally, with -b as an independent option. File1 and
278 file2 are both required. The usage message for this version
279 of diff would be
280
281 usage : diff [-b] -{efh} file1 file2
282
283 This call could be used for a simplified version of the
284 sed(1) command
285
286 int efile; int noprint; char *script; char *file1; char
287 *file2;
288 scanargs(argc, argv, "sed n%- script%s f%-editfile!s
289 file%s"
290 , &noprint, &script, &efile, &file1, &file2 );
291
292 If the -f option is specified, then a file name must be
293 given as the next string argument. The usage message for
294 this version of sed would be
295
296 usage : sed [-n] [script] [-f editfile] file
297
298
299 Further notes on putting together a format string:
300
301 It is still possible for conditional arguments to be con-
302 fused with arguments which stand alone. For this reason, it
303 is recommended that all flags (and associated conditional
304 arguments) be specified first in the scanargs format string.
305 This ordering is not necessary for the command line argu-
306 ments, however. The only case which could still cause con-
307 fusion if these rules are followed is illustrated below:
308 format string: "prog d%-num%d othernum%d"
309 command line: prog -d 9
310 It is unclear whether the number 9 should be associated with
311 the num parameter or the othernum parameter. Scanargs
312 assigns it to the num parameter. To force it to be associ-
313 ated with othernum the command could be invoked as either
314 prog 9 -d
315 or prog -d -- 9
316 The -- in the second example is interpreted as a flag,
317 thereby terminating the scan for arguments introduced by the
318 -d. According to the proposed standard, an argument of --
319 is to be interpreted as terminating the optional arguments
320 on a flag.
321
322 Note that if the format string in the above example were
323 "prog othernum%d d%-num%d"
324
325
326
327Printed 10/6/84 AMPEX CORP. 5
328
329
330
331
332
333
334SCANARGS(3) UNIX Programmer's Manual SCANARGS(3)
335
336
337
338 it would be impossible to assign a value to num without also
339 assigning a value to othernum.
340
341
342SEE ALSO
343 exec(2), scanf(3S)
344
345DIAGNOSTICS
346 Returns 0 on error, 1 on success.
347
348AUTHOR
349 Gary Newman - Ampex Corporation
350 Spencer W. Thomas - University of Utah
351
352BUGS
353 By its nature a call to scanargs defines a syntax which may
354 be ambiguous, and although the results may be surprising,
355 they are quite predictable. The heuristic used to tell
356 string arguments from numeric arguments is just that. In
357 fact, that you can't give a number as a string argument is
358 sort of bogus.
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393Printed 10/6/84 AMPEX CORP. 6
394
395
396
397
Note: See TracBrowser for help on using the repository browser.