| 1 | /* cmdline.h |
|---|
| 2 | * Wrapper for GNU getopt which deals with standard options |
|---|
| 3 | * Copyright (C) 1998-2001,2003 Olly Betts |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #ifndef CMDLINE_H |
|---|
| 21 | # define CMDLINE_H |
|---|
| 22 | |
|---|
| 23 | #ifdef __cplusplus |
|---|
| 24 | extern "C" { |
|---|
| 25 | #endif |
|---|
| 26 | |
|---|
| 27 | /* Duplicate these from getopt.h to avoid problems |
|---|
| 28 | * with trying to compile getopt.h with C++ on MacOS X */ |
|---|
| 29 | #ifndef no_argument |
|---|
| 30 | /* These values are definitely correct since getopt.h says 0, 1, 2 work */ |
|---|
| 31 | # define no_argument 0 |
|---|
| 32 | # define required_argument 1 |
|---|
| 33 | # define optional_argument 2 |
|---|
| 34 | |
|---|
| 35 | /* FIXME: struct definition needs to match getopt.h */ |
|---|
| 36 | struct option { |
|---|
| 37 | const char *name; |
|---|
| 38 | /* has_arg can't be an enum because some compilers complain about |
|---|
| 39 | * type mismatches in all the code that assumes it is an int. */ |
|---|
| 40 | int has_arg; |
|---|
| 41 | int *flag; |
|---|
| 42 | int val; |
|---|
| 43 | }; |
|---|
| 44 | |
|---|
| 45 | extern char *optarg; |
|---|
| 46 | extern int optind; |
|---|
| 47 | extern int opterr; |
|---|
| 48 | extern int optopt; |
|---|
| 49 | #endif |
|---|
| 50 | |
|---|
| 51 | struct help_msg { |
|---|
| 52 | int opt; |
|---|
| 53 | const char *msg; |
|---|
| 54 | }; |
|---|
| 55 | |
|---|
| 56 | /* give -1 for max_args_ if there's no limit */ |
|---|
| 57 | void cmdline_init(int argc_, char *const *argv_, const char *shortopts_, |
|---|
| 58 | const struct option *longopts_, int *longind_, |
|---|
| 59 | const struct help_msg *help_, |
|---|
| 60 | int min_args_, int max_args_); |
|---|
| 61 | /* if args not NULL, use instead of auto-generated FILE args */ |
|---|
| 62 | /* if extra not NULL, display as extra blurb at end */ |
|---|
| 63 | void cmdline_set_syntax_message(const char *args, const char *extra); |
|---|
| 64 | int cmdline_getopt(void); |
|---|
| 65 | void cmdline_help(void); |
|---|
| 66 | void cmdline_version(void); |
|---|
| 67 | void cmdline_syntax(void); |
|---|
| 68 | void cmdline_too_few_args(void); |
|---|
| 69 | void cmdline_too_many_args(void); |
|---|
| 70 | int cmdline_int_arg(void); |
|---|
| 71 | double cmdline_double_arg(void); |
|---|
| 72 | |
|---|
| 73 | #define HLP_ENCODELONG(N) (-(N + 1)) |
|---|
| 74 | #define HLP_DECODELONG(N) (-(N + 1)) |
|---|
| 75 | #define HLP_ISLONG(N) ((N) <= 0) |
|---|
| 76 | #define HLP_HELP 30000 |
|---|
| 77 | #define HLP_VERSION 30001 |
|---|
| 78 | |
|---|
| 79 | #ifdef __cplusplus |
|---|
| 80 | } |
|---|
| 81 | #endif |
|---|
| 82 | |
|---|
| 83 | #endif |
|---|