Added check for invalid parent item.
[wine/testsucceed.git] / miscemu / main.c
blob3df298308c0516a773209bbb34bcb2912751aaf4
1 /*
2 * Emulator initialisation code
4 */
6 #include <stdlib.h>
7 #include <assert.h>
8 #include "wine/winbase16.h"
9 #include "callback.h"
10 #include "debugger.h"
11 #include "main.h"
12 #include "miscemu.h"
13 #include "win16drv.h"
14 #include "module.h"
15 #include "options.h"
16 #include "process.h"
17 #include "thread.h"
18 #include "task.h"
19 #include "stackframe.h"
20 #include "wine/exception.h"
21 #include "debug.h"
23 static int MAIN_argc;
24 static char **MAIN_argv;
26 extern int (*INSTR_IsRelay)( const void *addr );
28 static int is_relay_addr( const void *addr )
30 extern char CallFrom16_Start, CallFrom16_End, CALLTO16_Start, CALLTO16_End;
32 return ((((char *)addr >= &CallFrom16_Start) &&
33 ((char *)addr < &CallFrom16_End)) ||
34 (((char *)addr >= &CALLTO16_Start) &&
35 ((char *)addr < &CALLTO16_End)));
38 /***********************************************************************
39 * Emulator initialisation
41 BOOL MAIN_EmulatorInit(void)
43 /* Main initialization */
44 if (!MAIN_MainInit()) return FALSE;
46 /* Initialize relay code */
47 if (!RELAY_Init()) return FALSE;
49 /* Create the Win16 printer driver */
50 if (!WIN16DRV_Init()) return FALSE;
52 return TRUE;
56 /***********************************************************************
57 * Main loop of initial task
59 void MAIN_EmulatorRun( void )
61 extern void THUNK_InitCallout( void );
62 char startProg[256], defProg[256];
63 HINSTANCE handle;
64 int i, tasks = 0;
65 MSG msg;
67 /* Load system DLLs into the initial process (and initialize them) */
68 if ( !LoadLibrary16("GDI.EXE" ) || !LoadLibraryA("GDI32.DLL" )
69 || !LoadLibrary16("USER.EXE") || !LoadLibraryA("USER32.DLL"))
70 ExitProcess( 1 );
72 /* Get pointers to USER routines called by KERNEL */
73 THUNK_InitCallout();
75 /* Call InitApp for initial task */
76 Callout.InitApp16( MapHModuleLS( 0 ) );
78 /* Add the Default Program if no program on the command line */
79 if (!MAIN_argv[1])
81 PROFILE_GetWineIniString( "programs", "Default", "",
82 defProg, sizeof(defProg) );
83 if (defProg[0]) MAIN_argv[MAIN_argc++] = defProg;
86 /* Add the Startup Program to the run list */
87 PROFILE_GetWineIniString( "programs", "Startup", "",
88 startProg, sizeof(startProg) );
89 if (startProg[0]) MAIN_argv[MAIN_argc++] = startProg;
91 /* Abort if no executable on command line */
92 if (MAIN_argc <= 1)
94 MAIN_Usage(MAIN_argv[0]);
95 ExitProcess( 1 );
98 /* Load and run executables given on command line */
99 for (i = 1; i < MAIN_argc; i++)
101 if ((handle = WinExec( MAIN_argv[i], SW_SHOWNORMAL )) < 32)
103 MSG("wine: can't exec '%s': ", MAIN_argv[i]);
104 switch (handle)
106 case 2: MSG("file not found\n" ); break;
107 case 11: MSG("invalid exe file\n" ); break;
108 default: MSG("error=%d\n", handle ); break;
111 else tasks++;
114 if (!tasks)
116 MSG("wine: no executable file found.\n" );
117 ExitProcess( 0 );
120 /* Start message loop for desktop window */
122 while ( GetNumTasks16() > 1 && Callout.GetMessageA( &msg, 0, 0, 0 ) )
124 Callout.TranslateMessage( &msg );
125 Callout.DispatchMessageA( &msg );
128 ExitProcess( 0 );
132 /**********************************************************************
133 * main
135 int main( int argc, char *argv[] )
137 NE_MODULE *pModule;
138 extern char * DEBUG_argv0;
140 __winelib = 0; /* First of all, clear the Winelib flag */
143 * Save this so that the internal debugger can get a hold of it if
144 * it needs to.
146 DEBUG_argv0 = argv[0];
148 /* Create the initial process */
149 if (!PROCESS_Init()) return FALSE;
151 /* Parse command-line */
152 if (!MAIN_WineInit( &argc, argv )) return 1;
153 MAIN_argc = argc; MAIN_argv = argv;
155 /* Set up debugger hook */
156 INSTR_IsRelay = is_relay_addr;
157 EXC_SetDebugEventHook( wine_debugger );
159 if (Options.debug)
160 TASK_AddTaskEntryBreakpoint = DEBUG_AddTaskEntryBreakpoint;
162 /* Initialize everything */
163 if (!MAIN_EmulatorInit()) return 1;
165 /* Load kernel modules */
166 if (!LoadLibrary16( "KERNEL" )) return 1;
167 if (!LoadLibraryA( "KERNEL32" )) return 1;
169 /* Create initial task */
170 if ( !(pModule = NE_GetPtr( GetModuleHandle16( "KERNEL" ) )) ) return 1;
171 if ( !TASK_Create( pModule, 0, 0, FALSE ) ) return 1;
173 /* Switch to initial task */
174 PostEvent16( PROCESS_Current()->task );
175 TASK_Reschedule();
177 /* Switch stacks and jump to MAIN_EmulatorRun */
178 CALL32_Init( &IF1632_CallLargeStack, MAIN_EmulatorRun, NtCurrentTeb()->stack_top );
180 MSG( "main: Should never happen: returned from CALL32_Init()\n" );
181 return 0;