- Merge from trunk up to r45543
[reactos.git] / boot / freeldr / freeldr / oslist.c
index 2461a8d..24313b1 100644 (file)
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <freeldr.h>
 
-BOOLEAN InitOperatingSystemList(PCSTR **SectionNamesPointer, PCSTR **DisplayNamesPointer, ULONG* OperatingSystemCountPointer)
+static PCSTR CopyString(PCSTR Source)
 {
-       ULONG           Idx;
-       ULONG           CurrentOperatingSystemIndex;
-       CHAR    SettingName[260];
-       CHAR    SettingValue[260];
-       ULONG           OperatingSystemCount;
-       ULONG_PTR       SectionId;
-       ULONG_PTR       OperatingSystemSectionId;
-       ULONG           SectionSettingCount;
-       PCHAR   *OperatingSystemSectionNames;
-       PCHAR   *OperatingSystemDisplayNames;
+       PSTR Dest;
 
-       //
-       // Open the [FreeLoader] section
-       //
-       if (!IniOpenSection("Operating Systems", &SectionId))
-       {
-               UiMessageBox("Section [Operating Systems] not found in freeldr.ini.");
-               return FALSE;
-       }
-
-       SectionSettingCount = IniGetNumSectionItems(SectionId);
-       OperatingSystemCount = CountOperatingSystems(SectionId);
-
-       //
-       // Allocate memory to hold operating system lists
-       //
-       if (!AllocateListMemory(&OperatingSystemSectionNames, &OperatingSystemDisplayNames, OperatingSystemCount))
+       if (!Source)
+               return NULL;
+       Dest = MmHeapAlloc(strlen(Source) + 1);
+       if (Dest)
        {
-               return FALSE;
+               strcpy(Dest, Source);
        }
 
-       //
-       // Now loop through and read the operating system section and display names
-       //
-       CurrentOperatingSystemIndex = 0;
-       for (Idx=0; Idx<SectionSettingCount; Idx++)
-       {
-               IniReadSettingByNumber(SectionId, Idx, SettingName, sizeof(SettingName), SettingValue, sizeof(SettingValue));
-
-               if (IniOpenSection(SettingName, &OperatingSystemSectionId))
-               {
-                       // Copy the section name
-                       strcpy(OperatingSystemSectionNames[CurrentOperatingSystemIndex], SettingName);
-
-                       // Copy the display name
-                       RemoveQuotes(SettingValue);
-                       strcpy(OperatingSystemDisplayNames[CurrentOperatingSystemIndex], SettingValue);
-
-                       CurrentOperatingSystemIndex++;
-               }
-       }
-
-       *OperatingSystemCountPointer = OperatingSystemCount;
-       *SectionNamesPointer = (PCSTR*)OperatingSystemSectionNames;
-       *DisplayNamesPointer = (PCSTR*)OperatingSystemDisplayNames;
-
-       return TRUE;
+       return Dest;
 }
 
-ULONG CountOperatingSystems(ULONG SectionId)
+OperatingSystemItem* InitOperatingSystemList(ULONG* OperatingSystemCountPointer)
 {
-       ULONG           Idx;
-       CHAR    SettingName[260];
-       CHAR    SettingValue[260];
-       ULONG           OperatingSystemCount = 0;
-       ULONG           SectionSettingCount;
+       ULONG Idx;
+       CHAR SettingName[260];
+       CHAR SettingValue[260];
+       ULONG_PTR SectionId;
+       PCHAR TitleStart, TitleEnd;
+       PCSTR OsLoadOptions;
+       ULONG Count;
+       OperatingSystemItem* Items;
 
        //
-       // Loop through and count the operating systems
+       // Open the [FreeLoader] section
        //
-       SectionSettingCount = IniGetNumSectionItems(SectionId);
-       for (Idx=0; Idx<SectionSettingCount; Idx++)
+       if (!IniOpenSection("Operating Systems", &SectionId))
        {
-               IniReadSettingByNumber(SectionId, Idx, SettingName, sizeof(SettingName), SettingValue, sizeof(SettingValue));
-
-               if (IniOpenSection(SettingName, NULL))
-               {
-                       OperatingSystemCount++;
-               }
-               else
-               {
-                       sprintf(SettingName, "Operating System '%s' is listed in\nfreeldr.ini but doesn't have a [section].", SettingValue);
-                       UiMessageBox(SettingName);
-               }
+               return NULL;
        }
 
-       return OperatingSystemCount;
-}
-
-BOOLEAN AllocateListMemory(PCHAR **SectionNamesPointer, PCHAR **DisplayNamesPointer, ULONG OperatingSystemCount)
-{
-       ULONG           Idx;
-       PCHAR   *OperatingSystemSectionNames = NULL;
-       PCHAR   *OperatingSystemDisplayNames = NULL;
-
        //
-       // Allocate memory to hold operating system list arrays
+       // Count number of operating systems in the section
        //
-       OperatingSystemSectionNames = MmHeapAlloc( sizeof(PCHAR) * OperatingSystemCount);
-       OperatingSystemDisplayNames = MmHeapAlloc( sizeof(PCHAR) * OperatingSystemCount);
+       Count = IniGetNumSectionItems(SectionId);
 
        //
-       // If either allocation failed then return FALSE
+       // Allocate memory to hold operating system lists
        //
-       if ( (OperatingSystemSectionNames == NULL) || (OperatingSystemDisplayNames == NULL) )
+       Items = MmHeapAlloc(Count * sizeof(OperatingSystemItem));
+       if (!Items)
        {
-               if (OperatingSystemSectionNames != NULL)
-               {
-                       MmHeapFree(OperatingSystemSectionNames);
-               }
-
-               if (OperatingSystemDisplayNames != NULL)
-               {
-                       MmHeapFree(OperatingSystemDisplayNames);
-               }
-
-               return FALSE;
+               return NULL;
        }
 
        //
-       // Clear our newly allocated memory
-       //
-       memset(OperatingSystemSectionNames, 0, sizeof(PCHAR) * OperatingSystemCount);
-       memset(OperatingSystemDisplayNames, 0, sizeof(PCHAR) * OperatingSystemCount);
-
-       //
-       // Loop through each array element and allocate it's string memory
+       // Now loop through and read the operating system section and display names
        //
-       for (Idx=0; Idx<OperatingSystemCount; Idx++)
+       for (Idx = 0; Idx < Count; Idx++)
        {
-               OperatingSystemSectionNames[Idx] = MmHeapAlloc(80);
-               OperatingSystemDisplayNames[Idx] = MmHeapAlloc(80);
+               IniReadSettingByNumber(SectionId, Idx, SettingName, sizeof(SettingName), SettingValue, sizeof(SettingValue));
 
                //
-               // If it failed then jump to the cleanup code
+               // Search start and end of the title
                //
-               if ( (OperatingSystemSectionNames[Idx] == NULL) || (OperatingSystemDisplayNames[Idx] == NULL))
-               {
-                       goto AllocateListMemoryFailed;
-               }
-       }
-
-       *SectionNamesPointer = OperatingSystemSectionNames;
-       *DisplayNamesPointer = OperatingSystemDisplayNames;
-
-       return TRUE;
-
-AllocateListMemoryFailed:
-
-       //
-       // Loop through each array element and free it's string memory
-       //
-       for (Idx=0; Idx<OperatingSystemCount; Idx++)
-       {
-               if (OperatingSystemSectionNames[Idx] != NULL)
+               OsLoadOptions = NULL;
+               TitleStart = SettingValue;
+               while (*TitleStart == ' ' || *TitleStart == '"')
+                       TitleStart++;
+               TitleEnd = TitleStart;
+               if (*TitleEnd != ANSI_NULL)
+                       TitleEnd++;
+               while (*TitleEnd != ANSI_NULL && *TitleEnd != '"')
+                       TitleEnd++;
+               if (*TitleEnd != ANSI_NULL)
                {
-                       MmHeapFree(OperatingSystemSectionNames[Idx]);
+                       *TitleEnd = ANSI_NULL;
+                       OsLoadOptions = TitleEnd + 1;
                }
 
-               if (OperatingSystemDisplayNames[Idx] != NULL)
-               {
-                       MmHeapFree(OperatingSystemDisplayNames[Idx]);
-               }
+               //
+               // Copy the system partition, identifier and options
+               //
+               Items[Idx].SystemPartition = CopyString(SettingName);
+               Items[Idx].LoadIdentifier = CopyString(TitleStart);
+               Items[Idx].OsLoadOptions = CopyString(OsLoadOptions);
        }
 
        //
-       // Free operating system list arrays
-       //
-       MmHeapFree(OperatingSystemSectionNames);
-       MmHeapFree(OperatingSystemDisplayNames);
-
-       return FALSE;
-}
-
-BOOLEAN RemoveQuotes(PCHAR QuotedString)
-{
-       CHAR    TempString[200];
-
-       //
-       // If this string is not quoted then return FALSE
+       // Return success
        //
-       if ((QuotedString[0] != '\"') && (QuotedString[strlen(QuotedString)-1] != '\"'))
-       {
-               return FALSE;
-       }
-
-       if (QuotedString[0] == '\"')
-       {
-               strcpy(TempString, (QuotedString + 1));
-       }
-       else
-       {
-               strcpy(TempString, QuotedString);
-       }
-
-       if (TempString[strlen(TempString)-1] == '\"')
-       {
-               TempString[strlen(TempString)-1] = '\0';
-       }
-
-       strcpy(QuotedString, TempString);
-
-       return TRUE;
+       *OperatingSystemCountPointer = Count;
+       return Items;
 }