3 // SWIG file embedding the Python interpreter in something else.
 
   4 // This file is based on Python-1.3, but it might work with
 
   7 // This file makes it possible to extend Python and all of its
 
   8 // built-in functions without having to hack it's setup script.
 
  12 %subsection "embed13.i"
 
  14 This module provides support for building a new version of the
 
  15 Python 1.3 executable.  This will be necessary on systems that do
 
  16 not support shared libraries and may be necessary with C++
 
  17 extensions.  This file contains everything you need to build
 
  18 a new version of Python from include files and libraries normally
 
  19 installed with the Python language.
 
  21 This module is functionally equivalent to the embed.i library,
 
  22 but has a number of changes needed to work with older versions
 
  26 %echo "embed.i : Using Python 1.3"
 
  38 #include <pythonrun.h>
 
  39 typedef struct SWIGPyTab {
 
  47 void SWIG_init(void);  /* Forward reference */
 
  49 #define inittab    python_inittab
 
  51 /* Grab Python's inittab[] structure */
 
  61 /* Now define our own version of it.
 
  62    God forbid someone have more than 1000 built-in modules! */
 
  64 SWIGPyTab inittab[1000];       
 
  66 static int  swig_num_modules = 0;
 
  68 /* Function for adding modules to Python */
 
  70 static void swig_add_module(char *name, void (*initfunc)()) {
 
  71         inittab[swig_num_modules].name = name;
 
  72         inittab[swig_num_modules].initfunc = initfunc;
 
  74         inittab[swig_num_modules].name = (char *) 0;
 
  75         inittab[swig_num_modules].initfunc = (void (*)()) 0;
 
  78 /* Function to add all of Python's build in modules to our interpreter */
 
  80 static void swig_add_builtin() {
 
  82         while (python_inittab[i].name) {
 
  83                 swig_add_module(python_inittab[i].name, python_inittab[i].initfunc);
 
  87         /* Add SWIG builtin function */
 
  88         swig_add_module(SWIG_name, SWIG_init);
 
  97 /* Interface to getopt(): */
 
 103 extern "C" int getopt(int, char **, char *);
 
 105 extern int getopt();    /* PROTO((int, char **, char *)); -- not standardized */
 
 109 extern int Py_DebugFlag;             /* For parser.c, declared in pythonrun.c */
 
 110 extern int Py_VerboseFlag;           /* For import.c, declared in pythonrun.c */
 
 111 extern int Py_SuppressPrintingFlag;  /* For ceval.c, declared in pythonrun.c */
 
 113 /* Subroutines that live in their own file */
 
 116 extern int  isatty(int fd);
 
 117 extern int  PySys_SetArgv(int, char **);
 
 119 extern char *getversion();
 
 120 extern char *getcopyright();
 
 125 /* For getprogramname(); set by main() */
 
 128 /* For getargcargv(); set by main() */
 
 129 static char **orig_argv;
 
 130 static int  orig_argc;
 
 132 /* Short usage message (with %s for argv0) */
 
 133 static char *usage_line =
 
 134 "usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n";
 
 136 /* Long usage message, split into parts < 512 bytes */
 
 137 static char *usage_top = "\n\
 
 138 Options and arguments (and corresponding environment variables):\n\
 
 139 -d     : debug output from parser (also PYTHONDEBUG=x)\n\
 
 140 -i     : inspect interactively after running script (also PYTHONINSPECT=x)\n\
 
 141 -s     : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\
 
 142 -u     : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
 
 143 -v     : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
 
 144 -c cmd : program passed in as string (terminates option list)\n\
 
 146 static char *usage_bot = "\
 
 147 file   : program read from script file\n\
 
 148 -      : program read from stdin (default; interactive mode if a tty)\n\
 
 149 arg ...: arguments passed to program in sys.argv[1:]\n\
 
 151 Other environment variables:\n\
 
 152 PYTHONSTARTUP: file executed on interactive startup (no default)\n\
 
 153 PYTHONPATH   : colon-separated list of directories prefixed to the\n\
 
 154                default module search path.  The result is sys.path.\n\
 
 160 main(int argc, char **argv) {
 
 163         char *command = NULL;
 
 164         char *filename = NULL;
 
 170         swig_add_builtin();     /* Add SWIG built-in modules */
 
 171         orig_argc = argc;       /* For getargcargv() */
 
 173         argv0 = argv[0];        /* For getprogramname() */
 
 175         if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
 
 177         if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
 
 178                 Py_SuppressPrintingFlag = 1;
 
 179         if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
 
 181         if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
 
 183         if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
 
 186         while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
 
 188                         /* -c is the last option; following arguments
 
 189                            that look like options are left for the
 
 190                            the command to interpret. */
 
 191                         command = (char *) malloc(strlen(optarg) + 2);
 
 194                                    "not enough memory to copy -c argument");
 
 195                         strcpy(command, optarg);
 
 196                         strcat(command, "\n");
 
 211                         Py_SuppressPrintingFlag++;
 
 222                 /* This space reserved for other options */
 
 225                         fprintf(stderr, usage_line, argv[0]);
 
 226                         fprintf(stderr, usage_top);
 
 227                         fprintf(stderr, usage_bot);
 
 236                 setbuf(stdout, (char *)NULL);
 
 237                 setbuf(stderr, (char *)NULL);
 
 239                 /* On MPW (3.2) unbuffered seems to hang */
 
 240                 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
 
 241                 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
 
 245         if (command == NULL && optind < argc &&
 
 246             strcmp(argv[optind], "-") != 0)
 
 247                 filename = argv[optind];
 
 249         if (Py_VerboseFlag ||
 
 250             command == NULL && filename == NULL && isatty((int)fileno(fp)))
 
 251                 fprintf(stderr, "Python %s\n%s\n",
 
 252                         getversion(), getcopyright());
 
 254         if (filename != NULL) {
 
 255                 if ((fp = fopen(filename, "r")) == NULL) {
 
 256                         fprintf(stderr, "%s: can't open file '%s'\n",
 
 263         if (command != NULL) {
 
 264                 /* Backup optind and force sys.argv[0] = '-c' */
 
 269         PySys_SetArgv(argc-optind, argv+optind);
 
 272                 sts = PyRun_SimpleString(command) != 0;
 
 275                 if (filename == NULL && isatty((int)fileno(fp))) {
 
 276                         char *startup = getenv("PYTHONSTARTUP");
 
 277                         if (startup != NULL && startup[0] != '\0') {
 
 278                                 FILE *fp = fopen(startup, "r");
 
 280                                         (void) PyRun_SimpleFile(fp, startup);
 
 287                         fp, filename == NULL ? "<stdin>" : filename) != 0;
 
 288                 if (filename != NULL)
 
 292         if (inspect && isatty((int)fileno(stdin)) &&
 
 293             (filename != NULL || command != NULL))
 
 294                 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
 
 301 /* Return the program name -- some code out there needs this. */
 
 314 /* Make the *original* argc/argv available to other modules.
 
 315    This is rare, but it is needed by the secureware extension. */
 
 321 getargcargv(int *argc,char ***argv)
 
 327 /* Total Hack to get getpath.c to compile under C++ */
 
 330 #define malloc   (char *) malloc