xref: /titanic_51/usr/src/common/util/getoptstr.c (revision 1bdd6c0e3710e91cb1f31aa78de33cb638494480)
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1992-1996,2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 /*
34  * This file contains getoptstr(), which is getopt() for strings of arguments
35  * (not arrays of strings).  It is used by the bootloader ({*fs,inet}boot) and
36  * the kernel to process argument strings.
37  */
38 
39 
40 #include "getoptstr.h"
41 
42 
43 /*
44  * This routine needs to be supplied by whatever uses this file.
45  */
46 char *strchr(const char *s, int c);
47 
48 
49 #define	NULL	((void *)0)
50 
51 #define	ISNTWORDCH(c)	((c) == '\0' || ISSPACE(c))
52 
53 
54 /*
55  * Prepare a gos_params structure for use by getoptstr().
56  */
57 void
58 getoptstr_init(struct gos_params *params)
59 {
60 	params->gos_pos = 1;
61 }
62 
63 /*
64  * Modeled after lib/libc/port/gen/getopt.c.
65  *
66  * With params->gos_opts set to a string of options and params->gos_strp set to
67  * an argument string, getoptstr returns
68  *   * the next option letter, where the options are given by the
69  *     params->gos_opts string.  If the option is followed by a ':' in gos_opts,
70  *     then params->gos_optargp will point to the beginning of the argument in
71  *     the string, and params->gos_optarglen will contain its length.
72  *   * -1 if "--" or a non-option argument is encountered.  In the former
73  *     case, params->gos_strp is advanced to the next argument.
74  *   * '?' if an illegal option is encountered or if an argument is not
75  *     supplied for an option which requires one and ':' is not the first
76  *     character of opts.  In both cases, the option letter is available in
77  *     params->gos_last_opt, and in the former case, params->gos_errp will
78  *     point to the offending character.
79  *   * ':' if an argument is not supplied for an option which requires one and
80  *     ':' is the first character of params->gos_opts.
81  */
82 int
83 getoptstr(struct gos_params *params)
84 {
85 	char c;
86 	char *cp;
87 
88 	/*
89 	 * const because we should update params.  Just make sure you don't
90 	 * use this after you update params->gos_strp.
91 	 */
92 	const char * const strp = params->gos_strp;
93 
94 
95 	if (params->gos_opts == NULL || strp == NULL)
96 		return (-1);
97 
98 	if (params->gos_pos == 1) {
99 		/* At beginning of new word. */
100 
101 		if (strp[0] == '\0' || strp[0] != '-')
102 			return (-1);
103 		if (ISNTWORDCH(strp[1])) {
104 			/* Lone dash. */
105 			return (-1);
106 		}
107 
108 		/* Check for "--" */
109 		if (strp[1] == '-' && ISNTWORDCH(strp[2])) {
110 			params->gos_strp = &strp[2];
111 			SKIP_SPC(params->gos_strp);
112 			return (-1);
113 		}
114 	}
115 
116 	params->gos_last_opt = c = strp[params->gos_pos];
117 	if (c == ':' || (cp = strchr(params->gos_opts, c)) == NULL) {
118 		/* Unrecognized option error. */
119 		params->gos_errp = &strp[params->gos_pos];
120 		++params->gos_pos;
121 		if (ISNTWORDCH(strp[params->gos_pos])) {
122 			params->gos_strp = &strp[params->gos_pos];
123 			SKIP_SPC(params->gos_strp);
124 			params->gos_pos = 1;
125 		}
126 		return ('?');
127 	}
128 
129 	if (cp[1] == ':') {
130 		/* This option expects an argument. */
131 
132 		params->gos_strp = &strp[params->gos_pos + 1];
133 
134 		if (ISNTWORDCH(*params->gos_strp)) {
135 			/* The argument is in the next word. */
136 			SKIP_SPC(params->gos_strp);
137 
138 			if (*params->gos_strp == '\0') {
139 				/* Not.  Missing argument. */
140 				params->gos_pos = 1;
141 				params->gos_optargp = NULL;
142 				return (params->gos_opts[0] == ':' ? ':' : '?');
143 			}
144 		}
145 
146 		params->gos_optargp = params->gos_strp;
147 
148 		/* Advance to the next word. */
149 		SKIP_WORD(params->gos_strp);
150 		params->gos_optarglen = params->gos_strp - params->gos_optargp;
151 		SKIP_SPC(params->gos_strp);
152 
153 		params->gos_pos = 1;
154 	} else {
155 		++params->gos_pos;
156 		if (ISNTWORDCH(strp[params->gos_pos])) {
157 			params->gos_strp = &strp[params->gos_pos];
158 			SKIP_SPC(params->gos_strp);
159 			params->gos_pos = 1;
160 		}
161 		params->gos_optargp = NULL;
162 	}
163 	return (c);
164 }
165