/* display.c - repeatedly display command through curses * vix 18apr86 [written] * vix 15dec86 [major overhaul] * Dave Remien [added on demand updates and "q" to exit] * dar 27nov00 [ctrl-l to clear, cleanup] * dar 25mar01 [loop counter, current time, max lines, initscr to clean screen from scratch] */ #include #include #include #include #include #define DEFAULT_DELAY 5 #define DISPLAY_VERSION "1.2" static char *Command; static int Delay; static void die(), display(), parse_args(), usage(); char lt[128]; int ntimes; main(argc, argv) int argc; char *argv[]; { int i, c; for(i = 0; i < 128; i++) { lt[i] = 0; }; ntimes = 0; parse_args(argc, argv); signal(SIGHUP, die); signal(SIGINT, die); signal(SIGQUIT, die); signal(SIGTERM, die); initscr(); cbreak(); noecho(); nodelay(stdscr, TRUE); /*halfdelay(1);*/ clear(); while (TRUE) { display(); for(i = 0; i < (Delay * 5) ; i ++){ c = getch(); if(c == 81 || c == 113){ die(); }else if(c == 67 || c == 99 || c == 12){ initscr(); cbreak(); noecho(); nodelay(stdscr, TRUE); /*halfdelay(1);*/ clear(); i = Delay * 6; }else if(c != ERR){ move(0, 0); clrtobot(); refresh(); i = Delay * 6; }else if(c == ERR){ usleep(200000); }else{ i = Delay * 6; } } } } void die() { move(LINES-1, 0); clrtoeol(); refresh(); endwin(); exit(0); } void display() { FILE *fp; int ch; char *dt; time_t tnow; int nlines; char count[64]; nlines=0; ntimes++; if (!(fp = popen(Command, "r"))) { perror("popen"); exit(1); } move(0, 0); while (EOF != (ch = fgetc(fp)) && nlines < (LINES - 1)) { if (ch == '\n'){ clrtoeol(); nlines++; } addch(ch); } clrtoeol(); clrtobot(); refresh(); move(LINES-1, 0 ); time(&tnow); dt = ctime(&tnow); strncpy(lt, dt, 19); sprintf(count, "Loop count = %d", ntimes); addstr(count); addstr(" "); addstr(lt); refresh(); pclose(fp); } void parse_args(argc, argv) int argc; char *argv[]; { extern void usage(); auto int argn, delay_found; Command = NULL; Delay = DEFAULT_DELAY; delay_found = FALSE; for (argn = 1; argn < argc; argn++) { if (argv[argn][0] == '-') if (delay_found) usage(); /* already got this once */ else if (!isdigit(argv[argn][1])) usage(); /* not a numeric */ else { Delay = atoi(&argv[argn][1]); delay_found = TRUE; } else if (Command != NULL) usage(); /* already got this once */ else Command = argv[argn]; } if (Command == NULL) usage(); /* no Command on line */ } void usage() { extern char *getenv(); auto char *shell = getenv("SHELL"); fprintf(stderr, "\ usage: display [-] \n\ = # of seconds between displays, default=%d\n\ = command to display, quoted if it contains blanks\n\n\ Display (a.k.a. follow) version %s\n\n", DEFAULT_DELAY, DISPLAY_VERSION); if (strcmp(shell, "/bin/sh")) fprintf(stderr, "\ NOTE: /bin/sh will be used to process the command, not SHELL (%s)\n", shell); exit(1); }