1 2 /* $OpenBSD: getopt_long.c,v 1.13 2003/06/03 01:52:40 millert Exp $ */ 3 /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ 4 5 /* 6 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL 13 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 14 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE 15 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 17 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 18 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 /*- 21 * Copyright (c) 2000 The NetBSD Foundation, Inc. 22 * All rights reserved. 23 * 24 * This code is derived from software contributed to The NetBSD Foundation 25 * by Dieter Baron and Thomas Klausner. 26 * 27 * Redistribution and use in source and binary forms, with or without 28 * modification, are permitted provided that the following conditions 29 * are met: 30 * 1. Redistributions of source code must retain the above copyright 31 * notice, this list of conditions and the following disclaimer. 32 * 2. Redistributions in binary form must reproduce the above copyright 33 * notice, this list of conditions and the following disclaimer in the 34 * documentation and/or other materials provided with the distribution. 35 * 3. All advertising materials mentioning features or use of this software 36 * must display the following acknowledgement: 37 * This product includes software developed by the NetBSD 38 * Foundation, Inc. and its contributors. 39 * 4. Neither the name of The NetBSD Foundation nor the names of its 40 * contributors may be used to endorse or promote products derived 41 * from this software without specific prior written permission. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 44 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 45 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 47 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 48 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 49 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 51 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 52 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 53 * POSSIBILITY OF SUCH DAMAGE. 54 */ 55 56 #ifndef __BSD_GETOPT_LONG_H__ 57 #define __BSD_GETOPT_LONG_H__ 58 59 #ifndef HAVE_GETOPT_LONG 60 61 /* 62 * GNU-like getopt_long() and 4.4BSD getsubopt()/optreset extensions 63 */ 64 # ifndef no_argument 65 # define no_argument 0 66 # endif 67 # ifndef required_argument 68 # define required_argument 1 69 # endif 70 # ifndef optional_argument 71 # define optional_argument 2 72 # endif 73 74 struct pure_option { 75 /* name of long option */ 76 const char *name; 77 /* 78 * one of no_argument, required_argument, and optional_argument: 79 * whether option takes an argument 80 */ 81 int has_arg; 82 /* if not NULL, set *flag to val when option found */ 83 int *flag; 84 /* if flag not NULL, value to set *flag to; else return value */ 85 int val; 86 }; 87 88 int pure_getopt_long(int nargc, char * const *nargv, const char *options, 89 const struct pure_option *long_options, int *idx); 90 91 int pure_pure_getopt_long_only(int nargc, char * const *nargv, 92 const char *options, 93 const struct pure_option *long_options, 94 int *idx); 95 96 int pure_getopt(int nargc, char * const *nargv, const char *options); 97 98 extern const char *pure_optarg; /* getopt(3) external variables */ 99 extern int pure_opterr; 100 extern int pure_optind; 101 extern int pure_optopt; 102 extern int pure_optreset; 103 104 /* prefix+macros just to avoid clashes with existing getopt() implementations */ 105 106 # ifndef IN_GETOPT_LONG_C 107 # undef option 108 # define option pure_option 109 # undef getopt_long 110 # define getopt_long(A, B, C, D, E) pure_getopt_long(A, B, C, D, E) 111 # undef getopt_long_only 112 # define getopt_long_only(A, B, C, D, E) pure_getopt_long_only(A, B, C, D, E) 113 # undef getopt 114 # define getopt(A, B, C) pure_getopt(A, B, C) 115 # undef optarg 116 # define optarg pure_optarg 117 # undef opterr 118 # define opterr pure_opterr 119 # undef optind 120 # define optind pure_optind 121 # undef optopt 122 # define optopt pure_optopt 123 # undef optreset 124 # define optreset pure_optreset 125 # endif 126 127 #endif 128 129 #endif 130