1 /* 2 * Copyright (c) 2021 Nordic Semiconductor ASA 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef ZEPHYR_INCLUDE_SYS_GETOPT_H_ 8 #define ZEPHYR_INCLUDE_SYS_GETOPT_H_ 9 10 #ifdef __cplusplus 11 extern "C" { 12 #endif 13 14 #include <zephyr/kernel.h> 15 16 struct sys_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 sys_getopt_optreset; /* reset getopt */ 32 extern char *sys_getopt_optarg; 33 extern int sys_getopt_opterr; 34 extern int sys_getopt_optind; 35 extern int sys_getopt_optopt; 36 37 #define sys_getopt_no_argument 0 38 #define sys_getopt_required_argument 1 39 #define sys_getopt_optional_argument 2 40 41 struct sys_getopt_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 sys_getopt_init(void); 57 58 /* Function returns getopt_state structure for the current thread. */ 59 struct sys_getopt_state *sys_getopt_state_get(void); 60 61 /** 62 * @brief Parses the command-line arguments. 63 * 64 * @note This function is based on FreeBSD implementation but it does not 65 * support environment variable: POSIXLY_CORRECT. 66 * 67 * @param[in] nargc Arguments count. 68 * @param[in] nargv Arguments. 69 * @param[in] ostr String containing the legitimate option characters. 70 * 71 * @return If an option was successfully found, function returns 72 * the option character. 73 */ 74 int sys_getopt(int nargc, char *const nargv[], const char *ostr); 75 76 /** 77 * @brief Parses the command-line arguments. 78 * 79 * The sys_getopt_long() function works like @ref sys_getopt() except 80 * it also accepts long options, started with two dashes. 81 * 82 * @note This function is based on FreeBSD implementation but it does not 83 * support environment variable: POSIXLY_CORRECT. 84 * 85 * @param[in] nargc Arguments count. 86 * @param[in] nargv Arguments. 87 * @param[in] options String containing the legitimate option characters. 88 * @param[in] long_options Pointer to the first element of an array of 89 * @a struct sys_getopt_option. 90 * @param[in] idx If idx is not NULL, it points to a variable 91 * which is set to the index of the long option relative 92 * to @p long_options. 93 * 94 * @return If an option was successfully found, function returns 95 * the option character. 96 */ 97 int sys_getopt_long(int nargc, char *const *nargv, const char *options, 98 const struct sys_getopt_option *long_options, int *idx); 99 100 /** 101 * @brief Parses the command-line arguments. 102 * 103 * The sys_getopt_long_only() function works like @ref sys_getopt_long(), 104 * but '-' as well as "--" can indicate a long option. If an option that starts 105 * with '-' (not "--") doesn't match a long option, but does match a short 106 * option, it is parsed as a short option instead. 107 * 108 * @note This function is based on FreeBSD implementation but it does not 109 * support environment variable: POSIXLY_CORRECT. 110 * 111 * @param[in] nargc Arguments count. 112 * @param[in] nargv Arguments. 113 * @param[in] options String containing the legitimate option characters. 114 * @param[in] long_options Pointer to the first element of an array of 115 * @a struct sys_getopt_option. 116 * @param[in] idx If idx is not NULL, it points to a variable 117 * which is set to the index of the long option relative 118 * to @p long_options. 119 * 120 * @return If an option was successfully found, function returns 121 * the option character. 122 */ 123 int sys_getopt_long_only(int nargc, char *const *nargv, const char *options, 124 const struct sys_getopt_option *long_options, int *idx); 125 126 #ifdef __cplusplus 127 } 128 #endif 129 130 #endif /* ZEPHYR_INCLUDE_SYS_GETOPT_H_ */ 131