summaryrefslogtreecommitdiff
path: root/usr/src/common/util/getoptstr.c
blob: 75a16c8860e7e3219edbe2bea56c3d56a01203d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 1992-1996,2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved  	*/


#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * This file contains getoptstr(), which is getopt() for strings of arguments
 * (not arrays of strings).  It is used by the bootloader ({*fs,inet}boot) and
 * the kernel to process argument strings.
 */


#include "getoptstr.h"


/*
 * This routine needs to be supplied by whatever uses this file.
 */
char *strchr(const char *s, int c);


#define	NULL	((void *)0)

#define	ISNTWORDCH(c)	((c) == '\0' || ISSPACE(c))


/*
 * Prepare a gos_params structure for use by getoptstr().
 */
void
getoptstr_init(struct gos_params *params)
{
	params->gos_pos = 1;
}

/*
 * Modeled after lib/libc/port/gen/getopt.c.
 *
 * With params->gos_opts set to a string of options and params->gos_strp set to
 * an argument string, getoptstr returns
 *   * the next option letter, where the options are given by the
 *     params->gos_opts string.  If the option is followed by a ':' in gos_opts,
 *     then params->gos_optargp will point to the beginning of the argument in
 *     the string, and params->gos_optarglen will contain its length.
 *   * -1 if "--" or a non-option argument is encountered.  In the former
 *     case, params->gos_strp is advanced to the next argument.
 *   * '?' if an illegal option is encountered or if an argument is not
 *     supplied for an option which requires one and ':' is not the first
 *     character of opts.  In both cases, the option letter is available in
 *     params->gos_last_opt, and in the former case, params->gos_errp will
 *     point to the offending character.
 *   * ':' if an argument is not supplied for an option which requires one and
 *     ':' is the first character of params->gos_opts.
 */
int
getoptstr(struct gos_params *params)
{
	char c;
	char *cp;

	/*
	 * const because we should update params.  Just make sure you don't
	 * use this after you update params->gos_strp.
	 */
	const char * const strp = params->gos_strp;


	if (params->gos_opts == NULL || strp == NULL)
		return (-1);

	if (params->gos_pos == 1) {
		/* At beginning of new word. */

		if (strp[0] == '\0' || strp[0] != '-')
			return (-1);
		if (ISNTWORDCH(strp[1])) {
			/* Lone dash. */
			return (-1);
		}

		/* Check for "--" */
		if (strp[1] == '-' && ISNTWORDCH(strp[2])) {
			params->gos_strp = &strp[2];
			SKIP_SPC(params->gos_strp);
			return (-1);
		}
	}

	params->gos_last_opt = c = strp[params->gos_pos];
	if (c == ':' || (cp = strchr(params->gos_opts, c)) == NULL) {
		/* Unrecognized option error. */
		params->gos_errp = &strp[params->gos_pos];
		++params->gos_pos;
		if (ISNTWORDCH(strp[params->gos_pos])) {
			params->gos_strp = &strp[params->gos_pos];
			SKIP_SPC(params->gos_strp);
			params->gos_pos = 1;
		}
		return ('?');
	}

	if (cp[1] == ':') {
		/* This option expects an argument. */

		params->gos_strp = &strp[params->gos_pos + 1];

		if (ISNTWORDCH(*params->gos_strp)) {
			/* The argument is in the next word. */
			SKIP_SPC(params->gos_strp);

			if (*params->gos_strp == '\0') {
				/* Not.  Missing argument. */
				params->gos_pos = 1;
				params->gos_optargp = NULL;
				return (params->gos_opts[0] == ':' ? ':' : '?');
			}
		}

		params->gos_optargp = params->gos_strp;

		/* Advance to the next word. */
		SKIP_WORD(params->gos_strp);
		params->gos_optarglen = params->gos_strp - params->gos_optargp;
		SKIP_SPC(params->gos_strp);

		params->gos_pos = 1;
	} else {
		++params->gos_pos;
		if (ISNTWORDCH(strp[params->gos_pos])) {
			params->gos_strp = &strp[params->gos_pos];
			SKIP_SPC(params->gos_strp);
			params->gos_pos = 1;
		}
		params->gos_optargp = NULL;
	}
	return (c);
}