blob: 0f76bfd2d927196e32d9e6307f327da718ec1c32 [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 Rossum3f5da241990-12-20 15:06:42 +000029extern char *getpythonpath();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030
Guido van Rossum1984f1e1992-08-04 12:41:02 +000031extern int debugging; /* Needed by parser.c */
32extern int verbose; /* Needed by import.c */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033
Guido van Rossum689e7011991-06-07 13:59:53 +000034/* Interface to getopt(): */
35extern int optind;
36extern char *optarg;
37extern int getopt PROTO((int, char **, char *));
38
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039main(argc, argv)
40 int argc;
41 char **argv;
42{
Guido van Rossum689e7011991-06-07 13:59:53 +000043 int c;
44 int sts;
45 char *command = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046 char *filename = NULL;
47 FILE *fp = stdin;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048
Guido van Rossum689e7011991-06-07 13:59:53 +000049 initargs(&argc, &argv); /* Defined in config*.c */
50
Guido van Rossume3d70451992-03-27 17:21:30 +000051 while ((c = getopt(argc, argv, "c:dv")) != EOF) {
Guido van Rossum46b16381992-01-02 16:16:18 +000052 if (c == 'c') {
53 /* -c is the last option; following arguments
54 that look like options are left for the
55 the command to interpret. */
56 command = malloc(strlen(optarg) + 2);
57 /* Ignore malloc errors this early... */
58 strcpy(command, optarg);
59 strcat(command, "\n");
60 break;
61 }
62
Guido van Rossum689e7011991-06-07 13:59:53 +000063 switch (c) {
64
Guido van Rossum8401e561992-01-19 16:48:36 +000065 case 'd':
66 debugging++;
67 break;
68
Guido van Rossume3d70451992-03-27 17:21:30 +000069 case 'v':
70 verbose++;
71 break;
72
Guido van Rossum46b16381992-01-02 16:16:18 +000073 /* This space reserved for other options */
74
Guido van Rossum689e7011991-06-07 13:59:53 +000075 default:
76 fprintf(stderr,
77 "usage: %s [-c cmd | file | -] [arg] ...\n",
78 argv[0]);
79 exit(2);
80 /*NOTREACHED*/
81
Guido van Rossum689e7011991-06-07 13:59:53 +000082 }
83 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084
Guido van Rossum689e7011991-06-07 13:59:53 +000085 if (command == NULL && optind < argc && strcmp(argv[optind], "-") != 0)
86 filename = argv[optind];
Guido van Rossum3f5da241990-12-20 15:06:42 +000087
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 if (filename != NULL) {
89 if ((fp = fopen(filename, "r")) == NULL) {
Guido van Rossum689e7011991-06-07 13:59:53 +000090 fprintf(stderr, "%s: can't open file '%s'\n",
91 argv[0], filename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092 exit(2);
93 }
94 }
95
Guido van Rossum138e6bf1992-06-03 17:08:15 +000096 initall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097
Guido van Rossum689e7011991-06-07 13:59:53 +000098 if (command != NULL) {
99 /* Backup optind and force sys.argv[0] = '-c' */
100 optind--;
101 argv[optind] = "-c";
102 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000103
Guido van Rossum689e7011991-06-07 13:59:53 +0000104 setpythonargv(argc-optind, argv+optind);
105
106 if (command) {
107 sts = run_command(command) != 0;
108 }
109 else {
110 sts = run(fp, filename == NULL ? "<stdin>" : filename) != 0;
111 }
112
113 goaway(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114 /*NOTREACHED*/
115}