File: winconsole.c

package info (click to toggle)
openscad 2015.03-2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 30,804 kB
  • ctags: 5,692
  • sloc: cpp: 39,386; sh: 3,856; ansic: 3,674; python: 1,393; yacc: 496; lex: 272; lisp: 159; makefile: 67; xml: 60
file content (207 lines) | stat: -rw-r--r-- 6,677 bytes parent folder | download | duplicates (5)
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
/*
 Enable easy piping under Windows(TM) command line.

 We use the 'devenv'(TM) method, which means we have two binary files:

  openscad.com, with IMAGE_SUBSYSTEM_WINDOWS_CUI flag set
  openscad.exe, with IMAGE_SUBSYSTEM_WINDOWS_GUI flag set

 The .com version is a 'wrapper' for the .exe version. If you call
 'openscad' with no extension from a script or shell, the .com version
 is prioritized by the OS and feeds the GUI stdout to the console.
 We keep it in 'pure c' to minimize binary size on the cross-compile.

 Note the .com file is not a 'real' .com file. It is just an .exe
 that has been renamed to a .com during the Windows(TM) OpenSCAD package build.

 See Also:

 ../doc/windows_issues.txt
 http://stackoverflow.com/questions/493536/can-one-executable-be-both-a-console-and-gui-app
 http://blogs.msdn.com/b/oldnewthing/archive/2009/01/01/9259142.aspx
 http://blogs.msdn.com/b/junfeng/archive/2004/02/06/68531.aspx
 http://msdn.microsoft.com/en-us/library/aa298534%28v=vs.60%29.aspx
 http://cournape.wordpress.com/2008/07/29/redirecting-stderrstdout-in-cmdexe/
 Open Group popen() documentation
 inkscapec by Jos Hirth work at http://kaioa.com
 Nop Head's OpenSCAD_cl at github.com
 ImageMagick's utilities, like convert.cc
 http://www.i18nguy.com/unicode/c-unicode.html

A few other notes:

We throw out argc/argv and pull the w_char commandline using special
Win(TM) functions. We then strip out the 'openscad' program name, but
leave the rest of the commandline in tact in a tail. Then we call
'openscad.exe'.

stderr is used to support writing of STL/PNG etc. output to stdout.
MS Windows API used instead of popen() to append stdout to stderr and
avoid running cmd.exe (so save some resources).

TODO:

Fix printing of unicode on console.
*/

#include <windows.h>
#include <process.h>
#include <io.h>
#include <fcntl.h>
/*#include <stdio.h>*/
#include <string.h>

// manage MS Windows error codes
// Do not use fprintf() etc. due to call from thread.
static void displayError(char *msg, DWORD errcode) {
	HANDLE hError = GetStdHandle(STD_ERROR_HANDLE);
	char buffer[1024];
	if (msg && *msg) {
		WriteFile(hError, msg, strlen(msg), NULL, NULL);
	}
	if (ERROR_SUCCESS == errcode) return;
	if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errcode, 0,
		buffer, sizeof(buffer), NULL)) {
		WriteFile(hError, buffer, strlen(buffer), NULL, NULL);
	}
}

static void displayLastError(char *msg) {
	displayError(msg, GetLastError());
}

typedef struct {
	/*volatile*/ int status;
	HANDLE hRead, hOutput;
	STARTUPINFOW startupInfo;
	PROCESS_INFORMATION processInfo;
} WATCH_INFO;

static DWORD WINAPI watchdog(LPVOID arg) {
#define info ((WATCH_INFO*)arg)
	DWORD bc;
	char buffer[1024];
	for (info->status=0;;) {
		if (!ReadFile(info->hRead,buffer,sizeof(buffer),&bc,NULL)) {
			displayLastError("Failed to read pipe\n");
			/* return 1; */ info->status = 1;
		} else if (bc) {
		    (void)WriteFile(info->hOutput, buffer, bc, NULL, NULL);
		} else break; // closed pipe
	}
	return 0;
#undef info
}

#define EXE_NAME "openscad.exe"
#define ___WIDECHARTEXT(s) L##s
#define W(x) ___WIDECHARTEXT(x)

#define IS_WHITESPACE(c) (' ' == (c) || '\t' == (c))
#define MAXCMDLEN 32768 /* MS Windows limit */

int main(int argc, char *argv[]) {
	HANDLE hWrite, curr_proc;
	SECURITY_ATTRIBUTES sa;
	WATCH_INFO info;
	wchar_t cmd[MAXCMDLEN];
	DWORD status;
	int result = 0;
	register int i;
	/*bool*/ int quote, preserve_stdout;
	register wchar_t *cmdline = GetCommandLineW();
	// Look for the end of executable
	// There is no need to check for escaped double quotes here
	// because MS Windows file name can not contain such quotes
	for (quote=0; *cmdline && (quote || !IS_WHITESPACE(*cmdline)); ++cmdline) {
		if ('"' == *cmdline) quote ^= 1;
	}
	if (IS_WHITESPACE(*cmdline)) {
		while (IS_WHITESPACE(*(cmdline+1))) ++cmdline;
	}
	(void)wcscpy(cmd, W(EXE_NAME));
	if (wcslen(cmd) + wcslen(cmdline) >= MAXCMDLEN) {
		// fprintf(stderr, "Command line length exceeds limit of %d\n", MAXCMDLEN);
		// avoid fprintf() to decrease executable size
		displayError("Command line length exceeds limit\n", ERROR_SUCCESS);
		return 1;
	}
	(void)wcscat(cmd, cmdline);

	// look for '-o -' combination
	for (preserve_stdout=FALSE, i=/*sic!*/2; i<argc; ++i) {
		register char *s = argv[i];
		if ('-' == s[0] && '\0' == s[1]) // it is "-"
			if (!strcmp("-o", argv[i-1])) {
				preserve_stdout = TRUE; break;
			}
	}

	ZeroMemory(&info, sizeof(info));

	info.startupInfo.cb = sizeof(info.startupInfo);
	info.startupInfo.dwFlags = STARTF_USESTDHANDLES;
	info.startupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
	info.startupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	info.hOutput = GetStdHandle(STD_ERROR_HANDLE);

	sa.nLength = sizeof(sa);
	sa.lpSecurityDescriptor = NULL;
	sa.bInheritHandle = FALSE;
	if (!CreatePipe(&info.hRead, &hWrite, &sa, 1024)) {
		displayLastError("CreatePipe(): ");
		return 1;
	}
	// make inheritable write handle(s)
	curr_proc = GetCurrentProcess();
	if (!DuplicateHandle(curr_proc, hWrite, curr_proc,
		&info.startupInfo.hStdError, 0L, TRUE, DUPLICATE_SAME_ACCESS)) {
		displayLastError("DuplicateHandle(): ");
		return 1;
	}
	if (!preserve_stdout) {
		info.hOutput = info.startupInfo.hStdOutput;
#if 1 // Should I really duplicate handle or plain copy suffice?
		if (!DuplicateHandle(curr_proc, hWrite, curr_proc,
			&info.startupInfo.hStdOutput, 0L, TRUE, DUPLICATE_SAME_ACCESS)) {
			displayLastError("DuplicateHandle(): ");
			return 1;
		}
#else
		info.startupInfo.hStdOutput = info.startupInfo.hStdError;
#endif
	}
	(void)CloseHandle(hWrite);
	if (!CreateProcessW(NULL, cmd, NULL, NULL, TRUE,
		CREATE_UNICODE_ENVIRONMENT, NULL, NULL,
		&info.startupInfo, &info.processInfo)) {
		// fwprintf(stderr, L"Cannot run: %s\n", cmd);
		// avoid fwprintf() to decrease executable size
		// fputws(L"Cannot run: ", stderr); fputws(cmd, stderr); fputws(L"\n", stderr);
		displayLastError("Cannot run " EXE_NAME "\n");
		return 1;
	}
	// Create thread to work around ReadFile() blocking
	if (!CreateThread(NULL, 32768, watchdog, &info, 0, NULL)) {
		displayLastError("CreateThread(): ");
		result = 1;
	}
	// WaitForSingleObject() returns zero on success
	if (WaitForSingleObject(info.processInfo.hProcess, INFINITE)) {
		displayLastError("WaitForSingleObject(): ");
		result = 1;
	}
	if (!GetExitCodeProcess(info.processInfo.hProcess, &status)) {
		displayLastError("GetExitCodeProcess(): ");
		result = 1;
	}
	if (!result) {
		if (info.status) result = info.status;
		else result = status; // return value of openscad.exe
	}

	// All currently open streams and handles will be
	// closed automatically upon the process termination.
	return result;
}