1GETSUBOPT(3) Library Functions Manual GETSUBOPT(3)
2
3
4
6 getsubopt - get sub options from an argument
7
9 #include <stdlib.h>
10
11 extern char *suboptarg
12
13 int
14 getsubopt(optionp, tokens, valuep)
15 char **optionp;
16 char **tokens;
17 char **valuep;
18
20 The getsubopt() function parses a string containing tokens delimited by
21 one or more tab, space or comma (`,') characters. It is intended for
22 use in parsing groups of option arguments provided as part of a utility
23 command line.
24
25 The argument optionp is a pointer to a pointer to the string. The
26 argument tokens is a pointer to a NULL-terminated array of pointers to
27 strings.
28
29 The getsubopt() function returns the zero-based offset of the pointer
30 in the tokens array referencing a string which matches the first token
31 in the string, or, -1 if the string contains no tokens or tokens does
32 not contain a matching string.
33
34 If the token is of the form ``name=value'', the location referenced by
35 valuep will be set to point to the start of the ``value'' portion of
36 the token.
37
38 On return from getsubopt(), optionp will be set to point to the start
39 of the next token in the string, or the null at the end of the string
40 if no more tokens are present. The external variable suboptarg will be
41 set to point to the start of the current token, or NULL if no tokens
42 were present. The argument valuep will be set to point to the
43 ``value'' portion of the token, or NULL if no ``value'' portion was
44 present.
45
47 char *tokens[] = {
48 #define ONE 0
49 "one",
50 #define TWO 1
51 "two",
52 NULL
53 };
54
55 ...
56
57 extern char *optarg, *suboptarg;
58 char *options, *value;
59
60 while ((ch = getopt(argc, argv, "ab:")) != -1) {
61 switch(ch) {
62 case 'a':
63 /* process ``a'' option */
64 break;
65 case 'b':
66 options = optarg;
67 while (*options) {
68 switch(getsubopt(&options, tokens, &value)) {
69 case ONE:
70 /* process ``one'' sub option */
71 break;
72 case TWO:
73 /* process ``two'' sub option */
74 if (!value)
75 error("no value for two");
76 i = atoi(value);
77 break;
78 case -1:
79 if (suboptarg)
80 error("illegal sub option %s",
81 suboptarg);
82 else
83 error("missing sub option");
84 break;
85 }
86 break;
87 }
88
90 getopt(3), strsep(3)
91
93 The getsubopt() function first appeared in 4.4BSD.
94
95
96
974.4 Berkeley Distribution January 12, 1996 GETSUBOPT(3)