os: New OS abstraction module.
[mesa.git] / src / gallium / auxiliary / os / os_misc.c
1 /**************************************************************************
2 *
3 * Copyright 2008-2010 Vmware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "os_misc.h"
30
31 #include <stdarg.h>
32
33
34 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
35
36 #include <windows.h>
37 #include <winddi.h>
38
39 #elif defined(PIPE_SUBSYSTEM_WINDOWS_CE)
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <windows.h>
44 #include <types.h>
45
46 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
47
48 #ifndef WIN32_LEAN_AND_MEAN
49 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
50 #endif
51 #include <windows.h>
52 #include <stdio.h>
53
54 #else
55
56 #include <stdio.h>
57 #include <stdlib.h>
58
59 #endif
60
61
62 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
63 static INLINE void
64 _EngDebugPrint(const char *format, ...)
65 {
66 va_list ap;
67 va_start(ap, format);
68 EngDebugPrint("", (PCHAR)format, ap);
69 va_end(ap);
70 }
71 #endif
72
73
74 void
75 os_log_message(const char *message)
76 {
77 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
78 _EngDebugPrint("%s", message);
79 #elif defined(PIPE_SUBSYSTEM_WINDOWS_USER)
80 OutputDebugStringA(message);
81 if(GetConsoleWindow() && !IsDebuggerPresent()) {
82 fflush(stdout);
83 fputs(message, stderr);
84 fflush(stderr);
85 }
86 #elif defined(PIPE_SUBSYSTEM_WINDOWS_CE)
87 wchar_t *wide_format;
88 long wide_str_len;
89 /* Format is ascii - needs to be converted to wchar_t for printing */
90 wide_str_len = MultiByteToWideChar(CP_ACP, 0, message, -1, NULL, 0);
91 wide_format = (wchar_t *) malloc((wide_str_len+1) * sizeof(wchar_t));
92 if (wide_format) {
93 MultiByteToWideChar(CP_ACP, 0, message, -1,
94 wide_format, wide_str_len);
95 NKDbgPrintfW(wide_format, wide_format);
96 free(wide_format);
97 }
98 #elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
99 /* TODO */
100 #else /* !PIPE_SUBSYSTEM_WINDOWS */
101 fflush(stdout);
102 fputs(message, stderr);
103 #endif
104 }
105
106
107 #ifdef PIPE_SUBSYSTEM_WINDOWS_DISPLAY
108 static const char *
109 find(const char *start, const char *end, char c)
110 {
111 const char *p;
112 for(p = start; !end || p != end; ++p) {
113 if(*p == c)
114 return p;
115 if(*p < 32)
116 break;
117 }
118 return NULL;
119 }
120
121 static int
122 compare(const char *start, const char *end, const char *s)
123 {
124 const char *p, *q;
125 for(p = start, q = s; p != end && *q != '\0'; ++p, ++q) {
126 if(*p != *q)
127 return 0;
128 }
129 return p == end && *q == '\0';
130 }
131
132 static void
133 copy(char *dst, const char *start, const char *end, size_t n)
134 {
135 const char *p;
136 char *q;
137 for(p = start, q = dst, n = n - 1; p != end && n; ++p, ++q, --n)
138 *q = *p;
139 *q = '\0';
140 }
141 #endif
142
143
144 const char *
145 os_get_option(const char *name)
146 {
147 #if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
148 /* EngMapFile creates the file if it does not exists, so it must either be
149 * disabled on release versions (or put in a less conspicuous place). */
150 #ifdef DEBUG
151 const char *result = NULL;
152 ULONG_PTR iFile = 0;
153 const void *pMap = NULL;
154 const char *sol, *eol, *sep;
155 static char output[1024];
156
157 pMap = EngMapFile(L"\\??\\c:\\gallium.cfg", 0, &iFile);
158 if(pMap) {
159 sol = (const char *)pMap;
160 while(1) {
161 /* TODO: handle LF line endings */
162 eol = find(sol, NULL, '\r');
163 if(!eol || eol == sol)
164 break;
165 sep = find(sol, eol, '=');
166 if(!sep)
167 break;
168 if(compare(sol, sep, name)) {
169 copy(output, sep + 1, eol, sizeof(output));
170 result = output;
171 break;
172 }
173 sol = eol + 2;
174 }
175 EngUnmapFile(iFile);
176 }
177 return result;
178 #else
179 return NULL;
180 #endif
181 #elif defined(PIPE_SUBSYSTEM_WINDOWS_CE) || defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
182 /* TODO: implement */
183 return NULL;
184 #else
185 return getenv(name);
186 #endif
187 }
188