Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 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. |
| 14 | |
| 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. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* System module */ |
| 26 | |
| 27 | /* |
| 28 | Various bits of information used by the interpreter are collected in |
| 29 | module 'sys'. |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | Function member: |
| 31 | - exit(sts): call (C, POSIX) exit(sts) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | Data members: |
| 33 | - stdin, stdout, stderr: standard file objects |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 34 | - modules: the table of modules (dictionary) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | - 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | */ |
| 39 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 40 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | #include "sysmodule.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 43 | #include "import.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | #include "modsupport.h" |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 45 | #include "osdefs.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 47 | object *sys_trace, *sys_profile; |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 48 | int sys_checkinterval; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 49 | |
| 50 | static object *sysdict; |
| 51 | |
| 52 | object * |
| 53 | sysget(name) |
| 54 | char *name; |
| 55 | { |
| 56 | return dictlookup(sysdict, name); |
| 57 | } |
| 58 | |
| 59 | FILE * |
| 60 | sysgetfile(name, def) |
| 61 | char *name; |
| 62 | FILE *def; |
| 63 | { |
| 64 | FILE *fp = NULL; |
| 65 | object *v = sysget(name); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 66 | if (v != NULL && is_fileobject(v)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | fp = getfilefile(v); |
| 68 | if (fp == NULL) |
| 69 | fp = def; |
| 70 | return fp; |
| 71 | } |
| 72 | |
| 73 | int |
| 74 | sysset(name, v) |
| 75 | char *name; |
| 76 | object *v; |
| 77 | { |
Guido van Rossum | 5ad58c6 | 1992-01-26 18:15:48 +0000 | [diff] [blame] | 78 | if (v == NULL) { |
| 79 | if (dictlookup(sysdict, name) == NULL) |
| 80 | return 0; |
| 81 | else |
| 82 | return dictremove(sysdict, name); |
| 83 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 84 | else |
| 85 | return dictinsert(sysdict, name, v); |
| 86 | } |
| 87 | |
| 88 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 89 | sys_exit(self, args) |
| 90 | object *self; |
| 91 | object *args; |
| 92 | { |
Guido van Rossum | 6a468bf | 1991-12-31 13:15:35 +0000 | [diff] [blame] | 93 | /* Raise SystemExit so callers may catch it or clean up. */ |
| 94 | err_setval(SystemExit, args); |
| 95 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 98 | static object * |
| 99 | sys_settrace(self, args) |
| 100 | object *self; |
| 101 | object *args; |
| 102 | { |
| 103 | if (args == None) |
| 104 | args = NULL; |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 105 | else |
Guido van Rossum | 5dc8eb0 | 1992-06-19 13:57:27 +0000 | [diff] [blame] | 106 | XINCREF(args); |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 107 | XDECREF(sys_trace); |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 108 | sys_trace = args; |
| 109 | INCREF(None); |
| 110 | return None; |
| 111 | } |
| 112 | |
| 113 | static object * |
| 114 | sys_setprofile(self, args) |
| 115 | object *self; |
| 116 | object *args; |
| 117 | { |
| 118 | if (args == None) |
| 119 | args = NULL; |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 120 | else |
Guido van Rossum | 5dc8eb0 | 1992-06-19 13:57:27 +0000 | [diff] [blame] | 121 | XINCREF(args); |
Guido van Rossum | e765f7d | 1992-04-05 14:17:55 +0000 | [diff] [blame] | 122 | XDECREF(sys_profile); |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 123 | sys_profile = args; |
| 124 | INCREF(None); |
| 125 | return None; |
| 126 | } |
| 127 | |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 128 | static object * |
| 129 | sys_setcheckinterval(self, args) |
| 130 | object *self; |
| 131 | object *args; |
| 132 | { |
| 133 | if (!newgetargs(args, "i", &sys_checkinterval)) |
| 134 | return NULL; |
| 135 | INCREF(None); |
| 136 | return None; |
| 137 | } |
| 138 | |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 139 | #ifdef USE_MALLOPT |
| 140 | /* Link with -lmalloc (or -lmpc) on an SGI */ |
| 141 | #include <malloc.h> |
| 142 | |
| 143 | static object * |
| 144 | sys_mdebug(self, args) |
| 145 | object *self; |
| 146 | object *args; |
| 147 | { |
| 148 | int flag; |
| 149 | if (!getargs(args, "i", &flag)) |
| 150 | return NULL; |
| 151 | mallopt(M_DEBUG, flag); |
| 152 | INCREF(None); |
| 153 | return None; |
| 154 | } |
| 155 | #endif /* USE_MALLOPT */ |
| 156 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 157 | static struct methodlist sys_methods[] = { |
| 158 | {"exit", sys_exit}, |
Guido van Rossum | 14b4adb | 1992-09-03 20:25:30 +0000 | [diff] [blame] | 159 | #ifdef USE_MALLOPT |
| 160 | {"mdebug", sys_mdebug}, |
| 161 | #endif |
Guido van Rossum | e2437a1 | 1992-03-23 18:20:18 +0000 | [diff] [blame] | 162 | {"setprofile", sys_setprofile}, |
| 163 | {"settrace", sys_settrace}, |
Guido van Rossum | a0d7a23 | 1995-01-09 17:46:13 +0000 | [diff] [blame] | 164 | {"setcheckinterval", sys_setcheckinterval}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 165 | {NULL, NULL} /* sentinel */ |
| 166 | }; |
| 167 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 168 | static object *sysin, *sysout, *syserr; |
| 169 | |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 170 | static object * |
| 171 | list_builtin_module_names() |
| 172 | { |
| 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); |
| 183 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 184 | if (sortlist(list) != 0) { |
| 185 | DECREF(list); |
| 186 | list = NULL; |
| 187 | } |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 188 | return list; |
| 189 | } |
| 190 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 191 | void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 192 | initsys() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 193 | { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 194 | extern long getmaxint PROTO((void)); |
| 195 | extern char *getversion PROTO((void)); |
| 196 | extern char *getcopyright PROTO((void)); |
Guido van Rossum | 3caa6e3 | 1991-06-04 19:40:59 +0000 | [diff] [blame] | 197 | extern int fclose PROTO((FILE *)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 198 | object *m = initmodule("sys", sys_methods); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 199 | object *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 200 | sysdict = getmoduledict(m); |
| 201 | INCREF(sysdict); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 202 | /* NB keep an extra ref to the std files to avoid closing them |
| 203 | when the user deletes them */ |
Guido van Rossum | 3caa6e3 | 1991-06-04 19:40:59 +0000 | [diff] [blame] | 204 | sysin = newopenfileobject(stdin, "<stdin>", "r", fclose); |
| 205 | sysout = newopenfileobject(stdout, "<stdout>", "w", fclose); |
| 206 | syserr = newopenfileobject(stderr, "<stderr>", "w", fclose); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 207 | if (err_occurred()) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 208 | fatal("can't initialize sys.std{in,out,err}"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 209 | dictinsert(sysdict, "stdin", sysin); |
| 210 | dictinsert(sysdict, "stdout", sysout); |
| 211 | dictinsert(sysdict, "stderr", syserr); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 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); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 218 | dictinsert(sysdict, "modules", get_modules()); |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 219 | dictinsert(sysdict, "builtin_module_names", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 220 | v = list_builtin_module_names()); |
| 221 | XDECREF(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | if (err_occurred()) |
| 223 | fatal("can't insert sys.* objects in sys dict"); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 226 | static object * |
| 227 | makepathobject(path, delim) |
| 228 | char *path; |
| 229 | int delim; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 230 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 231 | int i, n; |
| 232 | char *p; |
| 233 | object *v, *w; |
| 234 | |
| 235 | n = 1; |
| 236 | p = path; |
| 237 | while ((p = strchr(p, delim)) != NULL) { |
| 238 | n++; |
| 239 | p++; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 240 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 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; |
| 252 | } |
| 253 | setlistitem(v, i, w); |
| 254 | if (*p == '\0') |
| 255 | break; |
| 256 | path = p+1; |
| 257 | } |
| 258 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 262 | setpythonpath(path) |
| 263 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 264 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 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); |
| 271 | } |
| 272 | |
| 273 | static object * |
| 274 | makeargvobject(argc, argv) |
| 275 | int argc; |
| 276 | char **argv; |
| 277 | { |
| 278 | object *av; |
Guido van Rossum | ee3a299 | 1992-01-14 18:42:53 +0000 | [diff] [blame] | 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; |
| 284 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 285 | av = newlistobject(argc); |
| 286 | if (av != NULL) { |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 287 | int i; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 294 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 295 | setlistitem(av, i, v); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 296 | } |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 297 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 298 | return av; |
| 299 | } |
| 300 | |
| 301 | void |
| 302 | setpythonargv(argc, argv) |
| 303 | int argc; |
| 304 | char **argv; |
| 305 | { |
| 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); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 312 | } |