source: trunk/athena/bin/rep/rep.c @ 23

Revision 23, 2.5 KB checked in by builder, 39 years ago (diff)
Initial revision
Line 
1/******************************************************************************
2 *                                                                            *
3 * rep -- run a program repeatedly using 'curses'.                            *
4 *                                                                            *
5 *      Usage: rep [-n] command                                               *
6 *                                                                            *
7 *      Permits the user to watch a program like 'w' or 'finger' change with  *
8 *      time.  Given an argument '-x' will cause a repetion every x seconds.  *
9 *                                                                            *
10 * ORIGINAL                                                                   *
11 * Ray Lubinsky University of Virginia, Dept. of Computer Science             *
12 *           uucp: decvax!mcnc!ncsu!uvacs!rwl                                 *
13 * MODIFIED 3/85                                                              *
14 * Bruce Karsh, Univ. Wisconsin, Madison.  Dept. of Geophysics.               *
15 *           uucp: uwvax\!geowhiz\!karsh                                      *
16 ******************************************************************************/
17
18#include <curses.h>
19#include <signal.h>
20
21#define NextLine        fgets(buf,sizeof buf,input)
22
23FILE    *input;                         /* Pipe from 'source' */
24char    source[512];                    /* Source program to run repeatedly */
25char    *progname;                      /* Name of this program (for errors) */
26int     interval = 1;                   /* Seconds between repitions */
27int     deathflag = 0;                  /* Set by interupt trap routine */
28
29main(argc,argv)
30        int     argc;
31        char    **argv;
32{
33        int     endrep();               /* Mop up routine on break */
34        FILE    *popen();
35
36        char    buf[BUFSIZ];            /* Buffer holds a line from 'source' */
37        int     i;
38
39        progname = *argv;
40        source[0]=0;
41        if (argc == 1)
42                badargs();
43        if (**(argv+1) == '-')          /* User specified interval */
44                if ((--argc == 1) || ((interval = atoi(*(++argv)+1)) == 0))
45                        badargs();
46        while (--argc > 0) {            /* Argument becomes source program */
47                strcat(source,*++argv);
48                strcat(source," ");
49        }
50        signal(SIGINT,endrep);
51        if(deathflag == 1)goto die;
52        initscr();
53        if(deathflag == 1)goto die;
54        crmode();
55        if(deathflag == 1)goto die;
56        nonl();
57        if(deathflag == 1)goto die;
58        clear();
59        if(deathflag == 1)goto die;
60        for (;;) {
61                if(deathflag == 1)goto die;
62                if ((input = popen(source,"r")) == NULL) {
63                        fprintf(stderr,"%s: can't run %s\n",progname,source);
64                        goto die;
65                }
66                for (i = 0; (i < LINES) && (NextLine != NULL); i++) {
67                        mvaddstr(i,0,buf);
68                        clrtoeol();
69                        if(deathflag == 1)goto die;
70                }
71                if(deathflag == 1)goto die;
72                pclose(input);
73                if(deathflag == 1)goto die;
74                clrtobot();
75                if(deathflag == 1)goto die;
76                refresh();
77                if(deathflag == 1)goto die;
78                sleep(interval);
79                if(deathflag == 1)goto die;
80        }
81die:
82signal(SIGINT,SIG_IGN);
83if (input != NULL)
84        pclose(input);
85clear();
86refresh();
87endwin();
88exit(0);
89}
90
91endrep()
92{
93deathflag=1;
94}
95
96badargs()
97{
98        fprintf(stderr,"Usage: %s [-n] command\n",progname);
99        exit(1);
100}
Note: See TracBrowser for help on using the repository browser.