blob: d725c88cc8b0debf8e0404cc022bc11e3ba3c5e4 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
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/* System module */
26
27/*
28Various bits of information used by the interpreter are collected in
29module 'sys'.
Guido van Rossum3f5da241990-12-20 15:06:42 +000030Function member:
31- exit(sts): call (C, POSIX) exit(sts)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032Data members:
33- stdin, stdout, stderr: standard file objects
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034- modules: the table of modules (dictionary)
Guido van Rossum3f5da241990-12-20 15:06:42 +000035- path: module search path (list of strings)
36- argv: script arguments (list of strings)
37- ps1, ps2: optional primary and secondary prompts (strings)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038*/
39
Guido van Rossum3f5da241990-12-20 15:06:42 +000040#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042#include "sysmodule.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043#include "import.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044#include "modsupport.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000045
46/* Define delimiter used in $PYTHONPATH */
47
Guido van Rossumd6a15ad1991-06-24 22:30:42 +000048#ifdef macintosh
Guido van Rossum3f5da241990-12-20 15:06:42 +000049#define DELIM ' '
50#endif
51
Guido van Rossumeb4361a1991-05-05 20:14:35 +000052#ifdef MSDOS
53#define DELIM ';'
54#endif
55
Guido van Rossum3f5da241990-12-20 15:06:42 +000056#ifndef DELIM
57#define DELIM ':'
58#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059
60static object *sysdict;
61
62object *
63sysget(name)
64 char *name;
65{
66 return dictlookup(sysdict, name);
67}
68
69FILE *
70sysgetfile(name, def)
71 char *name;
72 FILE *def;
73{
74 FILE *fp = NULL;
75 object *v = sysget(name);
76 if (v != NULL)
77 fp = getfilefile(v);
78 if (fp == NULL)
79 fp = def;
80 return fp;
81}
82
83int
84sysset(name, v)
85 char *name;
86 object *v;
87{
88 if (v == NULL)
89 return dictremove(sysdict, name);
90 else
91 return dictinsert(sysdict, name, v);
92}
93
94static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095sys_exit(self, args)
96 object *self;
97 object *args;
98{
Guido van Rossum6a468bf1991-12-31 13:15:35 +000099 /* Raise SystemExit so callers may catch it or clean up. */
100 err_setval(SystemExit, args);
101 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102}
103
Guido van Rossum3f5da241990-12-20 15:06:42 +0000104static struct methodlist sys_methods[] = {
105 {"exit", sys_exit},
106 {NULL, NULL} /* sentinel */
107};
108
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109static object *sysin, *sysout, *syserr;
110
111void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000112initsys()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113{
Guido van Rossum3caa6e31991-06-04 19:40:59 +0000114 extern int fclose PROTO((FILE *));
Guido van Rossum71776571991-12-10 14:00:21 +0000115 extern char version[];
116 object *v = newstringobject(version);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117 object *m = initmodule("sys", sys_methods);
118 sysdict = getmoduledict(m);
119 INCREF(sysdict);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120 /* NB keep an extra ref to the std files to avoid closing them
121 when the user deletes them */
Guido van Rossum3caa6e31991-06-04 19:40:59 +0000122 sysin = newopenfileobject(stdin, "<stdin>", "r", fclose);
123 sysout = newopenfileobject(stdout, "<stdout>", "w", fclose);
124 syserr = newopenfileobject(stderr, "<stderr>", "w", fclose);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125 if (err_occurred())
Guido van Rossum71776571991-12-10 14:00:21 +0000126 fatal("can't create sys.* objects");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127 dictinsert(sysdict, "stdin", sysin);
128 dictinsert(sysdict, "stdout", sysout);
129 dictinsert(sysdict, "stderr", syserr);
Guido van Rossum71776571991-12-10 14:00:21 +0000130 dictinsert(sysdict, "version", v);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000131 dictinsert(sysdict, "modules", get_modules());
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132 if (err_occurred())
133 fatal("can't insert sys.* objects in sys dict");
Guido van Rossum71776571991-12-10 14:00:21 +0000134 DECREF(v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000135}
136
Guido van Rossum3f5da241990-12-20 15:06:42 +0000137static object *
138makepathobject(path, delim)
139 char *path;
140 int delim;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000141{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000142 int i, n;
143 char *p;
144 object *v, *w;
145
146 n = 1;
147 p = path;
148 while ((p = strchr(p, delim)) != NULL) {
149 n++;
150 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000151 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000152 v = newlistobject(n);
153 if (v == NULL)
154 return NULL;
155 for (i = 0; ; i++) {
156 p = strchr(path, delim);
157 if (p == NULL)
158 p = strchr(path, '\0'); /* End of string */
159 w = newsizedstringobject(path, (int) (p - path));
160 if (w == NULL) {
161 DECREF(v);
162 return NULL;
163 }
164 setlistitem(v, i, w);
165 if (*p == '\0')
166 break;
167 path = p+1;
168 }
169 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
172void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000173setpythonpath(path)
174 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000176 object *v;
177 if ((v = makepathobject(path, DELIM)) == NULL)
178 fatal("can't create sys.path");
179 if (sysset("path", v) != 0)
180 fatal("can't assign sys.path");
181 DECREF(v);
182}
183
184static object *
185makeargvobject(argc, argv)
186 int argc;
187 char **argv;
188{
189 object *av;
Guido van Rossumee3a2991992-01-14 18:42:53 +0000190 if (argc <= 0 || argv == NULL) {
191 /* Ensure at least one (empty) argument is seen */
192 static char *empty_argv[1] = {""};
193 argv = empty_argv;
194 argc = 1;
195 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000196 av = newlistobject(argc);
197 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000198 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000199 for (i = 0; i < argc; i++) {
200 object *v = newstringobject(argv[i]);
201 if (v == NULL) {
202 DECREF(av);
203 av = NULL;
204 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000205 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000206 setlistitem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000207 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000208 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000209 return av;
210}
211
212void
213setpythonargv(argc, argv)
214 int argc;
215 char **argv;
216{
217 object *av = makeargvobject(argc, argv);
218 if (av == NULL)
219 fatal("no mem for sys.argv");
220 if (sysset("argv", av) != 0)
221 fatal("can't assign sys.argv");
222 DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223}