/*
 *  smoke16/include/getopt.h - Interface to my re-implementation of getopt.
 *  $Revision: 1.13 $
 *  Copyright 1997, 2000, 2001, Benjamin Sittler
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*
 * WHY RE-INVENT THE WHEEL?
 *
 * I re-implemented getopt, getopt_long, and getopt_long_only because
 * there were noticable bugs in several versions of the GNU
 * implementations, and because the GNU versions aren't always
 * available on some systems (*BSD, for example.)
 *
 * These should do all the expected Unix- and GNU-style argument
 * parsing, including permution, bunching, long options with single or
 * double dashes (double dashes are required if you use
 * my_getopt_long,) and optional arguments for both long and short
 * options. A word with double dashes all by themselves halts argument
 * parsing. A required long option argument can be in the same word as
 * the option name, separated by '=', or in the next word. An optional
 * long option argument must be in the same word as the option name,
 * separated by '='.
 *
 * As with the GNU versions, a '+' prefix to the short option
 * specification (or the POSIXLY_CORRECT environment variable)
 * disables permution, a '-' prefix to the short option specification
 * returns 1 for non-options, ':' after a short option indicates a
 * required argument, and '::' after a short option specification
 * indicates an optional argument (which must appear in the same
 * word.)
 *
 * The original intent was to re-implement the documented behavior of
 * the GNU versions, but I have found it necessary to emulate some of
 * the undocumented behavior as well. Some programs depend on it.
 *
 * The GNU versions support POSIX-style -W "name=value" long
 * options. Currently, my_getopt does not support these, because I
 * don't have any documentation on them (other than the fact that they
 * are enabled by "W;" in the short option specification.) As a
 * temporary workaround, my_getopt treats "W;" in the short option
 * string identically to "W:".
 * */

#ifndef SMOKE16__GETOPT_H_INCLUDED
#define SMOKE16__GETOPT_H_INCLUDED

extern int my_getopt(int argc, char * argv[], const char *opts);

extern int optind, opterr, optopt;
extern char *optarg;

struct option {
  const char *name;
  int has_arg;
  int *flag;
  int val;
};

#undef no_argument
#define no_argument 0
#undef required_argument
#define required_argument 1
#undef optional_argument
#define optional_argument 2

extern int my_getopt_long(int argc, char * argv[], const char *shortopts,
		       const struct option *longopts, int *longind);

extern int my_getopt_long_only(int argc, char * argv[], const char *shortopts,
			    const struct option *longopts, int *longind);

extern int _my_getopt_internal(int argc, char * argv[], const char *shortopts,
			    const struct option *longopts, int *longind,
			    int long_only);

#undef getopt
#define getopt my_getopt
#undef getopt_long
#define getopt_long my_getopt_long
#undef getopt_long_only
#define getopt_long_only my_getopt_long_only
#undef _getopt_internal
#define _getopt_internal _my_getopt_internal

#endif /* SMOKE16__GETOPT_H_INCLUDED */
