summaryrefslogtreecommitdiff
path: root/src/winconsole.c
diff options
context:
space:
mode:
authordon bright <hugh.m.bright@gmail.com>2013-01-28 02:42:20 (GMT)
committerdon bright <hugh.m.bright@gmail.com>2013-01-28 02:42:20 (GMT)
commit1e64dddf1ea30282c89de7f35854a68614234652 (patch)
tree165d37c1c66f6ff79d48c74794238b3f0bed09da /src/winconsole.c
parent5c779159c208ca3d88c88479ab29f9cd66574859 (diff)
parentd0856efe6da545693f9c50a8a2514a9f999ab5ef (diff)
Merge branch 'master' of github.com:openscad/openscad into issue159
Diffstat (limited to 'src/winconsole.c')
-rw-r--r--src/winconsole.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/winconsole.c b/src/winconsole.c
new file mode 100644
index 0000000..de8e682
--- /dev/null
+++ b/src/winconsole.c
@@ -0,0 +1,84 @@
+/*
+ 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 use
+ pure C to minimize binary size when cross-compiling (~10kbytes). See Also:
+
+ 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
+
+ TODO:
+ Work with unicode: http://www.i18nguy.com/unicode/c-unicode.html
+*/
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#define MAXCMDLEN 64000
+#define BUFFSIZE 42
+
+int main( int argc, char * argv[] )
+{
+ FILE *cmd_stdout;
+ char cmd[MAXCMDLEN];
+ char buffer[BUFFSIZE];
+ char *fgets_result;
+ int eof = 0;
+ int pclose_result;
+ int i;
+ int result = 0;
+
+ strcat( cmd, "\0" );
+ strcat( cmd, "openscad.exe" );
+ for ( i = 1 ; i < argc ; ++i ) {
+ strcat( cmd, " " );
+ strcat( cmd, argv[i] );
+ }
+ strcat( cmd, " ");
+ strcat( cmd, " 2>&1"); // capture stderr and stdout
+
+ cmd_stdout = _popen( cmd, "rt" );
+ if ( cmd_stdout == NULL ) {
+ printf( "Error opening _popen for command: %s", cmd );
+ perror( "Error message:" );
+ return 1;
+ }
+
+ while ( !eof )
+ {
+ fgets_result = fgets( buffer, BUFFSIZE, cmd_stdout );
+ if ( fgets_result == NULL ) {
+ if ( ferror( cmd_stdout ) ) {
+ printf("Error reading from stdout of %s\n", cmd);
+ result = 1;
+ }
+ if ( feof( cmd_stdout ) ) {
+ eof = 1;
+ }
+ } else {
+ fprintf( stdout, "%s", buffer );
+ }
+ }
+
+ pclose_result = _pclose( cmd_stdout );
+ if ( pclose_result < 0 ) {
+ perror("Error while closing stdout for command:");
+ result = 1;
+ }
+
+ return result;
+}
contact: Jan Huwald // Impressum