Module Name:    src
Committed By:   roy
Date:           Thu Mar 23 00:55:39 UTC 2017

Modified Files:
        src/lib/libcurses: setterm.c
        src/lib/libterminfo: setupterm.c

Log Message:
POSIX says that use_env(3) must precede setupterm(3).
The former lives in curses.h, but the latter lives in term.h.

This is solved by moving the function to libterminfo.
Because the environment can affect the terminal capabilities for
lines and columns, it follows that the tty size should affect it to.
So move that code to libterminfo and adjust in libcurses.


To generate a diff of this commit:
cvs rdiff -u -r1.65 -r1.66 src/lib/libcurses/setterm.c
cvs rdiff -u -r1.6 -r1.7 src/lib/libterminfo/setupterm.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libcurses/setterm.c
diff -u src/lib/libcurses/setterm.c:1.65 src/lib/libcurses/setterm.c:1.66
--- src/lib/libcurses/setterm.c:1.65	Mon Mar 20 20:44:06 2017
+++ src/lib/libcurses/setterm.c	Thu Mar 23 00:55:39 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: setterm.c,v 1.65 2017/03/20 20:44:06 christos Exp $	*/
+/*	$NetBSD: setterm.c,v 1.66 2017/03/23 00:55:39 roy Exp $	*/
 
 /*
  * Copyright (c) 1981, 1993, 1994
@@ -34,12 +34,10 @@
 #if 0
 static char sccsid[] = "@(#)setterm.c	8.8 (Berkeley) 10/25/94";
 #else
-__RCSID("$NetBSD: setterm.c,v 1.65 2017/03/20 20:44:06 christos Exp $");
+__RCSID("$NetBSD: setterm.c,v 1.66 2017/03/23 00:55:39 roy Exp $");
 #endif
 #endif /* not lint */
 
-#include <sys/ioctl.h>		/* TIOCGWINSZ on old systems. */
-
 #include <stdlib.h>
 #include <string.h>
 #include <termios.h>
@@ -50,17 +48,9 @@ __RCSID("$NetBSD: setterm.c,v 1.65 2017/
 
 static int does_esc_m(const char *cap);
 static int does_ctrl_o(const char *exit_cap, const char *acs_cap);
-static bool __use_env = true;
 
 attr_t	 __mask_op, __mask_me, __mask_ue, __mask_se;
 
-void
-use_env(bool value)
-{
-
-	__use_env = value;
-}
-
 int
 setterm(char *type)
 {
@@ -72,7 +62,6 @@ int
 _cursesi_setterm(char *type, SCREEN *screen)
 {
 	int unknown, r;
-	struct winsize win;
 	char *p;
 
 	if (type[0] == '\0')
@@ -93,20 +82,9 @@ _cursesi_setterm(char *type, SCREEN *scr
 	__CTRACE(__CTRACE_INIT, "setterm: tty = %s\n", type);
 #endif
 
-	/* Try TIOCGWINSZ, and, if it fails, the terminfo entry. */
-	if (ioctl(fileno(screen->outfd), TIOCGWINSZ, &win) != -1 &&
-	    win.ws_row != 0 && win.ws_col != 0) {
-		screen->LINES = win.ws_row;
-		screen->COLS = win.ws_col;
-	}  else {
-		if (unknown) {
-			screen->LINES = -1;
-			screen->COLS = -1;
-		} else {
-			screen->LINES = t_lines(screen->term);
-			screen->COLS = t_columns(screen->term);
-		}
-	}
+	/* lines and cols will have been setup correctly by ti_setupterm(3). */
+	screen->LINES = t_lines(screen->term);
+	screen->COLS = t_columns(screen->term);
 
 	if (screen->filtered) {
 		/* Disable use of clear, cud, cud1, cup, cuu1 and vpa. */
@@ -122,18 +100,12 @@ _cursesi_setterm(char *type, SCREEN *scr
 		screen->term->strs[TICODE_home] = screen->term->strs[TICODE_cr];
 		/* Set lines equal to 1. */
 		screen->LINES = 1;
+		t_lines(screen->term) = 1;
 	}
 #ifdef DEBUG
 	__CTRACE(__CTRACE_INIT, "setterm: filtered %d", screen->filtered);
 #endif
 
-	/* POSIX 1003.2 requires that the environment override. */
-	if (__use_env) {
-		if (!screen->filtered && (p = getenv("LINES")) != NULL)
-			screen->LINES = (int)strtol(p, NULL, 0);
-		if ((p = getenv("COLUMNS")) != NULL)
-			screen->COLS = (int)strtol(p, NULL, 0);
-	}
 	if ((p = getenv("ESCDELAY")) != NULL)
 		screen->ESCDELAY = (int)strtol(p, NULL, 0);
 	else

Index: src/lib/libterminfo/setupterm.c
diff -u src/lib/libterminfo/setupterm.c:1.6 src/lib/libterminfo/setupterm.c:1.7
--- src/lib/libterminfo/setupterm.c:1.6	Thu Mar 23 00:36:37 2017
+++ src/lib/libterminfo/setupterm.c	Thu Mar 23 00:55:39 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: setupterm.c,v 1.6 2017/03/23 00:36:37 roy Exp $ */
+/* $NetBSD: setupterm.c,v 1.7 2017/03/23 00:55:39 roy Exp $ */
 
 /*
  * Copyright (c) 2009, 2011 The NetBSD Foundation, Inc.
@@ -28,10 +28,12 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: setupterm.c,v 1.6 2017/03/23 00:36:37 roy Exp $");
+__RCSID("$NetBSD: setupterm.c,v 1.7 2017/03/23 00:55:39 roy Exp $");
 
+#include <sys/ioctl.h>
 #include <assert.h>
 #include <err.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <strings.h>
@@ -39,6 +41,20 @@ __RCSID("$NetBSD: setupterm.c,v 1.6 2017
 #include <term_private.h>
 #include <term.h>
 
+/*
+ * use_env is really a curses function - POSIX mandates it's in curses.h
+ * But it has to live in terminfo because it must precede a call to setupterm().
+ */
+#include <curses.h>
+
+static bool __use_env = true;
+
+void
+use_env(bool value)
+{
+
+	__use_env = value;
+}
 #define reterr(code, msg)						      \
 	do {								      \
 		if (errret == NULL)					      \
@@ -64,6 +80,7 @@ int
 ti_setupterm(TERMINAL **nterm, const char *term, int fildes, int *errret)
 {
 	int error;
+	struct winsize win;
 
 	_DIAGASSERT(nterm != NULL);
 
@@ -105,6 +122,25 @@ ti_setupterm(TERMINAL **nterm, const cha
 		reterrarg(0, "%s: generic terminal", term);
 	if (t_hard_copy(*nterm))
 		reterrarg(1, "%s: hardcopy terminal", term);
+
+	/* If TIOCGWINSZ works, then set initial lines and columns. */
+	if (ioctl(fildes, TIOCGWINSZ, &win) != -1 &&
+	    win.ws_row != 0 && win.ws_col != 0)
+	{
+		t_lines(*nterm) = win.ws_row;
+		t_columns(*nterm) = win.ws_col;
+	}
+
+	/* POSIX 1003.2 requires that the environment override. */
+	if (__use_env) {
+		char *p;
+
+		if ((p = getenv("LINES")) != NULL)
+			t_lines(*nterm) = (int)strtol(p, NULL, 0);
+		if ((p = getenv("COLUMNS")) != NULL)
+			t_columns(*nterm) = (int)strtol(p, NULL, 0);
+	}
+
 	/* POSIX requires 1 for success */
 	if (errret)
 		*errret = 1;

Reply via email to