home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tel2305s.zip / LPR / LP.C next >
C/C++ Source or Header  |  1991-12-15  |  9KB  |  249 lines

  1. /*  -------------------------------------------------------------
  2.     lp.c
  3.  
  4.     Routines common to lpr, lpq, and lprm.
  5.  
  6.     Paul Hilchey    May 1989
  7.  
  8.     Copyright (C) 1989  The University of British Columbia
  9.     All rights reserved.
  10.  
  11.      history
  12.      -------
  13.      1/6/89   Microsoft C port by Heeren Pathak (NCSA)
  14.     -------------------------------------------------------------
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <conio.h>
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21. #ifdef MEMORY_DEBUG
  22. #include "memdebug.h"
  23. #endif
  24. #include "netevent.h"
  25. #include "hostform.h"
  26. #include "lp.h"
  27. #include "externs.h"
  28.  
  29. #ifdef MSC
  30. #define EXIT_FAILURE 1
  31. #endif
  32.  
  33. /****************************************************************
  34.  * lookup                                                       *
  35.  * Try to find the remote host in the local cache or from a     *
  36.  * domain name server.                                          *
  37.  * parameters: null terminated string containing the name or    *
  38.  *                ip address of the host                        *
  39.  * return value: pointer to machine info record, or 0 if the    *
  40.  *               lookup failed                                  *
  41.  ****************************************************************/
  42. struct machinfo *lookup(char *host)
  43. {
  44.     int what,dat;
  45.     int machine_number;     /* used to identify domain lookup events */
  46.     struct machinfo *machine_info;
  47.  
  48.     machine_info = Sgethost(host);  /* look up in hosts cache */
  49.  
  50.     if (!machine_info) {
  51.         if ((machine_number = Sdomain(host)) < 0) return(0);  /* initiate domain name lookup */
  52.  
  53.         while (machine_info==NULL) {                /* wait for DOMOK or DOMFAIL event */
  54.             switch(lgetevent(USERCLASS,&what,&dat)) {
  55.                 case DOMFAIL: return(0);                             /* lookup failed, return 0 */
  56.                 case DOMOK:   machine_info=Slooknum(machine_number); /* get pointer to machine record */
  57.                 default: break;
  58.             }
  59.         }
  60.     if (debug) puts("Domain lookup worked");
  61.     }
  62.     return(machine_info);
  63. }
  64.  
  65. /*****************************************************************
  66.  *  open_connection                                              *
  67.  *  Open the TCP connection.                                     *
  68.  *  parameters: pointer to machine info record                   *
  69.  *              source port number                               *
  70.  *              destination port number                          *
  71.  *  return value: connection identifier (port number), or -1 if  *
  72.  *                connection could not be opened                 *
  73.  *****************************************************************/
  74. int open_connection(struct machinfo *machine_record, int source_port,
  75.                     int dest_port)
  76. {
  77.     int ev,what,dat;  /* parameters for lgetevent */
  78.     int conid;    /* connection identifier */
  79.  
  80.     /* set the source port */
  81.     netfromport(source_port);
  82.  
  83.     /* initiate connection open */
  84.     if (0 > (conid = Snetopen(machine_record,dest_port)))
  85.         return(-1);
  86.  
  87.     if (debug) puts("snetopen ok");
  88.  
  89.     /* wait for connection to open or for attempt to fail */
  90.     while(1) {
  91.         if (0 != (ev = lgetevent(CONCLASS,&what,&dat))) {
  92.             if (dat != conid) {     /* not for us */
  93. /*              netputevent(what,ev,dat); */
  94.                 continue;
  95.             }
  96.             if (ev == CONOPEN)
  97.                 break;
  98.             else
  99.                 return(-1);
  100.         }
  101.     }
  102.     if (debug) puts("Conopen");
  103.     return(conid);
  104. }
  105.  
  106.  
  107. /*******************************************************************
  108.  * crash                                                           *
  109.  * Shut down all network stuff, print an error message to stderr,  *
  110.  * and abort.                                                      *
  111.  * parameters: variable length argument list for the error         *
  112.  *                message (a la printf)                            *
  113.  *******************************************************************/
  114. void crash(char *msg,...)
  115. {
  116.     va_list argptr;
  117.  
  118.     fprintf(stderr,"\nError: ");
  119.     va_start(argptr,msg);
  120.     vfprintf(stderr,msg,argptr);
  121.     va_end(argptr);
  122.     fprintf(stderr,"\n");
  123.  
  124.     /* shut everything down */
  125.     netshut();
  126.     exit(EXIT_FAILURE);
  127. }
  128.  
  129. /*********************************************************************
  130.  * Check for any error events that may have occured.  Either print   *
  131.  * the message on stderr or just ignore it if it is probably not     *
  132.  * serious.  Set debug on to see all error messages.                 *
  133.  *********************************************************************/
  134. void checkerr(void )
  135. {
  136.     char *errmsg;
  137.     int i,j;
  138.  
  139.     while (ERR1 == Sgetevent(ERRCLASS,&i,&j)) {
  140.         if ((!debug) &&
  141.            ((300 <= j && j <= 399) ||  /* IP messages */
  142.             (400 <= j && j <= 499) ||  /* TCP messages */
  143.             (600 <= j && j <= 699) ||  /* ICMP messages */
  144.             j == 801  || j == 805  ||  /* misc. domain stuff */
  145.             j == 806))
  146.             continue;                  /* just ignore them */
  147.         errmsg = neterrstring(j);
  148.         fprintf(stderr,"%s\n",errmsg);
  149.     }
  150. }
  151.  
  152. /*********************************************************************
  153.  * lgetevent                                                         *
  154.  * Check for network events. The next pending non-error event is     *
  155.  * returned (if any).                                                *
  156.  * Takes the same parameters as sgetevent.                           *
  157.  *********************************************************************/
  158. int lgetevent(int class, int *what, int *datp)
  159. {
  160.     checkerr();
  161.     return(Sgetevent(class, what, datp));
  162. }
  163.  
  164. /******************************************************************
  165.  * nprintf                                                        *
  166.  * Formatted write to an open TCP conection.  Like fprintf, but   *
  167.  * use a connection id returned from snteopen instead of a file   *
  168.  * handle.  The formatted string must not exceed 1023 bytes.      *
  169.  * Returns EOF if an error occurs                                 *
  170.  ******************************************************************/
  171. int nprintf(int connection_id, char *format,...)
  172. #define BUFF_SIZE 1024
  173. {
  174.     va_list argptr;
  175.     char    buff[BUFF_SIZE], *buff_ptr;
  176.     int     len1, len2;
  177.  
  178.     va_start(argptr,format);
  179.     len1 = vsprintf(buff,format,argptr);
  180.     va_end(argptr);
  181.     if ((len1 == EOF) || len1+1 >= BUFF_SIZE) return(EOF);
  182.     buff_ptr = buff;
  183.     while (buff_ptr < (buff + len1)) {
  184.         len2 = netwrite(connection_id, buff_ptr, len1-(buff_ptr - buff));
  185.         checkerr();
  186.         Stask();
  187.         if (len2 < 0) return(EOF);
  188.         buff_ptr += len2;
  189.     }
  190.     if (debug) puts(buff);
  191.     return (len1);
  192. }
  193.  
  194. /******************************************************************
  195.  * nread                                                          *
  196.  * Read from an open TCP connection.  Waits for incoming data if  *
  197.  * there is none in the queue.  Returns EOF if the connection     *
  198.  * closes and there is no more data.                              *
  199.  *                                                                *
  200.  * parameters: connection id returned by Snetopen                 *
  201.  *             buffer for returned data                           *
  202.  *             size of buffer                                     *
  203.  * returned value: number of characters read into the buffer      *
  204.  ******************************************************************/
  205.  
  206. int nread(int connection_id, char *buff, int buff_size)
  207. {
  208.     int class,data,ev;
  209.     int len;
  210.  
  211.     netpush(connection_id);  /* flush buffer */
  212.  
  213.     while (0 == netest(connection_id)) {
  214.         ev = lgetevent(CONCLASS, &class, &data);
  215.         if (!ev) continue;
  216.         if (data != connection_id) {   /* not for us; throw away */
  217. /*         netputevent(class, ev, data); */
  218.            continue;
  219.         }
  220.         if (debug) printf("nread %d %d\n",class,ev);
  221.         if (ev == CONDATA) {
  222.             len = netread(connection_id,buff,buff_size);
  223.             if (len == 0) continue;
  224.             if (debug) printf("received %.*s\n",len,buff);
  225.             return (len);
  226.         }
  227.     }
  228.     /* throw away other events.  getevent should be changed so we
  229.        can retrieve events for a selected port only  */
  230.     while (lgetevent(USERCLASS | CONCLASS, &class, &data));
  231.     return (EOF);    /* connection is closed and no data in queue */
  232. }
  233.  
  234. #ifdef MSC
  235. #else
  236. #pragma warn .par
  237. #endif
  238.  
  239. /******************************************************************
  240.  * breakstop                                                      *
  241.  * Handle break interrupts by shutting down the network stuff and *
  242.  * aborting.                                                      *
  243.  ******************************************************************/
  244. int breakstop(void )
  245. {
  246.     netshut();
  247.     return(0);
  248. }
  249.