win32: Use libinstaller option parser
authorShao Miller <shao.miller@yrdsb.edu.on.ca>
Sat, 3 Jul 2010 20:23:44 +0000 (16:23 -0400)
committerShao Miller <shao.miller@yrdsb.edu.on.ca>
Sat, 3 Jul 2010 21:03:00 +0000 (17:03 -0400)
We will produce an error message if a user attempts
to use an option we don't implement.

Signed-off-by: Shao Miller <shao.miller@yrdsb.edu.on.ca>
libinstaller/syslxopt.c
libinstaller/syslxopt.h
win32/Makefile [changed mode: 0644->0755]
win32/sysexits.h [new file with mode: 0644]
win32/syslinux.c [changed mode: 0644->0755]

index 8aee160..e7f405a 100755 (executable)
@@ -43,6 +43,7 @@ struct sys_options opt = {
     .install_mbr = 0,
     .activate_partition = 0,
     .force = 0,
+    .bootsecfile = NULL,
 };
 
 const struct option long_options[] = {
@@ -88,6 +89,13 @@ void __attribute__ ((noreturn)) usage(int rv, enum syslinux_mode mode)
            "Usage: %s [options] directory\n",
            program);
        break;
+
+    case MODE_SYSLINUX_DOSWIN:
+       /* For fs installation under Windows (syslinux.exe) */
+       fprintf(stderr,
+           "Usage: %s [options] <drive>: [bootsecfile]\n",
+           program);
+       break;
     }
 
     fprintf(stderr,
@@ -113,7 +121,7 @@ void __attribute__ ((noreturn)) usage(int rv, enum syslinux_mode mode)
            "\n"
            "  The -z option is useful for USB devices which are considered\n"
            "  hard disks by some BIOSes and zipdrives by other BIOSes.\n",
-           mode == MODE_SYSLINUX ? "  " : "-o");
+           mode == MODE_SYSLINUX  ? "  " : "-o");
 
     exit(rv);
 }
@@ -211,6 +219,7 @@ void parse_options(int argc, char *argv[], enum syslinux_mode mode)
 
     switch (mode) {
     case MODE_SYSLINUX:
+    case MODE_SYSLINUX_DOSWIN:
        opt.device = argv[optind++];
        break;
     case MODE_EXTLINUX:
@@ -219,6 +228,9 @@ void parse_options(int argc, char *argv[], enum syslinux_mode mode)
        break;
     }
 
+    if (argv[optind] && (mode == MODE_SYSLINUX_DOSWIN))
+       /* Allow for the boot-sector argument */
+       opt.bootsecfile = argv[optind++];
     if (argv[optind])
        usage(EX_USAGE, mode);  /* Excess arguments */
 }
index 6fdf1d9..bcbe035 100755 (executable)
@@ -17,6 +17,7 @@ struct sys_options {
     int force;
     int install_mbr;
     int activate_partition;
+    const char *bootsecfile;
 };
 
 enum long_only_opt {
@@ -28,6 +29,7 @@ enum long_only_opt {
 enum syslinux_mode {
     MODE_SYSLINUX,             /* Unmounted filesystem */
     MODE_EXTLINUX,
+    MODE_SYSLINUX_DOSWIN,
 };
 
 void __attribute__ ((noreturn)) usage(int rv, enum syslinux_mode mode);
old mode 100644 (file)
new mode 100755 (executable)
index a077b30..81a6e9e
@@ -49,6 +49,7 @@ SRCS     = syslinux.c
 OBJS     = $(patsubst %.c,%.obj,$(notdir $(SRCS)))
 LIBSRC   = ../libinstaller/fat.c \
           ../libinstaller/syslxmod.c \
+          ../libinstaller/syslxopt.c \
           ../libinstaller/setadv.c \
           ../libinstaller/bootsect_bin.c \
           ../libinstaller/ldlinux_bin.c \
diff --git a/win32/sysexits.h b/win32/sysexits.h
new file mode 100644 (file)
index 0000000..483d3ba
--- /dev/null
@@ -0,0 +1 @@
+#define EX_USAGE 0x40
old mode 100644 (file)
new mode 100755 (executable)
index ca98d78..9459385
 #include <windows.h>
 #include <stdio.h>
 #include <ctype.h>
+#include <getopt.h>
 
 #include "syslinux.h"
 #include "libfat.h"
 #include "setadv.h"
+#include "sysexits.h"
+#include "syslxopt.h"
 
 #ifdef __GNUC__
 # define noreturn void __attribute__((noreturn))
@@ -168,7 +171,6 @@ BOOL FixMBR(int driveNum, int partitionNum, int write_mbr, int set_active)
 /* End stuff for MBR code */
 
 const char *program;           /* Name of program */
-const char *drive;             /* Drive to install to */
 
 /*
  * Check Windows version.
@@ -229,13 +231,6 @@ int libfat_readfile(intptr_t pp, void *buf, size_t secsize,
     return secsize;
 }
 
-noreturn usage(void)
-{
-    fprintf(stderr,
-           "Usage: syslinux.exe [-sfmar][-d directory] <drive>: [bootsecfile]\n");
-    exit(1);
-}
-
 int main(int argc, char *argv[])
 {
     HANDLE f_handle, d_handle;
@@ -245,7 +240,7 @@ int main(int argc, char *argv[])
     UINT drive_type;
 
     static unsigned char sectbuf[SECTOR_SIZE];
-    char **argp, *opt;
+    char **argp;
     static char drive_name[] = "\\\\.\\?:";
     static char drive_root[] = "?:\\";
     static char ldlinux_name[] = "?:\\ldlinux.sys";
@@ -256,14 +251,6 @@ int main(int argc, char *argv[])
     int ldlinux_sectors;
     uint32_t ldlinux_cluster;
     int nsectors;
-    const char *bootsecfile = NULL;
-    const char *subdir = NULL;
-
-    int force = 0;             /* -f (force) option */
-    int mbr = 0;               /* -m (MBR) option */
-    int setactive = 0;         /* -a (set partition active) */
-    int stupid = 0;            /* -s (stupid) option */
-    int raid_mode = 0;         /* -r (RAID) option */
 
     (void)argc;
 
@@ -274,69 +261,36 @@ int main(int argc, char *argv[])
     }
 
     program = argv[0];
-    drive = NULL;
-
-    for (argp = argv + 1; *argp; argp++) {
-       if (**argp == '-') {
-           opt = *argp + 1;
-           if (!*opt)
-               usage();
-
-           while (*opt) {
-               switch (*opt) {
-               case 's':       /* Use "safe, slow and stupid" code */
-                   stupid = 1;
-                   break;
-               case 'r':       /* RAID mode */
-                   raid_mode = 1;
-                   break;
-               case 'f':       /* Force install */
-                   force = 1;
-                   break;
-               case 'm':       /* Install MBR */
-                   mbr = 1;
-                   break;
-               case 'a':       /* Mark this partition active */
-                   setactive = 1;
-                   break;
-               case 'd':
-                   if (argp[1])
-                       subdir = *++argp;
-                   break;
-               default:
-                   usage();
-                   break;
-               }
-               opt++;
-           }
-       } else {
-           if (bootsecfile)
-               usage();
-           else if (drive)
-               bootsecfile = *argp;
-           else
-               drive = *argp;
-       }
-    }
 
-    if (!drive || !isalpha(drive[0]) || drive[1] != ':' || drive[2])
-       usage();
+    parse_options(argc, argv, MODE_SYSLINUX_DOSWIN);
+
+    if (!opt.device || !isalpha(opt.device[0]) || opt.device[1] != ':'
+       || opt.device[2])
+       usage(EX_USAGE, MODE_SYSLINUX_DOSWIN);
+
+    if (opt.sectors || opt.heads || opt.reset_adv || opt.set_once
+       || (opt.update_only > 0) || opt.menu_save || opt.offset) {
+       fprintf(stderr,
+               "At least one specified option not yet implemented"
+               " for this installer.\n");
+       exit(1);
+    }
 
     /* Test if drive exists */
     drives = GetLogicalDrives();
-    if (!(drives & (1 << (tolower(drive[0]) - 'a')))) {
-       fprintf(stderr, "No such drive %c:\n", drive[0]);
+    if (!(drives & (1 << (tolower(opt.device[0]) - 'a')))) {
+       fprintf(stderr, "No such drive %c:\n", opt.device[0]);
        exit(1);
     }
 
     /* Determines the drive type */
-    drive_name[4] = drive[0];
-    ldlinux_name[0] = drive[0];
-    drive_root[0] = drive[0];
+    drive_name[4] = opt.device[0];
+    ldlinux_name[0] = opt.device[0];
+    drive_root[0] = opt.device[0];
     drive_type = GetDriveType(drive_root);
 
     /* Test for removeable media */
-    if ((drive_type == DRIVE_FIXED) && (force == 0)) {
+    if ((drive_type == DRIVE_FIXED) && (opt.force == 0)) {
        fprintf(stderr, "Not a removable drive (use -f to override) \n");
        exit(1);
     }
@@ -439,7 +393,7 @@ int main(int argc, char *argv[])
     /*
      * Patch ldlinux.sys and the boot sector
      */
-    syslinux_patch(sectors, nsectors, stupid, raid_mode, subdir, NULL);
+    syslinux_patch(sectors, nsectors, opt.stupid_mode, opt.raid_mode, opt.directory, NULL);
 
     /*
      * Rewrite the file
@@ -453,10 +407,10 @@ int main(int argc, char *argv[])
     }
 
     /* If desired, fix the MBR */
-    if (mbr || setactive) {
+    if (opt.install_mbr || opt.activate_partition) {
        STORAGE_DEVICE_NUMBER sdn;
        if (GetStorageDeviceNumberByHandle(d_handle, &sdn)) {
-           if (!FixMBR(sdn.DeviceNumber, sdn.PartitionNumber, mbr, setactive)) {
+           if (!FixMBR(sdn.DeviceNumber, sdn.PartitionNumber, opt.install_mbr, opt.activate_partition)) {
                fprintf(stderr,
                        "Did not successfully update the MBR; continuing...\n");
            }
@@ -470,17 +424,17 @@ int main(int argc, char *argv[])
     CloseHandle(f_handle);
 
     /* Move the file to the desired location */
-    if (subdir) {
-       char new_ldlinux_name[strlen(subdir) + 16];
+    if (opt.directory) {
+       char new_ldlinux_name[strlen(opt.directory) + 16];
        char *cp = new_ldlinux_name + 3;
        const char *sd;
        int slash = 1;
 
-       new_ldlinux_name[0] = drive[0];
+       new_ldlinux_name[0] = opt.device[0];
        new_ldlinux_name[1] = ':';
        new_ldlinux_name[2] = '\\';
 
-       for (sd = subdir; *sd; sd++) {
+       for (sd = opt.directory; *sd; sd++) {
            char c = *sd;
 
            if (c == '/' || c == '\\') {
@@ -520,8 +474,8 @@ int main(int argc, char *argv[])
     syslinux_make_bootsect(sectbuf);
 
     /* Write the syslinux boot sector into the boot sector */
-    if (bootsecfile) {
-       f_handle = CreateFile(bootsecfile, GENERIC_READ | GENERIC_WRITE,
+    if (opt.bootsecfile) {
+       f_handle = CreateFile(opt.bootsecfile, GENERIC_READ | GENERIC_WRITE,
                              FILE_SHARE_READ | FILE_SHARE_WRITE,
                              NULL, CREATE_ALWAYS,
                              FILE_ATTRIBUTE_ARCHIVE, NULL);