Mercurial > parpg-core
annotate bin/parpg.c.in @ 6:dd4ed4945411
Ported binary launcher to Windows.
* Reorganized the parpg executable source so that it now compiles with the MSVC compiler (tested with MSVC Express 2010).
* Cleaned up the launcher source by adding a bunch of const statements and removing the superfluous NULL_TERMINATE macro.
* Fixed a few memory leaks in the launcher source by freeing malloced/calloced pointers;
* Added a new SubstfileEscape builder to the SConstruct script used to escape certain sequences when substituting a template file. This is used to escape backslashes ("\") in c strings in the bin/parpg.c.in template for Windows paths.
* Fixed the PY_LIB_DIR_DEFAULT for Windows so that it now correctly points to the default Python installation path.
* Modified the SConstruct script to support compiling the launcher executable with debugging symbols in Windows with the DEBUG flag.
author | M. George Hansen |
---|---|
date | Sun, 22 May 2011 00:53:59 -0700 |
parents | 33684971cdb1 |
children |
rev | line source |
---|---|
5
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
1 #include <stdlib.h> |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
2 #include <stdio.h> |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
3 #include <string.h> |
6 | 4 #ifdef _WIN32 |
5
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
5 # include <process.h> |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
6 #else |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
7 # include <sys/types.h> |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
8 # include <sys/wait.h> |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
9 #endif |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
10 |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
11 #define ARRAY_LEN(ARRAY) (sizeof(ARRAY) / sizeof *(ARRAY)) |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
12 |
6 | 13 #define CONCAT_ARRAYS(TYPE, ARRAY_A, ARRAY_B) \ |
14 (TYPE*)concatArrays((const void** const)(ARRAY_A), \ | |
15 ARRAY_LEN(ARRAY_A) * sizeof(TYPE), (const void** const)(ARRAY_B), \ | |
16 ARRAY_LEN(ARRAY_B) * sizeof(TYPE), sizeof(TYPE)) | |
17 | |
5
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
18 void* |
6 | 19 concatArrays(const void* const arrayA, const size_t sizeA, |
20 const void* const arrayB, const size_t sizeB, const size_t sizeType) { | |
21 char* const concatenatedArray = malloc(sizeType * (sizeA + sizeB)); | |
5
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
22 memcpy(concatenatedArray, arrayA, sizeA * sizeType); |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
23 memcpy(concatenatedArray + sizeA * sizeType, arrayB, sizeB * sizeType); |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
24 return concatenatedArray; |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
25 } |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
26 |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
27 char* |
6 | 28 joinStr(const char* const strA, const char* const strB, |
29 const char* const separator) { | |
30 const int lenA = strlen(strA); | |
31 const int lenB = strlen(strB); | |
32 const int lenSeparator = strlen(separator); | |
33 char* const concatenatedStr = malloc(lenA + lenB + lenSeparator + 1); | |
5
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
34 memcpy(concatenatedStr, strA, lenA); |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
35 memcpy(concatenatedStr + lenA, separator, lenSeparator); |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
36 memcpy(concatenatedStr + lenA + lenSeparator, strB, lenB + 1); |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
37 return concatenatedStr; |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
38 } |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
39 |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
40 int |
6 | 41 spawnParpgProcess(char* const pythonExecutable, char* const configDir, |
42 const int argc, char* const argv[]) { | |
43 const int sizeCharPtr = sizeof(char*); | |
44 const int nBaseArgs = 4; | |
45 char** const baseArgs = malloc(nBaseArgs * sizeCharPtr); | |
46 const int nAdditionalArgs = argc - 1; | |
47 char** const additionalArgs = | |
48 (char**)malloc(nAdditionalArgs * sizeCharPtr); | |
49 const int nArgs = nBaseArgs + nAdditionalArgs; | |
50 char* const oldPythonPath = getenv("PYTHONPATH"); | |
51 char* newPythonPath; | |
52 // Use calloc so that the last element is NULL. | |
53 char** const env = calloc(2, sizeCharPtr); | |
54 int exitStatus; | |
55 // Use calloc so that the last element is NULL. | |
56 char** const args = (char**)calloc(nArgs + 1, sizeCharPtr); | |
5
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
57 int i; |
6 | 58 |
59 baseArgs[0] = pythonExecutable; | |
60 baseArgs[1] = "-m"; | |
61 baseArgs[2] = "parpg.main"; | |
62 baseArgs[3] = configDir; | |
63 if (nAdditionalArgs > 0) { | |
64 for (i = 1; i < argc; i++) { | |
65 additionalArgs[i - 1] = argv[i]; | |
66 } | |
67 memcpy(args, CONCAT_ARRAYS(char*, baseArgs, additionalArgs), | |
68 nArgs * sizeCharPtr); | |
69 } else { | |
70 // Use memcpy so that the last NULL element in args is preserved. | |
71 memcpy(args, baseArgs, nArgs * sizeCharPtr); | |
5
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
72 } |
6 | 73 if (oldPythonPath != 0) { |
74 newPythonPath = joinStr(oldPythonPath, "@PY_PACKAGES_DIR@", ":"); | |
75 } else { | |
76 newPythonPath = "@PY_PACKAGES_DIR@"; | |
77 } | |
78 env[0] = joinStr("PYTHONPATH=", newPythonPath, ""); | |
79 #ifdef _WIN32 | |
80 # ifdef _MSC_VER // MSVC deprecated POSIX spawn functions. | |
81 exitStatus = _spawnve(_P_WAIT, pythonExecutable, args, env); | |
82 # else | |
5
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
83 exitStatus = spawnve(P_WAIT, pythonExecutable, args, env); |
6 | 84 # endif |
5
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
85 #else |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
86 exitStatus = execve(pythonExecutable, args, env); |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
87 #endif |
6 | 88 free(baseArgs); |
89 free(additionalArgs); | |
90 free(env); | |
91 free(args); | |
5
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
92 return exitStatus; |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
93 } |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
94 |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
95 int |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
96 main(int argc, char* argv[]) { |
6 | 97 const int exitStatus = spawnParpgProcess("@PYTHON@", "@SYS_CONF_DIR@", |
98 argc, argv); | |
99 if (exitStatus < 0) { | |
100 perror("failed to execute subprocess"); | |
101 exit(EXIT_FAILURE); | |
102 } else { | |
103 exit(EXIT_SUCCESS); | |
5
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
104 } |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
105 } |
33684971cdb1
Replaced the shell script launcher with a cross-platform C executable.
M. George Hansen <technopolitica@gmail.com>
parents:
diff
changeset
|
106 |