svn commit: r227934 - head/usr.sbin/tzsetup

Max Khon fjoe at FreeBSD.org
Thu Nov 24 13:24:07 UTC 2011


Author: fjoe
Date: Thu Nov 24 13:24:06 2011
New Revision: 227934
URL: http://svn.freebsd.org/changeset/base/227934

Log:
  Port to new libdialog.

Modified:
  head/usr.sbin/tzsetup/Makefile
  head/usr.sbin/tzsetup/tzsetup.c

Modified: head/usr.sbin/tzsetup/Makefile
==============================================================================
--- head/usr.sbin/tzsetup/Makefile	Thu Nov 24 13:18:58 2011	(r227933)
+++ head/usr.sbin/tzsetup/Makefile	Thu Nov 24 13:24:06 2011	(r227934)
@@ -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 <bsd.prog.mk>

Modified: head/usr.sbin/tzsetup/tzsetup.c
==============================================================================
--- head/usr.sbin/tzsetup/tzsetup.c	Thu Nov 24 13:18:58 2011	(r227933)
+++ head/usr.sbin/tzsetup/tzsetup.c	Thu Nov 24 13:24:06 2011	(r227934)
@@ -35,7 +35,6 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include <dialog.h>
 #include <err.h>
 #include <errno.h>
 #include <stdio.h>
@@ -49,6 +48,8 @@ __FBSDID("$FreeBSD$");
 #include <sys/queue.h>
 #include <sys/stat.h>
 
+#include <dialog.h>
+
 #define	_PATH_ZONETAB		"/usr/share/zoneinfo/zone.tab"
 #define	_PATH_ISO3166		"/usr/share/misc/iso3166"
 #define	_PATH_ZONEINFO		"/usr/share/zoneinfo"
@@ -56,8 +57,92 @@ __FBSDID("$FreeBSD$");
 #define	_PATH_DB		"/var/db/zoneinfo"
 #define	_PATH_WALL_CMOS_CLOCK	"/etc/wall_cmos_clock"
 
+/* 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_menu(const char *title, const char *cprompt, int height, int width,
+	     int menu_height, int item_no, dialogMenuItem *ditems)
+{
+	int i, result, choice;
+	DIALOG_LISTITEM *listitems;
+	DIALOG_VARS save_vars;
+
+	dlg_save_vars(&save_vars);
+
+	/* initialize list items */
+	listitems = dlg_calloc(DIALOG_LISTITEM, item_no);
+	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 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(dlg_count_columns(cprompt), title != NULL ? dlg_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 = ditems[choice].prompt;
+	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;
@@ -77,8 +162,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;
@@ -98,7 +181,7 @@ static struct continent_names {
 	{ "Europe",	&europe },
 	{ "Indian",	&indian },
 	{ "Pacific",	&pacific },
-	{ "UTC", 	&utc }
+	{ "UTC",	&utc }
 };
 
 static struct continent_items {
@@ -134,7 +217,7 @@ continent_country_menu(dialogMenuItem *c
 	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)
@@ -152,8 +235,8 @@ continent_country_menu(dialogMenuItem *c
 	}
 
 	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);
@@ -266,7 +349,7 @@ add_zone_to_country(int lineno, const ch
 	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,
@@ -280,7 +363,7 @@ add_zone_to_country(int lineno, const ch
 		zp = malloc(sizeof(*zp));
 		if (zp == 0)
 			errx(1, "malloc(%zu)", sizeof(*zp));
-		
+
 		if (cp->nzones == 0)
 			TAILQ_INIT(&cp->zones);
 
@@ -451,9 +534,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));
@@ -466,9 +547,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);
@@ -482,9 +561,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;
 			}
 		}
@@ -503,8 +580,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);
@@ -515,7 +592,7 @@ set_zone_utc(void)
 {
 	if (!confirm_zone(NULL))
 		return (DITEM_FAILURE | DITEM_RECREATE);
-		
+
 	return (install_zoneinfo_file(NULL));
 }
 
@@ -559,7 +636,7 @@ install_zoneinfo_file(const char *zonein
 				     "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);
 
@@ -571,7 +648,7 @@ install_zoneinfo_file(const char *zonein
 				     "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);
 
@@ -579,7 +656,7 @@ install_zoneinfo_file(const char *zonein
 			}
 			return (DITEM_LEAVE_MENU);
 		}
-		
+
 		if (copymode) {
 			fd1 = open(zoneinfo_file, O_RDONLY, 0);
 			if (fd1 < 0) {
@@ -588,7 +665,7 @@ install_zoneinfo_file(const char *zonein
 				    "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);
@@ -603,7 +680,7 @@ install_zoneinfo_file(const char *zonein
 				    "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);
@@ -619,7 +696,7 @@ install_zoneinfo_file(const char *zonein
 				    "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. */
@@ -635,7 +712,7 @@ install_zoneinfo_file(const char *zonein
 				    "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);
@@ -648,7 +725,7 @@ install_zoneinfo_file(const char *zonein
 				    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);
@@ -667,7 +744,7 @@ install_zoneinfo_file(const char *zonein
 		    "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
@@ -701,7 +778,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);
@@ -748,12 +825,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)
 {
@@ -858,15 +929,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 {
@@ -882,7 +962,7 @@ main(int argc, char **argv)
 				close(fd);
 			}
 		}
-		dialog_clear_norefresh();
+		dlg_clear();
 	}
 	if (optind == argc - 1) {
 		snprintf(title, sizeof(title), "Default timezone provided");
@@ -890,18 +970,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);
 }


More information about the svn-src-head mailing list