From 2cffc7d4202fc1197280a05d998075551b459283 Mon Sep 17 00:00:00 2001 From: Thomas Wouters Date: Fri, 3 Nov 2000 08:18:37 +0000 Subject: [PATCH] Move our own getopt() implementation to _PyOS_GetOpt(), and use it regardless of whether the system getopt() does what we want. This avoids the hassle with prototypes and externs, and the check to see if the system getopt() does what we want. Prefix optind, optarg and opterr with _PyOS_ to avoid name clashes. Add new include file to define the right symbols. Fix Demo/pyserv/pyserv.c to include getopt.h itself, instead of relying on Python to provide it. --- Demo/pysvr/pysvr.c | 5 +-- Include/pygetopt.h | 17 +++++++++ Modules/main.c | 28 +++++++-------- Python/Makefile.in | 2 +- Python/getopt.c | 49 ++++++++++---------------- configure | 86 +++++++++++++--------------------------------- configure.in | 16 --------- 7 files changed, 73 insertions(+), 130 deletions(-) create mode 100644 Include/pygetopt.h diff --git a/Demo/pysvr/pysvr.c b/Demo/pysvr/pysvr.c index d7d755cb3c0..99d7b078188 100644 --- a/Demo/pysvr/pysvr.c +++ b/Demo/pysvr/pysvr.c @@ -19,6 +19,7 @@ can log in on your machine. Use with caution! #include #include +#include /* XXX Umpfh. Python.h defines a typedef destructor, which conflicts with pthread.h. @@ -32,10 +33,6 @@ extern int Py_VerboseFlag; #define PORT 4000 #endif -extern int optind; -extern char *optarg; -extern int getopt(int, char **, char *); - struct workorder { int conn; struct sockaddr_in addr; diff --git a/Include/pygetopt.h b/Include/pygetopt.h new file mode 100644 index 00000000000..9c74cbe9521 --- /dev/null +++ b/Include/pygetopt.h @@ -0,0 +1,17 @@ + +#ifndef Py_PYGETOPT_H +#define Py_PYGETOPT_H +#ifdef __cplusplus +extern "C" { +#endif + +extern DL_IMPORT(int) _PyOS_opterr; +extern DL_IMPORT(int) _PyOS_optind; +extern DL_IMPORT(char *) _PyOS_optarg; + +DL_IMPORT(int) _PyOS_GetOpt(int argc, char **argv, char *optstring); + +#ifdef __cplusplus +} +#endif +#endif /* !Py_PYGETOPT_H */ diff --git a/Modules/main.c b/Modules/main.c index b4d7cdf6bed..c508eb6c2b6 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -17,15 +17,11 @@ #define PYTHONHOMEHELP "/python2.0" #endif +#include "pygetopt.h" + #define COPYRIGHT \ "Type \"copyright\", \"credits\" or \"license\" for more information." -/* Interface to getopt(): */ -extern int optind; -extern char *optarg; -extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */ - - /* For Py_GetArgcArgv(); set by main() */ static char **orig_argv; static int orig_argc; @@ -105,16 +101,16 @@ Py_Main(int argc, char **argv) if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0') unbuffered = 1; - while ((c = getopt(argc, argv, "c:diOStuUvxXhV")) != EOF) { + while ((c = _PyOS_GetOpt(argc, argv, "c:diOStuUvxXhV")) != EOF) { if (c == 'c') { /* -c is the last option; following arguments that look like options are left for the the command to interpret. */ - command = malloc(strlen(optarg) + 2); + command = malloc(strlen(_PyOS_optarg) + 2); if (command == NULL) Py_FatalError( "not enough memory to copy -c argument"); - strcpy(command, optarg); + strcpy(command, _PyOS_optarg); strcat(command, "\n"); break; } @@ -181,10 +177,10 @@ Py_Main(int argc, char **argv) exit(0); } - if (command == NULL && optind < argc && - strcmp(argv[optind], "-") != 0) + if (command == NULL && _PyOS_optind < argc && + strcmp(argv[_PyOS_optind], "-") != 0) { - filename = argv[optind]; + filename = argv[_PyOS_optind]; if (filename != NULL) { if ((fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "%s: can't open file '%s'\n", @@ -253,12 +249,12 @@ Py_Main(int argc, char **argv) if (command != NULL) { - /* Backup optind and force sys.argv[0] = '-c' */ - optind--; - argv[optind] = "-c"; + /* Backup _PyOS_optind and force sys.argv[0] = '-c' */ + _PyOS_optind--; + argv[_PyOS_optind] = "-c"; } - PySys_SetArgv(argc-optind, argv+optind); + PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind); if ((inspect || (command == NULL && filename == NULL)) && isatty(fileno(stdin))) { diff --git a/Python/Makefile.in b/Python/Makefile.in index 597cb208bf7..0e4ca27bee3 100644 --- a/Python/Makefile.in +++ b/Python/Makefile.in @@ -46,7 +46,7 @@ AROBJS= \ marshal.o modsupport.o mystrtoul.o \ pyfpe.o pystate.o pythonrun.o \ structmember.o sysmodule.o \ - traceback.o \ + traceback.o getopt.o \ $(DYNLOADFILE) \ $(LIBOBJS) OBJS= $(AROBJS) sigcheck.o diff --git a/Python/getopt.c b/Python/getopt.c index 8af67fa94d0..d80f60721e2 100644 --- a/Python/getopt.c +++ b/Python/getopt.c @@ -27,48 +27,35 @@ #include #include -#define bool int -#ifndef TRUE -#define TRUE 1 -#endif -#ifndef FALSE -#define FALSE 0 -#endif +int _PyOS_opterr = 1; /* generate error messages */ +int _PyOS_optind = 1; /* index into argv array */ +char *_PyOS_optarg = NULL; /* optional argument */ -bool opterr = TRUE; /* generate error messages */ -int optind = 1; /* index into argv array */ -char * optarg = NULL; /* optional argument */ - - -#ifndef __BEOS__ -int getopt(int argc, char *argv[], char optstring[]) -#else -int getopt(int argc, char *const *argv, const char *optstring) -#endif +int _PyOS_GetOpt(int argc, char **argv, char *optstring) { - static char *opt_ptr = ""; - register char *ptr; - int option; + static char *opt_ptr = ""; + char *ptr; + int option; if (*opt_ptr == '\0') { - if (optind >= argc || argv[optind][0] != '-' || - argv[optind][1] == '\0' /* lone dash */ ) + if (_PyOS_optind >= argc || argv[_PyOS_optind][0] != '-' || + argv[_PyOS_optind][1] == '\0' /* lone dash */ ) return -1; - else if (strcmp(argv[optind], "--") == 0) { - ++optind; + else if (strcmp(argv[_PyOS_optind], "--") == 0) { + ++_PyOS_optind; return -1; } - opt_ptr = &argv[optind++][1]; + opt_ptr = &argv[_PyOS_optind++][1]; } if ( (option = *opt_ptr++) == '\0') - return -1; + return -1; if ((ptr = strchr(optstring, option)) == NULL) { - if (opterr) + if (_PyOS_opterr) fprintf(stderr, "Unknown option: -%c\n", option); return '?'; @@ -76,19 +63,19 @@ int getopt(int argc, char *const *argv, const char *optstring) if (*(ptr + 1) == ':') { if (*opt_ptr != '\0') { - optarg = opt_ptr; + _PyOS_optarg = opt_ptr; opt_ptr = ""; } else { - if (optind >= argc) { - if (opterr) + if (_PyOS_optind >= argc) { + if (_PyOS_opterr) fprintf(stderr, "Argument expected for the -%c option\n", option); return '?'; } - optarg = argv[optind++]; + _PyOS_optarg = argv[_PyOS_optind++]; } } diff --git a/configure b/configure index 93df88fce9b..eb0d9acadb7 100755 --- a/configure +++ b/configure @@ -4839,7 +4839,7 @@ else int main() { /* Ultrix mips cc rejects this. */ -typedef int charset[2]; const charset x; +typedef int charset[2]; const charset x = {0,0}; /* SunOS 4.1.1 cc rejects this. */ char const *const *ccp; char **p; @@ -4914,7 +4914,7 @@ for ac_kw in inline __inline__ __inline; do #include "confdefs.h" int main() { -} $ac_kw foo() { +} int $ac_kw foo() { ; return 0; } EOF if { (eval echo configure:4921: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then @@ -5625,47 +5625,9 @@ done LIBS=$LIBS_SAVE -# check for getopt -echo $ac_n "checking for genuine getopt""... $ac_c" 1>&6 -echo "configure:5631: checking for genuine getopt" >&5 -if eval "test \"`echo '$''{'ac_cv_func_getopt'+set}'`\" = set"; then - echo $ac_n "(cached) $ac_c" 1>&6 -else - if test "$cross_compiling" = yes; then - ac_cv_func_getopt=no -else - cat > conftest.$ac_ext < -extern int optind, opterr, getopt(); -extern char* optarg; -int main() { - char* av[] = { "testprog", "parameter", "-fFlag", NULL }; - opterr = 0; - if (getopt(3, av, "f:") == 'f') { exit(1); } - exit(0); -} -EOF -if { (eval echo configure:5651: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null -then - ac_cv_func_getopt=yes -else - echo "configure: failed program was:" >&5 - cat conftest.$ac_ext >&5 - rm -fr conftest* - ac_cv_func_getopt=no -fi -rm -fr conftest* -fi - -fi -echo "$ac_t""$ac_cv_func_getopt" 1>&6 -test $ac_cv_func_getopt = no && LIBOBJS="$LIBOBJS getopt.o" - # check whether malloc(0) returns NULL or not echo $ac_n "checking what malloc(0) returns""... $ac_c" 1>&6 -echo "configure:5669: checking what malloc(0) returns" >&5 +echo "configure:5631: checking what malloc(0) returns" >&5 if eval "test \"`echo '$''{'ac_cv_malloc_zero'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else @@ -5673,7 +5635,7 @@ else { echo "configure: error: can not run test program while cross compiling" 1>&2; exit 1; } else cat > conftest.$ac_ext < #ifdef HAVE_STDLIB @@ -5692,7 +5654,7 @@ main() { exit(0); } EOF -if { (eval echo configure:5696: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null +if { (eval echo configure:5658: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null then ac_cv_malloc_zero=nonnull else @@ -5718,17 +5680,17 @@ fi # check for wchar.h ac_safe=`echo "wchar.h" | sed 'y%./+-%__p_%'` echo $ac_n "checking for wchar.h""... $ac_c" 1>&6 -echo "configure:5722: checking for wchar.h" >&5 +echo "configure:5684: checking for wchar.h" >&5 if eval "test \"`echo '$''{'ac_cv_header_$ac_safe'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < EOF ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out" -{ (eval echo configure:5732: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; } +{ (eval echo configure:5694: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; } ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"` if test -z "$ac_err"; then rm -rf conftest* @@ -5758,12 +5720,12 @@ fi # check for usable wchar_t usable_wchar_t="unkown" echo $ac_n "checking for usable wchar_t""... $ac_c" 1>&6 -echo "configure:5762: checking for usable wchar_t" >&5 +echo "configure:5724: checking for usable wchar_t" >&5 if test "$cross_compiling" = yes; then { echo "configure: error: can not run test program while cross compiling" 1>&2; exit 1; } else cat > conftest.$ac_ext <&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null +if { (eval echo configure:5743: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null then cat >> confdefs.h <<\EOF #define HAVE_USABLE_WCHAR_T 1 @@ -5796,14 +5758,14 @@ echo "$ac_t""$usable_wchar_t" 1>&6 # check for endianness echo $ac_n "checking whether byte ordering is bigendian""... $ac_c" 1>&6 -echo "configure:5800: checking whether byte ordering is bigendian" >&5 +echo "configure:5762: checking whether byte ordering is bigendian" >&5 if eval "test \"`echo '$''{'ac_cv_c_bigendian'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else ac_cv_c_bigendian=unknown # See if sys/param.h defines the BYTE_ORDER macro. cat > conftest.$ac_ext < #include @@ -5814,11 +5776,11 @@ int main() { #endif ; return 0; } EOF -if { (eval echo configure:5818: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:5780: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* # It does; now see whether it defined to BIG_ENDIAN or not. cat > conftest.$ac_ext < #include @@ -5829,7 +5791,7 @@ int main() { #endif ; return 0; } EOF -if { (eval echo configure:5833: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:5795: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* ac_cv_c_bigendian=yes else @@ -5849,7 +5811,7 @@ if test "$cross_compiling" = yes; then { echo "configure: error: can not run test program while cross compiling" 1>&2; exit 1; } else cat > conftest.$ac_ext <&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null +if { (eval echo configure:5828: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null then ac_cv_c_bigendian=no else @@ -5889,7 +5851,7 @@ fi # Check whether right shifting a negative integer extends the sign bit # or fills with zeros (like the Cray J90, according to Tim Peters). echo $ac_n "checking whether right shift extends the sign bit""... $ac_c" 1>&6 -echo "configure:5893: checking whether right shift extends the sign bit" >&5 +echo "configure:5855: checking whether right shift extends the sign bit" >&5 if eval "test \"`echo '$''{'ac_cv_rshift_extends_sign'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else @@ -5898,7 +5860,7 @@ if test "$cross_compiling" = yes; then { echo "configure: error: can not run test program while cross compiling" 1>&2; exit 1; } else cat > conftest.$ac_ext <&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null +if { (eval echo configure:5873: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext} && (./conftest; exit) 2>/dev/null then ac_cv_rshift_extends_sign=yes else @@ -5939,12 +5901,12 @@ cat >> confdefs.h <<\EOF #endif EOF echo $ac_n "checking for socklen_t""... $ac_c" 1>&6 -echo "configure:5943: checking for socklen_t" >&5 +echo "configure:5905: checking for socklen_t" >&5 if eval "test \"`echo '$''{'ac_cv_type_socklen_t'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #if STDC_HEADERS @@ -5973,7 +5935,7 @@ fi echo $ac_n "checking for Modules/Setup""... $ac_c" 1>&6 -echo "configure:5977: checking for Modules/Setup" >&5 +echo "configure:5939: checking for Modules/Setup" >&5 if test ! -f Modules/Setup ; then if test ! -d Modules ; then mkdir Modules diff --git a/configure.in b/configure.in index 15e9a3a1159..f2017480bbd 100644 --- a/configure.in +++ b/configure.in @@ -1204,22 +1204,6 @@ LIBS="$LIBS $LIBM" AC_REPLACE_FUNCS(hypot) LIBS=$LIBS_SAVE -# check for getopt -AC_MSG_CHECKING(for genuine getopt) -AC_CACHE_VAL(ac_cv_func_getopt, -[AC_TRY_RUN([#include -extern int optind, opterr, getopt(); -extern char* optarg; -int main() { - char* av[] = { "testprog", "parameter", "-fFlag", NULL }; - opterr = 0; - if (getopt(3, av, "f:") == 'f') { exit(1); } - exit(0); -}], ac_cv_func_getopt=yes, ac_cv_func_getopt=no, ac_cv_func_getopt=no)])dnl -AC_MSG_RESULT($ac_cv_func_getopt) -test $ac_cv_func_getopt = no && LIBOBJS="$LIBOBJS getopt.o" -AC_SUBST(LIBOBJS)dnl - # check whether malloc(0) returns NULL or not AC_MSG_CHECKING(what malloc(0) returns) AC_CACHE_VAL(ac_cv_malloc_zero,