blob: 5cc1d960f2cebe2e5ac34851b1c68be49cb40380 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* System module */
2
3/*
4Various bits of information used by the interpreter are collected in
5module 'sys'.
Guido van Rossum3f5da241990-12-20 15:06:42 +00006Function member:
7- exit(sts): call (C, POSIX) exit(sts)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008Data members:
9- stdin, stdout, stderr: standard file objects
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010- modules: the table of modules (dictionary)
Guido van Rossum3f5da241990-12-20 15:06:42 +000011- path: module search path (list of strings)
12- argv: script arguments (list of strings)
13- ps1, ps2: optional primary and secondary prompts (strings)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014*/
15
Guido van Rossum3f5da241990-12-20 15:06:42 +000016#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018#include "sysmodule.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000019#include "import.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020#include "modsupport.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000021
22/* Define delimiter used in $PYTHONPATH */
23
24#ifdef THINK_C
25#define DELIM ' '
26#endif
27
28#ifndef DELIM
29#define DELIM ':'
30#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031
32static object *sysdict;
33
34object *
35sysget(name)
36 char *name;
37{
38 return dictlookup(sysdict, name);
39}
40
41FILE *
42sysgetfile(name, def)
43 char *name;
44 FILE *def;
45{
46 FILE *fp = NULL;
47 object *v = sysget(name);
48 if (v != NULL)
49 fp = getfilefile(v);
50 if (fp == NULL)
51 fp = def;
52 return fp;
53}
54
55int
56sysset(name, v)
57 char *name;
58 object *v;
59{
60 if (v == NULL)
61 return dictremove(sysdict, name);
62 else
63 return dictinsert(sysdict, name, v);
64}
65
66static object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067sys_exit(self, args)
68 object *self;
69 object *args;
70{
71 int sts;
72 if (!getintarg(args, &sts))
73 return NULL;
74 goaway(sts);
75 exit(sts); /* Just in case */
76 /* NOTREACHED */
77}
78
Guido van Rossum3f5da241990-12-20 15:06:42 +000079static struct methodlist sys_methods[] = {
80 {"exit", sys_exit},
81 {NULL, NULL} /* sentinel */
82};
83
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084static object *sysin, *sysout, *syserr;
85
86void
Guido van Rossum3f5da241990-12-20 15:06:42 +000087initsys()
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088{
Guido van Rossum3f5da241990-12-20 15:06:42 +000089 object *m = initmodule("sys", sys_methods);
90 sysdict = getmoduledict(m);
91 INCREF(sysdict);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092 /* NB keep an extra ref to the std files to avoid closing them
93 when the user deletes them */
Guido van Rossum3f5da241990-12-20 15:06:42 +000094 /* XXX File objects should have a "don't close" flag instead */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000095 sysin = newopenfileobject(stdin, "<stdin>", "r");
96 sysout = newopenfileobject(stdout, "<stdout>", "w");
97 syserr = newopenfileobject(stderr, "<stderr>", "w");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098 if (err_occurred())
Guido van Rossum3f5da241990-12-20 15:06:42 +000099 fatal("can't create sys.std* file objects");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100 dictinsert(sysdict, "stdin", sysin);
101 dictinsert(sysdict, "stdout", sysout);
102 dictinsert(sysdict, "stderr", syserr);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103 dictinsert(sysdict, "modules", get_modules());
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104 if (err_occurred())
105 fatal("can't insert sys.* objects in sys dict");
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000106}
107
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108static object *
109makepathobject(path, delim)
110 char *path;
111 int delim;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000112{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000113 int i, n;
114 char *p;
115 object *v, *w;
116
117 n = 1;
118 p = path;
119 while ((p = strchr(p, delim)) != NULL) {
120 n++;
121 p++;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000122 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000123 v = newlistobject(n);
124 if (v == NULL)
125 return NULL;
126 for (i = 0; ; i++) {
127 p = strchr(path, delim);
128 if (p == NULL)
129 p = strchr(path, '\0'); /* End of string */
130 w = newsizedstringobject(path, (int) (p - path));
131 if (w == NULL) {
132 DECREF(v);
133 return NULL;
134 }
135 setlistitem(v, i, w);
136 if (*p == '\0')
137 break;
138 path = p+1;
139 }
140 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141}
142
143void
Guido van Rossum3f5da241990-12-20 15:06:42 +0000144setpythonpath(path)
145 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000147 object *v;
148 if ((v = makepathobject(path, DELIM)) == NULL)
149 fatal("can't create sys.path");
150 if (sysset("path", v) != 0)
151 fatal("can't assign sys.path");
152 DECREF(v);
153}
154
155static object *
156makeargvobject(argc, argv)
157 int argc;
158 char **argv;
159{
160 object *av;
161 if (argc < 0 || argv == NULL)
162 argc = 0;
163 av = newlistobject(argc);
164 if (av != NULL) {
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000165 int i;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000166 for (i = 0; i < argc; i++) {
167 object *v = newstringobject(argv[i]);
168 if (v == NULL) {
169 DECREF(av);
170 av = NULL;
171 break;
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000172 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000173 setlistitem(av, i, v);
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000174 }
Guido van Rossum5b3138b1990-11-18 17:41:40 +0000175 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000176 return av;
177}
178
179void
180setpythonargv(argc, argv)
181 int argc;
182 char **argv;
183{
184 object *av = makeargvobject(argc, argv);
185 if (av == NULL)
186 fatal("no mem for sys.argv");
187 if (sysset("argv", av) != 0)
188 fatal("can't assign sys.argv");
189 DECREF(av);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190}