BCPL_GlobVec size fix.
[AROS.git] / compiler / startup / detach.c
blobc622d1d62ba4f696fb4e911ab5fbfe1e6a400294
1 /*
2 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Common startup code
6 Lang: english
8 Use: To make a program detach itself from the launching CLI, use
9 detach.o as your program's start file, before any other usual start
10 file.
12 If you want the detached process to have a name different than the
13 detacher's one, define the following variable in your program's
14 source code:
16 STRPTR __detached_name = "My Preferred Program Name";
18 You can also decide to control exactly when to detach your program
19 from the shell. To do so, simply use the function
21 Detach();
23 declared in
25 <aros/detach.h>
27 when you want to detach the program. Not using this function will
28 result in your program being detached from the shell before the main()
29 function is reached.
31 #define DEBUG 0
33 #include <aros/config.h>
34 #include <dos/dos.h>
35 #include <dos/stdio.h>
36 #include <proto/exec.h>
37 #include <proto/dos.h>
38 #include <aros/asmcall.h>
39 #include <aros/debug.h>
40 #include <aros/symbolsets.h>
41 #include <aros/startup.h>
43 int __detached_manages_detach;
44 int __detacher_go_away;
45 STRPTR __detached_name;
46 LONG __detached_return_value;
47 struct Process *__detacher_process;
49 AROS_UFP3S(LONG, __detach_trampoline,
50 AROS_UFPA(char *,argstr,A0),
51 AROS_UFPA(ULONG,argsize,D0),
52 AROS_UFPA(struct ExecBase *,SysBase,A6));
54 static void __startup_detach(void)
56 struct CommandLineInterface *cli;
57 struct Process *newproc;
58 BPTR mysegment = BNULL;
59 STRPTR detached_name;
61 D(bug("Entering __startup_detach()\n"));
63 cli = Cli();
64 /* Without a CLI detaching makes no sense, just jump to
65 the real program. */
66 if (!cli)
68 D(bug("Wasn't started from cli.\n"));
69 __startup_entries_next();
71 else
73 D(bug("Was started from cli.\n"));
74 mysegment = cli->cli_Module;
75 cli->cli_Module = BNULL;
76 BPTR in, out;
78 detached_name = __detached_name ? __detached_name : (STRPTR) FindTask(NULL)->tc_Node.ln_Name;
80 in = OpenFromLock(DupLockFromFH(Input()));
81 out = OpenFromLock(DupLockFromFH(Output()));
82 if (IsInteractive(in))
83 SetVBuf(in, NULL, BUF_LINE, -1);
84 if (IsInteractive(out))
85 SetVBuf(out, NULL, BUF_LINE, -1);
88 struct TagItem tags[] =
90 { NP_Seglist, (IPTR)mysegment },
91 { NP_Entry, (IPTR)&__detach_trampoline },
92 { NP_Name, (IPTR)detached_name },
93 { NP_Arguments, (IPTR)__argstr },
94 { NP_Input, (IPTR)in },
95 { NP_Output, (IPTR)out },
96 { NP_Cli, TRUE },
97 { TAG_DONE, 0 }
100 __detacher_process = (struct Process *)FindTask(NULL);
102 /* CreateNewProc() will take care of freeing the seglist */
103 newproc = CreateNewProc(tags);
106 D(bug("Process \"%s\" = 0x%x.\n", detached_name, newproc));
107 if (!newproc)
109 cli->cli_Module = mysegment;
110 __detached_return_value = RETURN_ERROR;
112 else
113 while (!__detacher_go_away) Wait(SIGF_SINGLE);
115 D(bug("__detached_return_value = %d.\n", __detached_return_value));
116 if (__detached_return_value != RETURN_OK)
118 PutStr(FindTask(NULL)->tc_Node.ln_Name); PutStr(": Failed to detach.\n");
121 if (newproc)
123 Forbid();
124 Signal(&newproc->pr_Task, SIGF_SINGLE);
127 __startup_error = __detached_return_value;
130 D(bug("Leaving __startup_detach\n"));
133 ADD2SET(__startup_detach, program_entries, -100);
135 void __Detach(LONG retval);
137 AROS_UFH3S(LONG, __detach_trampoline,
138 AROS_UFHA(char *,argstr,A0),
139 AROS_UFHA(ULONG,argsize,D0),
140 AROS_UFHA(struct ExecBase *,SysBase,A6))
142 AROS_USERFUNC_INIT
144 LONG retval = RETURN_OK;
146 D(bug("Entering __detach_trampoline()\n"));
148 /* We have a CLI that is not 'attached' to a Shell,
149 * and to get WB 1.3's C:Status to list us properly,
150 * we need to set cli_Module to something.
152 Cli()->cli_Module = (BPTR)-1;
154 /* The program has two options: either take care of telling the detacher
155 process when exactly to go away, via the Detach() function, or let this
156 startup code take care of it. If __detached_manages_detach is TRUE, then
157 the detached process handles it, otherwise we handle it. */
159 if (!__detached_manages_detach)
160 __Detach(RETURN_OK);
162 __startup_entries_next();
164 /* At this point the detacher process might still be around,
165 If the program forgot to detach, or if it couldn't, but in any
166 case we need to tell the detacher to go away. */
167 __Detach(retval);
169 D(bug("Leaving __detach_trampoline\n"));
171 return 0;
173 AROS_USERFUNC_EXIT
176 void __Detach(LONG retval)
178 D(bug("Entering __Detach(%d)\n", retval));
180 if (__detacher_process != NULL)
182 __detached_return_value = retval;
183 __detacher_go_away = TRUE;
185 SetSignal(0, SIGF_SINGLE);
186 /* Tell the detacher process it can now go away */
187 Signal(&__detacher_process->pr_Task, SIGF_SINGLE);
189 /* Wait for it to say "goodbye" */
190 Wait(SIGF_SINGLE);
191 __detacher_process = NULL;
193 Close(Input());
194 SelectInput(BNULL);
195 Close(Output());
196 SelectOutput(BNULL);
199 D(bug("Leaving __Detach\n"));