2 Copyright © 2009-2012, The AROS Development Team. All rights reserved.
5 Desc: arosc library - support code for entering and leaving a program
9 #include <exec/alerts.h>
10 #include <proto/exec.h>
11 #include <proto/dos.h>
17 #include <aros/debug.h>
19 #include "__arosc_privdata.h"
20 #include "__exitfunc.h"
22 /*****************************************************************************
25 void __arosc_program_startup(
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.
37 exitjmp - jmp_buf to jump to to exit the program
38 errorptr - pointer to store return value of program
44 This function is normally called by the startup code so one
45 should not need to do it oneself.
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
68 if (me
->pr_Task
.tc_Node
.ln_Type
== NT_PROCESS
&&
71 SetVBuf(me
->pr_CES
, NULL
, BUF_NONE
, -1);
75 /*****************************************************************************
78 void __arosc_program_end(
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.
95 This function is normally called by the startup code so one
96 should not need to do it oneself.
107 ******************************************************************************/
109 struct aroscbase
*aroscbase
= __aros_getbase_aroscbase();
110 D(bug("[__arosc_program_end]\n"));
112 if (!(aroscbase
->acb_flags
& ABNORMAL_EXIT
))
116 /*****************************************************************************
119 int *__arosc_set_errorptr(
125 This function sets the pointer to store error return value for
129 errorptr - new pointer to return value
132 old pointer to return value
144 ******************************************************************************/
146 struct aroscbase
*aroscbase
= __aros_getbase_aroscbase();
147 int *old
= aroscbase
->acb_startup_error_ptr
;
149 aroscbase
->acb_startup_error_ptr
= errorptr
;
154 /*****************************************************************************
157 int *__arosc_get_errorptr(
163 This function gets the pointer to store error return value for
170 pointer to return value
182 ******************************************************************************/
184 struct aroscbase
*aroscbase
= __aros_getbase_aroscbase();
185 return aroscbase
->acb_startup_error_ptr
;
188 /*****************************************************************************
191 void __arosc_set_exitjmp(
198 This function set the jmp_buf to use for directly exiting current
202 exitjmp - new jmp_buf for exiting
205 previous jmp_buf for exiting
217 ******************************************************************************/
219 struct aroscbase
*aroscbase
= __aros_getbase_aroscbase();
221 *previousjmp
= *aroscbase
->acb_exit_jmp_buf
;
222 *aroscbase
->acb_exit_jmp_buf
= *exitjmp
;
225 /*****************************************************************************
228 void __arosc_jmp2exit(
235 This function directly jumps to the exit of a program.
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.
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.
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
);
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 */