199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-2-Clause 299a2dd95SBruce Richardson * Copyright (c) 2000 The NetBSD Foundation, Inc. 399a2dd95SBruce Richardson * All rights reserved. 499a2dd95SBruce Richardson * 599a2dd95SBruce Richardson * This code is derived from software contributed to The NetBSD Foundation 699a2dd95SBruce Richardson * by Dieter Baron and Thomas Klausner. 799a2dd95SBruce Richardson */ 899a2dd95SBruce Richardson 999a2dd95SBruce Richardson /** 1099a2dd95SBruce Richardson * @file 1199a2dd95SBruce Richardson * getopt compat. 1299a2dd95SBruce Richardson * 1399a2dd95SBruce Richardson * This module provides getopt() and getopt_long(). 1499a2dd95SBruce Richardson */ 1599a2dd95SBruce Richardson 1699a2dd95SBruce Richardson #ifndef _USUAL_GETOPT_H_ 1799a2dd95SBruce Richardson #define _USUAL_GETOPT_H_ 1899a2dd95SBruce Richardson 1999a2dd95SBruce Richardson #ifndef NEED_USUAL_GETOPT 2099a2dd95SBruce Richardson #if !defined(HAVE_GETOPT_H) || !defined(HAVE_GETOPT) || \ 2199a2dd95SBruce Richardson !defined(HAVE_GETOPT_LONG) 2299a2dd95SBruce Richardson #define NEED_USUAL_GETOPT 2399a2dd95SBruce Richardson #endif 2499a2dd95SBruce Richardson #endif 2599a2dd95SBruce Richardson 2699a2dd95SBruce Richardson #ifndef NEED_USUAL_GETOPT 2799a2dd95SBruce Richardson 2899a2dd95SBruce Richardson /* Use system getopt */ 2999a2dd95SBruce Richardson #ifdef RTE_TOOLCHAIN_GCC 3099a2dd95SBruce Richardson #include_next <getopt.h> 3199a2dd95SBruce Richardson #else 3299a2dd95SBruce Richardson #include <getopt.h> 3399a2dd95SBruce Richardson #endif 3499a2dd95SBruce Richardson 3599a2dd95SBruce Richardson #else /* NEED_USUAL_GETOPT */ 3699a2dd95SBruce Richardson 3799a2dd95SBruce Richardson /* avoid name collision */ 3899a2dd95SBruce Richardson #define optarg usual_optarg 3999a2dd95SBruce Richardson #define opterr usual_opterr 4099a2dd95SBruce Richardson #define optind usual_optind 4199a2dd95SBruce Richardson #define optopt usual_optopt 4299a2dd95SBruce Richardson #define getopt(a, b, c) usual_getopt(a, b, c) 4399a2dd95SBruce Richardson #define getopt_long(a, b, c, d, e) usual_getopt_long(a, b, c, d, e) 4499a2dd95SBruce Richardson 4599a2dd95SBruce Richardson 4699a2dd95SBruce Richardson /** argument to current option, or NULL if it has none */ 47*a06fb0faSStephen Hemminger extern char *optarg; 4899a2dd95SBruce Richardson /** Current position in arg string. Starts from 1. 4999a2dd95SBruce Richardson * Setting to 0 resets state. 5099a2dd95SBruce Richardson */ 5199a2dd95SBruce Richardson extern int optind; 5299a2dd95SBruce Richardson /** whether getopt() should print error messages on problems. Default: 1. */ 5399a2dd95SBruce Richardson extern int opterr; 5499a2dd95SBruce Richardson /** Option char which caused error */ 5599a2dd95SBruce Richardson extern int optopt; 5699a2dd95SBruce Richardson 5799a2dd95SBruce Richardson /** long option takes no argument */ 5899a2dd95SBruce Richardson #define no_argument 0 5999a2dd95SBruce Richardson /** long option requires argument */ 6099a2dd95SBruce Richardson #define required_argument 1 6199a2dd95SBruce Richardson /** long option has optional argument */ 6299a2dd95SBruce Richardson #define optional_argument 2 6399a2dd95SBruce Richardson 6499a2dd95SBruce Richardson /** Long option description */ 6599a2dd95SBruce Richardson struct option { 6699a2dd95SBruce Richardson /** name of long option */ 6799a2dd95SBruce Richardson const char *name; 6899a2dd95SBruce Richardson 6999a2dd95SBruce Richardson /** 7099a2dd95SBruce Richardson * whether option takes an argument. 7199a2dd95SBruce Richardson * One of no_argument, required_argument, and optional_argument. 7299a2dd95SBruce Richardson */ 7399a2dd95SBruce Richardson int has_arg; 7499a2dd95SBruce Richardson 7599a2dd95SBruce Richardson /** if not NULL, set *flag to val when option found */ 7699a2dd95SBruce Richardson int *flag; 7799a2dd95SBruce Richardson 7899a2dd95SBruce Richardson /** if flag not NULL, value to set *flag to; else return value */ 7999a2dd95SBruce Richardson int val; 8099a2dd95SBruce Richardson }; 8199a2dd95SBruce Richardson 8299a2dd95SBruce Richardson /** Compat: getopt */ 83*a06fb0faSStephen Hemminger int getopt(int argc, char *const argv[], const char *options); 8499a2dd95SBruce Richardson 8599a2dd95SBruce Richardson /** Compat: getopt_long */ 86*a06fb0faSStephen Hemminger int getopt_long(int argc, char *const argv[], const char *options, 8799a2dd95SBruce Richardson const struct option *longopts, int *longindex); 8899a2dd95SBruce Richardson 8999a2dd95SBruce Richardson /** Compat: getopt_long_only */ 90*a06fb0faSStephen Hemminger int getopt_long_only(int nargc, char *const argv[], const char *options, 9199a2dd95SBruce Richardson const struct option *long_options, int *idx); 9299a2dd95SBruce Richardson 9399a2dd95SBruce Richardson 9499a2dd95SBruce Richardson #endif /* NEED_USUAL_GETOPT */ 9599a2dd95SBruce Richardson 9699a2dd95SBruce Richardson #endif /* !_USUAL_GETOPT_H_ */ 97