From fcad183e66bf15b9e8ed5fcef8891bfd0ecc8ec1 Mon Sep 17 00:00:00 2001 From: lum Date: Fri, 16 Jul 2010 05:22:48 +0000 Subject: [PATCH] Make the terminal checking capability of systat better. Checks taken from top(1) screen.c/init_termcap() ok canacar@ sthen@ nicm@ --- usr.bin/systat/engine.c | 46 ++++++++++++++++++++++++++++++++++++++++- usr.bin/systat/engine.h | 3 ++- usr.bin/systat/main.c | 4 ++-- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/usr.bin/systat/engine.c b/usr.bin/systat/engine.c index a30437ec529..0b1baa210dc 100644 --- a/usr.bin/systat/engine.c +++ b/usr.bin/systat/engine.c @@ -1,4 +1,4 @@ -/* $Id: engine.c,v 1.11 2010/01/12 23:22:14 nicm Exp $ */ +/* $Id: engine.c,v 1.12 2010/07/16 05:22:48 lum Exp $ */ /* * Copyright (c) 2001, 2007 Can Erkin Acar * @@ -27,6 +27,7 @@ #include #include #include +#include /* XXX These are defined in term.h and conflict with our variable names */ #ifdef columns @@ -1329,3 +1330,46 @@ engine_loop(int countmax) if (rawmode == 0) endwin(); } + +int +check_termcap(void) +{ + char *term_name; + int status; + static struct termios screen_settings; + + if (!interactive) + /* pretend we have a dumb terminal */ + return(1); + + /* get the terminal name */ + term_name = getenv("TERM"); + if (term_name == NULL) + return(1); + + /* now get the termcap entry */ + if ((status = tgetent(NULL, term_name)) != 1) { + if (status == -1) + warnx("can't open termcap file"); + else + warnx("no termcap entry for a `%s' terminal", + term_name); + + /* pretend it's dumb and proceed */ + return(1); + } + + /* "hardcopy" immediately indicates a very stupid terminal */ + if (tgetflag("hc")) + return(1); + + /* get necessary capabilities */ + if (tgetstr("cl", NULL) == NULL || tgetstr("cm", NULL) == NULL) + return(1); + + /* if stdout is not a terminal, pretend we are a dumb terminal */ + if (tcgetattr(STDOUT_FILENO, &screen_settings) == -1) + return(1); + + return(0); +} diff --git a/usr.bin/systat/engine.h b/usr.bin/systat/engine.h index d34eb1d48e0..e4725ca1a0b 100644 --- a/usr.bin/systat/engine.h +++ b/usr.bin/systat/engine.h @@ -1,4 +1,4 @@ -/* $Id: engine.h,v 1.5 2008/12/07 02:56:06 canacar Exp $ */ +/* $Id: engine.h,v 1.6 2010/07/16 05:22:48 lum Exp $ */ /* * Copyright (c) 2001, 2007 Can Erkin Acar * @@ -133,6 +133,7 @@ void set_order(const char *opt); void next_order(void); void setup_term(int maxpr); +int check_termcap(void); void engine_initialize(void); void engine_loop(int countmax); diff --git a/usr.bin/systat/main.c b/usr.bin/systat/main.c index b476a2e1d2f..f4e0012b3b5 100644 --- a/usr.bin/systat/main.c +++ b/usr.bin/systat/main.c @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.56 2010/07/02 13:30:03 lum Exp $ */ +/* $Id: main.c,v 1.57 2010/07/16 05:22:48 lum Exp $ */ /* * Copyright (c) 2001, 2007 Can Erkin Acar * Copyright (c) 2001 Daniel Hartmeier @@ -465,7 +465,7 @@ main(int argc, char *argv[]) return 1; } - if (!isatty(STDOUT_FILENO)) { + if (check_termcap()) { rawmode = 1; interactive = 0; } -- 2.20.1