From: Jeremy Kerr Date: Tue, 24 Jun 2014 05:18:03 +0000 (+0800) Subject: ui/ncurses: Use a separate type for help text X-Git-Tag: v1.0.0~148 X-Git-Url: https://git.ozlabs.org/?p=petitboot;a=commitdiff_plain;h=4e30f260106ac5f7007e213db1d1e54298393290 ui/ncurses: Use a separate type for help text Because it's initialised statically, help text won't be directly gettext()-ed. Instead, we need to perform the gettext translation at runtime, and pass untranslated strings into the help_screen code. Instead of trusting callers to pass the untranslated strings though, we encapsulate the help text data into struct help_text, so we know we have an unstranslated string. Signed-off-by: Jeremy Kerr --- diff --git a/ui/ncurses/generic-main-help.c b/ui/ncurses/generic-main-help.c index d3c5662..ac690c5 100644 --- a/ui/ncurses/generic-main-help.c +++ b/ui/ncurses/generic-main-help.c @@ -1,5 +1,7 @@ -const char *main_menu_help_text = +#include "nc-helpscreen.h" + +struct help_text main_menu_help_text = define_help_text( "From the main menu screen, select a boot option. The options displayed are \ available on the system and the network.\n\ \n\ @@ -14,4 +16,5 @@ network interface, type I (information).\n\ \n\ To make changes to the system configuration, type C (configure).\n\ \n\ -To close the Petitboot interface, type X (exit).\n"; +To close the Petitboot interface, type X (exit).\n" +); diff --git a/ui/ncurses/generic-main.c b/ui/ncurses/generic-main.c index abf3356..f253469 100644 --- a/ui/ncurses/generic-main.c +++ b/ui/ncurses/generic-main.c @@ -40,7 +40,7 @@ #include "ui/common/discover-client.h" #include "nc-cui.h" -extern const char *main_menu_help_text; +extern const struct help_text main_menu_help_text; static void print_version(void) { @@ -234,7 +234,7 @@ static struct pmenu *pb_mm_init(struct pb_cui *pb_cui) } m->help_title = _("main menu"); - m->help_text = main_menu_help_text; + m->help_text = &main_menu_help_text; menu_opts_off(m->ncm, O_SHOWDESC); set_menu_mark(m->ncm, " *"); diff --git a/ui/ncurses/nc-boot-editor-help.c b/ui/ncurses/nc-boot-editor-help.c index 3f0c7fb..4c0ba24 100644 --- a/ui/ncurses/nc-boot-editor-help.c +++ b/ui/ncurses/nc-boot-editor-help.c @@ -1,4 +1,6 @@ -const char *boot_editor_help_text = "\ +#include "nc-helpscreen.h" + +struct help_text boot_editor_help_text = define_help_text("\ This screen allows you to edit or create boot options.\n\ \n\ Device: This is a list of block devices available on the system. Select \ @@ -21,4 +23,4 @@ Example: /boot/device-tree.dtb\n\ \n\ Boot arguments: enter the kernel command-line arguments. This is optional.\n\ Example: root=/dev/sda1 console=hvc0\n\ -\n"; +\n"); diff --git a/ui/ncurses/nc-boot-editor.c b/ui/ncurses/nc-boot-editor.c index 6bc1d89..6da3630 100644 --- a/ui/ncurses/nc-boot-editor.c +++ b/ui/ncurses/nc-boot-editor.c @@ -73,7 +73,7 @@ struct boot_editor { char *args; }; -extern const char *boot_editor_help_text; +extern const struct help_text boot_editor_help_text; static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr) { @@ -222,7 +222,7 @@ static void boot_editor_process_key(struct nc_scr *scr, int key) case STATE_HELP: boot_editor->state = STATE_EDIT; cui_show_help(boot_editor->cui, _("Boot Option Editor"), - boot_editor_help_text); + &boot_editor_help_text); break; default: break; diff --git a/ui/ncurses/nc-config-help.c b/ui/ncurses/nc-config-help.c index 828fce7..18c51d4 100644 --- a/ui/ncurses/nc-config-help.c +++ b/ui/ncurses/nc-config-help.c @@ -1,4 +1,6 @@ -const char *config_help_text = "\ +#include "nc-helpscreen.h" + +struct help_text config_help_text = define_help_text("\ Autoboot: There are three possible options for automatic-boot hehaviour:\n" "\n" "Don't autoboot: boot options will be listed in the petitboot menu, but none \ @@ -35,5 +37,4 @@ only want to configure a single interface during boot.\n" "Static IP configuration: Allows you to specify an IPv4 address and network \ mask, gateway, and a DNS server or servers for a network interface. Select \ this option if you do not have a DHCP server, or want explicit control of \ -network settings." -; +network settings."); diff --git a/ui/ncurses/nc-config.c b/ui/ncurses/nc-config.c index 54eb7ac..a095353 100644 --- a/ui/ncurses/nc-config.c +++ b/ui/ncurses/nc-config.c @@ -35,7 +35,7 @@ #define N_FIELDS 25 -extern const char *config_help_text; +extern struct help_text config_help_text; enum autoboot_type { AUTOBOOT_ANY, @@ -146,7 +146,7 @@ static void config_screen_process_key(struct nc_scr *scr, int key) } else if (screen->show_help) { screen->show_help = false; cui_show_help(screen->cui, _("System Configuration"), - config_help_text); + &config_help_text); } else if (handled) { pad_refresh(screen); diff --git a/ui/ncurses/nc-cui.c b/ui/ncurses/nc-cui.c index 96d82ca..cd7952f 100644 --- a/ui/ncurses/nc-cui.c +++ b/ui/ncurses/nc-cui.c @@ -270,7 +270,8 @@ static void cui_help_exit(struct cui *cui) cui->help_screen = NULL; } -void cui_show_help(struct cui *cui, const char *title, const char *text) +void cui_show_help(struct cui *cui, const char *title, + const struct help_text *text) { if (!cui->current) return; diff --git a/ui/ncurses/nc-cui.h b/ui/ncurses/nc-cui.h index 8632806..e9e4d38 100644 --- a/ui/ncurses/nc-cui.h +++ b/ui/ncurses/nc-cui.h @@ -23,6 +23,7 @@ #include "ui/common/joystick.h" #include "nc-menu.h" +#include "nc-helpscreen.h" struct cui_opt_data { const char *name; @@ -75,7 +76,8 @@ void cui_item_edit(struct pmenu_item *item); void cui_item_new(struct pmenu *menu); void cui_show_sysinfo(struct cui *cui); void cui_show_config(struct cui *cui); -void cui_show_help(struct cui *cui, const char *title, const char *text); +void cui_show_help(struct cui *cui, const char *title, + const struct help_text *text); int cui_send_config(struct cui *cui, struct config *config); void cui_send_reinit(struct cui *cui); diff --git a/ui/ncurses/nc-helpscreen.c b/ui/ncurses/nc-helpscreen.c index 3b27d57..fd76a43 100644 --- a/ui/ncurses/nc-helpscreen.c +++ b/ui/ncurses/nc-helpscreen.c @@ -48,7 +48,8 @@ struct nc_scr *help_screen_return_scr(struct help_screen *screen) struct help_screen *help_screen_init(struct cui *cui, struct nc_scr *current_scr, - const char *title_suffix, const char *text, + const char *title_suffix, + const struct help_text *text, void (*on_exit)(struct cui *)) { struct help_screen *screen; @@ -63,7 +64,7 @@ struct help_screen *help_screen_init(struct cui *cui, _("Petitboot help: %s"), title_suffix); text_screen_init(&screen->text_scr, cui, title, on_exit); - text_screen_set_text(&screen->text_scr, text); + text_screen_set_text(&screen->text_scr, text->text); text_screen_draw(&screen->text_scr); return screen; diff --git a/ui/ncurses/nc-helpscreen.h b/ui/ncurses/nc-helpscreen.h index f104411..99b99f7 100644 --- a/ui/ncurses/nc-helpscreen.h +++ b/ui/ncurses/nc-helpscreen.h @@ -19,13 +19,24 @@ #define _NC_HELPSCREEN_H struct help_screen; +struct cui; + +/* Container struct for type-safety; we need to use gettext() before + * displaying the untranslated string. */ +struct help_text { + const char *text; +}; + +#define define_help_text(s) { .text = s } struct nc_scr *help_screen_scr(struct help_screen *screen); struct nc_scr *help_screen_return_scr(struct help_screen *screen); struct help_screen *help_screen_init(struct cui *cui, struct nc_scr *current_scr, - const char *title_suffix, const char *text, + const char *title_suffix, + const struct help_text *text, void (*on_exit)(struct cui *)); + #endif /* defined _NC_HELPSCREEN_H */ diff --git a/ui/ncurses/nc-menu.h b/ui/ncurses/nc-menu.h index 12eafaf..2f66bbb 100644 --- a/ui/ncurses/nc-menu.h +++ b/ui/ncurses/nc-menu.h @@ -88,7 +88,7 @@ struct pmenu { unsigned int item_count; unsigned int insert_pt; const char *help_title; - const char *help_text; + const struct help_text *help_text; int (*hot_key)(struct pmenu *menu, struct pmenu_item *item, int c); void (*on_exit)(struct pmenu *menu); void (*on_new)(struct pmenu *menu); diff --git a/ui/ncurses/nc-sysinfo.c b/ui/ncurses/nc-sysinfo.c index 0d362e4..f793010 100644 --- a/ui/ncurses/nc-sysinfo.c +++ b/ui/ncurses/nc-sysinfo.c @@ -35,7 +35,7 @@ struct sysinfo_screen { struct text_screen text_scr; }; -extern const char *sysinfo_help_text; +extern const struct help_text sysinfo_help_text; struct nc_scr *sysinfo_screen_scr(struct sysinfo_screen *screen) { @@ -115,7 +115,7 @@ struct sysinfo_screen *sysinfo_screen_init(struct cui *cui, text_screen_init(&screen->text_scr, cui, _("Petitboot System Information"), on_exit); text_screen_set_help(&screen->text_scr, - _("System Information"), sysinfo_help_text); + _("System Information"), &sysinfo_help_text); sysinfo_screen_update(screen, sysinfo); diff --git a/ui/ncurses/nc-textscreen.c b/ui/ncurses/nc-textscreen.c index 4ae0db4..826244c 100644 --- a/ui/ncurses/nc-textscreen.c +++ b/ui/ncurses/nc-textscreen.c @@ -174,7 +174,7 @@ struct nc_scr *text_screen_scr(struct text_screen *screen) } void text_screen_set_help(struct text_screen *screen, const char *title, - const char *text) + const struct help_text *text) { screen->help_title = title; screen->help_text = text; diff --git a/ui/ncurses/nc-textscreen.h b/ui/ncurses/nc-textscreen.h index 72b8c8a..25107cb 100644 --- a/ui/ncurses/nc-textscreen.h +++ b/ui/ncurses/nc-textscreen.h @@ -22,15 +22,15 @@ #include "nc-cui.h" struct text_screen { - struct nc_scr scr; - struct cui *cui; - const char **lines; - int n_lines; - int n_alloc_lines; - int scroll_y; - const char *help_title; - const char *help_text; - void (*on_exit)(struct cui *); + struct nc_scr scr; + struct cui *cui; + const char **lines; + int n_lines; + int n_alloc_lines; + int scroll_y; + const char *help_title; + const struct help_text *help_text; + void (*on_exit)(struct cui *); }; void text_screen_init(struct text_screen *screen, struct cui *cui, @@ -45,7 +45,7 @@ void text_screen_append_line(struct text_screen *screen, const char *fmt, ...) __attribute__((format(printf, 2, 3))); void text_screen_set_text(struct text_screen *screen, const char *text); void text_screen_set_help(struct text_screen *screen, const char *title, - const char *text); + const struct help_text *text); /* interaction */ void text_screen_process_key(struct nc_scr *scr, int key);