winspool.drv: Make cupsGetPPD optional to work around deprecation warning.
[wine.git] / programs / winevdm / winevdm.c
blobccf6c8b5d3e881bc178fff4d83dabc22615c67f3
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);
39 /*** PIF file structures ***/
40 #include "pshpack1.h"
42 /* header of a PIF file */
43 typedef struct {
44 BYTE unk1[2]; /* 0x00 */
45 CHAR windowtitle[ 30 ]; /* 0x02 seems to be padded with blanks*/
46 WORD memmax; /* 0x20 */
47 WORD memmin; /* 0x22 */
48 CHAR program[63]; /* 0x24 seems to be zero terminated */
49 BYTE hdrflags1; /* 0x63 various flags:
50 * 02 286: text mode selected
51 * 10 close window at exit
53 BYTE startdrive; /* 0x64 */
54 char startdir[64]; /* 0x65 */
55 char optparams[64]; /* 0xa5 seems to be zero terminated */
56 BYTE videomode; /* 0xe5 */
57 BYTE unkn2; /* 0xe6 ?*/
58 BYTE irqlow; /* 0xe7 */
59 BYTE irqhigh; /* 0xe8 */
60 BYTE rows; /* 0xe9 */
61 BYTE cols; /* 0xea */
62 BYTE winY; /* 0xeb */
63 BYTE winX; /* 0xec */
64 WORD unkn3; /* 0xed 7??? */
65 CHAR unkn4[64]; /* 0xef */
66 CHAR unkn5[64]; /* 0x12f */
67 BYTE hdrflags2; /* 0x16f */
68 BYTE hdrflags3; /* 0x170 */
69 } pifhead_t;
71 /* record header: present on every record */
72 typedef struct {
73 CHAR recordname[16]; /* zero terminated */
74 WORD posofnextrecord; /* file offset, 0xffff if last */
75 WORD startofdata; /* file offset */
76 WORD sizeofdata; /* data is expected to follow directly */
77 } recordhead_t;
79 /* 386 -enhanced mode- record */
80 typedef struct {
81 WORD memmax; /* memory desired, overrides the pif header*/
82 WORD memmin; /* memory required, overrides the pif header*/
83 WORD prifg; /* foreground priority */
84 WORD pribg; /* background priority */
85 WORD emsmax; /* EMS memory limit */
86 WORD emsmin; /* EMS memory required */
87 WORD xmsmax; /* XMS memory limit */
88 WORD xmsmin; /* XMS memory required */
89 WORD optflags; /* option flags:
90 * 0008 full screen
91 * 0004 exclusive
92 * 0002 background
93 * 0001 close when active
95 WORD memflags; /* various memory flags*/
96 WORD videoflags; /* video flags:
97 * 0010 text
98 * 0020 med. res. graphics
99 * 0040 hi. res. graphics
101 WORD hotkey[9]; /* Hot key info */
102 CHAR optparams[64]; /* optional params, replaces those in the pif header */
103 } pif386rec_t;
105 #include "poppack.h"
107 /***********************************************************************
108 * find_dosbox
110 static char *find_dosbox(void)
112 const char *envpath = getenv( "PATH" );
113 struct stat st;
114 char *path, *p, *buffer, *dir;
115 size_t envpath_len;
117 if (!envpath) return NULL;
119 envpath_len = strlen( envpath );
120 path = HeapAlloc( GetProcessHeap(), 0, envpath_len + 1 );
121 buffer = HeapAlloc( GetProcessHeap(), 0, envpath_len + sizeof("/dosbox") );
122 strcpy( path, envpath );
124 p = path;
125 while (*p)
127 while (*p == ':') p++;
128 if (!*p) break;
129 dir = p;
130 while (*p && *p != ':') p++;
131 if (*p == ':') *p++ = 0;
132 strcpy( buffer, dir );
133 strcat( buffer, "/dosbox" );
134 if (!stat( buffer, &st ))
136 HeapFree( GetProcessHeap(), 0, path );
137 return buffer;
140 HeapFree( GetProcessHeap(), 0, buffer );
141 HeapFree( GetProcessHeap(), 0, path );
142 return NULL;
146 /***********************************************************************
147 * start_dosbox
149 static void start_dosbox( const char *appname, const char *args )
151 static const WCHAR cfgW[] = {'c','f','g',0};
152 const char *config_dir = wine_get_config_dir();
153 WCHAR path[MAX_PATH], config[MAX_PATH];
154 HANDLE file;
155 char *p, *buffer, app[MAX_PATH];
156 int i;
157 int ret = 1;
158 DWORD written, drives = GetLogicalDrives();
159 char *dosbox = find_dosbox();
161 if (!dosbox) return;
162 if (!GetTempPathW( MAX_PATH, path )) return;
163 if (!GetTempFileNameW( path, cfgW, 0, config )) return;
164 if (!GetCurrentDirectoryW( MAX_PATH, path )) return;
165 if (!GetShortPathNameA( appname, app, MAX_PATH )) return;
166 GetShortPathNameW( path, path, MAX_PATH );
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 sizeof("mount -z c") + sizeof("config -securemode") +
172 25 * (strlen(config_dir) + sizeof("mount c /dosdevices/c:")) +
173 4 * strlenW( path ) +
174 6 + strlen( app ) + strlen( args ) + 20 );
175 p = buffer;
176 p += sprintf( p, "[autoexec]\n" );
177 for (i = 25; i >= 0; i--)
178 if (!(drives & (1 << i)))
180 p += sprintf( p, "mount -z %c\n", 'a' + i );
181 break;
183 for (i = 0; i <= 25; i++)
184 if (drives & (1 << i))
185 p += sprintf( p, "mount %c %s/dosdevices/%c:\n", 'a' + i, config_dir, 'a' + i );
186 p += sprintf( p, "%c:\ncd ", path[0] );
187 p += WideCharToMultiByte( CP_UNIXCP, 0, path + 2, -1, p, 4 * strlenW(path), NULL, NULL ) - 1;
188 p += sprintf( p, "\nconfig -securemode\n" );
189 p += sprintf( p, "%s %s\n", app, args );
190 p += sprintf( p, "exit\n" );
191 if (WriteFile( file, buffer, strlen(buffer), &written, NULL ) && written == strlen(buffer))
193 const char *args[5];
194 char *config_file = wine_get_unix_file_name( config );
195 args[0] = dosbox;
196 args[1] = "-userconf";
197 args[2] = "-conf";
198 args[3] = config_file;
199 args[4] = NULL;
200 ret = _spawnvp( _P_WAIT, args[0], args );
202 CloseHandle( file );
203 DeleteFileW( config );
204 HeapFree( GetProcessHeap(), 0, buffer );
205 ExitProcess( ret );
209 /***********************************************************************
210 * start_dos_exe
212 static void start_dos_exe( LPCSTR filename, LPCSTR cmdline )
214 start_dosbox( filename, cmdline );
215 WINE_MESSAGE( "winevdm: %s is a DOS application, you need to install DOSBox.\n", filename );
216 ExitProcess(1);
219 /***********************************************************************
220 * read_pif_file
221 *pif386rec_tu
222 * Read a pif file and return the header and possibly the 286 (real mode)
223 * record or 386 (enhanced mode) record. Returns FALSE if the file is
224 * invalid otherwise TRUE.
226 static BOOL read_pif_file( HANDLE hFile, char *progname, char *title,
227 char *optparams, char *startdir, int *closeonexit, int *textmode)
229 DWORD nread;
230 LARGE_INTEGER filesize;
231 recordhead_t rhead;
232 BOOL found386rec = FALSE;
233 pif386rec_t pif386rec;
234 pifhead_t pifheader;
235 if( !GetFileSizeEx( hFile, &filesize) ||
236 filesize.QuadPart < (sizeof(pifhead_t) + sizeof(recordhead_t))) {
237 WINE_ERR("Invalid pif file: size error %d\n", (int)filesize.QuadPart);
238 return FALSE;
240 SetFilePointer( hFile, 0, NULL, FILE_BEGIN);
241 if( !ReadFile( hFile, &pifheader, sizeof(pifhead_t), &nread, NULL))
242 return FALSE;
243 WINE_TRACE("header: program %s title %s startdir %s params %s\n",
244 wine_dbgstr_a(pifheader.program),
245 wine_dbgstr_an(pifheader.windowtitle, sizeof(pifheader.windowtitle)),
246 wine_dbgstr_a(pifheader.startdir),
247 wine_dbgstr_a(pifheader.optparams));
248 WINE_TRACE("header: memory req'd %d desr'd %d drive %d videomode %d\n",
249 pifheader.memmin, pifheader.memmax, pifheader.startdrive,
250 pifheader.videomode);
251 WINE_TRACE("header: flags 0x%x 0x%x 0x%x\n",
252 pifheader.hdrflags1, pifheader.hdrflags2, pifheader.hdrflags3);
253 ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL);
254 if( strncmp( rhead.recordname, "MICROSOFT PIFEX", 15)) {
255 WINE_ERR("Invalid pif file: magic string not found\n");
256 return FALSE;
258 /* now process the following records */
259 while( 1) {
260 WORD nextrecord = rhead.posofnextrecord;
261 if( (nextrecord & 0x8000) ||
262 filesize.QuadPart <( nextrecord + sizeof(recordhead_t))) break;
263 if( !SetFilePointer( hFile, nextrecord, NULL, FILE_BEGIN) ||
264 !ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL))
265 return FALSE;
266 if( !rhead.recordname[0]) continue; /* deleted record */
267 WINE_TRACE("reading record %s size %d next 0x%x\n",
268 wine_dbgstr_a(rhead.recordname), rhead.sizeofdata,
269 rhead.posofnextrecord );
270 if( !strncmp( rhead.recordname, "WINDOWS 386", 11)) {
271 found386rec = TRUE;
272 ReadFile( hFile, &pif386rec, sizeof(pif386rec_t), &nread, NULL);
273 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",
274 pif386rec.memmin, pif386rec.memmax,
275 pif386rec.emsmin, pif386rec.emsmax,
276 pif386rec.xmsmin, pif386rec.xmsmax);
277 WINE_TRACE("386rec: option 0x%x memory 0x%x video 0x%x\n",
278 pif386rec.optflags, pif386rec.memflags,
279 pif386rec.videoflags);
280 WINE_TRACE("386rec: optional parameters %s\n",
281 wine_dbgstr_a(pif386rec.optparams));
284 /* prepare the return data */
285 lstrcpynA( progname, pifheader.program, sizeof(pifheader.program)+1);
286 lstrcpynA( title, pifheader.windowtitle, sizeof(pifheader.windowtitle)+1);
287 if( found386rec)
288 lstrcpynA( optparams, pif386rec.optparams, sizeof( pif386rec.optparams)+1);
289 else
290 lstrcpynA( optparams, pifheader.optparams, sizeof(pifheader.optparams)+1);
291 lstrcpynA( startdir, pifheader.startdir, sizeof(pifheader.startdir)+1);
292 *closeonexit = pifheader.hdrflags1 & 0x10;
293 *textmode = found386rec ? pif386rec.videoflags & 0x0010
294 : pifheader.hdrflags1 & 0x0002;
295 return TRUE;
298 /***********************************************************************
299 * pif_cmd
301 * execute a pif file.
303 static VOID pif_cmd( char *filename, char *cmdline)
305 HANDLE hFile;
306 char progpath[MAX_PATH];
307 char buf[128];
308 char progname[64];
309 char title[31];
310 char optparams[65];
311 char startdir[65];
312 char *p;
313 int closeonexit;
314 int textmode;
315 if( (hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
316 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
318 WINE_ERR("open file %s failed\n", wine_dbgstr_a(filename));
319 return;
321 if( !read_pif_file( hFile, progname, title, optparams, startdir,
322 &closeonexit, &textmode)) {
323 WINE_ERR( "failed to read %s\n", wine_dbgstr_a(filename));
324 CloseHandle( hFile);
325 sprintf( buf, "%s\nInvalid file format. Check your pif file.",
326 filename);
327 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
328 SetLastError( ERROR_BAD_FORMAT);
329 return;
331 CloseHandle( hFile);
332 if( (p = strrchr( progname, '.')) && !strcasecmp( p, ".bat"))
333 WINE_FIXME(".bat programs in pif files are not supported.\n");
334 /* first change dir, so the search below can start from there */
335 if( startdir[0] && !SetCurrentDirectoryA( startdir)) {
336 WINE_ERR("Cannot change directory %s\n", wine_dbgstr_a( startdir));
337 sprintf( buf, "%s\nInvalid startup directory. Check your pif file.",
338 filename);
339 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
341 /* search for the program */
342 if( !SearchPathA( NULL, progname, NULL, MAX_PATH, progpath, NULL )) {
343 sprintf( buf, "%s\nInvalid program file name. Check your pif file.",
344 filename);
345 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONERROR);
346 SetLastError( ERROR_FILE_NOT_FOUND);
347 return;
349 if( textmode)
350 if( AllocConsole())
351 SetConsoleTitleA( title) ;
352 /* if no arguments on the commandline, use them from the pif file */
353 if( !cmdline[0] && optparams[0])
354 cmdline = optparams;
355 /* FIXME: do something with:
356 * - close on exit
357 * - graphic modes
358 * - hot key's
359 * - etc.
361 start_dos_exe( progpath, cmdline );
364 /***********************************************************************
365 * build_command_line
367 * Build the command line of a process from the argv array.
368 * Copied from ENV_BuildCommandLine.
370 static char *build_command_line( char **argv )
372 int len;
373 char *p, **arg, *cmd_line;
375 len = 0;
376 for (arg = argv; *arg; arg++)
378 BOOL has_space;
379 int bcount;
380 char* a;
382 has_space=FALSE;
383 bcount=0;
384 a=*arg;
385 if( !*a ) has_space=TRUE;
386 while (*a!='\0') {
387 if (*a=='\\') {
388 bcount++;
389 } else {
390 if (*a==' ' || *a=='\t') {
391 has_space=TRUE;
392 } else if (*a=='"') {
393 /* doubling of '\' preceding a '"',
394 * plus escaping of said '"'
396 len+=2*bcount+1;
398 bcount=0;
400 a++;
402 len+=(a-*arg)+1 /* for the separating space */;
403 if (has_space)
404 len+=2; /* for the quotes */
407 if (!(cmd_line = HeapAlloc( GetProcessHeap(), 0, len ? len + 1 : 2 )))
408 return NULL;
410 p = cmd_line;
411 *p++ = (len < 256) ? len : 255;
412 for (arg = argv; *arg; arg++)
414 BOOL has_space,has_quote;
415 char* a;
417 /* Check for quotes and spaces in this argument */
418 has_space=has_quote=FALSE;
419 a=*arg;
420 if( !*a ) has_space=TRUE;
421 while (*a!='\0') {
422 if (*a==' ' || *a=='\t') {
423 has_space=TRUE;
424 if (has_quote)
425 break;
426 } else if (*a=='"') {
427 has_quote=TRUE;
428 if (has_space)
429 break;
431 a++;
434 /* Now transfer it to the command line */
435 if (has_space)
436 *p++='"';
437 if (has_quote) {
438 int bcount;
440 bcount=0;
441 a=*arg;
442 while (*a!='\0') {
443 if (*a=='\\') {
444 *p++=*a;
445 bcount++;
446 } else {
447 if (*a=='"') {
448 int i;
450 /* Double all the '\\' preceding this '"', plus one */
451 for (i=0;i<=bcount;i++)
452 *p++='\\';
453 *p++='"';
454 } else {
455 *p++=*a;
457 bcount=0;
459 a++;
461 } else {
462 strcpy(p,*arg);
463 p+=strlen(*arg);
465 if (has_space)
466 *p++='"';
467 *p++=' ';
469 if (len) p--; /* remove last space */
470 *p = '\0';
471 return cmd_line;
475 /***********************************************************************
476 * usage
478 static void usage(void)
480 WINE_MESSAGE( "Usage: winevdm.exe [--app-name app.exe] command line\n\n" );
481 ExitProcess(1);
485 /***********************************************************************
486 * main
488 int main( int argc, char *argv[] )
490 DWORD count;
491 HINSTANCE16 instance;
492 LOADPARAMS16 params;
493 WORD showCmd[2];
494 char buffer[MAX_PATH];
495 STARTUPINFOA info;
496 char *cmdline, *appname, **first_arg;
497 char *p;
499 if (!argv[1]) usage();
501 if (!strcmp( argv[1], "--app-name" ))
503 if (!(appname = argv[2])) usage();
504 first_arg = argv + 3;
506 else
508 if (!SearchPathA( NULL, argv[1], ".exe", sizeof(buffer), buffer, NULL ))
510 WINE_MESSAGE( "winevdm: unable to exec '%s': file not found\n", argv[1] );
511 ExitProcess(1);
513 appname = buffer;
514 first_arg = argv + 1;
517 if (*first_arg) first_arg++; /* skip program name */
518 cmdline = build_command_line( first_arg );
520 if (WINE_TRACE_ON(winevdm))
522 int i;
523 WINE_TRACE( "GetCommandLine = '%s'\n", GetCommandLineA() );
524 WINE_TRACE( "appname = '%s'\n", appname );
525 WINE_TRACE( "cmdline = '%.*s'\n", cmdline[0], cmdline+1 );
526 for (i = 0; argv[i]; i++) WINE_TRACE( "argv[%d]: '%s'\n", i, argv[i] );
529 GetStartupInfoA( &info );
530 showCmd[0] = 2;
531 showCmd[1] = (info.dwFlags & STARTF_USESHOWWINDOW) ? info.wShowWindow : SW_SHOWNORMAL;
533 params.hEnvironment = 0;
534 params.cmdLine = MapLS( cmdline );
535 params.showCmd = MapLS( showCmd );
536 params.reserved = 0;
538 RestoreThunkLock(1); /* grab the Win16 lock */
540 /* some programs assume mmsystem is always present */
541 LoadLibrary16( "gdi.exe" );
542 LoadLibrary16( "user.exe" );
543 LoadLibrary16( "mmsystem.dll" );
545 if ((instance = LoadModule16( appname, &params )) < 32)
547 if (instance == 11)
549 /* first see if it is a .pif file */
550 if( ( p = strrchr( appname, '.' )) && !strcasecmp( p, ".pif"))
551 pif_cmd( appname, cmdline + 1);
552 else
554 /* try DOS format */
555 /* loader expects arguments to be regular C strings */
556 start_dos_exe( appname, cmdline + 1 );
558 /* if we get back here it failed */
559 instance = GetLastError();
562 WINE_MESSAGE( "winevdm: can't exec '%s': ", appname );
563 switch (instance)
565 case 2: WINE_MESSAGE("file not found\n" ); break;
566 case 11: WINE_MESSAGE("invalid program file\n" ); break;
567 default: WINE_MESSAGE("error=%d\n", instance ); break;
569 ExitProcess(instance);
572 /* wait forever; the process will be killed when the last task exits */
573 ReleaseThunkLock( &count );
574 Sleep( INFINITE );
575 return 0;