Don't call ReadArgs() if started from WB.
[tangerine.git] / compiler / startup / startup.c
blob5be9ddaebf341119ed78c111be7e4e5fe2066964
2 /*
3 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
4 $Id$
6 Desc: Common startup code
7 Lang: english
8 */
9 #include <aros/config.h>
10 #include <setjmp.h>
11 #include <dos/dos.h>
12 #include <exec/memory.h>
13 #include <workbench/startup.h>
14 #include <proto/exec.h>
15 #include <proto/dos.h>
16 #include <aros/asmcall.h>
17 #include <aros/debug.h>
18 #include <aros/symbolsets.h>
19 #include <aros/startup.h>
21 #include "etask.h"
23 THIS_PROGRAM_HANDLES_SYMBOLSETS
25 /* Don't define symbols before the entry point. */
26 extern struct ExecBase *SysBase;
27 extern struct WBStartup *WBenchMsg;
28 extern int main(int argc, char ** argv);
29 int (*__main_function_ptr)(int argc, char ** argv) __attribute__((__weak__)) = main;
31 DECLARESET(INIT);
32 DECLARESET(EXIT);
33 DECLARESET(CTORS);
34 DECLARESET(DTORS);
35 DECLARESET(PROGRAM_ENTRIES);
38 This won't work for normal AmigaOS because you can't expect SysBase to be
39 in A6. The correct way is to use *(struct ExecBase **)4 and because GCC
40 emits strings for a certain function _before_ the code the program will
41 crash immediately because the first element in the code won't be valid
42 assembler code.
45 extern char *__argstr;
46 extern ULONG __argsize;
47 extern char **__argv;
48 extern int __argc;
49 extern struct DosLibrary *DOSBase;
51 static struct aros_startup __aros_startup;
53 #warning TODO: reset and initialize the FPU
54 #warning TODO: resident startup
55 AROS_UFH3(static LONG, __startup_entry,
56 AROS_UFHA(char *,argstr,A0),
57 AROS_UFHA(ULONG,argsize,D0),
58 AROS_UFHA(struct ExecBase *,sysbase,A6)
61 AROS_USERFUNC_INIT
63 struct Process *myproc;
65 SysBase = sysbase;
68 No one program will be able to do anything useful without the dos.library,
69 so we open it here instead of using the automatic opening system
71 DOSBase = (struct DosLibrary *)OpenLibrary(DOSNAME, 39);
72 if (!DOSBase) return RETURN_FAIL;
74 __argstr = argstr;
75 __argsize = argsize;
77 myproc = (struct Process *)FindTask(NULL);
79 GetIntETask(myproc)->iet_startup = &__aros_startup;
81 /* Do we have a CLI structure? */
82 if (!myproc->pr_CLI)
84 /* Workbench startup. Get WBenchMsg and pass it to main() */
86 WaitPort(&myproc->pr_MsgPort);
87 WBenchMsg = (struct WBStartup *)GetMsg(&myproc->pr_MsgPort);
88 __argv = (char **) WBenchMsg;
89 __argc = 0;
92 __aros_startup.as_startup_error = RETURN_FAIL;
94 if (set_open_libraries())
98 setjmp(__aros_startup.as_startup_jmp_buf) == 0 &&
99 set_call_funcs(SETNAME(INIT), 1, 1)
102 /* ctors/dtors get called in inverse order than init funcs */
103 set_call_funcs(SETNAME(CTORS), -1, 0);
105 /* Invoke the main function. A weak symbol is used as function name so that
106 it can be overridden (for *nix stuff, for instance). */
107 __aros_startup.as_startup_error = (*__main_function_ptr) (__argc, __argv);
109 set_call_funcs(SETNAME(DTORS), 1, 0);
111 set_call_funcs(SETNAME(EXIT), -1, 0);
113 set_close_libraries();
116 /* Reply startup message to Workbench */
117 if (WBenchMsg)
119 Forbid(); /* make sure we're not UnLoadseg()ed before we're really done */
120 ReplyMsg((struct Message *) WBenchMsg);
123 CloseLibrary((struct Library *)DOSBase);
125 return __aros_startup.as_startup_error;
127 AROS_USERFUNC_EXIT
128 } /* entry */
130 /* if the programmer hasn't defined a symbol with the name __nocommandline
131 then the code to handle the commandline will be included from the autoinit.lib
133 extern int __nocommandline;
134 asm(".set __importcommandline, __nocommandline");
136 /* pass these values to the command line handling function */
137 char *__argstr;
138 ULONG __argsize;
140 /* the command line handling functions will pass these values back to us */
141 char **__argv;
142 int __argc;
144 struct ExecBase *SysBase;
145 struct DosLibrary *DOSBase;
146 struct WBStartup *WBenchMsg;
148 DEFINESET(CTORS);
149 DEFINESET(DTORS);
150 DEFINESET(INIT);
151 DEFINESET(EXIT);
152 DEFINESET(PROGRAM_ENTRIES);
153 ADD2SET(__startup_entry, program_entries, 0);
157 Stub function for GCC __main().
159 The __main() function is originally used for C++ style constructors
160 and destructors in C. This replacement does nothing and gets rid of
161 linker-errors about references to __main().
163 #ifdef AROS_NEEDS___MAIN
164 void __main(void)
166 /* Do nothing. */
168 #endif