2 Copyright © 2006-2012, The AROS Development Team. All rights reserved.
5 Desc: IconX WB script starter
9 /******************************************************************************
26 Starts DOS scripts from Workbench. In order to use it you need an icon for
27 your script. Set 'IconX' as default tool.
31 FILE - The script filename to execute.
33 Tooltypes for script icon:
34 WINDOW -- Specification of the shell window
35 default: con:0/50//120/IconX/AUTO/WAIT/CLOSE
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
57 ******************************************************************************/
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>
66 #include <proto/alib.h>
68 #include <workbench/startup.h>
74 /* some default values */
75 #define DEFWINDOW "con:0/50//120/IconX/AUTO/WAIT/CLOSE"
76 #define DEFSTACK (40960)
77 #define DEFWAIT (2 * 50) // two seconds
78 #define DEFUSHELL (TRUE)
80 #define BASECOMMAND "EXECUTE "
82 const TEXT version
[] = "\0$VER: IconX 41.4 (2.8.2014)";
83 int __forceerrorrequester
= 1;
84 static TEXT errbuffer
[255];
87 void displayMsg(LONG code
)
91 Fault(code
, "IconX", errbuffer
, sizeof(errbuffer
));
92 struct EasyStruct es
= {sizeof(struct EasyStruct
), 0,
93 "Error", errbuffer
, "OK"};
94 EasyRequest(0, &es
, 0);
98 STRPTR
AllocateNameFromLock(BPTR lock
)
101 STRPTR buffer
= NULL
;
106 if (buffer
!= NULL
) FreeVec(buffer
);
108 buffer
= AllocVec(length
, MEMF_ANY
);
111 if (NameFromLock(lock
, buffer
, length
))
118 if (IoErr() == ERROR_LINE_TOO_LONG
)
131 SetIoErr(ERROR_NO_FREE_STORE
);
142 if (buffer
!= NULL
) FreeVec(buffer
);
149 STRPTR
BuildCommandLine(struct WBStartup
*startup
)
151 const struct WBStartup
*wbsstate
= startup
;
152 STRPTR buffer
= NULL
;
153 ULONG length
= 2 /* NULL + '\n' */ + strlen(BASECOMMAND
);
156 /*-- Calculate length of resulting string ------------------------------*/
157 for (i
= 1 ; i
< wbsstate
->sm_NumArgs
; i
++)
159 BPTR lock
= Lock((STRPTR
) wbsstate
->sm_ArgList
[i
].wa_Name
, ACCESS_READ
);
162 BPTR cd
= CurrentDir(lock
);
163 STRPTR path
= AllocateNameFromLock(lock
);
166 length
+= 3 /* space + 2 '"' */ + strlen(path
);
174 /*-- Allocate space for command line string ----------------------------*/
175 buffer
= AllocVec(length
, MEMF_ANY
);
176 D(bug("[IconX] buffer length: %d\n", length
));
180 /*-- Build command line --------------------------------------------*/
181 strcpy(buffer
, BASECOMMAND
);
183 for (i
= 1 ; i
< wbsstate
->sm_NumArgs
; i
++)
185 BPTR lock
= Lock((STRPTR
) wbsstate
->sm_ArgList
[i
].wa_Name
, ACCESS_READ
);
188 BPTR cd
= CurrentDir(lock
);
189 STRPTR path
= AllocateNameFromLock(lock
);
192 strcat(buffer
, " \"");
193 strcat(buffer
, path
);
194 strcat(buffer
, "\"");
201 strcat(buffer
, "\n");
205 SetIoErr(ERROR_NO_FREE_STORE
);
212 int main(int argc
, char **argv
)
214 LONG rc
= RETURN_FAIL
;
217 ixWindow
= DEFWINDOW
;
220 BOOL ixUShell
= DEFUSHELL
;
221 BPTR oldlock
= (BPTR
)-1,
224 struct DiskObject
*dobj
= NULL
;
226 D(bug("IconX argc %d\n", argc
));
230 displayMsg(ERROR_REQUIRED_ARG_MISSING
);
234 struct WBStartup
*startup
= (struct WBStartup
*) argv
;
235 if (startup
->sm_NumArgs
< 2)
237 displayMsg(ERROR_REQUIRED_ARG_MISSING
);
241 D(bug("[IconX] startup->sm_NumArgs: %d\n", startup
->sm_NumArgs
));
243 dirlock
= startup
->sm_ArgList
[1].wa_Lock
;
244 filename
= startup
->sm_ArgList
[1].wa_Name
;
246 oldlock
= CurrentDir(dirlock
);
248 /* query diskobject for tooltypes */
249 dobj
= GetDiskObject(filename
);
252 struct EasyStruct es
= {sizeof(struct EasyStruct
), 0,
253 "Error", "IconX\nGetDiskObject failed for:\n%s", "OK"};
254 EasyRequest(0, &es
, 0, filename
);
258 if (dobj
->do_Type
== WBPROJECT
)
260 const STRPTR
*toolarray
= (const STRPTR
*)dobj
->do_ToolTypes
;
262 if ((s
= FindToolType(toolarray
, "WINDOW")))
266 if ((s
= FindToolType(toolarray
, "STACK")))
270 if ((s
= FindToolType(toolarray
, "USERSHELL")))
272 if (MatchToolValue(s
, "NO"))
277 if ((s
= FindToolType(toolarray
, "WAIT")))
279 ixWait
+= atol(s
) * 50;
281 if ((s
= FindToolType(toolarray
, "DELAY")))
288 displayMsg(ERROR_OBJECT_WRONG_TYPE
);
298 D(bug("wait %d stack %d usershell %d window %s\n", ixWait
, ixStack
, ixUShell
, ixWindow
));
300 D(bug("Building command line\n"));
301 commandLine
= BuildCommandLine(startup
);
302 if (commandLine
== NULL
)
307 D(bug("[IconX] commandLine: '%s'\n", commandLine
));
309 window
= Open(ixWindow
, MODE_OLDFILE
);
312 /* try to open default window */
313 window
= Open(DEFWINDOW
, MODE_OLDFILE
);
318 D(bug("[IconX] window ok\n"));
319 struct TagItem tags
[] =
321 { SYS_Asynch
, FALSE
},
322 { SYS_Input
, (IPTR
)window
},
323 { SYS_Output
, (IPTR
)NULL
},
324 { SYS_Error
, (IPTR
)NULL
},
325 { SYS_UserShell
, ixUShell
},
326 { NP_StackSize
, ixStack
},
330 rc
= SystemTagList(commandLine
, tags
);
348 FreeDiskObject(dobj
);
350 if (oldlock
!= (BPTR
)-1)