##// END OF EJS Templates
exewrapper: use generic term script...
Adrian Buehlmann -
r17063:3fbc6e3a default
parent child Browse files
Show More
@@ -1,101 +1,101 b''
1 1 /*
2 2 exewrapper.c - wrapper for calling a python script on Windows
3 3
4 4 Copyright 2012 Adrian Buehlmann <adrian@cadifra.com> and others
5 5
6 6 This software may be used and distributed according to the terms of the
7 7 GNU General Public License version 2 or any later version.
8 8 */
9 9
10 10 #include <Python.h>
11 11 #include <windows.h>
12 12
13 13
14 14 #ifdef __GNUC__
15 15 int strcat_s(char *d, size_t n, const char *s)
16 16 {
17 17 return !strncat(d, s, n);
18 18 }
19 19 #endif
20 20
21 21
22 22 static char pyscript[MAX_PATH + 10];
23 23
24 24 int main(int argc, char *argv[])
25 25 {
26 26 char *dot;
27 27 int ret;
28 28 int i;
29 29 int n;
30 30 char **pyargv;
31 31 WIN32_FIND_DATA fdata;
32 32 HANDLE hfind;
33 33 const char *err;
34 34
35 35 if (GetModuleFileName(NULL, pyscript, sizeof(pyscript)) == 0)
36 36 {
37 37 err = "GetModuleFileName failed";
38 38 goto bail;
39 39 }
40 40
41 41 dot = strrchr(pyscript, '.');
42 42 if (dot == NULL) {
43 43 err = "malformed module filename";
44 44 goto bail;
45 45 }
46 46 *dot = 0; /* cut trailing ".exe" */
47 47
48 48 hfind = FindFirstFile(pyscript, &fdata);
49 49 if (hfind != INVALID_HANDLE_VALUE) {
50 50 /* pyscript exists, close handle */
51 51 FindClose(hfind);
52 52 } else {
53 53 /* file pyscript isn't there, take <pyscript>exe.py */
54 54 strcat_s(pyscript, sizeof(pyscript), "exe.py");
55 55 }
56 56
57 57 /*
58 58 Only add the pyscript to the args, if it's not already there. It may
59 already be there, if Mercurial spawned a child process of itself, in
59 already be there, if the script spawned a child process of itself, in
60 60 the same way as it got called, that is, with the pyscript already in
61 61 place. So we optionally accept the pyscript as the first argument
62 62 (argv[1]), letting our exe taking the role of the python interpreter.
63 63 */
64 64 if (argc >= 2 && strcmp(argv[1], pyscript) == 0) {
65 65 /*
66 66 pyscript is already in the args, so there is no need to copy
67 67 the args and we can directly call the python interpreter with
68 68 the original args.
69 69 */
70 70 return Py_Main(argc, argv);
71 71 }
72 72
73 73 /*
74 74 Start assembling the args for the Python interpreter call. We put the
75 75 name of our exe (argv[0]) in the position where the python.exe
76 76 canonically is, and insert the pyscript next.
77 77 */
78 78 pyargv = malloc((argc + 5) * sizeof(char*));
79 79 if (pyargv == NULL) {
80 80 err = "not enough memory";
81 81 goto bail;
82 82 }
83 83 n = 0;
84 84 pyargv[n++] = argv[0];
85 85 pyargv[n++] = pyscript;
86 86
87 87 /* copy remaining args from the command line */
88 88 for (i = 1; i < argc; i++)
89 89 pyargv[n++] = argv[i];
90 90 /* argv[argc] is guaranteed to be NULL, so we forward that guarantee */
91 91 pyargv[n] = NULL;
92 92
93 93 ret = Py_Main(n, pyargv); /* The Python interpreter call */
94 94
95 95 free(pyargv);
96 96 return ret;
97 97
98 98 bail:
99 99 fprintf(stderr, "abort: %s\n", err);
100 100 return 255;
101 101 }
General Comments 0
You need to be logged in to leave comments. Login now