1 /*	$NetBSD: getopt.c,v 1.26 2003/08/07 16:43:40 agc Exp $	*/
2 /* SPDX-License-Identifier: BSD-3-Clause */
3 /*
4  * Copyright (c) 1987, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <string.h>
33 #ifdef CONFIG_ARCH_POSIX
34 #include <unistd.h>
35 #else
36 #include <zephyr/posix/unistd.h>
37 #endif
38 #include "getopt.h"
39 #include "getopt_common.h"
40 
41 #include <zephyr/logging/log.h>
42 LOG_MODULE_REGISTER(getopt);
43 
44 #define	BADCH	((int)'?')
45 #define	BADARG	((int)':')
46 #define	EMSG	""
47 
getopt_init(void)48 void getopt_init(void)
49 {
50 	struct getopt_state *state;
51 
52 	state = getopt_state_get();
53 
54 	state->opterr = 1;
55 	state->optind = 1;
56 	state->optopt = 0;
57 	state->optreset = 0;
58 	state->optarg = NULL;
59 
60 	state->place = ""; /* EMSG */
61 
62 #if CONFIG_GETOPT_LONG
63 	state->nonopt_start = -1; /* first non option argument (for permute) */
64 	state->nonopt_end = -1; /* first option after non options (for permute) */
65 #endif
66 
67 	opterr = 1;
68 	optind = 1;
69 	optopt = 0;
70 	optreset = 0;
71 	optarg = NULL;
72 }
73 
74 /*
75  * getopt --
76  *	Parse argc/argv argument vector.
77  */
getopt(int nargc,char * const nargv[],const char * ostr)78 int getopt(int nargc, char *const nargv[], const char *ostr)
79 {
80 	struct getopt_state *state;
81 	char *oli; /* option letter list index */
82 
83 	/* get getopt state of the current thread */
84 	state = getopt_state_get();
85 
86 	if (state->optreset || *state->place == 0) { /* update scanning pointer */
87 		state->optreset = 0;
88 		state->place = nargv[state->optind];
89 		if (state->optind >= nargc || *state->place++ != '-') {
90 			/* Argument is absent or is not an option */
91 			state->place = EMSG;
92 			z_getopt_global_state_update(state);
93 			return -1;
94 		}
95 		state->optopt = *state->place++;
96 		if (state->optopt == '-' && *state->place == 0) {
97 			/* "--" => end of options */
98 			++state->optind;
99 			state->place = EMSG;
100 			z_getopt_global_state_update(state);
101 			return -1;
102 		}
103 		if (state->optopt == 0) {
104 			/* Solitary '-', treat as a '-' option
105 			 * if the program (eg su) is looking for it.
106 			 */
107 			state->place = EMSG;
108 			if (strchr(ostr, '-') == NULL) {
109 				z_getopt_global_state_update(state);
110 				return -1;
111 			}
112 			state->optopt = '-';
113 		}
114 	} else {
115 		state->optopt = *state->place++;
116 	}
117 
118 	/* See if option letter is one the caller wanted... */
119 	oli = strchr(ostr, state->optopt);
120 	if (state->optopt == ':' || oli == NULL) {
121 		if (*state->place == 0) {
122 			++state->optind;
123 		}
124 		if (state->opterr && *ostr != ':') {
125 			LOG_ERR("illegal option -- %c", state->optopt);
126 		}
127 		z_getopt_global_state_update(state);
128 		return BADCH;
129 	}
130 
131 	/* Does this option need an argument? */
132 	if (oli[1] != ':') {
133 		/* don't need argument */
134 		state->optarg = NULL;
135 		if (*state->place == 0) {
136 			++state->optind;
137 		}
138 	} else {
139 		/* Option-argument is either the rest of this argument or the
140 		 * entire next argument.
141 		 */
142 		if (*state->place) {
143 			state->optarg = state->place;
144 		} else if (nargc > ++state->optind) {
145 			state->optarg = nargv[state->optind];
146 		} else {
147 			/* option-argument absent */
148 			state->place = EMSG;
149 			if (*ostr == ':') {
150 				z_getopt_global_state_update(state);
151 				return BADARG;
152 			}
153 			if (state->opterr) {
154 				LOG_ERR("option requires an argument -- %c",
155 					state->optopt);
156 			}
157 			z_getopt_global_state_update(state);
158 			return BADCH;
159 		}
160 		state->place = EMSG;
161 		++state->optind;
162 	}
163 	z_getopt_global_state_update(state);
164 	return state->optopt;	/* return option letter */
165 }
166