Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIGWINCH support #213

Merged
merged 4 commits into from
Aug 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
AUTOMAKE_OPTIONS=foreign

AM_CFLAGS=-pedantic -Wall -Werror -Wextra
AM_CFLAGS=-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 All @@ -16,7 +16,7 @@ TESTS=tests/01.t tests/02.t tests/03.t tests/04.t tests/05.t tests/06.t \
tests/37.t tests/38.t tests/39.t tests/40.t tests/41.t tests/42.t \
tests/43.t tests/44.t tests/45.t tests/46.t tests/47.t tests/48.t \
tests/49.t tests/50.t tests/51.t tests/52.t tests/53.t tests/54.t \
tests/55.t tests/56.t
tests/55.t tests/56.t tests/57.t
TEST_EXTENSIONS=.t
T_LOG_COMPILER=$(top_srcdir)/tests/pick-test.sh
AM_COLOR_TESTS=no
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
2 changes: 2 additions & 0 deletions pick.1
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Disable the use of the alternate screen terminal feature.
.Bl -tag -width XXXX
.It Ic Ctrl-C
Exit with a erroneous status without outputting the selected choice.
.It Ic Ctrl-L
Redraw interface with respect to the current size of the terminal.
.It Ic Up/Down | Ctrl-P/Ctrl-N
Select between choices matching the current search query.
.It Ic Page-Down/Page-Up | Ctrl-V/Alt-V
Expand Down
111 changes: 95 additions & 16 deletions pick.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "config.h"
#endif

#include <sys/ioctl.h>

#include <ctype.h>
#include <err.h>
#include <limits.h>
Expand Down Expand Up @@ -39,6 +41,7 @@ enum key {
CTRL_A,
CTRL_E,
CTRL_K,
CTRL_L,
CTRL_U,
CTRL_W,
RIGHT,
Expand Down Expand Up @@ -68,6 +71,7 @@ static void filter_choices(void);
static char *get_choices(void);
static enum key get_key(char *, size_t, size_t *);
static __dead void handle_sigint(int);
static void handle_sigwinch(int);
static int isu8cont(unsigned char);
static int isu8start(unsigned char);
static size_t min_match(const char *, size_t, ssize_t *,
Expand All @@ -77,26 +81,29 @@ static void print_line(const char *, size_t, int, ssize_t,
ssize_t);
static const struct choice *selected_choice(void);
static const char *strcasechr(const char *, const char *);
static void toggle_sigwinch(int);
static int tty_getc(void);
static const char *tty_getcap(char *);
static void tty_init(void);
static const char *tty_parm1(const char *, int);
static int tty_putc(int);
static void tty_restore(void);
static void tty_size(void);
static __dead void usage(void);

static struct termios original_attributes;
static struct termios original_attributes;
static struct {
size_t size;
size_t length;
struct choice *v;
} choices;
static FILE *tty_in, *tty_out;
static char *query;
static size_t query_length, query_size;
static int descriptions, choices_lines;
static int sort = 1;
static int use_alternate_screen = 1;
} choices;
static FILE *tty_in, *tty_out;
static char *query;
static size_t query_length, query_size;
static volatile sig_atomic_t gotsigwinch;
static int descriptions, choices_lines, tty_columns, tty_lines;
static int sort = 1;
static int use_alternate_screen = 1;

int
main(int argc, char *argv[])
Expand Down Expand Up @@ -398,6 +405,10 @@ selected_choice(void)
filter_choices();
selection = yscroll = 0;
break;
case CTRL_L:
tty_size();
selection = yscroll = 0;
break;
case CTRL_W:
if (cursor_position == 0)
break;
Expand Down Expand Up @@ -652,14 +663,15 @@ tty_init(void)

setupterm((char *)0, fileno(tty_out), (int *)0);

choices_lines = lines - 1; /* available lines, minus query line */
tty_size();

if (use_alternate_screen)
tty_putp(enter_ca_mode, 0);

tty_putp(keypad_xmit, 0);

signal(SIGINT, handle_sigint);
toggle_sigwinch(0);
}

int
Expand All @@ -675,6 +687,25 @@ handle_sigint(int sig __attribute__((unused)))
exit(1);
}

void
handle_sigwinch(int sig)
{
gotsigwinch = sig == SIGWINCH;
}

void
toggle_sigwinch(int enable)
{
struct sigaction sa;

sa.sa_flags = 0;
sa.sa_handler = enable ? handle_sigwinch : SIG_IGN;
sigemptyset(&sa.sa_mask);

if (sigaction(SIGWINCH, &sa, NULL) == -1)
err(1, "sigaction: SIGWINCH");
}

void
tty_restore(void)
{
Expand All @@ -691,6 +722,38 @@ tty_restore(void)
fclose(tty_out);
}

void
tty_size(void)
{
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;
}

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 */
}

void
print_line(const char *str, size_t len, int standout,
ssize_t enter_underline, ssize_t exit_underline)
Expand All @@ -703,7 +766,7 @@ print_line(const char *str, size_t len, int standout,
tty_putp(enter_standout_mode, 1);

col = i = in_esc_seq = 0;
while (col < columns) {
while (col < tty_columns) {
if (enter_underline == (ssize_t)i)
tty_putp(enter_underline_mode, 1);
else if (exit_underline == (ssize_t)i)
Expand All @@ -713,7 +776,7 @@ print_line(const char *str, size_t len, int standout,

if (str[i] == '\t') {
width = 8 - (col & 7); /* ceil to multiple of 8 */
if (col + width > columns)
if (col + width > tty_columns)
break;
col += width;

Expand Down Expand Up @@ -767,15 +830,15 @@ print_line(const char *str, size_t len, int standout,
else if (str[i] >= '@' && str[i] <= '~')
in_esc_seq = 0;

if (col + width > columns)
if (col + width > tty_columns)
break;
col += width;

for (; nbytes > 0; nbytes--, i++)
if (tty_putc(str[i]) == EOF)
err(1, "tty_putc");
}
for (; col < columns; col++)
for (; col < tty_columns; col++)
if (tty_putc(' ') == EOF)
err(1, "tty_putc");

Expand Down Expand Up @@ -827,6 +890,7 @@ get_key(char *buf, size_t size, size_t *nread)
KEY(CTRL_A, "\001"),
KEY(CTRL_E, "\005"),
KEY(CTRL_K, "\013"),
KEY(CTRL_L, "\014"),
KEY(CTRL_U, "\025"),
KEY(CTRL_W, "\027"),
CAP(DEL, "kdch1"),
Expand All @@ -853,9 +917,20 @@ get_key(char *buf, size_t size, size_t *nread)
int c, i;

*nread = 0;
for (; size > 0; size--) {
buf[(*nread)++] = tty_getc();

/*
* Allow SIGWINCH on the first read. If the signal is received, return
* CTRL_L which will trigger a resize.
*/
toggle_sigwinch(1);
buf[(*nread)++] = tty_getc();
toggle_sigwinch(0);
if (gotsigwinch) {
gotsigwinch = 0;
return CTRL_L;
}

for (;;) {
for (i = 0; keys[i].key != UNKNOWN; i++) {
if (keys[i].str == NULL) {
keys[i].str = tty_getcap(keys[i].cap);
Expand All @@ -872,6 +947,10 @@ get_key(char *buf, size_t size, size_t *nread)
}
if (keys[i].key == UNKNOWN)
break;

if (size-- == 0)
break;
buf[(*nread)++] = tty_getc();
}

if (*nread > 1 && buf[0] == '\033' && (buf[1] == '[' || buf[1] == 'O')) {
Expand Down Expand Up @@ -917,7 +996,7 @@ tty_getc(void)
{
int c;

if ((c = getc(tty_in)) == ERR)
if ((c = getc(tty_in)) == ERR && !gotsigwinch)
err(1, "getc");

return c;
Expand Down
7 changes: 7 additions & 0 deletions tests/57.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
description: CTRL-L clears the selection
keys: \016 \014 \n # DOWN CTRL_L ENTER
stdin:
a
b
stdout:
a