-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshebang.c
432 lines (362 loc) · 13 KB
/
shebang.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* Proj: shebang
* Auth: matveyt
* Desc: Allows direct execution of MSYS/Cygwin shebang scripts
* Note: Rename or symlink to match the desired script and put both on PATH
*/
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#endif // UNICODE
#define WIN32_LEAN_AND_MEAN
#include <tchar.h>
#include <windows.h>
#include <shlwapi.h>
#include <strsafe.h>
#if !defined(__GNUC__)
#pragma comment(lib, "shlwapi.lib")
#endif // __GNUC__
// our original name
#define PROGRAM_NAME "shebang"
// macro to facilitate function call
#define COUNT(a) (sizeof(a) / sizeof(*a))
#define COUNT1(a) (COUNT(a) - 1)
#define ARRAY(a) (a), COUNT(a)
#define ARRAY1(a) (a), COUNT1(a)
// latin letter test
#define IS_LATIN(c) (('A' <= (c) && (c) <= 'Z') || ('a' <= (c) && (c) <= 'z'))
// known POSIX layers
typedef struct {
enum {
POSIX_UNKNOWN = -1,
POSIX_CLANGARM64, // CLANGARM64
POSIX_MINGW32, // MINGW32
POSIX_MINGW64, // MINGW64
POSIX_UCRT64, // UCRT64
POSIX_CLANG32, // CLANG32
POSIX_CLANG64, // CLANG64
POSIX_MSYS, // MSYS
POSIX_CYGWIN, // Cygwin
POSIX_COUNT
} sys;
TCHAR root[MAX_PATH];
} POSIX;
// internal implementation of memcmp()
int compare_bytes(const void* s1, const void* s2, size_t n)
{
const unsigned char* p1 = s1;
const unsigned char* p2 = s2;
do {
int b1 = *p1++;
int b2 = *p2++;
if (b1 != b2)
return (b1 - b2);
} while (--n);
return 0;
}
// concats TCHAR string with UTF-8 string
BOOL concat_with_utf8(PTSTR pszDest, size_t cchDest, const char* src)
{
size_t cnt;
if (FAILED(StringCchLength(pszDest, cchDest, &cnt)))
return FALSE;
#ifdef UNICODE
return (MultiByteToWideChar(CP_UTF8, 0, src, -1, pszDest + cnt, cchDest - cnt) > 0);
#else
wchar_t tmp[cchDest - cnt]; // VLA
return (MultiByteToWideChar(CP_UTF8, 0, src, -1, tmp, cchDest - cnt) > 0
&& WideCharToMultiByte(CP_ACP, 0, tmp, -1, pszDest + cnt, cchDest - cnt,
NULL, NULL) > 0);
#endif // UNICODE
}
// replaces all occurences of a char in a string
void replace_char(PTSTR psz, TCHAR cTo, TCHAR cFrom)
{
for ( ; *psz; ++psz)
if (*psz == cFrom)
*psz = cTo;
}
// finds active MSYS/Cygwin installation by scanning PATH
void find_posix(POSIX* ppx)
{
// root paths
static const struct {
PCTSTR spec;
size_t tail;
} sRoot[POSIX_COUNT] = {
{ TEXT("*\\msys*\\clangarm64\\bin"), COUNT1("\\clangarm64\\bin") },
{ TEXT("*\\msys*\\mingw32\\bin"), COUNT1("\\mingw32\\bin") },
{ TEXT("*\\msys*\\mingw64\\bin"), COUNT1("\\mingw64\\bin") },
{ TEXT("*\\msys*\\ucrt64\\bin"), COUNT1("\\ucrt64\\bin") },
{ TEXT("*\\msys*\\clang32\\bin"), COUNT1("\\clang32\\bin") },
{ TEXT("*\\msys*\\clang64\\bin"), COUNT1("\\clang64\\bin") },
{ TEXT("*\\msys*\\usr\\bin"), COUNT1("\\usr\\bin") },
{ TEXT("*\\cygwin*\\bin"), COUNT1("\\bin") }
};
// nothing found yet
ppx->sys = POSIX_UNKNOWN;
// get PATH
TCHAR achPATH[4096]; // max
size_t cchPATH = (size_t)GetEnvironmentVariable(TEXT("PATH"), ARRAY(achPATH));
if (!cchPATH || cchPATH > COUNT(achPATH))
return; // unexpected error
++cchPATH; // count null-terminating character
replace_char(achPATH, TEXT('\0'), TEXT(';')); // split PATH
// find first path matching one of the patterns
size_t cch;
for (TCHAR* cp = achPATH; cchPATH; cp += cch + 1, cchPATH -= cch + 1) {
if (FAILED(StringCchLength(cp, cchPATH, &cch)))
break; // unexpected error
for (int i = 0; i < POSIX_COUNT; ++i) {
if (PathMatchSpec(cp, sRoot[i].spec)) {
ppx->sys = i;
StringCchCopyN(ARRAY(ppx->root), cp, cch - sRoot[i].tail);
return;
}
}
}
}
// sets some of MSYS/Cygwin environment variables
void setup_posix_env(POSIX* ppx)
{
// MSYS names and POSIX prefixes
static PCTSTR const pszMSYS[POSIX_COUNT][2] = {
{ TEXT("CLANGARM64"), TEXT("/clangarm64") },
{ TEXT("MINGW32"), TEXT("/mingw32") },
{ TEXT("MINGW64"), TEXT("/mingw64") },
{ TEXT("UCRT64"), TEXT("/ucrt64") },
{ TEXT("CLANG32"), TEXT("/clang32") },
{ TEXT("CLANG64"), TEXT("/clang64") },
{ TEXT("MSYS"), TEXT("/usr") },
{ NULL, NULL }
};
// set MSYSTEM, MSYSTEM_PREFIX and MINGW_PREFIX
SetEnvironmentVariable(TEXT("MSYSTEM"), pszMSYS[ppx->sys][0]);
SetEnvironmentVariable(TEXT("MSYSTEM_PREFIX"), pszMSYS[ppx->sys][1]);
SetEnvironmentVariable(TEXT("MINGW_PREFIX"),
ppx->sys == POSIX_MSYS ? NULL : pszMSYS[ppx->sys][1]);
// set USER and HOSTNAME
TCHAR tmp[256]; // max
if (GetEnvironmentVariable(TEXT("USERNAME"), ARRAY(tmp)))
SetEnvironmentVariable(TEXT("USER"), tmp);
if (GetComputerNameEx(ComputerNameDnsHostname, tmp, &(DWORD){COUNT(tmp)}))
SetEnvironmentVariable(TEXT("HOSTNAME"), tmp);
}
// converts POSIX path to native path
BOOL convert_path(PTSTR pszTo, size_t cchTo, POSIX* ppx, const char* from)
{
TCHAR tmp[MAX_PATH];
if (*from == '\\' || (IS_LATIN(from[0]) && from[1] == ':')) { // Win path
tmp[0] = TEXT('\0');
} else if (*from == '/') { // POSIX path
++from;
// skip over cygdrive/
if (!compare_bytes(from, ARRAY1("cygdrive/")))
from += COUNT1("cygdrive/");
// substitute: /c --> c:
if (IS_LATIN(from[0]) && from[1] == '/') {
tmp[0] = (TCHAR)from[0];
tmp[1] = TEXT(':');
tmp[2] = TEXT('/');
tmp[3] = TEXT('\0');
from += COUNT1("c/");
} else {
// start from POSIX root
StringCchCopy(ARRAY(tmp), ppx->root);
StringCchCat(ARRAY(tmp), TEXT("/"));
if (ppx->sys == POSIX_CYGWIN) {
// /usr/bin --> /bin
if (!compare_bytes(from, ARRAY1("usr/bin/"))) {
StringCchCat(ARRAY(tmp), TEXT("bin/"));
from += COUNT1("usr/bin/");
}
} else {
// /bin --> /usr/bin
if (!compare_bytes(from, ARRAY1("bin/"))) {
StringCchCat(ARRAY(tmp), TEXT("usr/bin/"));
from += COUNT1("bin/");
}
}
}
} else { // relative path
GetCurrentDirectory(COUNT(tmp), tmp);
StringCchCat(ARRAY(tmp), TEXT("/"));
}
// add the rest
if (!concat_with_utf8(ARRAY(tmp), from))
return FALSE;
// apply native separators and .exe extension
replace_char(tmp, TEXT('\\'), TEXT('/')); // to Win separators
PathAddExtension(tmp, NULL); // note: PathCchAddExtension is for Win 8+ only
// check if file exists
if (!PathFileExists(tmp))
return FALSE;
// executable name containing spaces should be enquoted for security reasons
PathQuoteSpaces(tmp);
// copy out result
return SUCCEEDED(StringCchCopy(pszTo, cchTo, tmp));
}
// parses a shebang line
BOOL parse_line(char* line, size_t cnt, const char** ppc1, const char** ppc2)
{
char* cp;
// #!
if (cnt < 2 || *line++ != '#' || *line++ != '!')
return FALSE;
cnt -= 2;
// convert tabs to spaces, break at a newline
for (cp = line; cnt; ++cp, --cnt) {
if (*cp == '\0') // unexpected end of string
return FALSE;
if (*cp == '\n' || *cp == '\r') {
*cp = '\0';
break;
}
if (*cp == '\t')
*cp = ' ';
}
if (!cnt) // no newline
return FALSE;
// skip leading spaces
for (cp = line; *cp == ' '; ++cp) ;
if (*cp == '\0') // shell not found
return FALSE;
// store pointer to shell name
*ppc1 = cp;
// skip until next space
while (*cp && *cp != ' ') ++cp;
if (*cp == ' ') {
// have args
*cp++ = '\0';
*ppc2 = cp;
} else {
// no args
*ppc2 = NULL;
}
return TRUE;
}
// checks if file is a shell script
BOOL can_shebang(PCTSTR pszScriptName, PTSTR pszShellName, size_t cchShellName,
POSIX* ppx, PDWORD pdwErrorCode)
{
// open script file
HANDLE hScriptFile = CreateFile(pszScriptName, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hScriptFile == INVALID_HANDLE_VALUE) {
*pdwErrorCode = GetLastError();
return FALSE;
}
BOOL shebang = FALSE;
char buf[cchShellName]; // VLA
DWORD cb;
const char *pc1, *pc2;
if (!ReadFile(hScriptFile, buf, (DWORD)cchShellName, &cb, NULL)) {
// cannot read file
*pdwErrorCode = GetLastError();
} else if (!parse_line(buf, (size_t)cb, &pc1, &pc2)) {
// not a script
*pdwErrorCode = ERROR_BAD_FORMAT;
} else if (!convert_path(pszShellName, cchShellName, ppx, pc1)) {
// invalid shell
*pdwErrorCode = ERROR_PATH_NOT_FOUND;
} else {
// process shebang args if any
shebang = pc2 ? (
SUCCEEDED(StringCchCat(pszShellName, cchShellName, TEXT(" "))) &&
concat_with_utf8(pszShellName, cchShellName, pc2)
) : TRUE;
if (!shebang) // invalid shebang args
*pdwErrorCode = ERROR_BAD_ARGUMENTS;
}
CloseHandle(hScriptFile);
return shebang;
}
// prints error message and quits the application
__declspec(noreturn)
void print_error_and_exit(DWORD dwErrorCode)
{
LPTSTR pszErrorText;
DWORD cchErrorText = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&pszErrorText,
0, NULL);
if (cchErrorText > 0) {
HANDLE hStdErr = GetStdHandle(STD_ERROR_HANDLE);
DWORD cb;
WriteConsole(hStdErr, ARRAY1(TEXT(PROGRAM_NAME " error: ")), &cb, NULL);
WriteConsole(hStdErr, pszErrorText, cchErrorText, &cb, NULL);
// no need to free memory before exit
//HeapFree(GetProcessHeap(), 0, pszErrorText);
}
ExitProcess(dwErrorCode);
}
// application entry point
int _tmain(void)
{
DWORD dwErrorCode;
#ifndef UNICODE
// get rid of OEM codepage
SetConsoleOutputCP(GetACP());
#endif // UNICODE
// find our basename
TCHAR szName[MAX_PATH];
GetModuleFileName(NULL, ARRAY(szName));
PathStripPath(szName);
PathRemoveExtension(szName); // note: PathCchRemoveExtension is for Win 8+ only
// check if we're renamed
if (CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, szName, -1,
TEXT(PROGRAM_NAME), -1) == CSTR_EQUAL) {
WriteConsole(GetStdHandle(STD_ERROR_HANDLE), ARRAY1(TEXT(
"Hello Windows(R) world! It\'s me, humble \'" PROGRAM_NAME "\' utility.\n\n"
"I can help you to execute MSYS/Cygwin shell scripts from your native \'cmd\',\n"
"but only if you already have it on your PATH.\n\n"
"All you have to do is to rename or symlink me, so that I match the script you want.\n"
"And, of course, please, make sure that we\'re both on the PATH too.\n\n"
"Let\'s do it!\n\n"
)), &(DWORD){0}, NULL);
print_error_and_exit(ERROR_CANT_RESOLVE_FILENAME);
}
// find POSIX root
POSIX px;
find_posix(&px);
if (px.sys == POSIX_UNKNOWN)
print_error_and_exit(ERROR_INVALID_ENVIRONMENT);
// find matching shell script on PATH
if (!PathFindOnPath(szName, NULL))
print_error_and_exit(ERROR_FILE_NOT_FOUND);
// can she bang?
TCHAR szShellCmd[MAX_PATH];
if (!can_shebang(szName, ARRAY(szShellCmd), &px, &dwErrorCode))
print_error_and_exit(dwErrorCode);
// prepare script name for passing onto the shell
PathQuoteSpaces(szName);
replace_char(szName, TEXT('/'), TEXT('\\')); // to POSIX separators
// make command line
TCHAR szCmdLine[32768]; // max
StringCchCopy(ARRAY(szCmdLine), szShellCmd); // shell + shebang args
StringCchCat(ARRAY(szCmdLine), TEXT(" ")); // space
StringCchCat(ARRAY(szCmdLine), szName); // script
PTSTR pszRawArgs = PathGetArgs(GetCommandLine());
if (pszRawArgs && *pszRawArgs) {
StringCchCat(ARRAY(szCmdLine), TEXT(" ")); // space
StringCchCat(ARRAY(szCmdLine), pszRawArgs); // our args
}
// setup POSIX environment
setup_posix_env(&px);
// launch shell
PROCESS_INFORMATION pi = {0};
if (!CreateProcess(NULL, szCmdLine, NULL, NULL, FALSE, 0, NULL, NULL,
&(STARTUPINFO){.cb = sizeof(STARTUPINFO)}, &pi))
print_error_and_exit(GetLastError());
// wait for the child and exit
WaitForSingleObject(pi.hProcess, INFINITE);
GetExitCodeProcess(pi.hProcess, &dwErrorCode);
// no need to close handles before exit
//CloseHandle(pi.hProcess);
//CloseHandle(pi.hThread);
return (int)dwErrorCode;
}
// micro CRT startup code
#if __has_include("nocrt0c.c")
#define ARGV none
#include "nocrt0c.c"
#endif