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
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
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
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
13 #define CONCAT_ARRAYS(TYPE, ARRAY_A, ARRAY_B) \
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
14 (TYPE*)concatArrays((const void** const)(ARRAY_A), \
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
15 ARRAY_LEN(ARRAY_A) * sizeof(TYPE), (const void** const)(ARRAY_B), \
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
16 ARRAY_LEN(ARRAY_B) * sizeof(TYPE), sizeof(TYPE))
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
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
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
19 concatArrays(const void* const arrayA, const size_t sizeA,
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
20 const void* const arrayB, const size_t sizeB, const size_t sizeType) {
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
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
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
28 joinStr(const char* const strA, const char* const strB,
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
29 const char* const separator) {
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
30 const int lenA = strlen(strA);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
31 const int lenB = strlen(strB);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
32 const int lenSeparator = strlen(separator);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
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
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
41 spawnParpgProcess(char* const pythonExecutable, char* const configDir,
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
42 const int argc, char* const argv[]) {
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
43 const int sizeCharPtr = sizeof(char*);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
44 const int nBaseArgs = 4;
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
45 char** const baseArgs = malloc(nBaseArgs * sizeCharPtr);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
46 const int nAdditionalArgs = argc - 1;
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
47 char** const additionalArgs =
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
48 (char**)malloc(nAdditionalArgs * sizeCharPtr);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
49 const int nArgs = nBaseArgs + nAdditionalArgs;
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
50 char* const oldPythonPath = getenv("PYTHONPATH");
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
51 char* newPythonPath;
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
52 // Use calloc so that the last element is NULL.
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
53 char** const env = calloc(2, sizeCharPtr);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
54 int exitStatus;
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
55 // Use calloc so that the last element is NULL.
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
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
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
58
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
59 baseArgs[0] = pythonExecutable;
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
60 baseArgs[1] = "-m";
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
61 baseArgs[2] = "parpg.main";
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
62 baseArgs[3] = configDir;
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
63 if (nAdditionalArgs > 0) {
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
64 for (i = 1; i < argc; i++) {
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
65 additionalArgs[i - 1] = argv[i];
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
66 }
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
67 memcpy(args, CONCAT_ARRAYS(char*, baseArgs, additionalArgs),
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
68 nArgs * sizeCharPtr);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
69 } else {
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
70 // Use memcpy so that the last NULL element in args is preserved.
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
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
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
73 if (oldPythonPath != 0) {
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
74 newPythonPath = joinStr(oldPythonPath, "@PY_PACKAGES_DIR@", ":");
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
75 } else {
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
76 newPythonPath = "@PY_PACKAGES_DIR@";
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
77 }
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
78 env[0] = joinStr("PYTHONPATH=", newPythonPath, "");
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
79 #ifdef _WIN32
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
80 # ifdef _MSC_VER // MSVC deprecated POSIX spawn functions.
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
81 exitStatus = _spawnve(_P_WAIT, pythonExecutable, args, env);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
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
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
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
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
88 free(baseArgs);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
89 free(additionalArgs);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
90 free(env);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
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
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
97 const int exitStatus = spawnParpgProcess("@PYTHON@", "@SYS_CONF_DIR@",
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
98 argc, argv);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
99 if (exitStatus < 0) {
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
100 perror("failed to execute subprocess");
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
101 exit(EXIT_FAILURE);
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
102 } else {
dd4ed4945411 Ported binary launcher to Windows.
M. George Hansen
parents: 5
diff changeset
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