Fixed a bug in the LBA extensions detection code.
[reactos.git] / freeldr / freeldr / inifile / inifile.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "freeldr.h"
21 #include "parseini.h"
22 #include "ui.h"
23 #include "fs.h"
24 #include "rtl.h"
25 #include "mm.h"
26 #include "debug.h"
27
28 PUCHAR FreeLoaderIniFileData = NULL;
29 ULONG FreeLoaderIniFileSize = 0;
30
31 BOOL ParseIniFile(VOID)
32 {
33 //int i;
34 //char name[1024];
35 //char value[1024];
36 PFILE Freeldr_Ini; // File handle for freeldr.ini
37
38 // Open the boot drive for file access
39 if (!OpenDiskDrive(BootDrive, 0))
40 {
41 printf("Error opening boot drive for file access.\n");
42 return FALSE;
43 }
44
45 // Try to open freeldr.ini or fail
46 Freeldr_Ini = OpenFile("freeldr.ini");
47 if (Freeldr_Ini == NULL)
48 {
49 printf("FREELDR.INI not found.\nYou need to re-install FreeLoader.\n");
50 return FALSE;
51 }
52
53 // Get the file size & allocate enough memory for it
54 FreeLoaderIniFileSize = GetFileSize(Freeldr_Ini);
55 FreeLoaderIniFileData = AllocateMemory(FreeLoaderIniFileSize);
56
57 // If we are out of memory then return FALSE
58 if (FreeLoaderIniFileData == NULL)
59 {
60 printf("Out of memory while loading FREELDR.INI.\n");
61 CloseFile(Freeldr_Ini);
62 return FALSE;
63 }
64
65 // Read freeldr.ini off the disk
66 ReadFile(Freeldr_Ini, FreeLoaderIniFileSize, NULL, FreeLoaderIniFileData);
67 CloseFile(Freeldr_Ini);
68
69 // Make sure the [FREELOADER] section exists
70 /*if (OpenSection("FREELOADER", NULL))
71 {
72 printf("Section [FREELOADER] not found in FREELDR.INI.\nYou need to re-install FreeLoader.\n");
73 return FALSE;
74 }
75
76 // Validate the settings in the [FREELOADER] section
77 for (i=1; i<=GetNumSectionItems("FREELOADER"); i++)
78 {
79 ReadSectionSettingByNumber("FREELOADER", i, name, value);
80 if (!IsValidSetting(name, value))
81 {
82 printf("Invalid setting in freeldr.ini.\nName: \"%s\", Value: \"%s\"\n", name, value);
83 printf("Press any key to continue.\n");
84 getch();
85 }
86 else
87 SetSetting(name, value);
88 }*/
89
90 return TRUE;
91 }
92
93 ULONG GetNextLineOfFileData(PUCHAR Buffer, ULONG BufferSize, ULONG CurrentOffset)
94 {
95 ULONG Idx;
96
97 // Loop through grabbing chars until we hit the end of the
98 // file or we encounter a new line char
99 for (Idx=0; (CurrentOffset < FreeLoaderIniFileSize); CurrentOffset++)
100 {
101 // If we haven't exceeded our buffer size yet
102 // then store another char
103 if (Idx < (BufferSize - 1))
104 {
105 Buffer[Idx++] = FreeLoaderIniFileData[CurrentOffset];
106 }
107
108 // Check for new line char
109 if (FreeLoaderIniFileData[CurrentOffset] == '\n')
110 {
111 CurrentOffset++;
112 break;
113 }
114 }
115
116 // Terminate the string
117 Buffer[Idx] = '\0';
118
119 // Get rid of newline & linefeed characters (if any)
120 if((Buffer[strlen(Buffer)-1] == '\n') || (Buffer[strlen(Buffer)-1] == '\r'))
121 Buffer[strlen(Buffer)-1] = '\0';
122 if((Buffer[strlen(Buffer)-1] == '\n') || (Buffer[strlen(Buffer)-1] == '\r'))
123 Buffer[strlen(Buffer)-1] = '\0';
124
125 // Send back new offset
126 return CurrentOffset;
127 }
128
129 BOOL OpenSection(PUCHAR SectionName, PULONG SectionId)
130 {
131 UCHAR TempString[80];
132 UCHAR RealSectionName[80];
133 ULONG FileOffset;
134 BOOL SectionFound = FALSE;
135
136 //
137 // Get the real section name
138 //
139 strcpy(RealSectionName, "[");
140 strcat(RealSectionName, SectionName);
141 strcat(RealSectionName, "]");
142
143 //
144 // Get to the beginning of the file
145 //
146 FileOffset = 0;
147
148 //
149 // Find the section
150 //
151 while (FileOffset < FreeLoaderIniFileSize)
152 {
153 //
154 // Read a line
155 //
156 FileOffset = GetNextLineOfFileData(TempString, 80, FileOffset);
157
158 //
159 // If it isn't a section header then continue on
160 //
161 if (TempString[0] != '[')
162 continue;
163
164 //
165 // Check and see if we found it
166 //
167 if (stricmp(TempString, RealSectionName) == 0)
168 {
169 SectionFound = TRUE;
170 break;
171 }
172 }
173
174 if (SectionId)
175 {
176 *SectionId = FileOffset;
177 }
178
179 return SectionFound;
180 }
181
182 ULONG GetNumSectionItems(ULONG SectionId)
183 {
184 UCHAR TempString[80];
185 ULONG SectionItemCount = 0;
186
187 // Now count how many settings are in this section
188 while (SectionId < FreeLoaderIniFileSize)
189 {
190 // Read a line
191 SectionId = GetNextLineOfFileData(TempString, 80, SectionId);
192
193 // If we hit a new section then we're done
194 if (TempString[0] == '[')
195 break;
196
197 // Skip comments
198 if (TempString[0] == '#')
199 continue;
200
201 // Skip blank lines
202 if (!strlen(TempString))
203 continue;
204
205 SectionItemCount++;
206 }
207
208 return SectionItemCount;
209 }
210
211 BOOL ReadSectionSettingByNumber(ULONG SectionId, ULONG SettingNumber, PUCHAR SettingName, ULONG NameSize, PUCHAR SettingValue, ULONG ValueSize)
212 {
213 UCHAR TempString[1024];
214 ULONG SectionItemCount = 0;
215 ULONG Idx;
216 ULONG FileOffset;
217
218 //
219 // Get to the beginning of the section
220 //
221 FileOffset = SectionId;
222
223 //
224 // Now find the setting we are looking for
225 //
226 do
227 {
228 // Read a line
229 FileOffset = GetNextLineOfFileData(TempString, 1024, FileOffset);
230
231 // Skip comments
232 if (TempString[0] == '#')
233 continue;
234
235 // Skip blank lines
236 if (!strlen(TempString))
237 continue;
238
239 // If we hit a new section then we're done
240 if (TempString[0] == '[')
241 break;
242
243 // Check and see if we found the setting
244 if (SectionItemCount == SettingNumber)
245 {
246 for (Idx=0; Idx<strlen(TempString); Idx++)
247 {
248 // Check and see if this character is the separator
249 if (TempString[Idx] == '=')
250 {
251 SettingName[Idx] = '\0';
252
253 strncpy(SettingValue, TempString + Idx + 1, ValueSize);
254
255 return TRUE;
256 }
257 else if (Idx < NameSize)
258 {
259 SettingName[Idx] = TempString[Idx];
260 }
261 }
262 }
263
264 // Increment setting number
265 SectionItemCount++;
266 }
267 while (FileOffset < FreeLoaderIniFileSize);
268
269 return FALSE;
270 }
271
272 BOOL ReadSectionSettingByName(ULONG SectionId, PUCHAR SettingName, PUCHAR Buffer, ULONG BufferSize)
273 {
274 UCHAR TempString[1024];
275 UCHAR TempBuffer[80];
276 ULONG Idx;
277 ULONG FileOffset;
278
279 //
280 // Get to the beginning of the section
281 //
282 FileOffset = SectionId;
283
284 //
285 // Now find the setting we are looking for
286 //
287 while (FileOffset < FreeLoaderIniFileSize)
288 {
289 // Read a line
290 FileOffset = GetNextLineOfFileData(TempString, 1024, FileOffset);
291
292 // Skip comments
293 if (TempString[0] == '#')
294 continue;
295
296 // Skip blank lines
297 if (!strlen(TempString))
298 continue;
299
300 // If we hit a new section then we're done
301 if (TempString[0] == '[')
302 break;
303
304 // Extract the setting name
305 for (Idx=0; Idx<strlen(TempString); Idx++)
306 {
307 if (TempString[Idx] != '=')
308 TempBuffer[Idx] = TempString[Idx];
309 else
310 {
311 TempBuffer[Idx] = '\0';
312 break;
313 }
314 }
315
316 // Check and see if we found the setting
317 if (stricmp(TempBuffer, SettingName) == 0)
318 {
319 for (Idx=0; Idx<strlen(TempString); Idx++)
320 {
321 // Check and see if this character is the separator
322 if (TempString[Idx] == '=')
323 {
324 strcpy(Buffer, TempString + Idx + 1);
325
326 return TRUE;
327 }
328 }
329 }
330 }
331
332 return FALSE;
333 }
334
335 BOOL IsValidSetting(char *setting, char *value)
336 {
337 if(stricmp(setting, "MessageBox") == 0)
338 return TRUE;
339 else if(stricmp(setting, "MessageLine") == 0)
340 return TRUE;
341 else if(stricmp(setting, "TitleText") == 0)
342 return TRUE;
343 else if(stricmp(setting, "StatusBarColor") == 0)
344 {
345 if(IsValidColor(value))
346 return TRUE;
347 }
348 else if(stricmp(setting, "StatusBarTextColor") == 0)
349 {
350 if(IsValidColor(value))
351 return TRUE;
352 }
353 else if(stricmp(setting, "BackdropTextColor") == 0)
354 {
355 if(IsValidColor(value))
356 return TRUE;
357 }
358 else if(stricmp(setting, "BackdropColor") == 0)
359 {
360 if(IsValidColor(value))
361 return TRUE;
362 }
363 else if(stricmp(setting, "BackdropFillStyle") == 0)
364 {
365 if(IsValidFillStyle(value))
366 return TRUE;
367 }
368 else if(stricmp(setting, "TitleBoxTextColor") == 0)
369 {
370 if(IsValidColor(value))
371 return TRUE;
372 }
373 else if(stricmp(setting, "TitleBoxColor") == 0)
374 {
375 if(IsValidColor(value))
376 return TRUE;
377 }
378 else if(stricmp(setting, "MessageBoxTextColor") == 0)
379 {
380 if(IsValidColor(value))
381 return TRUE;
382 }
383 else if(stricmp(setting, "MessageBoxColor") == 0)
384 {
385 if(IsValidColor(value))
386 return TRUE;
387 }
388 else if(stricmp(setting, "MenuTextColor") == 0)
389 {
390 if(IsValidColor(value))
391 return TRUE;
392 }
393 else if(stricmp(setting, "MenuColor") == 0)
394 {
395 if(IsValidColor(value))
396 return TRUE;
397 }
398 else if(stricmp(setting, "TextColor") == 0)
399 {
400 if(IsValidColor(value))
401 return TRUE;
402 }
403 else if(stricmp(setting, "SelectedTextColor") == 0)
404 {
405 if(IsValidColor(value))
406 return TRUE;
407 }
408 else if(stricmp(setting, "SelectedColor") == 0)
409 {
410 if(IsValidColor(value))
411 return TRUE;
412 }
413 else if(stricmp(setting, "OS") == 0)
414 return TRUE;
415 else if(stricmp(setting, "TimeOut") == 0)
416 return TRUE;
417 /*else if(stricmp(setting, "") == 0)
418 return TRUE;
419 else if(stricmp(setting, "") == 0)
420 return TRUE;
421 else if(stricmp(setting, "") == 0)
422 return TRUE;
423 else if(stricmp(setting, "") == 0)
424 return TRUE;
425 else if(stricmp(setting, "") == 0)
426 return TRUE;
427 else if(stricmp(setting, "") == 0)
428 return TRUE;
429 else if(stricmp(setting, "") == 0)
430 return TRUE;*/
431
432 return FALSE;
433 }