add casts to zune macros to silence some warnings
[tangerine.git] / workbench / system / Wanderer / Tools / ExecuteStartup / main.c
blob6c31c6b29f0152010a845d0bc26fede0c92b8c92
1 /*
2 Copyright © 2006-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Executes programs in sys:WBStartup
6 Lang: English
7 */
9 /*****************************************************************************
11 NAME
12 ExecuteStartup
14 LOCATION
15 Wanderer:Tools
17 FUNCTION
18 Executes programs in sys:WBStartup.
20 It checks the following tooltypes of the called programs:
21 STARTPRI=n n can be between +127 and -128. The program
22 with the highest priority is started first.
24 WAIT=n Wait n seconds after execution.
25 n can be between 0 and 60.
27 DONOTWAIT don't wait till the program is finished.
29 EXAMPLE
31 BUGS
32 All programs are treated like DONOTWAIT is set.
34 SEE ALSO
36 ******************************************************************************/
38 #define DEBUG 0
40 #include <string.h>
41 #include <stdlib.h>
43 #include <aros/debug.h>
44 #include <proto/exec.h>
45 #include <proto/dos.h>
46 #include <proto/icon.h>
47 #include <proto/workbench.h>
48 #include <proto/intuition.h>
50 #define STARTUPDIR "sys:WBStartup/"
52 struct InfoNode
54 struct Node node;
55 ULONG waittime;
56 BOOL donotwait;
59 const TEXT version_string[] = "$VER: ExecuteStartup 0.2 (20.10.2007) © AROS Dev Team";
61 static TEXT fileNameBuffer[MAXFILENAMELENGTH];
62 static APTR poolmem;
64 static BOOL checkIcon(STRPTR name, LONG *pri, ULONG *time, BOOL *notwait);
65 static BOOL searchInfo(struct List *infoList);
66 static void executePrograms(struct List *infolist);
67 static BOOL executeWBStartup(void);
69 int
70 main(void)
72 executeWBStartup();
73 return RETURN_OK;
76 static BOOL
77 checkIcon(STRPTR name, LONG *pri, ULONG *time, BOOL *notwait)
79 *pri = 0;
80 *time = 0;
81 *notwait = FALSE;
83 struct DiskObject *dobj = GetDiskObject(name);
84 if (dobj == NULL)
86 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
87 "Error", "ExecuteStartup\nGetDiskObject failed for:\n%s", "OK"};
88 EasyRequest(0, &es, 0, name);
89 return FALSE;
92 if ((dobj->do_Type == WBTOOL) || (dobj->do_Type == WBPROJECT))
94 const STRPTR *toolarray = (const STRPTR *)dobj->do_ToolTypes;
95 STRPTR s;
96 if ((s = FindToolType(toolarray, "STARTPRI")))
98 *pri = atol(s);
99 if (*pri < -128) *pri = -128;
100 if (*pri > 127) *pri = 127;
102 if ((s = FindToolType(toolarray, "WAIT")))
104 *time = atol(s);
105 if (*time > 60) *time = 60;
107 if ((s = FindToolType(toolarray, "DONOTWAIT")))
109 *notwait = TRUE;
111 FreeDiskObject(dobj);
112 return TRUE;
114 return FALSE;
117 static BOOL
118 searchInfo(struct List *infoList)
120 BOOL retvalue = TRUE;
121 LONG error;
122 struct AnchorPath *ap = AllocPooled(poolmem, sizeof(struct AnchorPath));
123 if (ap)
125 error = MatchFirst(STARTUPDIR "?#?.info", ap);
126 while (!error)
128 strcpy(fileNameBuffer, ap->ap_Info.fib_FileName);
129 BYTE * pointPos = strrchr(fileNameBuffer, '.');
130 *pointPos = '\0';
132 LONG pri;
133 ULONG time;
134 BOOL notwait;
135 if (checkIcon(fileNameBuffer, &pri, &time, &notwait))
137 struct InfoNode *newnode = AllocPooled(poolmem, sizeof (*newnode));
138 if (newnode == NULL)
140 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
141 "Error", "ExecuteStartup\nOut of memory for InfoNode", "OK"};
142 EasyRequest(0, &es, 0);
143 retvalue = FALSE;
144 goto exit;
146 newnode->node.ln_Name = AllocPooled(poolmem, strlen(fileNameBuffer) + 1);
147 if (newnode->node.ln_Name == NULL)
149 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
150 "Error", "ExecuteStartup\nOut of memory for ln_Name", "OK"};
151 EasyRequest(0, &es, 0);
152 retvalue = FALSE;
153 goto exit;
155 strcpy(newnode->node.ln_Name, fileNameBuffer);
156 newnode->node.ln_Pri = pri;
157 newnode->waittime = time;
158 newnode->donotwait = notwait;
159 Enqueue(infoList, (struct Node*)newnode);
161 error = MatchNext(ap);
164 if (error != ERROR_NO_MORE_ENTRIES)
166 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
167 "Error", "ExecuteStartup\nError %ld occured while scanning directory", "OK"};
168 EasyRequest(0, &es, 0, error);
169 retvalue = FALSE;
170 goto exit;
172 MatchEnd(ap);
174 else
176 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
177 "Error", "ExecuteStartup\nOut of memory for AnchorPath", "OK"};
178 EasyRequest(0, &es, 0);
179 retvalue = FALSE;
181 exit:
182 return retvalue;
185 static void
186 executePrograms(struct List *infolist)
188 struct InfoNode *infonode;
189 ForeachNode(infolist, infonode)
191 D(bug("ExecuteStartup Name %s Pri %d Time %d Notwait %d\n",
192 infonode->node.ln_Name, infonode->node.ln_Pri,
193 infonode->waittime, infonode->donotwait));
194 if (OpenWorkbenchObject(infonode->node.ln_Name, TAG_END))
196 Delay(infonode->waittime * 50);
198 else
200 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
201 "Warning", "ExecuteStartup\nOpenWorkbenchObject failed for:\n%s", "OK"};
202 EasyRequest(0, &es, 0, infonode->node.ln_Name);
207 static BOOL
208 executeWBStartup(void)
210 struct List infoList;
211 NEWLIST(&infoList);
212 BOOL retvalue = FALSE;
213 BPTR olddir = (BPTR)-1;
214 BPTR startupdir = 0;
216 struct FileInfoBlock *fib = AllocDosObjectTags(DOS_FIB, TAG_END);
217 poolmem = CreatePool(MEMF_ANY | MEMF_CLEAR, sizeof(struct InfoNode) * 10, sizeof(struct InfoNode) * 5);
219 while (TRUE) /*Cicle loop to avoid the use of goto; It uses now break instruction;*/
221 if (fib == NULL)
223 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
224 "Error", "ExecuteStartup\nOut of memory for FileInfoBlock", "OK"};
225 EasyRequest(0, &es, 0);
226 break;
229 if (poolmem == NULL)
231 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
232 "Error", "ExecuteStartup\nCouldn't create pool memory", "OK"};
233 EasyRequest(0, &es, 0);
234 break;
237 if ( (startupdir = Lock(STARTUPDIR, ACCESS_READ) ) == 0)
239 D(bug("ExecuteStartup: Couldn't lock " STARTUPDIR "\n"));
240 break;
243 if ( ! Examine(startupdir, fib))
245 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
246 "Error", "ExecuteStartup\nCouldn't examine " STARTUPDIR, "OK"};
247 EasyRequest(0, &es, 0);
248 break;
251 // check if startupdir is a directory
252 if (fib->fib_DirEntryType >= 0)
254 olddir = CurrentDir(startupdir);
255 if (searchInfo(&infoList))
257 executePrograms(&infoList);
258 retvalue = TRUE;
261 else
263 D(bug("ExecuteStartup: " STARTUPDIR " isn't a directory\n"));
266 break;
270 // Cleanup
271 if (startupdir) UnLock(startupdir);
272 if (olddir != (BPTR)-1) CurrentDir(olddir);
273 if (fib) FreeDosObject(DOS_FIB, fib);
274 if (poolmem) DeletePool(poolmem);
276 return retvalue;