2 Copyright © 2003, The AROS Development Team. All rights reserved.
6 #define MUIMASTER_YES_INLINE_STDARG
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>
25 #include "executecommand.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
42 BOOL ecd_UnlockParent
;
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
,
56 BOOL unlockParent
= FALSE
;
57 CONST_STRPTR initial
= NULL
;
58 BOOL freeInitial
= FALSE
;
59 BOOL saveCommand
= FALSE
;
66 /* Parse initial taglist -----------------------------------------------*/
67 while ((tag
= NextTagItem(&tstate
)) != NULL
)
71 case MUIA_ExecuteCommand_Parent
:
72 parent
= (BPTR
) tag
->ti_Data
;
75 case MUIA_ExecuteCommand_Initial
:
76 initial
= (CONST_STRPTR
) tag
->ti_Data
;
81 /* Setup parameters ----------------------------------------------------*/
84 if ((parent
= Lock("RAM:", ACCESS_READ
)) != NULL
)
92 if ((initial
= GetENV("LastExecutedCommand")) != NULL
)
104 /* Create application and window objects -------------------------------*/
105 self
= (Object
*) DoSuperNewTags
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
,
128 MUIA_Popstring_Button
, (IPTR
) PopButton(MUII_PopFile
),
131 Child
, (IPTR
) HGroup
,
135 Child
, (IPTR
) HVSpace
,
136 Child
, (IPTR
) HGroup
,
137 MUIA_Group_SameSize
, TRUE
,
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")),
150 /* Not needed anymore */
151 if (freeInitial
) FreeVec((APTR
) initial
);
153 /* Check if object creation succeeded */
156 if (unlockParent
) UnLock(parent
);
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 -------------------------------------------------*/
173 window
, MUIM_Notify
, MUIA_Window_CloseRequest
, TRUE
,
174 (IPTR
) self
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
179 cancelButton
, MUIM_Notify
, MUIA_Pressed
, FALSE
,
180 (IPTR
) self
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
185 commandString
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
,
186 (IPTR
) self
, 2, MUIM_ExecuteCommand_ExecuteCommand
, TRUE
191 executeButton
, MUIM_Notify
, MUIA_Pressed
, FALSE
,
192 (IPTR
) self
, 2, MUIM_ExecuteCommand_ExecuteCommand
, TRUE
197 commandPop
, MUIM_Notify
, MUIA_Popasl_Active
, FALSE
,
198 (IPTR
) window
, 3, MUIM_Set
, MUIA_Window_ActiveObject
, (IPTR
) commandString
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
);
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
);
249 self
, MUIM_Application_PushMethod
, (IPTR
) self
, 2,
250 MUIM_ExecuteCommand_ExecuteCommand
, FALSE
256 BPTR console
, cd
= NULL
;
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')
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
);
282 /* The string was truncated; fallback to english title */
283 console
= Open("CON:////Command Output/CLOSE/AUTO/WAIT", MODE_OLDFILE
);
288 BPTR searchPath
= NULL
;
293 WBCTRLA_DuplicateSearchPath
, (IPTR
) &searchPath
,
297 if (data
->ecd_Parent
!= NULL
) cd
= CurrentDir(data
->ecd_Parent
);
306 SYS_Input
, (IPTR
) console
,
307 SYS_Output
, (IPTR
) NULL
,
308 SYS_Error
, (IPTR
) NULL
,
309 SYS_Background
, FALSE
,
310 NP_Path
, (IPTR
) searchPath
,
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).
324 self
, data
->ecd_Window
,
325 _(MSG_ERROR_EXECUTE
), TRUE
331 WBCTRLA_FreeSearchPath
, (IPTR
) searchPath
,
338 if (cd
!= NULL
) CurrentDir(cd
);
344 self
, data
->ecd_Window
,
345 _(MSG_ERROR_OPEN_CONSOLE
), TRUE
350 DoMethod(self
, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
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
);
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
);