Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* System module */ |
| 2 | |
| 3 | /* |
| 4 | Various bits of information used by the interpreter are collected in |
| 5 | module 'sys'. |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 6 | Function member: |
| 7 | - exit(sts): call (C, POSIX) exit(sts) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8 | Data members: |
| 9 | - stdin, stdout, stderr: standard file objects |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | - modules: the table of modules (dictionary) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 11 | - 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 16 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 18 | #include "sysmodule.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 19 | #include "import.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 20 | #include "modsupport.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 21 | |
| 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | |
| 32 | static object *sysdict; |
| 33 | |
| 34 | object * |
| 35 | sysget(name) |
| 36 | char *name; |
| 37 | { |
| 38 | return dictlookup(sysdict, name); |
| 39 | } |
| 40 | |
| 41 | FILE * |
| 42 | sysgetfile(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 | |
| 55 | int |
| 56 | sysset(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 | |
| 66 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | sys_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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 79 | static struct methodlist sys_methods[] = { |
| 80 | {"exit", sys_exit}, |
| 81 | {NULL, NULL} /* sentinel */ |
| 82 | }; |
| 83 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 84 | static object *sysin, *sysout, *syserr; |
| 85 | |
| 86 | void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 87 | initsys() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 89 | object *m = initmodule("sys", sys_methods); |
| 90 | sysdict = getmoduledict(m); |
| 91 | INCREF(sysdict); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | /* NB keep an extra ref to the std files to avoid closing them |
| 93 | when the user deletes them */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 94 | /* XXX File objects should have a "don't close" flag instead */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | sysin = newopenfileobject(stdin, "<stdin>", "r"); |
| 96 | sysout = newopenfileobject(stdout, "<stdout>", "w"); |
| 97 | syserr = newopenfileobject(stderr, "<stderr>", "w"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | if (err_occurred()) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 99 | fatal("can't create sys.std* file objects"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | dictinsert(sysdict, "stdin", sysin); |
| 101 | dictinsert(sysdict, "stdout", sysout); |
| 102 | dictinsert(sysdict, "stderr", syserr); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 103 | dictinsert(sysdict, "modules", get_modules()); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 104 | if (err_occurred()) |
| 105 | fatal("can't insert sys.* objects in sys dict"); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 108 | static object * |
| 109 | makepathobject(path, delim) |
| 110 | char *path; |
| 111 | int delim; |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 112 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 113 | 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 Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 122 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 123 | 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 144 | setpythonpath(path) |
| 145 | char *path; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 146 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 147 | 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 | |
| 155 | static object * |
| 156 | makeargvobject(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 Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 165 | int i; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 166 | 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 Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 172 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 173 | setlistitem(av, i, v); |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 174 | } |
Guido van Rossum | 5b3138b | 1990-11-18 17:41:40 +0000 | [diff] [blame] | 175 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 176 | return av; |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | setpythonargv(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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 190 | } |