Sean Anderson | 3b4a6f5 | 2020-10-27 19:55:36 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* |
| 3 | * getopt.h - a simple getopt(3) implementation. |
| 4 | * |
| 5 | * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com> |
| 6 | * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix |
| 7 | */ |
| 8 | |
| 9 | #ifndef __GETOPT_H |
| 10 | #define __GETOPT_H |
| 11 | |
Tom Rini | a4a8ea9 | 2023-12-14 13:16:57 -0500 | [diff] [blame] | 12 | #include <stdbool.h> |
| 13 | |
Sean Anderson | 3b4a6f5 | 2020-10-27 19:55:36 -0400 | [diff] [blame] | 14 | /** |
| 15 | * struct getopt_state - Saved state across getopt() calls |
| 16 | */ |
| 17 | struct getopt_state { |
| 18 | /** |
| 19 | * @index: Index of the next unparsed argument of @argv. If getopt() has |
| 20 | * parsed all of @argv, then @index will equal @argc. |
| 21 | */ |
| 22 | int index; |
Sean Anderson | 3b4a6f5 | 2020-10-27 19:55:36 -0400 | [diff] [blame] | 23 | /** @arg_index: Index within the current argument */ |
| 24 | int arg_index; |
| 25 | union { |
Sean Anderson | 3b4a6f5 | 2020-10-27 19:55:36 -0400 | [diff] [blame] | 26 | /** |
| 27 | * @opt: Option being parsed when an error occurs. @opt is only |
| 28 | * valid when getopt() returns ``?`` or ``:``. |
| 29 | */ |
| 30 | int opt; |
| 31 | /** |
| 32 | * @arg: The argument to an option, NULL if there is none. @arg |
| 33 | * is only valid when getopt() returns an option character. |
| 34 | */ |
| 35 | char *arg; |
Sean Anderson | 3b4a6f5 | 2020-10-27 19:55:36 -0400 | [diff] [blame] | 36 | }; |
| 37 | }; |
| 38 | |
| 39 | /** |
| 40 | * getopt_init_state() - Initialize a &struct getopt_state |
| 41 | * @gs: The state to initialize |
| 42 | * |
| 43 | * This must be called before using @gs with getopt(). |
| 44 | */ |
| 45 | void getopt_init_state(struct getopt_state *gs); |
| 46 | |
| 47 | int __getopt(struct getopt_state *gs, int argc, char *const argv[], |
| 48 | const char *optstring, bool silent); |
| 49 | |
| 50 | /** |
| 51 | * getopt() - Parse short command-line options |
| 52 | * @gs: Internal state and out-of-band return arguments. This must be |
| 53 | * initialized with getopt_init_context() beforehand. |
| 54 | * @argc: Number of arguments, not including the %NULL terminator |
| 55 | * @argv: Argument list, terminated by %NULL |
| 56 | * @optstring: Option specification, as described below |
| 57 | * |
| 58 | * getopt() parses short options. Short options are single characters. They may |
| 59 | * be followed by a required argument or an optional argument. Arguments to |
| 60 | * options may occur in the same argument as an option (like ``-larg``), or |
| 61 | * in the following argument (like ``-l arg``). An argument containing |
| 62 | * options begins with a ``-``. If an option expects no arguments, then it may |
| 63 | * be immediately followed by another option (like ``ls -alR``). |
| 64 | * |
| 65 | * @optstring is a list of accepted options. If an option is followed by ``:`` |
| 66 | * in @optstring, then it expects a mandatory argument. If an option is followed |
| 67 | * by ``::`` in @optstring, it expects an optional argument. @gs.arg points |
| 68 | * to the argument, if one is parsed. |
| 69 | * |
| 70 | * getopt() stops parsing options when it encounters the first non-option |
| 71 | * argument, when it encounters the argument ``--``, or when it runs out of |
| 72 | * arguments. For example, in ``ls -l foo -R``, option parsing will stop when |
| 73 | * getopt() encounters ``foo``, if ``l`` does not expect an argument. However, |
| 74 | * the whole list of arguments would be parsed if ``l`` expects an argument. |
| 75 | * |
| 76 | * An example invocation of getopt() might look like:: |
| 77 | * |
| 78 | * char *argv[] = { "program", "-cbx", "-a", "foo", "bar", 0 }; |
| 79 | * int opt, argc = ARRAY_SIZE(argv) - 1; |
| 80 | * struct getopt_state gs; |
| 81 | * |
| 82 | * getopt_init_state(&gs); |
| 83 | * while ((opt = getopt(&gs, argc, argv, "a::b:c")) != -1) |
| 84 | * printf("opt = %c, index = %d, arg = \"%s\"\n", opt, gs.index, gs.arg); |
| 85 | * printf("%d argument(s) left\n", argc - gs.index); |
| 86 | * |
| 87 | * and would produce an output of:: |
| 88 | * |
| 89 | * opt = c, index = 1, arg = "<NULL>" |
| 90 | * opt = b, index = 2, arg = "x" |
| 91 | * opt = a, index = 4, arg = "foo" |
| 92 | * 1 argument(s) left |
| 93 | * |
| 94 | * For further information, refer to the getopt(3) man page. |
| 95 | * |
| 96 | * Return: |
| 97 | * * An option character if an option is found. @gs.arg is set to the |
| 98 | * argument if there is one, otherwise it is set to ``NULL``. |
| 99 | * * ``-1`` if there are no more options, if a non-option argument is |
| 100 | * encountered, or if an ``--`` argument is encountered. |
| 101 | * * ``'?'`` if we encounter an option not in @optstring. @gs.opt is set to |
| 102 | * the unknown option. |
| 103 | * * ``':'`` if an argument is required, but no argument follows the |
| 104 | * option. @gs.opt is set to the option missing its argument. |
| 105 | * |
| 106 | * @gs.index is always set to the index of the next unparsed argument in @argv. |
| 107 | */ |
| 108 | static inline int getopt(struct getopt_state *gs, int argc, |
| 109 | char *const argv[], const char *optstring) |
| 110 | { |
| 111 | return __getopt(gs, argc, argv, optstring, false); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * getopt_silent() - Parse short command-line options silently |
| 116 | * @gs: State |
| 117 | * @argc: Argument count |
| 118 | * @argv: Argument list |
| 119 | * @optstring: Option specification |
| 120 | * |
| 121 | * Same as getopt(), except no error messages are printed. |
| 122 | */ |
| 123 | static inline int getopt_silent(struct getopt_state *gs, int argc, |
| 124 | char *const argv[], const char *optstring) |
| 125 | { |
| 126 | return __getopt(gs, argc, argv, optstring, true); |
| 127 | } |
| 128 | |
| 129 | #endif /* __GETOPT_H */ |