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>
This commit is contained in:
parent
74fcee985d
commit
a410f8df0f
|
@ -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;
|
||||
|
|
|
@ -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; }
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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__)));
|
||||
|
|
Loading…
Reference in New Issue