1 /* -------------------------------------------------------------
4 Routines common to lpr, lpq, and lprm.
8 Copyright (C) 1989 The University of British Columbia
13 1/6/89 Microsoft C port by Heeren Pathak (NCSA)
14 -------------------------------------------------------------
35 #define EXIT_FAILURE 1
38 void checkerr( void );
40 /****************************************************************
42 * Try to find the remote host in the local cache or from a *
43 * domain name server. *
44 * parameters: null terminated string containing the name or *
45 * ip address of the host *
46 * return value: pointer to machine info record, or 0 if the *
48 ****************************************************************/
49 struct machinfo *lookup(char *host)
52 int machine_number; /* used to identify domain lookup events */
53 struct machinfo *machine_info;
55 machine_info = Sgethost(host); /* look up in hosts cache */
58 if ((machine_number = Sdomain(host)) < 0)
59 return(0); /* initiate domain name lookup */
61 /* wait for DOMOK or DOMFAIL event */
62 while (machine_info==NULL) {
63 switch(lgetevent(USERCLASS,&what,&dat)) {
65 /* lookup failed, return 0 */
68 /* get pointer to machine record */
69 machine_info=Slooknum(machine_number);
74 if (debug) puts("Domain lookup worked");
79 /*****************************************************************
81 * Open the TCP connection. *
82 * parameters: pointer to machine info record *
83 * source port number *
84 * destination port number *
85 * return value: connection identifier (port number), or -1 if *
86 * connection could not be opened *
87 *****************************************************************/
88 int open_connection(struct machinfo *machine_record, int source_port,
91 int ev,what,dat; /* parameters for lgetevent */
92 int conid; /* connection identifier */
94 /* set the source port */
95 netfromport(source_port);
97 /* initiate connection open */
98 if (0 > (conid = Snetopen(machine_record,dest_port)))
101 if (debug) puts("snetopen ok");
103 /* wait for connection to open or for attempt to fail */
105 if (0 != (ev = lgetevent(CONCLASS,&what,&dat))) {
106 if (dat != conid) { /* not for us */
107 /* netputevent(what,ev,dat); */
116 if (debug) puts("Conopen");
121 /*******************************************************************
123 * Shut down all network stuff, print an error message to stderr, *
125 * parameters: variable length argument list for the error *
126 * message (a la printf) *
127 *******************************************************************/
128 void crash(char *msg,...)
132 fprintf(stderr,"\nError: ");
133 va_start(argptr,msg);
134 vfprintf(stderr,msg,argptr);
136 fprintf(stderr,"\n");
138 /* shut everything down */
143 /*********************************************************************
144 * Check for any error events that may have occured. Either print *
145 * the message on stderr or just ignore it if it is probably not *
146 * serious. Set debug on to see all error messages. *
147 *********************************************************************/
153 while (ERR1 == Sgetevent(ERRCLASS,&i,&j)) {
155 ((300 <= j && j <= 399) || /* IP messages */
156 (400 <= j && j <= 499) || /* TCP messages */
157 (600 <= j && j <= 699) || /* ICMP messages */
158 j == 801 || j == 805 || /* misc. domain stuff */
160 continue; /* just ignore them */
161 errmsg = neterrstring(j);
162 fprintf(stderr,"%s\n",errmsg);
166 /*********************************************************************
168 * Check for network events. The next pending non-error event is *
169 * returned (if any). *
170 * Takes the same parameters as sgetevent. *
171 *********************************************************************/
172 int lgetevent(int class, int *what, int *datp)
175 return(Sgetevent(class, what, datp));
178 /******************************************************************
180 * Formatted write to an open TCP conection. Like fprintf, but *
181 * use a connection id returned from snteopen instead of a file *
182 * handle. The formatted string must not exceed 1023 bytes. *
183 * Returns EOF if an error occurs *
184 ******************************************************************/
185 int nprintf(int connection_id, char *format,...)
186 #define BUFF_SIZE 1024
189 char buff[BUFF_SIZE], *buff_ptr;
192 va_start(argptr,format);
193 len1 = vsprintf(buff,format,argptr);
195 if ((len1 == EOF) || len1+1 >= BUFF_SIZE) return(EOF);
197 while (buff_ptr < (buff + len1)) {
198 len2 = netwrite(connection_id, buff_ptr,
199 len1-(buff_ptr - buff));
202 if (len2 < 0) return(EOF);
205 if (debug) puts(buff);
209 /******************************************************************
211 * Read from an open TCP connection. Waits for incoming data if *
212 * there is none in the queue. Returns EOF if the connection *
213 * closes and there is no more data. *
215 * parameters: connection id returned by Snetopen *
216 * buffer for returned data *
218 * returned value: number of characters read into the buffer *
219 ******************************************************************/
221 int nread(int connection_id, char *buff, int buff_size)
226 netpush(connection_id); /* flush buffer */
228 while (0 == netest(connection_id)) {
229 ev = lgetevent(CONCLASS, &class, &data);
231 if (data != connection_id) { /* not for us; throw away */
232 /* netputevent(class, ev, data); */
235 if (debug) printf("nread %d %d\n",class,ev);
237 len = netread(connection_id,buff,buff_size);
238 if (len == 0) continue;
242 /* throw away other events. getevent should be changed so we
243 can retrieve events for a selected port only */
244 while (lgetevent(USERCLASS | CONCLASS, &class, &data));
245 return (EOF); /* connection is closed and no data in queue */
253 /******************************************************************
255 * Handle break interrupts by shutting down the network stuff and *
257 ******************************************************************/