From: Robert Pengelly Date: Tue, 25 Mar 2025 23:32:22 +0000 (+0000) Subject: WIN32 bux fixes X-Git-Url: https://git.candlhat.org/?a=commitdiff_plain;h=67571703832a2ff1f226cdf96af2a8e788f026ac;p=xmake.git WIN32 bux fixes --- diff --git a/variable.c b/variable.c index 2e69435..6900cb0 100644 --- a/variable.c +++ b/variable.c @@ -51,6 +51,54 @@ static char *func_error (const char *filename, unsigned long line_no, char *inpu #if !defined (__PDOS__) && !defined (__MSDOS__) # if defined (_WIN32) || defined (__WIN32__) # include +static char *internal_commands[] = { + + "assoc", + "break", + "call", + "cd", + "chcp", + "chdir", + "cls", + "color", + "copy", + "ctty", + "date", + "del", + "dir", + "echo", + "echo.", + "endlocal", + "erase", + "exit", + "for", + "ftype", + "goto", + "if", + "md", + "mkdir", + "move", + "path", + "pause", + "prompt", + "rd", + "rem", + "ren", + "rename", + "rmdir", + "set", + "setlocal", + "shift", + "time", + "title", + "type", + "ver", + "verify", + "vol", + 0 + +}; + static char *func_shell (const char *filename, unsigned long line_no, char *input) { DWORD dwExitCode; @@ -62,10 +110,13 @@ static char *func_shell (const char *filename, unsigned long line_no, char *inpu HANDLE hStdInPipeRead, hStdInPipeWrite; HANDLE hStdOutPipeRead, hStdOutPipeWrite; - char *lpApplicationName = 0, *lpCommandLine = skip_whitespace (input), *p, ch; + char *lpApplicationName = 0, *lpCommandLine = 0; + char *p, ch; + struct linebuf lbuf; + input = skip_whitespace (input); - for (p = lpCommandLine; !isspace ((int) *p); p++) { + for (p = input; !isspace ((int) *p); p++) { if (*p == '/') { *p = '\\'; @@ -73,6 +124,51 @@ static char *func_shell (const char *filename, unsigned long line_no, char *inpu } + if ((p = strchr (input, ' '))) { + + lpApplicationName = xstrndup (input, p - input); + lpCommandLine = xstrdup (skip_whitespace (p)); + + } else { + lpCommandLine = xstrdup (input); + } + + if (!(p = strchr (lpApplicationName, '\\'))) { + + unsigned long count = sizeof (internal_commands) / sizeof (internal_commands[0]), i; + + for (i = 0; i < count; i++) { + + if (strcasecmp (lpApplicationName, internal_commands[i]) == 0) { + + if (lpApplicationName) { + free (lpApplicationName); + } + + if (lpCommandLine) { + free (lpCommandLine); + } + + lpCommandLine = xmalloc (4 + strlen (input) + 1); + sprintf (lpCommandLine, "/C %s", input); + + lpApplicationName = xstrdup (getenv ("COMSPEC")); + break; + + } + + } + + } else { + + if (lpApplicationName) { + free (lpApplicationName); + } + + lpApplicationName = 0; + + } + memset (&sa, 0, sizeof (sa)); memset (&si, 0, sizeof (si)); memset (&pi, 0, sizeof (pi)); diff --git a/xmake.c b/xmake.c index f89f5f7..7481b44 100644 --- a/xmake.c +++ b/xmake.c @@ -56,11 +56,18 @@ static void pipe_command (const char *filename, unsigned long line_no, char *inp DWORD dwExitCode; PROCESS_INFORMATION pi; + SECURITY_ATTRIBUTES sa; STARTUPINFO si; - char *lpApplicationName = 0, *lpCommandLine = skip_whitespace (input), *p; + HANDLE hStdInPipeRead, hStdInPipeWrite; + HANDLE hStdOutPipeRead, hStdOutPipeWrite; - for (p = lpCommandLine; !isspace ((int) *p); p++) { + char *lpApplicationName = getenv ("COMSPEC"), *lpCommandLine = 0, *p; + struct linebuf lbuf; + + input = skip_whitespace (input); + + for (p = input; !isspace ((int) *p); p++) { if (*p == '/') { *p = '\\'; @@ -68,12 +75,61 @@ static void pipe_command (const char *filename, unsigned long line_no, char *inp } + lpCommandLine = xmalloc (4 + strlen (input) + 1); + sprintf (lpCommandLine, "/C %s", input); + + memset (&sa, 0, sizeof (sa)); memset (&si, 0, sizeof (si)); memset (&pi, 0, sizeof (pi)); + sa.nLength = sizeof (sa); + sa.bInheritHandle = 1; + + if (!CreatePipe (&hStdInPipeRead, &hStdInPipeWrite, &sa, 0)) { + + fprintf (stderr, "process_begin: CreateProcess(NULL, %s, ...) failed.\n", input); + fprintf (stderr, "%s: %s:%lu: pipe: %s\n", program_name, filename, line_no, strerror (GetLastError ())); + + exit (EXIT_FAILURE); + + } + + if (!SetHandleInformation (hStdInPipeRead, HANDLE_FLAG_INHERIT, 0)) { + + fprintf (stderr, "process_begin: CreateProcess(NULL, %s, ...) failed.\n", input); + fprintf (stderr, "%s: %s:%lu: pipe: %s\n", program_name, filename, line_no, strerror (GetLastError ())); + + exit (EXIT_FAILURE); + + } + + if (!CreatePipe (&hStdOutPipeRead, &hStdOutPipeWrite, &sa, 0)) { + + fprintf (stderr, "process_begin: CreateProcess(NULL, %s, ...) failed.\n", input); + fprintf (stderr, "%s: %s:%lu: pipe: %s\n", program_name, filename, line_no, strerror (GetLastError ())); + + exit (EXIT_FAILURE); + + } + + if (!SetHandleInformation (hStdOutPipeRead, HANDLE_FLAG_INHERIT, 0)) { + + fprintf (stderr, "process_begin: CreateProcess(NULL, %s, ...) failed.\n", input); + fprintf (stderr, "%s: %s:%lu: pipe: %s\n", program_name, filename, line_no, strerror (GetLastError ())); + + exit (EXIT_FAILURE); + + } + si.cb = sizeof (si); - if (!CreateProcess (lpApplicationName, lpCommandLine, 0, 0, 0, 0, 0, 0, &si, &pi)) { + si.hStdError = hStdOutPipeWrite; + si.hStdOutput = hStdOutPipeWrite; + si.hStdInput = hStdInPipeRead; + + si.dwFlags |= STARTF_USESTDHANDLES; + + if (!CreateProcess (lpApplicationName, lpCommandLine, 0, 0, 1, 0, 0, 0, &si, &pi)) { fprintf (stderr, "process_begin: CreateProcess(NULL, %s, ...) failed.\n", input); fprintf (stderr, "%s: %s:%lu: pipe: %s\n", program_name, filename, line_no, strerror (GetLastError ())); @@ -82,6 +138,43 @@ static void pipe_command (const char *filename, unsigned long line_no, char *inp } + CloseHandle (hStdOutPipeWrite); + CloseHandle (hStdInPipeRead); + + /*WaitForSingleObject (pi.hProcess, INFINITE);*/ + lbuf.start = xmalloc (lbuf.size = 256); + + { + + char *end = lbuf.start + lbuf.size; + char *p = lbuf.start; + + DWORD dwRead; + + while (ReadFile (hStdOutPipeRead, p, end - p, &dwRead, 0)) { + + size_t offset; + p += strlen (p); + + offset = p - lbuf.start; + + if (end - p >= 80) { + continue; + } + + lbuf.size *= 2; + lbuf.start = xrealloc (lbuf.start, lbuf.size); + + p = lbuf.start + offset; + end = lbuf.start + lbuf.size; + + } + + } + + CloseHandle (hStdOutPipeRead); + CloseHandle (hStdInPipeWrite); + GetExitCodeProcess (pi.hProcess, &dwExitCode); CloseHandle (pi.hProcess); @@ -97,7 +190,7 @@ static void pipe_command (const char *filename, unsigned long line_no, char *inp p = xstrndup (p, space - p); } - fprintf (stderr, "%s: %s: %s\n", program_name, p, strerror (GetLastError ())); + fprintf (stderr, "%s", lbuf.start); fprintf (stderr, "%s: *** [%s:%lu: %s] Error %ld\n", program_name, filename, line_no, name, dwExitCode); exit (EXIT_FAILURE); @@ -650,10 +743,10 @@ int main (int argc, char **argv) { variable_add (xstrdup ("OS"), xstrdup (os_name), VAR_ORIGIN_FILE); variable_add (xstrdup ("MAKE"), xstrdup (argv[0]), VAR_ORIGIN_FILE); -#if defined (_WIN32) || defined (__WIN32__) || defined (__MSDOS__) - variable_add (xstrdup ("SHELL"), xstrdup ("sh.exe"), VAR_ORIGIN_FILE); -#else +#if defined (unix) || defined (__unix) || defined (__unix__) || defined (__APPLE__) variable_add (xstrdup ("SHELL"), xstrdup ("/bin/sh"), VAR_ORIGIN_FILE); +#else + variable_add (xstrdup ("SHELL"), xstrdup ("sh.exe"), VAR_ORIGIN_FILE); #endif state = xmalloc (sizeof (*state));