- Made pciuhci.device and pciehci.device compile again: completed
[AROS.git] / compiler / clib / __arosc_startup.c
blob46d468b5920c997a0c8f17414fc2a445ede798e0
1 /*
2 Copyright © 2009-2012, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: arosc library - support code for entering and leaving a program
6 Lang: english
7 */
8 #include <dos/stdio.h>
9 #include <exec/alerts.h>
10 #include <proto/exec.h>
11 #include <proto/dos.h>
13 #include <assert.h>
14 #include <setjmp.h>
16 #define DEBUG 0
17 #include <aros/debug.h>
19 #include "__arosc_privdata.h"
20 #include "__exitfunc.h"
22 /*****************************************************************************
24 NAME */
25 void __arosc_program_startup(
27 /* SYNOPSIS */
28 jmp_buf exitjmp,
29 int *errorptr)
31 /* FUNCTION
32 This is called during program startup and before calling main.
33 This is to allow arosc.library to do some initialization that couldn't
34 be done when opening the library.
36 INPUTS
37 exitjmp - jmp_buf to jump to to exit the program
38 errorptr - pointer to store return value of program
40 RESULT
43 NOTES
44 This function is normally called by the startup code so one
45 should not need to do it oneself.
47 EXAMPLE
49 BUGS
51 SEE ALSO
53 INTERNALS
55 ******************************************************************************/
57 struct aroscbase *aroscbase = __aros_getbase_aroscbase();
58 struct Process *me = (struct Process *)FindTask(NULL);
60 D(bug("[__arosc_program_startup] aroscbase 0x%p\n", aroscbase));
62 aroscbase->acb_startup_error_ptr = errorptr;
63 *aroscbase->acb_exit_jmp_buf = *exitjmp;
65 /* A some C error IO routines evidently rely on this, and
66 * should be fixed!
68 if (me->pr_Task.tc_Node.ln_Type == NT_PROCESS &&
69 me->pr_CES != BNULL)
71 SetVBuf(me->pr_CES, NULL, BUF_NONE, -1);
75 /*****************************************************************************
77 NAME */
78 void __arosc_program_end(
80 /* SYNOPSIS */
81 void)
83 /* FUNCTION
84 This function is to be called when main() has returned or after
85 program has exited. This allows to arosc.library to do some
86 cleanup that can't be done during closing of the library.
88 INPUTS
91 RESULT
94 NOTES
95 This function is normally called by the startup code so one
96 should not need to do it oneself.
98 EXAMPLE
100 BUGS
102 SEE ALSO
104 INTERNALS
107 ******************************************************************************/
109 struct aroscbase *aroscbase = __aros_getbase_aroscbase();
110 D(bug("[__arosc_program_end]\n"));
112 if (!(aroscbase->acb_flags & ABNORMAL_EXIT))
113 __callexitfuncs();
116 /*****************************************************************************
118 NAME */
119 int *__arosc_set_errorptr(
121 /* SYNOPSIS */
122 int *errorptr)
124 /* FUNCTION
125 This function sets the pointer to store error return value for
126 program exit.
128 INPUTS
129 errorptr - new pointer to return value
131 RESULT
132 old pointer to return value
134 NOTES
136 EXAMPLE
138 BUGS
140 SEE ALSO
142 INTERNALS
144 ******************************************************************************/
146 struct aroscbase *aroscbase = __aros_getbase_aroscbase();
147 int *old = aroscbase->acb_startup_error_ptr;
149 aroscbase->acb_startup_error_ptr = errorptr;
151 return old;
154 /*****************************************************************************
156 NAME */
157 int *__arosc_get_errorptr(
159 /* SYNOPSIS */
160 void)
162 /* FUNCTION
163 This function gets the pointer to store error return value for
164 program exit.
166 INPUTS
169 RESULT
170 pointer to return value
172 NOTES
174 EXAMPLE
176 BUGS
178 SEE ALSO
180 INTERNALS
182 ******************************************************************************/
184 struct aroscbase *aroscbase = __aros_getbase_aroscbase();
185 return aroscbase->acb_startup_error_ptr;
188 /*****************************************************************************
190 NAME */
191 void __arosc_set_exitjmp(
193 /* SYNOPSIS */
194 jmp_buf exitjmp,
195 jmp_buf previousjmp)
197 /* FUNCTION
198 This function set the jmp_buf to use for directly exiting current
199 program.
201 INPUTS
202 exitjmp - new jmp_buf for exiting
204 RESULT
205 previous jmp_buf for exiting
207 NOTES
209 EXAMPLE
211 BUGS
213 SEE ALSO
215 INTERNALS
217 ******************************************************************************/
219 struct aroscbase *aroscbase = __aros_getbase_aroscbase();
221 *previousjmp = *aroscbase->acb_exit_jmp_buf;
222 *aroscbase->acb_exit_jmp_buf = *exitjmp;
225 /*****************************************************************************
227 NAME */
228 void __arosc_jmp2exit(
230 /* SYNOPSIS */
231 int normal,
232 int retcode)
234 /* FUNCTION
235 This function directly jumps to the exit of a program.
237 INPUTS
238 normal - Indicates if exit is normal or not. When it is abnormal no
239 atexit functions will be called.
240 retcode - the return code for the program.
242 RESULT
245 NOTES
246 In normal operation this function does not return.
247 If this function returns it means that this function was called in a
248 context where jmp_buf for exit was not initialized. Likely cause is
249 a module that opened arosstdc.library.
250 Be sure to capture this situation.
252 EXAMPLE
254 BUGS
256 SEE ALSO
258 INTERNALS
260 ******************************************************************************/
262 struct aroscbase *aroscbase = __aros_getbase_aroscbase();
264 /* No __arosc_progam_startup() called; Alert()
266 if (aroscbase->acb_startup_error_ptr == NULL)
268 kprintf("[__arosc_jmp2exit] Trying to exit without proper initialization\n");
269 Alert(AT_DeadEnd | AG_BadParm);
272 if (!normal)
273 aroscbase->acb_flags |= ABNORMAL_EXIT;
275 *aroscbase->acb_startup_error_ptr = retcode;
277 longjmp(aroscbase->acb_exit_jmp_buf, 1);
279 assert(0); /* Not reached */