Skip to content

Commit

Permalink
Move our own getopt() implementation to _PyOS_GetOpt(), and use it
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Yhg1s committed Nov 3, 2000
1 parent 9dce7b3 commit 2cffc7d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 130 deletions.
5 changes: 1 addition & 4 deletions Demo/pysvr/pysvr.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ can log in on your machine. Use with caution!
#include <netinet/in.h>

#include <pthread.h>
#include <getopt.h>

/* XXX Umpfh.
Python.h defines a typedef destructor, which conflicts with pthread.h.
Expand All @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions Include/pygetopt.h
Original file line number Diff line number Diff line change
@@ -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 */
28 changes: 12 additions & 16 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@
#define PYTHONHOMEHELP "<prefix>/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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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))) {
Expand Down
2 changes: 1 addition & 1 deletion Python/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 18 additions & 31 deletions Python/getopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,68 +27,55 @@
#include <stdio.h>
#include <string.h>

#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 '?';
}

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++];
}
}

Expand Down
Loading

0 comments on commit 2cffc7d

Please sign in to comment.