Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/5/84; site uvacs.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!decvax!mcnc!ncsu!uvacs!rwl From: rwl@uvacs.UUCP (Ray Lubinsky) Newsgroups: net.sources Subject: Re: program to repetitively display command on crt Message-ID: <1667@uvacs.UUCP> Date: Mon, 12-Nov-84 22:04:23 EST Article-I.D.: uvacs.1667 Posted: Mon Nov 12 22:04:23 1984 Date-Received: Wed, 14-Nov-84 05:33:52 EST References: <5699@brl-tgr.ARPA> Organization: U.Va. CS dept. Charlottesville, VA Lines: 117 > /* 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 #include #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 */