added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / c / IconX.c
blob343a4b54f1f5dfb8fd707e0ccd4f0245c9859e18
1 /*
2 Copyright © 2006-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: IconX WB script starter
6 Lang: English
7 */
9 /******************************************************************************
12 NAME
14 IconX
16 SYNOPSIS
18 FILE/A
20 LOCATION
22 Sys:C
24 FUNCTION
26 Starts DOS scripts from Workbench. In order to use it you need an icon for
27 your script. Set 'IconX' as default tool.
29 INPUTS
31 FILE - The script filename to execute.
33 Tooltypes for script icon:
34 WINDOW -- Specification of the shell window
35 default: con:0/50//80/IconX/Auto
36 STACK=n -- default: 40960
37 USERSHELL=YES|NO -- default: YES
38 WAIT=n -- Wait n seconds before closing window (default 2)
39 DELAY=n -- Wait n/50 seconds before closing window
41 RESULT
43 NOTES
45 EXAMPLE
47 BUGS
49 SEE ALSO
51 Execute
53 INTERNALS
55 HISTORY
57 ******************************************************************************/
59 //#define DEBUG 1
61 #include <aros/debug.h>
62 #include <proto/exec.h>
63 #include <proto/dos.h>
64 #include <proto/intuition.h>
65 #include <proto/icon.h>
67 #include <workbench/startup.h>
69 #include <string.h>
70 #include <stdlib.h>
72 /* some default values */
73 #define DEFWINDOW "con:0/50//80/IconX/Auto"
74 #define DEFSTACK (40960)
75 #define DEFWAIT (2 * 50) // two seconds
76 #define DEFUSHELL (TRUE)
78 const TEXT version[] = "\0$VER: IconX 41.1 (8.12.2007)";
79 int __forceerrorrequester = 1;
80 static TEXT errbuffer[255];
83 void displayMsg(LONG code)
85 if (code)
87 Fault(code, "IconX", errbuffer, sizeof(errbuffer));
88 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
89 "Error", errbuffer, "OK"};
90 EasyRequest(0, &es, 0);
95 int main(int argc, char **argv)
97 LONG rc = RETURN_FAIL;
98 STRPTR filename;
99 BPTR oldlock = (BPTR)-1;
100 BPTR dirlock = (BPTR)-1;
101 struct DiskObject *dobj = NULL;
103 STRPTR ixWindow = DEFWINDOW;
104 LONG ixWait = 0;
105 LONG ixStack = DEFSTACK;
106 BOOL ixUShell = DEFUSHELL;
108 BPTR from = NULL;
109 BPTR window = NULL;
111 D(bug("IconX argc %d\n", argc));
113 if (argc != 0)
115 displayMsg(ERROR_REQUIRED_ARG_MISSING);
116 goto exit;
119 struct WBStartup *startup = (struct WBStartup *) argv;
120 if (startup->sm_NumArgs != 2)
122 displayMsg(ERROR_REQUIRED_ARG_MISSING);
123 goto exit;
126 dirlock = startup->sm_ArgList[1].wa_Lock;
127 filename = startup->sm_ArgList[1].wa_Name;
129 oldlock = CurrentDir(dirlock);
131 /* query diskobject for tooltypes */
132 dobj = GetDiskObject(filename);
133 if (dobj == NULL)
135 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
136 "Error", "IconX\nGetDiskObject failed for:\n%s", "OK"};
137 EasyRequest(0, &es, 0, filename);
138 goto exit;
141 if (dobj->do_Type == WBPROJECT)
143 const STRPTR *toolarray = (const STRPTR *)dobj->do_ToolTypes;
144 STRPTR s;
145 if ((s = FindToolType(toolarray, "WINDOW")))
147 ixWindow = s;
149 if ((s = FindToolType(toolarray, "STACK")))
151 ixStack = atol(s);
153 if ((s = FindToolType(toolarray, "USERSHELL")))
155 if (MatchToolValue(s, "NO"))
157 ixUShell = FALSE;
160 if ((s = FindToolType(toolarray, "WAIT")))
162 ixWait += atol(s) * 50;
164 if ((s = FindToolType(toolarray, "DELAY")))
166 ixWait += atol(s);
169 else
171 displayMsg(ERROR_OBJECT_WRONG_TYPE);
172 goto exit;
175 if (ixWait <= 0)
176 ixWait = DEFWAIT;
178 if (ixStack <= 4096)
179 ixStack = DEFSTACK;
181 D(bug("wait %d stack %d usershell %d window %s\n", ixWait, ixStack, ixUShell, ixWindow));
183 from = Open(filename, MODE_OLDFILE);
184 if (from == NULL)
186 displayMsg(IoErr());
187 goto exit;
190 window = Open(ixWindow, MODE_OLDFILE);
191 if (window == NULL)
193 /* try to open default window */
194 window = Open(DEFWINDOW, MODE_OLDFILE);
197 if (window)
199 struct TagItem tags[] =
201 { SYS_Asynch, FALSE },
202 { SYS_Background, TRUE },
203 { SYS_Input, (IPTR)window },
204 { SYS_Output, (IPTR)NULL },
205 { SYS_Error, (IPTR)NULL },
206 { SYS_ScriptInput, (IPTR)from },
207 { SYS_UserShell, ixUShell },
208 { NP_StackSize, ixStack },
209 { TAG_DONE, 0 }
212 rc = SystemTagList("", tags);
213 if (rc == -1)
215 displayMsg(IoErr());
216 rc = RETURN_FAIL;
219 else
221 displayMsg(IoErr());
222 goto exit;
225 Delay(ixWait);
226 rc = RETURN_OK;
228 exit:
229 Close(window);
230 FreeDiskObject(dobj);
232 if (oldlock != (BPTR)-1)
233 CurrentDir(oldlock);
235 return rc;