Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | Netherlands. |
| 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 | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 45 | |
| 46 | /* Define delimiter used in $PYTHONPATH */ |
| 47 | |
Guido van Rossum | d6a15ad | 1991-06-24 22:30:42 +0000 | [diff] [blame] | 48 | #ifdef macintosh |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 49 | #define DELIM ' ' |
| 50 | #endif |
| 51 | |
Guido van Rossum | eb4361a | 1991-05-05 20:14:35 +0000 | [diff] [blame] | 52 | #ifdef MSDOS |
| 53 | #define DELIM ';' |
| 54 | #endif |
| 55 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 56 | #ifndef DELIM |
| 57 | #define DELIM ':' |
| 58 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | |
| 60 | static object *sysdict; |
| 61 | |
| 62 | object * |
| 63 | sysget(name) |
| 64 | char *name; |
| 65 | { |
| 66 | return dictlookup(sysdict, name); |
| 67 | } |
| 68 | |
| 69 | FILE * |
| 70 | sysgetfile(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 | |
| 83 | int |
| 84 | sysset(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 | |
| 94 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | sys_exit(self, args) |
| 96 | object *self; |
| 97 | object *args; |
| 98 | { |
Guido van Rossum | 6a468bf | 1991-12-31 13:15:35 +0000 | [diff] [blame] | 99 | /* Raise SystemExit so callers may catch it or clean up. */ |
| 100 | err_setval(SystemExit, args); |
| 101 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 104 | static struct methodlist sys_methods[] = { |
| 105 | {"exit", sys_exit}, |
| 106 | {NULL, NULL} /* sentinel */ |
| 107 | }; |
| 108 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | static object *sysin, *sysout, *syserr; |
| 110 | |
| 111 | void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 112 | initsys() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | { |
Guido van Rossum | 3caa6e3 | 1991-06-04 19:40:59 +0000 | [diff] [blame] | 114 | extern int fclose PROTO((FILE *)); |
Guido van Rossum | 7177657 | 1991-12-10 14:00:21 +0000 | [diff] [blame] | 115 | extern char version[]; |
| 116 | object *v = newstringobject(version); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 117 | object *m = initmodule("sys", sys_methods); |
| 118 | sysdict = getmoduledict(m); |
| 119 | INCREF(sysdict); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 120 | /* NB keep an extra ref to the std files to avoid closing them |
| 121 | when the user deletes them */ |
Guido van Rossum | 3caa6e3 | 1991-06-04 19:40:59 +0000 | [diff] [blame] | 122 | sysin = newopenfileobject(stdin, "<stdin>", "r", fclose); |
| 123 | sysout = newopenfileobject(stdout, "<stdout>", "w", fclose); |
| 124 | syserr = newopenfileobject(stderr, "<stderr>", "w", fclose); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 125 | if (err_occurred()) |
Guido van Rossum | 7177657 | 1991-12-10 14:00:21 +0000 | [diff] [blame] | 126 | fatal("can't create sys.* objects"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 127 | dictinsert(sysdict, "stdin", sysin); |
| 128 | dictinsert(sysdict, "stdout", sysout); |
| 129 | dictinsert(sysdict, "stderr", syserr); |
Guido van Rossum | 7177657 | 1991-12-10 14:00:21 +0000 | [diff] [blame] | 130 | dictinsert(sysdict, "version", v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 131 | dictinsert(sysdict, "modules", get_modules()); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | if (err_occurred()) |
| 133 | fatal("can't insert sys.* objects in sys dict"); |
Guido van Rossum | 7177657 | 1991-12-10 14:00:21 +0000 | [diff] [blame] | 134 | DECREF(v); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 137 | static object * |
| 138 | makepathobject(path, delim) |
| 139 | char *path; |
| 140 | int delim; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 141 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 142 | 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 Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 151 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 152 | 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 173 | setpythonpath(path) |
| 174 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 175 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 176 | 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 | |
| 184 | static object * |
| 185 | makeargvobject(argc, argv) |
| 186 | int argc; |
| 187 | char **argv; |
| 188 | { |
| 189 | object *av; |
Guido van Rossum | ee3a299 | 1992-01-14 18:42:53 +0000 | [diff] [blame] | 190 | 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 196 | av = newlistobject(argc); |
| 197 | if (av != NULL) { |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 198 | int i; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 199 | 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 Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 205 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 206 | setlistitem(av, i, v); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 207 | } |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 208 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 209 | return av; |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | setpythonargv(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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 223 | } |