qfcc: getopt support. Yes, it exists, and it works, and it's fun! :)

This commit is contained in:
Jeff Teunissen 2001-10-26 08:14:05 +00:00
parent 5cbd50d835
commit 89c7e0df1a
4 changed files with 1561 additions and 118 deletions

169
tools/qfcc/include/getopt.h Normal file
View file

@ -0,0 +1,169 @@
/* Declarations for getopt.
Copyright (C) 1989,90,91,92,93,94,96,97,98 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#ifndef _GETOPT_H
#ifndef __need_getopt
# define _GETOPT_H 1
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* For communication from `getopt' to the caller.
When `getopt' finds an option that takes an argument,
the argument value is returned here.
Also, when `ordering' is RETURN_IN_ORDER,
each non-option ARGV-element is returned here. */
extern char *optarg;
/* Index in ARGV of the next element to be scanned.
This is used for communication to and from the caller
and for communication between successive calls to `getopt'.
On entry to `getopt', zero means this is the first call; initialize.
When `getopt' returns -1, this is the index of the first of the
non-option elements that the caller should itself scan.
Otherwise, `optind' communicates from one call to the next
how much of ARGV has been scanned so far. */
extern int optind;
/* Callers store zero here to inhibit the error message `getopt' prints
for unrecognized options. */
extern int opterr;
/* Set to an option character which was unrecognized. */
extern int optopt;
#ifndef __need_getopt
/* Describe the long-named options requested by the application.
The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
of `struct option' terminated by an element containing a name which is
zero.
The field `has_arg' is:
no_argument (or 0) if the option does not take an argument,
required_argument (or 1) if the option requires an argument,
optional_argument (or 2) if the option takes an optional argument.
If the field `flag' is not NULL, it points to a variable that is set
to the value given in the field `val' when the option is found, but
left unchanged if the option is not found.
To have a long-named option do something other than set an `int' to
a compiled-in constant, such as set a value from `optarg', set the
option's `flag' field to zero and its `val' field to a nonzero
value (the equivalent single-letter option character, if there is
one). For long options that have a zero `flag' field, `getopt'
returns the contents of the `val' field. */
struct option
{
# if defined __STDC__ && __STDC__
const char *name;
# else
char *name;
# endif
/* has_arg can't be an enum because some compilers complain about
type mismatches in all the code that assumes it is an int. */
int has_arg;
int *flag;
int val;
};
/* Names for the values of the `has_arg' field of `struct option'. */
# define no_argument 0
# define required_argument 1
# define optional_argument 2
#endif /* need getopt */
/* Get definitions and prototypes for functions to process the
arguments in ARGV (ARGC of them, minus the program name) for
options given in OPTS.
Return the option character from OPTS just read. Return -1 when
there are no more options. For unrecognized options, or options
missing arguments, `optopt' is set to the option letter, and '?' is
returned.
The OPTS string is a list of characters which are recognized option
letters, optionally followed by colons, specifying that that letter
takes an argument, to be placed in `optarg'.
If a letter in OPTS is followed by two colons, its argument is
optional. This behavior is specific to the GNU `getopt'.
The argument `--' causes premature termination of argument
scanning, explicitly telling `getopt' that there are no more
options.
If OPTS begins with `--', then non-option arguments are treated as
arguments to the option '\0'. This behavior is specific to the GNU
`getopt'. */
#if defined __STDC__ && __STDC__
# ifdef __GNU_LIBRARY__
/* Many other libraries have conflicting prototypes for getopt, with
differences in the consts, in stdlib.h. To avoid compilation
errors, only prototype getopt for the GNU C library. */
extern int getopt (int __argc, char *const *__argv, const char *__shortopts);
# else /* not __GNU_LIBRARY__ */
extern int getopt ();
# endif /* __GNU_LIBRARY__ */
# ifndef __need_getopt
extern int getopt_long (int __argc, char *const *__argv, const char *__shortopts,
const struct option *__longopts, int *__longind);
extern int getopt_long_only (int __argc, char *const *__argv,
const char *__shortopts,
const struct option *__longopts, int *__longind);
/* Internal only. Users should not call this directly. */
extern int _getopt_internal (int __argc, char *const *__argv,
const char *__shortopts,
const struct option *__longopts, int *__longind,
int __long_only);
# endif
#else /* not __STDC__ */
extern int getopt ();
# ifndef __need_getopt
extern int getopt_long ();
extern int getopt_long_only ();
extern int _getopt_internal ();
# endif
#endif /* __STDC__ */
#ifdef __cplusplus
}
#endif
/* Make sure we later can get all the definitions and declarations. */
#undef __need_getopt
#endif /* getopt.h */

1055
tools/qfcc/source/getopt.c Normal file

File diff suppressed because it is too large Load diff

188
tools/qfcc/source/getopt1.c Normal file
View file

@ -0,0 +1,188 @@
/* getopt_long and getopt_long_only entry points for GNU getopt.
Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98
Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "getopt.h"
#if !defined __STDC__ || !__STDC__
/* This is a separate conditional since some stdc systems
reject `defined (const)'. */
#ifndef const
#define const
#endif
#endif
#include <stdio.h>
/* Comment out all this code if we are using the GNU C Library, and are not
actually compiling the library itself. This code is part of the GNU C
Library, but also included in many other GNU distributions. Compiling
and linking in this code is a waste when using the GNU C library
(especially if it is a shared library). Rather than having every GNU
program understand `configure --with-gnu-libc' and omit the object files,
it is simpler to just do this in the source for each such file. */
#define GETOPT_INTERFACE_VERSION 2
#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
#include <gnu-versions.h>
#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
#define ELIDE_CODE
#endif
#endif
#ifndef ELIDE_CODE
/* This needs to come after some library #include
to get __GNU_LIBRARY__ defined. */
#ifdef __GNU_LIBRARY__
#include <stdlib.h>
#endif
#ifndef NULL
#define NULL 0
#endif
int
getopt_long (argc, argv, options, long_options, opt_index)
int argc;
char *const *argv;
const char *options;
const struct option *long_options;
int *opt_index;
{
return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
}
/* Like getopt_long, but '-' as well as '--' can indicate a long option.
If an option that starts with '-' (not '--') doesn't match a long option,
but does match a short option, it is parsed as a short option
instead. */
int
getopt_long_only (argc, argv, options, long_options, opt_index)
int argc;
char *const *argv;
const char *options;
const struct option *long_options;
int *opt_index;
{
return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
}
#endif /* Not ELIDE_CODE. */
#ifdef TEST
#include <stdio.h>
int
main (argc, argv)
int argc;
char **argv;
{
int c;
int digit_optind = 0;
while (1)
{
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] =
{
{"add", 1, 0, 0},
{"append", 0, 0, 0},
{"delete", 1, 0, 0},
{"verbose", 0, 0, 0},
{"create", 0, 0, 0},
{"file", 1, 0, 0},
{0, 0, 0, 0}
};
c = getopt_long (argc, argv, "abc:d:0123456789",
long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf ("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf ("option %c\n", c);
break;
case 'a':
printf ("option a\n");
break;
case 'b':
printf ("option b\n");
break;
case 'c':
printf ("option c with value `%s'\n", optarg);
break;
case 'd':
printf ("option d with value `%s'\n", optarg);
break;
case '?':
break;
default:
printf ("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc)
{
printf ("non-option ARGV-elements: ");
while (optind < argc)
printf ("%s ", argv[optind++]);
printf ("\n");
}
exit (0);
}
#endif /* TEST */

View file

@ -42,6 +42,8 @@
# include <unistd.h>
#endif
#include <getopt.h>
#include <QF/crc.h>
#include <QF/hash.h>
#include <QF/qendian.h>
@ -49,9 +51,22 @@
#include "qfcc.h"
options_t options;
options_t options;
char *sourcedir;
static struct option const long_options[] =
{
{"source", required_argument, 0, 's'},
{"quiet", no_argument, 0, 'q'},
{"verbose", no_argument, 0, 'v'},
{"code", required_argument, 0, 'C'},
{"warn", required_argument, 0, 'W'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{NULL, 0, NULL, 0}
};
char sourcedir[1024];
char destfile[1024];
char debugfile[1024];
@ -905,28 +920,133 @@ PR_PrintFunction (def_t *def)
" --undefined-function-warning warn when a function isn't defined\n"
*/
qboolean
ParseParmOptions (const char *options, const char *name)
static void
usage (int status)
{
const char *start;
char *where, *terminator;
// Options are comma-separated, so bail if the string has a comma
if ((!(*name)) || strchr (name, ','))
return false;
for (start = options;; start = terminator) {
if (!(where = strstr (start, name)))
break;
terminator = where + strlen (name);
if ((where == start) || (*(where - 1) == ','))
if ((*terminator == ',') || (*terminator == '\0'))
return true;
}
return false;
printf ("%s - the QuakeForge Code Compiler\n", myargv[0]);
printf ("Usage: %s [options]\n", myargv[0]);
printf (
"Options:\n"
" -s, --source DIR look for progs.src in DIR instead of \".\"\n"
" -q, --quiet Inhibit usual output\n"
" -v, --verbose Display more output than usual\n"
" -C, --code OPTION,... Set code generation options\n"
" -W, --warn OPTION,... Set warning options\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n\n"
"For help on options for --code and --warn, see the qfcc(1) manual page\n"
);
exit (status);
}
static int
DecodeArgs (int argc, char **argv)
{
int c;
options.code.progsversion = PROG_VERSION;
options.warnings.uninited_variable = 1;
options.verbosity = 2;
sourcedir = ".";
while ((c = getopt_long (argc, argv,
"s:" // source dir
"q" // quiet
"v" // verbose
"C:" // code options
"W:" // warning options
"h" // help
"V", // version
long_options, (int *) 0)) != EOF) {
switch (c) {
case 'h': // help
usage (0);
break;
case 'V': // version
printf ("%s version %s", PACKAGE, VERSION);
exit (0);
break;
case 's': // src dir
sourcedir = strdup (optarg);
break;
case 'q': // quiet
options.verbosity -= 1;
break;
case 'v': // verbose
options.verbosity += 1;
break;
case 'C': { // code options
char *opts = strdup (optarg);
char *temp = strtok (opts, ",");
while (temp) {
if (!(strcasecmp (temp, "cow"))) {
options.code.cow = true;
} else if (!(strcasecmp (temp, "no-cow"))) {
options.code.cow = false;
} else if (!(strcasecmp (temp, "debug"))) {
options.code.debug = true;
} else if (!(strcasecmp (temp, "no-debug"))) {
options.code.debug = false;
} else if (!(strcasecmp (temp, "v6only"))) {
options.code.progsversion = PROG_ID_VERSION;
} else if (!(strcasecmp (temp, "no-v6only"))) {
options.code.progsversion = PROG_VERSION;
}
temp = strtok (NULL, ",");
}
free (opts);
}
break;
case 'W': { // warning options
char *opts = strdup (optarg);
char *temp = strtok (opts, ",");
while (temp) {
if (!(strcasecmp (temp, "all"))) {
options.warnings.cow = true;
options.warnings.undefined_function = true;
options.warnings.uninited_variable = true;
options.warnings.vararg_integer = true;
} else if (!(strcasecmp (temp, "none"))) {
options.warnings.cow = false;
options.warnings.undefined_function = false;
options.warnings.uninited_variable = false;
options.warnings.vararg_integer = false;
} else if (!(strcasecmp (temp, "cow"))) {
options.warnings.cow = true;
} else if (!(strcasecmp (temp, "no-cow"))) {
options.warnings.cow = false;
} else if (!(strcasecmp (temp, "error"))) {
options.warnings.promote = true;
} else if (!(strcasecmp (temp, "no-error"))) {
options.warnings.promote = false;
} else if (!(strcasecmp (temp, "undef-function"))) {
options.warnings.undefined_function = true;
} else if (!(strcasecmp (temp, "no-undef-function"))) {
options.warnings.undefined_function = false;
} else if (!(strcasecmp (temp, "uninited-var"))) {
options.warnings.uninited_variable = true;
} else if (!(strcasecmp (temp, "no-uninited-var"))) {
options.warnings.uninited_variable = false;
} else if (!(strcasecmp (temp, "vararg-integer"))) {
options.warnings.vararg_integer = true;
} else if (!(strcasecmp (temp, "no-vararg-integer"))) {
options.warnings.vararg_integer = false;
}
temp = strtok (NULL, ",");
}
free (opts);
}
break;
default:
usage (1);
}
}
return optind;
}
//============================================================================
/*
@ -937,89 +1057,21 @@ ParseParmOptions (const char *options, const char *name)
int
main (int argc, char **argv)
{
char *src;
char filename[1024];
int p, crc;
double start, stop;
int no_cpp = 0;
char *src;
char filename[1024];
long int crc;
double start, stop;
qboolean no_cpp = false;
start = Sys_DoubleTime ();
myargc = argc;
myargv = argv;
options.code.progsversion = PROG_VERSION;
options.warnings.uninited_variable = 1;
options.verbosity = 2;
if (CheckParm ("-h") || CheckParm ("--help")) {
printf ("%s - QuakeForge Code Compiler\n", argv[0]);
printf ("Usage: %s [options]\n", argv[0]);
printf (
"Options: \n"
" -s, --source <dir> look for progs.src in directory <dir>\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n"
" --cow allow assignment to initialized globals\n"
" --id only support id (progs version 6) features\n"
" --warn=error treat warnings as errors\n"
" --no-undefined-function-warning don't warn when a function isn't defined\n"
);
return 1;
}
if (CheckParm ("-V") || CheckParm ("--version")) {
printf ("%s version %s\n", PACKAGE, VERSION);
return 1;
}
if ((p = CheckParm ("--source")) && p < argc - 1) {
strcpy (sourcedir, argv[p + 1]);
} else {
if ((p = CheckParm ("-s")) && p < argc - 1) {
strcpy (sourcedir, argv[p + 1]);
} else {
strcpy (sourcedir, ".");
}
}
if (CheckParm ("--quiet=2")) { //FIXME: getopt, need getopt </#5>
options.verbosity = 0;
}
if (CheckParm ("--quiet")) {
options.verbosity = 1;
}
if (CheckParm ("--cow")) {
options.code.cow = 1;
options.warnings.cow = 1;
}
if (CheckParm ("--id")) {
options.code.progsversion = PROG_ID_VERSION;
}
if (CheckParm ("--debug")) {
options.code.debug = 1;
}
DecodeArgs (argc, argv);
if (CheckParm ("--no-cpp")) {
no_cpp = 1;
}
// FIXME eww, really must go to getopt
if (CheckParm ("--warn=error")) {
options.warnings.promote = 1;
}
if (CheckParm ("--warn=nocow")) {
options.warnings.cow = 0;
}
options.warnings.undefined_function = 1;
if (CheckParm ("--no-undefined-function-warning")) {
options.warnings.undefined_function = 0;
no_cpp = true;
}
if (strcmp (sourcedir, ".")) {
@ -1173,24 +1225,3 @@ main (int argc, char **argv)
printf ("Compilation time: %0.3g seconds.\n", (stop - start));
return 0;
}
/*
static void
usage (int status)
{
printf ("%s - the QuakeForge Code Compiler\n", PROGRAM);
printf ("Usage: %s [options]\n", argv[0]);
printf (
"Options:\n"
" -s, --source DIR look for progs.src in DIR instead of \".\"\n"
" -q, --quiet NUM Inhibit usual output\n"
" -g, --debug Output symbol information\n"
" --code=<option,...> Set code generation options\n"
" --warn=<option,...> Set warning options\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n\n"
"For help on options for --code and --warn, see the qfcc(1) man page\n"
);
exit (status);
}
*/