Skip to content

Commit

Permalink
Fix TTY size precedence
Browse files Browse the repository at this point in the history
Precedence order: environment variables, TIOCGWINSZ, terminfo
  • Loading branch information
mptre committed Aug 24, 2017
1 parent 7cdafc8 commit 5491427
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ AM_CFLAGS=-pedantic -Wall -Werror -Wextra
AM_CPPFLAGS=-D_GNU_SOURCE

bin_PROGRAMS=pick
dist_pick_SOURCES=pick.c compat-reallocarray.c compat.h
dist_pick_SOURCES=pick.c compat-reallocarray.c compat-strtonum.c compat.h
dist_man_MANS=pick.1

TESTS=tests/01.t tests/02.t tests/03.t tests/04.t tests/05.t tests/06.t \
Expand Down
75 changes: 75 additions & 0 deletions compat-strtonum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

int unused;

#ifndef HAVE_STRTONUM

/* $OpenBSD: strtonum.c,v 1.8 2015/09/13 08:31:48 guenther Exp $ */

/*
* Copyright (c) 2004 Ted Unangst and Todd Miller
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <errno.h>
#include <limits.h>
#include <stdlib.h>

#define INVALID 1
#define TOOSMALL 2
#define TOOLARGE 3

long long
strtonum(const char *numstr, long long minval, long long maxval,
const char **errstrp)
{
long long ll = 0;
int error = 0;
char *ep;
struct errval {
const char *errstr;
int err;
} ev[4] = {
{ NULL, 0 },
{ "invalid", EINVAL },
{ "too small", ERANGE },
{ "too large", ERANGE },
};

ev[0].err = errno;
errno = 0;
if (minval > maxval) {
error = INVALID;
} else {
ll = strtoll(numstr, &ep, 10);
if (numstr == ep || *ep != '\0')
error = INVALID;
else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
error = TOOSMALL;
else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
error = TOOLARGE;
}
if (errstrp != NULL)
*errstrp = ev[error].errstr;
errno = ev[error].err;
if (error)
ll = 0;

return (ll);
}

#endif /* !HAVE_STRTONUM */
6 changes: 6 additions & 0 deletions compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ void *reallocarray(void *, size_t, size_t);

#endif /* !HAVE_REALLOCARRAY */

#ifndef HAVE_STRTONUM

long long strtonum(const char *, long long, long long, const char **);

#endif /* !HAVE_STRTONUM */

#endif /* COMPAT_H */
3 changes: 3 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
/* Define to 1 if you have the `reallocarray' function. */
#undef HAVE_REALLOCARRAY

/* Define to 1 if you have the `strtonum' function. */
#undef HAVE_STRTONUM

/* Name of package */
#undef PACKAGE

Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ AM_INIT_AUTOMAKE([subdir-objects])
AC_CONFIG_HEADERS([config.h])
AC_PROG_CC
AM_PROG_CC_C_O
AC_CHECK_FUNCS([pledge reallocarray])
AC_CHECK_FUNCS([pledge reallocarray strtonum])
AC_SEARCH_LIBS([setupterm], [curses], [],
[
AC_SEARCH_LIBS([setupterm], [ncursesw],
Expand Down
22 changes: 19 additions & 3 deletions pick.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,15 +702,31 @@ tty_restore(void)
void
tty_size(void)
{
struct winsize ws;
const char *cp;
struct winsize ws;
int sz;

if (ioctl(fileno(tty_in), TIOCGWINSZ, &ws) != -1) {
tty_columns = ws.ws_col;
tty_lines = ws.ws_row;
} else {
}

if (tty_columns == 0)
tty_columns = tigetnum("cols");
if (tty_lines == 0)
tty_lines = tigetnum("lines");
}

if ((cp = getenv("COLUMNS")) != NULL &&
(sz = strtonum(cp, 1, INT_MAX, NULL)) > 0)
tty_columns = sz;
if ((cp = getenv("LINES")) != NULL &&
(sz = strtonum(cp, 1, INT_MAX, NULL)) > 0)
tty_lines = sz;

if (tty_columns == 0)
tty_columns = 80;
if (tty_lines == 0)
tty_lines = 24;

choices_lines = tty_lines - 1; /* available lines, minus query line */
}
Expand Down

0 comments on commit 5491427

Please sign in to comment.