Show More
@@ -8,6 +8,7 b'' | |||||
8 | */ |
|
8 | */ | |
9 |
|
9 | |||
10 | #include <stdio.h> |
|
10 | #include <stdio.h> | |
|
11 | #include <tchar.h> | |||
11 | #include <windows.h> |
|
12 | #include <windows.h> | |
12 |
|
13 | |||
13 | #include "hgpythonlib.h" |
|
14 | #include "hgpythonlib.h" | |
@@ -21,38 +22,42 b' int strcpy_s(char *d, size_t n, const ch' | |||||
21 | { |
|
22 | { | |
22 | return !strncpy(d, s, n); |
|
23 | return !strncpy(d, s, n); | |
23 | } |
|
24 | } | |
|
25 | ||||
|
26 | #define _tcscpy_s strcpy_s | |||
|
27 | #define _tcscat_s strcat_s | |||
|
28 | #define _countof(array) (sizeof(array)/sizeof(array[0])) | |||
24 | #endif |
|
29 | #endif | |
25 |
|
30 | |||
26 |
static |
|
31 | static TCHAR pyscript[MAX_PATH + 10]; | |
27 |
static |
|
32 | static TCHAR pyhome[MAX_PATH + 10]; | |
28 |
static |
|
33 | static TCHAR pydllfile[MAX_PATH + 10]; | |
29 |
|
34 | |||
30 |
int main(int argc, |
|
35 | int _tmain(int argc, TCHAR *argv[]) | |
31 | { |
|
36 | { | |
32 | char *p; |
|
37 | TCHAR *p; | |
33 | int ret; |
|
38 | int ret; | |
34 | int i; |
|
39 | int i; | |
35 | int n; |
|
40 | int n; | |
36 |
|
|
41 | TCHAR **pyargv; | |
37 | WIN32_FIND_DATA fdata; |
|
42 | WIN32_FIND_DATA fdata; | |
38 | HANDLE hfind; |
|
43 | HANDLE hfind; | |
39 | const char *err; |
|
44 | const char *err; | |
40 | HMODULE pydll; |
|
45 | HMODULE pydll; | |
41 |
void(__cdecl * Py_SetPythonHome)( |
|
46 | void(__cdecl * Py_SetPythonHome)(TCHAR *home); | |
42 |
int(__cdecl * Py_Main)(int argc, |
|
47 | int(__cdecl * Py_Main)(int argc, TCHAR *argv[]); | |
43 |
|
48 | |||
44 |
if (GetModuleFileName(NULL, pyscript, |
|
49 | if (GetModuleFileName(NULL, pyscript, _countof(pyscript)) == 0) { | |
45 | err = "GetModuleFileName failed"; |
|
50 | err = "GetModuleFileName failed"; | |
46 | goto bail; |
|
51 | goto bail; | |
47 | } |
|
52 | } | |
48 |
|
53 | |||
49 |
p = s |
|
54 | p = _tcsrchr(pyscript, '.'); | |
50 | if (p == NULL) { |
|
55 | if (p == NULL) { | |
51 | err = "malformed module filename"; |
|
56 | err = "malformed module filename"; | |
52 | goto bail; |
|
57 | goto bail; | |
53 | } |
|
58 | } | |
54 | *p = 0; /* cut trailing ".exe" */ |
|
59 | *p = 0; /* cut trailing ".exe" */ | |
55 |
s |
|
60 | _tcscpy_s(pyhome, _countof(pyhome), pyscript); | |
56 |
|
61 | |||
57 | hfind = FindFirstFile(pyscript, &fdata); |
|
62 | hfind = FindFirstFile(pyscript, &fdata); | |
58 | if (hfind != INVALID_HANDLE_VALUE) { |
|
63 | if (hfind != INVALID_HANDLE_VALUE) { | |
@@ -60,12 +65,12 b' int main(int argc, char *argv[])' | |||||
60 | FindClose(hfind); |
|
65 | FindClose(hfind); | |
61 | } else { |
|
66 | } else { | |
62 | /* file pyscript isn't there, take <pyscript>exe.py */ |
|
67 | /* file pyscript isn't there, take <pyscript>exe.py */ | |
63 |
s |
|
68 | _tcscat_s(pyscript, _countof(pyscript), _T("exe.py")); | |
64 | } |
|
69 | } | |
65 |
|
70 | |||
66 | pydll = NULL; |
|
71 | pydll = NULL; | |
67 |
|
72 | |||
68 |
p = s |
|
73 | p = _tcsrchr(pyhome, _T('\\')); | |
69 | if (p == NULL) { |
|
74 | if (p == NULL) { | |
70 | err = "can't find backslash in module filename"; |
|
75 | err = "can't find backslash in module filename"; | |
71 | goto bail; |
|
76 | goto bail; | |
@@ -73,19 +78,19 b' int main(int argc, char *argv[])' | |||||
73 | *p = 0; /* cut at directory */ |
|
78 | *p = 0; /* cut at directory */ | |
74 |
|
79 | |||
75 | /* check for private Python of HackableMercurial */ |
|
80 | /* check for private Python of HackableMercurial */ | |
76 |
s |
|
81 | _tcscat_s(pyhome, _countof(pyhome), _T("\\hg-python")); | |
77 |
|
82 | |||
78 | hfind = FindFirstFile(pyhome, &fdata); |
|
83 | hfind = FindFirstFile(pyhome, &fdata); | |
79 | if (hfind != INVALID_HANDLE_VALUE) { |
|
84 | if (hfind != INVALID_HANDLE_VALUE) { | |
80 | /* Path .\hg-python exists. We are probably in HackableMercurial |
|
85 | /* Path .\hg-python exists. We are probably in HackableMercurial | |
81 | scenario, so let's load python dll from this dir. */ |
|
86 | scenario, so let's load python dll from this dir. */ | |
82 | FindClose(hfind); |
|
87 | FindClose(hfind); | |
83 |
s |
|
88 | _tcscpy_s(pydllfile, _countof(pydllfile), pyhome); | |
84 |
s |
|
89 | _tcscat_s(pydllfile, _countof(pydllfile), _T("\\") _T(HGPYTHONLIB) | |
|
90 | _T(".dll")); | |||
85 | pydll = LoadLibrary(pydllfile); |
|
91 | pydll = LoadLibrary(pydllfile); | |
86 | if (pydll == NULL) { |
|
92 | if (pydll == NULL) { | |
87 | err = "failed to load private Python DLL " HGPYTHONLIB |
|
93 | err = "failed to load private Python DLL " HGPYTHONLIB ".dll"; | |
88 | ".dll"; |
|
|||
89 | goto bail; |
|
94 | goto bail; | |
90 | } |
|
95 | } | |
91 | Py_SetPythonHome = |
|
96 | Py_SetPythonHome = | |
@@ -98,7 +103,7 b' int main(int argc, char *argv[])' | |||||
98 | } |
|
103 | } | |
99 |
|
104 | |||
100 | if (pydll == NULL) { |
|
105 | if (pydll == NULL) { | |
101 | pydll = LoadLibrary(HGPYTHONLIB ".dll"); |
|
106 | pydll = LoadLibrary(_T(HGPYTHONLIB) _T(".dll")); | |
102 | if (pydll == NULL) { |
|
107 | if (pydll == NULL) { | |
103 | err = "failed to load Python DLL " HGPYTHONLIB ".dll"; |
|
108 | err = "failed to load Python DLL " HGPYTHONLIB ".dll"; | |
104 | goto bail; |
|
109 | goto bail; | |
@@ -118,7 +123,7 b' int main(int argc, char *argv[])' | |||||
118 | place. So we optionally accept the pyscript as the first argument |
|
123 | place. So we optionally accept the pyscript as the first argument | |
119 | (argv[1]), letting our exe taking the role of the python interpreter. |
|
124 | (argv[1]), letting our exe taking the role of the python interpreter. | |
120 | */ |
|
125 | */ | |
121 |
if (argc >= 2 && s |
|
126 | if (argc >= 2 && _tcscmp(argv[1], pyscript) == 0) { | |
122 | /* |
|
127 | /* | |
123 | pyscript is already in the args, so there is no need to copy |
|
128 | pyscript is already in the args, so there is no need to copy | |
124 | the args and we can directly call the python interpreter with |
|
129 | the args and we can directly call the python interpreter with | |
@@ -132,7 +137,7 b' int main(int argc, char *argv[])' | |||||
132 | name of our exe (argv[0]) in the position where the python.exe |
|
137 | name of our exe (argv[0]) in the position where the python.exe | |
133 | canonically is, and insert the pyscript next. |
|
138 | canonically is, and insert the pyscript next. | |
134 | */ |
|
139 | */ | |
135 |
pyargv = malloc((argc + 5) * sizeof( |
|
140 | pyargv = malloc((argc + 5) * sizeof(TCHAR *)); | |
136 | if (pyargv == NULL) { |
|
141 | if (pyargv == NULL) { | |
137 | err = "not enough memory"; |
|
142 | err = "not enough memory"; | |
138 | goto bail; |
|
143 | goto bail; |
General Comments 0
You need to be logged in to leave comments.
Login now