Aucbcory.422
net.sources
utzoo!decvax!ucbvax!ARPAVAX:CAD:ESVAX:Cory:erik
Thu Apr 29 03:21:47 1982
New Version of upd.c
/******************************************************************************
* upd.c - popen a process and use its output to update the screen
*
* argv[1] is popen'ed so quote your command if it's more than one arg.
* argv[2] is number of seconds to sleep between cycles.
*
* During its sojourn in Berkeley, a number a features were added:
*
* Commands: type characters during program execution and things
*              are now likely to happen!
*
*      Char    Action
*      ----    ------
*       ^L     clear/refresh
*       c      clear/refresh
*       r      clear/refresh
*       e      exit            Note: a signal (INT, QUIT, TERM) will also
*       q      exit            stop the program, and quicker too!
*
* Windows: Those commands include windows. Type a number between 0 and 9
*              and if there's program output in that window, you will see it.
*
* To Compile:
*      cc upd.c -lcurses -ltermlib -n -O -o upd
*
* Author(s)
*      Andy Tannenbaum
*              WECo/BTL Whippany, NJ
*              (UUCP: mhtsa!harpo!floyd!trb)
*      Erik Fair
*              UCB/CS Berkeley, CA
*              (UUCP: ucbvax!ucbcory!erik)
*              (ARPA: Cory.erik@Berkeley)
*****************************************************************************/

#include <CURSES.H>
#include <SIGNAL.H>
#include <CTYPE.H>

/*      If your system does NOT have the ioctl FIONREAD, then #define
*      KLUDGE, so that the command input code will compile correctly
*      {I use alarm(2) instead}. This means that vanilla v7 systems,
*      and other systems w/o the new tty driver (as it appears in
*      VMUNIX) will have to use KLUDGE.
*/
#define WINDOWS 10              /* Number of windows of output */
#define BUFSIZE WINDOWS*(24*80) /* Windows of 24*80 (Assuming 24 Lines) */
#define USAGE   "usage: upd 'command_string' [delay_time]\n"

char    buf[BUFSIZE];           /* Massive output buffer */
char    *screen[WINDOWS];       /* Screen pointers into output buffer */
int     scrn;                   /* Screen Number (index to `screen') */
int     timeout;                /* For input kludge */

/******************************************************************************
*       Begin MainLine
*/

main(argc,argv)
int     argc;
char    **argv;
{
register int    i;
register int    bufcnt;
register int    lncnt;
extern int      LINES;  /* Curses looks this up in termcap */
unsigned        sleeptime;
FILE            *pp;
int             wincnt,poof();

       /* Must have at least two arguements and not more than three */
       if ((argc  3)) {
               fprintf(stderr,USAGE);
               exit(1);
       }
       /* sleep for sleeptime seconds between cycles */
       sleeptime=((argc > 2) ? atoi(argv[2]) : 0);
       fprintf(stderr,"Delay is %u seconds between updates\n",sleeptime);

       signal(SIGINT,poof);    /* Set up signals so that when one comes, we */
       signal(SIGTERM,poof);   /* exit gracefully from the program          */
       signal(SIGQUIT,poof);

       scrn=0;                 /* Start with screen Number Zero */
       screen[0]=(&buf[0]);    /* Always */
       initscr();              /* Curses screen initialization */
       savetty();              /* Save current tty state */
       noecho();               /* Set no echoing */
       crmode();               /* set cbreak mode */

/******************************************************************************
*       Begin Main Loop
*/
       for(;;) {
               /* if popen fails, we're hurting, so break */
               if((pp=popen(argv[1],"r")) == NULL) {
                       fprintf(stderr,"upd: popen failed.\n");
                       poof();
               }
               bufcnt=fread(buf,1,BUFSIZE,pp); /* Grab the output */
               pclose(pp);                     /* Close the channel */
               buf[bufcnt]=0;                  /* Zero that last char */
               wincnt = lncnt = 1;             /* Initialize some stuff */
               for( i=0 ; i  0))
                       scrn--;         /* If window is empty, then backup */

               mvaddstr(0,0,screen[scrn]);
               clrtobot();
               refresh();
               docomd();               /* Do any commands that were input */
               sleep(sleeptime);       /* Sleep awhile */
       }
}

/**********************************************************************
*       Exit from the program gracefully
*/

poof()
{
       clear();        /* Clear the screen */
       refresh();
       resetty();      /* Put tty back the way we found it */
       exit();         /* Go home */
}

/**********************************************************************
*       Do commands input from the keyboard
*/

docomd()
{
char    comnd;
long    count;
int     numb;
int     wakeup();

#ifdef KLUDGE
       timeout=FALSE;
       signal(SIGALRM,wakeup);
       alarm(3);                       /* Three seconds for input */
#else
       ioctl(0,FIONREAD,&count);       /* check standard input for chars */
       if (count == 0L)
               return;
#endif
       comnd=toascii(getch());         /* Wait here for alarm or input */
#ifdef KLUDGE
       if (timeout == TRUE)
               return;                 /* No input, go back to whatever */
       else {
               timeout=FALSE;          /* No timeout */
               alarm(0);               /* Shut off alarm clock */
       }
#endif
       /* Convert input to lower case, if upper case */
       comnd=((isupper(comnd)) ? tolower(comnd) : comnd);

       /* Selecting new window? */
       if(isdigit(comnd)) {
               numb=(comnd-'0');
               if ((numb <= POOF(); QUIT NUMBER NEW && FPRINTF(STDERR,"\07"); REFRESH (SCREEN[NUMB] CLEAR(); PROGRAM DEFAULT: !="NULL))" * </PRE WAIT /* : EXIT WAKEUP() A C E UP FOR #ENDIF SET Q R CLRTOBOT(); SCRN="numb;" }; REFRESH(); KLUDGE NO BREAK; ELSE { } TIMEOUT="TRUE;" \014 */ WINDOWS) SWITCH(COMND) SCREEN #IFDEF WAKING CLEAR AFTER FROM MVADDSTR(0,0,SCREEN[SCRN]); WINDOW CASE THE INPUT SUCH BLEEP, NOTHING THERE CONTROL-L /****************************************************************************** COMMAND>
<HR>
This Usenet Oldnews Archive
article may be copied and distributed freely, provided:
<P>
1. There is no money collected for the text(s) of the articles.
<BR>
2. The following notice remains appended to each copy:
<P>
<EM>The Usenet Oldnews Archive: Compilation Copyright&copy 1981, 1996
<BR> Bruce Jones, Henry Spencer, David Wiseman.</EM>
<P>
<HR>
<BR>Return to <A HREF="NET.sources-index.html">NET.sources index</A>
<BR>Return to the
       <A HREF="../index.html">Usenet Oldnews Archive index</A>
</HTML>

-----------------------------------------------------------------
gopher://quux.org/ conversion by John Goerzen <[email protected]>
of http://communication.ucsd.edu/A-News/


This Usenet Oldnews Archive
article may be copied and distributed freely, provided:

1. There is no money collected for the text(s) of the articles.

2. The following notice remains appended to each copy:

The Usenet Oldnews Archive: Compilation Copyright (C) 1981, 1996
Bruce Jones, Henry Spencer, David Wiseman.