comparison Python/pythonmain.c @ 1848:b724599401ff legacy-trunk

Merge back to main trunk
author Guido van Rossum <guido@python.org>
date Tue, 30 Aug 1994 08:27:36 +0000
parents c914b10bf2ed
children 35947f60528e
comparison
equal deleted inserted replaced
1847:0d318475f9e3 1848:b724599401ff
1 /*********************************************************** 1 /***********************************************************
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, 2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3 Amsterdam, The Netherlands. 3 Amsterdam, The Netherlands.
4 4
5 All Rights Reserved 5 All Rights Reserved
6 6
7 Permission to use, copy, modify, and distribute this software and its 7 Permission to use, copy, modify, and distribute this software and its
24 24
25 /* Python interpreter main program */ 25 /* Python interpreter main program */
26 26
27 #include "allobjects.h" 27 #include "allobjects.h"
28 28
29 extern int debugging; /* Defined in parser.c */ 29 extern int debugging; /* Needed in parser.c, declared in pythonrun.c */
30 extern int verbose; /* Defined in import.c */ 30 extern int verbose; /* Needed in import.c, declared in pythonrun.c */
31 extern int killprint; /* Defined in ceval.c */ 31 extern int suppress_print; /* Needed in ceval.c, declared in pythonrun.c */
32 32
33 /* Interface to getopt(): */ 33 /* Interface to getopt(): */
34 extern int optind; 34 extern int optind;
35 extern char *optarg; 35 extern char *optarg;
36 extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */ 36 extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
37 37
38 extern char *getenv(); 38 extern char *getenv();
39 39
40 main(argc, argv) 40 extern char *getversion();
41 extern char *getcopyright();
42
43 int
44 realmain(argc, argv)
41 int argc; 45 int argc;
42 char **argv; 46 char **argv;
43 { 47 {
44 int c; 48 int c;
45 int sts; 49 int sts;
46 char *command = NULL; 50 char *command = NULL;
47 char *filename = NULL; 51 char *filename = NULL;
48 FILE *fp = stdin; 52 FILE *fp = stdin;
49 char *p; 53 char *p;
50 int inspect = 0; 54 int inspect = 0;
55 int unbuffered = 0;
51 56
52 if ((p = getenv("PYTHONDEBUG")) && *p != '\0') 57 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
53 debugging = 1; 58 debugging = 1;
59 if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
60 suppress_print = 1;
54 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0') 61 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
55 verbose = 1; 62 verbose = 1;
56 if ((p = getenv("PYTHONINSPECT")) && *p != '\0') 63 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
57 inspect = 1; 64 inspect = 1;
58 if ((p = getenv("PYTHONKILLPRINT")) && *p != '\0') 65 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
59 killprint = 1; 66 unbuffered = 1;
60 67
61 initargs(&argc, &argv); 68 while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
62
63 while ((c = getopt(argc, argv, "c:dikv")) != EOF) {
64 if (c == 'c') { 69 if (c == 'c') {
65 /* -c is the last option; following arguments 70 /* -c is the last option; following arguments
66 that look like options are left for the 71 that look like options are left for the
67 the command to interpret. */ 72 the command to interpret. */
68 command = malloc(strlen(optarg) + 2); 73 command = malloc(strlen(optarg) + 2);
80 85
81 case 'i': 86 case 'i':
82 inspect++; 87 inspect++;
83 break; 88 break;
84 89
85 case 'k': 90 case 's':
86 killprint++; 91 suppress_print++;
92 break;
93
94 case 'u':
95 unbuffered++;
87 break; 96 break;
88 97
89 case 'v': 98 case 'v':
90 verbose++; 99 verbose++;
91 break; 100 break;
92 101
93 /* This space reserved for other options */ 102 /* This space reserved for other options */
94 103
95 default: 104 default:
96 fprintf(stderr, 105 fprintf(stderr,
97 "usage: %s [-d] [-i] [-k] [-v] [-c cmd | file | -] [arg] ...\n", 106 "usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n",
98 argv[0]); 107 argv[0]);
99 fprintf(stderr, "\ 108 fprintf(stderr, "\
100 \n\ 109 \n\
101 Options and arguments (and corresponding environment variables):\n\ 110 Options and arguments (and corresponding environment variables):\n\
102 -d : debug output from parser (also PYTHONDEBUG=x)\n\ 111 -d : debug output from parser (also PYTHONDEBUG=x)\n\
103 -i : inspect interactively after running script (also PYTHONINSPECT=x)\n\ 112 -i : inspect interactively after running script (also PYTHONINSPECT=x)\n\
104 -k : kill printing expression statement (also PYTHONKILLPRINT=x)\n\ 113 -s : suppress the printing of top level expressions (also PYTHONSUPPRESS=x)\n\
114 -u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
105 -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\ 115 -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
106 -c cmd : program passed in as string (terminates option list)\n\ 116 -c cmd : program passed in as string (terminates option list)\n\
117 ");
118 /* ANSI does not allow strings > 512 chars
119 and MPW doesn't like it either -- so split it! */
120 fprintf(stderr, "\
107 file : program read from script file\n\ 121 file : program read from script file\n\
108 - : program read from stdin (default; interactive mode if a tty)\n\ 122 - : program read from stdin (default; interactive mode if a tty)\n\
109 arg ...: arguments passed to program in sys.argv[1:]\n\ 123 arg ...: arguments passed to program in sys.argv[1:]\n\
110 \n\ 124 \n\
111 Other environment variables:\n\ 125 Other environment variables:\n\
116 exit(2); 130 exit(2);
117 /*NOTREACHED*/ 131 /*NOTREACHED*/
118 132
119 } 133 }
120 } 134 }
121 135
136 if (unbuffered) {
137 #ifndef MPW
138 setbuf(stdout, (char *)NULL);
139 setbuf(stderr, (char *)NULL);
140 #else
141 /* On MPW (3.2) unbuffered seems to hang */
142 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
143 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
144 #endif
145 }
146
122 if (command == NULL && optind < argc && strcmp(argv[optind], "-") != 0) 147 if (command == NULL && optind < argc && strcmp(argv[optind], "-") != 0)
123 filename = argv[optind]; 148 filename = argv[optind];
149
150 if (verbose ||
151 command == NULL && filename == NULL && isatty((int)fileno(fp)))
152 fprintf(stderr, "Python %s\n%s\n",
153 getversion(), getcopyright());
124 154
125 if (filename != NULL) { 155 if (filename != NULL) {
126 if ((fp = fopen(filename, "r")) == NULL) { 156 if ((fp = fopen(filename, "r")) == NULL) {
127 fprintf(stderr, "%s: can't open file '%s'\n", 157 fprintf(stderr, "%s: can't open file '%s'\n",
128 argv[0], filename); 158 argv[0], filename);
144 sts = run_command(command) != 0; 174 sts = run_command(command) != 0;
145 } 175 }
146 else { 176 else {
147 if (filename == NULL && isatty((int)fileno(fp))) { 177 if (filename == NULL && isatty((int)fileno(fp))) {
148 char *startup = getenv("PYTHONSTARTUP"); 178 char *startup = getenv("PYTHONSTARTUP");
179 #ifdef macintosh
180 if (startup == NULL)
181 startup = "PythonStartup";
182 #endif
149 if (startup != NULL && startup[0] != '\0') { 183 if (startup != NULL && startup[0] != '\0') {
150 FILE *fp = fopen(startup, "r"); 184 FILE *fp = fopen(startup, "r");
151 if (fp != NULL) { 185 if (fp != NULL) {
152 (void) run_script(fp, startup); 186 (void) run_script(fp, startup);
153 err_clear(); 187 err_clear();
188 fclose(fp);
154 } 189 }
155 } 190 }
156 } 191 }
157 sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0; 192 sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0;
193 if (filename != NULL)
194 fclose(fp);
158 } 195 }
159 196
160 if (inspect && isatty((int)fileno(stdin)) && 197 if (inspect && isatty((int)fileno(stdin)) &&
161 (filename != NULL || command != NULL)) 198 (filename != NULL || command != NULL))
162 sts = run(stdin, "<stdin>") != 0; 199 sts = run(stdin, "<stdin>") != 0;