Index: usr.sbin/tzsetup/tzsetup.c =================================================================== --- usr.sbin/tzsetup/tzsetup.c +++ usr.sbin/tzsetup/tzsetup.c @@ -50,7 +50,7 @@ #include #ifdef HAVE_DIALOG -#include +#include #endif #define _PATH_ZONETAB "/usr/share/zoneinfo/zone1970.tab" @@ -78,6 +78,7 @@ path_zoneinfo[MAXPATHLEN], path_localtime[MAXPATHLEN], path_db[MAXPATHLEN], path_wall_cmos_clock[MAXPATHLEN]; +static struct bsddialog_conf conf; static int reallydoit = 1; static int reinstall = 0; static char *chrootenv = NULL; @@ -128,28 +129,27 @@ } static int -xdialog_menu(const char *title, const char *cprompt, int height, int width, +xdialog_menu(char *title, 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); + struct bsddialog_menuitem *listitems; /* initialize list items */ - listitems = dlg_calloc(DIALOG_LISTITEM, item_no + 1); - assert_ptr(listitems, "xdialog_menu"); + listitems = calloc(item_no + 1, sizeof(listitems)); + if (listitems == NULL) + errx(1, "Failed to allocate memory in xdialog_menu"); for (i = 0; i < item_no; i++) { + listitems[i].prefix = ""; listitems[i].name = ditems[i].prompt; - listitems[i].text = ditems[i].title; + listitems[i].desc = ditems[i].title; } /* calculate height */ if (height < 0) height = xdialog_count_rows(cprompt) + menu_height + 4 + 2; - if (height > LINES) - height = LINES; + if (height > terminalheight()) + height = terminalheight(); /* calculate width */ if (width < 0) { @@ -160,7 +160,7 @@ l = strlen(listitems[i].name); for (j = 0; j < item_no; j++) { - int k = strlen(listitems[j].text); + int k = strlen(listitems[j].desc); tag_x = MAX(tag_x, l + k + 2); } } @@ -169,37 +169,39 @@ width = MAX(width, tag_x + 4) + 4; } width = MAX(width, 24); - if (width > COLS) - width = COLS; + if (width > terminalwidth()) + width = terminalwidth(); again: - dialog_vars.default_item = listitems[choice].name; - result = dlg_menu(title, cprompt, height, width, - menu_height, item_no, listitems, &choice, NULL); + conf.menu.default_item = listitems[choice].name; + conf.title = title; + result = bsddialog_menu(conf, cprompt, height, width, + menu_height, item_no, listitems, NULL); + for (int i = 0; i < item_no; i++) + if (listitems[i].on == 'X') + choice = i; switch (result) { - case DLG_EXIT_ESC: + case BSDDIALOG_ESC: result = -1; break; - case DLG_EXIT_OK: + case BSDDIALOG_YESOK: 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: + case BSDDIALOG_NOCANCEL: default: result = 1; break; } free(listitems); - dlg_restore_vars(&save_vars); return (result); } @@ -657,7 +659,7 @@ static int confirm_zone(const char *filename) { - char title[64], prompt[64]; + char prompt[64]; time_t t = time(0); struct tm *tm; int rv; @@ -666,10 +668,10 @@ tzset(); tm = localtime(&t); - snprintf(title, sizeof(title), "Confirmation"); snprintf(prompt, sizeof(prompt), "Does the abbreviation `%s' look reasonable?", tm->tm_zone); - rv = !dialog_yesno(title, prompt, 5, 72); + conf.title = "Confirmation"; + rv = !bsddialog_yesno(conf, prompt, 5, 72); return (rv); } @@ -705,7 +707,7 @@ install_zoneinfo_file(const char *zoneinfo_file) { char buf[1024]; - char title[64], prompt[SILLY_BUFFER_SIZE]; + char prompt[SILLY_BUFFER_SIZE]; struct stat sb; ssize_t len; int fd1, fd2, copymode; @@ -719,7 +721,6 @@ copymode = 1; #ifdef VERBOSE - snprintf(title, sizeof(title), "Info"); if (copymode) snprintf(prompt, sizeof(prompt), "Copying %s to %s", zoneinfo_file, path_localtime); @@ -729,7 +730,8 @@ path_localtime, zoneinfo_file); #ifdef HAVE_DIALOG if (usedialog) - dialog_msgbox(title, prompt, 8, 72, 1); + conf.title = "Info"; + bsddialog_msgbox(conf, prompt, 8, 72); else #endif fprintf(stderr, "%s\n", prompt); @@ -739,14 +741,14 @@ if (copymode) { fd1 = open(zoneinfo_file, O_RDONLY, 0); if (fd1 < 0) { - snprintf(title, sizeof(title), "Error"); snprintf(prompt, sizeof(prompt), "Could not open %s: %s", zoneinfo_file, strerror(errno)); #ifdef HAVE_DIALOG - if (usedialog) - dialog_msgbox(title, prompt, 8, 72, 1); - else + if (usedialog) { + conf.title = "Error"; + bsddialog_msgbox(conf, prompt, 8, 72); + } else #endif fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); @@ -758,8 +760,8 @@ path_localtime, strerror(errno)); #ifdef HAVE_DIALOG if (usedialog) { - snprintf(title, sizeof(title), "Error"); - dialog_msgbox(title, prompt, 8, 72, 1); + conf.title = "error"; + bsddialog_msgbox(conf, prompt, 8, 72); } else #endif fprintf(stderr, "%s\n", prompt); @@ -769,14 +771,14 @@ fd2 = open(path_localtime, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IRGRP | S_IROTH); if (fd2 < 0) { - snprintf(title, sizeof(title), "Error"); snprintf(prompt, sizeof(prompt), "Could not open %s: %s", path_localtime, strerror(errno)); #ifdef HAVE_DIALOG - if (usedialog) - dialog_msgbox(title, prompt, 8, 72, 1); - else + if (usedialog) { + conf.title = "Error"; + bsddialog_msgbox(conf, prompt, 8, 72); + } else #endif fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); @@ -787,14 +789,14 @@ break; if (len == -1) { - snprintf(title, sizeof(title), "Error"); snprintf(prompt, sizeof(prompt), "Error copying %s to %s %s", zoneinfo_file, path_localtime, strerror(errno)); #ifdef HAVE_DIALOG - if (usedialog) - dialog_msgbox(title, prompt, 8, 72, 1); - else + if (usedialog) { + conf.title = "Error"; + bsddialog_msgbox(conf, prompt, 8, 72); + } else #endif fprintf(stderr, "%s\n", prompt); /* Better to leave none than a corrupt one. */ @@ -805,14 +807,14 @@ close(fd2); } else { if (access(zoneinfo_file, R_OK) != 0) { - snprintf(title, sizeof(title), "Error"); snprintf(prompt, sizeof(prompt), "Cannot access %s: %s", zoneinfo_file, strerror(errno)); #ifdef HAVE_DIALOG - if (usedialog) - dialog_msgbox(title, prompt, 8, 72, 1); - else + if (usedialog) { + conf.title = "Error"; + bsddialog_msgbox(conf, prompt, 8, 72); + } else #endif fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); @@ -823,23 +825,23 @@ path_localtime, strerror(errno)); #ifdef HAVE_DIALOG if (usedialog) { - snprintf(title, sizeof(title), "Error"); - dialog_msgbox(title, prompt, 8, 72, 1); + conf.title = "Error"; + bsddialog_msgbox(conf, prompt, 8, 72); } else #endif fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); } if (symlink(zoneinfo_file, path_localtime) < 0) { - snprintf(title, sizeof(title), "Error"); snprintf(prompt, sizeof(prompt), "Cannot create symbolic link %s to %s: %s", path_localtime, zoneinfo_file, strerror(errno)); #ifdef HAVE_DIALOG - if (usedialog) - dialog_msgbox(title, prompt, 8, 72, 1); - else + if (usedialog) { + conf.title = "Error"; + bsddialog_msgbox(conf, prompt, 8, 72); + } else #endif fprintf(stderr, "%s\n", prompt); return (DITEM_FAILURE | DITEM_RECREATE); @@ -847,7 +849,6 @@ } #ifdef VERBOSE - snprintf(title, sizeof(title), "Done"); if (copymode) snprintf(prompt, sizeof(prompt), "Copied timezone file from %s to %s", @@ -857,9 +858,10 @@ "Created symbolic link from %s to %s", zoneinfo_file, path_localtime); #ifdef HAVE_DIALOG - if (usedialog) - dialog_msgbox(title, prompt, 8, 72, 1); - else + if (usedialog) { + conf.title = "Done"; + bsddialog_msgbox(conf, prompt, 8, 72); + } else #endif fprintf(stderr, "%s\n", prompt); #endif @@ -904,7 +906,7 @@ main(int argc, char **argv) { #ifdef HAVE_DIALOG - char title[64], prompt[128]; + char prompt[128]; int fd; #endif int c, rv, skiputc; @@ -1008,21 +1010,26 @@ sort_countries(); make_menus(); - init_dialog(stdin, stdout); + memset(&conf, 0, sizeof(struct bsddialog_conf)); + conf.y = conf.x = -1; + conf.shadow = true; + conf.clear = true; + + if (bsddialog_init() < 0) + return (1); + 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!"); - dlg_save_vars(&save_vars); - dialog_vars.defaultno = TRUE; - yesno = dialog_yesno(title, prompt, 7, 73); - dlg_restore_vars(&save_vars); + + conf.button.defaultno = true; + conf.title = "Select local or UTC (Greenwich Mean Time) clock"; + yesno = bsddialog_yesno(conf, prompt, 7, 73); + conf.button.defaultno = false; if (!yesno) { if (reallydoit) unlink(path_wall_cmos_clock); @@ -1032,34 +1039,28 @@ O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH); if (fd < 0) { - end_dialog(); + bsddialog_end(); err(1, "create %s", path_wall_cmos_clock); } close(fd); } } - dlg_clear(); } if (optind == argc - 1) { - snprintf(title, sizeof(title), "Default timezone provided"); snprintf(prompt, sizeof(prompt), "\nUse the default `%s' zone?", argv[optind]); - if (!dialog_yesno(title, prompt, 7, 72)) { + conf.title = "Default timezone provided"; + if (!bsddialog_yesno(conf, prompt, 7, 72)) { rv = install_zoneinfo_file(argv[optind]); - dlg_clear(); - end_dialog(); + bsddialog_end(); exit(rv & ~DITEM_LEAVE_MENU); } - dlg_clear(); } - snprintf(title, sizeof(title), "Time Zone Selector"); - snprintf(prompt, sizeof(prompt), "Select a region"); - xdialog_menu(title, prompt, -1, -1, NCONTINENTS, NCONTINENTS, - continents); + xdialog_menu("Time Zone Selector", "Select a region", -1, -1, + NCONTINENTS, NCONTINENTS, continents); - dlg_clear(); - end_dialog(); + bsddialog_end(); #else usage(); #endif