cmd: Reorder some functions to avoid forward declarations.
[wine] / programs / winevdm / winevdm.c
1 /*
2  * Wine virtual DOS machine
3  *
4  * Copyright 2003 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
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"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(winevdm);
32
33 static void (WINAPI *wine_load_dos_exe)( LPCSTR filename, LPCSTR cmdline );
34
35
36 /*** PIF file structures ***/
37 #include "pshpack1.h"
38
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
49                                  */
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;
67
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;
75
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
91                            */
92     WORD memflags;       /* various memory flags*/
93     WORD videoflags;     /* video flags:
94                           *   0010 text
95                           *   0020 med. res. graphics
96                           *   0040 hi. res. graphics
97                           */
98     WORD hotkey[9];      /* Hot key info */
99     CHAR optparams[64];  /* optional params, replaces those in the pif header */
100 } pif386rec_t;
101
102 #include "poppack.h"
103
104 /***********************************************************************
105  *           read_pif_file
106  *pif386rec_tu
107  * Read a pif file and return the header and possibly the 286 (real mode)
108  * record or 386 (enhanced mode) record. Returns FALSE if the file is
109  * invalid otherwise TRUE.
110  */
111 static BOOL read_pif_file( HANDLE hFile, char *progname, char *title,
112         char *optparams, char *startdir, int *closeonexit, int *textmode)
113 {
114     DWORD nread;
115     LARGE_INTEGER filesize;
116     recordhead_t rhead;
117     BOOL found386rec = FALSE;
118     pif386rec_t pif386rec;
119     pifhead_t pifheader;
120     if( !GetFileSizeEx( hFile, &filesize) ||
121             filesize.QuadPart <  (sizeof(pifhead_t) + sizeof(recordhead_t))) {
122         WINE_ERR("Invalid pif file: size error %d\n", (int)filesize.QuadPart);
123         return FALSE;
124     }
125     SetFilePointer( hFile, 0, NULL, FILE_BEGIN);
126     if( !ReadFile( hFile, &pifheader, sizeof(pifhead_t), &nread, NULL))
127         return FALSE;
128     WINE_TRACE("header: program %s title %s startdir %s params %s\n",
129             wine_dbgstr_a(pifheader.program),
130             wine_dbgstr_an(pifheader.windowtitle, sizeof(pifheader.windowtitle)),
131             wine_dbgstr_a(pifheader.startdir),
132             wine_dbgstr_a(pifheader.optparams)); 
133     WINE_TRACE("header: memory req'd %d desr'd %d drive %d videomode %d\n",
134             pifheader.memmin, pifheader.memmax, pifheader.startdrive,
135             pifheader.videomode);
136     WINE_TRACE("header: flags 0x%x 0x%x 0x%x\n",
137             pifheader.hdrflags1, pifheader.hdrflags2, pifheader.hdrflags3);
138     ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL);
139     if( strncmp( rhead.recordname, "MICROSOFT PIFEX", 15)) {
140         WINE_ERR("Invalid pif file: magic string not found\n");
141         return FALSE;
142     }
143     /* now process the following records */
144     while( 1) {
145         WORD nextrecord = rhead.posofnextrecord;
146         if( (nextrecord & 0x8000) || 
147                 filesize.QuadPart <( nextrecord + sizeof(recordhead_t))) break;
148         if( !SetFilePointer( hFile, nextrecord, NULL, FILE_BEGIN) ||
149                 !ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL))
150             return FALSE;
151         if( !rhead.recordname[0]) continue; /* deleted record */
152         WINE_TRACE("reading record %s size %d next 0x%x\n",
153                 wine_dbgstr_a(rhead.recordname), rhead.sizeofdata,
154                 rhead.posofnextrecord );
155         if( !strncmp( rhead.recordname, "WINDOWS 386", 11)) {
156             found386rec = TRUE;
157             ReadFile( hFile, &pif386rec, sizeof(pif386rec_t), &nread, NULL);
158             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",
159                     pif386rec.memmin, pif386rec.memmax,
160                     pif386rec.emsmin, pif386rec.emsmax,
161                     pif386rec.xmsmin, pif386rec.xmsmax);
162             WINE_TRACE("386rec: option 0x%x memory 0x%x video 0x%x\n",
163                     pif386rec.optflags, pif386rec.memflags,
164                     pif386rec.videoflags);
165             WINE_TRACE("386rec: optional parameters %s\n",
166                     wine_dbgstr_a(pif386rec.optparams));
167         }
168     }
169     /* prepare the return data */
170     strncpy( progname, pifheader.program, sizeof(pifheader.program));
171     memcpy( title, pifheader.windowtitle, sizeof(pifheader.windowtitle));
172     title[ sizeof(pifheader.windowtitle) ] = '\0';
173     if( found386rec)
174         strncpy( optparams, pif386rec.optparams, sizeof( pif386rec.optparams));
175     else
176         strncpy( optparams, pifheader.optparams, sizeof(pifheader.optparams));
177     strncpy( startdir, pifheader.startdir, sizeof(pifheader.startdir));
178     *closeonexit = pifheader.hdrflags1 & 0x10;
179     *textmode = found386rec ? pif386rec.videoflags & 0x0010
180                             : pifheader.hdrflags1 & 0x0002;
181     return TRUE;
182 }
183
184 /***********************************************************************
185  *              pif_cmd
186  *
187  * execute a pif file.
188  */
189 static VOID pif_cmd( char *filename, char *cmdline)
190
191     HANDLE hFile;
192     char progpath[MAX_PATH];
193     char buf[128];
194     char progname[64];
195     char title[31];
196     char optparams[64];
197     char startdir[64];
198     char *p;
199     int closeonexit;
200     int textmode;
201     if( (hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
202                     NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
203     {
204         WINE_ERR("open file %s failed\n", wine_dbgstr_a(filename));
205         return;
206     }
207     if( !read_pif_file( hFile, progname, title, optparams, startdir,
208                 &closeonexit, &textmode)) {
209         WINE_ERR( "failed to read %s\n", wine_dbgstr_a(filename));
210         CloseHandle( hFile);
211         sprintf( buf, "%s\nInvalid file format. Check your pif file.", 
212                 filename);
213         MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
214         SetLastError( ERROR_BAD_FORMAT);
215         return;
216     }
217     CloseHandle( hFile);
218     if( (p = strrchr( progname, '.')) && !strcasecmp( p, ".bat"))
219         WINE_FIXME(".bat programs in pif files are not supported.\n"); 
220     /* first change dir, so the search below can start from there */
221     if( startdir[0] && !SetCurrentDirectoryA( startdir)) {
222         WINE_ERR("Cannot change directory %s\n", wine_dbgstr_a( startdir));
223         sprintf( buf, "%s\nInvalid startup directory. Check your pif file.", 
224                 filename);
225         MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
226     }
227     /* search for the program */
228     if( !SearchPathA( NULL, progname, NULL, MAX_PATH, progpath, NULL )) {
229         sprintf( buf, "%s\nInvalid program file name. Check your pif file.", 
230                 filename);
231         MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONERROR);
232         SetLastError( ERROR_FILE_NOT_FOUND);
233         return;
234     }
235     if( textmode)
236         if( AllocConsole())
237             SetConsoleTitleA( title) ;
238     /* if no arguments on the commandline, use them from the pif file */
239     if( !cmdline[0] && optparams[0])
240         cmdline = optparams;
241     /* FIXME: do something with:
242      * - close on exit
243      * - graphic modes
244      * - hot key's
245      * - etc.
246      */ 
247     wine_load_dos_exe( progpath, cmdline );
248     return;
249 }
250
251 /***********************************************************************
252  *           build_command_line
253  *
254  * Build the command line of a process from the argv array.
255  * Copied from ENV_BuildCommandLine.
256  */
257 static char *build_command_line( char **argv )
258 {
259     int len;
260     char *p, **arg, *cmd_line;
261
262     len = 0;
263     for (arg = argv; *arg; arg++)
264     {
265         int has_space,bcount;
266         char* a;
267
268         has_space=0;
269         bcount=0;
270         a=*arg;
271         if( !*a ) has_space=1;
272         while (*a!='\0') {
273             if (*a=='\\') {
274                 bcount++;
275             } else {
276                 if (*a==' ' || *a=='\t') {
277                     has_space=1;
278                 } else if (*a=='"') {
279                     /* doubling of '\' preceding a '"',
280                      * plus escaping of said '"'
281                      */
282                     len+=2*bcount+1;
283                 }
284                 bcount=0;
285             }
286             a++;
287         }
288         len+=(a-*arg)+1 /* for the separating space */;
289         if (has_space)
290             len+=2; /* for the quotes */
291     }
292
293     if (!(cmd_line = HeapAlloc( GetProcessHeap(), 0, len ? len + 1 : 2 ))) 
294         return NULL;
295
296     p = cmd_line;
297     *p++ = (len < 256) ? len : 255;
298     for (arg = argv; *arg; arg++)
299     {
300         int has_space,has_quote;
301         char* a;
302
303         /* Check for quotes and spaces in this argument */
304         has_space=has_quote=0;
305         a=*arg;
306         if( !*a ) has_space=1;
307         while (*a!='\0') {
308             if (*a==' ' || *a=='\t') {
309                 has_space=1;
310                 if (has_quote)
311                     break;
312             } else if (*a=='"') {
313                 has_quote=1;
314                 if (has_space)
315                     break;
316             }
317             a++;
318         }
319
320         /* Now transfer it to the command line */
321         if (has_space)
322             *p++='"';
323         if (has_quote) {
324             int bcount;
325             char* a;
326
327             bcount=0;
328             a=*arg;
329             while (*a!='\0') {
330                 if (*a=='\\') {
331                     *p++=*a;
332                     bcount++;
333                 } else {
334                     if (*a=='"') {
335                         int i;
336
337                         /* Double all the '\\' preceding this '"', plus one */
338                         for (i=0;i<=bcount;i++)
339                             *p++='\\';
340                         *p++='"';
341                     } else {
342                         *p++=*a;
343                     }
344                     bcount=0;
345                 }
346                 a++;
347             }
348         } else {
349             strcpy(p,*arg);
350             p+=strlen(*arg);
351         }
352         if (has_space)
353             *p++='"';
354         *p++=' ';
355     }
356     if (len) p--;  /* remove last space */
357     *p = '\0';
358     return cmd_line;
359 }
360
361
362 /***********************************************************************
363  *           usage
364  */
365 static void usage(void)
366 {
367     WINE_MESSAGE( "Usage: winevdm.exe [--app-name app.exe] command line\n\n" );
368     ExitProcess(1);
369 }
370
371
372 /***********************************************************************
373  *           main
374  */
375 int main( int argc, char *argv[] )
376 {
377     DWORD count;
378     HINSTANCE16 instance;
379     LOADPARAMS16 params;
380     WORD showCmd[2];
381     char buffer[MAX_PATH];
382     STARTUPINFOA info;
383     char *cmdline, *appname, **first_arg;
384     char *p;
385     HMODULE winedos;
386     MEMORY_BASIC_INFORMATION mem_info;
387
388     if (!argv[1]) usage();
389
390     if (!strcmp( argv[1], "--app-name" ))
391     {
392         if (!(appname = argv[2])) usage();
393         first_arg = argv + 3;
394     }
395     else
396     {
397         if (!SearchPathA( NULL, argv[1], ".exe", sizeof(buffer), buffer, NULL ))
398         {
399             WINE_MESSAGE( "winevdm: unable to exec '%s': file not found\n", argv[1] );
400             ExitProcess(1);
401         }
402         appname = buffer;
403         first_arg = argv + 1;
404     }
405
406     if (!(winedos = LoadLibraryA( "winedos.dll" )) ||
407         !(wine_load_dos_exe = (void *)GetProcAddress( winedos, "wine_load_dos_exe" )))
408     {
409         WINE_MESSAGE( "winevdm: unable to exec '%s': DOS support unavailable\n", appname );
410         ExitProcess(1);
411     }
412
413     if (*first_arg) first_arg++;  /* skip program name */
414     cmdline = build_command_line( first_arg );
415
416     if (WINE_TRACE_ON(winevdm))
417     {
418         int i;
419         WINE_TRACE( "GetCommandLine = '%s'\n", GetCommandLineA() );
420         WINE_TRACE( "appname = '%s'\n", appname );
421         WINE_TRACE( "cmdline = '%.*s'\n", cmdline[0], cmdline+1 );
422         for (i = 0; argv[i]; i++) WINE_TRACE( "argv[%d]: '%s'\n", i, argv[i] );
423     }
424
425     GetStartupInfoA( &info );
426     showCmd[0] = 2;
427     showCmd[1] = (info.dwFlags & STARTF_USESHOWWINDOW) ? info.wShowWindow : SW_SHOWNORMAL;
428
429     params.hEnvironment = 0;
430     params.cmdLine = MapLS( cmdline );
431     params.showCmd = MapLS( showCmd );
432     params.reserved = 0;
433
434     RestoreThunkLock(1);  /* grab the Win16 lock */
435
436     /* some programs assume mmsystem is always present */
437     LoadLibrary16( "gdi.exe" );
438     LoadLibrary16( "user.exe" );
439     LoadLibrary16( "mmsystem.dll" );
440
441     /* make sure system drivers are loaded */
442     LoadLibrary16( "comm.drv" );
443     LoadLibrary16( "display.drv" );
444     LoadLibrary16( "keyboard.drv" );
445     LoadLibrary16( "mouse.drv" );
446     LoadLibrary16( "sound.drv" );
447
448     if ((instance = LoadModule16( appname, &params )) < 32)
449     {
450         if (instance == 11)
451         {
452             /* first see if it is a .pif file */
453             if( ( p = strrchr( appname, '.' )) && !strcasecmp( p, ".pif"))
454                 pif_cmd( appname, cmdline + 1);
455             else
456             {
457                 if (!VirtualQuery( NULL, &mem_info, sizeof(mem_info) ) || mem_info.State == MEM_FREE)
458                 {
459                     WINE_MESSAGE( "winevdm: unable to exec '%s': DOS memory range unavailable\n", appname );
460                     ExitProcess(1);
461                 }
462
463                 /* try DOS format */
464                 /* loader expects arguments to be regular C strings */
465                 wine_load_dos_exe( appname, cmdline + 1 );
466             }
467             /* if we get back here it failed */
468             instance = GetLastError();
469         }
470
471         WINE_MESSAGE( "winevdm: can't exec '%s': ", appname );
472         switch (instance)
473         {
474         case  2: WINE_MESSAGE("file not found\n" ); break;
475         case 11: WINE_MESSAGE("invalid program file\n" ); break;
476         default: WINE_MESSAGE("error=%d\n", instance ); break;
477         }
478         ExitProcess(instance);
479     }
480
481     /* wait forever; the process will be killed when the last task exits */
482     ReleaseThunkLock( &count );
483     Sleep( INFINITE );
484     return 0;
485 }