home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Modules / main.c < prev    next >
C/C++ Source or Header  |  1999-06-27  |  9KB  |  314 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. 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 or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Python interpreter main program */
  33.  
  34. #include "Python.h"
  35. #include "osdefs.h"
  36.  
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40.  
  41. #ifdef MS_WINDOWS
  42. #include <fcntl.h>
  43. #endif
  44.  
  45. #if defined(PYOS_OS2) || defined(MS_WINDOWS)
  46. #define PYTHONHOMEHELP "<prefix>\\lib"
  47. #else
  48. #define PYTHONHOMEHELP "<prefix>/python1.5"
  49. #endif
  50.  
  51. /* Interface to getopt(): */
  52. extern int optind;
  53. extern char *optarg;
  54. extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
  55.  
  56.  
  57. /* For Py_GetArgcArgv(); set by main() */
  58. static char **orig_argv;
  59. static int  orig_argc;
  60.  
  61. /* Short usage message (with %s for argv0) */
  62. static char *usage_line =
  63. "usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
  64.  
  65. /* Long usage message, split into parts < 512 bytes */
  66. static char *usage_top = "\
  67. Options and arguments (and corresponding environment variables):\n\
  68. -d     : debug output from parser (also PYTHONDEBUG=x)\n\
  69. -i     : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
  70.          and force prompts, even if stdin does not appear to be a terminal\n\
  71. -O     : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
  72. -OO    : remove doc-strings in addition to the -O optimizations\n\
  73. -S     : don't imply 'import site' on initialization\n\
  74. -t     : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
  75. ";
  76. static char *usage_mid = "\
  77. -u     : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
  78. -v     : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
  79. -x     : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
  80. -X     : disable class based built-in exceptions\n\
  81. -c cmd : program passed in as string (terminates option list)\n\
  82. file   : program read from script file\n\
  83. -      : program read from stdin (default; interactive mode if a tty)\n\
  84. ";
  85. static char *usage_bot = "\
  86. arg ...: arguments passed to program in sys.argv[1:]\n\
  87. Other environment variables:\n\
  88. PYTHONSTARTUP: file executed on interactive startup (no default)\n\
  89. PYTHONPATH   : '%c'-separated list of directories prefixed to the\n\
  90.                default module search path.  The result is sys.path.\n\
  91. PYTHONHOME   : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
  92.                The default module search path uses %s.\n\
  93. ";
  94.  
  95.  
  96. /* Main program */
  97.  
  98. DL_EXPORT(int)
  99. Py_Main(argc, argv)
  100.     int argc;
  101.     char **argv;
  102. {
  103.     int c;
  104.     int sts;
  105.     char *command = NULL;
  106.     char *filename = NULL;
  107.     FILE *fp = stdin;
  108.     char *p;
  109.     int inspect = 0;
  110.     int unbuffered = 0;
  111.     int skipfirstline = 0;
  112.     int stdin_is_interactive = 0;
  113.  
  114.     orig_argc = argc;    /* For Py_GetArgcArgv() */
  115.     orig_argv = argv;
  116.  
  117.     if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
  118.         inspect = 1;
  119.     if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
  120.         unbuffered = 1;
  121.  
  122.     while ((c = getopt(argc, argv, "c:diOStuvxX")) != EOF) {
  123.         if (c == 'c') {
  124.             /* -c is the last option; following arguments
  125.                that look like options are left for the
  126.                the command to interpret. */
  127.             command = malloc(strlen(optarg) + 2);
  128.             if (command == NULL)
  129.                 Py_FatalError(
  130.                    "not enough memory to copy -c argument");
  131.             strcpy(command, optarg);
  132.             strcat(command, "\n");
  133.             break;
  134.         }
  135.         
  136.         switch (c) {
  137.  
  138.         case 'd':
  139.             Py_DebugFlag++;
  140.             break;
  141.  
  142.         case 'i':
  143.             inspect++;
  144.             Py_InteractiveFlag++;
  145.             break;
  146.  
  147.         case 'O':
  148.             Py_OptimizeFlag++;
  149.             break;
  150.  
  151.         case 'S':
  152.             Py_NoSiteFlag++;
  153.             break;
  154.  
  155.         case 't':
  156.             Py_TabcheckFlag++;
  157.             break;
  158.  
  159.         case 'u':
  160.             unbuffered++;
  161.             break;
  162.  
  163.         case 'v':
  164.             Py_VerboseFlag++;
  165.             break;
  166.  
  167.         case 'x':
  168.             skipfirstline = 1;
  169.             break;
  170.  
  171.         case 'X':
  172.             Py_UseClassExceptionsFlag = 0;
  173.             break;
  174.  
  175.         /* This space reserved for other options */
  176.  
  177.         default:
  178.             fprintf(stderr, usage_line, argv[0]);
  179.             fprintf(stderr, usage_top);
  180.             fprintf(stderr, usage_mid);
  181.             fprintf(stderr, usage_bot,
  182.                 DELIM, DELIM, PYTHONHOMEHELP);
  183.             exit(2);
  184.             /*NOTREACHED*/
  185.  
  186.         }
  187.     }
  188.  
  189.     if (command == NULL && optind < argc &&
  190.         strcmp(argv[optind], "-") != 0)
  191.     {
  192.         filename = argv[optind];
  193.         if (filename != NULL) {
  194.             if ((fp = fopen(filename, "r")) == NULL) {
  195.                 fprintf(stderr, "%s: can't open file '%s'\n",
  196.                     argv[0], filename);
  197.                 exit(2);
  198.             }
  199.             else if (skipfirstline) {
  200.                 char line[256];
  201.                 fgets(line, sizeof line, fp);
  202.             }
  203.         }
  204.     }
  205.  
  206.     stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
  207.  
  208.     if (unbuffered) {
  209. #ifdef MS_WINDOWS
  210.         _setmode(fileno(stdin), O_BINARY);
  211.         _setmode(fileno(stdout), O_BINARY);
  212. #endif
  213. #ifndef MPW
  214. #ifdef HAVE_SETVBUF
  215.         setvbuf(stdin,  (char *)NULL, _IONBF, BUFSIZ);
  216.         setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
  217.         setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
  218. #else /* !HAVE_SETVBUF */
  219.         setbuf(stdin,  (char *)NULL);
  220.         setbuf(stdout, (char *)NULL);
  221.         setbuf(stderr, (char *)NULL);
  222. #endif /* !HAVE_SETVBUF */
  223. #else /* MPW */
  224.         /* On MPW (3.2) unbuffered seems to hang */
  225.         setvbuf(stdin,  (char *)NULL, _IOLBF, BUFSIZ);
  226.         setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
  227.         setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
  228. #endif /* MPW */
  229.     }
  230.     else if (Py_InteractiveFlag) {
  231. #ifdef MS_WINDOWS
  232.         /* Doesn't have to have line-buffered -- use unbuffered */
  233.         /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
  234.         setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
  235. #else /* !MS_WINDOWS */
  236. #ifdef HAVE_SETVBUF
  237.         setvbuf(stdin,  (char *)NULL, _IOLBF, BUFSIZ);
  238.         setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
  239. #endif /* HAVE_SETVBUF */
  240. #endif /* !MS_WINDOWS */
  241.         /* Leave stderr alone - it should be unbuffered anyway. */
  242.       }
  243.  
  244.     Py_SetProgramName(argv[0]);
  245.     Py_Initialize();
  246.  
  247.     if (Py_VerboseFlag ||
  248.         (command == NULL && filename == NULL && stdin_is_interactive))
  249.         fprintf(stderr, "Python %s on %s\n%s\n",
  250.             Py_GetVersion(), Py_GetPlatform(), Py_GetCopyright());
  251.     
  252.     
  253.     if (command != NULL) {
  254.         /* Backup optind and force sys.argv[0] = '-c' */
  255.         optind--;
  256.         argv[optind] = "-c";
  257.     }
  258.  
  259.     PySys_SetArgv(argc-optind, argv+optind);
  260.  
  261.     if ((inspect || (command == NULL && filename == NULL)) &&
  262.         isatty(fileno(stdin))) {
  263.         PyObject *v;
  264.         v = PyImport_ImportModule("readline");
  265.         if (v == NULL)
  266.             PyErr_Clear();
  267.         else
  268.             Py_DECREF(v);
  269.     }
  270.  
  271.     if (command) {
  272.         sts = PyRun_SimpleString(command) != 0;
  273.         free(command);
  274.     }
  275.     else {
  276.         if (filename == NULL && stdin_is_interactive) {
  277.             char *startup = getenv("PYTHONSTARTUP");
  278.             if (startup != NULL && startup[0] != '\0') {
  279.                 FILE *fp = fopen(startup, "r");
  280.                 if (fp != NULL) {
  281.                     (void) PyRun_SimpleFile(fp, startup);
  282.                     PyErr_Clear();
  283.                     fclose(fp);
  284.                 }
  285.             }
  286.         }
  287.         sts = PyRun_AnyFile(
  288.             fp,
  289.             filename == NULL ? "<stdin>" : filename) != 0;
  290.         if (filename != NULL)
  291.             fclose(fp);
  292.     }
  293.  
  294.     if (inspect && stdin_is_interactive &&
  295.         (filename != NULL || command != NULL))
  296.         sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
  297.  
  298.     Py_Finalize();
  299.     return sts;
  300. }
  301.  
  302.  
  303. /* Make the *original* argc/argv available to other modules.
  304.    This is rare, but it is needed by the secureware extension. */
  305.  
  306. void
  307. Py_GetArgcArgv(argc, argv)
  308.     int *argc;
  309.     char ***argv;
  310. {
  311.     *argc = orig_argc;
  312.     *argv = orig_argv;
  313. }
  314.