Added -dll option for winelib programs. (Note: will not print warnings
[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     /* Add the Default Program if no program on the command line */
63     if (!MAIN_argv[1])
64     {
65         PROFILE_GetWineIniString( "programs", "Default", "",
66                                   defProg, sizeof(defProg) );
67         if (defProg[0]) MAIN_argv[MAIN_argc++] = defProg;
68     }
69     
70     /* Add the Startup Program to the run list */
71     PROFILE_GetWineIniString( "programs", "Startup", "", 
72                                startProg, sizeof(startProg) );
73     if (startProg[0]) MAIN_argv[MAIN_argc++] = startProg;
74
75     /* Abort if no executable on command line */
76     if (MAIN_argc <= 1) 
77     {
78         MAIN_Usage(MAIN_argv[0]);
79         exit(1);
80     }
81
82     /* Load and run executables given on command line */
83     for (i = 1; i < MAIN_argc; i++)
84     {
85         if ((handle = WinExec( MAIN_argv[i], SW_SHOWNORMAL )) < 32)
86         {
87             MSG("wine: can't exec '%s': ", MAIN_argv[i]);
88             switch (handle)
89             {
90             case 2: MSG("file not found\n" ); break;
91             case 11: MSG("invalid exe file\n" ); break;
92             default: MSG("error=%d\n", handle ); break;
93             }
94         }
95         else tasks++;
96     }
97
98     if (!tasks)
99     {
100         MSG("wine: no executable file found.\n" );
101         ExitProcess( 0 );
102     }
103
104
105     /* Start message loop for desktop window */
106
107     while ( GetNumTasks16() > 1 && Callout.GetMessageA( &msg, 0, 0, 0 ) )
108     {
109         Callout.TranslateMessage( &msg );
110         Callout.DispatchMessageA( &msg );
111     }
112
113     ExitProcess( 0 );
114 }
115
116
117 /**********************************************************************
118  *           main
119  */
120 int main( int argc, char *argv[] )
121 {
122     NE_MODULE *pModule;
123     extern char * DEBUG_argv0;
124
125     __winelib = 0;  /* First of all, clear the Winelib flag */
126
127     /*
128      * Save this so that the internal debugger can get a hold of it if
129      * it needs to.
130      */
131     DEBUG_argv0 = argv[0];
132
133     /* Create the initial process */
134     if (!PROCESS_Init()) return FALSE;
135
136     /* Parse command-line */
137     if (!MAIN_WineInit( &argc, argv )) return 1;
138     MAIN_argc = argc; MAIN_argv = argv;
139
140     /* Handle -dll option (hack) */
141     if (Options.dllFlags)
142     {
143         /* If there are options left, or if the parser had errors, report it */
144         if (!BUILTIN_ParseDLLOptions( Options.dllFlags )||Options.dllFlags[0]) {
145             MSG("%s: Syntax: -dll +xxx,... or -dll -xxx,...\n",
146                      argv[0] );
147             BUILTIN_PrintDLLs();
148             exit(1);
149         }
150     }
151
152     /* Set up debugger/instruction emulation callback routines */
153     ctx_debug_call              = ctx_debug;
154     fnWINE_Debugger             = wine_debug;
155     fnINSTR_EmulateInstruction  = INSTR_EmulateInstruction;
156
157     if (Options.debug) 
158         TASK_AddTaskEntryBreakpoint = DEBUG_AddTaskEntryBreakpoint;
159
160     /* Initialize everything */
161     if (!MAIN_EmulatorInit()) return 1;
162
163     /* Load kernel modules */
164     if (!LoadLibrary16(  "KERNEL" )) return 1;
165     if (!LoadLibraryA( "KERNEL32" )) return 1;
166
167     /* Create initial task */
168     if ( !(pModule = NE_GetPtr( GetModuleHandle16( "KERNEL32" ) )) ) return 1;
169     if ( !TASK_Create( THREAD_Current(), pModule, 0, 0, FALSE ) ) return 1;
170
171     /* Initialize CALL32 routines */
172     /* This needs to be done just before switching stacks */
173     IF1632_CallLargeStack = (int (*)(int (*func)(), void *arg))CALL32_Init();
174
175     /* Switch to initial task */
176     CURRENT_STACK16->frame32->retaddr = (DWORD)MAIN_EmulatorRun;
177     TASK_StartTask( PROCESS_Current()->task );
178     MSG( "main: Should never happen: returned from TASK_StartTask()\n" );
179     return 0;
180 }