home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume8 / display < prev    next >
Text File  |  1987-03-01  |  6KB  |  269 lines

  1. Subject:  v08i092:  Execute command repeatedly, display output
  2. Newsgroups: mod.sources
  3. Approved: mirror!rs
  4.  
  5. Submitted by: amdahl!vixie!paul (Paul Vixie Esq)
  6. Mod.sources: Volume 8, Issue 92
  7. Archive-name: display
  8.  
  9. [  As the man says, this is a dinky program and many people have
  10.    written them.  Nice to have one for the archives, tho.  --r$  ]
  11.  
  12. -----CUT-----HERE-----
  13. # This is a shell archive.  Remove anything before this line,
  14. # then unpack it by saving it in a file and typing "sh file".
  15. # Contents:  README display.man Makefile display.c
  16. echo x - README
  17. sed 's/^@//' > "README" <<'@//E*O*F README//'
  18. Display 1.0
  19. 15-December-1986
  20.  
  21. This is another C implementation of the 'display' command, written because
  22. (a) is was easy, (b) I didn't know how to find the original, and (c) I have
  23. an disease whose only means to remission is writing flinky programs like
  24. this one.
  25.  
  26. Display runs a specified command over and over, printing the output through
  27. curses(3X).  The command can be compound ('date;uusnap' is a personal
  28. favorite), and the delay between executions is settable on the command line.
  29. The output from the command had better fit on a single screen, of course.
  30.  
  31. To install, unshar into an empty directory, edit the Makefile to change the
  32. DESTDIR (probably to /usr/local, /usr/local/bin or /usr/ucb), then su and
  33. type 'make install'.
  34.  
  35. Questions, problems, flames, to:
  36.  
  37.     Paul Vixie
  38.     paul@vixie.UUCP
  39.     ptsfa!vixie!paul
  40. @//E*O*F README//
  41. chmod u=rw,g=rw,o=r README
  42.  
  43. echo x - display.man
  44. sed 's/^@//' > "display.man" <<'@//E*O*F display.man//'
  45. @.TH AT 1 "December 15, 1986"
  46. @.UC 4
  47. @.SH NAME
  48. display \- repeat a command through curses for minimal update
  49. @.SH SYNOPSIS
  50. display [-<delay>] <command>
  51. @.SH DESCRIPTION
  52. @.I Display
  53. repeatedly executes a shell command, sending the output through
  54. @.IR curses (3X)
  55. to cause only the portions of the output which have changed from
  56. one execution to the next to be redrawn.
  57. @.I command
  58. must be quoted if it contains blanks or other special characters.
  59. @.I delay
  60. is the number of seconds to suspend between updates; the default is 5.
  61. @.SH EXAMPLES
  62. @.sp 1
  63. display -10 "date; echo ' '; uusnap; echo ' '; mailq"
  64. @.PP
  65. This provides a dynamic 'UUCP status watch' for BSD systems.  If anyone
  66. figures out a non-nightmarish csh alias for something with both kinds of
  67. quotes in it, please send mail.
  68. @.fi
  69. @.SH "SEE ALSO"
  70. popen(3),
  71. curses(3X),
  72. sh(1)
  73. @.SH DIAGNOSTICS
  74. Fairly informative usage message if you run it with a bad command line.
  75. @.SH BUGS
  76. @.IR Popen (3)
  77. always executes the command through
  78. @.IR sh (1)
  79. @.SH AUTHOR
  80. @.nf
  81. Paul Vixie
  82. {ptsfa,qantel,crash,winfree}!vixie!paul
  83. @//E*O*F display.man//
  84. chmod u=rw,g=rw,o=r display.man
  85.  
  86. echo x - Makefile
  87. sed 's/^@//' > "Makefile" <<'@//E*O*F Makefile//'
  88. # makefile for 'display'
  89. # vix 15dec86 [stolen from 'which']
  90.  
  91. # start changing
  92. CFLAGS        =    -O
  93. DEST_DIR    =    /usr/local/bin
  94. CURSES_LIBS    =    -lcurses -ltermcap
  95. MAN_DIR        =    /usr/man/manl
  96. MAN_SUFFIX    =    l
  97. SHAR_ARGS    =    -b -c -v
  98. # stop changing
  99.  
  100. SHAR_SOURCES    =    README display.man Makefile display.c
  101. SHELL        =    /bin/sh
  102.  
  103. all        :    display
  104.  
  105. display        :    display.c
  106.             cc $(CFLAGS) -o $@ display.c $(CURSES_LIBS)
  107.             strip display
  108.  
  109. install        :    all
  110.             mv display $(DEST_DIR)
  111.             chmod 775 $(DEST_DIR)/display
  112.             cp display.man $(MAN_DIR)/display.$(MAN_SUFFIX)
  113.  
  114. display.shar    :    $(SHAR_SOURCES)
  115.             shar $(SHAR_ARGS) $(SHAR_SOURCES) > $@
  116. @//E*O*F Makefile//
  117. chmod u=rw,g=rw,o= Makefile
  118.  
  119. echo x - display.c
  120. sed 's/^@//' > "display.c" <<'@//E*O*F display.c//'
  121. /* display.c - repeatedly display command through curses
  122.  * vix 18apr86 [written]
  123.  * vix 15dec86 [major overhaul]
  124.  */
  125.  
  126.  
  127. #include <curses.h>
  128. #include <signal.h>
  129. #include <ctype.h>
  130.  
  131.  
  132. #define        DEFAULT_DELAY    5
  133.  
  134.  
  135. static    char    *Command;
  136. static    int    Delay;
  137.  
  138.  
  139. main(argc, argv)
  140. int    argc;
  141. char    *argv[];
  142. {
  143.     extern    void    parse_args(),
  144.             die(),
  145.             display();
  146.  
  147.     parse_args(argc, argv);
  148.  
  149.     signal(SIGHUP, die);
  150.     signal(SIGINT, die);
  151.     signal(SIGQUIT, die);
  152.     signal(SIGTERM, die);
  153.  
  154.     initscr();
  155.     clear();
  156.  
  157.     while (TRUE) {
  158.         display();
  159.         sleep(Delay);
  160.     }
  161. }
  162.  
  163.  
  164. static void die()
  165. {
  166.     move(LINES-1, 0);
  167.     clrtoeol();
  168.     refresh();
  169.     endwin();
  170.     exit(0);
  171. }
  172.  
  173.  
  174. static void display()
  175. {
  176.     auto    FILE    *fp, *popen();
  177.     auto    char    ch;
  178.  
  179.     if (!(fp = popen(Command, "r"))) {
  180.         perror("popen");
  181.         exit(1);
  182.     }
  183.     move(0, 0);
  184.     while (EOF != (ch = fgetc(fp)))
  185.     {
  186.         if (ch == '\n')
  187.             clrtoeol();
  188.         addch(ch);
  189.     }
  190.     clrtoeol();
  191.     refresh();
  192.     pclose(fp);
  193. }
  194.  
  195.  
  196. static void parse_args(argc, argv)
  197. int    argc;
  198. char    *argv[];
  199. {
  200.     extern    void    usage();
  201.     auto    int    argn,
  202.             delay_found;
  203.  
  204.     Command = NULL;
  205.     Delay = DEFAULT_DELAY;
  206.     delay_found = FALSE;
  207.     for (argn = 1;  argn < argc;  argn++)
  208.     {
  209.         if (argv[argn][0] == '-')
  210.             if (delay_found)
  211.                 usage();    /* already got this once */
  212.             else if (!isdigit(argv[argn][1]))
  213.                 usage();    /* not a numeric */
  214.             else
  215.             {
  216.                 Delay = atoi(&argv[argn][1]);
  217.                 delay_found = TRUE;
  218.             }
  219.         else
  220.             if (Command != NULL)
  221.                 usage();    /* already got this once */
  222.             else
  223.                 Command = argv[1];
  224.     }
  225.     if (Command == NULL)
  226.         usage();            /* no Command on line */
  227. }
  228.  
  229.  
  230. static void usage()
  231. {
  232.     extern    char    *getenv();
  233.     auto    char    *shell = getenv("SHELL");
  234.  
  235.     fprintf(stderr, "\
  236. usage:  display [-<delay>] <command>\n\
  237.         <delay>   = # of seconds between displays, default=%d\n\
  238.         <command> = command to display, quoted if it contains blanks\n",
  239.         DEFAULT_DELAY);
  240.  
  241.     if (strcmp(shell, "/bin/sh"))
  242.         fprintf(stderr, "\
  243.     NOTE:  /bin/sh will be used to process the command, not SHELL (%s)\n",
  244.             shell);
  245.     
  246.     exit(1);
  247. }
  248. @//E*O*F display.c//
  249. chmod u=rw,g=rw,o=r display.c
  250.  
  251. echo Inspecting for damage in transit...
  252. temp=/tmp/shar$$; dtemp=/tmp/.shar$$
  253. trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
  254. cat > $temp <<\!!!
  255.       22     127     816 README
  256.       38     176    1043 display.man
  257.       28      80     601 Makefile
  258.      127     266    1977 display.c
  259.      215     649    4437 total
  260. !!!
  261. wc  README display.man Makefile display.c | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
  262. if [ -s $dtemp ]
  263. then echo "Ouch [diff of wc output]:" ; cat $dtemp
  264. else echo "No problems found."
  265. fi
  266. exit 0
  267.  
  268.  
  269.