diff options
author | Karel Zak | 2013-05-21 15:06:11 +0200 |
---|---|---|
committer | Karel Zak | 2013-09-16 16:46:56 +0200 |
commit | a410f8df0f4dcbd32986890111397f0b9309cd51 (patch) | |
tree | 01a32d877455a49f4ce91a22e23cb53f1f3ef9c7 | |
parent | fdisk: use remove read_int() usage from fdisk.c (diff) | |
download | kernel-qcow2-util-linux-a410f8df0f4dcbd32986890111397f0b9309cd51.tar.gz kernel-qcow2-util-linux-a410f8df0f4dcbd32986890111397f0b9309cd51.tar.xz kernel-qcow2-util-linux-a410f8df0f4dcbd32986890111397f0b9309cd51.zip |
fdisk: use new menu infrastructure to verify keys
- 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>
-rw-r--r-- | fdisks/fdisk-ask.c | 4 | ||||
-rw-r--r-- | fdisks/fdisk-menu.c | 53 | ||||
-rw-r--r-- | fdisks/fdisk.c | 26 | ||||
-rw-r--r-- | fdisks/fdisk.h | 4 |
4 files changed, 72 insertions, 15 deletions
diff --git a/fdisks/fdisk-ask.c b/fdisks/fdisk-ask.c index 439552f29..0c4e200f1 100644 --- a/fdisks/fdisk-ask.c +++ b/fdisks/fdisk-ask.c @@ -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; diff --git a/fdisks/fdisk-menu.c b/fdisks/fdisk-menu.c index 17ecdbab1..997d82b8e 100644 --- a/fdisks/fdisk-menu.c +++ b/fdisks/fdisk-menu.c @@ -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; } diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c index 721799327..eba0026e9 100644 --- a/fdisks/fdisk.c +++ b/fdisks/fdisk.c @@ -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); } } } diff --git a/fdisks/fdisk.h b/fdisks/fdisk.h index c44dd768f..cb7f877b1 100644 --- a/fdisks/fdisk.h +++ b/fdisks/fdisk.h @@ -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__))); |