dbghelp: Support pointer types of various sizes.
[wine.git] / programs / winevdm / winevdm.c
blob65f4bf2b87df840adc362c57c026bd952960b061
1 /*
2 * Wine virtual DOS machine
4 * Copyright 2003 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/winbase16.h"
27 #include "winuser.h"
28 #include "wincon.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(winevdm);
33 extern void __wine_load_dos_exe( LPCSTR filename, LPCSTR cmdline );
36 /*** PIF file structures ***/
37 #include "pshpack1.h"
39 /* header of a PIF file */
40 typedef struct {
41 BYTE unk1[2]; /* 0x00 */
42 CHAR windowtitle[ 30 ]; /* 0x02 seems to be padded with blanks*/
43 WORD memmax; /* 0x20 */
44 WORD memmin; /* 0x22 */
45 CHAR program[63]; /* 0x24 seems to be zero terminated */
46 BYTE hdrflags1; /* 0x63 various flags:
47 * 02 286: text mode selected
48 * 10 close window at exit
50 BYTE startdrive; /* 0x64 */
51 char startdir[64]; /* 0x65 */
52 char optparams[64]; /* 0xa5 seems to be zero terminated */
53 BYTE videomode; /* 0xe5 */
54 BYTE unkn2; /* 0xe6 ?*/
55 BYTE irqlow; /* 0xe7 */
56 BYTE irqhigh; /* 0xe8 */
57 BYTE rows; /* 0xe9 */
58 BYTE cols; /* 0xea */
59 BYTE winY; /* 0xeb */
60 BYTE winX; /* 0xec */
61 WORD unkn3; /* 0xed 7??? */
62 CHAR unkn4[64]; /* 0xef */
63 CHAR unkn5[64]; /* 0x12f */
64 BYTE hdrflags2; /* 0x16f */
65 BYTE hdrflags3; /* 0x170 */
66 } pifhead_t;
68 /* record header: present on every record */
69 typedef struct {
70 CHAR recordname[16]; /* zero terminated */
71 WORD posofnextrecord; /* file offset, 0xffff if last */
72 WORD startofdata; /* file offset */
73 WORD sizeofdata; /* data is expected to follow directly */
74 } recordhead_t;
76 /* 386 -enhanced mode- record */
77 typedef struct {
78 WORD memmax; /* memory desired, overrides the pif header*/
79 WORD memmin; /* memory required, overrides the pif header*/
80 WORD prifg; /* foreground priority */
81 WORD pribg; /* background priority */
82 WORD emsmax; /* EMS memory limit */
83 WORD emsmin; /* EMS memory required */
84 WORD xmsmax; /* XMS memory limit */
85 WORD xmsmin; /* XMS memory required */
86 WORD optflags; /* option flags:
87 * 0008 full screen
88 * 0004 exclusive
89 * 0002 background
90 * 0001 close when active
92 WORD memflags; /* various memory flags*/
93 WORD videoflags; /* video flags:
94 * 0010 text
95 * 0020 med. res. graphics
96 * 0040 hi. res. graphics
98 WORD hotkey[9]; /* Hot key info */
99 CHAR optparams[64]; /* optional params, replaces those in the pif header */
100 } pif386rec_t;
102 #include "poppack.h"
104 /***********************************************************************
105 * start_dos_exe
107 static void start_dos_exe( LPCSTR filename, LPCSTR cmdline )
109 MEMORY_BASIC_INFORMATION mem_info;
110 const char *reason;
112 if (VirtualQuery( NULL, &mem_info, sizeof(mem_info) ) && mem_info.State != MEM_FREE)
114 __wine_load_dos_exe( filename, cmdline );
115 if (GetLastError() == ERROR_NOT_SUPPORTED)
116 reason = "because vm86 mode is not supported on this platform";
117 else
118 reason = wine_dbg_sprintf( "It failed with error code %u", GetLastError() );
120 else reason = "because the DOS memory range is unavailable";
122 WINE_MESSAGE( "winevdm: Cannot start DOS application %s\n", filename );
123 WINE_MESSAGE( " %s.\n", reason );
124 WINE_MESSAGE( " Try running this application with DOSBox.\n" );
125 ExitProcess(1);
128 /***********************************************************************
129 * read_pif_file
130 *pif386rec_tu
131 * Read a pif file and return the header and possibly the 286 (real mode)
132 * record or 386 (enhanced mode) record. Returns FALSE if the file is
133 * invalid otherwise TRUE.
135 static BOOL read_pif_file( HANDLE hFile, char *progname, char *title,
136 char *optparams, char *startdir, int *closeonexit, int *textmode)
138 DWORD nread;
139 LARGE_INTEGER filesize;
140 recordhead_t rhead;
141 BOOL found386rec = FALSE;
142 pif386rec_t pif386rec;
143 pifhead_t pifheader;
144 if( !GetFileSizeEx( hFile, &filesize) ||
145 filesize.QuadPart < (sizeof(pifhead_t) + sizeof(recordhead_t))) {
146 WINE_ERR("Invalid pif file: size error %d\n", (int)filesize.QuadPart);
147 return FALSE;
149 SetFilePointer( hFile, 0, NULL, FILE_BEGIN);
150 if( !ReadFile( hFile, &pifheader, sizeof(pifhead_t), &nread, NULL))
151 return FALSE;
152 WINE_TRACE("header: program %s title %s startdir %s params %s\n",
153 wine_dbgstr_a(pifheader.program),
154 wine_dbgstr_an(pifheader.windowtitle, sizeof(pifheader.windowtitle)),
155 wine_dbgstr_a(pifheader.startdir),
156 wine_dbgstr_a(pifheader.optparams));
157 WINE_TRACE("header: memory req'd %d desr'd %d drive %d videomode %d\n",
158 pifheader.memmin, pifheader.memmax, pifheader.startdrive,
159 pifheader.videomode);
160 WINE_TRACE("header: flags 0x%x 0x%x 0x%x\n",
161 pifheader.hdrflags1, pifheader.hdrflags2, pifheader.hdrflags3);
162 ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL);
163 if( strncmp( rhead.recordname, "MICROSOFT PIFEX", 15)) {
164 WINE_ERR("Invalid pif file: magic string not found\n");
165 return FALSE;
167 /* now process the following records */
168 while( 1) {
169 WORD nextrecord = rhead.posofnextrecord;
170 if( (nextrecord & 0x8000) ||
171 filesize.QuadPart <( nextrecord + sizeof(recordhead_t))) break;
172 if( !SetFilePointer( hFile, nextrecord, NULL, FILE_BEGIN) ||
173 !ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL))
174 return FALSE;
175 if( !rhead.recordname[0]) continue; /* deleted record */
176 WINE_TRACE("reading record %s size %d next 0x%x\n",
177 wine_dbgstr_a(rhead.recordname), rhead.sizeofdata,
178 rhead.posofnextrecord );
179 if( !strncmp( rhead.recordname, "WINDOWS 386", 11)) {
180 found386rec = TRUE;
181 ReadFile( hFile, &pif386rec, sizeof(pif386rec_t), &nread, NULL);
182 WINE_TRACE("386rec: memory req'd %d des'd %d EMS req'd %d des'd %d XMS req'd %d des'd %d\n",
183 pif386rec.memmin, pif386rec.memmax,
184 pif386rec.emsmin, pif386rec.emsmax,
185 pif386rec.xmsmin, pif386rec.xmsmax);
186 WINE_TRACE("386rec: option 0x%x memory 0x%x video 0x%x\n",
187 pif386rec.optflags, pif386rec.memflags,
188 pif386rec.videoflags);
189 WINE_TRACE("386rec: optional parameters %s\n",
190 wine_dbgstr_a(pif386rec.optparams));
193 /* prepare the return data */
194 strncpy( progname, pifheader.program, sizeof(pifheader.program));
195 memcpy( title, pifheader.windowtitle, sizeof(pifheader.windowtitle));
196 title[ sizeof(pifheader.windowtitle) ] = '\0';
197 if( found386rec)
198 strncpy( optparams, pif386rec.optparams, sizeof( pif386rec.optparams));
199 else
200 strncpy( optparams, pifheader.optparams, sizeof(pifheader.optparams));
201 strncpy( startdir, pifheader.startdir, sizeof(pifheader.startdir));
202 *closeonexit = pifheader.hdrflags1 & 0x10;
203 *textmode = found386rec ? pif386rec.videoflags & 0x0010
204 : pifheader.hdrflags1 & 0x0002;
205 return TRUE;
208 /***********************************************************************
209 * pif_cmd
211 * execute a pif file.
213 static VOID pif_cmd( char *filename, char *cmdline)
215 HANDLE hFile;
216 char progpath[MAX_PATH];
217 char buf[128];
218 char progname[64];
219 char title[31];
220 char optparams[64];
221 char startdir[64];
222 char *p;
223 int closeonexit;
224 int textmode;
225 if( (hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
226 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
228 WINE_ERR("open file %s failed\n", wine_dbgstr_a(filename));
229 return;
231 if( !read_pif_file( hFile, progname, title, optparams, startdir,
232 &closeonexit, &textmode)) {
233 WINE_ERR( "failed to read %s\n", wine_dbgstr_a(filename));
234 CloseHandle( hFile);
235 sprintf( buf, "%s\nInvalid file format. Check your pif file.",
236 filename);
237 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
238 SetLastError( ERROR_BAD_FORMAT);
239 return;
241 CloseHandle( hFile);
242 if( (p = strrchr( progname, '.')) && !strcasecmp( p, ".bat"))
243 WINE_FIXME(".bat programs in pif files are not supported.\n");
244 /* first change dir, so the search below can start from there */
245 if( startdir[0] && !SetCurrentDirectoryA( startdir)) {
246 WINE_ERR("Cannot change directory %s\n", wine_dbgstr_a( startdir));
247 sprintf( buf, "%s\nInvalid startup directory. Check your pif file.",
248 filename);
249 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
251 /* search for the program */
252 if( !SearchPathA( NULL, progname, NULL, MAX_PATH, progpath, NULL )) {
253 sprintf( buf, "%s\nInvalid program file name. Check your pif file.",
254 filename);
255 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONERROR);
256 SetLastError( ERROR_FILE_NOT_FOUND);
257 return;
259 if( textmode)
260 if( AllocConsole())
261 SetConsoleTitleA( title) ;
262 /* if no arguments on the commandline, use them from the pif file */
263 if( !cmdline[0] && optparams[0])
264 cmdline = optparams;
265 /* FIXME: do something with:
266 * - close on exit
267 * - graphic modes
268 * - hot key's
269 * - etc.
271 start_dos_exe( progpath, cmdline );
274 /***********************************************************************
275 * build_command_line
277 * Build the command line of a process from the argv array.
278 * Copied from ENV_BuildCommandLine.
280 static char *build_command_line( char **argv )
282 int len;
283 char *p, **arg, *cmd_line;
285 len = 0;
286 for (arg = argv; *arg; arg++)
288 int has_space,bcount;
289 char* a;
291 has_space=0;
292 bcount=0;
293 a=*arg;
294 if( !*a ) has_space=1;
295 while (*a!='\0') {
296 if (*a=='\\') {
297 bcount++;
298 } else {
299 if (*a==' ' || *a=='\t') {
300 has_space=1;
301 } else if (*a=='"') {
302 /* doubling of '\' preceding a '"',
303 * plus escaping of said '"'
305 len+=2*bcount+1;
307 bcount=0;
309 a++;
311 len+=(a-*arg)+1 /* for the separating space */;
312 if (has_space)
313 len+=2; /* for the quotes */
316 if (!(cmd_line = HeapAlloc( GetProcessHeap(), 0, len ? len + 1 : 2 )))
317 return NULL;
319 p = cmd_line;
320 *p++ = (len < 256) ? len : 255;
321 for (arg = argv; *arg; arg++)
323 int has_space,has_quote;
324 char* a;
326 /* Check for quotes and spaces in this argument */
327 has_space=has_quote=0;
328 a=*arg;
329 if( !*a ) has_space=1;
330 while (*a!='\0') {
331 if (*a==' ' || *a=='\t') {
332 has_space=1;
333 if (has_quote)
334 break;
335 } else if (*a=='"') {
336 has_quote=1;
337 if (has_space)
338 break;
340 a++;
343 /* Now transfer it to the command line */
344 if (has_space)
345 *p++='"';
346 if (has_quote) {
347 int bcount;
348 char* a;
350 bcount=0;
351 a=*arg;
352 while (*a!='\0') {
353 if (*a=='\\') {
354 *p++=*a;
355 bcount++;
356 } else {
357 if (*a=='"') {
358 int i;
360 /* Double all the '\\' preceding this '"', plus one */
361 for (i=0;i<=bcount;i++)
362 *p++='\\';
363 *p++='"';
364 } else {
365 *p++=*a;
367 bcount=0;
369 a++;
371 } else {
372 strcpy(p,*arg);
373 p+=strlen(*arg);
375 if (has_space)
376 *p++='"';
377 *p++=' ';
379 if (len) p--; /* remove last space */
380 *p = '\0';
381 return cmd_line;
385 /***********************************************************************
386 * usage
388 static void usage(void)
390 WINE_MESSAGE( "Usage: winevdm.exe [--app-name app.exe] command line\n\n" );
391 ExitProcess(1);
395 /***********************************************************************
396 * main
398 int main( int argc, char *argv[] )
400 DWORD count;
401 HINSTANCE16 instance;
402 LOADPARAMS16 params;
403 WORD showCmd[2];
404 char buffer[MAX_PATH];
405 STARTUPINFOA info;
406 char *cmdline, *appname, **first_arg;
407 char *p;
409 if (!argv[1]) usage();
411 if (!strcmp( argv[1], "--app-name" ))
413 if (!(appname = argv[2])) usage();
414 first_arg = argv + 3;
416 else
418 if (!SearchPathA( NULL, argv[1], ".exe", sizeof(buffer), buffer, NULL ))
420 WINE_MESSAGE( "winevdm: unable to exec '%s': file not found\n", argv[1] );
421 ExitProcess(1);
423 appname = buffer;
424 first_arg = argv + 1;
427 if (*first_arg) first_arg++; /* skip program name */
428 cmdline = build_command_line( first_arg );
430 if (WINE_TRACE_ON(winevdm))
432 int i;
433 WINE_TRACE( "GetCommandLine = '%s'\n", GetCommandLineA() );
434 WINE_TRACE( "appname = '%s'\n", appname );
435 WINE_TRACE( "cmdline = '%.*s'\n", cmdline[0], cmdline+1 );
436 for (i = 0; argv[i]; i++) WINE_TRACE( "argv[%d]: '%s'\n", i, argv[i] );
439 GetStartupInfoA( &info );
440 showCmd[0] = 2;
441 showCmd[1] = (info.dwFlags & STARTF_USESHOWWINDOW) ? info.wShowWindow : SW_SHOWNORMAL;
443 params.hEnvironment = 0;
444 params.cmdLine = MapLS( cmdline );
445 params.showCmd = MapLS( showCmd );
446 params.reserved = 0;
448 RestoreThunkLock(1); /* grab the Win16 lock */
450 /* some programs assume mmsystem is always present */
451 LoadLibrary16( "gdi.exe" );
452 LoadLibrary16( "user.exe" );
453 LoadLibrary16( "mmsystem.dll" );
455 if ((instance = LoadModule16( appname, &params )) < 32)
457 if (instance == 11)
459 /* first see if it is a .pif file */
460 if( ( p = strrchr( appname, '.' )) && !strcasecmp( p, ".pif"))
461 pif_cmd( appname, cmdline + 1);
462 else
464 /* try DOS format */
465 /* loader expects arguments to be regular C strings */
466 start_dos_exe( appname, cmdline + 1 );
468 /* if we get back here it failed */
469 instance = GetLastError();
472 WINE_MESSAGE( "winevdm: can't exec '%s': ", appname );
473 switch (instance)
475 case 2: WINE_MESSAGE("file not found\n" ); break;
476 case 11: WINE_MESSAGE("invalid program file\n" ); break;
477 default: WINE_MESSAGE("error=%d\n", instance ); break;
479 ExitProcess(instance);
482 /* wait forever; the process will be killed when the last task exits */
483 ReleaseThunkLock( &count );
484 Sleep( INFINITE );
485 return 0;