A bit number was mistakenly used instead of a flag when setting notification
[AROS.git] / workbench / system / Wanderer / Tools / ExecuteStartup / main.c
blobdfef126a75aa1dfe5357bcc58db87c8838bf51b9
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 #include "locale.h"
52 #define STARTUPDIR "sys:WBStartup/"
54 struct InfoNode
56 struct Node node;
57 ULONG waittime;
58 BOOL donotwait;
61 const TEXT version_string[] = "$VER: ExecuteStartup 0.2 (20.10.2007) © AROS Dev Team";
63 static TEXT fileNameBuffer[MAXFILENAMELENGTH];
64 static APTR poolmem;
66 static BOOL checkIcon(STRPTR name, LONG *pri, ULONG *time, BOOL *notwait);
67 static BOOL searchInfo(struct List *infoList);
68 static void executePrograms(struct List *infolist);
69 static BOOL executeWBStartup(void);
71 int
72 main(void)
75 executeWBStartup();
77 return RETURN_OK;
80 static BOOL
81 checkIcon(STRPTR name, LONG *pri, ULONG *time, BOOL *notwait)
83 *pri = 0;
84 *time = 0;
85 *notwait = FALSE;
87 struct DiskObject *dobj = GetDiskObject(name);
88 if (dobj == NULL)
90 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
91 "Error", "ExecuteStartup\nGetDiskObject failed for:\n%s", "OK"};
92 EasyRequest(0, &es, 0, name);
93 return FALSE;
96 if ((dobj->do_Type == WBTOOL) || (dobj->do_Type == WBPROJECT))
98 const STRPTR *toolarray = (const STRPTR *)dobj->do_ToolTypes;
99 STRPTR s;
100 if ((s = FindToolType(toolarray, "STARTPRI")))
102 *pri = atol(s);
103 if (*pri < -128) *pri = -128;
104 if (*pri > 127) *pri = 127;
106 if ((s = FindToolType(toolarray, "WAIT")))
108 *time = atol(s);
109 if (*time > 60) *time = 60;
111 if ((s = FindToolType(toolarray, "DONOTWAIT")))
113 *notwait = TRUE;
115 FreeDiskObject(dobj);
116 return TRUE;
118 return FALSE;
121 static BOOL
122 searchInfo(struct List *infoList)
124 BOOL retvalue = TRUE;
125 LONG error;
126 struct AnchorPath *ap = AllocPooled(poolmem, sizeof(struct AnchorPath));
127 if (ap)
129 error = MatchFirst(STARTUPDIR "?#?.info", ap);
130 while (!error)
132 strcpy(fileNameBuffer, ap->ap_Info.fib_FileName);
133 BYTE * pointPos = strrchr(fileNameBuffer, '.');
134 *pointPos = '\0';
136 LONG pri;
137 ULONG time;
138 BOOL notwait;
139 if (checkIcon(fileNameBuffer, &pri, &time, &notwait))
141 struct InfoNode *newnode = AllocPooled(poolmem, sizeof (*newnode));
142 if (newnode == NULL)
144 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
145 "Error", "ExecuteStartup\nOut of memory for InfoNode", "OK"};
146 EasyRequest(0, &es, 0);
147 retvalue = FALSE;
148 goto exit;
150 newnode->node.ln_Name = AllocPooled(poolmem, strlen(fileNameBuffer) + 1);
151 if (newnode->node.ln_Name == NULL)
153 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
154 "Error", "ExecuteStartup\nOut of memory for ln_Name", "OK"};
155 EasyRequest(0, &es, 0);
156 retvalue = FALSE;
157 goto exit;
159 strcpy(newnode->node.ln_Name, fileNameBuffer);
160 newnode->node.ln_Pri = pri;
161 newnode->waittime = time;
162 newnode->donotwait = notwait;
163 Enqueue(infoList, (struct Node*)newnode);
165 error = MatchNext(ap);
168 if (error != ERROR_NO_MORE_ENTRIES)
170 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
171 "Error", "ExecuteStartup\nError %ld occured while scanning directory", "OK"};
172 EasyRequest(0, &es, 0, error);
173 retvalue = FALSE;
174 goto exit;
176 MatchEnd(ap);
178 else
180 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
181 "Error", "ExecuteStartup\nOut of memory for AnchorPath", "OK"};
182 EasyRequest(0, &es, 0);
183 retvalue = FALSE;
185 exit:
186 return retvalue;
189 static void
190 executePrograms(struct List *infolist)
192 struct InfoNode *infonode;
193 ForeachNode(infolist, infonode)
195 D(bug("ExecuteStartup Name %s Pri %d Time %d Notwait %d\n",
196 infonode->node.ln_Name, infonode->node.ln_Pri,
197 infonode->waittime, infonode->donotwait));
198 if (OpenWorkbenchObject(infonode->node.ln_Name, TAG_END))
200 Delay(infonode->waittime * 50);
202 else
204 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
205 "Warning", "ExecuteStartup\nOpenWorkbenchObject failed for:\n%s", "OK"};
206 EasyRequest(0, &es, 0, infonode->node.ln_Name);
211 static BOOL
212 executeWBStartup(void)
214 struct List infoList;
215 NEWLIST(&infoList);
216 BOOL retvalue = FALSE;
217 BPTR olddir = (BPTR)-1;
218 BPTR startupdir = 0;
220 struct FileInfoBlock *fib = AllocDosObjectTags(DOS_FIB, TAG_END);
221 poolmem = CreatePool(MEMF_ANY | MEMF_CLEAR, sizeof(struct InfoNode) * 10, sizeof(struct InfoNode) * 5);
223 while (TRUE) /*Cicle loop to avoid the use of goto; It uses now break instruction;*/
225 if (fib == NULL)
227 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
228 "Error", "ExecuteStartup\nOut of memory for FileInfoBlock", "OK"};
229 EasyRequest(0, &es, 0);
230 break;
233 if (poolmem == NULL)
235 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
236 "Error", "ExecuteStartup\nCouldn't create pool memory", "OK"};
237 EasyRequest(0, &es, 0);
238 break;
241 if ( (startupdir = Lock(STARTUPDIR, ACCESS_READ) ) == 0)
243 D(bug("ExecuteStartup: Couldn't lock " STARTUPDIR "\n"));
244 break;
247 if ( ! Examine(startupdir, fib))
249 struct EasyStruct es = {sizeof(struct EasyStruct), 0,
250 "Error", "ExecuteStartup\nCouldn't examine " STARTUPDIR, "OK"};
251 EasyRequest(0, &es, 0);
252 break;
255 // check if startupdir is a directory
256 if (fib->fib_DirEntryType >= 0)
258 olddir = CurrentDir(startupdir);
259 if (searchInfo(&infoList))
261 executePrograms(&infoList);
262 retvalue = TRUE;
265 else
267 D(bug("ExecuteStartup: " STARTUPDIR " isn't a directory\n"));
270 break;
274 // Cleanup
275 if (startupdir) UnLock(startupdir);
276 if (olddir != (BPTR)-1) CurrentDir(olddir);
277 if (fib) FreeDosObject(DOS_FIB, fib);
278 if (poolmem) DeletePool(poolmem);
280 return retvalue;