Bump version to 0.9.1.
[python/dscho.git] / Python / pythonmain.c
blob000a3d789f8ce458a88f8853a595b11a6c7c206a
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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(): */
34 extern int optind;
35 extern char *optarg;
36 extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
38 extern char *getenv();
40 extern char *getversion();
41 extern char *getcopyright();
43 int
44 realmain(argc, argv)
45 int argc;
46 char **argv;
48 int c;
49 int sts;
50 char *command = NULL;
51 char *filename = NULL;
52 FILE *fp = stdin;
53 char *p;
54 int inspect = 0;
55 int unbuffered = 0;
57 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
58 debugging = 1;
59 if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
60 suppress_print = 1;
61 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
62 verbose = 1;
63 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
64 inspect = 1;
65 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
66 unbuffered = 1;
68 #ifdef macintosh
69 PyMac_InteractiveOptions(&inspect, &verbose, &suppress_print, &unbuffered, &debugging);
70 #endif
72 while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
73 if (c == 'c') {
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);
78 if (command == NULL)
79 fatal("not enough memory to copy -c argument");
80 strcpy(command, optarg);
81 strcat(command, "\n");
82 break;
85 switch (c) {
87 case 'd':
88 debugging++;
89 break;
91 case 'i':
92 inspect++;
93 break;
95 case 's':
96 suppress_print++;
97 break;
99 case 'u':
100 unbuffered++;
101 break;
103 case 'v':
104 verbose++;
105 break;
107 /* This space reserved for other options */
109 default:
110 fprintf(stderr,
111 "usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n",
112 argv[0]);
113 #if !(defined(__CFM68K__) && defined(__MWERKS__))
114 /* Mwerks cfm68k linker doesn't like these... */
115 fprintf(stderr, "\
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! */
127 fprintf(stderr, "\
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 */
138 exit(2);
139 /*NOTREACHED*/
144 if (unbuffered) {
145 #ifndef MPW
146 setbuf(stdout, (char *)NULL);
147 setbuf(stderr, (char *)NULL);
148 #else
149 /* On MPW (3.2) unbuffered seems to hang */
150 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
151 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
152 #endif
155 if (command == NULL && optind < argc && strcmp(argv[optind], "-") != 0)
156 filename = argv[optind];
158 if (verbose ||
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",
166 argv[0], filename);
167 exit(2);
171 initall();
173 if (command != NULL) {
174 /* Backup optind and force sys.argv[0] = '-c' */
175 optind--;
176 argv[optind] = "-c";
179 setpythonargv(argc-optind, argv+optind);
181 if (command) {
182 sts = run_command(command) != 0;
184 else {
185 if (filename == NULL && isatty((int)fileno(fp))) {
186 char *startup = getenv("PYTHONSTARTUP");
187 #ifdef macintosh
188 if (startup == NULL)
189 startup = "PythonStartup";
190 #endif
191 if (startup != NULL && startup[0] != '\0') {
192 FILE *fp = fopen(startup, "r");
193 if (fp != NULL) {
194 (void) run_script(fp, startup);
195 err_clear();
196 fclose(fp);
200 sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0;
201 if (filename != NULL)
202 fclose(fp);
205 if (inspect && isatty((int)fileno(stdin)) &&
206 (filename != NULL || command != NULL))
207 sts = run(stdin, "<stdin>") != 0;
209 goaway(sts);
210 /*NOTREACHED*/