home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Python / sysmodule.c < prev    next >
C/C++ Source or Header  |  1994-01-03  |  6KB  |  299 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, The 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.  
  25. /* System module */
  26.  
  27. /*
  28. Various bits of information used by the interpreter are collected in
  29. module 'sys'.
  30. Function member:
  31. - exit(sts): call (C, POSIX) exit(sts)
  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)
  38. */
  39.  
  40. #include "allobjects.h"
  41.  
  42. #include "sysmodule.h"
  43. #include "import.h"
  44. #include "modsupport.h"
  45. #include "osdefs.h"
  46.  
  47. object *sys_trace, *sys_profile;
  48.  
  49. static object *sysdict;
  50.  
  51. object *
  52. sysget(name)
  53.     char *name;
  54. {
  55.     return dictlookup(sysdict, name);
  56. }
  57.  
  58. FILE *
  59. sysgetfile(name, def)
  60.     char *name;
  61.     FILE *def;
  62. {
  63.     FILE *fp = NULL;
  64.     object *v = sysget(name);
  65.     if (v != NULL && is_fileobject(v))
  66.         fp = getfilefile(v);
  67.     if (fp == NULL)
  68.         fp = def;
  69.     return fp;
  70. }
  71.  
  72. int
  73. sysset(name, v)
  74.     char *name;
  75.     object *v;
  76. {
  77.     if (v == NULL) {
  78.         if (dictlookup(sysdict, name) == NULL)
  79.             return 0;
  80.         else
  81.             return dictremove(sysdict, name);
  82.     }
  83.     else
  84.         return dictinsert(sysdict, name, v);
  85. }
  86.  
  87. static object *
  88. sys_exit(self, args)
  89.     object *self;
  90.     object *args;
  91. {
  92.     /* Raise SystemExit so callers may catch it or clean up. */
  93.     err_setval(SystemExit, args);
  94.     return NULL;
  95. }
  96.  
  97. static object *
  98. sys_settrace(self, args)
  99.     object *self;
  100.     object *args;
  101. {
  102.     if (args == None)
  103.         args = NULL;
  104.     else
  105.         XINCREF(args);
  106.     XDECREF(sys_trace);
  107.     sys_trace = args;
  108.     INCREF(None);
  109.     return None;
  110. }
  111.  
  112. static object *
  113. sys_setprofile(self, args)
  114.     object *self;
  115.     object *args;
  116. {
  117.     if (args == None)
  118.         args = NULL;
  119.     else
  120.         XINCREF(args);
  121.     XDECREF(sys_profile);
  122.     sys_profile = args;
  123.     INCREF(None);
  124.     return None;
  125. }
  126.  
  127. #ifdef USE_MALLOPT
  128. /* Link with -lmalloc (or -lmpc) on an SGI */
  129. #include <malloc.h>
  130.  
  131. static object *
  132. sys_mdebug(self, args)
  133.     object *self;
  134.     object *args;
  135. {
  136.     int flag;
  137.     if (!getargs(args, "i", &flag))
  138.         return NULL;
  139.     mallopt(M_DEBUG, flag);
  140.     INCREF(None);
  141.     return None;
  142. }
  143. #endif /* USE_MALLOPT */
  144.  
  145. static struct methodlist sys_methods[] = {
  146.     {"exit",    sys_exit},
  147. #ifdef USE_MALLOPT
  148.     {"mdebug",    sys_mdebug},
  149. #endif
  150.     {"setprofile",    sys_setprofile},
  151.     {"settrace",    sys_settrace},
  152.     {NULL,        NULL}        /* sentinel */
  153. };
  154.  
  155. static object *sysin, *sysout, *syserr;
  156.  
  157. static object *
  158. list_builtin_module_names()
  159. {
  160.     object *list = newlistobject(0);
  161.     int i;
  162.     if (list == NULL)
  163.         return NULL;
  164.     for (i = 0; inittab[i].name != NULL; i++) {
  165.         object *name = newstringobject(inittab[i].name);
  166.         if (name == NULL)
  167.             break;
  168.         addlistitem(list, name);
  169.         DECREF(name);
  170.     }
  171.     if (sortlist(list) != 0) {
  172.         DECREF(list);
  173.         list = NULL;
  174.     }
  175.     return list;
  176. }
  177.  
  178. void
  179. initsys()
  180. {
  181.     extern long getmaxint PROTO((void));
  182.     extern char *getversion PROTO((void));
  183.     extern char *getcopyright PROTO((void));
  184.     extern int fclose PROTO((FILE *));
  185.     object *m = initmodule("sys", sys_methods);
  186.     object *v;
  187.     sysdict = getmoduledict(m);
  188.     INCREF(sysdict);
  189.     /* NB keep an extra ref to the std files to avoid closing them
  190.        when the user deletes them */
  191.     sysin = newopenfileobject(stdin, "<stdin>", "r", fclose);
  192.     sysout = newopenfileobject(stdout, "<stdout>", "w", fclose);
  193.     syserr = newopenfileobject(stderr, "<stderr>", "w", fclose);
  194.     if (err_occurred())
  195.         fatal("can't initialize sys.std{in,out,err}");
  196.     dictinsert(sysdict, "stdin", sysin);
  197.     dictinsert(sysdict, "stdout", sysout);
  198.     dictinsert(sysdict, "stderr", syserr);
  199.     dictinsert(sysdict, "version", v = newstringobject(getversion()));
  200.     XDECREF(v);
  201.     dictinsert(sysdict, "copyright", v = newstringobject(getcopyright()));
  202.     XDECREF(v);
  203.     dictinsert(sysdict, "maxint", v = newintobject(getmaxint()));
  204.     XDECREF(v);
  205.     dictinsert(sysdict, "modules", get_modules());
  206.     dictinsert(sysdict, "builtin_module_names",
  207.            list_builtin_module_names());
  208.     if (err_occurred())
  209.         fatal("can't insert sys.* objects in sys dict");
  210. }
  211.  
  212. static object *
  213. makepathobject(path, delim)
  214.     char *path;
  215.     int delim;
  216. {
  217.     int i, n;
  218.     char *p;
  219.     object *v, *w;
  220.     
  221.     n = 1;
  222.     p = path;
  223.     while ((p = strchr(p, delim)) != NULL) {
  224.         n++;
  225.         p++;
  226.     }
  227.     v = newlistobject(n);
  228.     if (v == NULL)
  229.         return NULL;
  230.     for (i = 0; ; i++) {
  231.         p = strchr(path, delim);
  232.         if (p == NULL)
  233.             p = strchr(path, '\0'); /* End of string */
  234.         w = newsizedstringobject(path, (int) (p - path));
  235.         if (w == NULL) {
  236.             DECREF(v);
  237.             return NULL;
  238.         }
  239.         setlistitem(v, i, w);
  240.         if (*p == '\0')
  241.             break;
  242.         path = p+1;
  243.     }
  244.     return v;
  245. }
  246.  
  247. void
  248. setpythonpath(path)
  249.     char *path;
  250. {
  251.     object *v;
  252.     if ((v = makepathobject(path, DELIM)) == NULL)
  253.         fatal("can't create sys.path");
  254.     if (sysset("path", v) != 0)
  255.         fatal("can't assign sys.path");
  256.     DECREF(v);
  257. }
  258.  
  259. static object *
  260. makeargvobject(argc, argv)
  261.     int argc;
  262.     char **argv;
  263. {
  264.     object *av;
  265.     if (argc <= 0 || argv == NULL) {
  266.         /* Ensure at least one (empty) argument is seen */
  267.         static char *empty_argv[1] = {""};
  268.         argv = empty_argv;
  269.         argc = 1;
  270.     }
  271.     av = newlistobject(argc);
  272.     if (av != NULL) {
  273.         int i;
  274.         for (i = 0; i < argc; i++) {
  275.             object *v = newstringobject(argv[i]);
  276.             if (v == NULL) {
  277.                 DECREF(av);
  278.                 av = NULL;
  279.                 break;
  280.             }
  281.             setlistitem(av, i, v);
  282.         }
  283.     }
  284.     return av;
  285. }
  286.  
  287. void
  288. setpythonargv(argc, argv)
  289.     int argc;
  290.     char **argv;
  291. {
  292.     object *av = makeargvobject(argc, argv);
  293.     if (av == NULL)
  294.         fatal("no mem for sys.argv");
  295.     if (sysset("argv", av) != 0)
  296.         fatal("can't assign sys.argv");
  297.     DECREF(av);
  298. }
  299.