Changeset 17

Show
Ignore:
Timestamp:
Mon Sep 5 03:29:12 2005
Author:
giovannibajo
Message:

Reformat and properly indent

Files:

Legend:

Unmodified
Added
Removed
Modified
  • trunk/source/common/launch.c

    r2 r17  
    100 100 int testTempPath(char *buff)  
    101 101 {  
    102       char base[16];  
    103       int n;  
    104        
    105       n = strlen(buff);  
    106       if ( buff[n-1] == '/' || buff[n-1] == '\\' )  
    107           sprintf(base, "_MEI%d", getpid());  
    108       else  
    109           sprintf(base, "%s_MEI%d", SEP, getpid());  
    110       strcat(buff, base);  
    111   #ifdef WIN32  
    112       if (mkdir(buff) == 0) {  
    113   #else  
    114       if (mkdir(buff, 0700) == 0) {  
    115   #endif  
    116           strcat(buff, SEP);  
    117           return 1;  
    118       }  
    119       return 0;  
      102         char base[16];  
      103         int n;  
      104  
      105         n = strlen(buff);  
      106         if ( buff[n-1] == '/' || buff[n-1] == '\\' )  
      107                 sprintf(base, "_MEI%d", getpid());  
      108         else  
      109                 sprintf(base, "%s_MEI%d", SEP, getpid());  
      110         strcat(buff, base);  
      111 #ifdef WIN32  
      112         if (mkdir(buff) == 0) {  
      113 #else  
      114         if (mkdir(buff, 0700) == 0) {  
      115 #endif  
      116                 strcat(buff, SEP);  
      117                 return 1;  
      118         }  
      119         return 0;  
    120 120 }  
    121 121  
     
    123 123 {  
    124 124 #ifdef WIN32  
    125       GetTempPath(MAX_PATH, buff);  
    126       testTempPath(buff);  
      125         GetTempPath(MAX_PATH, buff);  
      126         testTempPath(buff);  
    127 127 #else  
    128       static const char *envname[] = {  
    129           "TMPDIR", "TEMP", "TMP", 0  
    130       };  
    131       static const char *dirname[] = {  
    132           "/tmp", "/var/tmp", "/usr/tmp", 0  
    133       };  
    134       int i;  
    135       char *p;  
    136       for ( i=0; envname[i]; i++ ) {  
    137           p = getenv(envname[i]);  
    138           if (p) {  
    139               strcpy(buff, p);  
    140               if (testTempPath(buff))  
    141                   return;  
    142           }  
    143       }  
    144       for ( i=0; dirname[i]; i++ ) {  
    145           strcpy(buff, dirname[i]);  
    146           if (testTempPath(buff))  
    147               return;  
    148       }  
    149       buff[0] = '\0';  
      128         static const char *envname[] = {  
      129                 "TMPDIR", "TEMP", "TMP", 0  
      130         };  
      131         static const char *dirname[] = {  
      132                 "/tmp", "/var/tmp", "/usr/tmp", 0  
      133         };  
      134         int i;  
      135         char *p;  
      136         for ( i=0; envname[i]; i++ ) {  
      137                 p = getenv(envname[i]);  
      138                 if (p) {  
      139                         strcpy(buff, p);  
      140                         if (testTempPath(buff))  
      141                                 return;  
      142                 }  
      143         }  
      144         for ( i=0; dirname[i]; i++ ) {  
      145                 strcpy(buff, dirname[i]);  
      146                 if (testTempPath(buff))  
      147                         return;  
      148         }  
      149         buff[0] = '\0';  
    150 150 #endif  
    151 151 }  
     
    157 157 {  
    158 158 #ifdef WIN32  
    159       char *p;  
      159         char *p;  
    159 159 #endif  
    160       /* Get the archive Path */  
    161       strcpy(f_archivename, archivePath);  
    162       strcat(f_archivename, archiveName);  
    163    
    164       /* Set homepath to where the archive is */  
    165       strcpy(f_homepath, archivePath);  
    166   #ifdef WIN32  
    167       strcpy(f_homepathraw, archivePath);  
    168       for ( p = f_homepath; *p; p++ )  
    169           if (*p == '\\')  
    170               *p = '/';  
      160         /* Get the archive Path */  
      161         strcpy(f_archivename, archivePath);  
      162         strcat(f_archivename, archiveName);  
      163  
      164         /* Set homepath to where the archive is */  
      165         strcpy(f_homepath, archivePath);  
      166 #ifdef WIN32  
      167         strcpy(f_homepathraw, archivePath);  
      168         for ( p = f_homepath; *p; p++ )  
      169                 if (*p == '\\')  
      170                         *p = '/';  
    171 171 #endif  
    172 172  
    173       return 0;  
      173         return 0;  
    173 173 }  
    174 174  
     
    182 182 int openArchive()  
    183 183 {  
    184       int filelen;  
      184         int filelen;  
    184 184  
    185       /* Physically open the file */  
    186       f_fp = fopen(f_archivename, "rb");  
    187       if (f_fp == NULL) {  
    188           VS("Cannot open archive: ");  
    189           VS(f_archivename);  
    190           VS("\n");  
    191           return -1;  
    192       }  
    193    
    194       /* Seek to the Cookie at the end of the file. */  
    195       fseek(f_fp, 0, SEEK_END);  
    196       filelen = ftell(f_fp);  
    197       if (fseek(f_fp, -(int)sizeof(COOKIE), SEEK_END))  
    198       {  
    199           VS(f_archivename);  
    200           VS(" appears to be an invalid archive\n");  
    201           return -1;  
    202       }  
    203    
    204       /* Read the Cookie, and check its MAGIC bytes */  
    205       fread(&f_cookie, sizeof(COOKIE), 1, f_fp);  
    206       if (strncmp(f_cookie.magic, MAGIC, strlen(MAGIC)))  
    207       {  
    208           VS(f_archivename);  
    209           VS(" has bad magic!\n");  
    210           return -1;  
    211       }  
    212    
    213       /* From the cookie, calculate the archive start */  
    214       f_pkgstart = filelen - ntohl(f_cookie.len);  
    215    
    216       /* Read in in the table of contents */  
    217       fseek(f_fp, f_pkgstart + ntohl(f_cookie.TOC), SEEK_SET);  
    218       f_tocbuff = (TOC *) malloc(ntohl(f_cookie.TOClen));  
    219       if (f_tocbuff == NULL)  
    220       {  
    221           FATALERROR("Could not allocate buffer for TOC.");  
    222           return -1;  
    223       }  
    224       fread(f_tocbuff, ntohl(f_cookie.TOClen), 1, f_fp);  
    225       f_tocend = (TOC *) (((char *)f_tocbuff) + ntohl(f_cookie.TOClen));  
    226    
    227       /* Check input file is still ok (should be). */  
    228       if (ferror(f_fp))  
    229       {  
    230           FATALERROR("Error on file");  
    231           return -1;  
    232       }  
    233       return 0;  
      185         /* Physically open the file */  
      186         f_fp = fopen(f_archivename, "rb");  
      187         if (f_fp == NULL) {  
      188                 VS("Cannot open archive: ");  
      189                 VS(f_archivename);  
      190                 VS("\n");  
      191                 return -1;  
      192         }  
      193  
      194         /* Seek to the Cookie at the end of the file. */  
      195         fseek(f_fp, 0, SEEK_END);  
      196         filelen = ftell(f_fp);  
      197         if (fseek(f_fp, -(int)sizeof(COOKIE), SEEK_END))  
      198         {  
      199                 VS(f_archivename);  
      200                 VS(" appears to be an invalid archive\n");  
      201                 return -1;  
      202         }  
      203  
      204         /* Read the Cookie, and check its MAGIC bytes */  
      205         fread(&f_cookie, sizeof(COOKIE), 1, f_fp);  
      206         if (strncmp(f_cookie.magic, MAGIC, strlen(MAGIC)))  
      207         {  
      208                 VS(f_archivename);  
      209                 VS(" has bad magic!\n");  
      210                 return -1;  
      211         }  
      212  
      213         /* From the cookie, calculate the archive start */  
      214         f_pkgstart = filelen - ntohl(f_cookie.len);  
      215  
      216         /* Read in in the table of contents */  
      217         fseek(f_fp, f_pkgstart + ntohl(f_cookie.TOC), SEEK_SET);  
      218         f_tocbuff = (TOC *) malloc(ntohl(f_cookie.TOClen));  
      219         if (f_tocbuff == NULL)  
      220         {  
      221                 FATALERROR("Could not allocate buffer for TOC.");  
      222                 return -1;  
      223         }  
      224         fread(f_tocbuff, ntohl(f_cookie.TOClen), 1, f_fp);  
      225         f_tocend = (TOC *) (((char *)f_tocbuff) + ntohl(f_cookie.TOClen));  
      226  
      227         /* Check input file is still ok (should be). */  
      228         if (ferror(f_fp))  
      229         {  
      230                 FATALERROR("Error on file");  
      231                 return -1;  
      232         }  
      233         return 0;  
    234 234 }  
    235 235 #ifdef WIN32  
     
    238 238 {  
    239 239     /* Get all of the entry points that we are interested in */  
    240       GETVAR(dll, Py_NoSiteFlag);  
    241       GETVAR(dll, Py_OptimizeFlag);  
    242       GETVAR(dll, Py_VerboseFlag);  
    243       GETPROC(dll, Py_Initialize);  
    244       GETPROC(dll, Py_Finalize);  
    245       GETPROC(dll, Py_CompileString);  
    246       GETPROC(dll, PyImport_ExecCodeModule);  
    247       GETPROC(dll, PyRun_SimpleString);  
    248       GETPROC(dll, PySys_SetArgv);  
    249       GETPROC(dll, Py_SetProgramName);  
    250       GETPROC(dll, PyImport_ImportModule);  
    251       GETPROC(dll, PyImport_AddModule);  
    252       GETPROC(dll, PyObject_SetAttrString);  
    253       GETPROC(dll, PyList_New);  
    254       GETPROC(dll, PyList_Append);  
    255       GETPROC(dll, Py_BuildValue);  
    256       GETPROC(dll, PyFile_FromFile);  
    257       GETPROC(dll, PyObject_CallFunction);  
    258       GETPROC(dll, PyModule_GetDict);  
    259       GETPROC(dll, PyDict_GetItemString);  
    260       GETPROC(dll, PyErr_Clear);  
    261       GETPROC(dll, PyErr_Occurred);  
    262       GETPROC(dll, PyErr_Print);  
    263       GETPROC(dll, PyObject_CallObject);  
    264       GETPROC(dll, PyObject_CallMethod);  
      240         GETVAR(dll, Py_NoSiteFlag);  
      241         GETVAR(dll, Py_OptimizeFlag);  
      242         GETVAR(dll, Py_VerboseFlag);  
      243         GETPROC(dll, Py_Initialize);  
      244         GETPROC(dll, Py_Finalize);  
      245         GETPROC(dll, Py_CompileString);  
      246         GETPROC(dll, PyImport_ExecCodeModule);  
      247         GETPROC(dll, PyRun_SimpleString);  
      248         GETPROC(dll, PySys_SetArgv);  
      249         GETPROC(dll, Py_SetProgramName);  
      250         GETPROC(dll, PyImport_ImportModule);  
      251         GETPROC(dll, PyImport_AddModule);  
      252         GETPROC(dll, PyObject_SetAttrString);  
      253         GETPROC(dll, PyList_New);  
      254         GETPROC(dll, PyList_Append);  
      255         GETPROC(dll, Py_BuildValue);  
      256         GETPROC(dll, PyFile_FromFile);  
      257         GETPROC(dll, PyObject_CallFunction);  
      258         GETPROC(dll, PyModule_GetDict);  
      259         GETPROC(dll, PyDict_GetItemString);  
      260         GETPROC(dll, PyErr_Clear);  
      261         GETPROC(dll, PyErr_Occurred);  
      262         GETPROC(dll, PyErr_Print);  
      263         GETPROC(dll, PyObject_CallObject);  
      264         GETPROC(dll, PyObject_CallMethod);  
    265 265         if (ntohl(f_cookie.pyvers) >= 21) {  
    266 266                 GETPROC(dll, PySys_AddWarnOption);  
     
    281 281         GETPROC(dll, PyInt_AsLong);  
    282 282         GETPROC(dll, PySys_SetObject);  
    283       return 0;  
      283         return 0;  
    283 283 }  
    284 284 #endif  
     
    291 291 {  
    292 292 #ifdef WIN32  
    293       HINSTANCE dll;  
    294       char dllpath[_MAX_PATH + 1];  
      293         HINSTANCE dll;  
      294         char dllpath[_MAX_PATH + 1];  
    295 295  
    296       /* Determine the path */  
    297       sprintf(dllpath, "%spython%02d.dll", f_homepathraw, ntohl(f_cookie.pyvers));  
      296         /* Determine the path */  
      297         sprintf(dllpath, "%spython%02d.dll", f_homepathraw, ntohl(f_cookie.pyvers));  
    298 298  
    299       /* Load the DLL */  
    300       dll = LoadLibraryEx(dllpath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);   
    301       if (dll) {  
    302           VS(dllpath);  
    303           VS("\n");  
    304       }  
    305       else {  
    306           sprintf(dllpath, "%spython%02d.dll", f_temppathraw, ntohl(f_cookie.pyvers));  
    307           dll = LoadLibraryEx(dllpath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );  
    308           if (dll) {  
    309               VS(dllpath);  
    310               VS("\n");  
    311           }  
    312       }  
    313       if (dll == 0) {  
    314           FATALERROR("Error loading Python DLL: ");  
    315           FATALERROR(dllpath);  
    316           FATALERROR("\n");  
    317           return -1;  
    318       }  
      299         /* Load the DLL */  
      300         dll = LoadLibraryEx(dllpath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);   
      301         if (dll) {  
      302                 VS(dllpath);  
      303                 VS("\n");  
      304         }  
      305         else {  
      306                 sprintf(dllpath, "%spython%02d.dll", f_temppathraw, ntohl(f_cookie.pyvers));  
      307                 dll = LoadLibraryEx(dllpath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );  
      308                 if (dll) {  
      309                         VS(dllpath);  
      310                         VS("\n");  
      311                 }  
      312         }  
      313         if (dll == 0) {  
      314                 FATALERROR("Error loading Python DLL: ");  
      315                 FATALERROR(dllpath);  
      316                 FATALERROR("\n");  
      317                 return -1;  
      318         }  
    319 319  
    320       mapNames(dll);  
      320         mapNames(dll);  
    320 320 #endif  
    321 321  
    322       return 0;  
      322         return 0;  
    322 322 }  
    323 323 #ifdef WIN32  
     
    331 331 int attachPython(int *loadedNew)  
    332 332 {  
    333       HMODULE dll;  
    334       char nm[_MAX_PATH + 1];  
      333         HMODULE dll;  
      334         char nm[_MAX_PATH + 1];  
    335 335  
    336       /* Get python's name */  
    337       sprintf(nm, "python%02d.dll", ntohl(f_cookie.pyvers));  
      336         /* Get python's name */  
      337         sprintf(nm, "python%02d.dll", ntohl(f_cookie.pyvers));  
    338 338  
    339       /* See if it's loaded */  
    340       dll = GetModuleHandle(nm);   
      339         /* See if it's loaded */  
      340         dll = GetModuleHandle(nm);   
    341 341         if (dll == 0) {  
    342 342                 *loadedNew = 1;  
     
    354 354 TOC *incrementTocPtr(TOC* ptoc)  
    355 355 {  
    356       TOC *result = (TOC*)((char *)ptoc + ntohl(ptoc->structlen));  
    357       if (result < f_tocbuff) {  
    358           FATALERROR("Cannot read Table of Contents.\n");  
    359           return f_tocend;  
    360       }  
    361       return result;  
      356         TOC *result = (TOC*)((char *)ptoc + ntohl(ptoc->structlen));  
      357         if (result < f_tocbuff) {  
      358                 FATALERROR("Cannot read Table of Contents.\n");  
      359                 return f_tocend;  
      360         }  
      361         return result;  
    362 362 }  
    363 363 /*  
     
    383 383 {  
    384 384         int unbuffered = 0;  
    385       TOC *ptoc = f_tocbuff;  
    386       while (ptoc < f_tocend) {  
    387           if (ptoc->typcd == 'o') {  
    388               VS(ptoc->name);  
    389               VS("\n");  
    390               switch (ptoc->name[0]) {  
    391               case 'v':  
      385         TOC *ptoc = f_tocbuff;  
      386         while (ptoc < f_tocend) {  
      387                 if (ptoc->typcd == 'o') {  
      388                         VS(ptoc->name);  
      389                         VS("\n");  
      390                         switch (ptoc->name[0]) {  
      391                         case 'v':  
    392 392 #if defined  WIN32  
    393                   *Py_VerboseFlag = 1;  
      393                                 *Py_VerboseFlag = 1;  
    393 393 #else  
    394                   Py_VerboseFlag = 1;  
      394                                 Py_VerboseFlag = 1;  
    394 394 #endif  
    395                   break;  
    396               case 'u':  
    397                   unbuffered = 1;  
    398                   break;  
      395                         break;  
      396                         case 'u':  
      397                                 unbuffered = 1;  
      398                         break;  
    399 399 #ifdef HAVE_WARNINGS  
    400               case 'W':  
    401                   if (ntohl(f_cookie.pyvers) >= 21) {  
    402                           PySys_AddWarnOption(&ptoc->name[2]);  
    403                   }  
    404                   break;  
      400                         case 'W':  
      401                                 if (ntohl(f_cookie.pyvers) >= 21) {  
      402                                         PySys_AddWarnOption(&ptoc->name[2]);  
      403                                 }  
      404                         break;  
    405 405 #endif  
    406               case 's':  
      406                         case 's':  
    406 406 #if defined  WIN32  
    407                   *Py_NoSiteFlag = 0;  
      407                                 *Py_NoSiteFlag = 0;  
    407 407 #else  
    408                   Py_NoSiteFlag = 0;  
      408                                 Py_NoSiteFlag = 0;  
    408 408 #endif  
    409                   break;  
    410               case 'O':  
      409                         break;  
      410                         case 'O':  
    411 411 #if defined  WIN32  
    412                   *Py_OptimizeFlag = 1;  
      412                                 *Py_OptimizeFlag = 1;  
    412 412 #else  
    413                   Py_OptimizeFlag = 1;  
      413                                 Py_OptimizeFlag = 1;  
    413 413 #endif  
    414               break;  
    415               }  
      414                         break;  
      415                         }  
      416                 }  
      417                 ptoc = incrementTocPtr(ptoc);  
    416 418         }  
    417           ptoc = incrementTocPtr(ptoc);  
    418       }  
    419       if (unbuffered) {  
      419         if (unbuffered) {  
    420 420 #ifdef WIN32  
    421           _setmode(fileno(stdin), O_BINARY);  
    422           _setmode(fileno(stdout), O_BINARY);  
      421                 _setmode(fileno(stdin), O_BINARY);  
      422                 _setmode(fileno(stdout), O_BINARY);  
    423 423 #else  
    424           fflush(stdout);  
    425           fflush(stderr);  
      424                 fflush(stdout);  
      425                 fflush(stderr);  
    426 426 #ifdef HAVE_SETVBUF  
    427           setvbuf(stdin, (char *)NULL, _IONBF, 0);  
    428           setvbuf(stdout, (char *)NULL, _IONBF, 0);  
    429           setvbuf(stderr, (char *)NULL, _IONBF, 0);  
    430   #else  
    431           setbuf(stdin, (char *)NULL);  
    432           setbuf(stdout, (char *)NULL);  
    433           setbuf(stderr, (char *)NULL);  
      427                 setvbuf(stdin, (char *)NULL, _IONBF, 0);  
      428                 setvbuf(stdout, (char *)NULL, _IONBF, 0);  
      429                 setvbuf(stderr, (char *)NULL, _IONBF, 0);  
      430 #else  
      431                 setbuf(stdin, (char *)NULL);  
      432                 setbuf(stdout, (char *)NULL);  
      433                 setbuf(stderr, (char *)NULL);  
    434 434 #endif  
    435 435 #endif  
    436       }  
    437       return 0;  
      436         }  
      437         return 0;  
    438 438 }  
    439 439 /*  
     
    450 450 {  
    451 451     /* Set PYTHONPATH so dynamic libs will load */  
    452       static char pypath[2*_MAX_PATH + 14];  
    453       int pathlen = 1;  
    454       int i;  
    455       char cmd[80];  
    456       char tmp[_MAX_PATH+1];  
    457       PyObject *py_argv;  
    458       PyObject *val;  
    459       PyObject *sys;  
    460            
    461       VS("Manipulating evironment\n");  
    462       if (f_workpath && (strcmp(f_workpath, f_homepath) != 0)) {  
    463           strcpy(pypath, "PYTHONPATH=");  
    464           strcat(pypath, f_workpath);  
    465           pypath[strlen(pypath)-1] = '\0';  
    466           strcat(pypath, PATHSEP);  
    467           strcat(pypath, f_homepath);  
    468           pathlen = 2;  
    469       }  
    470       else {  
    471           /* never extracted anything, or extracted to homepath - homepath will do */  
    472           strcpy(pypath, "PYTHONPATH=");  
    473           strcat(pypath, f_homepath);  
    474       }  
    475       /* don't chop off SEP if root directory */  
    476   #ifdef WIN32  
    477       if (strlen(pypath) > 14)     
    478   #else  
    479       if (strlen(pypath) > 12)  
    480   #endif  
    481           pypath[strlen(pypath)-1] = '\0';  
    482    
    483       putenv(pypath);  
    484       VS(pypath);  
    485       VS("\n");  
    486       /* Clear out PYTHONHOME to avoid clashing with any installation */  
      452         static char pypath[2*_MAX_PATH + 14];  
      453         int pathlen = 1;  
      454         int i;  
      455         char cmd[80];  
      456         char tmp[_MAX_PATH+1];  
      457         PyObject *py_argv;  
      458         PyObject *val;  
      459         PyObject *sys;  
      460  
      461         VS("Manipulating evironment\n");  
      462         if (f_workpath && (strcmp(f_workpath, f_homepath) != 0)) {  
      463                 strcpy(pypath, "PYTHONPATH=");  
      464                 strcat(pypath, f_workpath);  
      465                 pypath[strlen(pypath)-1] = '\0';  
      466                 strcat(pypath, PATHSEP);  
      467                 strcat(pypath, f_homepath);  
      468                 pathlen = 2;  
      469         }  
      470         else {  
      471                 /* never extracted anything, or extracted to homepath - homepath will do */  
      472                 strcpy(pypath, "PYTHONPATH=");  
      473                 strcat(pypath, f_homepath);  
      474         }  
      475         /* don't chop off SEP if root directory */  
    487 476 #ifdef WIN32  
    488       putenv("PYTHONHOME=");  
      477         if (strlen(pypath) > 14)  
      478 #else  
      479         if (strlen(pypath) > 12)  
      480 #endif  
      481                 pypath[strlen(pypath)-1] = '\0';  
      482  
      483         putenv(pypath);  
      484         VS(pypath);  
      485         VS("\n");  
      486         /* Clear out PYTHONHOME to avoid clashing with any installation */  
      487 #ifdef WIN32  
      488         putenv("PYTHONHOME=");  
    489 489 #endif  
    490 490  
    491       /* Start python. */  
    492       /* VS("Loading python\n"); */  
      491         /* Start python. */  
      492         /* VS("Loading python\n"); */  
    493 493 #if defined  WIN32  
    494       *Py_NoSiteFlag = 1; /* maybe changed to 0 by setRuntimeOptions() */  
      494         *Py_NoSiteFlag = 1;     /* maybe changed to 0 by setRuntimeOptions() */  
    494 494 #else  
    495       Py_NoSiteFlag = 1;  
      495         Py_NoSiteFlag = 1;  
    495 495 #endif  
    496       setRuntimeOptions();  
      496         setRuntimeOptions();  
    496 496 #ifdef WIN32  
    497       Py_SetProgramName(f_archivename); /*XXX*/  
      497         Py_SetProgramName(f_archivename); /*XXX*/  
    497 497 #endif  
    498       Py_Initialize();  
    499    
    500       /* Set sys.path */  
    501       /* VS("Manipulating Python's sys.path\n"); */  
    502       strcpy(tmp, f_homepath);  
    503       tmp[strlen(tmp)-1] = '\0';  
    504       PyRun_SimpleString("import sys\n");  
    505       PyRun_SimpleString("while sys.path:\n del sys.path[0]\n");  
    506       sprintf(cmd, "sys.path.append('%s')", tmp);  
    507       PyRun_SimpleString (cmd);  
    508       if (pathlen == 2) {  
    509           strcpy(tmp, f_workpath);  
    510           tmp[strlen(tmp)-1] = '\0';  
    511           sprintf(cmd, "sys.path.insert(0, '%s')", tmp);  
    512           PyRun_SimpleString(cmd);  
    513       }  
    514    
    515       /* Set argv[0] to be the archiveName */  
    516       py_argv = PyList_New(0);  
    517       val = Py_BuildValue("s", f_archivename);  
    518       PyList_Append(py_argv, val);  
    519       for (i = 1; i < argc; ++i) {  
    520           val = Py_BuildValue ("s", argv[i]);  
    521           PyList_Append (py_argv, val);  
    522       }  
    523       sys = PyImport_ImportModule("sys");  
    524       /* VS("Setting sys.argv\n"); */  
    525       PyObject_SetAttrString(sys, "argv", py_argv);  
    526    
    527       /* Check for a python error */  
    528       if (PyErr_Occurred())  
    529       {  
    530           FATALERROR("Error detected starting Python VM.");  
    531           return -1;  
    532       }  
      498         Py_Initialize();  
      499  
      500         /* Set sys.path */  
      501         /* VS("Manipulating Python's sys.path\n"); */  
      502         strcpy(tmp, f_homepath);  
      503         tmp[strlen(tmp)-1] = '\0';  
      504         PyRun_SimpleString("import sys\n");  
      505         PyRun_SimpleString("while sys.path:\n del sys.path[0]\n");  
      506         sprintf(cmd, "sys.path.append('%s')", tmp);  
      507         PyRun_SimpleString (cmd);  
      508         if (pathlen == 2) {  
      509                 strcpy(tmp, f_workpath);  
      510                 tmp[strlen(tmp)-1] = '\0';  
      511                 sprintf(cmd, "sys.path.insert(0, '%s')", tmp);  
      512                 PyRun_SimpleString(cmd);  
      513         }  
      514  
      515         /* Set argv[0] to be the archiveName */  
      516         py_argv = PyList_New(0);  
      517         val = Py_BuildValue("s", f_archivename);  
      518         PyList_Append(py_argv, val);  
      519         for (i = 1; i < argc; ++i) {  
      520                 val = Py_BuildValue ("s", argv[i]);  
      521                 PyList_Append (py_argv, val);  
      522         }  
      523         sys = PyImport_ImportModule("sys");  
      524         /* VS("Setting sys.argv\n"); */  
      525         PyObject_SetAttrString(sys, "argv", py_argv);  
      526  
      527         /* Check for a python error */  
      528         if (PyErr_Occurred())  
      529         {  
      530                 FATALERROR("Error detected starting Python VM.");  
      531                 return -1;  
      532         }  
    533 533  
    534       return 0;  
      534         return 0;  
    534 534 }  
    535 535  
     
    552 552         PyObject *mod;  
    553 553  
    554       VS("importing modules from CArchive\n");  
      554         VS("importing modules from CArchive\n");  
    554 554  
    555       /* Get the Python function marshall.load  
    556        * Here we collect some reference to PyObject that we don't dereference  
    557        * Doesn't matter because the objects won't be going away anyway.  
    558            */  
    559       marshal = PyImport_ImportModule("marshal");  
    560       marshaldict = PyModule_GetDict(marshal);  
    561       loadfunc = PyDict_GetItemString(marshaldict, "load");  
    562    
    563       /* Make a Python file object from f_fp */  
    564       pyfile = PyFile_FromFile(f_fp, f_archivename, "rb+", 0);  
    565    
    566       /* Iterate through toc looking for module entries (type 'm')  
    567        * this is normally just bootstrap stuff (archive and iu)  
    568            */  
    569       ptoc = f_tocbuff;  
    570       while (ptoc < f_tocend) {  
    571           if (ptoc->typcd == 'm' || ptoc->typcd == 'M')  
    572           {  
    573               VS(ptoc->name);  
    574               VS("\n");  
    575               /* Go to start of Python module (start + 8) and load the code object */  
    576               fseek(f_fp, f_pkgstart + ntohl(ptoc->pos) + 8, SEEK_SET);  
    577               co = PyObject_CallFunction(loadfunc, "O", pyfile);  
    578               mod = PyImport_ExecCodeModule(ptoc->name, co);  
    579                
    580