Some compiler warnings removed.
[cake.git] / workbench / system / Wanderer / Tools / ExecuteCommand / executecommand.c
blob74691a62595c9657377a9ab1e5301724ef4de3f3
1 /*
2 Copyright © 2003, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define MUIMASTER_YES_INLINE_STDARG
8 #define DEBUG 1
9 #include <aros/debug.h>
11 #include <exec/types.h>
12 #include <utility/tagitem.h>
13 #include <libraries/mui.h>
14 #include <libraries/asl.h>
16 #include <proto/exec.h>
17 #include <proto/dos.h>
18 #include <proto/intuition.h>
19 #include <proto/utility.h>
20 #include <proto/muimaster.h>
21 #include <proto/workbench.h>
23 #include <stdio.h>
25 #include "executecommand.h"
26 #include "locale.h"
27 #include "support.h"
29 #define VERSION "$VER: Execute 0.2 (13.01.2006) ©AROS Dev Team"
31 /*** Private methods ********************************************************/
32 #define MUIM_ExecuteCommand_ExecuteCommand (TAG_USER | 0x20000000)
33 struct MUIP_ExecuteCommand_ExecuteCommand { STACKULONG MethodID; STACKULONG delayed; };
35 /*** Instance data **********************************************************/
36 struct ExecuteCommand_DATA
38 Object *ecd_Window,
39 *ecd_CommandString,
40 *ecd_CommandPop;
41 BPTR ecd_Parent;
42 BOOL ecd_UnlockParent;
43 BOOL ecd_SaveCommand;
46 /*** Methods ****************************************************************/
47 Object *ExecuteCommand__OM_NEW
49 Class *CLASS, Object *self, struct opSet *message
52 struct ExecuteCommand_DATA *data = NULL;
53 const struct TagItem *tstate = message->ops_AttrList,
54 *tag = NULL;
55 BPTR parent = NULL;
56 BOOL unlockParent = FALSE;
57 CONST_STRPTR initial = NULL;
58 BOOL freeInitial = FALSE;
59 BOOL saveCommand = FALSE;
60 Object *window,
61 *commandString,
62 *commandPop,
63 *executeButton,
64 *cancelButton;
66 /* Parse initial taglist -----------------------------------------------*/
67 while ((tag = NextTagItem(&tstate)) != NULL)
69 switch (tag->ti_Tag)
71 case MUIA_ExecuteCommand_Parent:
72 parent = (BPTR) tag->ti_Data;
73 break;
75 case MUIA_ExecuteCommand_Initial:
76 initial = (CONST_STRPTR) tag->ti_Data;
77 break;
81 /* Setup parameters ----------------------------------------------------*/
82 if (parent == NULL)
84 if ((parent = Lock("RAM:", ACCESS_READ)) != NULL)
86 unlockParent = TRUE;
90 if (initial == NULL)
92 if ((initial = GetENV("LastExecutedCommand")) != NULL)
94 freeInitial = TRUE;
96 else
98 initial = "";
101 saveCommand = TRUE;
104 /* Create application and window objects -------------------------------*/
105 self = (Object *) DoSuperNewTags
107 CLASS, self, NULL,
109 MUIA_Application_Title, __(MSG_TITLE),
110 MUIA_Application_Version, (IPTR) VERSION,
111 MUIA_Application_Copyright, (IPTR)"© 2006, The AROS Development Team",
112 MUIA_Application_Description, __(MSG_DESCRIPTION),
113 SubWindow, (IPTR) (window = (Object *)WindowObject,
114 MUIA_Window_Title, __(MSG_DESCRIPTION),
115 MUIA_Window_Activate, TRUE,
116 MUIA_Window_NoMenus, TRUE,
117 MUIA_Window_CloseGadget, FALSE,
118 MUIA_Window_ID, MAKE_ID('E','C','W','N'),
119 WindowContents, (IPTR) VGroup,
120 Child, (IPTR) VGroup,
121 Child, (IPTR) LLabel(_(MSG_LABEL_COMMANDLINE)),
122 Child, (IPTR) (commandPop = (Object *)PopaslObject,
123 MUIA_Popstring_String, (IPTR) (commandString = (Object *)StringObject,
124 MUIA_String_Contents, (IPTR) initial,
125 MUIA_CycleChain, 1,
126 StringFrame,
127 End),
128 MUIA_Popstring_Button, (IPTR) PopButton(MUII_PopFile),
129 End),
130 End,
131 Child, (IPTR) HGroup,
132 InnerSpacing(0, 0),
133 GroupSpacing(0),
135 Child, (IPTR) HVSpace,
136 Child, (IPTR) HGroup,
137 MUIA_Group_SameSize, TRUE,
138 MUIA_Weight, 0,
140 Child, (IPTR)(executeButton = ImageButton(_(MSG_BUTTON_EXECUTE),"THEME:Images/Gadgets/Prefs/Save")),
141 Child, (IPTR)(cancelButton = ImageButton(_(MSG_BUTTON_CANCEL),"THEME:Images/Gadgets/Prefs/Cancel")),
142 End,
143 End,
144 End,
145 End),
147 TAG_DONE
150 /* Not needed anymore */
151 if (freeInitial) FreeVec((APTR) initial);
153 /* Check if object creation succeeded */
154 if (self == NULL)
156 if (unlockParent) UnLock(parent);
158 return NULL;
161 /* Store instance data -------------------------------------------------*/
162 data = INST_DATA(CLASS, self);
163 data->ecd_Window = window;
164 data->ecd_CommandString = commandString;
165 data->ecd_CommandPop = commandPop;
166 data->ecd_Parent = parent;
167 data->ecd_UnlockParent = unlockParent;
168 data->ecd_SaveCommand = saveCommand;
170 /* Setup notifications -------------------------------------------------*/
171 DoMethod
173 window, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
174 (IPTR) self, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
177 DoMethod
179 cancelButton, MUIM_Notify, MUIA_Pressed, FALSE,
180 (IPTR) self, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit
183 DoMethod
185 commandString, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime,
186 (IPTR) self, 2, MUIM_ExecuteCommand_ExecuteCommand, TRUE
189 DoMethod
191 executeButton, MUIM_Notify, MUIA_Pressed, FALSE,
192 (IPTR) self, 2, MUIM_ExecuteCommand_ExecuteCommand, TRUE
195 DoMethod
197 commandPop, MUIM_Notify, MUIA_Popasl_Active, FALSE,
198 (IPTR) window, 3, MUIM_Set, MUIA_Window_ActiveObject, (IPTR) commandString
201 return self;
204 IPTR ExecuteCommand__OM_DISPOSE(Class *CLASS, Object *self, Msg message)
206 struct ExecuteCommand_DATA *data = INST_DATA(CLASS, self);
208 if (data->ecd_UnlockParent && data->ecd_Parent != NULL)
210 UnLock(data->ecd_Parent);
213 return DoSuperMethodA(CLASS, self, message);
216 IPTR ExecuteCommand__MUIM_Application_Execute
218 Class *CLASS, Object *self, Msg message
221 struct ExecuteCommand_DATA *data = INST_DATA(CLASS, self);
223 SET(data->ecd_Window, MUIA_Window_Open, TRUE);
224 SET(data->ecd_Window, MUIA_Window_ActiveObject, (IPTR) data->ecd_CommandString);
226 DoSuperMethodA(CLASS, self, message);
228 SET(data->ecd_Window, MUIA_Window_Open, FALSE);
230 return 0;
233 IPTR ExecuteCommand__MUIM_ExecuteCommand_ExecuteCommand
235 Class *CLASS, Object *self,
236 struct MUIP_ExecuteCommand_ExecuteCommand *message
239 struct ExecuteCommand_DATA *data = INST_DATA(CLASS, self);
241 if (message->delayed)
243 if (!XGET(data->ecd_CommandPop, MUIA_Popasl_Active))
245 SET(data->ecd_Window, MUIA_Window_Open, FALSE);
247 DoMethod
249 self, MUIM_Application_PushMethod, (IPTR) self, 2,
250 MUIM_ExecuteCommand_ExecuteCommand, FALSE
254 else
256 BPTR console, cd = NULL;
257 STRPTR command;
259 GET(data->ecd_CommandString, MUIA_String_Contents, &command);
261 if (data->ecd_SaveCommand) SetENV("LastExecutedCommand", command);
263 /* Make sure that the commandline isn't just whitespace or NULL */
264 if (command != NULL && command[strspn(command, " \t")] != '\0')
266 TEXT buffer[1024];
270 sizeof(buffer) > snprintf
272 buffer, sizeof(buffer),
273 "CON:////%s/CLOSE/AUTO/WAIT", _(MSG_CONSOLE_TITLE)
277 /* The string was not truncated */
278 console = Open(buffer, MODE_OLDFILE);
280 else
282 /* The string was truncated; fallback to english title */
283 console = Open("CON:////Command Output/CLOSE/AUTO/WAIT", MODE_OLDFILE);
286 if (console != NULL)
288 BPTR searchPath = NULL;
290 WorkbenchControl
292 NULL,
293 WBCTRLA_DuplicateSearchPath, (IPTR) &searchPath,
294 TAG_DONE
297 if (data->ecd_Parent != NULL) cd = CurrentDir(data->ecd_Parent);
301 SystemTags
303 command,
305 SYS_Asynch, TRUE,
306 SYS_Input, (IPTR) console,
307 SYS_Output, (IPTR) NULL,
308 SYS_Error, (IPTR) NULL,
309 SYS_Background, FALSE,
310 NP_Path, (IPTR) searchPath,
312 TAG_DONE
313 ) == -1
317 An error occured, so we need to close the filehandle
318 and free the search path list (which are otherwise
319 automatically freed when the process exits).
322 ShowError
324 self, data->ecd_Window,
325 _(MSG_ERROR_EXECUTE), TRUE
328 WorkbenchControl
330 NULL,
331 WBCTRLA_FreeSearchPath, (IPTR) searchPath,
332 TAG_DONE
335 Close(console);
338 if (cd != NULL) CurrentDir(cd);
340 else
342 ShowError
344 self, data->ecd_Window,
345 _(MSG_ERROR_OPEN_CONSOLE), TRUE
350 DoMethod(self, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
353 return 0;
357 /*** Dispatcher *************************************************************/
358 BOOPSI_DISPATCHER(IPTR, ExecuteCommand_Dispatcher, CLASS, self, message)
360 switch (message->MethodID)
362 case OM_NEW: return (IPTR) ExecuteCommand__OM_NEW(CLASS, self, (struct opSet *) message);
363 case OM_DISPOSE: return ExecuteCommand__OM_DISPOSE(CLASS, self, message);
364 case MUIM_Application_Execute: return ExecuteCommand__MUIM_Application_Execute(CLASS, self, message);
365 case MUIM_ExecuteCommand_ExecuteCommand: return ExecuteCommand__MUIM_ExecuteCommand_ExecuteCommand(CLASS, self, (struct MUIP_ExecuteCommand_ExecuteCommand *) message);
366 default: return DoSuperMethodA(CLASS, self, message);
369 return 0;
371 BOOPSI_DISPATCHER_END
373 /*** Setup ******************************************************************/
374 struct MUI_CustomClass *ExecuteCommand_CLASS;
376 BOOL ExecuteCommand_Initialize()
378 ExecuteCommand_CLASS = MUI_CreateCustomClass
380 NULL, MUIC_Application, NULL,
381 sizeof(struct ExecuteCommand_DATA), ExecuteCommand_Dispatcher
384 return ExecuteCommand_CLASS != NULL ? TRUE : FALSE;
387 VOID ExecuteCommand_Deinitialize()
389 if (ExecuteCommand_CLASS != NULL)
391 MUI_DeleteCustomClass(ExecuteCommand_CLASS);