server: Added access rights mapping to process and thread objects.
[wine/multimedia.git] / programs / winevdm / winevdm.c
blobff41cda7cd7d2553f02a69ff3d27a3a91e3cc4d3
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
22 #include <stdio.h>
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"
31 WINE_DEFAULT_DEBUG_CHANNEL(winevdm);
33 static void (WINAPI *wine_load_dos_exe)( LPCSTR filename, LPCSTR cmdline );
36 /*** PIF file structures ***/
37 #include "pshpack1.h"
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
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;
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;
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
92 WORD memflags; /* various memory flags*/
93 WORD videoflags; /* video flags:
94 * 0010 text
95 * 0020 med. res. graphics
96 * 0040 hi. res. graphics
98 WORD hotkey[9]; /* Hot key info */
99 CHAR optparams[64]; /* optional params, replaces those in the pif header */
100 } pif386rec_t;
102 #include "poppack.h"
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.
111 static BOOL read_pif_file( HANDLE hFile, char *progname, char *title,
112 char *optparams, char *startdir, int *closeonexit, int *textmode)
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 must be >= %d\n",
123 (int)filesize.QuadPart,
124 (sizeof(pifhead_t) + sizeof(recordhead_t)));
125 return FALSE;
127 SetFilePointer( hFile, 0, NULL, FILE_BEGIN);
128 if( !ReadFile( hFile, &pifheader, sizeof(pifhead_t), &nread, NULL))
129 return FALSE;
130 WINE_TRACE("header: program %s title %s startdir %s params %s\n",
131 wine_dbgstr_a(pifheader.program),
132 wine_dbgstr_an(pifheader.windowtitle, sizeof(pifheader.windowtitle)),
133 wine_dbgstr_a(pifheader.startdir),
134 wine_dbgstr_a(pifheader.optparams));
135 WINE_TRACE("header: memory req'd %d desr'd %d drive %d videomode %d\n",
136 pifheader.memmin, pifheader.memmax, pifheader.startdrive,
137 pifheader.videomode);
138 WINE_TRACE("header: flags 0x%x 0x%x 0x%x\n",
139 pifheader.hdrflags1, pifheader.hdrflags2, pifheader.hdrflags3);
140 ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL);
141 if( strncmp( rhead.recordname, "MICROSOFT PIFEX", 15)) {
142 WINE_ERR("Invalid pif file: magic string not found\n");
143 return FALSE;
145 /* now process the following records */
146 while( 1) {
147 WORD nextrecord = rhead.posofnextrecord;
148 if( (nextrecord & 0x8000) ||
149 filesize.QuadPart <( nextrecord + sizeof(recordhead_t))) break;
150 if( !SetFilePointer( hFile, nextrecord, NULL, FILE_BEGIN) ||
151 !ReadFile( hFile, &rhead, sizeof(recordhead_t), &nread, NULL))
152 return FALSE;
153 if( !rhead.recordname[0]) continue; /* deleted record */
154 WINE_TRACE("reading record %s size %d next 0x%x\n",
155 wine_dbgstr_a(rhead.recordname), rhead.sizeofdata,
156 rhead.posofnextrecord );
157 if( !strncmp( rhead.recordname, "WINDOWS 386", 11)) {
158 found386rec = TRUE;
159 ReadFile( hFile, &pif386rec, sizeof(pif386rec_t), &nread, NULL);
160 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",
161 pif386rec.memmin, pif386rec.memmax,
162 pif386rec.emsmin, pif386rec.emsmax,
163 pif386rec.xmsmin, pif386rec.xmsmax);
164 WINE_TRACE("386rec: option 0x%x memory 0x%x video 0x%x\n",
165 pif386rec.optflags, pif386rec.memflags,
166 pif386rec.videoflags);
167 WINE_TRACE("386rec: optional parameters %s\n",
168 wine_dbgstr_a(pif386rec.optparams));
171 /* prepare the return data */
172 strncpy( progname, pifheader.program, sizeof(pifheader.program));
173 memcpy( title, pifheader.windowtitle, sizeof(pifheader.windowtitle));
174 title[ sizeof(pifheader.windowtitle) ] = '\0';
175 if( found386rec)
176 strncpy( optparams, pif386rec.optparams, sizeof( pif386rec.optparams));
177 else
178 strncpy( optparams, pifheader.optparams, sizeof(pifheader.optparams));
179 strncpy( startdir, pifheader.startdir, sizeof(pifheader.startdir));
180 *closeonexit = pifheader.hdrflags1 & 0x10;
181 *textmode = found386rec ? pif386rec.videoflags & 0x0010
182 : pifheader.hdrflags1 & 0x0002;
183 return TRUE;
186 /***********************************************************************
187 * pif_cmd
189 * execute a pif file.
191 static VOID pif_cmd( char *filename, char *cmdline)
193 HANDLE hFile;
194 char progpath[MAX_PATH];
195 char buf[128];
196 char progname[64];
197 char title[31];
198 char optparams[64];
199 char startdir[64];
200 char *p;
201 int closeonexit;
202 int textmode;
203 if( (hFile = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ,
204 NULL, OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE)
206 WINE_ERR("open file %s failed\n", wine_dbgstr_a(filename));
207 return;
209 if( !read_pif_file( hFile, progname, title, optparams, startdir,
210 &closeonexit, &textmode)) {
211 WINE_ERR( "failed to read %s\n", wine_dbgstr_a(filename));
212 CloseHandle( hFile);
213 sprintf( buf, "%s\nInvalid file format. Check your pif file.",
214 filename);
215 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
216 SetLastError( ERROR_BAD_FORMAT);
217 return;
219 CloseHandle( hFile);
220 if( (p = strrchr( progname, '.')) && !strcasecmp( p, ".bat"))
221 WINE_FIXME(".bat programs in pif files are not supported.\n");
222 /* first change dir, so the search below can start from there */
223 if( startdir[0] && !SetCurrentDirectory( startdir)) {
224 WINE_ERR("Cannot change directory %s\n", wine_dbgstr_a( startdir));
225 sprintf( buf, "%s\nInvalid startup directory. Check your pif file.",
226 filename);
227 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONWARNING);
229 /* search for the program */
230 if( !SearchPathA( NULL, progname, NULL, MAX_PATH, progpath, NULL )) {
231 sprintf( buf, "%s\nInvalid program file name. Check your pif file.",
232 filename);
233 MessageBoxA( NULL, buf, "16 bit DOS subsystem", MB_OK|MB_ICONERROR);
234 SetLastError( ERROR_FILE_NOT_FOUND);
235 return;
237 if( textmode)
238 if( AllocConsole())
239 SetConsoleTitleA( title) ;
240 /* if no arguments on the commandline, use them from the pif file */
241 if( !cmdline[0] && optparams[0])
242 cmdline = optparams;
243 /* FIXME: do something with:
244 * - close on exit
245 * - graphic modes
246 * - hot key's
247 * - etc.
249 wine_load_dos_exe( progpath, cmdline );
250 return;
253 /***********************************************************************
254 * build_command_line
256 * Build the command line of a process from the argv array.
257 * Copied from ENV_BuildCommandLine.
259 static char *build_command_line( char **argv )
261 int len;
262 char *p, **arg, *cmd_line;
264 len = 0;
265 for (arg = argv; *arg; arg++)
267 int has_space,bcount;
268 char* a;
270 has_space=0;
271 bcount=0;
272 a=*arg;
273 if( !*a ) has_space=1;
274 while (*a!='\0') {
275 if (*a=='\\') {
276 bcount++;
277 } else {
278 if (*a==' ' || *a=='\t') {
279 has_space=1;
280 } else if (*a=='"') {
281 /* doubling of '\' preceding a '"',
282 * plus escaping of said '"'
284 len+=2*bcount+1;
286 bcount=0;
288 a++;
290 len+=(a-*arg)+1 /* for the separating space */;
291 if (has_space)
292 len+=2; /* for the quotes */
295 if (!(cmd_line = HeapAlloc( GetProcessHeap(), 0, len ? len + 1 : 2 )))
296 return NULL;
298 p = cmd_line;
299 *p++ = (len < 256) ? len : 255;
300 for (arg = argv; *arg; arg++)
302 int has_space,has_quote;
303 char* a;
305 /* Check for quotes and spaces in this argument */
306 has_space=has_quote=0;
307 a=*arg;
308 if( !*a ) has_space=1;
309 while (*a!='\0') {
310 if (*a==' ' || *a=='\t') {
311 has_space=1;
312 if (has_quote)
313 break;
314 } else if (*a=='"') {
315 has_quote=1;
316 if (has_space)
317 break;
319 a++;
322 /* Now transfer it to the command line */
323 if (has_space)
324 *p++='"';
325 if (has_quote) {
326 int bcount;
327 char* a;
329 bcount=0;
330 a=*arg;
331 while (*a!='\0') {
332 if (*a=='\\') {
333 *p++=*a;
334 bcount++;
335 } else {
336 if (*a=='"') {
337 int i;
339 /* Double all the '\\' preceding this '"', plus one */
340 for (i=0;i<=bcount;i++)
341 *p++='\\';
342 *p++='"';
343 } else {
344 *p++=*a;
346 bcount=0;
348 a++;
350 } else {
351 strcpy(p,*arg);
352 p+=strlen(*arg);
354 if (has_space)
355 *p++='"';
356 *p++=' ';
358 if (len) p--; /* remove last space */
359 *p = '\0';
360 return cmd_line;
364 /***********************************************************************
365 * usage
367 static void usage(void)
369 WINE_MESSAGE( "Usage: winevdm.exe [--app-name app.exe] command line\n\n" );
370 ExitProcess(1);
374 /***********************************************************************
375 * main
377 int main( int argc, char *argv[] )
379 DWORD count;
380 HINSTANCE16 instance;
381 LOADPARAMS16 params;
382 WORD showCmd[2];
383 char buffer[MAX_PATH];
384 STARTUPINFOA info;
385 char *cmdline, *appname, **first_arg;
386 char *p;
387 HMODULE winedos;
389 if (!argv[1]) usage();
391 if (!strcmp( argv[1], "--app-name" ))
393 if (!(appname = argv[2])) usage();
394 first_arg = argv + 3;
396 else
398 if (!SearchPathA( NULL, argv[1], ".exe", sizeof(buffer), buffer, NULL ))
400 WINE_MESSAGE( "winevdm: unable to exec '%s': file not found\n", argv[1] );
401 ExitProcess(1);
403 appname = buffer;
404 first_arg = argv + 1;
407 if (!(winedos = LoadLibraryA( "winedos.dll" )) ||
408 !(wine_load_dos_exe = (void *)GetProcAddress( winedos, "wine_load_dos_exe" )))
410 WINE_MESSAGE( "winevdm: unable to exec '%s': 16-bit support missing\n", argv[1] );
411 ExitProcess(1);
414 if (*first_arg) first_arg++; /* skip program name */
415 cmdline = build_command_line( first_arg );
417 if (WINE_TRACE_ON(winevdm))
419 int i;
420 WINE_TRACE( "GetCommandLine = '%s'\n", GetCommandLineA() );
421 WINE_TRACE( "appname = '%s'\n", appname );
422 WINE_TRACE( "cmdline = '%.*s'\n", cmdline[0], cmdline+1 );
423 for (i = 0; argv[i]; i++) WINE_TRACE( "argv[%d]: '%s'\n", i, argv[i] );
426 GetStartupInfoA( &info );
427 showCmd[0] = 2;
428 showCmd[1] = (info.dwFlags & STARTF_USESHOWWINDOW) ? info.wShowWindow : SW_SHOWNORMAL;
430 params.hEnvironment = 0;
431 params.cmdLine = MapLS( cmdline );
432 params.showCmd = MapLS( showCmd );
433 params.reserved = 0;
435 RestoreThunkLock(1); /* grab the Win16 lock */
437 /* some programs assume mmsystem is always present */
438 LoadLibrary16( "gdi.exe" );
439 LoadLibrary16( "user.exe" );
440 LoadLibrary16( "mmsystem.dll" );
442 /* make sure system drivers are loaded */
443 LoadLibrary16( "comm.drv" );
444 LoadLibrary16( "display.drv" );
445 LoadLibrary16( "keyboard.drv" );
446 LoadLibrary16( "mouse.drv" );
448 if ((instance = LoadModule16( appname, &params )) < 32)
450 if (instance == 11)
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 /* try DOS format */
457 /* loader expects arguments to be regular C strings */
458 wine_load_dos_exe( appname, cmdline + 1 );
459 /* if we get back here it failed */
460 instance = GetLastError();
463 WINE_MESSAGE( "winevdm: can't exec '%s': ", appname );
464 switch (instance)
466 case 2: WINE_MESSAGE("file not found\n" ); break;
467 case 11: WINE_MESSAGE("invalid program file\n" ); break;
468 default: WINE_MESSAGE("error=%d\n", instance ); break;
470 ExitProcess(instance);
473 /* wait forever; the process will be killed when the last task exits */
474 ReleaseThunkLock( &count );
475 Sleep( INFINITE );
476 return 0;