1 /*  Part of XPCE --- The SWI-Prolog GUI toolkit
2 
3     Author:        Jan Wielemaker and Anjo Anjewierden
4     E-mail:        jan@swi.psy.uva.nl
5     WWW:           http://www.swi.psy.uva.nl/projects/xpce/
6     Copyright (c)  1995-2013, University of Amsterdam
7     All rights reserved.
8 
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions
11     are met:
12 
13     1. Redistributions of source code must retain the above copyright
14        notice, this list of conditions and the following disclaimer.
15 
16     2. Redistributions in binary form must reproduce the above copyright
17        notice, this list of conditions and the following disclaimer in
18        the documentation and/or other materials provided with the
19        distribution.
20 
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25     COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32     POSSIBILITY OF SUCH DAMAGE.
33 */
34 
35 #include <stdio.h>
36 #include <stdarg.h>
37 
38 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39 XPCE Console IO is only used  for   debugging  purposes. On Unix systems
40 this IO will normally be bound to  Unix stdout/stdin. On strictly window
41 based systems you may redefine these functions to use a window.
42 
43     void
44     vCprintf(const char *fmt, va_list args)
45 	Behaves like: vprintf(fmt, args);
46 
47     int
48     Cputchar(int chr)
49 	Behaves like: putchar(chr);
50 
51     void
52     Cflush(void)
53 	Behaves like fflush(stdout);
54 
55     char *
56     Cgetline(char *buf, int size)
57 	Behaves like: fgets(buf, size, stdin);
58 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
59 
60 #ifdef __WINDOWS__
61 
62 static FILE *console_in = NULL;
63 static FILE *console_out = NULL;
64 
65 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
66 Whenever a stand-alone XPCE/Something application  writes explicitely to
67 the console, this stub will allocate a console to write to. Note the use
68 of _IONBF, instead of _IOLBF  which  would   be  much  more  natural. It
69 doesn't appear to work however (Windows-NT 4.0, MSVC 4.2).
70 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
71 
72 #include <windows.h>
73 #include <io.h>
74 #include <fcntl.h>
75 
76 static int
ensure_console(void)77 ensure_console(void)
78 { static int allocated = 0;
79 
80   if ( !allocated )
81   { allocated++;
82     if ( AllocConsole() )
83     { HANDLE hin  = GetStdHandle(STD_INPUT_HANDLE);
84       HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
85       int in  = _open_osfhandle((intptr_t)hin, _O_RDONLY);
86       int out = _open_osfhandle((intptr_t)hout, _O_APPEND);
87 
88       console_in  = _fdopen(in, "r");
89       console_out = _fdopen(out, "w");
90 
91       setvbuf(console_in,  NULL, _IONBF, 256);
92       setvbuf(console_out, NULL, _IONBF, 256);
93      }
94   }
95 
96   return 1;
97 }
98 
99 #else /* ~__WINDOWS__ */
100 
101 static FILE *console_in = NULL;
102 static FILE *console_out = NULL;
103 
104 static int
ensure_console()105 ensure_console()
106 { console_in = stdin;
107   console_out = stdout;
108 
109   return 1;
110 }
111 
112 #endif /*__WINDOWS__*/
113 
114 void
Stub__vCprintf(const char * fmt,va_list args)115 Stub__vCprintf(const char *fmt, va_list args)
116 { if ( ensure_console() )
117     vfprintf(console_out, fmt, args);
118 }
119 
120 
121 int
Stub__Cputchar(int chr)122 Stub__Cputchar(int chr)
123 { if ( ensure_console() )
124     return fputc(chr, console_out);
125   else
126     return EOF;				/* signal error */
127 }
128 
129 
130 char *
Stub__Cgetline(char * line,int size)131 Stub__Cgetline(char *line, int size)
132 { if ( ensure_console() )
133     return fgets(line, size, console_in);
134   else
135     return NULL;			/* signal error */
136 }
137 
138 
139 void
Stub__Cflush()140 Stub__Cflush()
141 { if ( ensure_console() )
142     fflush(console_out);
143 }
144