Skip to content

Commit 2cffc7d

Browse files
committed
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.
1 parent 9dce7b3 commit 2cffc7d

File tree

7 files changed

+73
-130
lines changed

7 files changed

+73
-130
lines changed

Demo/pysvr/pysvr.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ can log in on your machine. Use with caution!
1919
#include <netinet/in.h>
2020

2121
#include <pthread.h>
22+
#include <getopt.h>
2223

2324
/* XXX Umpfh.
2425
Python.h defines a typedef destructor, which conflicts with pthread.h.
@@ -32,10 +33,6 @@ extern int Py_VerboseFlag;
3233
#define PORT 4000
3334
#endif
3435

35-
extern int optind;
36-
extern char *optarg;
37-
extern int getopt(int, char **, char *);
38-
3936
struct workorder {
4037
int conn;
4138
struct sockaddr_in addr;

Include/pygetopt.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
#ifndef Py_PYGETOPT_H
3+
#define Py_PYGETOPT_H
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
extern DL_IMPORT(int) _PyOS_opterr;
9+
extern DL_IMPORT(int) _PyOS_optind;
10+
extern DL_IMPORT(char *) _PyOS_optarg;
11+
12+
DL_IMPORT(int) _PyOS_GetOpt(int argc, char **argv, char *optstring);
13+
14+
#ifdef __cplusplus
15+
}
16+
#endif
17+
#endif /* !Py_PYGETOPT_H */

Modules/main.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@
1717
#define PYTHONHOMEHELP "<prefix>/python2.0"
1818
#endif
1919

20+
#include "pygetopt.h"
21+
2022
#define COPYRIGHT \
2123
"Type \"copyright\", \"credits\" or \"license\" for more information."
2224

23-
/* Interface to getopt(): */
24-
extern int optind;
25-
extern char *optarg;
26-
extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
27-
28-
2925
/* For Py_GetArgcArgv(); set by main() */
3026
static char **orig_argv;
3127
static int orig_argc;
@@ -105,16 +101,16 @@ Py_Main(int argc, char **argv)
105101
if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
106102
unbuffered = 1;
107103

108-
while ((c = getopt(argc, argv, "c:diOStuUvxXhV")) != EOF) {
104+
while ((c = _PyOS_GetOpt(argc, argv, "c:diOStuUvxXhV")) != EOF) {
109105
if (c == 'c') {
110106
/* -c is the last option; following arguments
111107
that look like options are left for the
112108
the command to interpret. */
113-
command = malloc(strlen(optarg) + 2);
109+
command = malloc(strlen(_PyOS_optarg) + 2);
114110
if (command == NULL)
115111
Py_FatalError(
116112
"not enough memory to copy -c argument");
117-
strcpy(command, optarg);
113+
strcpy(command, _PyOS_optarg);
118114
strcat(command, "\n");
119115
break;
120116
}
@@ -181,10 +177,10 @@ Py_Main(int argc, char **argv)
181177
exit(0);
182178
}
183179

184-
if (command == NULL && optind < argc &&
185-
strcmp(argv[optind], "-") != 0)
180+
if (command == NULL && _PyOS_optind < argc &&
181+
strcmp(argv[_PyOS_optind], "-") != 0)
186182
{
187-
filename = argv[optind];
183+
filename = argv[_PyOS_optind];
188184
if (filename != NULL) {
189185
if ((fp = fopen(filename, "r")) == NULL) {
190186
fprintf(stderr, "%s: can't open file '%s'\n",
@@ -253,12 +249,12 @@ Py_Main(int argc, char **argv)
253249

254250

255251
if (command != NULL) {
256-
/* Backup optind and force sys.argv[0] = '-c' */
257-
optind--;
258-
argv[optind] = "-c";
252+
/* Backup _PyOS_optind and force sys.argv[0] = '-c' */
253+
_PyOS_optind--;
254+
argv[_PyOS_optind] = "-c";
259255
}
260256

261-
PySys_SetArgv(argc-optind, argv+optind);
257+
PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
262258

263259
if ((inspect || (command == NULL && filename == NULL)) &&
264260
isatty(fileno(stdin))) {

Python/Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ AROBJS= \
4646
marshal.o modsupport.o mystrtoul.o \
4747
pyfpe.o pystate.o pythonrun.o \
4848
structmember.o sysmodule.o \
49-
traceback.o \
49+
traceback.o getopt.o \
5050
$(DYNLOADFILE) \
5151
$(LIBOBJS)
5252
OBJS= $(AROBJS) sigcheck.o

Python/getopt.c

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,68 +27,55 @@
2727
#include <stdio.h>
2828
#include <string.h>
2929

30-
#define bool int
31-
#ifndef TRUE
32-
#define TRUE 1
33-
#endif
34-
#ifndef FALSE
35-
#define FALSE 0
36-
#endif
30+
int _PyOS_opterr = 1; /* generate error messages */
31+
int _PyOS_optind = 1; /* index into argv array */
32+
char *_PyOS_optarg = NULL; /* optional argument */
3733

38-
bool opterr = TRUE; /* generate error messages */
39-
int optind = 1; /* index into argv array */
40-
char * optarg = NULL; /* optional argument */
41-
42-
43-
#ifndef __BEOS__
44-
int getopt(int argc, char *argv[], char optstring[])
45-
#else
46-
int getopt(int argc, char *const *argv, const char *optstring)
47-
#endif
34+
int _PyOS_GetOpt(int argc, char **argv, char *optstring)
4835
{
49-
static char *opt_ptr = "";
50-
register char *ptr;
51-
int option;
36+
static char *opt_ptr = "";
37+
char *ptr;
38+
int option;
5239

5340
if (*opt_ptr == '\0') {
5441

55-
if (optind >= argc || argv[optind][0] != '-' ||
56-
argv[optind][1] == '\0' /* lone dash */ )
42+
if (_PyOS_optind >= argc || argv[_PyOS_optind][0] != '-' ||
43+
argv[_PyOS_optind][1] == '\0' /* lone dash */ )
5744
return -1;
5845

59-
else if (strcmp(argv[optind], "--") == 0) {
60-
++optind;
46+
else if (strcmp(argv[_PyOS_optind], "--") == 0) {
47+
++_PyOS_optind;
6148
return -1;
6249
}
6350

64-
opt_ptr = &argv[optind++][1];
51+
opt_ptr = &argv[_PyOS_optind++][1];
6552
}
6653

6754
if ( (option = *opt_ptr++) == '\0')
68-
return -1;
55+
return -1;
6956

7057
if ((ptr = strchr(optstring, option)) == NULL) {
71-
if (opterr)
58+
if (_PyOS_opterr)
7259
fprintf(stderr, "Unknown option: -%c\n", option);
7360

7461
return '?';
7562
}
7663

7764
if (*(ptr + 1) == ':') {
7865
if (*opt_ptr != '\0') {
79-
optarg = opt_ptr;
66+
_PyOS_optarg = opt_ptr;
8067
opt_ptr = "";
8168
}
8269

8370
else {
84-
if (optind >= argc) {
85-
if (opterr)
71+
if (_PyOS_optind >= argc) {
72+
if (_PyOS_opterr)
8673
fprintf(stderr,
8774
"Argument expected for the -%c option\n", option);
8875
return '?';
8976
}
9077

91-
optarg = argv[optind++];
78+
_PyOS_optarg = argv[_PyOS_optind++];
9279
}
9380
}
9481

0 commit comments

Comments
 (0)