4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 1992-1996,2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
32 * This file contains getoptstr(), which is getopt() for strings of arguments
33 * (not arrays of strings). It is used by the bootloader ({*fs,inet}boot) and
34 * the kernel to process argument strings.
38 #include "getoptstr.h"
43 * This routine needs to be supplied by whatever uses this file.
45 char *strchr(const char *s
, int c
);
48 #define ISNTWORDCH(c) ((c) == '\0' || ISSPACE(c))
52 * Prepare a gos_params structure for use by getoptstr().
55 getoptstr_init(struct gos_params
*params
)
61 * Modeled after lib/libc/port/gen/getopt.c.
63 * With params->gos_opts set to a string of options and params->gos_strp set to
64 * an argument string, getoptstr returns
65 * * the next option letter, where the options are given by the
66 * params->gos_opts string. If the option is followed by a ':' in gos_opts,
67 * then params->gos_optargp will point to the beginning of the argument in
68 * the string, and params->gos_optarglen will contain its length.
69 * * -1 if "--" or a non-option argument is encountered. In the former
70 * case, params->gos_strp is advanced to the next argument.
71 * * '?' if an illegal option is encountered or if an argument is not
72 * supplied for an option which requires one and ':' is not the first
73 * character of opts. In both cases, the option letter is available in
74 * params->gos_last_opt, and in the former case, params->gos_errp will
75 * point to the offending character.
76 * * ':' if an argument is not supplied for an option which requires one and
77 * ':' is the first character of params->gos_opts.
80 getoptstr(struct gos_params
*params
)
86 * const because we should update params. Just make sure you don't
87 * use this after you update params->gos_strp.
89 const char * const strp
= params
->gos_strp
;
92 if (params
->gos_opts
== NULL
|| strp
== NULL
)
95 if (params
->gos_pos
== 1) {
96 /* At beginning of new word. */
98 if (strp
[0] == '\0' || strp
[0] != '-')
100 if (ISNTWORDCH(strp
[1])) {
106 if (strp
[1] == '-' && ISNTWORDCH(strp
[2])) {
107 params
->gos_strp
= &strp
[2];
108 SKIP_SPC(params
->gos_strp
);
113 params
->gos_last_opt
= c
= strp
[params
->gos_pos
];
114 if (c
== ':' || (cp
= strchr(params
->gos_opts
, c
)) == NULL
) {
115 /* Unrecognized option error. */
116 params
->gos_errp
= &strp
[params
->gos_pos
];
118 if (ISNTWORDCH(strp
[params
->gos_pos
])) {
119 params
->gos_strp
= &strp
[params
->gos_pos
];
120 SKIP_SPC(params
->gos_strp
);
127 /* This option expects an argument. */
129 params
->gos_strp
= &strp
[params
->gos_pos
+ 1];
131 if (ISNTWORDCH(*params
->gos_strp
)) {
132 /* The argument is in the next word. */
133 SKIP_SPC(params
->gos_strp
);
135 if (*params
->gos_strp
== '\0') {
136 /* Not. Missing argument. */
138 params
->gos_optargp
= NULL
;
139 return (params
->gos_opts
[0] == ':' ? ':' : '?');
143 params
->gos_optargp
= params
->gos_strp
;
145 /* Advance to the next word. */
146 SKIP_WORD(params
->gos_strp
);
147 params
->gos_optarglen
= params
->gos_strp
- params
->gos_optargp
;
148 SKIP_SPC(params
->gos_strp
);
153 if (ISNTWORDCH(strp
[params
->gos_pos
])) {
154 params
->gos_strp
= &strp
[params
->gos_pos
];
155 SKIP_SPC(params
->gos_strp
);
158 params
->gos_optargp
= NULL
;