| 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';
|
| 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 = '/';
|
| 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;
|
| 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);
|
| 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 |
}
|
| 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);
|
| 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 */
|
| 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 |
}
|
| 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 |
|
|