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