| 1 | #pragma once |
| 2 | #ifdef _WIN32 |
| 3 | |
| 4 | /* |
| 5 | getopt.h - comand line option parsing |
| 6 | Copyright Keristor Systems and Chris Croughton 1997 - 2005 |
| 7 | Internet: [email protected] |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any purpose, |
| 10 | including commercial applications, and to alter it and redistribute it |
| 11 | freely, subject to the following restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must not |
| 14 | claim that you wrote the original software. If you use this software |
| 15 | in a product, an acknowledgment in the product documentation would be |
| 16 | appreciated but is not required. |
| 17 | 2. Altered source versions must be plainly marked as such, and must not be |
| 18 | misrepresented as being the original software. |
| 19 | 3. This notice may not be removed or altered from any source distribution. |
| 20 | |
| 21 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 22 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 25 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 27 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 29 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 30 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | /**@file xgetopt.h |
| 35 | * |
| 36 | * This module is designed as a "drop-in" replacement for the GNU |
| 37 | * \c getopt.h module, providing the \c getopt(), \c getopt_long() and |
| 38 | * \c getopt_long_only() functions and the \c option structure. However, |
| 39 | * it is a compile-time replacement not an object one, the actual |
| 40 | * names of the external variables and functions are hidden so that |
| 41 | * namespace clashes don't occur (some linkers object if they find |
| 42 | * the same symbol in more than one library). |
| 43 | * |
| 44 | * It was written originally because of systems which don't have the GNU |
| 45 | * C library installed. |
| 46 | * |
| 47 | * It also provides a few functions to help create a string of short |
| 48 | * (single-character) options given an array of long option structures. |
| 49 | * |
| 50 | * This is a "clean-room" implementation, no GPL or proprietary code |
| 51 | * was looked at when creating it. The only things used were the |
| 52 | * descriptions of the interfaces in the \c man pages (and the POSIX |
| 53 | * specification), and some "black box" testing to determine error |
| 54 | * conditions and messages not specified in the interface description. |
| 55 | */ |
| 56 | |
| 57 | #if defined(_cplusplus) || defined(__cplusplus) |
| 58 | extern "C" { |
| 59 | #endif |
| 60 | |
| 61 | /* |
| 62 | * This bit of faffing about is because some systems don't like the same |
| 63 | * functions defined in more than one library, they get confused. So what |
| 64 | * we do is to redefine the variables to the names in the interface but |
| 65 | * fool the linker. This only makes a difference if you try to debug the |
| 66 | * getopt code. Note that we don't have to do it with the structure name |
| 67 | * or enums, since it's only linkage that's affected. |
| 68 | */ |
| 69 | |
| 70 | #define optarg _XLIB_optarg |
| 71 | #define optind _XLIB_optind |
| 72 | #define opterr _XLIB_opterr |
| 73 | #define optopt _XLIB_optopt |
| 74 | |
| 75 | #define getopt _XLIB_getopt |
| 76 | #define getopt_long _XLIB_getopt_long |
| 77 | #define getopt_long_only _XLIB_getopt_long_only |
| 78 | |
| 79 | /** holds a pointer to an option argument */ |
| 80 | extern char *optarg; |
| 81 | /** index to te first non-option argument */ |
| 82 | extern int optind; |
| 83 | /** allow error messages if non-zero (default) */ |
| 84 | extern int opterr; |
| 85 | /** holds the option character in error, if any */ |
| 86 | extern int optopt; |
| 87 | |
| 88 | enum |
| 89 | { |
| 90 | /** this option has no argument */ |
| 91 | no_argument, |
| 92 | /** an argument is required with this option */ |
| 93 | required_argument, |
| 94 | /** an argument to this option is optional */ |
| 95 | optional_argument |
| 96 | }; |
| 97 | |
| 98 | /** |
| 99 | * Structure to describe a long option. |
| 100 | * The last item in the array of these descriptors must have all elements |
| 101 | * set to zero |
| 102 | */ |
| 103 | struct option |
| 104 | { |
| 105 | /** long option name */ |
| 106 | const char *name; |
| 107 | /** argument type, if any */ |
| 108 | int has_arg; |
| 109 | /** pointer to a flag to be set if the option is present */ |
| 110 | int *flag; |
| 111 | /** value to be returned if the option is present */ |
| 112 | int val; |
| 113 | }; |
| 114 | |
| 115 | /** Scan the command-line parameters for options in the form {\em -x}. */ |
| 116 | int getopt(int argc, char * const argv[], const char *optstring); |
| 117 | /** Scan the command-line parameters for options, allowing both the short |
| 118 | * (single character) options and long (string) options. |
| 119 | */ |
| 120 | int getopt_long(int argc, char * argv[], const char *optstring, |
| 121 | const struct option *longopts, int *longindex); |
| 122 | /** Scan the command-line parameters for long options only */ |
| 123 | int getopt_long_only(int argc, char * argv[], const char *optstring, |
| 124 | const struct option *longopts, int *longindex); |
| 125 | |
| 126 | #if defined(_cplusplus) || defined(__cplusplus) |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | #else |
| 131 | #include <unistd.h> |
| 132 | #include <getopt.h> |
| 133 | #endif |