From be6f0de0dfe7bc762a15875e6c5c5a19e0f20f8d Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Tue, 4 Jul 2017 20:58:43 +0200 Subject: [PATCH] Add util.c:screen_init() to probe screen size once per boot Signed-off-by: Joachim Nilsson --- src/finit.c | 4 ++++ src/helpers.c | 5 +++-- src/initctl.c | 4 +--- src/serv.c | 2 -- src/util.c | 32 ++++++++++++++++++++++++++------ src/util.h | 7 +++++++ 6 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/finit.c b/src/finit.c index adb52562..5bfbb1e0 100644 --- a/src/finit.c +++ b/src/finit.c @@ -44,6 +44,7 @@ #include "sig.h" #include "sm.h" #include "tty.h" +#include "util.h" #include "utmp-api.h" #include "watchdog.h" @@ -228,6 +229,9 @@ int main(int argc, char* argv[]) if (getpid() != 1) return client(argc, argv); + /* Set up canvas */ + screen_init(); + /* * In case of emergency. */ diff --git a/src/helpers.c b/src/helpers.c index e07d15af..7c994960 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -32,13 +32,14 @@ #include #include #include -#include #include "finit.h" #include "helpers.h" #include "private.h" +#include "util.h" #include "utmp-api.h" + char *strip_line(char *line) { char *ptr; @@ -79,7 +80,7 @@ static int print_timestamp(void) void printv(const char *fmt, va_list ap) { - char buf[80]; + char buf[SCREEN_WIDTH]; size_t len; const char dots[] = " ....................................................................."; diff --git a/src/initctl.c b/src/initctl.c index ce24cbea..d2f1571f 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -37,9 +37,6 @@ #include #include -#define SCREEN_WIDTH screen_width() -#include - #include "finit.h" #include "cond.h" #include "serv.h" @@ -662,6 +659,7 @@ int main(int argc, char *argv[]) } } + screen_init(); if (optind < argc) { char *cmd = argv[optind++]; char arg[120] = ""; diff --git a/src/serv.c b/src/serv.c index 46a2ba96..b9158f65 100644 --- a/src/serv.c +++ b/src/serv.c @@ -28,8 +28,6 @@ #include #include -#define SCREEN_WIDTH screen_width() -#include #include "util.h" static const char *cwd; diff --git a/src/util.c b/src/util.c index a2a3d623..94134af1 100644 --- a/src/util.c +++ b/src/util.c @@ -32,7 +32,10 @@ #include #endif -char *prognm = NULL; +int screen_setup = -1; +int screen_rows = 24; +int screen_cols = 80; +char *prognm = NULL; char *progname(char *arg0) { @@ -77,14 +80,16 @@ char *sanitize(char *arg, size_t len) } #define ESC "\033" -int screen_width(void) +void screen_init(void) { - int ret = 80; #ifdef HAVE_TERMIOS_H char buf[42]; struct termios tc, saved; struct pollfd fd = { STDIN_FILENO, POLLIN, 0 }; + if (screen_setup != -1) + return; + memset(buf, 0, sizeof(buf)); tcgetattr(STDERR_FILENO, &tc); saved = tc; @@ -96,14 +101,29 @@ int screen_width(void) if (poll(&fd, 1, 300) > 0) { int row, col; - if (scanf(ESC "[%d;%dR", &row, &col) == 2) - ret = col; + if (scanf(ESC "[%d;%dR", &row, &col) == 2) { + screen_rows = row; + screen_cols = col; + screen_setup = 1; + } } fprintf(stderr, ESC "8"); tcsetattr(STDERR_FILENO, TCSANOW, &saved); #endif - return ret; + + if (screen_cols > 132) + screen_cols = 132; +} + +int screen_width(void) +{ + screen_init(); + + if (screen_cols < 1) + return 80; + + return screen_cols; } /** diff --git a/src/util.h b/src/util.h index 4f913e42..db44a876 100644 --- a/src/util.h +++ b/src/util.h @@ -24,11 +24,18 @@ #ifndef FINIT_UTIL_H_ #define FINIT_UTIL_H_ +#define SCREEN_WIDTH screen_cols +#include + +extern int screen_rows; +extern int screen_cols; extern char *prognm; char *progname (char *arg0); void do_sleep (unsigned int sec); char *sanitize (char *arg, size_t len); + +void screen_init (void); int screen_width (void); #endif /* FINIT_UTIL_H_ */