#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;
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;
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 */
};
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; }
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))
if (fdisk_is_disklabel(cxt, SUN))
fdisk_sun_set_pcylcount(cxt);
break;
- default:
- print_fdisk_menu(cxt);
- break;
}
}
}
}
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) &&
case 'l':
list_partition_types(cxt);
break;
- case 'm':
- print_fdisk_menu(cxt);
- break;
case 'n':
new_partition(cxt);
break;
break;
default:
unknown_command(c);
- print_fdisk_menu(cxt);
}
}
}
};
+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__)));