[net.sources] program to repetitively display command on crt

dunigan@ornl-msr (Tom Dunigan) (11/10/84)

/* display.c
 *   do screen update with given command
 *  usage is    display [-seconds] command [commandargs]
 *    examples:   display w    or   display ls -l   or   display -2 df
 *   to build, cc   with  -lcurses -ltermlib
 */

#include <stdio.h>
#include <curses.h>

char c;
char command[80];
FILE *p, *popen();
int interval=5;   /* number of seconds to pause */

main(argc,argv)
	int argc;
	char **argv;
{
	int row,i;

	if (argc==1){printf("Usage display [-seconds]  command\n");exit();}
	if (**(argv+1) == '-') {  /* interval specified */
		if (--argc == 1){printf("Usage display [-seconds] command\n");exit();}
		interval = atoi(*++argv + 1);
	}
	while(--argc>0){
		strcat(command,*++argv);
		strcat(command," ");
	}
	initscr();
	clear();
	for(;;){
		move(0,0);
		row=0;
		p=popen(command,"r");
		if (p==NULL){printf("failed\n"); exit();}
		while( (c=getc(p)) != EOF){
			if (c == '\n'){clrtoeol();move(++row,0);}
			 else addch(c);
		}
		pclose(p);
		clrtobot();
		refresh();
		sleep(interval);
	}
}

rwl@uvacs.UUCP (Ray Lubinsky) (11/13/84)

> /* display.c
>  *   do screen update with given command
>  *  usage is    display [-seconds] command [commandargs]
>  *    examples:   display w    or   display ls -l   or   display -2 df
>  *   to build, cc   with  -lcurses -ltermlib
>  */
> 
  ... etc.

------------------------------------------------------------------------------

   Oddly enough, I had just written a program like this called rep.c which, in
some ways, was less versatile than display.c from the referenced article.  His
program accepted command line arguments sensibly, whereas mine just assumed
there was one argument, so displaying ls -l would require typing  rep 'ls -l'.

   Naturally, I compiled display.c and ran it.  Very nice.  Displayed just like
my simpler version.  Unfortunately display.c, in true Turing machine fashion,
has a halting problem.  It's not designed to halt!  I tried hitting the
break key to exit and realized that display.c has no call to endwin() to
restore the terminal's characteristics.  Another problem (though I didn't test
it) was the lack of reference to the curses(1) value LINES which tells how
long the terminal screen is; apparently display.c would just scroll when it
received more lines than this, which seems counterproductive for a screen-
oriented program.

   The following is an amalgam of the two programs.  It accepts the same
command line arguments, but jumps on the interrupt signal to a mop-up routine
which includes endwin().  Here goes:

--------------------------   CUT      HERE   --------------------------------
/******************************************************************************
 *									      *
 * 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 repition every x seconds.  *
 *									      *
 ******************************************************************************/

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

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

FILE	*input;				/* Pipe from 'source' */
char	*source = "";			/* Source program to run repeatedly */
char	*progname;			/* Name of this program (for errors) */
int	interval = 1;			/* Seconds between repitions */

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;
	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);
	initscr();
	crmode();
	nonl();
	clear();
	for (;;) {
		if ((input = popen(source,"r")) == NULL) {
			fprintf(stderr,"%s: can't run %s\n",progname,source);
			endrep();
		}
		for (i = 0; (i < LINES) && (NextLine != NULL); i++) {
			mvaddstr(i,0,buf);
			clrtoeol();
		}
		pclose(input);
		clrtobot();
		refresh();
		sleep(interval);
	}
}

endrep()
{
	signal(SIGINT,SIG_IGN);
	if (input != NULL)
		pclose(input);
	clear();
	refresh();
	endwin();
	exit(0);
}

badargs()
{
	fprintf(stderr,"Usage: %s [-n] command\n",progname);
	exit(1);
}

/*
------------------------------------------------------------------------------

Ray Lubinsky		     University of Virginia, Dept. of Computer Science
			     uucp: decvax!mcnc!ncsu!uvacs!rwl
*/

guy@proper.UUCP (Guy Hillyer ) (11/16/84)

I wonder how many times this program has been written.
I also wrote one... I called it "dyna.c" ...