From 5070bb1ff03d7a666cc4cb6bf94b5e9e3107603e Mon Sep 17 00:00:00 2001 From: dteske Date: Sun, 3 Mar 2013 18:59:59 +0000 Subject: [PATCH] MFC r227934: Port to new libdialog. git-svn-id: svn://svn.freebsd.org/base/stable/9@247722 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- usr.sbin/tzsetup/Makefile | 6 +- usr.sbin/tzsetup/tzsetup.c | 204 +++++++++++++++++++++++++++++-------- 2 files changed, 164 insertions(+), 46 deletions(-) diff --git a/usr.sbin/tzsetup/Makefile b/usr.sbin/tzsetup/Makefile index 4bf9dd75a..4ef65331c 100644 --- a/usr.sbin/tzsetup/Makefile +++ b/usr.sbin/tzsetup/Makefile @@ -3,11 +3,11 @@ PROG= tzsetup MAN= tzsetup.8 -CFLAGS+= -I${.CURDIR}/../../gnu/lib/libodialog -I. +CFLAGS+= -I${.CURDIR}/../../contrib/dialog -I. WARNS?= 3 -DPADD= ${LIBODIALOG} ${LIBNCURSES} -LDADD= -lodialog -lncurses +DPADD= ${LIBDIALOG} ${LIBNCURSESW} ${LIBM} +LDADD= -ldialog -lncursesw -lm .include diff --git a/usr.sbin/tzsetup/tzsetup.c b/usr.sbin/tzsetup/tzsetup.c index d5ff2b1de..820b3c6b0 100644 --- a/usr.sbin/tzsetup/tzsetup.c +++ b/usr.sbin/tzsetup/tzsetup.c @@ -35,7 +35,6 @@ #include __FBSDID("$FreeBSD$"); -#include #include #include #include @@ -49,6 +48,8 @@ __FBSDID("$FreeBSD$"); #include #include +#include + #define _PATH_ZONETAB "/usr/share/zoneinfo/zone.tab" #define _PATH_ISO3166 "/usr/share/misc/iso3166" #define _PATH_ZONEINFO "/usr/share/zoneinfo" @@ -63,8 +64,130 @@ __FBSDID("$FreeBSD$"); #define SILLY_BUFFER_SIZE 2048 #endif +/* special return codes for `fire' actions */ +#define DITEM_FAILURE 1 + +/* flags - returned in upper 16 bits of return status */ +#define DITEM_LEAVE_MENU (1 << 16) +#define DITEM_RECREATE (1 << 18) + +/* for use in describing more exotic behaviors */ +typedef struct dialogMenuItem { + char *prompt; + char *title; + int (*fire)(struct dialogMenuItem *self); + void *data; +} dialogMenuItem; + +static int +xdialog_count_rows(const char *p) +{ + int rows = 0; + + while ((p = strchr(p, '\n')) != NULL) { + p++; + if (*p == '\0') + break; + rows++; + } + + return rows ? rows : 1; +} + +static int +xdialog_count_columns(const char *p) +{ + int len; + int max_len = 0; + const char *q; + + for (; (q = strchr(p, '\n')) != NULL; p = q + 1) { + len = q - p; + max_len = MAX(max_len, len); + } + + len = strlen(p); + max_len = MAX(max_len, len); + return max_len; +} + +static int +xdialog_menu(const char *title, const char *cprompt, int height, int width, + int menu_height, int item_no, dialogMenuItem *ditems) +{ + int i, result, choice = 0; + DIALOG_LISTITEM *listitems; + DIALOG_VARS save_vars; + + dlg_save_vars(&save_vars); + + /* initialize list items */ + listitems = dlg_calloc(DIALOG_LISTITEM, item_no + 1); + assert_ptr(listitems, "xdialog_menu"); + for (i = 0; i < item_no; i++) { + listitems[i].name = ditems[i].prompt; + listitems[i].text = ditems[i].title; + } + + /* calculate height */ + if (height < 0) + height = xdialog_count_rows(cprompt) + menu_height + 4 + 2; + if (height > LINES) + height = LINES; + + /* calculate width */ + if (width < 0) { + int tag_x = 0; + + for (i = 0; i < item_no; i++) { + int j, l; + + l = strlen(listitems[i].name); + for (j = 0; j < item_no; j++) { + int k = strlen(listitems[j].text); + tag_x = MAX(tag_x, l + k + 2); + } + } + width = MAX(xdialog_count_columns(cprompt), title != NULL ? xdialog_count_columns(title) : 0); + width = MAX(width, tag_x + 4) + 4; + } + width = MAX(width, 24); + if (width > COLS) + width = COLS; + +again: + dialog_vars.default_item = listitems[choice].name; + result = dlg_menu(title, cprompt, height, width, + menu_height, item_no, listitems, &choice, NULL); + switch (result) { + case DLG_EXIT_ESC: + result = -1; + break; + case DLG_EXIT_OK: + if (ditems[choice].fire != NULL) { + int status; + + status = ditems[choice].fire(ditems + choice); + if (status & DITEM_RECREATE) { + dlg_clear(); + goto again; + } + } + result = 0; + break; + case DLG_EXIT_CANCEL: + default: + result = 1; + break; + } + + free(listitems); + dlg_restore_vars(&save_vars); + return result; +} + static char path_zonetab[MAXPATHLEN], path_iso3166[MAXPATHLEN], - path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN], + path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN], path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN]; static int reallydoit = 1; @@ -84,8 +207,6 @@ static int set_zone_utc(void); struct continent { dialogMenuItem *menu; int nitems; - int ch; - int sc; }; static struct continent africa, america, antarctica, arctic, asia, atlantic; @@ -105,7 +226,7 @@ static struct continent_names { { "Europe", &europe }, { "Indian", &indian }, { "Pacific", &pacific }, - { "UTC", &utc } + { "UTC", &utc } }; static struct continent_items { @@ -141,7 +262,7 @@ continent_country_menu(dialogMenuItem *continent) int rv; if (strcmp(continent->title, "UTC") == 0) - return set_zone_utc(); + return set_zone_utc(); /* Short cut -- if there's only one country, don't post a menu. */ if (contp->nitems == 1) @@ -159,8 +280,8 @@ continent_country_menu(dialogMenuItem *continent) } menulen = contp->nitems < 16 ? contp->nitems : 16; - rv = dialog_menu(title, prompt, -1, -1, menulen, -contp->nitems, - contp->menu, 0, &contp->ch, &contp->sc); + rv = xdialog_menu(title, prompt, -1, -1, menulen, contp->nitems, + contp->menu); if (rv == 0) return (DITEM_LEAVE_MENU); return (DITEM_RECREATE); @@ -273,7 +394,7 @@ add_zone_to_country(int lineno, const char *tlc, const char *descr, if (tlc[0] < 'A' || tlc[0] > 'Z' || tlc[1] < 'A' || tlc[1] > 'Z') errx(1, "%s:%d: country code `%s' invalid", path_zonetab, lineno, tlc); - + cp = &countries[CODE2INT(tlc)]; if (cp->name == 0) errx(1, "%s:%d: country code `%s' unknown", path_zonetab, @@ -287,7 +408,7 @@ add_zone_to_country(int lineno, const char *tlc, const char *descr, zp = malloc(sizeof(*zp)); if (zp == 0) errx(1, "malloc(%zu)", sizeof(*zp)); - + if (cp->nzones == 0) TAILQ_INIT(&cp->zones); @@ -458,9 +579,7 @@ make_menus(void) memset(dmi, 0, sizeof(*dmi)); asprintf(&dmi->prompt, "%d", ++cp->continent->nitems); dmi->title = cp->name; - dmi->checked = 0; dmi->fire = set_zone_whole_country; - dmi->selected = 0; dmi->data = cp; } else { cp->submenu = malloc(cp->nzones * sizeof(*dmi)); @@ -473,9 +592,7 @@ make_menus(void) memset(dmi, 0, sizeof(*dmi)); asprintf(&dmi->prompt, "%d", ++cp->nzones); dmi->title = zp->descr; - dmi->checked = 0; dmi->fire = set_zone_multi; - dmi->selected = 0; dmi->data = zp; for (zp2 = TAILQ_FIRST(&cp->zones); @@ -489,9 +606,7 @@ make_menus(void) memset(dmi, 0, sizeof(*dmi)); asprintf(&dmi->prompt, "%d", ++cont->nitems); dmi->title = cp->name; - dmi->checked = 0; dmi->fire = set_zone_menu; - dmi->selected = 0; dmi->data = cp; } } @@ -510,8 +625,8 @@ set_zone_menu(dialogMenuItem *dmi) snprintf(prompt, sizeof(prompt), "Select a zone which observes the same time as your locality."); menulen = cp->nzones < 16 ? cp->nzones : 16; - rv = dialog_menu(title, prompt, -1, -1, menulen, -cp->nzones, - cp->submenu, 0, 0, 0); + rv = xdialog_menu(title, prompt, -1, -1, menulen, cp->nzones, + cp->submenu); if (rv != 0) return (DITEM_RECREATE); return (DITEM_LEAVE_MENU); @@ -522,7 +637,7 @@ set_zone_utc(void) { if (!confirm_zone(NULL)) return (DITEM_FAILURE | DITEM_RECREATE); - + return (install_zoneinfo_file(NULL)); } @@ -566,7 +681,7 @@ install_zoneinfo_file(const char *zoneinfo_file) "Could not delete %s: %s", path_localtime, strerror(errno)); if (usedialog) - dialog_mesgbox(title, prompt, 8, 72); + dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); @@ -578,7 +693,7 @@ install_zoneinfo_file(const char *zoneinfo_file) "Could not delete %s: %s", path_db, strerror(errno)); if (usedialog) - dialog_mesgbox(title, prompt, 8, 72); + dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); @@ -586,7 +701,7 @@ install_zoneinfo_file(const char *zoneinfo_file) } return (DITEM_LEAVE_MENU); } - + if (copymode) { fd1 = open(zoneinfo_file, O_RDONLY, 0); if (fd1 < 0) { @@ -595,7 +710,7 @@ install_zoneinfo_file(const char *zoneinfo_file) "Could not open %s: %s", zoneinfo_file, strerror(errno)); if (usedialog) - dialog_mesgbox(title, prompt, 8, 72); + dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); @@ -621,7 +736,7 @@ install_zoneinfo_file(const char *zoneinfo_file) "Could not open %s: %s", path_localtime, strerror(errno)); if (usedialog) - dialog_mesgbox(title, prompt, 8, 72); + dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); @@ -637,7 +752,7 @@ install_zoneinfo_file(const char *zoneinfo_file) "Error copying %s to %s %s", zoneinfo_file, path_localtime, strerror(errno)); if (usedialog) - dialog_mesgbox(title, prompt, 8, 72); + dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); /* Better to leave none than a corrupt one. */ @@ -653,7 +768,7 @@ install_zoneinfo_file(const char *zoneinfo_file) "Cannot access %s: %s", zoneinfo_file, strerror(errno)); if (usedialog) - dialog_mesgbox(title, prompt, 8, 72); + dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); @@ -676,7 +791,7 @@ install_zoneinfo_file(const char *zoneinfo_file) path_localtime, zoneinfo_file, strerror(errno)); if (usedialog) - dialog_mesgbox(title, prompt, 8, 72); + dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); @@ -695,7 +810,7 @@ install_zoneinfo_file(const char *zoneinfo_file) "Created symbolic link from %s to %s", zoneinfo_file, path_localtime); if (usedialog) - dialog_mesgbox(title, prompt, 8, 72); + dialog_msgbox(title, prompt, 8, 72, 1); else fprintf(stderr, "%s\n", prompt); #endif @@ -729,7 +844,7 @@ confirm_zone(const char *filename) time_t t = time(0); struct tm *tm; int rv; - + setenv("TZ", filename == NULL ? "" : filename, 1); tzset(); tm = localtime(&t); @@ -776,12 +891,6 @@ usage(void) exit(1); } -#if defined(__sparc64__) -#define DIALOG_UTC dialog_yesno -#else -#define DIALOG_UTC dialog_noyes -#endif - int main(int argc, char **argv) { @@ -886,15 +995,24 @@ main(int argc, char **argv) /* FALLTHROUGH */ } - init_dialog(); + init_dialog(stdin, stdout); if (skiputc == 0) { + DIALOG_VARS save_vars; + int yesno; + snprintf(title, sizeof(title), "Select local or UTC (Greenwich Mean Time) clock"); snprintf(prompt, sizeof(prompt), "Is this machine's CMOS clock set to UTC? " "If it is set to local time,\n" "or you don't know, please choose NO here!"); - if (!DIALOG_UTC(title, prompt, 7, 72)) { + dlg_save_vars(&save_vars); +#if !defined(__sparc64__) + dialog_vars.defaultno = TRUE; +#endif + yesno = dialog_yesno(title, prompt, 7, 73); + dlg_restore_vars(&save_vars); + if (!yesno) { if (reallydoit) unlink(path_wall_cmos_clock); } else { @@ -910,7 +1028,7 @@ main(int argc, char **argv) close(fd); } } - dialog_clear_norefresh(); + dlg_clear(); } if (optind == argc - 1) { snprintf(title, sizeof(title), "Default timezone provided"); @@ -918,18 +1036,18 @@ main(int argc, char **argv) "\nUse the default `%s' zone?", argv[optind]); if (!dialog_yesno(title, prompt, 7, 72)) { rv = install_zoneinfo_file(argv[optind]); - dialog_clear(); + dlg_clear(); end_dialog(); exit(rv & ~DITEM_LEAVE_MENU); } - dialog_clear_norefresh(); + dlg_clear(); } snprintf(title, sizeof(title), "Time Zone Selector"); snprintf(prompt, sizeof(prompt), "Select a region"); - dialog_menu(title, prompt, -1, -1, NCONTINENTS, -NCONTINENTS, - continents, 0, NULL, NULL); + xdialog_menu(title, prompt, -1, -1, NCONTINENTS, NCONTINENTS, + continents); - dialog_clear(); + dlg_clear(); end_dialog(); return (0); } -- 2.45.2