xapian-core  2.0.0
gnu_getopt.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2004,2009,2010 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef XAPIAN_INCLUDED_GNU_GETOPT_H
22 #define XAPIAN_INCLUDED_GNU_GETOPT_H
23 
24 // We need to include a header to get __GLIBC__ defined. Hopefully <cctype>
25 // is a safe bet.
26 #include <cctype>
27 
28 #define GNU_GETOPT_INTERFACE_VERSION 2
29 #if defined __GLIBC__ && __GLIBC__ >= 2
30 # include <gnu-versions.h>
31 # if _GNU_GETOPT_INTERFACE_VERSION == GNU_GETOPT_INTERFACE_VERSION
32 # define USE_GLIBC_GNUGETOPT
33 # endif
34 #endif
35 
36 #ifdef USE_GLIBC_GNUGETOPT
37 
38 #include <getopt.h>
39 
40 inline int
41 gnu_getopt(int argc_, char *const *argv_, const char *shortopts_) {
42  return getopt(argc_, argv_, shortopts_);
43 }
44 
45 inline int
46 gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_,
47  const struct option *longopts_, int *optind_) {
48  return getopt_long(argc_, argv_, shortopts_, longopts_, optind_);
49 }
50 
51 inline int
52 gnu_getopt_long_only(int argc_, char *const *argv_, const char *shortopts_,
53  const struct option *longopts_, int *optind_) {
54  return getopt_long_only(argc_, argv_, shortopts_, longopts_, optind_);
55 }
56 
57 #else
58 
59 #ifdef __CYGWIN__
60 // Cygwin has __declspec(dllimport) magic on optarg, etc, so just pull in the
61 // header there rather than trying to duplicate that.
62 # include <getopt.h>
63 #else
64 extern "C" {
65 extern char *optarg;
66 extern int optind;
67 extern int opterr;
68 extern int optopt;
69 }
70 
71 struct option {
72  const char *name;
73  int has_arg;
74  int * flag;
75  int val;
76 };
77 
78 # define no_argument 0
79 # define required_argument 1
80 # define optional_argument 2
81 #endif
82 
83 // For internal use only.
84 int
85 gnu_getopt_internal_(int, char *const *, const char *, const struct option *,
86  int *, int);
87 
88 inline int
89 gnu_getopt(int argc_, char *const *argv_, const char *shortopts_) {
90  return gnu_getopt_internal_(argc_, argv_, shortopts_,
91  reinterpret_cast<const struct option *>(0),
92  reinterpret_cast<int *>(0), 0);
93 }
94 
95 inline int
96 gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_,
97  const struct option *longopts_, int *optind_) {
98  return gnu_getopt_internal_(argc_, argv_, shortopts_, longopts_, optind_, 0);
99 }
100 
101 inline int
102 gnu_getopt_long_only(int argc_, char *const *argv_, const char *shortopts_,
103  const struct option *longopts_, int *optind_) {
104  return gnu_getopt_internal_(argc_, argv_, shortopts_, longopts_, optind_, 1);
105 }
106 #endif
107 
108 #endif
int gnu_getopt_long_only(int argc_, char *const *argv_, const char *shortopts_, const struct option *longopts_, int *optind_)
Definition: gnu_getopt.h:102
int gnu_getopt_internal_(int, char *const *, const char *, const struct option *, int *, int)
Definition: getopt.cc:338
int optopt
Definition: getopt.cc:104
int gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_, const struct option *longopts_, int *optind_)
Definition: gnu_getopt.h:96
int optind
Definition: getopt.cc:93
char * optarg
Definition: getopt.cc:78
int opterr
Definition: getopt.cc:98
int gnu_getopt(int argc_, char *const *argv_, const char *shortopts_)
Definition: gnu_getopt.h:89
int val
Definition: gnu_getopt.h:75
int has_arg
Definition: gnu_getopt.h:73
int * flag
Definition: gnu_getopt.h:74
const char * name
Definition: gnu_getopt.h:72