Removed debugging code.
[tangerine.git] / workbench / tools / HDToolBox / prefs.c
blobcb924686721672876c870858177534ed50665b5e
1 /*
2 Copyright © 1995-2002, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/dos.h>
7 #include <proto/exec.h>
8 #include <dos/dos.h>
9 #include <dos/rdargs.h>
10 #include <exec/lists.h>
11 #include <exec/memory.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <strings.h>
15 #define DEBUG 1
16 #include "debug.h"
18 #include "prefs.h"
19 #include "devices.h"
20 #include "hdtoolbox_support.h"
21 #include "platform.h"
23 struct List tabletypelist;
25 struct TableTypeNode *findTableTypeNodeName(STRPTR name) {
26 struct TableTypeNode *ttn;
28 ttn = (struct TableTypeNode *)tabletypelist.lh_Head;
29 while (ttn->ln.ln_Succ)
31 if (strcmp(ttn->pti->pti_Name, name) == 0)
32 return ttn;
33 ttn = (struct TableTypeNode *)ttn->ln.ln_Succ;
35 return NULL;
38 struct TableTypeNode *findTableTypeNode(ULONG tabletype) {
39 struct TableTypeNode *ttn;
41 ttn = (struct TableTypeNode *)tabletypelist.lh_Head;
42 while (ttn->ln.ln_Succ)
44 if (ttn->pti->pti_Type == tabletype)
45 return ttn;
46 ttn = (struct TableTypeNode *)ttn->ln.ln_Succ;
48 return NULL;
51 struct TypeNode *findPartitionType(struct PartitionType *type, ULONG tabletype) {
52 struct TableTypeNode *ttn;
53 struct TypeNode *tn;
55 if (tabletype == PHPTT_EBR)
56 tabletype = PHPTT_MBR; // EBR uses same types as MBR
57 ttn = findTableTypeNode(tabletype);
58 if (ttn)
60 tn = (struct TypeNode *)ttn->typelist.lh_Head;
61 while (tn->ln.ln_Succ)
63 if (tn->type.id_len == type->id_len)
65 if (memcmp(tn->type.id, type->id, type->id_len) == 0)
66 return tn;
68 tn = (struct TypeNode *)tn->ln.ln_Succ;
71 return NULL;
74 void getTableTypeList(struct List *list) {
75 struct TableTypeNode *ttn;
76 int i;
78 NEWLIST(&tabletypelist);
79 for (i=0;PartitionBase->tables[i];i++)
81 ttn = AllocMem(sizeof(struct TableTypeNode), MEMF_PUBLIC | MEMF_CLEAR);
82 if (ttn)
84 ttn->pti = PartitionBase->tables[i];
85 NEWLIST(&ttn->typelist);
86 AddTail(list, &ttn->ln);
91 LONG parsePrefs(char *buffer, LONG size) {
92 struct TableTypeNode *ttn=NULL;
93 struct TypeNode *tn;
94 struct CSource csrc = {buffer, size, 0};
95 char ident[256];
96 LONG res;
97 ULONG id_len=0;
98 WORD current=0;
99 WORD line=1;
101 while (csrc.CS_CurChr < csrc.CS_Length)
103 res = ReadItem(ident, 256, &csrc);
104 switch (res)
106 case ITEM_ERROR:
107 return IoErr();
108 case ITEM_UNQUOTED:
109 if (strcasecmp(ident, "[Devices]") == 0)
110 current = 1;
111 else if (strcasecmp(ident, "[TableIDs]") == 0)
113 current = 2;
114 ttn = NULL;
115 id_len = 0;
117 else
119 if (current == 1)
120 addDeviceName(ident);
121 else if (current == 2)
123 if (strcasecmp(ident, "TableType") == 0)
125 res = ReadItem(ident, 256, &csrc);
126 if (res == ITEM_ERROR)
127 return IoErr();
128 else if (res == ITEM_EQUAL)
130 res = ReadItem(ident, 256, &csrc);
131 if (res == ITEM_ERROR)
132 return IoErr();
133 else if (res == ITEM_QUOTED)
134 ttn = findTableTypeNodeName(ident);
135 else if (res == ITEM_UNQUOTED)
136 ttn = findTableTypeNode(strtoul(ident, NULL, 0));
137 else
139 printf("LINE %d: Unexpected item in TableType\n", line);
140 return 0;
142 if (ttn == 0)
144 printf("LINE %d: Unknown Table %s\n", line, ident);
145 return 0;
148 else
150 printf("LINE %d: Unexpected item after TableType\n", line);
151 return 0;
154 else if (strcasecmp(ident, "IDLen") == 0)
156 res = ReadItem(ident, 256, &csrc);
157 if (res == ITEM_ERROR)
158 return IoErr();
159 else if (res == ITEM_EQUAL)
161 res = ReadItem(ident, 256, &csrc);
162 if (res == ITEM_ERROR)
163 return IoErr();
164 else if (res == ITEM_UNQUOTED)
166 id_len = strtoul(ident, NULL, 0);
167 if (id_len == 0)
169 printf("LINE %d: Illegal value of IDLen\n", line);
170 return 0;
173 else
175 printf("LINE %d: Value in IDLen expected\n", line);
176 return 0;
179 else
181 printf("LINE %d: Unexpected item after IDLen\n", line);
182 return 0;
185 else if (strcasecmp(ident, "Default") == 0)
187 if (ttn && id_len)
189 res = ReadItem(ident, 256, &csrc);
190 if (res == ITEM_ERROR)
191 return IoErr();
192 else if (res == ITEM_EQUAL)
194 res = ReadItem(ident, 256, &csrc);
195 if (res == ITEM_ERROR)
196 return IoErr();
197 else if (res == ITEM_QUOTED)
199 ttn->defaulttype.id_len = id_len;
200 strcpyESC(ttn->defaulttype.id, ident);
202 else
204 printf("LINE %d: Unexpected expression after Default\n", line);
205 return 0;
208 else
210 printf("LINE %d: Unexpected item after IDLen\n", line);
211 return 0;
214 else
216 printf("LINE %d: Unknown option '%s'\n", line, ident);
217 return 0;
220 else
222 printf("LINE %d: Missing partition table type or IDLen\n", line);
223 return 0;
226 else
228 printf("LINE %d: Unexpected item '%s' in prefs\n", line, ident);
229 return 0;
232 break;
233 case ITEM_QUOTED:
234 if (ttn && id_len)
236 tn = AllocMem(sizeof(struct TypeNode), MEMF_PUBLIC | MEMF_CLEAR);
237 if (tn == NULL)
238 return ERROR_NO_FREE_STORE;
239 tn->type.id_len = id_len;
240 strcpyESC(tn->type.id, ident);
241 res = ReadItem(ident, 256, &csrc);
242 if (res == ITEM_ERROR)
243 return IoErr();
244 if (res == ITEM_EQUAL)
246 res = ReadItem(ident, 256, &csrc);
247 if (res == ITEM_ERROR)
248 return IoErr();
249 else if (res == ITEM_QUOTED)
251 tn->ln.ln_Name = AllocVec(strlen(ident)+1, MEMF_PUBLIC | MEMF_CLEAR);
252 if (tn->ln.ln_Name == NULL)
253 return ERROR_NO_FREE_STORE;
254 strcpy(tn->ln.ln_Name, ident);
256 else
258 printf("LINE %d: Quoted expression expected\n", line);
259 return 0;
262 else
264 printf("LINE %d: Unexpected item after table id\n", line);
265 return 0;
267 AddTail(&ttn->typelist, &tn->ln);
269 else
271 printf("LINE %d: Missing partition table type or IDLen\n", line);
272 return 0;
274 break;
275 case ITEM_NOTHING:
276 line++;
277 break;
280 return 0;
283 void LoadPrefs(STRPTR filename) {
284 struct FileInfoBlock fib;
285 char *buffer;
286 LONG retval;
287 LONG size;
288 BPTR fh;
290 getTableTypeList(&tabletypelist);
291 fh = Open(filename, MODE_OLDFILE);
292 if (fh)
294 if (ExamineFH(fh, &fib))
296 if (fib.fib_Size>0)
298 buffer = AllocMem(fib.fib_Size, MEMF_PUBLIC | MEMF_CLEAR);
299 if (buffer)
301 size = Read(fh, buffer, fib.fib_Size);
302 if (size == fib.fib_Size)
304 retval = parsePrefs(buffer, size);
305 if (retval)
306 PrintFault(retval, filename);
308 FreeMem(buffer, fib.fib_Size);
310 else
311 PrintFault(ERROR_NO_FREE_STORE, filename);
314 Close(fh);
316 else
317 PrintFault(IoErr(), filename);