2011-06-05 18:35:47 +05:30
|
|
|
/*
|
|
|
|
* watch -- execute a program repeatedly, displaying output fullscreen
|
2002-02-02 04:17:29 +05:30
|
|
|
*
|
|
|
|
* Based on the original 1991 'watch' by Tony Rems <rembo@unisoft.com>
|
|
|
|
* (with mods and corrections by Francois Pinard).
|
|
|
|
*
|
|
|
|
* Substantially reworked, new features (differences option, SIGWINCH
|
2011-06-05 18:35:47 +05:30
|
|
|
* handling, unlimited command length, long line handling) added Apr
|
|
|
|
* 1999 by Mike Coleman <mkc@acm.org>.
|
2002-12-15 06:00:17 +05:30
|
|
|
*
|
2003-03-18 05:12:00 +05:30
|
|
|
* Changes by Albert Cahalan, 2002-2003.
|
2009-11-24 05:30:43 +05:30
|
|
|
* stderr handling, exec, and beep option added by Morty Abzug, 2008
|
2009-11-04 00:54:27 +05:30
|
|
|
* Unicode Support added by Jarrod Lowe <procps@rrod.net> in 2009.
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
|
|
|
|
2011-12-20 16:42:37 +05:30
|
|
|
#include "config.h"
|
2011-06-05 18:35:47 +05:30
|
|
|
#include "proc/procps.h"
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <ctype.h>
|
2011-06-05 18:35:47 +05:30
|
|
|
#include <errno.h>
|
|
|
|
#include <errno.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <getopt.h>
|
2011-06-05 18:35:47 +05:30
|
|
|
#include <locale.h>
|
|
|
|
#include <locale.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
2009-11-24 05:30:46 +05:30
|
|
|
#include <sys/time.h>
|
2011-06-05 18:35:47 +05:30
|
|
|
#include <termios.h>
|
|
|
|
#include <termios.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2011-12-20 16:42:37 +05:30
|
|
|
#ifdef WITH_WATCH8BIT
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <ncursesw/ncurses.h>
|
2011-06-05 18:35:47 +05:30
|
|
|
#else
|
2011-12-20 16:42:37 +05:30
|
|
|
#include <ncurses.h>
|
2011-06-05 18:35:47 +05:30
|
|
|
#endif /* WITH_WATCH8BIT */
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2004-08-27 19:53:36 +05:30
|
|
|
#ifdef FORCE_8BIT
|
|
|
|
#undef isprint
|
|
|
|
#define isprint(x) ( (x>=' '&&x<='~') || (x>=0xa0) )
|
|
|
|
#endif
|
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
static int curses_started = 0;
|
2002-11-29 04:39:48 +05:30
|
|
|
static int height = 24, width = 80;
|
|
|
|
static int screen_size_changed = 0;
|
|
|
|
static int first_screen = 1;
|
2011-06-05 18:35:47 +05:30
|
|
|
static int show_title = 2; /* number of lines used, 2 or 0 */
|
2009-11-24 05:30:46 +05:30
|
|
|
static int precise_timekeeping = 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
#define min(x,y) ((x) > (y) ? (y) : (x))
|
2010-03-01 11:47:11 +05:30
|
|
|
#define MAX_ANSIBUF 10
|
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
static void __attribute__ ((__noreturn__))
|
|
|
|
usage(FILE * out)
|
2010-03-01 11:47:11 +05:30
|
|
|
{
|
2011-06-05 18:35:47 +05:30
|
|
|
fprintf(out,
|
|
|
|
"\nUsage: %s [options] command\n"
|
|
|
|
"\nOptions:\n", program_invocation_short_name);
|
|
|
|
fprintf(out,
|
|
|
|
" -b, --beep beep if command has a non-zero exit\n"
|
|
|
|
" -c, --color interpret ANSI color sequences\n"
|
|
|
|
" -e, --errexit exit if command has a non-zero exit\n"
|
|
|
|
" -f, --differences highlight changes between updates\n"
|
|
|
|
" -n, --interval <secs> seconds to wait between updates\n"
|
|
|
|
" -p, --precise attempt run command in precise intervals\n"
|
|
|
|
" -t, --no-title turn off header\n"
|
|
|
|
" -x, --exec pass command to exec instead of \"sh -c\"\n"
|
|
|
|
" -h, --help display this help text\n"
|
|
|
|
" -v, --version display version information and exit\n");
|
|
|
|
fprintf(out, "\nFor more information see watch(1).\n");
|
|
|
|
|
|
|
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
2010-03-01 11:47:11 +05:30
|
|
|
}
|
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
static void init_ansi_colors(void)
|
2010-03-01 11:47:11 +05:30
|
|
|
{
|
2011-06-05 18:35:47 +05:30
|
|
|
int i;
|
|
|
|
short ncurses_colors[] = {
|
|
|
|
COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE,
|
|
|
|
COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
init_pair(i + 1, ncurses_colors[i], -1);
|
2010-03-01 11:47:11 +05:30
|
|
|
}
|
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
static void set_ansi_attribute(const int attrib)
|
2010-03-01 11:47:11 +05:30
|
|
|
{
|
2011-06-05 18:35:47 +05:30
|
|
|
switch (attrib) {
|
|
|
|
case -1:
|
|
|
|
return;
|
|
|
|
case 0:
|
|
|
|
standend();
|
|
|
|
return;
|
|
|
|
case 1:
|
|
|
|
attrset(A_BOLD);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (attrib >= 30 && attrib <= 37) {
|
|
|
|
color_set(attrib - 29, NULL);
|
|
|
|
return;
|
|
|
|
}
|
2010-03-01 11:47:11 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
static void process_ansi(FILE * fp)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
2011-06-05 18:35:47 +05:30
|
|
|
int i, c, num1, num2;
|
|
|
|
char buf[MAX_ANSIBUF];
|
|
|
|
char *nextnum;
|
|
|
|
|
|
|
|
c = getc(fp);
|
|
|
|
if (c != '[') {
|
|
|
|
ungetc(c, fp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (i = 0; i < MAX_ANSIBUF; i++) {
|
|
|
|
c = getc(fp);
|
|
|
|
/* COLOUR SEQUENCE ENDS in 'm' */
|
|
|
|
if (c == 'm') {
|
|
|
|
buf[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c < '0' && c > '9' && c != ';') {
|
|
|
|
while (--i >= 0)
|
|
|
|
ungetc(buf[i], fp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf[i] = (char)c;
|
|
|
|
}
|
|
|
|
num1 = strtol(buf, &nextnum, 10);
|
|
|
|
if (nextnum != buf && nextnum[0] != '\0')
|
|
|
|
num2 = strtol(nextnum + 1, NULL, 10);
|
|
|
|
else
|
|
|
|
num2 = -1;
|
|
|
|
set_ansi_attribute(num1);
|
|
|
|
set_ansi_attribute(num2);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
static void __attribute__ ((__noreturn__)) do_exit(int status)
|
2002-11-29 04:39:48 +05:30
|
|
|
{
|
|
|
|
if (curses_started)
|
|
|
|
endwin();
|
|
|
|
exit(status);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* signal handler */
|
2011-06-05 18:35:47 +05:30
|
|
|
static void die(int notused __attribute__ ((__unused__)))
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
2011-06-05 18:35:47 +05:30
|
|
|
do_exit(EXIT_SUCCESS);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
static void winch_handler(int notused __attribute__ ((__unused__)))
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
2002-11-29 04:39:48 +05:30
|
|
|
screen_size_changed = 1;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2004-08-27 19:53:36 +05:30
|
|
|
static char env_col_buf[24];
|
|
|
|
static char env_row_buf[24];
|
|
|
|
static int incoming_cols;
|
|
|
|
static int incoming_rows;
|
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
static void get_terminal_size(void)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
2002-11-29 04:39:48 +05:30
|
|
|
struct winsize w;
|
2011-06-05 18:35:47 +05:30
|
|
|
if (!incoming_cols) {
|
|
|
|
/* have we checked COLUMNS? */
|
2004-08-27 19:53:36 +05:30
|
|
|
const char *s = getenv("COLUMNS");
|
|
|
|
incoming_cols = -1;
|
2011-06-05 18:35:47 +05:30
|
|
|
if (s && *s) {
|
2004-08-27 19:53:36 +05:30
|
|
|
long t;
|
|
|
|
char *endptr;
|
|
|
|
t = strtol(s, &endptr, 0);
|
2011-06-05 18:35:47 +05:30
|
|
|
if (!*endptr && (t > 0) && (t < (long)666))
|
|
|
|
incoming_cols = (int)t;
|
2004-08-27 19:53:36 +05:30
|
|
|
width = incoming_cols;
|
2011-06-05 18:35:47 +05:30
|
|
|
snprintf(env_col_buf, sizeof env_col_buf, "COLUMNS=%d",
|
|
|
|
width);
|
2004-08-27 19:53:36 +05:30
|
|
|
putenv(env_col_buf);
|
|
|
|
}
|
|
|
|
}
|
2011-06-05 18:35:47 +05:30
|
|
|
if (!incoming_rows) {
|
|
|
|
/* have we checked LINES? */
|
2004-08-27 19:53:36 +05:30
|
|
|
const char *s = getenv("LINES");
|
|
|
|
incoming_rows = -1;
|
2011-06-05 18:35:47 +05:30
|
|
|
if (s && *s) {
|
2004-08-27 19:53:36 +05:30
|
|
|
long t;
|
|
|
|
char *endptr;
|
|
|
|
t = strtol(s, &endptr, 0);
|
2011-06-05 18:35:47 +05:30
|
|
|
if (!*endptr && (t > 0) && (t < (long)666))
|
|
|
|
incoming_rows = (int)t;
|
2004-08-27 19:53:36 +05:30
|
|
|
height = incoming_rows;
|
2011-06-05 18:35:47 +05:30
|
|
|
snprintf(env_row_buf, sizeof env_row_buf, "LINES=%d",
|
|
|
|
height);
|
2004-08-27 19:53:36 +05:30
|
|
|
putenv(env_row_buf);
|
|
|
|
}
|
|
|
|
}
|
2011-06-05 18:35:47 +05:30
|
|
|
if (incoming_cols < 0 || incoming_rows < 0) {
|
2004-08-27 19:53:36 +05:30
|
|
|
if (ioctl(2, TIOCGWINSZ, &w) == 0) {
|
2011-06-05 18:35:47 +05:30
|
|
|
if (incoming_rows < 0 && w.ws_row > 0) {
|
2004-08-27 19:53:36 +05:30
|
|
|
height = w.ws_row;
|
2011-06-05 18:35:47 +05:30
|
|
|
snprintf(env_row_buf, sizeof env_row_buf,
|
|
|
|
"LINES=%d", height);
|
2004-08-27 19:53:36 +05:30
|
|
|
putenv(env_row_buf);
|
|
|
|
}
|
2011-06-05 18:35:47 +05:30
|
|
|
if (incoming_cols < 0 && w.ws_col > 0) {
|
2004-08-27 19:53:36 +05:30
|
|
|
width = w.ws_col;
|
2011-06-05 18:35:47 +05:30
|
|
|
snprintf(env_col_buf, sizeof env_col_buf,
|
|
|
|
"COLUMNS=%d", width);
|
2004-08-27 19:53:36 +05:30
|
|
|
putenv(env_col_buf);
|
|
|
|
}
|
|
|
|
}
|
2002-11-29 04:39:48 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2009-11-24 05:30:46 +05:30
|
|
|
/* get current time in usec */
|
|
|
|
typedef unsigned long long watch_usec_t;
|
|
|
|
#define USECS_PER_SEC (1000000ull)
|
2011-06-05 18:35:47 +05:30
|
|
|
watch_usec_t get_time_usec()
|
|
|
|
{
|
2009-11-24 05:30:46 +05:30
|
|
|
struct timeval now;
|
|
|
|
gettimeofday(&now, NULL);
|
2011-06-05 18:35:47 +05:30
|
|
|
return USECS_PER_SEC * now.tv_sec + now.tv_usec;
|
2009-11-24 05:30:46 +05:30
|
|
|
}
|
|
|
|
|
2011-12-20 16:42:37 +05:30
|
|
|
#ifdef WITH_WATCH8BIT
|
2011-06-05 18:35:47 +05:30
|
|
|
/* read a wide character from a popen'd stream */
|
2009-11-04 00:54:27 +05:30
|
|
|
#define MAX_ENC_BYTES 16
|
2011-06-05 18:35:47 +05:30
|
|
|
wint_t my_getwc(FILE * s);
|
|
|
|
wint_t my_getwc(FILE * s)
|
|
|
|
{
|
|
|
|
/* assuming no encoding ever consumes more than 16 bytes */
|
|
|
|
char i[MAX_ENC_BYTES];
|
2009-11-04 00:54:27 +05:30
|
|
|
int byte = 0;
|
|
|
|
int convert;
|
|
|
|
int x;
|
|
|
|
wchar_t rval;
|
2011-06-05 18:35:47 +05:30
|
|
|
while (1) {
|
2009-11-04 00:54:27 +05:30
|
|
|
i[byte] = getc(s);
|
2011-06-05 18:35:47 +05:30
|
|
|
if (i[byte] == EOF) {
|
|
|
|
return WEOF;
|
|
|
|
}
|
2009-11-04 00:54:27 +05:30
|
|
|
byte++;
|
|
|
|
errno = 0;
|
|
|
|
mbtowc(NULL, NULL, 0);
|
|
|
|
convert = mbtowc(&rval, i, byte);
|
|
|
|
x = errno;
|
2011-06-05 18:35:47 +05:30
|
|
|
if (convert > 0) {
|
|
|
|
/* legal conversion */
|
|
|
|
return rval;
|
|
|
|
}
|
|
|
|
if (byte == MAX_ENC_BYTES) {
|
|
|
|
while (byte > 1) {
|
|
|
|
/* at least *try* to fix up */
|
|
|
|
ungetc(i[--byte], s);
|
|
|
|
}
|
|
|
|
errno = -EILSEQ;
|
|
|
|
return WEOF;
|
2009-11-04 00:54:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-05 18:35:47 +05:30
|
|
|
#endif /* WITH_WATCH8BIT */
|
2009-11-04 00:54:27 +05:30
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
int main(int argc, char *argv[])
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
2002-11-29 04:39:48 +05:30
|
|
|
int optc;
|
|
|
|
int option_differences = 0,
|
|
|
|
option_differences_cumulative = 0,
|
2011-06-05 18:35:47 +05:30
|
|
|
option_exec = 0,
|
|
|
|
option_beep = 0,
|
|
|
|
option_color = 0,
|
|
|
|
option_errexit = 0, option_help = 0, option_version = 0;
|
2006-06-17 10:22:42 +05:30
|
|
|
double interval = 2;
|
2002-11-29 04:39:48 +05:30
|
|
|
char *command;
|
2009-11-24 05:30:43 +05:30
|
|
|
char **command_argv;
|
2002-11-29 04:39:48 +05:30
|
|
|
int command_length = 0; /* not including final \0 */
|
2011-06-05 18:35:47 +05:30
|
|
|
watch_usec_t next_loop; /* next loop time in us, used for precise time
|
|
|
|
* keeping only */
|
2011-12-20 16:42:37 +05:30
|
|
|
#ifdef WITH_WATCH8BIT
|
|
|
|
wchar_t *wcommand = NULL;
|
|
|
|
int wcommand_columns = 0; /* not including final \0 */
|
2011-06-05 18:35:47 +05:30
|
|
|
int wcommand_characters = 0; /* not including final \0 */
|
|
|
|
#endif /* WITH_WATCH8BIT */
|
2011-12-20 16:42:37 +05:30
|
|
|
|
2009-11-24 05:30:43 +05:30
|
|
|
int pipefd[2];
|
|
|
|
int status;
|
|
|
|
pid_t child;
|
2002-11-29 04:39:48 +05:30
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
static struct option longopts[] = {
|
|
|
|
{"color", no_argument, 0, 'c'},
|
|
|
|
{"differences", optional_argument, 0, 'd'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"interval", required_argument, 0, 'n'},
|
|
|
|
{"beep", no_argument, 0, 'b'},
|
|
|
|
{"errexit", no_argument, 0, 'e'},
|
|
|
|
{"exec", no_argument, 0, 'x'},
|
|
|
|
{"precise", no_argument, 0, 'p'},
|
|
|
|
{"no-title", no_argument, 0, 't'},
|
|
|
|
{"version", no_argument, 0, 'v'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2002-11-29 04:39:48 +05:30
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
while ((optc =
|
|
|
|
getopt_long(argc, argv, "+bced::hn:pvtx", longopts, (int *)0))
|
2002-11-29 04:39:48 +05:30
|
|
|
!= EOF) {
|
|
|
|
switch (optc) {
|
2009-11-24 05:30:43 +05:30
|
|
|
case 'b':
|
|
|
|
option_beep = 1;
|
|
|
|
break;
|
2011-06-05 18:35:47 +05:30
|
|
|
case 'c':
|
|
|
|
option_color = 1;
|
|
|
|
break;
|
2002-11-29 04:39:48 +05:30
|
|
|
case 'd':
|
|
|
|
option_differences = 1;
|
|
|
|
if (optarg)
|
|
|
|
option_differences_cumulative = 1;
|
|
|
|
break;
|
2011-06-05 18:35:47 +05:30
|
|
|
case 'e':
|
|
|
|
option_errexit = 1;
|
2002-11-29 04:39:48 +05:30
|
|
|
break;
|
2003-02-09 12:57:16 +05:30
|
|
|
case 't':
|
|
|
|
show_title = 0;
|
|
|
|
break;
|
2009-11-24 05:30:43 +05:30
|
|
|
case 'x':
|
2011-06-05 18:35:47 +05:30
|
|
|
option_exec = 1;
|
2009-11-24 05:30:43 +05:30
|
|
|
break;
|
2002-11-29 04:39:48 +05:30
|
|
|
case 'n':
|
|
|
|
{
|
|
|
|
char *str;
|
2006-06-17 10:22:42 +05:30
|
|
|
interval = strtod(optarg, &str);
|
2002-11-29 04:39:48 +05:30
|
|
|
if (!*optarg || *str)
|
2011-06-05 18:35:47 +05:30
|
|
|
usage(stderr);
|
|
|
|
if (interval < 0.1)
|
2004-07-15 06:47:15 +05:30
|
|
|
interval = 0.1;
|
2011-06-05 18:35:47 +05:30
|
|
|
if (interval > ~0u / 1000000)
|
|
|
|
interval = ~0u / 1000000;
|
2002-11-29 04:39:48 +05:30
|
|
|
}
|
|
|
|
break;
|
2009-11-24 05:30:46 +05:30
|
|
|
case 'p':
|
|
|
|
precise_timekeeping = 1;
|
|
|
|
break;
|
2011-06-05 18:35:47 +05:30
|
|
|
case 'h':
|
|
|
|
usage(stdout);
|
2002-11-29 04:39:48 +05:30
|
|
|
break;
|
2011-06-05 18:35:47 +05:30
|
|
|
case 'v':
|
|
|
|
display_version();
|
|
|
|
return EXIT_SUCCESS;
|
2002-11-29 04:39:48 +05:30
|
|
|
default:
|
2011-06-05 18:35:47 +05:30
|
|
|
usage(stderr);
|
2002-11-29 04:39:48 +05:30
|
|
|
break;
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2002-11-29 04:39:48 +05:30
|
|
|
|
|
|
|
if (optind >= argc)
|
2011-06-05 18:35:47 +05:30
|
|
|
usage(stderr);
|
2002-11-29 04:39:48 +05:30
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
/* save for later */
|
|
|
|
command_argv = &(argv[optind]);
|
2009-11-24 05:30:43 +05:30
|
|
|
|
2002-11-29 04:39:48 +05:30
|
|
|
command = strdup(argv[optind++]);
|
|
|
|
command_length = strlen(command);
|
|
|
|
for (; optind < argc; optind++) {
|
|
|
|
char *endp;
|
|
|
|
int s = strlen(argv[optind]);
|
2011-06-05 18:35:47 +05:30
|
|
|
/* space and \0 */
|
|
|
|
command = realloc(command, command_length + s + 2);
|
2002-11-29 04:39:48 +05:30
|
|
|
endp = command + command_length;
|
|
|
|
*endp = ' ';
|
|
|
|
memcpy(endp + 1, argv[optind], s);
|
2011-06-05 18:35:47 +05:30
|
|
|
/* space then string length */
|
|
|
|
command_length += 1 + s;
|
2002-11-29 04:39:48 +05:30
|
|
|
command[command_length] = '\0';
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2011-12-20 16:42:37 +05:30
|
|
|
#ifdef WITH_WATCH8BIT
|
2011-06-05 18:35:47 +05:30
|
|
|
/* convert to wide for printing purposes */
|
|
|
|
/*mbstowcs(NULL, NULL, 0); */
|
2009-11-04 00:54:27 +05:30
|
|
|
wcommand_characters = mbstowcs(NULL, command, 0);
|
2011-06-05 18:35:47 +05:30
|
|
|
if (wcommand_characters < 0) {
|
2009-11-04 00:54:27 +05:30
|
|
|
fprintf(stderr, "Unicode Handling Error\n");
|
2011-06-05 18:35:47 +05:30
|
|
|
exit(EXIT_FAILURE);
|
2009-11-04 00:54:27 +05:30
|
|
|
}
|
2011-06-05 18:35:47 +05:30
|
|
|
wcommand =
|
|
|
|
(wchar_t *) malloc((wcommand_characters + 1) * sizeof(wcommand));
|
|
|
|
if (wcommand == NULL) {
|
2009-11-04 00:54:27 +05:30
|
|
|
fprintf(stderr, "Unicode Handling Error (malloc)\n");
|
2011-06-05 18:35:47 +05:30
|
|
|
exit(EXIT_FAILURE);
|
2009-11-04 00:54:27 +05:30
|
|
|
}
|
2011-06-05 18:35:47 +05:30
|
|
|
mbstowcs(wcommand, command, wcommand_characters + 1);
|
2009-11-04 00:54:27 +05:30
|
|
|
wcommand_columns = wcswidth(wcommand, -1);
|
2011-06-05 18:35:47 +05:30
|
|
|
#endif /* WITH_WATCH8BIT */
|
2009-11-04 00:54:27 +05:30
|
|
|
|
2002-11-29 04:39:48 +05:30
|
|
|
get_terminal_size();
|
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
/* Catch keyboard interrupts so we can put tty back in a sane
|
|
|
|
* state. */
|
2002-11-29 04:39:48 +05:30
|
|
|
signal(SIGINT, die);
|
|
|
|
signal(SIGTERM, die);
|
|
|
|
signal(SIGHUP, die);
|
|
|
|
signal(SIGWINCH, winch_handler);
|
|
|
|
|
|
|
|
/* Set up tty for curses use. */
|
|
|
|
curses_started = 1;
|
|
|
|
initscr();
|
2011-06-05 18:35:47 +05:30
|
|
|
if (option_color) {
|
|
|
|
if (has_colors()) {
|
|
|
|
start_color();
|
|
|
|
use_default_colors();
|
|
|
|
init_ansi_colors();
|
|
|
|
} else
|
|
|
|
option_color = 0;
|
|
|
|
}
|
2002-11-29 04:39:48 +05:30
|
|
|
nonl();
|
|
|
|
noecho();
|
|
|
|
cbreak();
|
|
|
|
|
2009-11-24 05:30:46 +05:30
|
|
|
if (precise_timekeeping)
|
|
|
|
next_loop = get_time_usec();
|
|
|
|
|
2002-11-29 04:39:48 +05:30
|
|
|
for (;;) {
|
|
|
|
time_t t = time(NULL);
|
|
|
|
char *ts = ctime(&t);
|
|
|
|
int tsl = strlen(ts);
|
|
|
|
char *header;
|
|
|
|
FILE *p;
|
|
|
|
int x, y;
|
|
|
|
int oldeolseen = 1;
|
|
|
|
|
|
|
|
if (screen_size_changed) {
|
|
|
|
get_terminal_size();
|
|
|
|
resizeterm(height, width);
|
|
|
|
clear();
|
|
|
|
/* redrawwin(stdscr); */
|
|
|
|
screen_size_changed = 0;
|
|
|
|
first_screen = 1;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2002-11-29 04:39:48 +05:30
|
|
|
|
2003-02-09 12:57:16 +05:30
|
|
|
if (show_title) {
|
2011-06-05 18:35:47 +05:30
|
|
|
/*
|
|
|
|
* left justify interval and command, right
|
|
|
|
* justify time, clipping all to fit window
|
|
|
|
* width
|
|
|
|
*/
|
2009-11-04 00:54:27 +05:30
|
|
|
int hlen = asprintf(&header, "Every %.1fs: ", interval);
|
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
/*
|
|
|
|
* the rules:
|
|
|
|
* width < tsl : print nothing
|
|
|
|
* width < tsl + hlen + 1: print ts
|
|
|
|
* width = tsl + hlen + 1: print header, ts
|
|
|
|
* width < tsl + hlen + 4: print header, ..., ts
|
|
|
|
* width < tsl + hlen + wcommand_columns: print
|
|
|
|
* header, truncated wcommand,
|
|
|
|
* ..., ts
|
|
|
|
* width > "": print header, wcomand, ts
|
|
|
|
* this is slightly different from how it used to be
|
|
|
|
*/
|
|
|
|
if (width >= tsl) {
|
|
|
|
if (width >= tsl + hlen + 1) {
|
2009-11-04 00:54:27 +05:30
|
|
|
mvaddstr(0, 0, header);
|
2011-06-05 18:35:47 +05:30
|
|
|
if (width >= tsl + hlen + 2) {
|
|
|
|
if (width < tsl + hlen + 4) {
|
|
|
|
mvaddstr(0,
|
|
|
|
width - tsl -
|
|
|
|
4, "... ");
|
|
|
|
} else {
|
2011-12-20 16:42:37 +05:30
|
|
|
#ifdef WITH_WATCH8BIT
|
2011-06-05 18:35:47 +05:30
|
|
|
if (width <
|
|
|
|
tsl + hlen +
|
|
|
|
wcommand_columns) {
|
|
|
|
/* print truncated */
|
2009-11-04 00:54:27 +05:30
|
|
|
int avail_columns = width - tsl - hlen;
|
|
|
|
int using_columns = wcommand_columns;
|
|
|
|
int using_characters = wcommand_characters;
|
2011-06-05 18:35:47 +05:30
|
|
|
while (using_columns > avail_columns - 4) {
|
2009-11-04 00:54:27 +05:30
|
|
|
using_characters--;
|
2011-06-05 18:35:47 +05:30
|
|
|
using_columns
|
|
|
|
=
|
|
|
|
wcswidth
|
|
|
|
(wcommand,
|
|
|
|
using_characters);
|
2009-11-04 00:54:27 +05:30
|
|
|
}
|
2011-06-05 18:35:47 +05:30
|
|
|
mvaddnwstr(0,
|
|
|
|
hlen,
|
|
|
|
wcommand,
|
|
|
|
using_characters);
|
|
|
|
mvaddstr(0,
|
|
|
|
width -
|
|
|
|
tsl -
|
|
|
|
4,
|
|
|
|
"... ");
|
|
|
|
} else {
|
|
|
|
mvaddwstr(0,
|
|
|
|
hlen,
|
|
|
|
wcommand);
|
2009-11-04 00:54:27 +05:30
|
|
|
}
|
2011-12-20 16:42:37 +05:30
|
|
|
#else
|
2011-06-05 18:35:47 +05:30
|
|
|
mvaddnstr(0, hlen,
|
|
|
|
command,
|
|
|
|
width - tsl -
|
|
|
|
hlen);
|
|
|
|
#endif /* WITH_WATCH8BIT */
|
2009-11-04 00:54:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mvaddstr(0, width - tsl + 1, ts);
|
|
|
|
}
|
|
|
|
|
2003-02-09 12:57:16 +05:30
|
|
|
free(header);
|
|
|
|
}
|
2002-11-29 04:39:48 +05:30
|
|
|
|
2009-11-24 05:30:43 +05:30
|
|
|
/* allocate pipes */
|
2011-06-05 18:35:47 +05:30
|
|
|
if (pipe(pipefd) < 0)
|
|
|
|
err(7, "pipe");
|
2009-11-24 05:30:43 +05:30
|
|
|
|
|
|
|
/* flush stdout and stderr, since we're about to do fd stuff */
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
/* fork to prepare to run command */
|
2011-06-05 18:35:47 +05:30
|
|
|
child = fork();
|
|
|
|
|
|
|
|
if (child < 0) { /* fork error */
|
|
|
|
err(2, "fork");
|
|
|
|
} else if (child == 0) { /* in child */
|
|
|
|
close(pipefd[0]); /* child doesn't need read side of pipe */
|
|
|
|
close(1); /* prepare to replace stdout with pipe */
|
|
|
|
if (dup2(pipefd[1], 1) < 0) { /* replace stdout with write side of pipe */
|
|
|
|
err(3, "dup2");
|
2009-11-24 05:30:43 +05:30
|
|
|
}
|
2011-06-05 18:35:47 +05:30
|
|
|
dup2(1, 2); /* stderr should default to stdout */
|
2009-11-24 05:30:43 +05:30
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
if (option_exec) { /* pass command to exec instead of system */
|
|
|
|
if (execvp(command_argv[0], command_argv) == -1) {
|
|
|
|
err(4, "exec");
|
2009-11-24 05:30:43 +05:30
|
|
|
}
|
|
|
|
} else {
|
2011-06-05 18:35:47 +05:30
|
|
|
status = system(command); /* watch manpage promises sh quoting */
|
|
|
|
|
|
|
|
/* propagate command exit status as child exit status */
|
|
|
|
if (!WIFEXITED(status)) { /* child exits nonzero if command does */
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
exit(WEXITSTATUS(status));
|
|
|
|
}
|
2009-11-24 05:30:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* otherwise, we're in parent */
|
2011-06-05 18:35:47 +05:30
|
|
|
close(pipefd[1]); /* close write side of pipe */
|
|
|
|
if ((p = fdopen(pipefd[0], "r")) == NULL)
|
|
|
|
err(5, "fdopen");
|
2009-11-24 05:30:43 +05:30
|
|
|
|
2003-02-09 12:57:16 +05:30
|
|
|
for (y = show_title; y < height; y++) {
|
2002-11-29 04:39:48 +05:30
|
|
|
int eolseen = 0, tabpending = 0;
|
2011-12-20 16:42:37 +05:30
|
|
|
#ifdef WITH_WATCH8BIT
|
2009-11-04 00:54:27 +05:30
|
|
|
wint_t carry = WEOF;
|
2011-06-05 18:35:47 +05:30
|
|
|
#endif /* WITH_WATCH8BIT */
|
2002-11-29 04:39:48 +05:30
|
|
|
for (x = 0; x < width; x++) {
|
2011-12-20 16:42:37 +05:30
|
|
|
#ifdef WITH_WATCH8BIT
|
2009-11-04 00:54:27 +05:30
|
|
|
wint_t c = ' ';
|
2011-12-20 16:42:37 +05:30
|
|
|
#else
|
|
|
|
int c = ' ';
|
2011-06-05 18:35:47 +05:30
|
|
|
#endif /* WITH_WATCH8BIT */
|
2002-11-29 04:39:48 +05:30
|
|
|
int attr = 0;
|
|
|
|
|
|
|
|
if (!eolseen) {
|
2011-06-05 18:35:47 +05:30
|
|
|
/* if there is a tab pending, just
|
|
|
|
* spit spaces until the next stop
|
|
|
|
* instead of reading characters */
|
|
|
|
if (!tabpending)
|
2011-12-20 16:42:37 +05:30
|
|
|
#ifdef WITH_WATCH8BIT
|
2011-06-05 18:35:47 +05:30
|
|
|
do {
|
|
|
|
if (carry == WEOF) {
|
|
|
|
c = my_getwc(p);
|
|
|
|
} else {
|
|
|
|
c = carry;
|
|
|
|
carry = WEOF;
|
|
|
|
}
|
|
|
|
} while (c != WEOF
|
|
|
|
&& !isprint(c)
|
|
|
|
&& c < 12
|
|
|
|
&& wcwidth(c) == 0
|
|
|
|
&& c != L'\n'
|
|
|
|
&& c != L'\t'
|
|
|
|
&& (c != L'\033'
|
|
|
|
|| option_color !=
|
|
|
|
1));
|
2011-12-20 16:42:37 +05:30
|
|
|
#else
|
|
|
|
do
|
|
|
|
c = getc(p);
|
|
|
|
while (c != EOF && !isprint(c)
|
|
|
|
&& c != '\n'
|
|
|
|
&& c != '\t'
|
2011-06-05 18:35:47 +05:30
|
|
|
&& (c != L'\033'
|
|
|
|
|| option_color !=
|
|
|
|
1));
|
|
|
|
#endif /* WITH_WATCH8BIT */
|
|
|
|
if (c == L'\033' && option_color == 1) {
|
|
|
|
x--;
|
|
|
|
process_ansi(p);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == L'\n')
|
2002-11-29 04:39:48 +05:30
|
|
|
if (!oldeolseen && x == 0) {
|
|
|
|
x = -1;
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
eolseen = 1;
|
2009-11-04 00:54:27 +05:30
|
|
|
else if (c == L'\t')
|
2002-11-29 04:39:48 +05:30
|
|
|
tabpending = 1;
|
2011-12-20 16:42:37 +05:30
|
|
|
#ifdef WITH_WATCH8BIT
|
2011-06-05 18:35:47 +05:30
|
|
|
if (x == width - 1 && wcwidth(c) == 2) {
|
|
|
|
y++;
|
|
|
|
x = -1; /* process this double-width */
|
|
|
|
carry = c; /* character on the next line */
|
|
|
|
continue; /* because it won't fit here */
|
|
|
|
}
|
|
|
|
if (c == WEOF || c == L'\n'
|
|
|
|
|| c == L'\t')
|
|
|
|
c = L' ';
|
2011-12-20 16:42:37 +05:30
|
|
|
#else
|
|
|
|
if (c == EOF || c == '\n' || c == '\t')
|
|
|
|
c = ' ';
|
2011-06-05 18:35:47 +05:30
|
|
|
#endif /* WITH_WATCH8BIT */
|
2002-11-29 04:39:48 +05:30
|
|
|
if (tabpending && (((x + 1) % 8) == 0))
|
|
|
|
tabpending = 0;
|
|
|
|
}
|
|
|
|
move(y, x);
|
|
|
|
if (option_differences) {
|
2011-12-20 16:42:37 +05:30
|
|
|
#ifdef WITH_WATCH8BIT
|
2011-06-05 18:35:47 +05:30
|
|
|
cchar_t oldc;
|
|
|
|
in_wch(&oldc);
|
2002-11-29 04:39:48 +05:30
|
|
|
attr = !first_screen
|
2011-06-05 18:35:47 +05:30
|
|
|
&& ((wchar_t) c != oldc.chars[0]
|
2002-11-29 04:39:48 +05:30
|
|
|
||
|
|
|
|
(option_differences_cumulative
|
2011-06-05 18:35:47 +05:30
|
|
|
&& (oldc.
|
|
|
|
attr & A_ATTRIBUTES)));
|
2011-12-20 16:42:37 +05:30
|
|
|
#else
|
|
|
|
chtype oldch = inch();
|
|
|
|
unsigned char oldc = oldch & A_CHARTEXT;
|
|
|
|
attr = !first_screen
|
|
|
|
&& ((unsigned char)c != oldc
|
|
|
|
||
|
|
|
|
(option_differences_cumulative
|
|
|
|
&& (oldch & A_ATTRIBUTES)));
|
2011-06-05 18:35:47 +05:30
|
|
|
#endif /* WITH_WATCH8BIT */
|
2002-11-29 04:39:48 +05:30
|
|
|
}
|
|
|
|
if (attr)
|
|
|
|
standout();
|
2011-12-20 16:42:37 +05:30
|
|
|
#ifdef WITH_WATCH8BIT
|
2011-06-05 18:35:47 +05:30
|
|
|
addnwstr((wchar_t *) & c, 1);
|
2011-12-20 16:42:37 +05:30
|
|
|
#else
|
|
|
|
addch(c);
|
2011-06-05 18:35:47 +05:30
|
|
|
#endif /* WITH_WATCH8BIT */
|
2002-11-29 04:39:48 +05:30
|
|
|
if (attr)
|
|
|
|
standend();
|
2011-12-20 16:42:37 +05:30
|
|
|
#ifdef WITH_WATCH8BIT
|
2011-06-05 18:35:47 +05:30
|
|
|
if (wcwidth(c) == 0) {
|
|
|
|
x--;
|
|
|
|
}
|
|
|
|
if (wcwidth(c) == 2) {
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
#endif /* WITH_WATCH8BIT */
|
2002-11-29 04:39:48 +05:30
|
|
|
}
|
|
|
|
oldeolseen = eolseen;
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2009-11-24 05:30:43 +05:30
|
|
|
fclose(p);
|
|
|
|
|
|
|
|
/* harvest child process and get status, propagated from command */
|
2011-06-05 18:35:47 +05:30
|
|
|
if (waitpid(child, &status, 0) < 0)
|
|
|
|
err(8, "waitpid");
|
2009-11-24 05:30:43 +05:30
|
|
|
|
|
|
|
/* if child process exited in error, beep if option_beep is set */
|
|
|
|
if ((!WIFEXITED(status) || WEXITSTATUS(status))) {
|
2011-06-05 18:35:47 +05:30
|
|
|
if (option_beep)
|
|
|
|
beep();
|
|
|
|
if (option_errexit)
|
|
|
|
exit(8);
|
2009-11-24 05:30:43 +05:30
|
|
|
}
|
2002-11-29 04:39:48 +05:30
|
|
|
|
|
|
|
first_screen = 0;
|
|
|
|
refresh();
|
2009-11-24 05:30:46 +05:30
|
|
|
if (precise_timekeeping) {
|
|
|
|
watch_usec_t cur_time = get_time_usec();
|
2011-06-05 18:35:47 +05:30
|
|
|
next_loop += USECS_PER_SEC * interval;
|
2009-11-24 05:30:46 +05:30
|
|
|
if (cur_time < next_loop)
|
|
|
|
usleep(next_loop - cur_time);
|
|
|
|
} else
|
2011-06-05 18:35:47 +05:30
|
|
|
usleep(interval * 1000000);
|
2002-11-29 04:39:48 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-11-29 04:39:48 +05:30
|
|
|
endwin();
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2011-06-05 18:35:47 +05:30
|
|
|
return EXIT_SUCCESS;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|