Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib POSIX says that use_env(3) must precede setupterm(3).



details:   https://anonhg.NetBSD.org/src/rev/86aa9059b53e
branches:  trunk
changeset: 822508:86aa9059b53e
user:      roy <roy%NetBSD.org@localhost>
date:      Thu Mar 23 00:55:39 2017 +0000

description:
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.

diffstat:

 lib/libcurses/setterm.c     |  40 ++++++----------------------------------
 lib/libterminfo/setupterm.c |  40 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 44 insertions(+), 36 deletions(-)

diffs (171 lines):

diff -r c1b5bd5d266c -r 86aa9059b53e lib/libcurses/setterm.c
--- a/lib/libcurses/setterm.c   Thu Mar 23 00:39:06 2017 +0000
+++ b/lib/libcurses/setterm.c   Thu Mar 23 00:55:39 2017 +0000
@@ -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 @@
 
 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 @@
 _cursesi_setterm(char *type, SCREEN *screen)
 {
        int unknown, r;
-       struct winsize win;
        char *p;
 
        if (type[0] == '\0')
@@ -93,20 +82,9 @@
        __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 @@
                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
diff -r c1b5bd5d266c -r 86aa9059b53e lib/libterminfo/setupterm.c
--- a/lib/libterminfo/setupterm.c       Thu Mar 23 00:39:06 2017 +0000
+++ b/lib/libterminfo/setupterm.c       Thu Mar 23 00:55:39 2017 +0000
@@ -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 @@
 #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 @@
 ti_setupterm(TERMINAL **nterm, const char *term, int fildes, int *errret)
 {
        int error;
+       struct winsize win;
 
        _DIAGASSERT(nterm != NULL);
 
@@ -105,6 +122,25 @@
                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;



Home | Main Index | Thread Index | Old Index