blob: 1c6469231d01e5fa85a3df547af5f7dfef902b75 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumbab9d031992-04-05 14:26:55 +00002Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Guido van Rossumf70e43a1991-02-19 12:39:46 +00003Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Python interpreter main program */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Guido van Rossum1984f1e1992-08-04 12:41:02 +000029extern int debugging; /* Needed by parser.c */
30extern int verbose; /* Needed by import.c */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031
Guido van Rossum689e7011991-06-07 13:59:53 +000032/* Interface to getopt(): */
33extern int optind;
34extern char *optarg;
35extern int getopt PROTO((int, char **, char *));
36
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037main(argc, argv)
38 int argc;
39 char **argv;
40{
Guido van Rossum689e7011991-06-07 13:59:53 +000041 int c;
42 int sts;
43 char *command = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044 char *filename = NULL;
45 FILE *fp = stdin;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046
Guido van Rossum689e7011991-06-07 13:59:53 +000047 initargs(&argc, &argv); /* Defined in config*.c */
48
Guido van Rossume3d70451992-03-27 17:21:30 +000049 while ((c = getopt(argc, argv, "c:dv")) != EOF) {
Guido van Rossum46b16381992-01-02 16:16:18 +000050 if (c == 'c') {
51 /* -c is the last option; following arguments
52 that look like options are left for the
53 the command to interpret. */
54 command = malloc(strlen(optarg) + 2);
55 /* Ignore malloc errors this early... */
56 strcpy(command, optarg);
57 strcat(command, "\n");
58 break;
59 }
60
Guido van Rossum689e7011991-06-07 13:59:53 +000061 switch (c) {
62
Guido van Rossum8401e561992-01-19 16:48:36 +000063 case 'd':
64 debugging++;
65 break;
66
Guido van Rossume3d70451992-03-27 17:21:30 +000067 case 'v':
68 verbose++;
69 break;
70
Guido van Rossum46b16381992-01-02 16:16:18 +000071 /* This space reserved for other options */
72
Guido van Rossum689e7011991-06-07 13:59:53 +000073 default:
74 fprintf(stderr,
75 "usage: %s [-c cmd | file | -] [arg] ...\n",
76 argv[0]);
77 exit(2);
78 /*NOTREACHED*/
79
Guido van Rossum689e7011991-06-07 13:59:53 +000080 }
81 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082
Guido van Rossum689e7011991-06-07 13:59:53 +000083 if (command == NULL && optind < argc && strcmp(argv[optind], "-") != 0)
84 filename = argv[optind];
Guido van Rossum3f5da241990-12-20 15:06:42 +000085
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086 if (filename != NULL) {
87 if ((fp = fopen(filename, "r")) == NULL) {
Guido van Rossum689e7011991-06-07 13:59:53 +000088 fprintf(stderr, "%s: can't open file '%s'\n",
89 argv[0], filename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000090 exit(2);
91 }
92 }
93
Guido van Rossum138e6bf1992-06-03 17:08:15 +000094 initall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095
Guido van Rossum689e7011991-06-07 13:59:53 +000096 if (command != NULL) {
97 /* Backup optind and force sys.argv[0] = '-c' */
98 optind--;
99 argv[optind] = "-c";
100 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000101
Guido van Rossum689e7011991-06-07 13:59:53 +0000102 setpythonargv(argc-optind, argv+optind);
103
104 if (command) {
105 sts = run_command(command) != 0;
106 }
107 else {
Guido van Rossumda8cd861992-09-03 20:26:57 +0000108 if (filename == NULL && isatty((int)fileno(fp))) {
109 char *startup = getenv("PYTHONSTARTUP");
110 if (startup != NULL && startup[0] != '\0') {
111 FILE *fp = fopen(startup, "r");
112 if (fp != NULL) {
113 (void) run_script(fp, startup);
114 err_clear();
115 }
116 }
117 }
Guido van Rossum689e7011991-06-07 13:59:53 +0000118 sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0;
119 }
120
121 goaway(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122 /*NOTREACHED*/
123}