]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: use new menu infrastructure to verify keys
authorKarel Zak <kzak@redhat.com>
Tue, 21 May 2013 13:06:11 +0000 (15:06 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 16 Sep 2013 14:46:56 +0000 (16:46 +0200)
 - use generic function to ask for key
 - verify the key against the current menu
 - call menu callback if defined

Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/fdisk-ask.c
fdisks/fdisk-menu.c
fdisks/fdisk.c
fdisks/fdisk.h

index 439552f296b310b68b2b9d1eb965650eb16177a5..0c4e200f14cb12000dccc1af4b26bd9413f3c1fb 100644 (file)
@@ -12,7 +12,7 @@
 
 #include "fdisk.h"
 
-static int get_user_reply(struct fdisk_context *cxt, char *prompt,
+int get_user_reply(struct fdisk_context *cxt, const char *prompt,
                          char *buf, size_t bufsz)
 {
        char *p;
@@ -24,7 +24,7 @@ static int get_user_reply(struct fdisk_context *cxt, char *prompt,
 
                if (!fgets(buf, bufsz, stdin)) {
                        if (fdisk_label_is_changed(cxt->label)) {
-                               fprintf(stderr, _("Do you really want to quit? "));
+                               fprintf(stderr, _("\nDo you really want to quit? "));
 
                                if (fgets(buf, bufsz, stdin) && !rpmatch(buf))
                                        continue;
index 17ecdbab10a8606fafaaa481315545f3d29c9749..997d82b8e91360e507639f165d310251b76d4681 100644 (file)
@@ -25,7 +25,9 @@ struct menu {
        enum fdisk_labeltype    label;          /* only for this label */
        enum fdisk_labeltype    exclude;        /* all labels except this */
 
-       int (*callback)(struct fdisk_context *, struct menu *, int);
+       int (*callback)(struct fdisk_context *,
+                       const struct menu *,
+                       const struct menu_entry *);
 
        struct menu_entry       entries[];      /* NULL terminated array */
 };
@@ -301,6 +303,55 @@ int print_fdisk_menu(struct fdisk_context *cxt)
        return 0;
 }
 
+/* Asks for command, verify the key and perform the command or
+ * returns the command key if no callback for the command is
+ * implemented.
+ *
+ * Returns: <0 on error
+ *           0 on success (the command performed)
+ *          >0 if no callback (then returns the key)
+ */
+int process_fdisk_menu(struct fdisk_context *cxt)
+{
+       const struct menu_entry *ent;
+       const struct menu *menu;
+       int key, rc;
+       const char *prompt;
+       char buf[BUFSIZ];
+
+       if (fdisk_context_display_details(cxt))
+               prompt = _("Expert command (m for help): ");
+       else
+               prompt = _("Command (m for help): ");
+
+       fputc('\n',stdout);
+       rc = get_user_reply(cxt, prompt, buf, sizeof(buf));
+       if (rc)
+               return rc;
+
+       key = buf[0];
+       ent = get_fdisk_menu_entry(cxt, key, &menu);
+       if (!ent) {
+               fdisk_warnx(cxt, _("%c: unknown command"), key);
+               return -EINVAL;
+       }
+
+       DBG(CONTEXT, dbgprint("selected: key=%c, entry='%s'",
+                               key, ent->title));
+       /* hardcoded help */
+       if (key == 'm') {
+               print_fdisk_menu(cxt);
+               return 0;
+
+       /* menu has implemented callback, use it */
+       } else if (menu->callback)
+               return menu->callback(cxt, menu, ent);
+
+       /* no callback, return the key */
+       return key;
+}
+
+
 #ifdef TEST_PROGRAM
 struct fdisk_label *fdisk_new_dos_label(struct fdisk_context *cxt) { return NULL; }
 struct fdisk_label *fdisk_new_bsd_label(struct fdisk_context *cxt) { return NULL; }
index 721799327ce1485085c05d8264ed534a277295a9..eba0026e9bbc5416f9573f2af66ff0065805db5f 100644 (file)
@@ -731,8 +731,13 @@ expert_command_prompt(struct fdisk_context *cxt)
        while(1) {
                assert(cxt->label);
 
-               putchar('\n');
-               c = read_char(cxt, _("Expert command (m for help): "));
+               c = process_fdisk_menu(cxt);
+               if (c <= 0)
+                       continue;
+
+               /* well, process_fdisk_menu() returns commands that
+                * are not yet implemented by menu callbacks. Let's
+                * perform the commands here */
                switch (c) {
                case 'a':
                        if (fdisk_is_disklabel(cxt, SUN))
@@ -829,9 +834,6 @@ expert_command_prompt(struct fdisk_context *cxt)
                        if (fdisk_is_disklabel(cxt, SUN))
                                fdisk_sun_set_pcylcount(cxt);
                        break;
-               default:
-                       print_fdisk_menu(cxt);
-                       break;
                }
        }
 }
@@ -938,11 +940,15 @@ static void command_prompt(struct fdisk_context *cxt)
        }
 
        while (1) {
-
                assert(cxt->label);
 
-               putchar('\n');
-               c = read_char(cxt, _("Command (m for help): "));
+               c = process_fdisk_menu(cxt);
+               if (c <= 0)
+                       continue;
+
+               /* well, process_fdisk_menu() returns commands that
+                * are not yet implemented by menu callbacks. Let's
+                * perform the commands here */
                switch (c) {
                case 'a':
                        if (fdisk_is_disklabel(cxt, DOS) &&
@@ -1005,9 +1011,6 @@ static void command_prompt(struct fdisk_context *cxt)
                case 'l':
                        list_partition_types(cxt);
                        break;
-               case 'm':
-                       print_fdisk_menu(cxt);
-                       break;
                case 'n':
                        new_partition(cxt);
                        break;
@@ -1039,7 +1042,6 @@ static void command_prompt(struct fdisk_context *cxt)
                        break;
                default:
                        unknown_command(c);
-                       print_fdisk_menu(cxt);
                }
        }
 }
index c44dd768fad1dd3fdc9bcb890d93ea2802f7e534..cb7f877b1e6ee98c537ceb97ea19abd7903c59f9 100644 (file)
@@ -67,7 +67,11 @@ enum failure {
 };
 
 
+extern int get_user_reply(struct fdisk_context *cxt,
+                         const char *prompt,
+                         char *buf, size_t bufsz);
 extern int print_fdisk_menu(struct fdisk_context *cxt);
+extern int process_fdisk_menu(struct fdisk_context *cxt);
 
 extern int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask,
                    void *data __attribute__((__unused__)));