1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _GETOPT_H__
8 #define _GETOPT_H__
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #include <zephyr/kernel.h>
15 
16 struct getopt_state {
17 	int opterr;	/* if error message should be printed */
18 	int optind;	/* index into parent argv vector */
19 	int optopt;	/* character checked for validity */
20 	int optreset;	/* reset getopt */
21 	char *optarg;	/* argument associated with option */
22 
23 	char *place;	/* option letter processing */
24 
25 #if CONFIG_GETOPT_LONG
26 	int nonopt_start;
27 	int nonopt_end;
28 #endif
29 };
30 
31 extern int optreset;	/* reset getopt */
32 
33 #define no_argument        0
34 #define required_argument  1
35 #define optional_argument  2
36 
37 struct option {
38 	/* name of long option */
39 	const char *name;
40 	/*
41 	 * one of no_argument, required_argument, and optional_argument:
42 	 * whether option takes an argument
43 	 */
44 	int has_arg;
45 	/* if not NULL, set *flag to val when option found */
46 	int *flag;
47 	/* if flag not NULL, value to set *flag to; else return value */
48 	int val;
49 };
50 
51 /* Function intializes getopt_state structure for current thread */
52 void getopt_init(void);
53 
54 /* Function returns getopt_state structure for the current thread. */
55 struct getopt_state *getopt_state_get(void);
56 
57 /**
58  * @brief Parses the command-line arguments.
59  *
60  * The getopt_long() function works like @ref getopt() except
61  * it also accepts long options, started with two dashes.
62  *
63  * @note This function is based on FreeBSD implementation but it does not
64  * support environment variable: POSIXLY_CORRECT.
65  *
66  * @param[in] argc	   Arguments count.
67  * @param[in] argv	   Arguments.
68  * @param[in] options	   String containing the legitimate option characters.
69  * @param[in] long_options Pointer to the first element of an array of
70  *			   @a struct z_option.
71  * @param[in] long_idx	   If long_idx is not NULL, it points to a variable
72  *			   which is set to the index of the long option relative
73  *			   to @p long_options.
74  *
75  * @return		If an option was successfully found, function returns
76  *			the option character.
77  */
78 int getopt_long(int nargc, char *const *nargv,
79 		const char *options, const struct option *long_options,
80 		int *idx);
81 
82 /**
83  * @brief Parses the command-line arguments.
84  *
85  * The getopt_long_only() function works like @ref getopt_long(),
86  * but '-' as well as "--" can indicate a long option. If an option that starts
87  * with '-' (not "--") doesn't match a long option, but does match a short
88  * option, it is parsed as a short option instead.
89  *
90  * @note This function is based on FreeBSD implementation but it does not
91  * support environment variable: POSIXLY_CORRECT.
92  *
93  * @param[in] argc	   Arguments count.
94  * @param[in] argv	   Arguments.
95  * @param[in] options	   String containing the legitimate option characters.
96  * @param[in] long_options Pointer to the first element of an array of
97  *			   @a struct option.
98  * @param[in] long_idx	   If long_idx is not NULL, it points to a variable
99  *			   which is set to the index of the long option relative
100  *			   to @p long_options.
101  *
102  * @return		If an option was successfully found, function returns
103  *			the option character.
104  */
105 int getopt_long_only(int nargc, char *const *nargv,
106 		     const char *options, const struct option *long_options,
107 		     int *idx);
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 #endif /* _GETOPT_H__ */
114