1 /* SPDX-License-Identifier: BSD-2-Clause 2 * Copyright (c) 2000 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Dieter Baron and Thomas Klausner. 7 */ 8 9 /** 10 * @file 11 * getopt compat. 12 * 13 * This module provides getopt() and getopt_long(). 14 */ 15 16 #ifndef _USUAL_GETOPT_H_ 17 #define _USUAL_GETOPT_H_ 18 19 #ifndef NEED_USUAL_GETOPT 20 #if !defined(HAVE_GETOPT_H) || !defined(HAVE_GETOPT) || \ 21 !defined(HAVE_GETOPT_LONG) 22 #define NEED_USUAL_GETOPT 23 #endif 24 #endif 25 26 #ifndef NEED_USUAL_GETOPT 27 28 /* Use system getopt */ 29 #ifdef RTE_TOOLCHAIN_GCC 30 #include_next <getopt.h> 31 #else 32 #include <getopt.h> 33 #endif 34 35 #else /* NEED_USUAL_GETOPT */ 36 37 /* avoid name collision */ 38 #define optarg usual_optarg 39 #define opterr usual_opterr 40 #define optind usual_optind 41 #define optopt usual_optopt 42 #define getopt(a, b, c) usual_getopt(a, b, c) 43 #define getopt_long(a, b, c, d, e) usual_getopt_long(a, b, c, d, e) 44 45 46 /** argument to current option, or NULL if it has none */ 47 extern char *optarg; 48 /** Current position in arg string. Starts from 1. 49 * Setting to 0 resets state. 50 */ 51 extern int optind; 52 /** whether getopt() should print error messages on problems. Default: 1. */ 53 extern int opterr; 54 /** Option char which caused error */ 55 extern int optopt; 56 57 /** long option takes no argument */ 58 #define no_argument 0 59 /** long option requires argument */ 60 #define required_argument 1 61 /** long option has optional argument */ 62 #define optional_argument 2 63 64 /** Long option description */ 65 struct option { 66 /** name of long option */ 67 const char *name; 68 69 /** 70 * whether option takes an argument. 71 * One of no_argument, required_argument, and optional_argument. 72 */ 73 int has_arg; 74 75 /** if not NULL, set *flag to val when option found */ 76 int *flag; 77 78 /** if flag not NULL, value to set *flag to; else return value */ 79 int val; 80 }; 81 82 /** Compat: getopt */ 83 int getopt(int argc, char *const argv[], const char *options); 84 85 /** Compat: getopt_long */ 86 int getopt_long(int argc, char *const argv[], const char *options, 87 const struct option *longopts, int *longindex); 88 89 /** Compat: getopt_long_only */ 90 int getopt_long_only(int nargc, char *const argv[], const char *options, 91 const struct option *long_options, int *idx); 92 93 94 #endif /* NEED_USUAL_GETOPT */ 95 96 #endif /* !_USUAL_GETOPT_H_ */ 97