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 extern char *optarg;
33 extern int opterr;
34 extern int optind;
35 extern int optopt;
36 
37 #define no_argument        0
38 #define required_argument  1
39 #define optional_argument  2
40 
41 struct option {
42 	/* name of long option */
43 	const char *name;
44 	/*
45 	 * one of no_argument, required_argument, and optional_argument:
46 	 * whether option takes an argument
47 	 */
48 	int has_arg;
49 	/* if not NULL, set *flag to val when option found */
50 	int *flag;
51 	/* if flag not NULL, value to set *flag to; else return value */
52 	int val;
53 };
54 
55 /* Function initializes getopt_state structure for current thread */
56 void getopt_init(void);
57 
58 /* Function returns getopt_state structure for the current thread. */
59 struct getopt_state *getopt_state_get(void);
60 
61 /**
62  * @brief Parses the command-line arguments.
63  *
64  * The getopt_long() function works like @ref getopt() except
65  * it also accepts long options, started with two dashes.
66  *
67  * @note This function is based on FreeBSD implementation but it does not
68  * support environment variable: POSIXLY_CORRECT.
69  *
70  * @param[in] argc	   Arguments count.
71  * @param[in] argv	   Arguments.
72  * @param[in] options	   String containing the legitimate option characters.
73  * @param[in] long_options Pointer to the first element of an array of
74  *			   @a struct z_option.
75  * @param[in] long_idx	   If long_idx is not NULL, it points to a variable
76  *			   which is set to the index of the long option relative
77  *			   to @p long_options.
78  *
79  * @return		If an option was successfully found, function returns
80  *			the option character.
81  */
82 int getopt_long(int nargc, char *const *nargv,
83 		const char *options, const struct option *long_options,
84 		int *idx);
85 
86 /**
87  * @brief Parses the command-line arguments.
88  *
89  * The getopt_long_only() function works like @ref getopt_long(),
90  * but '-' as well as "--" can indicate a long option. If an option that starts
91  * with '-' (not "--") doesn't match a long option, but does match a short
92  * option, it is parsed as a short option instead.
93  *
94  * @note This function is based on FreeBSD implementation but it does not
95  * support environment variable: POSIXLY_CORRECT.
96  *
97  * @param[in] argc	   Arguments count.
98  * @param[in] argv	   Arguments.
99  * @param[in] options	   String containing the legitimate option characters.
100  * @param[in] long_options Pointer to the first element of an array of
101  *			   @a struct option.
102  * @param[in] long_idx	   If long_idx is not NULL, it points to a variable
103  *			   which is set to the index of the long option relative
104  *			   to @p long_options.
105  *
106  * @return		If an option was successfully found, function returns
107  *			the option character.
108  */
109 int getopt_long_only(int nargc, char *const *nargv,
110 		     const char *options, const struct option *long_options,
111 		     int *idx);
112 
113 #ifdef __cplusplus
114 }
115 #endif
116 
117 #endif /* _GETOPT_H__ */
118