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 | { |
| 99 | int sts; |
| 100 | if (!getintarg(args, &sts)) |
| 101 | return NULL; |
| 102 | goaway(sts); |
| 103 | exit(sts); /* Just in case */ |
| 104 | /* NOTREACHED */ |
| 105 | } |
| 106 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 107 | static struct methodlist sys_methods[] = { |
| 108 | {"exit", sys_exit}, |
| 109 | {NULL, NULL} /* sentinel */ |
| 110 | }; |
| 111 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 112 | static object *sysin, *sysout, *syserr; |
| 113 | |
| 114 | void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 115 | initsys() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | { |
Guido van Rossum | 3caa6e3 | 1991-06-04 19:40:59 +0000 | [diff] [blame] | 117 | extern int fclose PROTO((FILE *)); |
Guido van Rossum | 7177657 | 1991-12-10 14:00:21 +0000 | [diff] [blame] | 118 | extern char version[]; |
| 119 | object *v = newstringobject(version); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 120 | object *m = initmodule("sys", sys_methods); |
| 121 | sysdict = getmoduledict(m); |
| 122 | INCREF(sysdict); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | /* NB keep an extra ref to the std files to avoid closing them |
| 124 | when the user deletes them */ |
Guido van Rossum | 3caa6e3 | 1991-06-04 19:40:59 +0000 | [diff] [blame] | 125 | sysin = newopenfileobject(stdin, "<stdin>", "r", fclose); |
| 126 | sysout = newopenfileobject(stdout, "<stdout>", "w", fclose); |
| 127 | syserr = newopenfileobject(stderr, "<stderr>", "w", fclose); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 128 | if (err_occurred()) |
Guido van Rossum | 7177657 | 1991-12-10 14:00:21 +0000 | [diff] [blame] | 129 | fatal("can't create sys.* objects"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | dictinsert(sysdict, "stdin", sysin); |
| 131 | dictinsert(sysdict, "stdout", sysout); |
| 132 | dictinsert(sysdict, "stderr", syserr); |
Guido van Rossum | 7177657 | 1991-12-10 14:00:21 +0000 | [diff] [blame] | 133 | dictinsert(sysdict, "version", v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 134 | dictinsert(sysdict, "modules", get_modules()); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 135 | if (err_occurred()) |
| 136 | fatal("can't insert sys.* objects in sys dict"); |
Guido van Rossum | 7177657 | 1991-12-10 14:00:21 +0000 | [diff] [blame] | 137 | DECREF(v); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 140 | static object * |
| 141 | makepathobject(path, delim) |
| 142 | char *path; |
| 143 | int delim; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 144 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 145 | int i, n; |
| 146 | char *p; |
| 147 | object *v, *w; |
| 148 | |
| 149 | n = 1; |
| 150 | p = path; |
| 151 | while ((p = strchr(p, delim)) != NULL) { |
| 152 | n++; |
| 153 | p++; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 154 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 155 | v = newlistobject(n); |
| 156 | if (v == NULL) |
| 157 | return NULL; |
| 158 | for (i = 0; ; i++) { |
| 159 | p = strchr(path, delim); |
| 160 | if (p == NULL) |
| 161 | p = strchr(path, '\0'); /* End of string */ |
| 162 | w = newsizedstringobject(path, (int) (p - path)); |
| 163 | if (w == NULL) { |
| 164 | DECREF(v); |
| 165 | return NULL; |
| 166 | } |
| 167 | setlistitem(v, i, w); |
| 168 | if (*p == '\0') |
| 169 | break; |
| 170 | path = p+1; |
| 171 | } |
| 172 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 176 | setpythonpath(path) |
| 177 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 178 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 179 | object *v; |
| 180 | if ((v = makepathobject(path, DELIM)) == NULL) |
| 181 | fatal("can't create sys.path"); |
| 182 | if (sysset("path", v) != 0) |
| 183 | fatal("can't assign sys.path"); |
| 184 | DECREF(v); |
| 185 | } |
| 186 | |
| 187 | static object * |
| 188 | makeargvobject(argc, argv) |
| 189 | int argc; |
| 190 | char **argv; |
| 191 | { |
| 192 | object *av; |
| 193 | if (argc < 0 || argv == NULL) |
| 194 | argc = 0; |
| 195 | av = newlistobject(argc); |
| 196 | if (av != NULL) { |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 197 | int i; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 198 | for (i = 0; i < argc; i++) { |
| 199 | object *v = newstringobject(argv[i]); |
| 200 | if (v == NULL) { |
| 201 | DECREF(av); |
| 202 | av = NULL; |
| 203 | break; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 204 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 205 | setlistitem(av, i, v); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 206 | } |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 207 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 208 | return av; |
| 209 | } |
| 210 | |
| 211 | void |
| 212 | setpythonargv(argc, argv) |
| 213 | int argc; |
| 214 | char **argv; |
| 215 | { |
| 216 | object *av = makeargvobject(argc, argv); |
| 217 | if (av == NULL) |
| 218 | fatal("no mem for sys.argv"); |
| 219 | if (sysset("argv", av) != 0) |
| 220 | fatal("can't assign sys.argv"); |
| 221 | DECREF(av); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | } |