1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/* From: Tony Rems <rembo@unisoft.com>
* Newsgroups: comp.sources.unix
* Subject: v24i084: Repeatedly execute a program under curses(3)
* Date: 26 Mar 91 21:41:30 GMT
*
* Slighty modified, and corrected, Francois Pinard, 91-04.
*/
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/fcntl.h>
int die_flag;
void die ();
extern FILE *popen ();
extern int pclose ();
extern long time ();
extern char *ctime ();
/*-----------------------------------------.
* Decode parameters and launch execution. |
*-----------------------------------------*/
char *program_name;
int
main (argc, argv)
int argc;
char *argv[];
{
int hor = 1, ver = 0;
FILE *piper;
char buf[180];
char cmd[128];
int count = 1;
long timer;
int nsecs = 2;
program_name = argv[0];
if (argc < 2)
{
fprintf (stderr, "Usage: %s COMMAND [-n SLEEP] [ARG ...]\n", argv[0]);
exit (1);
}
/* If -n is specified, convert the next argument to the numver for the
number of seconds. */
if (strcmp (argv[1], "-n") == 0)
{
nsecs = atoi (argv[2]);
count = 3;
if (nsecs == 0 || argc < 4)
{
fprintf (stderr, "Usage: %s COMMAND [-n SLEEP] [ARG ...]\n", argv[0]);
exit (1);
}
}
/* Build command string to give to popen. */
bzero (cmd, sizeof (cmd));
strcpy (cmd, argv[count]);
while (++count < argc)
{
strcat (cmd, " ");
strcat (cmd, argv[count]);
}
/* Catch keyboard interrupts so we can put tty back in a sane state. */
signal (SIGINT, die);
signal (SIGTERM, die);
signal (SIGHUP, die);
/* Set up tty for curses use. */
initscr ();
nonl ();
noecho ();
crmode ();
die_flag = 0;
while (!die_flag)
{
/* Put up time interval and current time. */
hor = 1;
move (hor, ver);
time (&timer);
printw ("Every %d seconds\t\t%s\t\t%s", nsecs, cmd, ctime (&timer));
hor = 3;
/* Open pipe to command. */
if ((piper = popen (cmd, "r")) == (FILE *)NULL)
{
perror ("popen");
exit (2);
}
/* Read in output from the command and make sure that it will fit on 1
screen. */
while ((fgets(buf, sizeof (buf), piper) != NULL) && hor < LINES)
{
buf[COLS-1] = '\0';
mvaddstr (hor, ver, buf);
clrtoeol ();
hor++;
}
pclose (piper);
if (hor < LINES)
{
move (hor, ver);
clrtobot ();
}
refresh ();
sleep (nsecs);
}
nocrmode ();
echo ();
nl ();
endwin ();
return (0);
}
/*------------------------------------.
| Stop the program on any interrupt. |
`------------------------------------*/
void
die ()
{
die_flag = 1;
}
|