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
26 #define WIN32_NO_STATUS
29 #include "wine/winbase16.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winevdm
);
36 #define DOSBOX "dosbox"
38 /*** PIF file structures ***/
41 /* header of a PIF file */
43 BYTE unk1
[2]; /* 0x00 */
44 CHAR windowtitle
[ 30 ]; /* 0x02 seems to be padded with blanks*/
45 WORD memmax
; /* 0x20 */
46 WORD memmin
; /* 0x22 */
47 CHAR program
[63]; /* 0x24 seems to be zero terminated */
48 BYTE hdrflags1
; /* 0x63 various flags:
49 * 02 286: text mode selected
50 * 10 close window at exit
52 BYTE startdrive
; /* 0x64 */
53 char startdir
[64]; /* 0x65 */
54 char optparams
[64]; /* 0xa5 seems to be zero terminated */
55 BYTE videomode
; /* 0xe5 */
56 BYTE unkn2
; /* 0xe6 ?*/
57 BYTE irqlow
; /* 0xe7 */
58 BYTE irqhigh
; /* 0xe8 */
63 WORD unkn3
; /* 0xed 7??? */
64 CHAR unkn4
[64]; /* 0xef */
65 CHAR unkn5
[64]; /* 0x12f */
66 BYTE hdrflags2
; /* 0x16f */
67 BYTE hdrflags3
; /* 0x170 */
70 /* record header: present on every record */
72 CHAR recordname
[16]; /* zero terminated */
73 WORD posofnextrecord
; /* file offset, 0xffff if last */
74 WORD startofdata
; /* file offset */
75 WORD sizeofdata
; /* data is expected to follow directly */
78 /* 386 -enhanced mode- record */
80 WORD memmax
; /* memory desired, overrides the pif header*/
81 WORD memmin
; /* memory required, overrides the pif header*/
82 WORD prifg
; /* foreground priority */
83 WORD pribg
; /* background priority */
84 WORD emsmax
; /* EMS memory limit */
85 WORD emsmin
; /* EMS memory required */
86 WORD xmsmax
; /* XMS memory limit */
87 WORD xmsmin
; /* XMS memory required */
88 WORD optflags
; /* option flags:
92 * 0001 close when active
94 WORD memflags
; /* various memory flags*/
95 WORD videoflags
; /* video flags:
97 * 0020 med. res. graphics
98 * 0040 hi. res. graphics
100 WORD hotkey
[9]; /* Hot key info */
101 CHAR optparams
[64]; /* optional params, replaces those in the pif header */
106 /***********************************************************************
109 static void start_dosbox( const char *appname
, const char *args
)
111 const WCHAR
*config_dir
= _wgetenv( L
"WINECONFIGDIR" );
112 WCHAR path
[MAX_PATH
], config
[MAX_PATH
];
114 char *p
, *prefix
, *buffer
, app
[MAX_PATH
];
116 NTSTATUS ret
= STATUS_OBJECT_NAME_NOT_FOUND
;
117 DWORD written
, drives
= GetLogicalDrives();
119 if (!config_dir
|| !(prefix
= wine_get_unix_file_name( config_dir
))) return;
120 if (!GetTempPathW( MAX_PATH
, path
)) return;
121 if (!GetTempFileNameW( path
, L
"cfg", 0, config
)) return;
122 if (!GetCurrentDirectoryW( MAX_PATH
, path
)) return;
123 if (!GetShortPathNameA( appname
, app
, MAX_PATH
)) return;
124 GetShortPathNameW( path
, path
, MAX_PATH
);
125 file
= CreateFileW( config
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0 );
126 if (file
== INVALID_HANDLE_VALUE
) return;
128 buffer
= HeapAlloc( GetProcessHeap(), 0, sizeof("[autoexec]") +
129 sizeof("mount -z c") + sizeof("config -securemode") +
130 26 * (strlen(prefix
) + sizeof("mount c /dosdevices/c:")) +
131 4 * lstrlenW( path
) +
132 6 + strlen( app
) + strlen( args
) + 20 );
134 p
+= sprintf( p
, "[autoexec]\n" );
135 for (i
= 25; i
>= 0; i
--)
136 if (!(drives
& (1 << i
)))
138 p
+= sprintf( p
, "mount -z %c\n", 'a' + i
);
141 for (i
= 0; i
<= 25; i
++)
143 if (!(drives
& (1 << i
))) continue;
144 p
+= sprintf( p
, "mount %c %s/dosdevices/%c:\n", 'a' + i
, prefix
, 'a' + i
);
146 p
+= sprintf( p
, "%c:\ncd ", path
[0] );
147 p
+= WideCharToMultiByte( CP_UNIXCP
, 0, path
+ 2, -1, p
, 4 * lstrlenW(path
), NULL
, NULL
) - 1;
148 p
+= sprintf( p
, "\nconfig -securemode\n" );
149 p
+= sprintf( p
, "%s %s\n", app
, args
);
150 p
+= sprintf( p
, "exit\n" );
151 if (WriteFile( file
, buffer
, strlen(buffer
), &written
, NULL
) && written
== strlen(buffer
))
154 char *config_file
= wine_get_unix_file_name( config
);
156 args
[1] = "-userconf";
158 args
[3] = config_file
;
160 ret
= __wine_unix_spawnvp( (char **)args
, TRUE
);
163 DeleteFileW( config
);
164 HeapFree( GetProcessHeap(), 0, buffer
);
165 if (FAILED(ret
)) MESSAGE( "winevdm: %s is a DOS application, you need to install DOSBox.\n", appname
);
170 /***********************************************************************
173 * Read a pif file and return the header and possibly the 286 (real mode)
174 * record or 386 (enhanced mode) record. Returns FALSE if the file is
175 * invalid otherwise TRUE.
177 static BOOL
read_pif_file( HANDLE hFile
, char *progname
, char *title
,
178 char *optparams
, char *startdir
, int *closeonexit
, int *textmode
)
181 LARGE_INTEGER filesize
;
183 BOOL found386rec
= FALSE
;
184 pif386rec_t pif386rec
;
186 if( !GetFileSizeEx( hFile
, &filesize
) ||
187 filesize
.QuadPart
< (sizeof(pifhead_t
) + sizeof(recordhead_t
))) {
188 WINE_ERR("Invalid pif file: size error %d\n", (int)filesize
.QuadPart
);
191 SetFilePointer( hFile
, 0, NULL
, FILE_BEGIN
);
192 if( !ReadFile( hFile
, &pifheader
, sizeof(pifhead_t
), &nread
, NULL
))
194 WINE_TRACE("header: program %s title %s startdir %s params %s\n",
195 wine_dbgstr_a(pifheader
.program
),
196 wine_dbgstr_an(pifheader
.windowtitle
, sizeof(pifheader
.windowtitle
)),
197 wine_dbgstr_a(pifheader
.startdir
),
198 wine_dbgstr_a(pifheader
.optparams
));
199 WINE_TRACE("header: memory req'd %d desr'd %d drive %d videomode %d\n",
200 pifheader
.memmin
, pifheader
.memmax
, pifheader
.startdrive
,
201 pifheader
.videomode
);
202 WINE_TRACE("header: flags 0x%x 0x%x 0x%x\n",
203 pifheader
.hdrflags1
, pifheader
.hdrflags2
, pifheader
.hdrflags3
);
204 ReadFile( hFile
, &rhead
, sizeof(recordhead_t
), &nread
, NULL
);
205 if( strncmp( rhead
.recordname
, "MICROSOFT PIFEX", 15)) {
206 WINE_ERR("Invalid pif file: magic string not found\n");
209 /* now process the following records */
211 WORD nextrecord
= rhead
.posofnextrecord
;
212 if( (nextrecord
& 0x8000) ||
213 filesize
.QuadPart
<( nextrecord
+ sizeof(recordhead_t
))) break;
214 if( !SetFilePointer( hFile
, nextrecord
, NULL
, FILE_BEGIN
) ||
215 !ReadFile( hFile
, &rhead
, sizeof(recordhead_t
), &nread
, NULL
))
217 if( !rhead
.recordname
[0]) continue; /* deleted record */
218 WINE_TRACE("reading record %s size %d next 0x%x\n",
219 wine_dbgstr_a(rhead
.recordname
), rhead
.sizeofdata
,
220 rhead
.posofnextrecord
);
221 if( !strncmp( rhead
.recordname
, "WINDOWS 386", 11)) {
223 ReadFile( hFile
, &pif386rec
, sizeof(pif386rec_t
), &nread
, NULL
);
224 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",
225 pif386rec
.memmin
, pif386rec
.memmax
,
226 pif386rec
.emsmin
, pif386rec
.emsmax
,
227 pif386rec
.xmsmin
, pif386rec
.xmsmax
);
228 WINE_TRACE("386rec: option 0x%x memory 0x%x video 0x%x\n",
229 pif386rec
.optflags
, pif386rec
.memflags
,
230 pif386rec
.videoflags
);
231 WINE_TRACE("386rec: optional parameters %s\n",
232 wine_dbgstr_a(pif386rec
.optparams
));
235 /* prepare the return data */
236 lstrcpynA( progname
, pifheader
.program
, sizeof(pifheader
.program
)+1);
237 lstrcpynA( title
, pifheader
.windowtitle
, sizeof(pifheader
.windowtitle
)+1);
239 lstrcpynA( optparams
, pif386rec
.optparams
, sizeof( pif386rec
.optparams
)+1);
241 lstrcpynA( optparams
, pifheader
.optparams
, sizeof(pifheader
.optparams
)+1);
242 lstrcpynA( startdir
, pifheader
.startdir
, sizeof(pifheader
.startdir
)+1);
243 *closeonexit
= pifheader
.hdrflags1
& 0x10;
244 *textmode
= found386rec
? pif386rec
.videoflags
& 0x0010
245 : pifheader
.hdrflags1
& 0x0002;
249 /***********************************************************************
252 * execute a pif file.
254 static VOID
pif_cmd( char *filename
, char *cmdline
)
257 char progpath
[MAX_PATH
];
266 if( (hFile
= CreateFileA( filename
, GENERIC_READ
, FILE_SHARE_READ
,
267 NULL
, OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
)
269 WINE_ERR("open file %s failed\n", wine_dbgstr_a(filename
));
272 if( !read_pif_file( hFile
, progname
, title
, optparams
, startdir
,
273 &closeonexit
, &textmode
)) {
274 WINE_ERR( "failed to read %s\n", wine_dbgstr_a(filename
));
276 sprintf( buf
, "%s\nInvalid file format. Check your pif file.",
278 MessageBoxA( NULL
, buf
, "16 bit DOS subsystem", MB_OK
|MB_ICONWARNING
);
279 SetLastError( ERROR_BAD_FORMAT
);
283 if( (p
= strrchr( progname
, '.')) && !strcasecmp( p
, ".bat"))
284 WINE_FIXME(".bat programs in pif files are not supported.\n");
285 /* first change dir, so the search below can start from there */
286 if( startdir
[0] && !SetCurrentDirectoryA( startdir
)) {
287 WINE_ERR("Cannot change directory %s\n", wine_dbgstr_a( startdir
));
288 sprintf( buf
, "%s\nInvalid startup directory. Check your pif file.",
290 MessageBoxA( NULL
, buf
, "16 bit DOS subsystem", MB_OK
|MB_ICONWARNING
);
292 /* search for the program */
293 if( !SearchPathA( NULL
, progname
, NULL
, MAX_PATH
, progpath
, NULL
)) {
294 sprintf( buf
, "%s\nInvalid program file name. Check your pif file.",
296 MessageBoxA( NULL
, buf
, "16 bit DOS subsystem", MB_OK
|MB_ICONERROR
);
297 SetLastError( ERROR_FILE_NOT_FOUND
);
302 SetConsoleTitleA( title
) ;
303 /* if no arguments on the commandline, use them from the pif file */
304 if( !cmdline
[0] && optparams
[0])
306 /* FIXME: do something with:
312 start_dosbox( progpath
, cmdline
);
315 /***********************************************************************
318 * Build the command line of a process from the argv array.
319 * Copied from ENV_BuildCommandLine.
321 static char *build_command_line( char **argv
)
324 char *p
, **arg
, *cmd_line
;
327 for (arg
= argv
; *arg
; arg
++)
336 if( !*a
) has_space
=TRUE
;
341 if (*a
==' ' || *a
=='\t') {
343 } else if (*a
=='"') {
344 /* doubling of '\' preceding a '"',
345 * plus escaping of said '"'
353 len
+=(a
-*arg
)+1 /* for the separating space */;
355 len
+=2; /* for the quotes */
358 if (!(cmd_line
= HeapAlloc( GetProcessHeap(), 0, len
? len
+ 1 : 2 )))
362 *p
++ = (len
< 256) ? len
: 255;
363 for (arg
= argv
; *arg
; arg
++)
365 BOOL has_space
,has_quote
;
368 /* Check for quotes and spaces in this argument */
369 has_space
=has_quote
=FALSE
;
371 if( !*a
) has_space
=TRUE
;
373 if (*a
==' ' || *a
=='\t') {
377 } else if (*a
=='"') {
385 /* Now transfer it to the command line */
401 /* Double all the '\\' preceding this '"', plus one */
402 for (i
=0;i
<=bcount
;i
++)
420 if (len
) p
--; /* remove last space */
426 /***********************************************************************
429 static void usage(void)
431 WINE_MESSAGE( "Usage: winevdm.exe [--app-name app.exe] command line\n\n" );
436 /***********************************************************************
439 int main( int argc
, char *argv
[] )
442 HINSTANCE16 instance
;
445 char buffer
[MAX_PATH
];
447 char *cmdline
, *appname
, **first_arg
;
450 if (!argv
[1]) usage();
452 if (!strcmp( argv
[1], "--app-name" ))
454 if (!(appname
= argv
[2])) usage();
455 first_arg
= argv
+ 3;
459 if (!SearchPathA( NULL
, argv
[1], ".exe", sizeof(buffer
), buffer
, NULL
))
461 WINE_MESSAGE( "winevdm: unable to exec '%s': file not found\n", argv
[1] );
465 first_arg
= argv
+ 1;
468 if (*first_arg
) first_arg
++; /* skip program name */
469 cmdline
= build_command_line( first_arg
);
471 if (WINE_TRACE_ON(winevdm
))
474 WINE_TRACE( "GetCommandLine = '%s'\n", GetCommandLineA() );
475 WINE_TRACE( "appname = '%s'\n", appname
);
476 WINE_TRACE( "cmdline = '%.*s'\n", cmdline
[0], cmdline
+1 );
477 for (i
= 0; argv
[i
]; i
++) WINE_TRACE( "argv[%d]: '%s'\n", i
, argv
[i
] );
480 GetStartupInfoA( &info
);
482 showCmd
[1] = (info
.dwFlags
& STARTF_USESHOWWINDOW
) ? info
.wShowWindow
: SW_SHOWNORMAL
;
484 params
.hEnvironment
= 0;
485 params
.cmdLine
= MapLS( cmdline
);
486 params
.showCmd
= MapLS( showCmd
);
489 RestoreThunkLock(1); /* grab the Win16 lock */
491 /* some programs assume mmsystem is always present */
492 LoadLibrary16( "gdi.exe" );
493 LoadLibrary16( "user.exe" );
494 LoadLibrary16( "mmsystem.dll" );
496 if ((instance
= LoadModule16( appname
, ¶ms
)) < 32)
500 /* first see if it is a .pif file */
501 if( ( p
= strrchr( appname
, '.' )) && !strcasecmp( p
, ".pif"))
502 pif_cmd( appname
, cmdline
+ 1);
505 start_dosbox( appname
, cmdline
+ 1 );
508 WINE_MESSAGE( "winevdm: can't exec '%s': ", appname
);
511 case 2: WINE_MESSAGE("file not found\n" ); break;
512 case 11: WINE_MESSAGE("invalid program file\n" ); break;
513 default: WINE_MESSAGE("error=%d\n", instance
); break;
515 ExitProcess(instance
);
518 /* wait forever; the process will be killed when the last task exits */
519 ReleaseThunkLock( &count
);