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