grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / system / Wanderer / Tools / WBRename / main.c
blob1c592a934c9ce506f767f8390adceea794665480
1 /*
2 Copyright © 2006-2011, 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 <proto/alib.h>
12 #include <proto/exec.h>
13 #include <proto/intuition.h>
14 #include <proto/muimaster.h>
15 #include <proto/workbench.h>
16 #include <proto/dos.h>
17 #include <proto/icon.h>
19 #include <dos/dos.h>
20 #include <libraries/mui.h>
21 #include <workbench/startup.h>
23 #include "locale.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 char versionstring[] = "$VER: WBRename 0.4 (6.4.2011) ©2006-2011 AROS Dev Team";
31 static STRPTR AllocateNameFromLock(BPTR lock);
32 static void bt_ok_hook_function(void);
33 static void Cleanup(STRPTR s);
34 static BOOL doRename(const STRPTR oldname, const STRPTR newname);
35 static void MakeGUI(void);
37 static Object *app, *window, *bt_ok, *bt_cancel, *str_name;
38 static struct Hook bt_ok_hook;
39 static BPTR parentlock = (BPTR)-1;
40 static STRPTR oldname;
41 static TEXT str_line[256];
42 static BPTR oldlock = (BPTR)-1;
43 static STRPTR illegal_chars = "/:";
46 int main(int argc, char **argv)
48 struct WBStartup *startup;
49 STRPTR fullname;
51 if (argc != 0)
53 PutStr(_(MSG_WB_ONLY));
54 Cleanup(NULL);
56 startup = (struct WBStartup *) argv;
58 D(bug("[WBRename] Args %d\n", startup->sm_NumArgs));
60 if (startup->sm_NumArgs != 2)
61 Cleanup(_(MSG_NEEDS_MORE_ARGS));
63 parentlock = startup->sm_ArgList[1].wa_Lock;
64 oldname = startup->sm_ArgList[1].wa_Name;
65 if ((parentlock == BNULL) || (oldname == NULL))
66 Cleanup(_(MSG_INVALID_LOCK));
68 oldlock = CurrentDir(parentlock);
70 MakeGUI();
71 DoMethod(app, MUIM_Application_Execute);
73 fullname = AllocateNameFromLock(parentlock);
74 UpdateWorkbenchObject(fullname, WBDRAWER, TAG_DONE);
75 FreeVec(fullname);
77 Cleanup(NULL);
78 return RETURN_OK;
82 static void MakeGUI(void)
84 bt_ok_hook.h_Entry = (APTR)bt_ok_hook_function;
85 sprintf(str_line, _(MSG_LINE), oldname);
86 app = (Object *)ApplicationObject,
87 MUIA_Application_Title , __(MSG_TITLE),
88 MUIA_Application_Version , (IPTR) versionstring,
89 MUIA_Application_Copyright , __(MSG_COPYRIGHT),
90 MUIA_Application_Author , (IPTR) "The AROS Development Team",
91 MUIA_Application_Description, __(MSG_DESCRIPTION),
92 MUIA_Application_Base , (IPTR) "WBRENAME",
93 MUIA_Application_UseCommodities, FALSE,
94 MUIA_Application_UseRexx, FALSE,
95 SubWindow, (IPTR)(window = (Object *)WindowObject,
96 MUIA_Window_Title, __(MSG_WINDOW_TITLE),
97 MUIA_Window_ID, MAKE_ID('W','B','R','N'),
98 MUIA_Window_Width, MUIV_Window_Width_Visible(33), // Set width at least as 33% of visible screen
99 MUIA_Window_NoMenus, TRUE,
100 MUIA_Window_CloseGadget, FALSE,
101 WindowContents, (IPTR) (VGroup,
102 MUIA_Frame, MUIV_Frame_Group,
103 Child, (IPTR) (HGroup,
104 Child, (IPTR) HVSpace,
105 Child, (IPTR) TextObject,
106 MUIA_Text_PreParse, (IPTR) "\33r",
107 MUIA_Text_Contents, (IPTR) str_line,
108 End,
109 End),
110 Child, (IPTR) (HGroup,
111 Child, (IPTR) Label2(__(MSG_NAME)),
112 Child, (IPTR)(str_name = (Object *)StringObject,
113 MUIA_CycleChain, 1,
114 MUIA_String_Contents, (IPTR) oldname,
115 MUIA_String_MaxLen, MAXFILENAMELENGTH,
116 MUIA_String_Reject, (IPTR) illegal_chars, // Doesn't work :-/
117 MUIA_String_Columns, -1,
118 MUIA_Frame, MUIV_Frame_String,
119 End),
120 End),
121 Child, (IPTR) (RectangleObject,
122 MUIA_Rectangle_HBar, TRUE,
123 MUIA_FixHeight, 2,
124 End),
125 Child, (IPTR) (HGroup,
126 Child, (IPTR) (bt_ok = ImageButton(__(MSG_OK), "THEME:Images/Gadgets/OK")),
127 Child, (IPTR) (bt_cancel = ImageButton(__(MSG_CANCEL),"THEME:Images/Gadgets/Cancel")),
128 End),
129 End),
130 End),
131 End;
133 if (!app)
134 Cleanup(_(MSG_FAILED_CREATE_APP));
136 DoMethod(window, MUIM_Notify, MUIA_Window_CloseRequest, TRUE,
137 app, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
138 DoMethod(bt_cancel, MUIM_Notify, MUIA_Pressed, FALSE,
139 app, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
140 DoMethod(bt_ok, MUIM_Notify, MUIA_Pressed, FALSE,
141 app, 2, MUIM_CallHook, (IPTR)&bt_ok_hook);
142 set(window, MUIA_Window_Open, TRUE);
143 set(window, MUIA_Window_ActiveObject, str_name);
144 set(window, MUIA_Window_DefaultObject, bt_ok);
148 static void bt_ok_hook_function(void)
150 STRPTR newname = (STRPTR)XGET(str_name, MUIA_String_Contents);
151 D(bug("WBRename oldname %s newname %s \n", oldname, newname));
153 if (doRename(oldname, newname))
155 DoMethod(app, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
157 else
159 set(window, MUIA_Window_Activate, TRUE);
160 set(window, MUIA_Window_ActiveObject, str_name);
166 static BOOL doRename(const STRPTR oldname, const STRPTR newname)
168 BOOL retval = FALSE;
169 if (( oldname == NULL) || (newname == NULL))
170 return retval;
172 STRPTR oldinfoname = NULL;
173 STRPTR newinfoname = NULL;
174 BOOL infoexists=FALSE;
175 BPTR test;
177 oldinfoname = AllocVec(strlen(oldname) + 6, MEMF_ANY);
178 if (!oldinfoname)
180 MUI_Request(app, window, 0, _(MSG_ERROR_TITLE), _(MSG_OK), _(MSG_OUTOFMEMORY));
181 goto end;
183 strcpy(oldinfoname, oldname);
184 strcat(oldinfoname, ".info");
186 newinfoname = AllocVec(strlen(newname) + 6, MEMF_ANY);
187 if (!newinfoname)
189 MUI_Request(app, window, 0, _(MSG_ERROR_TITLE), _(MSG_OK), _(MSG_OUTOFMEMORY));
190 goto end;
192 strcpy(newinfoname, newname);
193 strcat(newinfoname, ".info");
195 if (strpbrk(newname, illegal_chars))
197 MUI_Request(app, window, 0, _(MSG_ERROR_TITLE), _(MSG_OK), _(MSG_ILLEGAL_CHARS), newname);
198 goto end;
201 if ((test = Lock(newname, ACCESS_READ)))
203 UnLock(test);
204 MUI_Request(app, window, 0, _(MSG_ERROR_TITLE), _(MSG_OK), _(MSG_ALREADY_EXIST), newname);
205 goto end;
208 if ((test = Lock(oldinfoname, ACCESS_READ)))
210 UnLock(test);
211 infoexists = TRUE; // we have an .info file
212 test = Lock(newinfoname, ACCESS_READ);
213 if (test)
215 UnLock(test);
216 MUI_Request(app, window, 0, _(MSG_ERROR_TITLE), _(MSG_OK), _(MSG_ALREADY_EXIST), newinfoname);
217 goto end;
221 if (Rename(oldname, newname) == DOSFALSE)
223 MUI_Request(app, window, 0, _(MSG_ERROR_TITLE), _(MSG_OK), _(MSG_FAILED), oldname, GetDosErrorString(IoErr()));
224 goto end;
227 if (infoexists)
229 if ( Rename(oldinfoname, newinfoname) == DOSFALSE)
231 MUI_Request(app, window, 0, _(MSG_ERROR_TITLE), _(MSG_OK), _(MSG_FAILED), oldinfoname, GetDosErrorString(IoErr()));
232 goto end;
236 retval = TRUE;
238 end:
239 FreeVec(oldinfoname);
240 FreeVec(newinfoname);
241 return retval;
245 static STRPTR AllocateNameFromLock(BPTR lock)
247 ULONG length = 512;
248 STRPTR buffer = NULL;
249 BOOL done = FALSE;
251 while (!done)
253 FreeVec(buffer);
255 buffer = AllocVec(length, MEMF_ANY);
256 if (buffer != NULL)
258 if (NameFromLock(lock, buffer, length))
260 done = TRUE;
261 break;
263 else
265 if (IoErr() == ERROR_LINE_TOO_LONG)
267 length += 512;
268 continue;
270 else
272 break;
276 else
278 SetIoErr(ERROR_NO_FREE_STORE);
279 break;
283 if (done)
285 return buffer;
287 else
289 FreeVec(buffer);
290 return NULL;
295 static void Cleanup(STRPTR s)
297 MUI_DisposeObject(app);
299 if (oldlock != (BPTR)-1)
300 CurrentDir(oldlock);
302 if (s)
304 if (IntuitionBase)
306 struct EasyStruct es;
307 es.es_StructSize = sizeof(struct EasyStruct);
308 es.es_Flags = 0;
309 es.es_Title = _(MSG_ERROR_TITLE);
310 es.es_TextFormat = s;
311 es.es_GadgetFormat = _(MSG_OK);
312 EasyRequest(NULL, &es, NULL, NULL);
314 else
316 PutStr(s);
318 exit(RETURN_ERROR);
320 exit(RETURN_OK);