home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8707 / 58 < prev    next >
Encoding:
Internet Message Format  |  1990-07-13  |  4.1 KB

  1. From: vandys@lindy.stanford.edu (Andy Valencia)
  2. Newsgroups: comp.sources.misc
  3. Subject: monitor -- monitor a command using curses
  4. Message-ID: <3266@ncoast.UUCP>
  5. Date: 21 Jul 87 01:27:10 GMT
  6. Sender: allbery@ncoast.UUCP
  7. Lines: 199
  8. Approved: allbery@ncoast.UUCP
  9. X-Archive: comp.sources.misc/8707/58
  10.  
  11. Rich,
  12.     Well, you had to ask.  Here's a generalized monitoring program;
  13. you run it with:
  14.     monitor -i 10 command arg arg...
  15.     It runs the command, then displays the first n (where n is the number
  16. of lines on your terminal) lines of output on your screen, then cycles each
  17. 10 seconds (-i means interval--default is 30 seconds).  I've seen this hither
  18. and yon for years, but I keep running into places which don't have it.
  19.     Just compile with:
  20.     cc -O -o monitor monitor.c -lcurses -ltermcap
  21.     Works on my 4.2 system, will need changes for SysV.
  22.  
  23. /*
  24.  * monitor.c--utility which cyclically takes the output of a command and
  25.  *    displays it on the screen via curses(3x).
  26.  */
  27. #include <stdio.h>
  28. #include <curses.h>
  29. #include <signal.h>
  30. #include <errno.h>
  31. extern int errno;
  32. static int numlines;
  33.  
  34.     /*
  35.      * Set up curses
  36.      */
  37. static void
  38. setup_screen(){
  39.     static char bp[1024];
  40.     extern char *getenv();
  41.  
  42.     if( getenv("TERM") == NULL ){
  43.     fprintf(stderr,"Please set your TERM variable\n");
  44.     exit(1);
  45.     }
  46.     if( tgetent(bp,getenv("TERM")) != 1 ){
  47.     fprintf(stderr,"I can't use terminal type '%s'\n",getenv("TERM"));
  48.     exit(1);
  49.     }
  50.     if( (numlines = tgetnum("li")) < 0 ){
  51.     fprintf(stderr,"You seem to have an illegal number of lines\n");
  52.     exit(1);
  53.     }
  54.     initscr();
  55.     clear();
  56.     refresh();
  57. }
  58.  
  59.     /*
  60.      * Run the command, take as many lines of output as will fit on the
  61.      *    screen, paint them.
  62.      */
  63. static void
  64. paint_screen(cmd)
  65.     char *cmd;
  66. {
  67.     char tmpf[80], buf[256];
  68.     register char *p;
  69.     FILE *fp;
  70.     register c;
  71.     int kid, kidstat, x, y;
  72.  
  73.     strcpy(tmpf,"/tmp/monXXXXXX");
  74.     mktemp(tmpf);
  75.     if( (kid = fork()) == 0 ){
  76.  
  77.         /*
  78.          * The subprocess's output will go to a file; close stdout and
  79.          *    stderr, then re-open them directed to a temp file.
  80.          */
  81.     close(1); close(2);
  82.     dup(creat(tmpf,0700));
  83.  
  84.         /*
  85.          * Just to make sure, disable access to stdin
  86.          */
  87.     close(0);
  88.  
  89.         /*
  90.          * Call the program, return its status
  91.          */
  92.     exit( system(cmd) );
  93.     }
  94.  
  95.     /*
  96.      * Wait for our kid.  Kill him off if we get a signal from the user.
  97.      */
  98.     if( wait(&kidstat) < 0 ){
  99.     int hold_errno = errno;
  100.  
  101.     endwin();
  102.     errno = hold_errno;
  103.     perror(cmd[0]);
  104.     kill(kid,SIGKILL);
  105.     unlink(tmpf);
  106.     exit( 1 );
  107.     }
  108.  
  109.     /*
  110.      * Wait didn't return error, check if kid ran OK.
  111.      */
  112.     if( kidstat ){
  113.     endwin();
  114.     fprintf(stderr,"Problem running command\n");
  115.     unlink(tmpf);
  116.     exit( 1 );
  117.     }
  118.  
  119.     /*
  120.      * Suck in tmpf's contents, feed it to curses
  121.      */
  122.     if( (fp = fopen(tmpf,"r")) == NULL ){
  123.     endwin();
  124.     fprintf(stderr,"Error reading file '%s'\n",tmpf);
  125.     exit( 1 );
  126.     }
  127.     unlink(tmpf);
  128.     for( x = 0; x < numlines; ++x ){
  129.     p = buf;
  130.     while( (c = getc(fp)) != EOF )
  131.         if( c == '\n' ) break;
  132.         else *p++ = c;
  133.     if( c == EOF ) break;
  134.     *p = '\0';
  135.     buf[79] = '\0';
  136.     move(x,0);
  137.     clrtoeol();
  138.     addstr(buf);
  139.     }
  140.     for( y = x; y < numlines; ++y ){
  141.     move(y,0);
  142.     clrtoeol();
  143.     }
  144.     move(numlines-1,79);
  145.     fclose(fp);
  146.     refresh();
  147. }
  148.  
  149.     /*
  150.      * Main routine: get arguments, run the thing
  151.      */
  152. main(argc,argv)
  153.     int argc;
  154.     char **argv;
  155. {
  156.     char cmd[4096];
  157.     int interval, x;
  158.  
  159.     /*
  160.      * Give'm help if they need it
  161.      */
  162.     if( argc < 2 ){
  163.     fprintf(stderr,"Usage is: %s [-i <secs>] command [args...]\n",
  164.         argv[0]);
  165.     exit( 1 );
  166.     }
  167.  
  168.     /*
  169.      * If they specify '-i', take a user-specified interval between
  170.      *    displays, otherwise default to 30 seconds.
  171.      */
  172.     if( strncmp(argv[1], "-i", 2) == 0 ){
  173.     if( argv[1][2] ){
  174.         interval = atoi(argv[1]+2);
  175.         argv += 2;
  176.     } else {
  177.         interval = atoi(argv[2]);
  178.         argv += 3;
  179.     }
  180.     } else {
  181.     interval = 30;
  182.     ++argv;
  183.     }
  184.  
  185.     /*
  186.      * Build a command line
  187.      */
  188.     cmd[0] = '\0';
  189.     for( x = 0; argv[x]; ++x ){
  190.     strcat(cmd,argv[x]);
  191.     strcat(cmd," ");
  192.     }
  193.  
  194.     /*
  195.      * Prepare curses
  196.      */
  197.     setup_screen();
  198.  
  199.     /*
  200.      * Forever: paint the screen & loop
  201.      */
  202.     while( 1 ){
  203.     paint_screen(cmd);
  204.     sleep(interval);
  205.     }
  206. }
  207.  
  208. ------- End of Forwarded Message
  209.