1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Python interpreter main program */
27 #include "allobjects.h"
29 extern int debugging
; /* Needed in parser.c, declared in pythonrun.c */
30 extern int verbose
; /* Needed in import.c, declared in pythonrun.c */
31 extern int suppress_print
; /* Needed in ceval.c, declared in pythonrun.c */
33 /* Interface to getopt(): */
36 extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
38 extern char *getenv();
40 extern char *getversion();
41 extern char *getcopyright();
51 char *filename
= NULL
;
57 if ((p
= getenv("PYTHONDEBUG")) && *p
!= '\0')
59 if ((p
= getenv("PYTHONSUPPRESS")) && *p
!= '\0')
61 if ((p
= getenv("PYTHONVERBOSE")) && *p
!= '\0')
63 if ((p
= getenv("PYTHONINSPECT")) && *p
!= '\0')
65 if ((p
= getenv("PYTHONUNBUFFERED")) && *p
!= '\0')
69 PyMac_InteractiveOptions(&inspect
, &verbose
, &suppress_print
, &unbuffered
, &debugging
);
72 while ((c
= getopt(argc
, argv
, "c:disuv")) != EOF
) {
74 /* -c is the last option; following arguments
75 that look like options are left for the
76 the command to interpret. */
77 command
= malloc(strlen(optarg
) + 2);
79 fatal("not enough memory to copy -c argument");
80 strcpy(command
, optarg
);
81 strcat(command
, "\n");
107 /* This space reserved for other options */
111 "usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n",
113 #if !(defined(__CFM68K__) && defined(__MWERKS__))
114 /* Mwerks cfm68k linker doesn't like these... */
117 Options and arguments (and corresponding environment variables):\n\
118 -d : debug output from parser (also PYTHONDEBUG=x)\n\
119 -i : inspect interactively after running script (also PYTHONINSPECT=x)\n\
120 -s : suppress the printing of top level expressions (also PYTHONSUPPRESS=x)\n\
121 -u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
122 -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
123 -c cmd : program passed in as string (terminates option list)\n\
125 /* ANSI does not allow strings > 512 chars
126 and MPW doesn't like it either -- so split it! */
128 file : program read from script file\n\
129 - : program read from stdin (default; interactive mode if a tty)\n\
130 arg ...: arguments passed to program in sys.argv[1:]\n\
132 Other environment variables:\n\
133 PYTHONSTARTUP: file executed on interactive startup (no default)\n\
134 PYTHONPATH : colon-separated list of directories prefixed to the\n\
135 default module search path. The result is sys.path.\n\
137 #endif /* !cfm68k || !mwerks */
146 setbuf(stdout
, (char *)NULL
);
147 setbuf(stderr
, (char *)NULL
);
149 /* On MPW (3.2) unbuffered seems to hang */
150 setvbuf(stdout
, (char *)NULL
, _IOLBF
, BUFSIZ
);
151 setvbuf(stderr
, (char *)NULL
, _IOLBF
, BUFSIZ
);
155 if (command
== NULL
&& optind
< argc
&& strcmp(argv
[optind
], "-") != 0)
156 filename
= argv
[optind
];
159 command
== NULL
&& filename
== NULL
&& isatty((int)fileno(fp
)))
160 fprintf(stderr
, "Python %s\n%s\n",
161 getversion(), getcopyright());
163 if (filename
!= NULL
) {
164 if ((fp
= fopen(filename
, "r")) == NULL
) {
165 fprintf(stderr
, "%s: can't open file '%s'\n",
173 if (command
!= NULL
) {
174 /* Backup optind and force sys.argv[0] = '-c' */
179 setpythonargv(argc
-optind
, argv
+optind
);
182 sts
= run_command(command
) != 0;
185 if (filename
== NULL
&& isatty((int)fileno(fp
))) {
186 char *startup
= getenv("PYTHONSTARTUP");
189 startup
= "PythonStartup";
191 if (startup
!= NULL
&& startup
[0] != '\0') {
192 FILE *fp
= fopen(startup
, "r");
194 (void) run_script(fp
, startup
);
200 sts
= run(fp
, filename
== NULL
? "<stdin>" : filename
) != 0;
201 if (filename
!= NULL
)
205 if (inspect
&& isatty((int)fileno(stdin
)) &&
206 (filename
!= NULL
|| command
!= NULL
))
207 sts
= run(stdin
, "<stdin>") != 0;