34d67df2a0717ec48ac740333631f334641c8e38
[reactos.git] / freeldr / freeldr / oslist.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1999, 2000, 2001 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 "oslist.h"
23
24 BOOL InitOperatingSystemList(PUCHAR **SectionNamesPointer, PUCHAR **DisplayNamesPointer, PULONG OperatingSystemCountPointer)
25 {
26 ULONG Idx;
27 ULONG CurrentOperatingSystemIndex;
28 UCHAR SettingName[80];
29 UCHAR SettingValue[80];
30 ULONG OperatingSystemCount;
31 ULONG SectionId;
32 ULONG OperatingSystemSectionId;
33 ULONG SectionSettingCount;
34 PUCHAR *OperatingSystemSectionNames;
35 PUCHAR *OperatingSystemDisplayNames;
36
37 //
38 // Open the [FreeLoader] section
39 //
40 if (!OpenSection("FreeLoader", &SectionId))
41 {
42 MessageBox("Section [FreeLoader] not found in freeldr.ini.");
43 return FALSE;
44 }
45
46 SectionSettingCount = GetNumSectionItems(SectionId);
47 OperatingSystemCount = CountOperatingSystems(SectionId);
48
49 //
50 // Allocate memory to hold operating system lists
51 //
52 if (!AllocateListMemory(&OperatingSystemSectionNames, &OperatingSystemDisplayNames, OperatingSystemCount))
53 {
54 return FALSE;
55 }
56
57 //
58 // Now loop through and read the operating system section names
59 //
60 CurrentOperatingSystemIndex = 0;
61 for (Idx=0; Idx<SectionSettingCount; Idx++)
62 {
63 ReadSectionSettingByNumber(SectionId, Idx, SettingName, 80, SettingValue, 80);
64
65 if (stricmp(SettingName, "OS") == 0)
66 {
67 strcpy(OperatingSystemSectionNames[CurrentOperatingSystemIndex], SettingValue);
68
69 CurrentOperatingSystemIndex++;
70 }
71 }
72
73 //
74 // Now loop through and read the operating system display names
75 //
76 for (Idx=0; Idx<OperatingSystemCount; Idx++)
77 {
78 if (OpenSection(OperatingSystemSectionNames[Idx], &OperatingSystemSectionId))
79 {
80 if (ReadSectionSettingByName(OperatingSystemSectionId, "Name", SettingValue, 80))
81 {
82 //
83 // Remove any quotes around the string
84 //
85 RemoveQuotes(SettingValue);
86 strcpy(OperatingSystemDisplayNames[Idx], SettingValue);
87 }
88 else
89 {
90 sprintf(SettingName, "Operating System '%s' has no Name= line in it's [section].", OperatingSystemSectionNames[Idx]);
91 MessageBox(SettingName);
92 strcpy(OperatingSystemDisplayNames[Idx], "");
93 }
94 }
95 }
96
97 *OperatingSystemCountPointer = OperatingSystemCount;
98 *SectionNamesPointer = OperatingSystemSectionNames;
99 *DisplayNamesPointer = OperatingSystemDisplayNames;
100
101 return TRUE;
102 }
103
104 ULONG CountOperatingSystems(ULONG SectionId)
105 {
106 ULONG Idx;
107 UCHAR SettingName[80];
108 UCHAR SettingValue[80];
109 ULONG OperatingSystemCount = 0;
110 ULONG SectionSettingCount;
111
112 //
113 // Loop through and count the operating systems
114 //
115 SectionSettingCount = GetNumSectionItems(SectionId);
116 for (Idx=0; Idx<SectionSettingCount; Idx++)
117 {
118 ReadSectionSettingByNumber(SectionId, Idx, SettingName, 80, SettingValue, 80);
119
120 if (stricmp(SettingName, "OS") == 0)
121 {
122 if (OpenSection(SettingValue, NULL))
123 {
124 OperatingSystemCount++;
125 }
126 else
127 {
128 sprintf(SettingName, "Operating System '%s' is listed in freeldr.ini but doesn't have a [section].", SettingValue);
129 MessageBox(SettingName);
130 }
131 }
132 }
133
134 return OperatingSystemCount;
135 }
136
137 BOOL AllocateListMemory(PUCHAR **SectionNamesPointer, PUCHAR **DisplayNamesPointer, ULONG OperatingSystemCount)
138 {
139 ULONG Idx;
140 PUCHAR *OperatingSystemSectionNames = NULL;
141 PUCHAR *OperatingSystemDisplayNames = NULL;
142
143 //
144 // Allocate memory to hold operating system list arrays
145 //
146 OperatingSystemSectionNames = (PUCHAR*) AllocateMemory( sizeof(PUCHAR) * OperatingSystemCount);
147 OperatingSystemDisplayNames = (PUCHAR*) AllocateMemory( sizeof(PUCHAR) * OperatingSystemCount);
148
149 //
150 // If either allocation failed then return FALSE
151 //
152 if ( (OperatingSystemSectionNames == NULL) || (OperatingSystemDisplayNames == NULL) )
153 {
154 if (OperatingSystemSectionNames != NULL)
155 {
156 FreeMemory(OperatingSystemSectionNames);
157 }
158
159 if (OperatingSystemDisplayNames != NULL)
160 {
161 FreeMemory(OperatingSystemDisplayNames);
162 }
163
164 return FALSE;
165 }
166
167 //
168 // Clear our newly allocated memory
169 //
170 memset(OperatingSystemSectionNames, 0, sizeof(PUCHAR) * OperatingSystemCount);
171 memset(OperatingSystemDisplayNames, 0, sizeof(PUCHAR) * OperatingSystemCount);
172
173 //
174 // Loop through each array element and allocate it's string memory
175 //
176 for (Idx=0; Idx<OperatingSystemCount; Idx++)
177 {
178 OperatingSystemSectionNames[Idx] = (PUCHAR) AllocateMemory(80);
179 OperatingSystemDisplayNames[Idx] = (PUCHAR) AllocateMemory(80);
180
181 //
182 // If it failed then jump to the cleanup code
183 //
184 if ( (OperatingSystemSectionNames[Idx] == NULL) || (OperatingSystemDisplayNames[Idx] == NULL))
185 {
186 goto AllocateListMemoryFailed;
187 }
188 }
189
190 *SectionNamesPointer = OperatingSystemSectionNames;
191 *DisplayNamesPointer = OperatingSystemDisplayNames;
192
193 return TRUE;
194
195 AllocateListMemoryFailed:
196
197 //
198 // Loop through each array element and free it's string memory
199 //
200 for (Idx=0; Idx<OperatingSystemCount; Idx++)
201 {
202 if (OperatingSystemSectionNames[Idx] != NULL)
203 {
204 FreeMemory(OperatingSystemSectionNames[Idx]);
205 }
206
207 if (OperatingSystemDisplayNames[Idx] != NULL)
208 {
209 FreeMemory(OperatingSystemDisplayNames[Idx]);
210 }
211 }
212
213 //
214 // Free operating system list arrays
215 //
216 FreeMemory(OperatingSystemSectionNames);
217 FreeMemory(OperatingSystemDisplayNames);
218
219 return FALSE;
220 }
221
222 BOOL RemoveQuotes(PUCHAR QuotedString)
223 {
224 UCHAR TempString[200];
225
226 //
227 // If this string is not quoted then return FALSE
228 //
229 if ((QuotedString[0] != '\"') && (QuotedString[strlen(QuotedString)-1] != '\"'))
230 {
231 return FALSE;
232 }
233
234 if (QuotedString[0] == '\"')
235 {
236 strcpy(TempString, (QuotedString + 1));
237 }
238 else
239 {
240 strcpy(TempString, QuotedString);
241 }
242
243 if (TempString[strlen(TempString)-1] == '\"')
244 {
245 TempString[strlen(TempString)-1] = '\0';
246 }
247
248 strcpy(QuotedString, TempString);
249
250 return TRUE;
251 }