mark scripts as executable
[AROS.git] / compiler / autoinit / stdiowin.c
blob43f217c0f338e23cd0d47ab94eaa5609c0b13d34
1 /*
2 Copyright © 2009-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: autoinit library - open IO window when started from WB
6 */
7 #include <dos/dos.h>
8 #include <aros/startup.h>
9 #include <dos/stdio.h>
10 #include <proto/dos.h>
12 #define DEBUG 0
13 #include <aros/debug.h>
15 /* programmers can define the __stdiowin for opening the win that will be used for
16 IO to the standard file descriptors.
17 If none is provided a default value will be used
19 extern char const __stdiowin[];
21 int __nostdiowin __attribute__((weak)) = 0;
23 static BPTR __iowinr = BNULL, __iowinw = BNULL, __iowine = BNULL;
24 static BPTR __old_in, __old_out, __old_err;
26 static BPTR DupFH(BPTR fh, LONG mode, struct DosLibrary * DOSBase)
28 BPTR nfh;
29 struct MsgPort *old;
31 if (!fh)
32 return BNULL;
33 old = SetConsoleTask(((struct FileHandle*)BADDR(fh))->fh_Type);
34 nfh = Open("*", mode);
35 SetConsoleTask(old);
36 return nfh;
39 static void __startup_stdiowin(struct ExecBase *SysBase)
41 struct Process *me;
43 D(bug("[__startup_stdiowin] Entering\n"));
45 if (!WBenchMsg)
47 __startup_entries_next();
48 return;
51 D(bug("[__startup_stdiowin] Opening console window: '%s'\n", __stdiowin));
53 __iowinw = Open(__stdiowin, MODE_OLDFILE);
54 __iowinr = DupFH(__iowinw, MODE_OLDFILE, DOSBase);
55 if (__iowinr) {
56 /* this is so ugly but ReadArgs() needs EOF or
57 * console window will always open and program
58 * pauses until RETURN is pressed.
60 struct FileHandle *fh = BADDR(__iowinr);
61 SetVBuf(__iowinr, NULL, BUF_LINE, 1);
62 /* force EOF */
63 fh->fh_Pos = fh->fh_End + 1;
65 __iowine = DupFH(__iowinw, MODE_OLDFILE, DOSBase);
67 if (!__iowinr || !__iowinw || !__iowine)
69 Close(__iowinr);
70 Close(__iowine);
71 Close(__iowinw);
72 D(bug("[__startup_stdiowin] Failed!\n"));
73 return;
76 D(bug("[__startup_stdiowin] Setting standard file handles\n"));
77 D(bug("[__startup_stdiowin] in %p out %p err %p\n", __iowinr, __iowinw, __iowine));
79 __old_in = SelectInput(__iowinr);
80 __old_out = SelectOutput(__iowinw);
81 me = (struct Process *)FindTask(NULL);
82 __old_err = me->pr_CES;
83 me->pr_CES = __iowine;
85 D(bug("[__startup_stdiowin] old in %p out %p err %p\n", __old_in, __old_out, __old_err));
87 __startup_entries_next();
89 D(bug("[__startup_stdiowin] Program executed\n"));
91 SelectInput(__old_in);
92 SelectOutput(__old_out);
93 me->pr_CES = __old_err;
95 Close(__iowinr);
96 Close(__iowine);
97 Close(__iowinw);
99 D(bug("[__startup_stdiowin] Done!\n"));
102 ADD2SET(__startup_stdiowin, PROGRAM_ENTRIES, -20);