xapian-core  1.4.27
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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19  * USA
20  */
21 
22 #ifndef XAPIAN_INCLUDED_GNU_GETOPT_H
23 #define XAPIAN_INCLUDED_GNU_GETOPT_H
24 
25 // We need to include a header to get __GLIBC__ defined. Hopefully <cctype>
26 // is a safe bet.
27 #include <cctype>
28 
29 #define GNU_GETOPT_INTERFACE_VERSION 2
30 #if defined __GLIBC__ && __GLIBC__ >= 2
31 # include <gnu-versions.h>
32 # if _GNU_GETOPT_INTERFACE_VERSION == GNU_GETOPT_INTERFACE_VERSION
33 # define USE_GLIBC_GNUGETOPT
34 # endif
35 #endif
36 
37 #ifdef USE_GLIBC_GNUGETOPT
38 
39 #include <getopt.h>
40 
41 inline int
42 gnu_getopt(int argc_, char *const *argv_, const char *shortopts_) {
43  return getopt(argc_, argv_, shortopts_);
44 }
45 
46 inline int
47 gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_,
48  const struct option *longopts_, int *optind_) {
49  return getopt_long(argc_, argv_, shortopts_, longopts_, optind_);
50 }
51 
52 inline int
53 gnu_getopt_long_only(int argc_, char *const *argv_, const char *shortopts_,
54  const struct option *longopts_, int *optind_) {
55  return getopt_long_only(argc_, argv_, shortopts_, longopts_, optind_);
56 }
57 
58 #else
59 
60 #ifdef __CYGWIN__
61 // Cygwin has __declspec(dllimport) magic on optarg, etc, so just pull in the
62 // header there rather than trying to duplicate that.
63 # include <getopt.h>
64 #else
65 extern "C" {
66 extern char *optarg;
67 extern int optind;
68 extern int opterr;
69 extern int optopt;
70 }
71 
72 struct option {
73  const char *name;
74  int has_arg;
75  int * flag;
76  int val;
77 };
78 
79 # define no_argument 0
80 # define required_argument 1
81 # define optional_argument 2
82 #endif
83 
84 // For internal use only.
85 int
86 gnu_getopt_internal_(int, char *const *, const char *, const struct option *,
87  int *, int);
88 
89 inline int
90 gnu_getopt(int argc_, char *const *argv_, const char *shortopts_) {
91  return gnu_getopt_internal_(argc_, argv_, shortopts_,
92  reinterpret_cast<const struct option *>(0),
93  reinterpret_cast<int *>(0), 0);
94 }
95 
96 inline int
97 gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_,
98  const struct option *longopts_, int *optind_) {
99  return gnu_getopt_internal_(argc_, argv_, shortopts_, longopts_, optind_, 0);
100 }
101 
102 inline int
103 gnu_getopt_long_only(int argc_, char *const *argv_, const char *shortopts_,
104  const struct option *longopts_, int *optind_) {
105  return gnu_getopt_internal_(argc_, argv_, shortopts_, longopts_, optind_, 1);
106 }
107 #endif
108 
109 #endif
int gnu_getopt(int argc_, char *const *argv_, const char *shortopts_)
Definition: gnu_getopt.h:90
int val
Definition: gnu_getopt.h:76
int gnu_getopt_long(int argc_, char *const *argv_, const char *shortopts_, const struct option *longopts_, int *optind_)
Definition: gnu_getopt.h:97
int optopt
Definition: getopt.cc:105
int optind
Definition: getopt.cc:94
int opterr
Definition: getopt.cc:99
const char * name
Definition: gnu_getopt.h:73
char * optarg
Definition: getopt.cc:79
int gnu_getopt_long_only(int argc_, char *const *argv_, const char *shortopts_, const struct option *longopts_, int *optind_)
Definition: gnu_getopt.h:103
int * flag
Definition: gnu_getopt.h:75
int has_arg
Definition: gnu_getopt.h:74
int gnu_getopt_internal_(int, char *const *, const char *, const struct option *, int *, int)
Definition: getopt.cc:339