2 Copyright © 2003-2011, The AROS Development Team. All rights reserved.
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>
24 #include "executecommand.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
41 BOOL ecd_UnlockParent
;
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
,
55 BOOL unlockParent
= FALSE
;
56 CONST_STRPTR initial
= NULL
;
57 BOOL freeInitial
= FALSE
;
58 BOOL saveCommand
= FALSE
;
65 /* Parse initial taglist -----------------------------------------------*/
66 while ((tag
= NextTagItem(&tstate
)) != NULL
)
70 case MUIA_ExecuteCommand_Parent
:
71 parent
= (BPTR
) tag
->ti_Data
;
74 case MUIA_ExecuteCommand_Initial
:
75 initial
= (CONST_STRPTR
) tag
->ti_Data
;
80 /* Setup parameters ----------------------------------------------------*/
83 if ((parent
= Lock("RAM:", ACCESS_READ
)) != BNULL
)
91 if ((initial
= GetENV("LastExecutedCommand")) != NULL
)
103 /* Create application and window objects -------------------------------*/
104 self
= (Object
*) DoSuperNewTags
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
,
127 MUIA_Popstring_Button
, (IPTR
) PopButton(MUII_PopFile
),
130 Child
, (IPTR
) HGroup
,
134 Child
, (IPTR
) HVSpace
,
135 Child
, (IPTR
) HGroup
,
136 MUIA_Group_SameSize
, TRUE
,
139 Child
, (IPTR
)(executeButton
= ImageButton(_(MSG_BUTTON_EXECUTE
),"THEME:Images/Gadgets/OK")),
140 Child
, (IPTR
)(cancelButton
= ImageButton(_(MSG_BUTTON_CANCEL
),"THEME:Images/Gadgets/Cancel")),
149 /* Not needed anymore */
150 if (freeInitial
) FreeVec((APTR
) initial
);
152 /* Check if object creation succeeded */
155 if (unlockParent
) UnLock(parent
);
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 -------------------------------------------------*/
172 window
, MUIM_Notify
, MUIA_Window_CloseRequest
, TRUE
,
173 (IPTR
) self
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
178 cancelButton
, MUIM_Notify
, MUIA_Pressed
, FALSE
,
179 (IPTR
) self
, 2, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
184 commandString
, MUIM_Notify
, MUIA_String_Acknowledge
, MUIV_EveryTime
,
185 (IPTR
) self
, 2, MUIM_ExecuteCommand_ExecuteCommand
, TRUE
190 executeButton
, MUIM_Notify
, MUIA_Pressed
, FALSE
,
191 (IPTR
) self
, 2, MUIM_ExecuteCommand_ExecuteCommand
, TRUE
196 commandPop
, MUIM_Notify
, MUIA_Popasl_Active
, FALSE
,
197 (IPTR
) window
, 3, MUIM_Set
, MUIA_Window_ActiveObject
, (IPTR
) commandString
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
);
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
);
248 self
, MUIM_Application_PushMethod
, (IPTR
) self
, 2,
249 MUIM_ExecuteCommand_ExecuteCommand
, FALSE
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')
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
);
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
;
292 WBCTRLA_DuplicateSearchPath
, (IPTR
) &searchPath
,
296 if (data
->ecd_Parent
!= BNULL
) cd
= CurrentDir(data
->ecd_Parent
);
305 SYS_Input
, (IPTR
) console
,
306 SYS_Output
, (IPTR
) NULL
,
307 SYS_Error
, (IPTR
) NULL
,
308 NP_Path
, (IPTR
) searchPath
,
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).
322 self
, data
->ecd_Window
,
323 _(MSG_ERROR_EXECUTE
), TRUE
329 WBCTRLA_FreeSearchPath
, (IPTR
) searchPath
,
336 if (cd
!= BNULL
) CurrentDir(cd
);
342 self
, data
->ecd_Window
,
343 _(MSG_ERROR_OPEN_CONSOLE
), TRUE
348 DoMethod(self
, MUIM_Application_ReturnID
, MUIV_Application_ReturnID_Quit
);
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
);
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
);