dwrite: Implement GetWeight() for IDWriteFont.
[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 "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
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"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(winevdm);
37
38 extern void __wine_load_dos_exe( LPCSTR filename, LPCSTR cmdline );
39
40
41 /*** PIF file structures ***/
42 #include "pshpack1.h"
43
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
54                                  */
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;
72
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;
80
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
96                            */
97     WORD memflags;       /* various memory flags*/
98     WORD videoflags;     /* video flags:
99                           *   0010 text
100                           *   0020 med. res. graphics
101                           *   0040 hi. res. graphics
102                           */
103     WORD hotkey[9];      /* Hot key info */
104     CHAR optparams[64];  /* optional params, replaces those in the pif header */
105 } pif386rec_t;
106
107 #include "poppack.h"
108
109 /***********************************************************************
110  *           find_dosbox
111  */
112 static char *find_dosbox(void)
113 {
114     const char *envpath = getenv( "PATH" );
115     struct stat st;
116     char *path, *p, *buffer, *dir;
117     size_t envpath_len;
118
119     if (!envpath) return NULL;
120
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 );
125
126     p = path;
127     while (*p)
128     {
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 ))
137         {
138             HeapFree( GetProcessHeap(), 0, path );
139             return buffer;
140         }
141     }
142     HeapFree( GetProcessHeap(), 0, buffer );
143     HeapFree( GetProcessHeap(), 0, path );
144     return NULL;
145 }
146
147
148 /***********************************************************************
149  *           start_dosbox
150  */
151 static void start_dosbox( const char *appname, const char *args )
152 {
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, app[MAX_PATH];
158     int i;
159     int ret = 1;
160     DWORD written, drives = GetLogicalDrives();
161     char *dosbox = find_dosbox();
162
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     if (!GetShortPathNameA( appname, app, MAX_PATH )) return;
168     GetShortPathNameW( path, path, MAX_PATH );
169     file = CreateFileW( config, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
170     if (file == INVALID_HANDLE_VALUE) return;
171
172     buffer = HeapAlloc( GetProcessHeap(), 0, sizeof("[autoexec]") +
173                         sizeof("mount -z c") + sizeof("config -securemode") +
174                         25 * (strlen(config_dir) + sizeof("mount c /dosdevices/c:")) +
175                         4 * strlenW( path ) +
176                         6 + strlen( app ) + strlen( args ) + 20 );
177     p = buffer;
178     p += sprintf( p, "[autoexec]\n" );
179     for (i = 25; i >= 0; i--)
180         if (!(drives & (1 << i)))
181         {
182             p += sprintf( p, "mount -z %c\n", 'a' + i );
183             break;
184         }
185     for (i = 0; i <= 25; i++)
186         if (drives & (1 << i))
187             p += sprintf( p, "mount %c %s/dosdevices/%c:\n", 'a' + i, config_dir, 'a' + i );
188     p += sprintf( p, "%c:\ncd ", path[0] );
189     p += WideCharToMultiByte( CP_UNIXCP, 0, path + 2, -1, p, 4 * strlenW(path), NULL, NULL ) - 1;
190     p += sprintf( p, "\nconfig -securemode\n" );
191     p += sprintf( p, "%s %s\n", app, args );
192     p += sprintf( p, "exit\n" );
193     if (WriteFile( file, buffer, strlen(buffer), &written, NULL ) && written == strlen(buffer))
194     {
195         const char *args[4];
196         char *config_file = wine_get_unix_file_name( config );
197         args[0] = dosbox;
198         args[1] = "-conf";
199         args[2] = config_file;
200         args[3] = NULL;
201         ret = spawnvp( _P_WAIT, args[0], args );
202     }
203     CloseHandle( file );
204     DeleteFileW( config );
205     HeapFree( GetProcessHeap(), 0, buffer );
206     ExitProcess( ret );
207 }
208
209
210 /***********************************************************************
211  *           start_dos_exe
212  */
213 static void start_dos_exe( LPCSTR filename, LPCSTR cmdline )
214 {
215     MEMORY_BASIC_INFORMATION mem_info;
216     const char *reason;
217
218     start_dosbox( filename, cmdline );
219
220     if (VirtualQuery( NULL, &mem_info, sizeof(mem_info) ) && mem_info.State != MEM_FREE)
221     {
222         __wine_load_dos_exe( filename, cmdline );
223         if (GetLastError() == ERROR_NOT_SUPPORTED)
224             reason = "because vm86 mode is not supported on this platform";
225         else
226             reason = wine_dbg_sprintf( "It failed with error code %u", GetLastError() );
227     }
228     else reason = "because the DOS memory range is unavailable";
229
230     WINE_MESSAGE( "winevdm: Cannot start DOS application %s\n", filename );
231     WINE_MESSAGE( "         %s.\n", reason );
232     WINE_MESSAGE( "         You should install DOSBox.\n" );
233     ExitProcess(1);
234 }
235
236 /***********************************************************************
237  *           read_pif_file
238  *pif386rec_tu
239  * Read a pif file and return the header and possibly the 286 (real mode)
240  * record or 386 (enhanced mode) record. Returns FALSE if the file is
241  * invalid otherwise TRUE.
242  */
243 static BOOL read_pif_file( HANDLE hFile, char *progname, char *title,
244         char *optparams, char *startdir, int *closeonexit, int *textmode)
245 {
246     DWORD nread;
247     LARGE_INTEGER filesize;
248     recordhead_t rhead;
249     BOOL found386rec = FALSE;
250     pif386rec_t pif386rec;
251     pifhead_t pifheader;
252     if( !GetFileSizeEx( hFile, &filesize) ||
253             filesize.QuadPart <  (sizeof(pifhead_t) + sizeof(recordhead_t))) {
254         WINE_ERR("Invalid pif file: size error %d\n", (int)filesize.QuadPart);
255         return FALSE;
256     }
257     SetFilePointer( hFile, 0, NULL, FILE_BEGIN);
258     if( !ReadFile( hFile, &pifheader, sizeof(pifhead_t), &nread, NULL))
259         return FALSE;
260     WINE_TRACE("header: program %s title %s startdir %s params %s\n",
261             wine_dbgstr_a(pifheader.program),
262             wine_dbgstr_an(pifheader.windowtitle, sizeof(pifheader.windowtitle)),
263             wine_dbgstr_a(pifheader.startdir),
264             wine_dbgstr_a(pifheader.optparams)); 
265     WINE_TRACE("header: memory req'd %d desr'd %d drive %d videomode %d\n",
266             pifheader.memmin, pifheader.memmax, pifheader.startdrive,
267             pifheader.videomode);
268     WINE_TRACE("header: flags 0x%x 0x%x 0x%x\n",
269             pifheader.hdrflags1, pifheader.hdrflags2, pifheader.hdrflags3);
270     ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL);
271     if( strncmp( rhead.recordname, "MICROSOFT PIFEX", 15)) {
272         WINE_ERR("Invalid pif file: magic string not found\n");
273         return FALSE;
274     }
275     /* now process the following records */
276     while( 1) {
277         WORD nextrecord = rhead.posofnextrecord;
278         if( (nextrecord & 0x8000) || 
279                 filesize.QuadPart <( nextrecord + sizeof(recordhead_t))) break;
280         if( !SetFilePointer( hFile, nextrecord, NULL, FILE_BEGIN) ||
281                 !ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL))
282             return FALSE;
283         if( !rhead.recordname[0]) continue; /* deleted record */
284         WINE_TRACE("reading record %s size %d next 0x%x\n",
285                 wine_dbgstr_a(rhead.recordname), rhead.sizeofdata,
286                 rhead.posofnextrecord );
287         if( !strncmp( rhead.recordname, "WINDOWS 386", 11)) {
288             found386rec = TRUE;
289             ReadFile( hFile, &pif386rec, sizeof(pif386rec_t), &nread, NULL);
290             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",
291                     pif386rec.memmin, pif386rec.memmax,
292                     pif386rec.emsmin, pif386rec.emsmax,
293                     pif386rec.xmsmin, pif386rec.xmsmax);
294             WINE_TRACE("386rec: option 0x%x memory 0x%x video 0x%x\n",
295                     pif386rec.optflags, pif386rec.memflags,
296                     pif386rec.videoflags);
297             WINE_TRACE("386rec: optional parameters %s\n",
298                     wine_dbgstr_a(pif386rec.optparams));
299         }
300     }
301     /* prepare the return data */
302     lstrcpynA( progname, pifheader.program, sizeof(pifheader.program)+1);
303     lstrcpynA( title, pifheader.windowtitle, sizeof(pifheader.windowtitle)+1);
304     if( found386rec)
305         lstrcpynA( optparams, pif386rec.optparams, sizeof( pif386rec.optparams)+1);
306     else
307         lstrcpynA( optparams, pifheader.optparams, sizeof(pifheader.optparams)+1);
308     lstrcpynA( startdir, pifheader.startdir, sizeof(pifheader.startdir)+1);
309     *closeonexit = pifheader.hdrflags1 & 0x10;
310     *textmode = found386rec ? pif386rec.videoflags & 0x0010
311                             : pifheader.hdrflags1 & 0x0002;
312     return TRUE;
313 }
314
315 /***********************************************************************
316  *              pif_cmd
317  *
318  * execute a pif file.
319  */
320 static VOID pif_cmd( char *filename, char *cmdline)
321
322     HANDLE hFile;
323     char progpath[MAX_PATH];
324     char buf[128];
325     char progname[64];
326     char title[31];
327     char optparams[65];
328     char startdir[65];
329     char *p;
330     int closeonexit;
331     int textmode;
332     if( (hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
333                     NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
334     {
335         WINE_ERR("open file %s failed\n", wine_dbgstr_a(filename));
336         return;
337     }
338     if( !read_pif_file( hFile, progname, title, optparams, startdir,
339                 &closeonexit, &textmode)) {
340         WINE_ERR( "failed to read %s\n", wine_dbgstr_a(filename));
341         CloseHandle( hFile);
342         sprintf( buf, "%s\nInvalid file format. Check your pif file.", 
343                 filename);
344         MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
345         SetLastError( ERROR_BAD_FORMAT);
346         return;
347     }
348     CloseHandle( hFile);
349     if( (p = strrchr( progname, '.')) && !strcasecmp( p, ".bat"))
350         WINE_FIXME(".bat programs in pif files are not supported.\n"); 
351     /* first change dir, so the search below can start from there */
352     if( startdir[0] && !SetCurrentDirectoryA( startdir)) {
353         WINE_ERR("Cannot change directory %s\n", wine_dbgstr_a( startdir));
354         sprintf( buf, "%s\nInvalid startup directory. Check your pif file.", 
355                 filename);
356         MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
357     }
358     /* search for the program */
359     if( !SearchPathA( NULL, progname, NULL, MAX_PATH, progpath, NULL )) {
360         sprintf( buf, "%s\nInvalid program file name. Check your pif file.", 
361                 filename);
362         MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONERROR);
363         SetLastError( ERROR_FILE_NOT_FOUND);
364         return;
365     }
366     if( textmode)
367         if( AllocConsole())
368             SetConsoleTitleA( title) ;
369     /* if no arguments on the commandline, use them from the pif file */
370     if( !cmdline[0] && optparams[0])
371         cmdline = optparams;
372     /* FIXME: do something with:
373      * - close on exit
374      * - graphic modes
375      * - hot key's
376      * - etc.
377      */ 
378     start_dos_exe( progpath, cmdline );
379 }
380
381 /***********************************************************************
382  *           build_command_line
383  *
384  * Build the command line of a process from the argv array.
385  * Copied from ENV_BuildCommandLine.
386  */
387 static char *build_command_line( char **argv )
388 {
389     int len;
390     char *p, **arg, *cmd_line;
391
392     len = 0;
393     for (arg = argv; *arg; arg++)
394     {
395         int has_space,bcount;
396         char* a;
397
398         has_space=0;
399         bcount=0;
400         a=*arg;
401         if( !*a ) has_space=1;
402         while (*a!='\0') {
403             if (*a=='\\') {
404                 bcount++;
405             } else {
406                 if (*a==' ' || *a=='\t') {
407                     has_space=1;
408                 } else if (*a=='"') {
409                     /* doubling of '\' preceding a '"',
410                      * plus escaping of said '"'
411                      */
412                     len+=2*bcount+1;
413                 }
414                 bcount=0;
415             }
416             a++;
417         }
418         len+=(a-*arg)+1 /* for the separating space */;
419         if (has_space)
420             len+=2; /* for the quotes */
421     }
422
423     if (!(cmd_line = HeapAlloc( GetProcessHeap(), 0, len ? len + 1 : 2 ))) 
424         return NULL;
425
426     p = cmd_line;
427     *p++ = (len < 256) ? len : 255;
428     for (arg = argv; *arg; arg++)
429     {
430         int has_space,has_quote;
431         char* a;
432
433         /* Check for quotes and spaces in this argument */
434         has_space=has_quote=0;
435         a=*arg;
436         if( !*a ) has_space=1;
437         while (*a!='\0') {
438             if (*a==' ' || *a=='\t') {
439                 has_space=1;
440                 if (has_quote)
441                     break;
442             } else if (*a=='"') {
443                 has_quote=1;
444                 if (has_space)
445                     break;
446             }
447             a++;
448         }
449
450         /* Now transfer it to the command line */
451         if (has_space)
452             *p++='"';
453         if (has_quote) {
454             int bcount;
455
456             bcount=0;
457             a=*arg;
458             while (*a!='\0') {
459                 if (*a=='\\') {
460                     *p++=*a;
461                     bcount++;
462                 } else {
463                     if (*a=='"') {
464                         int i;
465
466                         /* Double all the '\\' preceding this '"', plus one */
467                         for (i=0;i<=bcount;i++)
468                             *p++='\\';
469                         *p++='"';
470                     } else {
471                         *p++=*a;
472                     }
473                     bcount=0;
474                 }
475                 a++;
476             }
477         } else {
478             strcpy(p,*arg);
479             p+=strlen(*arg);
480         }
481         if (has_space)
482             *p++='"';
483         *p++=' ';
484     }
485     if (len) p--;  /* remove last space */
486     *p = '\0';
487     return cmd_line;
488 }
489
490
491 /***********************************************************************
492  *           usage
493  */
494 static void usage(void)
495 {
496     WINE_MESSAGE( "Usage: winevdm.exe [--app-name app.exe] command line\n\n" );
497     ExitProcess(1);
498 }
499
500
501 /***********************************************************************
502  *           main
503  */
504 int main( int argc, char *argv[] )
505 {
506     DWORD count;
507     HINSTANCE16 instance;
508     LOADPARAMS16 params;
509     WORD showCmd[2];
510     char buffer[MAX_PATH];
511     STARTUPINFOA info;
512     char *cmdline, *appname, **first_arg;
513     char *p;
514
515     if (!argv[1]) usage();
516
517     if (!strcmp( argv[1], "--app-name" ))
518     {
519         if (!(appname = argv[2])) usage();
520         first_arg = argv + 3;
521     }
522     else
523     {
524         if (!SearchPathA( NULL, argv[1], ".exe", sizeof(buffer), buffer, NULL ))
525         {
526             WINE_MESSAGE( "winevdm: unable to exec '%s': file not found\n", argv[1] );
527             ExitProcess(1);
528         }
529         appname = buffer;
530         first_arg = argv + 1;
531     }
532
533     if (*first_arg) first_arg++;  /* skip program name */
534     cmdline = build_command_line( first_arg );
535
536     if (WINE_TRACE_ON(winevdm))
537     {
538         int i;
539         WINE_TRACE( "GetCommandLine = '%s'\n", GetCommandLineA() );
540         WINE_TRACE( "appname = '%s'\n", appname );
541         WINE_TRACE( "cmdline = '%.*s'\n", cmdline[0], cmdline+1 );
542         for (i = 0; argv[i]; i++) WINE_TRACE( "argv[%d]: '%s'\n", i, argv[i] );
543     }
544
545     GetStartupInfoA( &info );
546     showCmd[0] = 2;
547     showCmd[1] = (info.dwFlags & STARTF_USESHOWWINDOW) ? info.wShowWindow : SW_SHOWNORMAL;
548
549     params.hEnvironment = 0;
550     params.cmdLine = MapLS( cmdline );
551     params.showCmd = MapLS( showCmd );
552     params.reserved = 0;
553
554     RestoreThunkLock(1);  /* grab the Win16 lock */
555
556     /* some programs assume mmsystem is always present */
557     LoadLibrary16( "gdi.exe" );
558     LoadLibrary16( "user.exe" );
559     LoadLibrary16( "mmsystem.dll" );
560
561     if ((instance = LoadModule16( appname, &params )) < 32)
562     {
563         if (instance == 11)
564         {
565             /* first see if it is a .pif file */
566             if( ( p = strrchr( appname, '.' )) && !strcasecmp( p, ".pif"))
567                 pif_cmd( appname, cmdline + 1);
568             else
569             {
570                 /* try DOS format */
571                 /* loader expects arguments to be regular C strings */
572                 start_dos_exe( appname, cmdline + 1 );
573             }
574             /* if we get back here it failed */
575             instance = GetLastError();
576         }
577
578         WINE_MESSAGE( "winevdm: can't exec '%s': ", appname );
579         switch (instance)
580         {
581         case  2: WINE_MESSAGE("file not found\n" ); break;
582         case 11: WINE_MESSAGE("invalid program file\n" ); break;
583         default: WINE_MESSAGE("error=%d\n", instance ); break;
584         }
585         ExitProcess(instance);
586     }
587
588     /* wait forever; the process will be killed when the last task exits */
589     ReleaseThunkLock( &count );
590     Sleep( INFINITE );
591     return 0;
592 }