Don't reference removed files in Makefile
[python/dscho.git] / Python / sysmodule.c
blobe526652d8a688567a39f78b9a246011f111c77fa
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* System module */
28 Various bits of information used by the interpreter are collected in
29 module 'sys'.
30 Function member:
31 - exit(sts): raise SystemExit
32 Data members:
33 - stdin, stdout, stderr: standard file objects
34 - modules: the table of modules (dictionary)
35 - path: module search path (list of strings)
36 - argv: script arguments (list of strings)
37 - ps1, ps2: optional primary and secondary prompts (strings)
40 #include "allobjects.h"
42 #include "sysmodule.h"
43 #include "import.h"
44 #include "modsupport.h"
45 #include "osdefs.h"
47 object *sys_trace, *sys_profile;
48 int sys_checkinterval;
50 static object *sysdict;
52 object *
53 sysget(name)
54 char *name;
56 return dictlookup(sysdict, name);
59 FILE *
60 sysgetfile(name, def)
61 char *name;
62 FILE *def;
64 FILE *fp = NULL;
65 object *v = sysget(name);
66 if (v != NULL && is_fileobject(v))
67 fp = getfilefile(v);
68 if (fp == NULL)
69 fp = def;
70 return fp;
73 int
74 sysset(name, v)
75 char *name;
76 object *v;
78 if (v == NULL) {
79 if (dictlookup(sysdict, name) == NULL)
80 return 0;
81 else
82 return dictremove(sysdict, name);
84 else
85 return dictinsert(sysdict, name, v);
88 static object *
89 sys_exit(self, args)
90 object *self;
91 object *args;
93 /* Raise SystemExit so callers may catch it or clean up. */
94 err_setval(SystemExit, args);
95 return NULL;
98 static object *
99 sys_settrace(self, args)
100 object *self;
101 object *args;
103 if (args == None)
104 args = NULL;
105 else
106 XINCREF(args);
107 XDECREF(sys_trace);
108 sys_trace = args;
109 INCREF(None);
110 return None;
113 static object *
114 sys_setprofile(self, args)
115 object *self;
116 object *args;
118 if (args == None)
119 args = NULL;
120 else
121 XINCREF(args);
122 XDECREF(sys_profile);
123 sys_profile = args;
124 INCREF(None);
125 return None;
128 static object *
129 sys_setcheckinterval(self, args)
130 object *self;
131 object *args;
133 if (!newgetargs(args, "i", &sys_checkinterval))
134 return NULL;
135 INCREF(None);
136 return None;
139 #ifdef USE_MALLOPT
140 /* Link with -lmalloc (or -lmpc) on an SGI */
141 #include <malloc.h>
143 static object *
144 sys_mdebug(self, args)
145 object *self;
146 object *args;
148 int flag;
149 if (!getargs(args, "i", &flag))
150 return NULL;
151 mallopt(M_DEBUG, flag);
152 INCREF(None);
153 return None;
155 #endif /* USE_MALLOPT */
157 static struct methodlist sys_methods[] = {
158 {"exit", sys_exit, 0},
159 #ifdef USE_MALLOPT
160 {"mdebug", sys_mdebug, 0},
161 #endif
162 {"setprofile", sys_setprofile, 0},
163 {"settrace", sys_settrace, 0},
164 {"setcheckinterval", sys_setcheckinterval, 0},
165 {NULL, NULL} /* sentinel */
168 static object *sysin, *sysout, *syserr;
170 static object *
171 list_builtin_module_names()
173 object *list = newlistobject(0);
174 int i;
175 if (list == NULL)
176 return NULL;
177 for (i = 0; inittab[i].name != NULL; i++) {
178 object *name = newstringobject(inittab[i].name);
179 if (name == NULL)
180 break;
181 addlistitem(list, name);
182 DECREF(name);
184 if (sortlist(list) != 0) {
185 DECREF(list);
186 list = NULL;
188 return list;
191 void
192 initsys()
194 extern long getmaxint PROTO((void));
195 extern char *getversion PROTO((void));
196 extern char *getcopyright PROTO((void));
197 extern int fclose PROTO((FILE *));
198 object *m = initmodule("sys", sys_methods);
199 object *v;
200 sysdict = getmoduledict(m);
201 INCREF(sysdict);
202 /* NB keep an extra ref to the std files to avoid closing them
203 when the user deletes them */
204 sysin = newopenfileobject(stdin, "<stdin>", "r", fclose);
205 sysout = newopenfileobject(stdout, "<stdout>", "w", fclose);
206 syserr = newopenfileobject(stderr, "<stderr>", "w", fclose);
207 if (err_occurred())
208 fatal("can't initialize sys.std{in,out,err}");
209 dictinsert(sysdict, "stdin", sysin);
210 dictinsert(sysdict, "stdout", sysout);
211 dictinsert(sysdict, "stderr", syserr);
212 dictinsert(sysdict, "version", v = newstringobject(getversion()));
213 XDECREF(v);
214 dictinsert(sysdict, "copyright", v = newstringobject(getcopyright()));
215 XDECREF(v);
216 dictinsert(sysdict, "maxint", v = newintobject(getmaxint()));
217 XDECREF(v);
218 dictinsert(sysdict, "modules", get_modules());
219 dictinsert(sysdict, "builtin_module_names",
220 v = list_builtin_module_names());
221 XDECREF(v);
222 if (err_occurred())
223 fatal("can't insert sys.* objects in sys dict");
226 static object *
227 makepathobject(path, delim)
228 char *path;
229 int delim;
231 int i, n;
232 char *p;
233 object *v, *w;
235 n = 1;
236 p = path;
237 while ((p = strchr(p, delim)) != NULL) {
238 n++;
239 p++;
241 v = newlistobject(n);
242 if (v == NULL)
243 return NULL;
244 for (i = 0; ; i++) {
245 p = strchr(path, delim);
246 if (p == NULL)
247 p = strchr(path, '\0'); /* End of string */
248 w = newsizedstringobject(path, (int) (p - path));
249 if (w == NULL) {
250 DECREF(v);
251 return NULL;
253 setlistitem(v, i, w);
254 if (*p == '\0')
255 break;
256 path = p+1;
258 return v;
261 void
262 setpythonpath(path)
263 char *path;
265 object *v;
266 if ((v = makepathobject(path, DELIM)) == NULL)
267 fatal("can't create sys.path");
268 if (sysset("path", v) != 0)
269 fatal("can't assign sys.path");
270 DECREF(v);
273 static object *
274 makeargvobject(argc, argv)
275 int argc;
276 char **argv;
278 object *av;
279 if (argc <= 0 || argv == NULL) {
280 /* Ensure at least one (empty) argument is seen */
281 static char *empty_argv[1] = {""};
282 argv = empty_argv;
283 argc = 1;
285 av = newlistobject(argc);
286 if (av != NULL) {
287 int i;
288 for (i = 0; i < argc; i++) {
289 object *v = newstringobject(argv[i]);
290 if (v == NULL) {
291 DECREF(av);
292 av = NULL;
293 break;
295 setlistitem(av, i, v);
298 return av;
301 void
302 setpythonargv(argc, argv)
303 int argc;
304 char **argv;
306 object *av = makeargvobject(argc, argv);
307 if (av == NULL)
308 fatal("no mem for sys.argv");
309 if (sysset("argv", av) != 0)
310 fatal("can't assign sys.argv");
311 DECREF(av);