grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / system / Wanderer / Tools / ExecuteCommand / executecommand.c
bloba5595cb26f3547a18efce2965d3f93c6717447a4
1 /*
2 Copyright © 2003-2011, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define MUIMASTER_YES_INLINE_STDARG
8 #include <aros/debug.h>
9 #include <exec/types.h>
10 #include <utility/tagitem.h>
11 #include <libraries/mui.h>
12 #include <libraries/asl.h>
14 #include <proto/alib.h>
15 #include <proto/exec.h>
16 #include <proto/dos.h>
17 #include <proto/intuition.h>
18 #include <proto/utility.h>
19 #include <proto/muimaster.h>
20 #include <proto/workbench.h>
22 #include <stdio.h>
24 #include "executecommand.h"
25 #include "locale.h"
26 #include "support.h"
28 #define VERSION "$VER: Execute 0.2 (13.01.2006) ©AROS Dev Team"
30 /*** Private methods ********************************************************/
31 #define MUIM_ExecuteCommand_ExecuteCommand (TAG_USER | 0x20000000)
32 struct MUIP_ExecuteCommand_ExecuteCommand { STACKULONG MethodID; STACKULONG delayed; };
34 /*** Instance data **********************************************************/
35 struct ExecuteCommand_DATA
37 Object *ecd_Window,
38 *ecd_CommandString,
39 *ecd_CommandPop;
40 BPTR ecd_Parent;
41 BOOL ecd_UnlockParent;
42 BOOL ecd_SaveCommand;
45 /*** Methods ****************************************************************/
46 Object *ExecuteCommand__OM_NEW
48 Class *CLASS, Object *self, struct opSet *message
51 struct ExecuteCommand_DATA *data = NULL;
52 struct TagItem *tstate = message->ops_AttrList,
53 *tag = NULL;
54 BPTR parent = BNULL;
55 BOOL unlockParent = FALSE;
56 CONST_STRPTR initial = NULL;
57 BOOL freeInitial = FALSE;
58 BOOL saveCommand = FALSE;
59 Object *window,
60 *commandString,
61 *commandPop,
62 *executeButton,
63 *cancelButton;
65 /* Parse initial taglist -----------------------------------------------*/
66 while ((tag = NextTagItem(&tstate)) != NULL)
68 switch (tag->ti_Tag)
70 case MUIA_ExecuteCommand_Parent:
71 parent = (BPTR) tag->ti_Data;
72 break;
74 case MUIA_ExecuteCommand_Initial:
75 initial = (CONST_STRPTR) tag->ti_Data;
76 break;
80 /* Setup parameters ----------------------------------------------------*/
81 if (parent == BNULL)
83 if ((parent = Lock("RAM:", ACCESS_READ)) != BNULL)
85 unlockParent = TRUE;
89 if (initial == NULL)
91 if ((initial = GetENV("LastExecutedCommand")) != NULL)
93 freeInitial = TRUE;
95 else
97 initial = "";
100 saveCommand = TRUE;
103 /* Create application and window objects -------------------------------*/
104 self = (Object *) DoSuperNewTags
106 CLASS, self, NULL,
108 MUIA_Application_Title, __(MSG_TITLE),
109 MUIA_Application_Version, (IPTR) VERSION,
110 MUIA_Application_Copyright, (IPTR)"© 2006, The AROS Development Team",
111 MUIA_Application_Description, __(MSG_DESCRIPTION),
112 SubWindow, (IPTR) (window = (Object *)WindowObject,
113 MUIA_Window_Title, __(MSG_DESCRIPTION),
114 MUIA_Window_Activate, TRUE,
115 MUIA_Window_NoMenus, TRUE,
116 MUIA_Window_CloseGadget, FALSE,
117 MUIA_Window_ID, MAKE_ID('E','C','W','N'),
118 WindowContents, (IPTR) VGroup,
119 Child, (IPTR) VGroup,
120 Child, (IPTR) LLabel(_(MSG_LABEL_COMMANDLINE)),
121 Child, (IPTR) (commandPop = (Object *)PopaslObject,
122 MUIA_Popstring_String, (IPTR) (commandString = (Object *)StringObject,
123 MUIA_String_Contents, (IPTR) initial,
124 MUIA_CycleChain, 1,
125 StringFrame,
126 End),
127 MUIA_Popstring_Button, (IPTR) PopButton(MUII_PopFile),
128 End),
129 End,
130 Child, (IPTR) HGroup,
131 InnerSpacing(0, 0),
132 GroupSpacing(0),
134 Child, (IPTR) HVSpace,
135 Child, (IPTR) HGroup,
136 MUIA_Group_SameSize, TRUE,
137 MUIA_Weight, 0,
139 Child, (IPTR)(executeButton = ImageButton(_(MSG_BUTTON_EXECUTE),"THEME:Images/Gadgets/OK")),
140 Child, (IPTR)(cancelButton = ImageButton(_(MSG_BUTTON_CANCEL),"THEME:Images/Gadgets/Cancel")),
141 End,
142 End,
143 End,
144 End),
146 TAG_DONE
149 /* Not needed anymore */
150 if (freeInitial) FreeVec((APTR) initial);
152 /* Check if object creation succeeded */
153 if (self == NULL)
155 if (unlockParent) UnLock(parent);
157 return NULL;
160 /* Store instance data -------------------------------------------------*/
161 data = INST_DATA(CLASS, self);
162 data->ecd_Window = window;
163 data->ecd_CommandString = commandString;
164 data->ecd_CommandPop = commandPop;
165 data->ecd_Parent = parent;
166 data->ecd_UnlockParent = unlockParent;
167 data->ecd_SaveCommand = saveCommand;
169 /* Setup notifications -------------------------------------------------*/
170 DoMethod
172 window, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
173 (IPTR) self, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
176 DoMethod
178 cancelButton, MUIM_Notify, MUIA_Pressed, FALSE,
179 (IPTR) self, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
182 DoMethod
184 commandString, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
185 (IPTR) self, 2, MUIM_ExecuteCommand_ExecuteCommand, TRUE
188 DoMethod
190 executeButton, MUIM_Notify, MUIA_Pressed, FALSE,
191 (IPTR) self, 2, MUIM_ExecuteCommand_ExecuteCommand, TRUE
194 DoMethod
196 commandPop, MUIM_Notify, MUIA_Popasl_Active, FALSE,
197 (IPTR) window, 3, MUIM_Set, MUIA_Window_ActiveObject, (IPTR) commandString
200 return self;
203 IPTR ExecuteCommand__OM_DISPOSE(Class *CLASS, Object *self, Msg message)
205 struct ExecuteCommand_DATA *data = INST_DATA(CLASS, self);
207 if (data->ecd_UnlockParent && data->ecd_Parent != BNULL)
209 UnLock(data->ecd_Parent);
212 return DoSuperMethodA(CLASS, self, message);
215 IPTR ExecuteCommand__MUIM_Application_Execute
217 Class *CLASS, Object *self, Msg message
220 struct ExecuteCommand_DATA *data = INST_DATA(CLASS, self);
222 SET(data->ecd_Window, MUIA_Window_Open, TRUE);
223 SET(data->ecd_Window, MUIA_Window_ActiveObject, (IPTR) data->ecd_CommandString);
225 DoSuperMethodA(CLASS, self, message);
227 SET(data->ecd_Window, MUIA_Window_Open, FALSE);
229 return 0;
232 IPTR ExecuteCommand__MUIM_ExecuteCommand_ExecuteCommand
234 Class *CLASS, Object *self,
235 struct MUIP_ExecuteCommand_ExecuteCommand *message
238 struct ExecuteCommand_DATA *data = INST_DATA(CLASS, self);
240 if (message->delayed)
242 if (!XGET(data->ecd_CommandPop, MUIA_Popasl_Active))
244 SET(data->ecd_Window, MUIA_Window_Open, FALSE);
246 DoMethod
248 self, MUIM_Application_PushMethod, (IPTR) self, 2,
249 MUIM_ExecuteCommand_ExecuteCommand, FALSE
253 else
255 BPTR console, cd = BNULL;
256 STRPTR command = NULL;
258 GET(data->ecd_CommandString, MUIA_String_Contents, &command);
260 if (data->ecd_SaveCommand) SetENV("LastExecutedCommand", command);
262 /* Make sure that the commandline isn't just whitespace or NULL */
263 if (command != NULL && command[strspn(command, " \t")] != '\0')
265 TEXT buffer[1024];
269 sizeof(buffer) > snprintf
271 buffer, sizeof(buffer),
272 "CON:////%s/CLOSE/AUTO/WAIT", _(MSG_CONSOLE_TITLE)
276 /* The string was not truncated */
277 console = Open(buffer, MODE_OLDFILE);
279 else
281 /* The string was truncated; fallback to english title */
282 console = Open("CON:////Command Output/CLOSE/AUTO/WAIT", MODE_OLDFILE);
285 if (console != BNULL)
287 BPTR searchPath = BNULL;
289 WorkbenchControl
291 NULL,
292 WBCTRLA_DuplicateSearchPath, (IPTR) &searchPath,
293 TAG_DONE
296 if (data->ecd_Parent != BNULL) cd = CurrentDir(data->ecd_Parent);
300 SystemTags
302 command,
304 SYS_Asynch, TRUE,
305 SYS_Input, (IPTR) console,
306 SYS_Output, (IPTR) NULL,
307 SYS_Error, (IPTR) NULL,
308 NP_Path, (IPTR) searchPath,
310 TAG_DONE
311 ) == -1
315 An error occured, so we need to close the filehandle
316 and free the search path list (which are otherwise
317 automatically freed when the process exits).
320 ShowError
322 self, data->ecd_Window,
323 _(MSG_ERROR_EXECUTE), TRUE
326 WorkbenchControl
328 NULL,
329 WBCTRLA_FreeSearchPath, (IPTR) searchPath,
330 TAG_DONE
333 Close(console);
336 if (cd != BNULL) CurrentDir(cd);
338 else
340 ShowError
342 self, data->ecd_Window,
343 _(MSG_ERROR_OPEN_CONSOLE), TRUE
348 DoMethod(self, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
351 return 0;
355 /*** Dispatcher *************************************************************/
356 BOOPSI_DISPATCHER(IPTR, ExecuteCommand_Dispatcher, CLASS, self, message)
358 switch (message->MethodID)
360 case OM_NEW: return (IPTR) ExecuteCommand__OM_NEW(CLASS, self, (struct opSet *) message);
361 case OM_DISPOSE: return ExecuteCommand__OM_DISPOSE(CLASS, self, message);
362 case MUIM_Application_Execute: return ExecuteCommand__MUIM_Application_Execute(CLASS, self, message);
363 case MUIM_ExecuteCommand_ExecuteCommand: return ExecuteCommand__MUIM_ExecuteCommand_ExecuteCommand(CLASS, self, (struct MUIP_ExecuteCommand_ExecuteCommand *) message);
364 default: return DoSuperMethodA(CLASS, self, message);
367 return 0;
369 BOOPSI_DISPATCHER_END
371 /*** Setup ******************************************************************/
372 struct MUI_CustomClass *ExecuteCommand_CLASS;
374 BOOL ExecuteCommand_Initialize()
376 ExecuteCommand_CLASS = MUI_CreateCustomClass
378 NULL, MUIC_Application, NULL,
379 sizeof(struct ExecuteCommand_DATA), ExecuteCommand_Dispatcher
382 return ExecuteCommand_CLASS != NULL ? TRUE : FALSE;
385 VOID ExecuteCommand_Deinitialize()
387 if (ExecuteCommand_CLASS != NULL)
389 MUI_DeleteCustomClass(ExecuteCommand_CLASS);