srcanamdw/codescanner/pyinstaller/source/common/launch.c
author noe\swadi
Thu, 18 Feb 2010 12:29:02 +0530
changeset 1 22878952f6e2
permissions -rw-r--r--
Committing the CodeScanner Core tool This component has been moved from the StaticAnaApps package. BUG : 5889 (http://developer.symbian.org/webbugs/show_bug.cgi?id=5889).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     1
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     2
 * Launch a python module from an archive.   
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     3
 * Copyright (C) 2005, Giovanni Bajo
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     4
 * Based on previous work under copyright (c) 2002 McMillan Enterprises, Inc.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     5
 *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     7
 * modify it under the terms of the GNU General Public License
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     8
 * as published by the Free Software Foundation; either version 2
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     9
 * of the License, or (at your option) any later version.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    10
 *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    11
 * In addition to the permissions in the GNU General Public License, the
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    12
 * authors give you unlimited permission to link or embed the compiled
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    13
 * version of this file into combinations with other programs, and to
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    14
 * distribute those combinations without any restriction coming from the
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    15
 * use of this file. (The General Public License restrictions do apply in
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    16
 * other respects; for example, they cover modification of the file, and
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    17
 * distribution when not linked into a combine executable.)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    18
 *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    19
 * This program is distributed in the hope that it will be useful,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    22
 * GNU General Public License for more details.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    23
 *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    24
 * You should have received a copy of the GNU General Public License
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    25
 * along with this program; if not, write to the Free Software
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    26
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    27
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    28
#include <stdio.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    29
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    30
 #include <windows.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    31
 #include <direct.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    32
 #include <process.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    33
 #include <io.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    34
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    35
 #include <unistd.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    36
 #include <fcntl.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    37
 #include <dirent.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    38
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    39
#include <stdio.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    40
#include <sys/types.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    41
#include <sys/stat.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    42
#include "launch.h"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    43
#ifndef NOZLIB
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    44
#include "zlib.h"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    45
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    46
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    47
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    48
 * Python Entry point declarations (see macros in launch.h).
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    49
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    50
DECLVAR(Py_NoSiteFlag);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    51
DECLVAR(Py_OptimizeFlag);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    52
DECLVAR(Py_VerboseFlag);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    53
DECLPROC(Py_Initialize);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    54
DECLPROC(Py_Finalize);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    55
DECLPROC(Py_CompileString);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    56
DECLPROC(PyImport_ExecCodeModule);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    57
DECLPROC(PyRun_SimpleString);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    58
DECLPROC(PySys_SetArgv);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    59
DECLPROC(Py_SetProgramName);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    60
DECLPROC(PyImport_ImportModule);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    61
DECLPROC(PyImport_AddModule);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    62
DECLPROC(PyObject_SetAttrString);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    63
DECLPROC(PyList_New);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    64
DECLPROC(PyList_Append);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    65
DECLPROC(Py_BuildValue);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    66
DECLPROC(PyFile_FromString);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    67
DECLPROC(PyString_FromStringAndSize);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    68
DECLPROC(PyObject_CallFunction);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    69
DECLPROC(PyModule_GetDict);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    70
DECLPROC(PyDict_GetItemString);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    71
DECLPROC(PyErr_Clear);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    72
DECLPROC(PyErr_Occurred);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    73
DECLPROC(PyErr_Print);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    74
DECLPROC(PyObject_CallObject);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    75
DECLPROC(PyObject_CallMethod);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    76
DECLPROC(PySys_AddWarnOption);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    77
DECLPROC(PyEval_InitThreads);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    78
DECLPROC(PyEval_AcquireThread);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    79
DECLPROC(PyEval_ReleaseThread);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    80
DECLPROC(PyEval_AcquireLock);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    81
DECLPROC(PyEval_ReleaseLock);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    82
DECLPROC(PyThreadState_Swap);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    83
DECLPROC(PyThreadState_New);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    84
DECLPROC(PyThreadState_Clear);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    85
DECLPROC(PyThreadState_Delete);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    86
DECLPROC(PyInterpreterState_New);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    87
DECLPROC(Py_NewInterpreter);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    88
DECLPROC(Py_EndInterpreter);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    89
DECLPROC(PyInt_AsLong);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    90
DECLPROC(PySys_SetObject);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    91
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    92
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    93
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    94
#define PATHSEP ";"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    95
#define SEP "/"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    96
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    97
#define PATHSEP ":"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    98
#define SEP "/"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    99
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   100
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   101
/* File Local Variables (all start with f_) */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   102
static char f_archivename[_MAX_PATH+1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   103
static char f_homepath[_MAX_PATH+1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   104
static char f_temppath[_MAX_PATH+1] = { '\0' };
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   105
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   106
static char f_temppathraw[MAX_PATH+1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   107
static char f_homepathraw[_MAX_PATH+1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   108
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   109
static char *f_workpath = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   110
static FILE *f_fp;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   111
static int f_pkgstart;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   112
static TOC *f_tocbuff = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   113
static TOC *f_tocend = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   114
static COOKIE f_cookie;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   115
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   116
unsigned char *extract(TOC *ptoc);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   117
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   118
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   119
 * The functions in this file defined in reverse order so that forward 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   120
 * declarations are not necessary.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   121
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   122
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   123
int testTempPath(char *buff)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   124
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   125
	char base[16];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   126
	int n;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   127
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   128
	n = strlen(buff);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   129
	if ( buff[n-1] == '/' || buff[n-1] == '\\' )
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   130
		sprintf(base, "_MEI%d", getpid());
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   131
	else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   132
		sprintf(base, "%s_MEI%d", SEP, getpid());
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   133
	strcat(buff, base);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   134
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   135
	if (mkdir(buff) == 0) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   136
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   137
	if (mkdir(buff, 0700) == 0) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   138
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   139
		strcat(buff, SEP);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   140
		return 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   141
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   142
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   143
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   144
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   145
void getTempPath(char *buff)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   146
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   147
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   148
	GetTempPath(MAX_PATH, buff);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   149
	testTempPath(buff);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   150
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   151
	static const char *envname[] = {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   152
		"TMPDIR", "TEMP", "TMP", 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   153
	};
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   154
	static const char *dirname[] = {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   155
		"/tmp", "/var/tmp", "/usr/tmp", 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   156
	};
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   157
	int i;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   158
	char *p;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   159
	for ( i=0; envname[i]; i++ ) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   160
		p = getenv(envname[i]);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   161
		if (p) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   162
			strcpy(buff, p);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   163
			if (testTempPath(buff))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   164
				return;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   165
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   166
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   167
	for ( i=0; dirname[i]; i++ ) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   168
		strcpy(buff, dirname[i]);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   169
		if (testTempPath(buff))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   170
			return;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   171
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   172
	buff[0] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   173
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   174
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   175
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   176
 * Set up paths required by rest of this module
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   177
 * Sets f_archivename, f_homepath
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   178
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   179
int setPaths(char const * archivePath, char const * archiveName)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   180
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   181
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   182
	char *p;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   183
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   184
	/* Get the archive Path */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   185
	strcpy(f_archivename, archivePath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   186
	strcat(f_archivename, archiveName);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   187
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   188
	/* Set homepath to where the archive is */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   189
	strcpy(f_homepath, archivePath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   190
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   191
	strcpy(f_homepathraw, archivePath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   192
	for ( p = f_homepath; *p; p++ )
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   193
		if (*p == '\\')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   194
			*p = '/';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   195
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   196
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   197
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   198
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   199
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   200
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   201
/* 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   202
 * Open the archive
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   203
 * Sets f_archiveFile, f_pkgstart, f_tocbuff and f_cookie.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   204
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   205
int openArchive()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   206
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   207
	int filelen;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   208
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   209
	/* Physically open the file */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   210
	f_fp = fopen(f_archivename, "rb");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   211
	if (f_fp == NULL) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   212
		VS("Cannot open archive: ");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   213
		VS(f_archivename);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   214
		VS("\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   215
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   216
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   217
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   218
	/* Seek to the Cookie at the end of the file. */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   219
	fseek(f_fp, 0, SEEK_END);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   220
	filelen = ftell(f_fp);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   221
	if (fseek(f_fp, -(int)sizeof(COOKIE), SEEK_END)) 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   222
	{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   223
		VS(f_archivename);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   224
		VS(" appears to be an invalid archive\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   225
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   226
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   227
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   228
	/* Read the Cookie, and check its MAGIC bytes */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   229
	fread(&f_cookie, sizeof(COOKIE), 1, f_fp);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   230
	if (strncmp(f_cookie.magic, MAGIC, strlen(MAGIC))) 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   231
	{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   232
		VS(f_archivename);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   233
		VS(" has bad magic!\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   234
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   235
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   236
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   237
	/* From the cookie, calculate the archive start */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   238
	f_pkgstart = filelen - ntohl(f_cookie.len);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   239
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   240
	/* Read in in the table of contents */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   241
	fseek(f_fp, f_pkgstart + ntohl(f_cookie.TOC), SEEK_SET);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   242
	f_tocbuff = (TOC *) malloc(ntohl(f_cookie.TOClen));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   243
	if (f_tocbuff == NULL) 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   244
	{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   245
		FATALERROR("Could not allocate buffer for TOC.");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   246
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   247
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   248
	fread(f_tocbuff, ntohl(f_cookie.TOClen), 1, f_fp);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   249
	f_tocend = (TOC *) (((char *)f_tocbuff) + ntohl(f_cookie.TOClen));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   250
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   251
	/* Check input file is still ok (should be). */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   252
	if (ferror(f_fp))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   253
	{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   254
		FATALERROR("Error on file");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   255
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   256
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   257
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   258
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   259
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   260
int mapNames(HMODULE dll)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   261
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   262
    /* Get all of the entry points that we are interested in */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   263
	GETVAR(dll, Py_NoSiteFlag);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   264
	GETVAR(dll, Py_OptimizeFlag);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   265
	GETVAR(dll, Py_VerboseFlag);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   266
	GETPROC(dll, Py_Initialize);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   267
	GETPROC(dll, Py_Finalize);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   268
	GETPROC(dll, Py_CompileString);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   269
	GETPROC(dll, PyImport_ExecCodeModule);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   270
	GETPROC(dll, PyRun_SimpleString);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   271
	GETPROC(dll, PySys_SetArgv);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   272
	GETPROC(dll, Py_SetProgramName);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   273
	GETPROC(dll, PyImport_ImportModule);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   274
	GETPROC(dll, PyImport_AddModule);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   275
	GETPROC(dll, PyObject_SetAttrString);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   276
	GETPROC(dll, PyList_New);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   277
	GETPROC(dll, PyList_Append);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   278
	GETPROC(dll, Py_BuildValue);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   279
	GETPROC(dll, PyFile_FromString);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   280
	GETPROC(dll, PyString_FromStringAndSize);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   281
	GETPROC(dll, PyObject_CallFunction);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   282
	GETPROC(dll, PyModule_GetDict);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   283
	GETPROC(dll, PyDict_GetItemString);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   284
	GETPROC(dll, PyErr_Clear);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   285
	GETPROC(dll, PyErr_Occurred);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   286
	GETPROC(dll, PyErr_Print);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   287
	GETPROC(dll, PyObject_CallObject);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   288
	GETPROC(dll, PyObject_CallMethod);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   289
	if (ntohl(f_cookie.pyvers) >= 21) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   290
		GETPROC(dll, PySys_AddWarnOption);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   291
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   292
	GETPROC(dll, PyEval_InitThreads);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   293
	GETPROC(dll, PyEval_AcquireThread);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   294
	GETPROC(dll, PyEval_ReleaseThread);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   295
	GETPROC(dll, PyEval_AcquireLock);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   296
	GETPROC(dll, PyEval_ReleaseLock);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   297
	GETPROC(dll, PyThreadState_Swap);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   298
	GETPROC(dll, PyThreadState_New);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   299
	GETPROC(dll, PyThreadState_Clear);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   300
	GETPROC(dll, PyThreadState_Delete);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   301
	GETPROC(dll, PyInterpreterState_New);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   302
	GETPROC(dll, Py_NewInterpreter);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   303
	GETPROC(dll, Py_EndInterpreter);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   304
	GETPROC(dll, PyErr_Print);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   305
	GETPROC(dll, PyInt_AsLong);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   306
	GETPROC(dll, PySys_SetObject);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   307
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   308
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   309
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   310
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   311
 * Load the Python DLL, and get all of the necessary entry points
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   312
 * Windows only (dynamic load)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   313
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   314
int loadPython()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   315
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   316
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   317
	HINSTANCE dll;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   318
	char dllpath[_MAX_PATH + 1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   319
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   320
	/* Determine the path */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   321
	sprintf(dllpath, "%spython%02d.dll", f_homepathraw, ntohl(f_cookie.pyvers));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   322
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   323
	/* Load the DLL */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   324
	dll = LoadLibraryEx(dllpath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);  
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   325
	if (dll) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   326
		VS(dllpath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   327
		VS("\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   328
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   329
	else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   330
		sprintf(dllpath, "%spython%02d.dll", f_temppathraw, ntohl(f_cookie.pyvers));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   331
		dll = LoadLibraryEx(dllpath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH );
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   332
		if (dll) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   333
			VS(dllpath); 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   334
			VS("\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   335
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   336
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   337
	if (dll == 0) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   338
		FATALERROR("Error loading Python DLL: ");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   339
		FATALERROR(dllpath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   340
		FATALERROR("\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   341
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   342
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   343
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   344
	mapNames(dll);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   345
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   346
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   347
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   348
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   349
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   350
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   351
 * use this from a dll instead of loadPython()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   352
 * it will attach to an existing pythonXX.dll,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   353
 * or load one if needed.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   354
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   355
int attachPython(int *loadedNew)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   356
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   357
	HMODULE dll;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   358
	char nm[_MAX_PATH + 1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   359
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   360
	/* Get python's name */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   361
	sprintf(nm, "python%02d.dll", ntohl(f_cookie.pyvers));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   362
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   363
	/* See if it's loaded */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   364
	dll = GetModuleHandle(nm);  
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   365
	if (dll == 0) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   366
		*loadedNew = 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   367
		return loadPython();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   368
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   369
	mapNames(dll);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   370
	*loadedNew = 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   371
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   372
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   373
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   374
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   375
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   376
 * Return pointer to next toc entry.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   377
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   378
TOC *incrementTocPtr(TOC* ptoc)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   379
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   380
	TOC *result = (TOC*)((char *)ptoc + ntohl(ptoc->structlen));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   381
	if (result < f_tocbuff) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   382
		FATALERROR("Cannot read Table of Contents.\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   383
		return f_tocend;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   384
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   385
	return result;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   386
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   387
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   388
 * external API for iterating TOCs
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   389
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   390
TOC *getFirstTocEntry(void)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   391
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   392
	return f_tocbuff;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   393
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   394
TOC *getNextTocEntry(TOC *entry)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   395
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   396
	TOC *rslt = (TOC*)((char *)entry + ntohl(entry->structlen));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   397
	if (rslt >= f_tocend)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   398
		return NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   399
	return rslt;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   400
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   401
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   402
 * A toc entry of type 'o' holds runtime options
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   403
 * toc->name is the arg
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   404
 * this is so you can freeze in command line args to Python
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   405
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   406
int setRuntimeOptions(void)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   407
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   408
	int unbuffered = 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   409
	TOC *ptoc = f_tocbuff;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   410
	while (ptoc < f_tocend) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   411
		if (ptoc->typcd == 'o') {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   412
			VS(ptoc->name);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   413
			VS("\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   414
			switch (ptoc->name[0]) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   415
			case 'v':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   416
#if defined  WIN32 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   417
				*Py_VerboseFlag = 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   418
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   419
				Py_VerboseFlag = 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   420
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   421
			break;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   422
			case 'u':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   423
				unbuffered = 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   424
			break;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   425
#ifdef HAVE_WARNINGS
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   426
			case 'W':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   427
				if (ntohl(f_cookie.pyvers) >= 21) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   428
					PySys_AddWarnOption(&ptoc->name[2]);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   429
				}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   430
			break;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   431
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   432
			case 's':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   433
#if defined  WIN32 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   434
				*Py_NoSiteFlag = 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   435
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   436
				Py_NoSiteFlag = 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   437
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   438
			break;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   439
			case 'O':
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   440
#if defined  WIN32 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   441
				*Py_OptimizeFlag = 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   442
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   443
				Py_OptimizeFlag = 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   444
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   445
			break;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   446
			}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   447
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   448
		ptoc = incrementTocPtr(ptoc);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   449
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   450
	if (unbuffered) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   451
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   452
		_setmode(fileno(stdin), O_BINARY);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   453
		_setmode(fileno(stdout), O_BINARY);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   454
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   455
		fflush(stdout);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   456
		fflush(stderr);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   457
#ifdef HAVE_SETVBUF
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   458
		setvbuf(stdin, (char *)NULL, _IONBF, 0);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   459
		setvbuf(stdout, (char *)NULL, _IONBF, 0);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   460
		setvbuf(stderr, (char *)NULL, _IONBF, 0);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   461
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   462
		setbuf(stdin, (char *)NULL);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   463
		setbuf(stdout, (char *)NULL);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   464
		setbuf(stderr, (char *)NULL);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   465
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   466
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   467
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   468
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   469
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   470
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   471
 * Start python - return 0 on success
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   472
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   473
int startPython(int argc, char *argv[])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   474
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   475
    /* Set PYTHONPATH so dynamic libs will load */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   476
	static char pypath[2*_MAX_PATH + 14];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   477
	int pathlen = 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   478
	int i;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   479
	char cmd[_MAX_PATH+1+80];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   480
	char tmp[_MAX_PATH+1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   481
	PyObject *py_argv;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   482
	PyObject *val;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   483
	PyObject *sys;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   484
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   485
	VS("Manipulating evironment\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   486
	if (f_workpath && (strcmp(f_workpath, f_homepath) != 0)) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   487
		strcpy(pypath, "PYTHONPATH=");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   488
		strcat(pypath, f_workpath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   489
		pypath[strlen(pypath)-1] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   490
		strcat(pypath, PATHSEP);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   491
		strcat(pypath, f_homepath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   492
		pathlen = 2;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   493
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   494
	else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   495
		/* never extracted anything, or extracted to homepath - homepath will do */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   496
		strcpy(pypath, "PYTHONPATH=");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   497
		strcat(pypath, f_homepath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   498
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   499
	/* don't chop off SEP if root directory */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   500
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   501
	if (strlen(pypath) > 14)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   502
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   503
	if (strlen(pypath) > 12)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   504
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   505
		pypath[strlen(pypath)-1] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   506
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   507
	putenv(pypath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   508
	VS(pypath); 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   509
	VS("\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   510
	/* Clear out PYTHONHOME to avoid clashing with any installation */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   511
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   512
	putenv("PYTHONHOME=");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   513
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   514
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   515
	/* Start python. */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   516
	/* VS("Loading python\n"); */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   517
#if defined  WIN32 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   518
	*Py_NoSiteFlag = 1;	/* maybe changed to 0 by setRuntimeOptions() */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   519
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   520
	Py_NoSiteFlag = 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   521
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   522
	setRuntimeOptions();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   523
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   524
	Py_SetProgramName(f_archivename); /*XXX*/
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   525
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   526
	Py_Initialize();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   527
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   528
	/* Set sys.path */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   529
	/* VS("Manipulating Python's sys.path\n"); */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   530
	strcpy(tmp, f_homepath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   531
	tmp[strlen(tmp)-1] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   532
	PyRun_SimpleString("import sys\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   533
	PyRun_SimpleString("while sys.path:\n del sys.path[0]\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   534
	sprintf(cmd, "sys.path.append('%s')", tmp);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   535
	PyRun_SimpleString (cmd);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   536
	if (pathlen == 2) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   537
		strcpy(tmp, f_workpath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   538
		tmp[strlen(tmp)-1] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   539
		sprintf(cmd, "sys.path.insert(0, '%s')", tmp);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   540
		PyRun_SimpleString(cmd);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   541
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   542
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   543
	/* Set argv[0] to be the archiveName */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   544
	py_argv = PyList_New(0);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   545
	val = Py_BuildValue("s", f_archivename);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   546
	PyList_Append(py_argv, val);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   547
	for (i = 1; i < argc; ++i) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   548
		val = Py_BuildValue ("s", argv[i]);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   549
		PyList_Append (py_argv, val);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   550
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   551
	sys = PyImport_ImportModule("sys");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   552
	/* VS("Setting sys.argv\n"); */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   553
	PyObject_SetAttrString(sys, "argv", py_argv);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   554
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   555
	/* Check for a python error */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   556
	if (PyErr_Occurred())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   557
	{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   558
		FATALERROR("Error detected starting Python VM.");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   559
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   560
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   561
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   562
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   563
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   564
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   565
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   566
 * Import modules embedded in the archive - return 0 on success
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   567
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   568
int importModules()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   569
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   570
	PyObject *marshal;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   571
	PyObject *marshaldict;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   572
	PyObject *loadfunc;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   573
	PyObject *pyfile;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   574
	TOC *ptoc;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   575
	PyObject *co;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   576
	PyObject *mod;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   577
	PyObject *res;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   578
	char buf[32];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   579
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   580
	VS("importing modules from CArchive\n"); 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   581
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   582
	/* Get the Python function marshall.load
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   583
		* Here we collect some reference to PyObject that we don't dereference
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   584
		* Doesn't matter because the objects won't be going away anyway.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   585
		*/
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   586
	marshal = PyImport_ImportModule("marshal");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   587
	marshaldict = PyModule_GetDict(marshal);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   588
	loadfunc = PyDict_GetItemString(marshaldict, "loads");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   589
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   590
	/* Iterate through toc looking for module entries (type 'm')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   591
		* this is normally just bootstrap stuff (archive and iu)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   592
		*/
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   593
	ptoc = f_tocbuff;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   594
	while (ptoc < f_tocend) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   595
		if (ptoc->typcd == 'm' || ptoc->typcd == 'M') 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   596
		{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   597
			unsigned char *modbuf = extract(ptoc);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   598
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   599
			/* .pyc/.pyo files have 8 bytes header. Skip it and get a Python
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   600
			 * string directly pointing at the marshalled code.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   601
			 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   602
			PyObject *mods = PyString_FromStringAndSize(modbuf + 8,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   603
				ntohl(ptoc->ulen) - 8);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   604
            
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   605
			VS(ptoc->name);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   606
			VS("\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   607
			
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   608
			co = PyObject_CallFunction(loadfunc, "O", mods);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   609
			mod = PyImport_ExecCodeModule(ptoc->name, co);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   610
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   611
			/* Check for errors in loading */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   612
			if (mod == NULL) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   613
				FATALERROR("mod is NULL - ");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   614
				FATALERROR(ptoc->name);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   615
			}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   616
			if (PyErr_Occurred())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   617
			{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   618
				PyErr_Print();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   619
				PyErr_Clear();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   620
			}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   621
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   622
			Py_DECREF(mods);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   623
			free(modbuf);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   624
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   625
		ptoc = incrementTocPtr(ptoc); 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   626
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   627
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   628
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   629
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   630
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   631
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   632
/* Install a zlib from a toc entry
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   633
 * Return non zero on failure
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   634
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   635
int installZlib(TOC *ptoc)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   636
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   637
	int rc;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   638
	int zlibpos = f_pkgstart + ntohl(ptoc->pos);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   639
	char *tmpl = "sys.path.append(r\"%s?%d\")\n";
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   640
	char *cmd = (char *) malloc(strlen(tmpl) + strlen(f_archivename) + 32);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   641
	sprintf(cmd, tmpl, f_archivename, zlibpos);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   642
	//VS(cmd);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   643
	rc = PyRun_SimpleString(cmd);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   644
	if (rc != 0)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   645
	{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   646
		FATALERROR("Error in command.");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   647
		FATALERROR(cmd);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   648
		free(cmd);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   649
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   650
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   651
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   652
	free(cmd);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   653
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   654
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   655
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   656
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   657
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   658
 * Install zlibs 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   659
 * Return non zero on failure
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   660
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   661
int installZlibs()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   662
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   663
	TOC * ptoc;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   664
	VS("Installing import hooks\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   665
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   666
	/* Iterate through toc looking for zlibs (type 'z') */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   667
	ptoc = f_tocbuff;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   668
	while (ptoc < f_tocend) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   669
		if (ptoc->typcd == 'z') 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   670
		{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   671
			VS(ptoc->name);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   672
			VS("\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   673
			installZlib(ptoc);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   674
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   675
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   676
		ptoc = incrementTocPtr(ptoc); 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   677
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   678
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   679
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   680
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   681
#ifndef NOZLIB
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   682
/* decompress data in buff, described by ptoc
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   683
 * return in malloc'ed buffer (needs to be freed)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   684
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   685
unsigned char *decompress(unsigned char * buff, TOC *ptoc)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   686
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   687
	const char *ver;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   688
	unsigned char *out;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   689
	z_stream zstream;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   690
	int rc;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   691
	char msg[400];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   692
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   693
	ver = (zlibVersion)();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   694
	out = (unsigned char *)malloc(ntohl(ptoc->ulen));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   695
	if (out == NULL) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   696
		OTHERERROR("Error allocating decompression buffer\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   697
		return NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   698
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   699
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   700
	zstream.zalloc = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   701
	zstream.zfree = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   702
	zstream.opaque = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   703
	zstream.next_in = buff;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   704
	zstream.avail_in = ntohl(ptoc->len);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   705
	zstream.next_out = out;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   706
	zstream.avail_out = ntohl(ptoc->ulen);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   707
	rc = inflateInit(&zstream);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   708
	if (rc >= 0) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   709
		rc = (inflate)(&zstream, Z_FINISH);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   710
		if (rc >= 0) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   711
			rc = (inflateEnd)(&zstream);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   712
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   713
		else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   714
			sprintf(msg, "Error %d from inflate: %s\n", rc, zstream.msg);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   715
			OTHERERROR(msg);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   716
			return NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   717
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   718
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   719
	else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   720
		sprintf(msg, "Error %d from inflateInit: %s\n", rc, zstream.msg);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   721
		OTHERERROR(msg);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   722
		return NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   723
	}	
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   724
	return out;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   725
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   726
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   727
/* 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   728
 * extract an archive entry
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   729
 * returns pointer to the data (must be freed)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   730
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   731
unsigned char *extract(TOC *ptoc)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   732
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   733
	unsigned char *data;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   734
	unsigned char *tmp;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   735
	char msg[400];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   736
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   737
	sprintf( msg, " extracting %1.20s (%d, %c)\n", ptoc->name, ptoc->cflag, ptoc->typcd);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   738
	//VS(msg);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   739
	fseek(f_fp, f_pkgstart + ntohl(ptoc->pos), SEEK_SET);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   740
	data = (unsigned char *)malloc(ntohl(ptoc->len));
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   741
	if (data == NULL) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   742
		OTHERERROR("Could not allocate read buffer\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   743
		return NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   744
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   745
	fread(data, ntohl(ptoc->len), 1, f_fp);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   746
	if (ptoc->cflag == '\1') {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   747
#ifndef NOZLIB
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   748
		tmp = decompress(data, ptoc);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   749
		free(data);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   750
		data = tmp;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   751
		if (data == NULL) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   752
			sprintf(msg, "Error decompressing %s\n", ptoc->name);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   753
			OTHERERROR(msg);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   754
			return NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   755
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   756
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   757
		FATALERROR("No ZLIB support but archive uses compression\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   758
		return NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   759
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   760
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   761
	return data;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   762
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   763
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   764
 * helper for extract2fs
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   765
 * which may try multiple places
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   766
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   767
FILE *openTarget(char *path, char*name)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   768
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   769
	struct stat sbuf;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   770
	char fnm[_MAX_PATH+1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   771
	strcpy(fnm, path);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   772
	strcat(fnm, name);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   773
	if (stat(fnm, &sbuf) == -1) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   774
		VS(fnm);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   775
		VS("\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   776
		return fopen(fnm, "wb");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   777
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   778
	return NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   779
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   780
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   781
 * extract from the archive
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   782
 * and copy to the filesystem 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   783
 * relative to the directory the archive's in
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   784
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   785
int extract2fs(TOC *ptoc)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   786
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   787
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   788
	char *p;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   789
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   790
	FILE *out;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   791
	unsigned char *data = extract(ptoc);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   792
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   793
	if (!f_workpath) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   794
		getTempPath(f_temppath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   795
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   796
		strcpy(f_temppathraw, f_temppath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   797
		for ( p=f_temppath; *p; p++ )
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   798
			if (*p == '\\')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   799
				*p = '/';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   800
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   801
		f_workpath = f_temppath;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   802
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   803
	out = openTarget(f_workpath, ptoc->name);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   804
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   805
	if (out == NULL)  {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   806
		FATALERROR(ptoc->name);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   807
		FATALERROR(" could not be extracted!\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   808
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   809
	else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   810
		fwrite(data, ntohl(ptoc->ulen), 1, out);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   811
#ifndef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   812
		fchmod(fileno(out), S_IRUSR | S_IWUSR | S_IXUSR);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   813
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   814
		fclose(out);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   815
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   816
	free(data);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   817
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   818
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   819
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   820
 * extract all binaries (type 'b') to the filesystem
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   821
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   822
int extractBinaries(char **workpath)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   823
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   824
	TOC * ptoc = f_tocbuff;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   825
	workpath[0] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   826
	VS("Extracting binaries\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   827
	while (ptoc < f_tocend) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   828
		if (ptoc->typcd == 'b') 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   829
		if (extract2fs(ptoc))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   830
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   831
		ptoc = incrementTocPtr(ptoc); 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   832
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   833
	*workpath = f_workpath;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   834
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   835
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   836
/* 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   837
 * Run scripts
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   838
 * Return non zero on failure
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   839
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   840
int runScripts()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   841
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   842
	unsigned char *data;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   843
	int rc = 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   844
	TOC * ptoc = f_tocbuff;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   845
	char msg[400];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   846
	VS("Running scripts\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   847
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   848
	/* Iterate through toc looking for scripts (type 's') */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   849
	while (ptoc < f_tocend) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   850
		if (ptoc->typcd == 's') {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   851
			/* Get data out of the archive.  */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   852
			data = extract(ptoc);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   853
			/* Run it */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   854
			rc = PyRun_SimpleString(data);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   855
			/* log errors and abort */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   856
			if (rc != 0) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   857
				sprintf(msg, " RC: %d from %s\n", rc, ptoc->name);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   858
				VS(msg);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   859
				return rc;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   860
			}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   861
			free(data);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   862
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   863
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   864
		ptoc = incrementTocPtr(ptoc); 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   865
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   866
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   867
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   868
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   869
/* 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   870
 * call a simple "int func(void)" entry point.  Assumes such a function
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   871
 * exists in the main namespace.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   872
 * Return non zero on failure, with -2 if the specific error is
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   873
 * that the function does not exist in the namespace.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   874
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   875
int callSimpleEntryPoint(char *name, int *presult)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   876
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   877
	int rc = -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   878
	/* Objects with no ref. */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   879
	PyObject *mod, *dict;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   880
	/* Objects with refs to kill. */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   881
	PyObject *func = NULL, *pyresult = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   882
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   883
	mod = PyImport_AddModule("__main__"); /* NO ref added */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   884
	if (!mod) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   885
		VS("No __main__\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   886
		goto done;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   887
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   888
	dict = PyModule_GetDict(mod); /* NO ref added */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   889
	if (!mod) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   890
		VS("No __dict__\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   891
		goto done;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   892
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   893
	func = PyDict_GetItemString(dict, name);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   894
	if (func == NULL) { /* should explicitly check KeyError */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   895
		VS("CallSimpleEntryPoint can't find the function name\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   896
		rc = -2;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   897
		goto done;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   898
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   899
	pyresult = PyObject_CallFunction(func, "");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   900
	if (pyresult==NULL) goto done;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   901
	PyErr_Clear();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   902
	*presult = PyInt_AsLong(pyresult);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   903
	rc = PyErr_Occurred() ? -1 : 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   904
	VS( rc ? "Finished with failure\n" : "Finished OK\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   905
	// all done!
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   906
done:
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   907
	Py_XDECREF(func);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   908
	Py_XDECREF(pyresult);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   909
	/* can't leave Python error set, else it may
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   910
	   cause failures in later async code */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   911
	if (rc)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   912
		/* But we will print them 'cos they may be useful */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   913
		PyErr_Print();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   914
	PyErr_Clear();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   915
	return rc;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   916
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   917
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   918
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   919
 * Launch an archive with the given fully-qualified path name
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   920
 * No command line, no extracting of binaries
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   921
 * Designed for embedding situations.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   922
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   923
int launchembedded(char const * archivePath, char  const * archiveName)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   924
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   925
	char pathnm[_MAX_PATH];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   926
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   927
	VS("START\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   928
	strcpy(pathnm, archivePath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   929
	strcat(pathnm, archiveName);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   930
	/* Set up paths */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   931
	if (setPaths(archivePath, archiveName))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   932
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   933
	VS("Got Paths\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   934
	/* Open the archive */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   935
	if (openArchive())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   936
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   937
	VS("Opened Archive\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   938
	/* Load Python DLL */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   939
	if (loadPython())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   940
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   941
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   942
	/* Start Python with silly command line */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   943
	if (startPython(1, (char**)&pathnm))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   944
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   945
	VS("Started Python\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   946
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   947
	/* a signal to scripts */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   948
	PyRun_SimpleString("import sys;sys.frozen='dll'\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   949
	VS("set sys.frozen\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   950
	/* Import modules from archive - this is to bootstrap */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   951
	if (importModules())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   952
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   953
	VS("Imported Modules\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   954
	/* Install zlibs - now import hooks are in place */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   955
	if (installZlibs())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   956
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   957
	VS("Installed Zlibs\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   958
	/* Run scripts */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   959
	if (runScripts())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   960
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   961
	VS("All scripts run\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   962
	if (PyErr_Occurred()) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   963
		// PyErr_Print();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   964
		//PyErr_Clear();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   965
		VS("Some error occurred\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   966
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   967
	VS("OK.\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   968
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   969
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   970
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   971
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   972
/* for finer grained control */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   973
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   974
 * initialize (this always needs to be done)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   975
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   976
int init(char const * archivePath, char  const * archiveName, char const * workpath)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   977
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   978
	char *p;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   979
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   980
	if (workpath) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   981
		f_workpath = (char *)workpath;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   982
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   983
		strcpy(f_temppathraw, f_workpath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   984
		for ( p = f_temppathraw; *p; p++ )
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   985
			if (*p == '/')
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   986
				*p = '\\';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   987
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   988
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   989
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   990
	/* Set up paths */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   991
	if (setPaths(archivePath, archiveName))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   992
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   993
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   994
	/* Open the archive */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   995
	if (openArchive())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   996
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   997
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   998
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   999
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1000
/* once init'ed, you might want to extractBinaries()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1001
 * If you do, what comes after is very platform specific.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1002
 * Once you've taken care of the platform specific details,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1003
 * or if there are no binaries to extract, you go on
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1004
 * to doIt(), which is the important part
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1005
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1006
int doIt(int argc, char *argv[]) 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1007
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1008
	int rc = 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1009
	/* Load Python DLL */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1010
	if (loadPython())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1011
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1012
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1013
	/* Start Python. */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1014
	if (startPython(argc, argv))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1015
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1016
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1017
	/* Import modules from archive - bootstrap */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1018
	if (importModules())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1019
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1020
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1021
	/* Install zlibs  - now all hooks in place */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1022
	if (installZlibs())
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1023
		return -1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1024
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1025
	/* Run scripts */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1026
	rc = runScripts();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1027
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1028
	VS("OK.\n");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1029
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1030
	return rc;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1031
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1032
void clear(const char *dir);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1033
#ifdef WIN32
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1034
void removeOne(char *fnm, int pos, struct _finddata_t finfo)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1035
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1036
	if ( strcmp(finfo.name, ".")==0  || strcmp(finfo.name, "..") == 0 )
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1037
		return;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1038
	fnm[pos] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1039
	strcat(fnm, finfo.name);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1040
	if ( finfo.attrib & _A_SUBDIR )
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1041
		clear(fnm);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1042
	else 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1043
		remove(fnm);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1044
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1045
void clear(const char *dir) 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1046
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1047
	char fnm[_MAX_PATH+1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1048
	struct _finddata_t finfo;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1049
	long h;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1050
	int dirnmlen;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1051
	strcpy(fnm, dir);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1052
	dirnmlen = strlen(fnm);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1053
	if ( fnm[dirnmlen-1] != '/' && fnm[dirnmlen-1] != '\\' ) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1054
		strcat(fnm, "\\");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1055
		dirnmlen++;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1056
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1057
	strcat(fnm, "*");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1058
	h = _findfirst(fnm, &finfo);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1059
	if (h != -1) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1060
		removeOne(fnm, dirnmlen, finfo);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1061
		while ( _findnext(h, &finfo) == 0 ) 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1062
			removeOne(fnm, dirnmlen, finfo);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1063
		_findclose(h);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1064
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1065
	rmdir(dir);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1066
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1067
#else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1068
void removeOne(char *pnm, int pos, const char *fnm)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1069
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1070
	struct stat sbuf;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1071
	if ( strcmp(fnm, ".")==0  || strcmp(fnm, "..") == 0 )
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1072
		return;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1073
	pnm[pos] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1074
	strcat(pnm, fnm);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1075
	if ( stat(pnm, &sbuf) == 0 ) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1076
		if ( S_ISDIR(sbuf.st_mode) )
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1077
			clear(pnm);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1078
		else 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1079
			unlink(pnm);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1080
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1081
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1082
void clear(const char *dir) 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1083
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1084
	char fnm[_MAX_PATH+1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1085
	DIR *ds;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1086
	struct dirent *finfo;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1087
	int dirnmlen;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1088
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1089
	strcpy(fnm, dir);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1090
	dirnmlen = strlen(fnm);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1091
	if ( fnm[dirnmlen-1] != '/' ) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1092
		strcat(fnm, "/");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1093
		dirnmlen++;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1094
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1095
	ds = opendir(dir);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1096
	finfo = readdir(ds);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1097
	while (finfo) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1098
		removeOne(fnm, dirnmlen, finfo->d_name);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1099
		finfo = readdir(ds);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1100
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1101
	closedir(ds);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1102
	rmdir(dir);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1103
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1104
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1105
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1106
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1107
 * If binaries were extracted, this should be called
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1108
 * to remove them
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1109
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1110
void cleanUp()
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1111
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1112
	if (f_temppath[0])
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1113
		clear(f_temppath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1114
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1115
/*
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1116
 * Helpers for embedders
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1117
 */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1118
int getPyVersion(void)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1119
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1120
	return ntohl(f_cookie.pyvers);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1121
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1122
void finalizePython(void)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1123
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1124
	Py_Finalize();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1125
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
  1126