usp10: Implementation of ScriptPlaceOpenType.
[wine/wine-gecko.git] / programs / winevdm / winevdm.c
blob0aaf041597c6375c14a5fc05a94423360c72098c
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 "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wine/winbase16.h"
31 #include "winuser.h"
32 #include "wincon.h"
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(winevdm);
38 extern void __wine_load_dos_exe( LPCSTR filename, LPCSTR cmdline );
41 /*** PIF file structures ***/
42 #include "pshpack1.h"
44 /* header of a PIF file */
45 typedef struct {
46 BYTE unk1[2]; /* 0x00 */
47 CHAR windowtitle[ 30 ]; /* 0x02 seems to be padded with blanks*/
48 WORD memmax; /* 0x20 */
49 WORD memmin; /* 0x22 */
50 CHAR program[63]; /* 0x24 seems to be zero terminated */
51 BYTE hdrflags1; /* 0x63 various flags:
52 * 02 286: text mode selected
53 * 10 close window at exit
55 BYTE startdrive; /* 0x64 */
56 char startdir[64]; /* 0x65 */
57 char optparams[64]; /* 0xa5 seems to be zero terminated */
58 BYTE videomode; /* 0xe5 */
59 BYTE unkn2; /* 0xe6 ?*/
60 BYTE irqlow; /* 0xe7 */
61 BYTE irqhigh; /* 0xe8 */
62 BYTE rows; /* 0xe9 */
63 BYTE cols; /* 0xea */
64 BYTE winY; /* 0xeb */
65 BYTE winX; /* 0xec */
66 WORD unkn3; /* 0xed 7??? */
67 CHAR unkn4[64]; /* 0xef */
68 CHAR unkn5[64]; /* 0x12f */
69 BYTE hdrflags2; /* 0x16f */
70 BYTE hdrflags3; /* 0x170 */
71 } pifhead_t;
73 /* record header: present on every record */
74 typedef struct {
75 CHAR recordname[16]; /* zero terminated */
76 WORD posofnextrecord; /* file offset, 0xffff if last */
77 WORD startofdata; /* file offset */
78 WORD sizeofdata; /* data is expected to follow directly */
79 } recordhead_t;
81 /* 386 -enhanced mode- record */
82 typedef struct {
83 WORD memmax; /* memory desired, overrides the pif header*/
84 WORD memmin; /* memory required, overrides the pif header*/
85 WORD prifg; /* foreground priority */
86 WORD pribg; /* background priority */
87 WORD emsmax; /* EMS memory limit */
88 WORD emsmin; /* EMS memory required */
89 WORD xmsmax; /* XMS memory limit */
90 WORD xmsmin; /* XMS memory required */
91 WORD optflags; /* option flags:
92 * 0008 full screen
93 * 0004 exclusive
94 * 0002 background
95 * 0001 close when active
97 WORD memflags; /* various memory flags*/
98 WORD videoflags; /* video flags:
99 * 0010 text
100 * 0020 med. res. graphics
101 * 0040 hi. res. graphics
103 WORD hotkey[9]; /* Hot key info */
104 CHAR optparams[64]; /* optional params, replaces those in the pif header */
105 } pif386rec_t;
107 #include "poppack.h"
109 /***********************************************************************
110 * find_dosbox
112 static char *find_dosbox(void)
114 const char *envpath = getenv( "PATH" );
115 struct stat st;
116 char *path, *p, *buffer, *dir;
117 size_t envpath_len;
119 if (!envpath) return NULL;
121 envpath_len = strlen( envpath );
122 path = HeapAlloc( GetProcessHeap(), 0, envpath_len + 1 );
123 buffer = HeapAlloc( GetProcessHeap(), 0, envpath_len + sizeof("/dosbox") );
124 strcpy( path, envpath );
126 p = path;
127 while (*p)
129 while (*p == ':') p++;
130 if (!*p) break;
131 dir = p;
132 while (*p && *p != ':') p++;
133 if (*p == ':') *p++ = 0;
134 strcpy( buffer, dir );
135 strcat( buffer, "/dosbox" );
136 if (!stat( buffer, &st ))
138 HeapFree( GetProcessHeap(), 0, path );
139 return buffer;
142 HeapFree( GetProcessHeap(), 0, buffer );
143 HeapFree( GetProcessHeap(), 0, path );
144 return NULL;
148 /***********************************************************************
149 * start_dosbox
151 static void start_dosbox( const char *appname, const char *args )
153 static const WCHAR cfgW[] = {'c','f','g',0};
154 const char *config_dir = wine_get_config_dir();
155 WCHAR path[MAX_PATH], config[MAX_PATH];
156 HANDLE file;
157 char *p, *buffer;
158 int i;
159 int ret = 1;
160 DWORD written, drives = GetLogicalDrives();
161 char *dosbox = find_dosbox();
163 if (!dosbox) return;
164 if (!GetTempPathW( MAX_PATH, path )) return;
165 if (!GetTempFileNameW( path, cfgW, 0, config )) return;
166 if (!GetCurrentDirectoryW( MAX_PATH, path )) return;
167 file = CreateFileW( config, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
168 if (file == INVALID_HANDLE_VALUE) return;
170 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof("[autoexec]") +
171 25 * (strlen(config_dir) + sizeof("mount c /dosdevices/c:")) +
172 4 * strlenW( path ) +
173 6 + strlen( appname ) + strlen( args ) + 20 );
174 p = buffer;
175 p += sprintf( p, "[autoexec]\n" );
176 for (i = 0; i < 25; i++)
177 if (drives & (1 << i))
178 p += sprintf( p, "mount %c %s/dosdevices/%c:\n", 'a' + i, config_dir, 'a' + i );
179 p += sprintf( p, "%c:\ncd ", path[0] );
180 p += WideCharToMultiByte( CP_UNIXCP, 0, path + 2, -1, p, 4 * strlenW(path), NULL, NULL ) - 1;
181 p += sprintf( p, "\n%s %s\n", appname, args );
182 p += sprintf( p, "exit\n" );
183 if (WriteFile( file, buffer, strlen(buffer), &written, NULL ) && written == strlen(buffer))
185 const char *args[4];
186 char *config_file = wine_get_unix_file_name( config );
187 args[0] = dosbox;
188 args[1] = "-conf";
189 args[2] = config_file;
190 args[3] = NULL;
191 ret = spawnvp( _P_WAIT, args[0], args );
193 CloseHandle( file );
194 DeleteFileW( config );
195 HeapFree( GetProcessHeap(), 0, buffer );
196 ExitProcess( ret );
200 /***********************************************************************
201 * start_dos_exe
203 static void start_dos_exe( LPCSTR filename, LPCSTR cmdline )
205 MEMORY_BASIC_INFORMATION mem_info;
206 const char *reason;
208 if (VirtualQuery( NULL, &mem_info, sizeof(mem_info) ) && mem_info.State != MEM_FREE)
210 __wine_load_dos_exe( filename, cmdline );
211 if (GetLastError() == ERROR_NOT_SUPPORTED)
212 reason = "because vm86 mode is not supported on this platform";
213 else
214 reason = wine_dbg_sprintf( "It failed with error code %u", GetLastError() );
216 else reason = "because the DOS memory range is unavailable";
218 start_dosbox( filename, cmdline );
220 WINE_MESSAGE( "winevdm: Cannot start DOS application %s\n", filename );
221 WINE_MESSAGE( " %s.\n", reason );
222 WINE_MESSAGE( " Try running this application with DOSBox.\n" );
223 ExitProcess(1);
226 /***********************************************************************
227 * read_pif_file
228 *pif386rec_tu
229 * Read a pif file and return the header and possibly the 286 (real mode)
230 * record or 386 (enhanced mode) record. Returns FALSE if the file is
231 * invalid otherwise TRUE.
233 static BOOL read_pif_file( HANDLE hFile, char *progname, char *title,
234 char *optparams, char *startdir, int *closeonexit, int *textmode)
236 DWORD nread;
237 LARGE_INTEGER filesize;
238 recordhead_t rhead;
239 BOOL found386rec = FALSE;
240 pif386rec_t pif386rec;
241 pifhead_t pifheader;
242 if( !GetFileSizeEx( hFile, &filesize) ||
243 filesize.QuadPart < (sizeof(pifhead_t) + sizeof(recordhead_t))) {
244 WINE_ERR("Invalid pif file: size error %d\n", (int)filesize.QuadPart);
245 return FALSE;
247 SetFilePointer( hFile, 0, NULL, FILE_BEGIN);
248 if( !ReadFile( hFile, &pifheader, sizeof(pifhead_t), &nread, NULL))
249 return FALSE;
250 WINE_TRACE("header: program %s title %s startdir %s params %s\n",
251 wine_dbgstr_a(pifheader.program),
252 wine_dbgstr_an(pifheader.windowtitle, sizeof(pifheader.windowtitle)),
253 wine_dbgstr_a(pifheader.startdir),
254 wine_dbgstr_a(pifheader.optparams));
255 WINE_TRACE("header: memory req'd %d desr'd %d drive %d videomode %d\n",
256 pifheader.memmin, pifheader.memmax, pifheader.startdrive,
257 pifheader.videomode);
258 WINE_TRACE("header: flags 0x%x 0x%x 0x%x\n",
259 pifheader.hdrflags1, pifheader.hdrflags2, pifheader.hdrflags3);
260 ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL);
261 if( strncmp( rhead.recordname, "MICROSOFT PIFEX", 15)) {
262 WINE_ERR("Invalid pif file: magic string not found\n");
263 return FALSE;
265 /* now process the following records */
266 while( 1) {
267 WORD nextrecord = rhead.posofnextrecord;
268 if( (nextrecord & 0x8000) ||
269 filesize.QuadPart <( nextrecord + sizeof(recordhead_t))) break;
270 if( !SetFilePointer( hFile, nextrecord, NULL, FILE_BEGIN) ||
271 !ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL))
272 return FALSE;
273 if( !rhead.recordname[0]) continue; /* deleted record */
274 WINE_TRACE("reading record %s size %d next 0x%x\n",
275 wine_dbgstr_a(rhead.recordname), rhead.sizeofdata,
276 rhead.posofnextrecord );
277 if( !strncmp( rhead.recordname, "WINDOWS 386", 11)) {
278 found386rec = TRUE;
279 ReadFile( hFile, &pif386rec, sizeof(pif386rec_t), &nread, NULL);
280 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",
281 pif386rec.memmin, pif386rec.memmax,
282 pif386rec.emsmin, pif386rec.emsmax,
283 pif386rec.xmsmin, pif386rec.xmsmax);
284 WINE_TRACE("386rec: option 0x%x memory 0x%x video 0x%x\n",
285 pif386rec.optflags, pif386rec.memflags,
286 pif386rec.videoflags);
287 WINE_TRACE("386rec: optional parameters %s\n",
288 wine_dbgstr_a(pif386rec.optparams));
291 /* prepare the return data */
292 strncpy( progname, pifheader.program, sizeof(pifheader.program));
293 memcpy( title, pifheader.windowtitle, sizeof(pifheader.windowtitle));
294 title[ sizeof(pifheader.windowtitle) ] = '\0';
295 if( found386rec)
296 strncpy( optparams, pif386rec.optparams, sizeof( pif386rec.optparams));
297 else
298 strncpy( optparams, pifheader.optparams, sizeof(pifheader.optparams));
299 strncpy( startdir, pifheader.startdir, sizeof(pifheader.startdir));
300 *closeonexit = pifheader.hdrflags1 & 0x10;
301 *textmode = found386rec ? pif386rec.videoflags & 0x0010
302 : pifheader.hdrflags1 & 0x0002;
303 return TRUE;
306 /***********************************************************************
307 * pif_cmd
309 * execute a pif file.
311 static VOID pif_cmd( char *filename, char *cmdline)
313 HANDLE hFile;
314 char progpath[MAX_PATH];
315 char buf[128];
316 char progname[64];
317 char title[31];
318 char optparams[64];
319 char startdir[64];
320 char *p;
321 int closeonexit;
322 int textmode;
323 if( (hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
324 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
326 WINE_ERR("open file %s failed\n", wine_dbgstr_a(filename));
327 return;
329 if( !read_pif_file( hFile, progname, title, optparams, startdir,
330 &closeonexit, &textmode)) {
331 WINE_ERR( "failed to read %s\n", wine_dbgstr_a(filename));
332 CloseHandle( hFile);
333 sprintf( buf, "%s\nInvalid file format. Check your pif file.",
334 filename);
335 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
336 SetLastError( ERROR_BAD_FORMAT);
337 return;
339 CloseHandle( hFile);
340 if( (p = strrchr( progname, '.')) && !strcasecmp( p, ".bat"))
341 WINE_FIXME(".bat programs in pif files are not supported.\n");
342 /* first change dir, so the search below can start from there */
343 if( startdir[0] && !SetCurrentDirectoryA( startdir)) {
344 WINE_ERR("Cannot change directory %s\n", wine_dbgstr_a( startdir));
345 sprintf( buf, "%s\nInvalid startup directory. Check your pif file.",
346 filename);
347 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
349 /* search for the program */
350 if( !SearchPathA( NULL, progname, NULL, MAX_PATH, progpath, NULL )) {
351 sprintf( buf, "%s\nInvalid program file name. Check your pif file.",
352 filename);
353 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONERROR);
354 SetLastError( ERROR_FILE_NOT_FOUND);
355 return;
357 if( textmode)
358 if( AllocConsole())
359 SetConsoleTitleA( title) ;
360 /* if no arguments on the commandline, use them from the pif file */
361 if( !cmdline[0] && optparams[0])
362 cmdline = optparams;
363 /* FIXME: do something with:
364 * - close on exit
365 * - graphic modes
366 * - hot key's
367 * - etc.
369 start_dos_exe( progpath, cmdline );
372 /***********************************************************************
373 * build_command_line
375 * Build the command line of a process from the argv array.
376 * Copied from ENV_BuildCommandLine.
378 static char *build_command_line( char **argv )
380 int len;
381 char *p, **arg, *cmd_line;
383 len = 0;
384 for (arg = argv; *arg; arg++)
386 int has_space,bcount;
387 char* a;
389 has_space=0;
390 bcount=0;
391 a=*arg;
392 if( !*a ) has_space=1;
393 while (*a!='\0') {
394 if (*a=='\\') {
395 bcount++;
396 } else {
397 if (*a==' ' || *a=='\t') {
398 has_space=1;
399 } else if (*a=='"') {
400 /* doubling of '\' preceding a '"',
401 * plus escaping of said '"'
403 len+=2*bcount+1;
405 bcount=0;
407 a++;
409 len+=(a-*arg)+1 /* for the separating space */;
410 if (has_space)
411 len+=2; /* for the quotes */
414 if (!(cmd_line = HeapAlloc( GetProcessHeap(), 0, len ? len + 1 : 2 )))
415 return NULL;
417 p = cmd_line;
418 *p++ = (len < 256) ? len : 255;
419 for (arg = argv; *arg; arg++)
421 int has_space,has_quote;
422 char* a;
424 /* Check for quotes and spaces in this argument */
425 has_space=has_quote=0;
426 a=*arg;
427 if( !*a ) has_space=1;
428 while (*a!='\0') {
429 if (*a==' ' || *a=='\t') {
430 has_space=1;
431 if (has_quote)
432 break;
433 } else if (*a=='"') {
434 has_quote=1;
435 if (has_space)
436 break;
438 a++;
441 /* Now transfer it to the command line */
442 if (has_space)
443 *p++='"';
444 if (has_quote) {
445 int bcount;
446 char* a;
448 bcount=0;
449 a=*arg;
450 while (*a!='\0') {
451 if (*a=='\\') {
452 *p++=*a;
453 bcount++;
454 } else {
455 if (*a=='"') {
456 int i;
458 /* Double all the '\\' preceding this '"', plus one */
459 for (i=0;i<=bcount;i++)
460 *p++='\\';
461 *p++='"';
462 } else {
463 *p++=*a;
465 bcount=0;
467 a++;
469 } else {
470 strcpy(p,*arg);
471 p+=strlen(*arg);
473 if (has_space)
474 *p++='"';
475 *p++=' ';
477 if (len) p--; /* remove last space */
478 *p = '\0';
479 return cmd_line;
483 /***********************************************************************
484 * usage
486 static void usage(void)
488 WINE_MESSAGE( "Usage: winevdm.exe [--app-name app.exe] command line\n\n" );
489 ExitProcess(1);
493 /***********************************************************************
494 * main
496 int main( int argc, char *argv[] )
498 DWORD count;
499 HINSTANCE16 instance;
500 LOADPARAMS16 params;
501 WORD showCmd[2];
502 char buffer[MAX_PATH];
503 STARTUPINFOA info;
504 char *cmdline, *appname, **first_arg;
505 char *p;
507 if (!argv[1]) usage();
509 if (!strcmp( argv[1], "--app-name" ))
511 if (!(appname = argv[2])) usage();
512 first_arg = argv + 3;
514 else
516 if (!SearchPathA( NULL, argv[1], ".exe", sizeof(buffer), buffer, NULL ))
518 WINE_MESSAGE( "winevdm: unable to exec '%s': file not found\n", argv[1] );
519 ExitProcess(1);
521 appname = buffer;
522 first_arg = argv + 1;
525 if (*first_arg) first_arg++; /* skip program name */
526 cmdline = build_command_line( first_arg );
528 if (WINE_TRACE_ON(winevdm))
530 int i;
531 WINE_TRACE( "GetCommandLine = '%s'\n", GetCommandLineA() );
532 WINE_TRACE( "appname = '%s'\n", appname );
533 WINE_TRACE( "cmdline = '%.*s'\n", cmdline[0], cmdline+1 );
534 for (i = 0; argv[i]; i++) WINE_TRACE( "argv[%d]: '%s'\n", i, argv[i] );
537 GetStartupInfoA( &info );
538 showCmd[0] = 2;
539 showCmd[1] = (info.dwFlags & STARTF_USESHOWWINDOW) ? info.wShowWindow : SW_SHOWNORMAL;
541 params.hEnvironment = 0;
542 params.cmdLine = MapLS( cmdline );
543 params.showCmd = MapLS( showCmd );
544 params.reserved = 0;
546 RestoreThunkLock(1); /* grab the Win16 lock */
548 /* some programs assume mmsystem is always present */
549 LoadLibrary16( "gdi.exe" );
550 LoadLibrary16( "user.exe" );
551 LoadLibrary16( "mmsystem.dll" );
553 if ((instance = LoadModule16( appname, &params )) < 32)
555 if (instance == 11)
557 /* first see if it is a .pif file */
558 if( ( p = strrchr( appname, '.' )) && !strcasecmp( p, ".pif"))
559 pif_cmd( appname, cmdline + 1);
560 else
562 /* try DOS format */
563 /* loader expects arguments to be regular C strings */
564 start_dos_exe( appname, cmdline + 1 );
566 /* if we get back here it failed */
567 instance = GetLastError();
570 WINE_MESSAGE( "winevdm: can't exec '%s': ", appname );
571 switch (instance)
573 case 2: WINE_MESSAGE("file not found\n" ); break;
574 case 11: WINE_MESSAGE("invalid program file\n" ); break;
575 default: WINE_MESSAGE("error=%d\n", instance ); break;
577 ExitProcess(instance);
580 /* wait forever; the process will be killed when the last task exits */
581 ReleaseThunkLock( &count );
582 Sleep( INFINITE );
583 return 0;