Switch stacks in CALL32_Init().
[wine] / miscemu / main.c
1 /*
2  * Emulator initialisation code
3  *
4  */
5
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 "debug.h"
21
22 static int MAIN_argc;
23 static char **MAIN_argv;
24
25 /***********************************************************************
26  *           Emulator initialisation
27  */
28 BOOL MAIN_EmulatorInit(void)
29 {
30     /* Main initialization */
31     if (!MAIN_MainInit()) return FALSE;
32
33     /* Initialize relay code */
34     if (!RELAY_Init()) return FALSE;
35
36     /* Create the Win16 printer driver */
37     if (!WIN16DRV_Init()) return FALSE;
38
39     return TRUE;
40 }
41
42
43 /***********************************************************************
44  *           Main loop of initial task
45  */
46 void MAIN_EmulatorRun( void )
47 {
48     extern void THUNK_InitCallout( void );
49     char startProg[256], defProg[256];
50     HINSTANCE handle;
51     int i, tasks = 0;
52     MSG msg;
53
54     /* Load system DLLs into the initial process (and initialize them) */
55     if (   !LoadLibrary16("GDI.EXE" ) || !LoadLibraryA("GDI32.DLL" )
56         || !LoadLibrary16("USER.EXE") || !LoadLibraryA("USER32.DLL"))
57         ExitProcess( 1 );
58
59     /* Get pointers to USER routines called by KERNEL */
60     THUNK_InitCallout();
61
62     /* Call InitApp for initial task */
63     Callout.InitApp16( MapHModuleLS( 0 ) );
64
65     /* Add the Default Program if no program on the command line */
66     if (!MAIN_argv[1])
67     {
68         PROFILE_GetWineIniString( "programs", "Default", "",
69                                   defProg, sizeof(defProg) );
70         if (defProg[0]) MAIN_argv[MAIN_argc++] = defProg;
71     }
72     
73     /* Add the Startup Program to the run list */
74     PROFILE_GetWineIniString( "programs", "Startup", "", 
75                                startProg, sizeof(startProg) );
76     if (startProg[0]) MAIN_argv[MAIN_argc++] = startProg;
77
78     /* Abort if no executable on command line */
79     if (MAIN_argc <= 1) 
80     {
81         MAIN_Usage(MAIN_argv[0]);
82         ExitProcess( 1 );
83     }
84
85     /* Load and run executables given on command line */
86     for (i = 1; i < MAIN_argc; i++)
87     {
88         if ((handle = WinExec( MAIN_argv[i], SW_SHOWNORMAL )) < 32)
89         {
90             MSG("wine: can't exec '%s': ", MAIN_argv[i]);
91             switch (handle)
92             {
93             case 2: MSG("file not found\n" ); break;
94             case 11: MSG("invalid exe file\n" ); break;
95             default: MSG("error=%d\n", handle ); break;
96             }
97         }
98         else tasks++;
99     }
100
101     if (!tasks)
102     {
103         MSG("wine: no executable file found.\n" );
104         ExitProcess( 0 );
105     }
106
107     /* Start message loop for desktop window */
108
109     while ( GetNumTasks16() > 1  && Callout.GetMessageA( &msg, 0, 0, 0 ) )
110     {
111         Callout.TranslateMessage( &msg );
112         Callout.DispatchMessageA( &msg );
113     }
114
115     ExitProcess( 0 );
116 }
117
118
119 /**********************************************************************
120  *           main
121  */
122 int main( int argc, char *argv[] )
123 {
124     NE_MODULE *pModule;
125     extern char * DEBUG_argv0;
126
127     __winelib = 0;  /* First of all, clear the Winelib flag */
128
129     /*
130      * Save this so that the internal debugger can get a hold of it if
131      * it needs to.
132      */
133     DEBUG_argv0 = argv[0];
134
135     /* Create the initial process */
136     if (!PROCESS_Init()) return FALSE;
137
138     /* Parse command-line */
139     if (!MAIN_WineInit( &argc, argv )) return 1;
140     MAIN_argc = argc; MAIN_argv = argv;
141
142     /* Set up debugger/instruction emulation callback routines */
143     ctx_debug_call              = ctx_debug;
144     fnWINE_Debugger             = wine_debug;
145     fnINSTR_EmulateInstruction  = INSTR_EmulateInstruction;
146
147     if (Options.debug) 
148         TASK_AddTaskEntryBreakpoint = DEBUG_AddTaskEntryBreakpoint;
149
150     /* Initialize everything */
151     if (!MAIN_EmulatorInit()) return 1;
152
153     /* Load kernel modules */
154     if (!LoadLibrary16(  "KERNEL" )) return 1;
155     if (!LoadLibraryA( "KERNEL32" )) return 1;
156
157     /* Create initial task */
158     if ( !(pModule = NE_GetPtr( GetModuleHandle16( "KERNEL" ) )) ) return 1;
159     if ( !TASK_Create( THREAD_Current(), pModule, 0, 0, FALSE ) ) return 1;
160
161     /* Switch to initial task */
162     PostEvent16( PROCESS_Current()->task );
163     TASK_Reschedule();
164
165     /* Switch stacks and jump to MAIN_EmulatorRun */
166     CALL32_Init( &IF1632_CallLargeStack, MAIN_EmulatorRun,
167                  THREAD_Current()->teb.stack_top );
168
169     MSG( "main: Should never happen: returned from CALL32_Init()\n" );
170     return 0;
171 }