[net.sources] rep - repeatedly execute a command

karsh@geowhiz.UUCP (Bruce Karsh) (03/25/85)

Last November, Ray Lubinsky put a program on the net called rep.  It
repetitively displayed a command on the crt, using termcap so that only
the characters which changed between repetitions were updated on the screen.

I have fixed some minor problems with this program and have added
a man page.

# This is a shell archive.  Remove anything before this line, then
# unpack it by saving it in a file and typing "sh file".  (Files
# unpacked will be owned by you and have default permissions.)
#
# This archive contains:
# Makefile rep.1 rep.c

echo x - Makefile
cat > "Makefile" << '//E*O*F Makefile//'
rep: rep.c
	cc -O rep.c -lcurses -ltermlib -o rep
//E*O*F Makefile//

echo x - rep.1
cat > "rep.1" << '//E*O*F rep.1//'
.TH REP 1 local 
.SH NAME
rep - repeatedly execute a command
.SH SYNOPSIS
rep [-n] command
.SH DESCRIPTION
.I Rep
repeatedly executes a command and prints out the
output in the same place each time.  (It uses
cursor optimization routines to only update chars
that have changed.)
.PP
The -n option specifies the number of seconds between
repetitions.
.SH EXAMPLES
.B rep ps -ef
.PP
prints out a display of system activity.
.PP
.B rep -1 date
.PP
puts a digital clock on the screen.
.SH "SEE ALSO"
.SH BUGS
//E*O*F rep.1//

echo x - rep.c
cat > "rep.c" << '//E*O*F rep.c//'
/******************************************************************************
 *									      *
 * rep -- run a program repeatedly using 'curses'.			      *
 *									      *
 *	Usage: rep [-n] command						      *
 *									      *
 *	Permits the user to watch a program like 'w' or 'finger' change with  *
 *	time.  Given an argument '-x' will cause a repetion every x seconds.  *
 *									      *
 * ORIGINAL								      *
 * Ray Lubinsky University of Virginia, Dept. of Computer Science             *
 *	     uucp: decvax!mcnc!ncsu!uvacs!rwl                                 *
 * MODIFIED 3/85							      *
 * Bruce Karsh, Univ. Wisconsin, Madison.  Dept. of Geophysics.               *
 *           uucp: uwvax\!geowhiz\!karsh				      *
 ******************************************************************************/

#include <curses.h>
#include <signal.h>

#define	NextLine	fgets(buf,sizeof buf,input)

FILE	*input;				/* Pipe from 'source' */
char	source[512];			/* Source program to run repeatedly */
char	*progname;			/* Name of this program (for errors) */
int	interval = 1;			/* Seconds between repitions */
int	deathflag = 0;			/* Set by interupt trap routine */

main(argc,argv)

	int	argc;
	char	**argv;
{
	int	endrep();		/* Mop up routine on break */
	FILE	*popen();

	char	buf[BUFSIZ];		/* Buffer holds a line from 'source' */
	int	i;

	progname = *argv;
        source[0]=0;
	if (argc == 1)
		badargs();
	if (**(argv+1) == '-')		/* User specified interval */
		if ((--argc == 1) || ((interval = atoi(*(++argv)+1)) == 0))
			badargs();
	while (--argc > 0) {		/* Argument becomes source program */
		strcat(source,*++argv);
		strcat(source," ");
	}
	signal(SIGINT,endrep);
	if(deathflag == 1)goto die;
	initscr();
	if(deathflag == 1)goto die;
	crmode();
	if(deathflag == 1)goto die;
	nonl();
	if(deathflag == 1)goto die;
	clear();
	if(deathflag == 1)goto die;
	for (;;) {
		if(deathflag == 1)goto die;
		if ((input = popen(source,"r")) == NULL) {
			fprintf(stderr,"%s: can't run %s\n",progname,source);
			goto die;
		}
		for (i = 0; (i < LINES) && (NextLine != NULL); i++) {
			mvaddstr(i,0,buf);
			clrtoeol();
			if(deathflag == 1)goto die;
		}
		if(deathflag == 1)goto die;
		pclose(input);
		if(deathflag == 1)goto die;
		clrtobot();
		if(deathflag == 1)goto die;
		refresh();
		if(deathflag == 1)goto die;
		sleep(interval);
		if(deathflag == 1)goto die;
	}
die:
signal(SIGINT,SIG_IGN);
if (input != NULL)
	pclose(input);
clear();
refresh();
endwin();
exit(0);
}

endrep()
{
deathflag=1;
}

badargs()
{
	fprintf(stderr,"Usage: %s [-n] command\n",progname);
	exit(1);
}
//E*O*F rep.c//

echo Possible errors detected by \'wc\' [hopefully none]:
temp=/tmp/shar$$
trap "rm -f $temp; exit" 0 1 2 3 15
cat > $temp <<\!!!
      2      9     50 Makefile
     24     91    505 rep.1
    101    333   2603 rep.c
    127    433   3158 total
!!!
wc  Makefile rep.1 rep.c | sed 's=[^ ]*/==' | diff -b $temp -
exit 0
-- 
Bruce Karsh                           |
U. Wisc. Dept. Geology and Geophysics |
1215 W Dayton, Madison, WI 53706      | This space for rent.
(608) 262-1697                        |
{ihnp4,seismo}!uwvax!geowhiz!karsh    |