home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / COMM / MISC / UWPC201.ZIP / UWSERVER.TAR / utility / uwplot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-25  |  2.7 KB  |  125 lines

  1. /*
  2.  *    uwplot
  3.  *
  4.  * Copyright 1986 by John D. Bruner.  All rights reserved.  Permission to
  5.  * copy this program is given provided that the copy is not sold and that
  6.  * this copyright notice is included.
  7.  */
  8.  
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <sys/signal.h>
  13. #include <sys/errno.h>
  14. #include <ctype.h>
  15. #include <stdio.h>
  16.  
  17. #include "uwlib.h"
  18.  
  19. char *argv0;
  20. UWIN uwin;
  21.  
  22. main(argc, argv)
  23. char **argv;
  24. {
  25.     register int c, len;
  26.     register char *cp;
  27.     register char *title;
  28.     register struct sockaddr_in *sin;
  29.     auto struct sockaddr_in sa;
  30.     auto char buf[4096];
  31.     extern char *optarg;
  32.     extern int errno;
  33.     extern onintr();
  34.  
  35.     /*
  36.      * Options which are recognized directly are:
  37.      *
  38.      *    -ninet    connect to server at address "inet"
  39.      *    -ttitle    label window with "title" (default is argv[0])
  40.      */
  41.     argv0 = argv[0];
  42.     sin = (struct sockaddr_in *)0;
  43.     title = argv0;
  44.     while ((c = getopt(argc, argv, "n:t:")) != EOF) {
  45.         switch (c) {
  46.         case 'n':
  47.             sa.sin_family = AF_INET;
  48.             sa.sin_addr.s_addr = 0;
  49.             sa.sin_port = 0;
  50.             bzero(sa.sin_zero, sizeof sa.sin_zero);
  51.             for (cp=optarg; isxdigit(c = *cp); cp++) {
  52.                 /* Pyramid compiler botch */
  53.                 /* sa.sin_addr.s_addr *= 16; */
  54.                 sa.sin_addr.s_addr <<= 4;
  55.                 if (isdigit(c))
  56.                     sa.sin_addr.s_addr += c - '0';
  57.                 else if (islower(c))
  58.                     sa.sin_addr.s_addr += c-'a' + 10;
  59.                 else
  60.                     sa.sin_addr.s_addr += c-'A' + 10;
  61.             }
  62.             if (c == '.')
  63.                 for (cp++; isdigit(c = *cp); cp++)
  64.                     sa.sin_port = sa.sin_port*10 + c-'0';
  65.             if (sa.sin_addr.s_addr == 0 || sa.sin_port == 0) {
  66.                 fprintf(stderr,
  67.                     "%s: bad Internet address: %s\n",
  68.                     argv0, optarg);
  69.                 return(1);
  70.             }
  71.             sa.sin_addr.s_addr = htonl(sa.sin_addr.s_addr);
  72.             sa.sin_port = htons(sa.sin_port);
  73.             sin = &sa;
  74.             break;
  75.         case 't':
  76.             title = optarg;
  77.             break;
  78.         }
  79.     }
  80.  
  81.     /*
  82.      * Catch hangup, interrupt, quit, and termination signals.  Kill
  83.      * the window if one of these is received.
  84.      */
  85.     (void)signal(SIGHUP, onintr);
  86.     (void)signal(SIGINT, onintr);
  87.     (void)signal(SIGQUIT, onintr);
  88.     (void)signal(SIGTERM, onintr);
  89.  
  90.     /*
  91.      * Create a new plot window, title it, and make it visible.
  92.      */
  93.     if ((uwin = uw_new(UWT_PLOT, sin)) == (UWIN)0) {
  94.         uw_perror(argv[0], uwerrno, errno);
  95.         return(1);
  96.     }
  97.     (void)uw_stitle(uwin, title);
  98.     (void)uw_svis(uwin, 1);
  99.  
  100.     /*
  101.      * Copy the standard input to the plot window.
  102.      */
  103.     while ((len = read(0, buf, sizeof buf)) > 0 ||
  104.         (len < 0 && errno == EINTR)) {
  105.         if (len > 0)
  106.             (void)write(UW_DATAFD(uwin), buf, len);
  107.     }
  108.  
  109.     /*
  110.      * This is something of a hack.  We don't expect to be able to
  111.      * read anything from the window.  The read will hang until the
  112.      * window is killed.
  113.      */
  114.     while ((len = read(UW_DATAFD(uwin), buf, sizeof buf)) > 0 ||
  115.         len < 0 && errno == EINTR)
  116.         ;
  117.     return(0);
  118. }
  119.  
  120. onintr()
  121. {
  122.     uw_kill(uwin);
  123.     exit(0);
  124. }
  125.