msgsm32.acm: Use standard dlopen() instead of the libwine wrappers.
[wine/zf.git] / programs / winevdm / winevdm.c
blob64750db135524211ab5ac19dd30a1d80a63b8fd5
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/library.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(winevdm);
40 /*** PIF file structures ***/
41 #include "pshpack1.h"
43 /* header of a PIF file */
44 typedef struct {
45 BYTE unk1[2]; /* 0x00 */
46 CHAR windowtitle[ 30 ]; /* 0x02 seems to be padded with blanks*/
47 WORD memmax; /* 0x20 */
48 WORD memmin; /* 0x22 */
49 CHAR program[63]; /* 0x24 seems to be zero terminated */
50 BYTE hdrflags1; /* 0x63 various flags:
51 * 02 286: text mode selected
52 * 10 close window at exit
54 BYTE startdrive; /* 0x64 */
55 char startdir[64]; /* 0x65 */
56 char optparams[64]; /* 0xa5 seems to be zero terminated */
57 BYTE videomode; /* 0xe5 */
58 BYTE unkn2; /* 0xe6 ?*/
59 BYTE irqlow; /* 0xe7 */
60 BYTE irqhigh; /* 0xe8 */
61 BYTE rows; /* 0xe9 */
62 BYTE cols; /* 0xea */
63 BYTE winY; /* 0xeb */
64 BYTE winX; /* 0xec */
65 WORD unkn3; /* 0xed 7??? */
66 CHAR unkn4[64]; /* 0xef */
67 CHAR unkn5[64]; /* 0x12f */
68 BYTE hdrflags2; /* 0x16f */
69 BYTE hdrflags3; /* 0x170 */
70 } pifhead_t;
72 /* record header: present on every record */
73 typedef struct {
74 CHAR recordname[16]; /* zero terminated */
75 WORD posofnextrecord; /* file offset, 0xffff if last */
76 WORD startofdata; /* file offset */
77 WORD sizeofdata; /* data is expected to follow directly */
78 } recordhead_t;
80 /* 386 -enhanced mode- record */
81 typedef struct {
82 WORD memmax; /* memory desired, overrides the pif header*/
83 WORD memmin; /* memory required, overrides the pif header*/
84 WORD prifg; /* foreground priority */
85 WORD pribg; /* background priority */
86 WORD emsmax; /* EMS memory limit */
87 WORD emsmin; /* EMS memory required */
88 WORD xmsmax; /* XMS memory limit */
89 WORD xmsmin; /* XMS memory required */
90 WORD optflags; /* option flags:
91 * 0008 full screen
92 * 0004 exclusive
93 * 0002 background
94 * 0001 close when active
96 WORD memflags; /* various memory flags*/
97 WORD videoflags; /* video flags:
98 * 0010 text
99 * 0020 med. res. graphics
100 * 0040 hi. res. graphics
102 WORD hotkey[9]; /* Hot key info */
103 CHAR optparams[64]; /* optional params, replaces those in the pif header */
104 } pif386rec_t;
106 #include "poppack.h"
108 /***********************************************************************
109 * find_dosbox
111 static char *find_dosbox(void)
113 const char *envpath = getenv( "PATH" );
114 struct stat st;
115 char *path, *p, *buffer, *dir;
116 size_t envpath_len;
118 if (!envpath) return NULL;
120 envpath_len = strlen( envpath );
121 path = HeapAlloc( GetProcessHeap(), 0, envpath_len + 1 );
122 buffer = HeapAlloc( GetProcessHeap(), 0, envpath_len + sizeof("/dosbox") );
123 strcpy( path, envpath );
125 p = path;
126 while (*p)
128 while (*p == ':') p++;
129 if (!*p) break;
130 dir = p;
131 while (*p && *p != ':') p++;
132 if (*p == ':') *p++ = 0;
133 strcpy( buffer, dir );
134 strcat( buffer, "/dosbox" );
135 if (!stat( buffer, &st ))
137 HeapFree( GetProcessHeap(), 0, path );
138 return buffer;
141 HeapFree( GetProcessHeap(), 0, buffer );
142 HeapFree( GetProcessHeap(), 0, path );
143 return NULL;
147 /***********************************************************************
148 * start_dosbox
150 static void start_dosbox( const char *appname, const char *args )
152 static const WCHAR cfgW[] = {'c','f','g',0};
153 const char *config_dir = wine_get_config_dir();
154 WCHAR path[MAX_PATH], config[MAX_PATH];
155 HANDLE file;
156 char *p, *buffer, app[MAX_PATH];
157 int i;
158 int ret = 1;
159 DWORD written, drives = GetLogicalDrives();
160 char *dosbox = find_dosbox();
162 if (!dosbox) return;
163 if (!GetTempPathW( MAX_PATH, path )) return;
164 if (!GetTempFileNameW( path, cfgW, 0, config )) return;
165 if (!GetCurrentDirectoryW( MAX_PATH, path )) return;
166 if (!GetShortPathNameA( appname, app, MAX_PATH )) return;
167 GetShortPathNameW( path, path, MAX_PATH );
168 file = CreateFileW( config, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
169 if (file == INVALID_HANDLE_VALUE) return;
171 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof("[autoexec]") +
172 sizeof("mount -z c") + sizeof("config -securemode") +
173 25 * (strlen(config_dir) + sizeof("mount c /dosdevices/c:")) +
174 4 * strlenW( path ) +
175 6 + strlen( app ) + strlen( args ) + 20 );
176 p = buffer;
177 p += sprintf( p, "[autoexec]\n" );
178 for (i = 25; i >= 0; i--)
179 if (!(drives & (1 << i)))
181 p += sprintf( p, "mount -z %c\n", 'a' + i );
182 break;
184 for (i = 0; i <= 25; i++)
185 if (drives & (1 << i))
186 p += sprintf( p, "mount %c %s/dosdevices/%c:\n", 'a' + i, config_dir, 'a' + i );
187 p += sprintf( p, "%c:\ncd ", path[0] );
188 p += WideCharToMultiByte( CP_UNIXCP, 0, path + 2, -1, p, 4 * strlenW(path), NULL, NULL ) - 1;
189 p += sprintf( p, "\nconfig -securemode\n" );
190 p += sprintf( p, "%s %s\n", app, args );
191 p += sprintf( p, "exit\n" );
192 if (WriteFile( file, buffer, strlen(buffer), &written, NULL ) && written == strlen(buffer))
194 const char *args[5];
195 char *config_file = wine_get_unix_file_name( config );
196 args[0] = dosbox;
197 args[1] = "-userconf";
198 args[2] = "-conf";
199 args[3] = config_file;
200 args[4] = NULL;
201 ret = _spawnvp( _P_WAIT, args[0], args );
203 CloseHandle( file );
204 DeleteFileW( config );
205 HeapFree( GetProcessHeap(), 0, buffer );
206 ExitProcess( ret );
210 /***********************************************************************
211 * start_dos_exe
213 static void start_dos_exe( LPCSTR filename, LPCSTR cmdline )
215 start_dosbox( filename, cmdline );
216 WINE_MESSAGE( "winevdm: %s is a DOS application, you need to install DOSBox.\n", filename );
217 ExitProcess(1);
220 /***********************************************************************
221 * read_pif_file
222 *pif386rec_tu
223 * Read a pif file and return the header and possibly the 286 (real mode)
224 * record or 386 (enhanced mode) record. Returns FALSE if the file is
225 * invalid otherwise TRUE.
227 static BOOL read_pif_file( HANDLE hFile, char *progname, char *title,
228 char *optparams, char *startdir, int *closeonexit, int *textmode)
230 DWORD nread;
231 LARGE_INTEGER filesize;
232 recordhead_t rhead;
233 BOOL found386rec = FALSE;
234 pif386rec_t pif386rec;
235 pifhead_t pifheader;
236 if( !GetFileSizeEx( hFile, &filesize) ||
237 filesize.QuadPart < (sizeof(pifhead_t) + sizeof(recordhead_t))) {
238 WINE_ERR("Invalid pif file: size error %d\n", (int)filesize.QuadPart);
239 return FALSE;
241 SetFilePointer( hFile, 0, NULL, FILE_BEGIN);
242 if( !ReadFile( hFile, &pifheader, sizeof(pifhead_t), &nread, NULL))
243 return FALSE;
244 WINE_TRACE("header: program %s title %s startdir %s params %s\n",
245 wine_dbgstr_a(pifheader.program),
246 wine_dbgstr_an(pifheader.windowtitle, sizeof(pifheader.windowtitle)),
247 wine_dbgstr_a(pifheader.startdir),
248 wine_dbgstr_a(pifheader.optparams));
249 WINE_TRACE("header: memory req'd %d desr'd %d drive %d videomode %d\n",
250 pifheader.memmin, pifheader.memmax, pifheader.startdrive,
251 pifheader.videomode);
252 WINE_TRACE("header: flags 0x%x 0x%x 0x%x\n",
253 pifheader.hdrflags1, pifheader.hdrflags2, pifheader.hdrflags3);
254 ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL);
255 if( strncmp( rhead.recordname, "MICROSOFT PIFEX", 15)) {
256 WINE_ERR("Invalid pif file: magic string not found\n");
257 return FALSE;
259 /* now process the following records */
260 while( 1) {
261 WORD nextrecord = rhead.posofnextrecord;
262 if( (nextrecord & 0x8000) ||
263 filesize.QuadPart <( nextrecord + sizeof(recordhead_t))) break;
264 if( !SetFilePointer( hFile, nextrecord, NULL, FILE_BEGIN) ||
265 !ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL))
266 return FALSE;
267 if( !rhead.recordname[0]) continue; /* deleted record */
268 WINE_TRACE("reading record %s size %d next 0x%x\n",
269 wine_dbgstr_a(rhead.recordname), rhead.sizeofdata,
270 rhead.posofnextrecord );
271 if( !strncmp( rhead.recordname, "WINDOWS 386", 11)) {
272 found386rec = TRUE;
273 ReadFile( hFile, &pif386rec, sizeof(pif386rec_t), &nread, NULL);
274 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",
275 pif386rec.memmin, pif386rec.memmax,
276 pif386rec.emsmin, pif386rec.emsmax,
277 pif386rec.xmsmin, pif386rec.xmsmax);
278 WINE_TRACE("386rec: option 0x%x memory 0x%x video 0x%x\n",
279 pif386rec.optflags, pif386rec.memflags,
280 pif386rec.videoflags);
281 WINE_TRACE("386rec: optional parameters %s\n",
282 wine_dbgstr_a(pif386rec.optparams));
285 /* prepare the return data */
286 lstrcpynA( progname, pifheader.program, sizeof(pifheader.program)+1);
287 lstrcpynA( title, pifheader.windowtitle, sizeof(pifheader.windowtitle)+1);
288 if( found386rec)
289 lstrcpynA( optparams, pif386rec.optparams, sizeof( pif386rec.optparams)+1);
290 else
291 lstrcpynA( optparams, pifheader.optparams, sizeof(pifheader.optparams)+1);
292 lstrcpynA( startdir, pifheader.startdir, sizeof(pifheader.startdir)+1);
293 *closeonexit = pifheader.hdrflags1 & 0x10;
294 *textmode = found386rec ? pif386rec.videoflags & 0x0010
295 : pifheader.hdrflags1 & 0x0002;
296 return TRUE;
299 /***********************************************************************
300 * pif_cmd
302 * execute a pif file.
304 static VOID pif_cmd( char *filename, char *cmdline)
306 HANDLE hFile;
307 char progpath[MAX_PATH];
308 char buf[308];
309 char progname[64];
310 char title[31];
311 char optparams[65];
312 char startdir[65];
313 char *p;
314 int closeonexit;
315 int textmode;
316 if( (hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
317 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
319 WINE_ERR("open file %s failed\n", wine_dbgstr_a(filename));
320 return;
322 if( !read_pif_file( hFile, progname, title, optparams, startdir,
323 &closeonexit, &textmode)) {
324 WINE_ERR( "failed to read %s\n", wine_dbgstr_a(filename));
325 CloseHandle( hFile);
326 sprintf( buf, "%s\nInvalid file format. Check your pif file.",
327 filename);
328 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
329 SetLastError( ERROR_BAD_FORMAT);
330 return;
332 CloseHandle( hFile);
333 if( (p = strrchr( progname, '.')) && !strcasecmp( p, ".bat"))
334 WINE_FIXME(".bat programs in pif files are not supported.\n");
335 /* first change dir, so the search below can start from there */
336 if( startdir[0] && !SetCurrentDirectoryA( startdir)) {
337 WINE_ERR("Cannot change directory %s\n", wine_dbgstr_a( startdir));
338 sprintf( buf, "%s\nInvalid startup directory. Check your pif file.",
339 filename);
340 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
342 /* search for the program */
343 if( !SearchPathA( NULL, progname, NULL, MAX_PATH, progpath, NULL )) {
344 sprintf( buf, "%s\nInvalid program file name. Check your pif file.",
345 filename);
346 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONERROR);
347 SetLastError( ERROR_FILE_NOT_FOUND);
348 return;
350 if( textmode)
351 if( AllocConsole())
352 SetConsoleTitleA( title) ;
353 /* if no arguments on the commandline, use them from the pif file */
354 if( !cmdline[0] && optparams[0])
355 cmdline = optparams;
356 /* FIXME: do something with:
357 * - close on exit
358 * - graphic modes
359 * - hot key's
360 * - etc.
362 start_dos_exe( progpath, cmdline );
365 /***********************************************************************
366 * build_command_line
368 * Build the command line of a process from the argv array.
369 * Copied from ENV_BuildCommandLine.
371 static char *build_command_line( char **argv )
373 int len;
374 char *p, **arg, *cmd_line;
376 len = 0;
377 for (arg = argv; *arg; arg++)
379 BOOL has_space;
380 int bcount;
381 char* a;
383 has_space=FALSE;
384 bcount=0;
385 a=*arg;
386 if( !*a ) has_space=TRUE;
387 while (*a!='\0') {
388 if (*a=='\\') {
389 bcount++;
390 } else {
391 if (*a==' ' || *a=='\t') {
392 has_space=TRUE;
393 } else if (*a=='"') {
394 /* doubling of '\' preceding a '"',
395 * plus escaping of said '"'
397 len+=2*bcount+1;
399 bcount=0;
401 a++;
403 len+=(a-*arg)+1 /* for the separating space */;
404 if (has_space)
405 len+=2; /* for the quotes */
408 if (!(cmd_line = HeapAlloc( GetProcessHeap(), 0, len ? len + 1 : 2 )))
409 return NULL;
411 p = cmd_line;
412 *p++ = (len < 256) ? len : 255;
413 for (arg = argv; *arg; arg++)
415 BOOL has_space,has_quote;
416 char* a;
418 /* Check for quotes and spaces in this argument */
419 has_space=has_quote=FALSE;
420 a=*arg;
421 if( !*a ) has_space=TRUE;
422 while (*a!='\0') {
423 if (*a==' ' || *a=='\t') {
424 has_space=TRUE;
425 if (has_quote)
426 break;
427 } else if (*a=='"') {
428 has_quote=TRUE;
429 if (has_space)
430 break;
432 a++;
435 /* Now transfer it to the command line */
436 if (has_space)
437 *p++='"';
438 if (has_quote) {
439 int bcount;
441 bcount=0;
442 a=*arg;
443 while (*a!='\0') {
444 if (*a=='\\') {
445 *p++=*a;
446 bcount++;
447 } else {
448 if (*a=='"') {
449 int i;
451 /* Double all the '\\' preceding this '"', plus one */
452 for (i=0;i<=bcount;i++)
453 *p++='\\';
454 *p++='"';
455 } else {
456 *p++=*a;
458 bcount=0;
460 a++;
462 } else {
463 strcpy(p,*arg);
464 p+=strlen(*arg);
466 if (has_space)
467 *p++='"';
468 *p++=' ';
470 if (len) p--; /* remove last space */
471 *p = '\0';
472 return cmd_line;
476 /***********************************************************************
477 * usage
479 static void usage(void)
481 WINE_MESSAGE( "Usage: winevdm.exe [--app-name app.exe] command line\n\n" );
482 ExitProcess(1);
486 /***********************************************************************
487 * main
489 int __cdecl main( int argc, char *argv[] )
491 DWORD count;
492 HINSTANCE16 instance;
493 LOADPARAMS16 params;
494 WORD showCmd[2];
495 char buffer[MAX_PATH];
496 STARTUPINFOA info;
497 char *cmdline, *appname, **first_arg;
498 char *p;
500 if (!argv[1]) usage();
502 if (!strcmp( argv[1], "--app-name" ))
504 if (!(appname = argv[2])) usage();
505 first_arg = argv + 3;
507 else
509 if (!SearchPathA( NULL, argv[1], ".exe", sizeof(buffer), buffer, NULL ))
511 WINE_MESSAGE( "winevdm: unable to exec '%s': file not found\n", argv[1] );
512 ExitProcess(1);
514 appname = buffer;
515 first_arg = argv + 1;
518 if (*first_arg) first_arg++; /* skip program name */
519 cmdline = build_command_line( first_arg );
521 if (WINE_TRACE_ON(winevdm))
523 int i;
524 WINE_TRACE( "GetCommandLine = '%s'\n", GetCommandLineA() );
525 WINE_TRACE( "appname = '%s'\n", appname );
526 WINE_TRACE( "cmdline = '%.*s'\n", cmdline[0], cmdline+1 );
527 for (i = 0; argv[i]; i++) WINE_TRACE( "argv[%d]: '%s'\n", i, argv[i] );
530 GetStartupInfoA( &info );
531 showCmd[0] = 2;
532 showCmd[1] = (info.dwFlags & STARTF_USESHOWWINDOW) ? info.wShowWindow : SW_SHOWNORMAL;
534 params.hEnvironment = 0;
535 params.cmdLine = MapLS( cmdline );
536 params.showCmd = MapLS( showCmd );
537 params.reserved = 0;
539 RestoreThunkLock(1); /* grab the Win16 lock */
541 /* some programs assume mmsystem is always present */
542 LoadLibrary16( "gdi.exe" );
543 LoadLibrary16( "user.exe" );
544 LoadLibrary16( "mmsystem.dll" );
546 if ((instance = LoadModule16( appname, &params )) < 32)
548 if (instance == 11)
550 /* first see if it is a .pif file */
551 if( ( p = strrchr( appname, '.' )) && !strcasecmp( p, ".pif"))
552 pif_cmd( appname, cmdline + 1);
553 else
555 /* try DOS format */
556 /* loader expects arguments to be regular C strings */
557 start_dos_exe( appname, cmdline + 1 );
559 /* if we get back here it failed */
560 instance = GetLastError();
563 WINE_MESSAGE( "winevdm: can't exec '%s': ", appname );
564 switch (instance)
566 case 2: WINE_MESSAGE("file not found\n" ); break;
567 case 11: WINE_MESSAGE("invalid program file\n" ); break;
568 default: WINE_MESSAGE("error=%d\n", instance ); break;
570 ExitProcess(instance);
573 /* wait forever; the process will be killed when the last task exits */
574 ReleaseThunkLock( &count );
575 Sleep( INFINITE );
576 return 0;