| 1 |
/* |
| 2 |
* *************************************************************************** |
| 3 |
* MALOC = < Minimal Abstraction Layer for Object-oriented C > |
| 4 |
* Copyright (C) 1994-- Michael Holst |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
* |
| 20 |
* rcsid="$Id: vio.c,v 1.32 2010/08/12 05:40:30 fetk Exp $" |
| 21 |
* *************************************************************************** |
| 22 |
*/ |
| 23 |
|
| 24 |
/* |
| 25 |
* *************************************************************************** |
| 26 |
* File: vio.c |
| 27 |
* |
| 28 |
* Purpose: Class Vio: methods. |
| 29 |
* |
| 30 |
* Author: Michael Holst |
| 31 |
* *************************************************************************** |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "vio_p.h" |
| 35 |
|
| 36 |
VEMBED(rcsid="$Id: vio.c,v 1.32 2010/08/12 05:40:30 fetk Exp $") |
| 37 |
|
| 38 |
#if defined(HAVE_UNISTD_H) |
| 39 |
# include <unistd.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#if defined(HAVE_SYS_TYPES_H) |
| 43 |
# include <sys/types.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#if defined(HAVE_SYS_STAT_H) |
| 47 |
# include <sys/stat.h> |
| 48 |
#endif |
| 49 |
|
| 50 |
#if defined(HAVE_FCNTL_H) |
| 51 |
# include <fcntl.h> |
| 52 |
#endif |
| 53 |
|
| 54 |
#if defined(HAVE_SYS_SOCKET_H) |
| 55 |
# include <sys/socket.h> |
| 56 |
#endif |
| 57 |
|
| 58 |
#if defined(HAVE_SYS_UN_H) |
| 59 |
# include <sys/un.h> |
| 60 |
#endif |
| 61 |
|
| 62 |
#if defined(HAVE_NETINET_IN_H) |
| 63 |
# include <netinet/in.h> |
| 64 |
#endif |
| 65 |
|
| 66 |
#if defined(HAVE_ARPA_INET_H) |
| 67 |
# include <arpa/inet.h> |
| 68 |
#endif |
| 69 |
|
| 70 |
#if defined(HAVE_NETDB_H) |
| 71 |
# include <netdb.h> |
| 72 |
#endif |
| 73 |
|
| 74 |
|
| 75 |
#if defined(HAVE_RPC_RPC_H) |
| 76 |
# include <rpc/rpc.h> |
| 77 |
#elif defined(HAVE_RPC_H) |
| 78 |
# include <rpc.h> |
| 79 |
#endif |
| 80 |
|
| 81 |
|
| 82 |
/*#include <rpc/rpc.h>*/ |
| 83 |
|
| 84 |
#if defined(HAVE_WINSOCK_H) |
| 85 |
# include <winsock.h> |
| 86 |
#endif |
| 87 |
|
| 88 |
#if defined(HAVE_IO_H) |
| 89 |
# include <io.h> |
| 90 |
#endif |
| 91 |
|
| 92 |
/* ASC modes as dual to the XDR modes */ |
| 93 |
typedef enum ASCmode { |
| 94 |
ASC_NO_MODE, |
| 95 |
ASC_DECODE, |
| 96 |
ASC_ENCODE |
| 97 |
} ASCmode; |
| 98 |
|
| 99 |
/* ASC structure as dual to the XDR structure */ |
| 100 |
typedef struct ASC { |
| 101 |
ASCmode mode; /* ASC_DECODE or ASC_ENCODE */ |
| 102 |
int pos; /* position of next character in buf */ |
| 103 |
int size; /* size of buf */ |
| 104 |
char *buf; /* the character buffer */ |
| 105 |
char whiteChars[VMAX_ARGNUM]; /* white space character set */ |
| 106 |
char commChars[VMAX_ARGNUM]; /* comment character set */ |
| 107 |
int error; /* APBS-KTS: Currently for EOF cond */ |
| 108 |
} ASC; |
| 109 |
|
| 110 |
/* use ASC in place of XDR if XDR does not exist */ |
| 111 |
#if !defined(HAVE_XDR) |
| 112 |
# define XDR ASC |
| 113 |
# define XDR_DECODE ASC_DECODE |
| 114 |
# define XDR_ENCODE ASC_ENCODE |
| 115 |
# define xdrmem_create ascmem_create |
| 116 |
# define xdr_destroy asc_destroy |
| 117 |
# define xdr_getpos asc_getpos |
| 118 |
# define xdr_setpos asc_setpos |
| 119 |
# define xdr_string asc_string |
| 120 |
# define xdr_char asc_char |
| 121 |
# define xdr_int asc_int |
| 122 |
# define xdr_float asc_float |
| 123 |
# define xdr_double asc_double |
| 124 |
#endif |
| 125 |
|
| 126 |
/* communication startup */ |
| 127 |
VPRIVATE int VIOstarted = 0; |
| 128 |
|
| 129 |
/* default comment char set and white space char set */ |
| 130 |
VPRIVATE char *VIOwhiteChars = " \t\n"; |
| 131 |
VPRIVATE char *VIOcommChars = ""; |
| 132 |
|
| 133 |
/* initialization, signals, and other tools */ |
| 134 |
VPRIVATE void VIOregHand(void); |
| 135 |
VPRIVATE void VIOunregHand(void); |
| 136 |
VPRIVATE void VIOsigHand(int num); |
| 137 |
VPRIVATE int VIOgethostname(char *name, unsigned int len); |
| 138 |
VPRIVATE const char *VIOstrerrno(int err); |
| 139 |
|
| 140 |
/* buffer management */ |
| 141 |
VPRIVATE void Vio_initIoPutBuffers(Vio *thee); |
| 142 |
VPRIVATE void Vio_purgePutBuffer(Vio *thee); |
| 143 |
VPRIVATE int Vio_writePutBuffer(Vio *thee, char *buf, int bufsize); |
| 144 |
|
| 145 |
/* ASC analogs to the core XDR routines */ |
| 146 |
VPRIVATE void ascmem_create(ASC *thee, char *buf, int size, ASCmode mode); |
| 147 |
VPRIVATE void asc_destroy(ASC *thee); |
| 148 |
VPRIVATE int asc_getpos(ASC *thee); |
| 149 |
VPRIVATE int asc_setpos(ASC *thee, int pos); |
| 150 |
VPRIVATE int asc_string(ASC *thee, char **sval, int size); |
| 151 |
VPRIVATE int asc_char(ASC *thee, char *cval); |
| 152 |
VPRIVATE int asc_int(ASC *thee, int *ival); |
| 153 |
VPRIVATE int asc_float(ASC *thee, float *fval); |
| 154 |
VPRIVATE int asc_double(ASC *thee, double *dval); |
| 155 |
|
| 156 |
/* some additional ASC routines (no analogs in XDR) */ |
| 157 |
VPRIVATE void asc_setWhiteChars(ASC *thee, char *whiteChars); |
| 158 |
VPRIVATE void asc_setCommChars(ASC *thee, char *commChars); |
| 159 |
VPRIVATE char *asc_getToken(ASC *thee, char *tok, int toksize); |
| 160 |
|
| 161 |
/* two low-level file-descriptor i/o jewels from Rick Steven's book */ |
| 162 |
VPRIVATE int readn(int fd, void *vptr, unsigned int n); |
| 163 |
VPRIVATE int writen(int fd, void *vptr, unsigned int n) ; |
| 164 |
|
| 165 |
/* |
| 166 |
* *************************************************************************** |
| 167 |
* Class Vio: Inlineable methods |
| 168 |
* *************************************************************************** |
| 169 |
*/ |
| 170 |
#if !defined(VINLINE_MALOC) |
| 171 |
|
| 172 |
#endif /* if !defined(VINLINE_MALOC) */ |
| 173 |
/* |
| 174 |
* *************************************************************************** |
| 175 |
* Class Vio: Non-inlineable methods |
| 176 |
* *************************************************************************** |
| 177 |
*/ |
| 178 |
|
| 179 |
/* |
| 180 |
* *************************************************************************** |
| 181 |
* Routine: Vio_start |
| 182 |
* |
| 183 |
* Purpose: Startup the Vio communication layer. |
| 184 |
* |
| 185 |
* Notes: This routine initializes some internal variables and buffers. |
| 186 |
* This routine also deals with the interception of fatal |
| 187 |
* SIGPIPE signals which are generated on some systems when a |
| 188 |
* socket connection is broken by one of the send/receive pair. |
| 189 |
* |
| 190 |
* We don't want to abort in this situation, but rather do some |
| 191 |
* damage control. Hence we register our own SIGPIPE interrupt |
| 192 |
* handler in Vio_start(), and re-register it every time a SIGPIPE |
| 193 |
* is intercepted. We re-register it because some systems reset |
| 194 |
* the interrupt mask after a single interrupt is generated. |
| 195 |
* We un-register the SIGPIPE handler in Vio_stop(). |
| 196 |
* |
| 197 |
* To use the Vio library, you need to call Vio_start() before |
| 198 |
* you make any calls to the Vio_ctor(). Calling the Vio_ctor() |
| 199 |
* (or any other Vio method) before Vio_start() generates an error. |
| 200 |
* For example, you can call this routine as follows: |
| 201 |
* |
| 202 |
* Vio_start() |
| 203 |
* |
| 204 |
* Author: Michael Holst |
| 205 |
* *************************************************************************** |
| 206 |
*/ |
| 207 |
VPUBLIC void Vio_start(void) |
| 208 |
{ |
| 209 |
/* repeated initializations ARE legal (no error; possibly frees memory) */ |
| 210 |
/* VASSERT( !VIOstarted ); */ |
| 211 |
|
| 212 |
/* mark as initialized */ |
| 213 |
VIOstarted = 1; |
| 214 |
|
| 215 |
/* register the SIGPIPE handler to avoid aborts on a SIGPIPE */ |
| 216 |
VIOregHand(); |
| 217 |
|
| 218 |
/* normal return */ |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
/* |
| 223 |
* *************************************************************************** |
| 224 |
* Routine: Vio_stop |
| 225 |
* |
| 226 |
* Purpose: Shutdown the Vio communication layer. |
| 227 |
* |
| 228 |
* Notes: This routine was written primarily to deal with some |
| 229 |
* communication runtime environments which require a shutdown. |
| 230 |
* |
| 231 |
* Author: Michael Holst |
| 232 |
* *************************************************************************** |
| 233 |
*/ |
| 234 |
VPUBLIC void Vio_stop(void) |
| 235 |
{ |
| 236 |
/* repeated de-initializations ARE legal (no error; just a no-op) */ |
| 237 |
/* VASSERT( VIOstarted ); */ |
| 238 |
|
| 239 |
/* un-initialize us */ |
| 240 |
VIOstarted = 0; |
| 241 |
|
| 242 |
/* un-register the SIGPIPE handler */ |
| 243 |
VIOunregHand(); |
| 244 |
|
| 245 |
/* normal return */ |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
/* |
| 250 |
* *************************************************************************** |
| 251 |
* Routine: VIOregHand |
| 252 |
* |
| 253 |
* Purpose: Register the signal handler with the operating system. |
| 254 |
* |
| 255 |
* Author: Michael Holst |
| 256 |
* *************************************************************************** |
| 257 |
*/ |
| 258 |
VPRIVATE void VIOregHand(void) |
| 259 |
{ |
| 260 |
#if !defined(HAVE_WINSOCK_H) |
| 261 |
VASSERT( signal(SIGPIPE,&VIOsigHand) != SIG_ERR ); |
| 262 |
#endif |
| 263 |
} |
| 264 |
|
| 265 |
/* |
| 266 |
* *************************************************************************** |
| 267 |
* Routine: VIOunregHand |
| 268 |
* |
| 269 |
* Purpose: Un-Register the signal handler with the operating system. |
| 270 |
* |
| 271 |
* Author: Michael Holst |
| 272 |
* *************************************************************************** |
| 273 |
*/ |
| 274 |
VPRIVATE void VIOunregHand(void) |
| 275 |
{ |
| 276 |
#if !defined(HAVE_WINSOCK_H) |
| 277 |
VASSERT( signal(SIGPIPE,SIG_DFL) != SIG_ERR ); |
| 278 |
#endif |
| 279 |
} |
| 280 |
|
| 281 |
/* |
| 282 |
* *************************************************************************** |
| 283 |
* Routine: VIOsigHand |
| 284 |
* |
| 285 |
* Purpose: Handle events such as SIGPIPE. |
| 286 |
* |
| 287 |
* Author: Michael Holst |
| 288 |
* *************************************************************************** |
| 289 |
*/ |
| 290 |
VPRIVATE void VIOsigHand(int num) |
| 291 |
{ |
| 292 |
/* some i/o */ |
| 293 |
fprintf(stderr,"VIOsigHand: yow! caught a hot SIGPIPE....diffused it.\n"); |
| 294 |
|
| 295 |
/* just re-register interrupt handler in case it was cleared by default */ |
| 296 |
VIOregHand(); |
| 297 |
} |
| 298 |
|
| 299 |
/* |
| 300 |
* *************************************************************************** |
| 301 |
* Routine: VIOgethostname |
| 302 |
* |
| 303 |
* Purpose: Get the hostname of this machine. |
| 304 |
* Returns 0 on success. Returns -1 on error. |
| 305 |
* |
| 306 |
* Author: Michael Holst |
| 307 |
* *************************************************************************** |
| 308 |
*/ |
| 309 |
VPRIVATE int VIOgethostname(char *name, unsigned int len) |
| 310 |
{ |
| 311 |
#if defined(HAVE_WINSOCK_H) |
| 312 |
return gethostname(name,(int)len); |
| 313 |
#else |
| 314 |
return gethostname(name,len); |
| 315 |
#endif |
| 316 |
} |
| 317 |
|
| 318 |
/* |
| 319 |
* *************************************************************************** |
| 320 |
* Routine: VIOstrerrno |
| 321 |
* |
| 322 |
* Purpose: Return the error string corresponding to the error number. |
| 323 |
* |
| 324 |
* Notes: This is a partial implementation of the "iberty" library |
| 325 |
* function "strerrno" that exists on most Linux boxes. It is |
| 326 |
* simply a mapping of error number to error string. It is |
| 327 |
* very useful for debugging UNIX and INET socket code. |
| 328 |
* |
| 329 |
* Author: Michael Holst |
| 330 |
* *************************************************************************** |
| 331 |
*/ |
| 332 |
VPRIVATE const char *VIOstrerrno(int err) |
| 333 |
{ |
| 334 |
static char errstr[VMAX_ARGLEN]; |
| 335 |
|
| 336 |
if (err == EFAULT ) strcpy(errstr,"EFAULT"); |
| 337 |
else if (err == EINTR ) strcpy(errstr,"EINTR"); |
| 338 |
else if (err == EINVAL ) strcpy(errstr,"EINVAL"); |
| 339 |
else if (err == ENOENT ) strcpy(errstr,"ENOENT"); |
| 340 |
else if (err == EPIPE ) strcpy(errstr,"EPIPE"); |
| 341 |
else if (err == ENOMEM ) strcpy(errstr,"ENOMEM"); |
| 342 |
else if (err == EAGAIN ) strcpy(errstr,"EAGAIN"); |
| 343 |
else if (err == EBADF ) strcpy(errstr,"EBADF"); |
| 344 |
|
| 345 |
#if defined(HAVE_WINSOCK_H) |
| 346 |
else if (err == WSAENETDOWN ) strcpy(errstr,"WSAENETDOWN"); |
| 347 |
else if (err == WSAEFAULT ) strcpy(errstr,"WSAEFAULT"); |
| 348 |
else if (err == WSAENOTCONN ) strcpy(errstr,"WSAENOTCONN"); |
| 349 |
else if (err == WSAEINTR ) strcpy(errstr,"WSAEINTR"); |
| 350 |
else if (err == WSAEINPROGRESS ) strcpy(errstr,"WSAEINPROGRESS"); |
| 351 |
else if (err == WSAENETRESET ) strcpy(errstr,"WSAENETRESET"); |
| 352 |
else if (err == WSAENOTSOCK ) strcpy(errstr,"WSAENOTSOCK"); |
| 353 |
else if (err == WSAEOPNOTSUPP ) strcpy(errstr,"WSAEOPNOTSUPP"); |
| 354 |
else if (err == WSAESHUTDOWN ) strcpy(errstr,"WSAESHUTDOWN"); |
| 355 |
else if (err == WSAEWOULDBLOCK ) strcpy(errstr,"WSAEWOULDBLOCK"); |
| 356 |
else if (err == WSAEMSGSIZE ) strcpy(errstr,"WSAEMSGSIZE"); |
| 357 |
else if (err == WSAEINVAL ) strcpy(errstr,"WSAEINVAL"); |
| 358 |
else if (err == WSAETIMEDOUT ) strcpy(errstr,"WSAETIMEDOUT"); |
| 359 |
else if (err == WSAECONNABORTED ) strcpy(errstr,"WSAECONNABORTED"); |
| 360 |
else if (err == WSAECONNREFUSED ) strcpy(errstr,"WSAECONNREFUSED"); |
| 361 |
else if (err == WSAECONNRESET ) strcpy(errstr,"WSAECONNRESET"); |
| 362 |
else if (err == WSANOTINITIALISED) strcpy(errstr,"WSANOTINITIALISED"); |
| 363 |
#else |
| 364 |
else if (err == ENETDOWN ) strcpy(errstr,"ENETDOWN"); |
| 365 |
else if (err == ENOTCONN ) strcpy(errstr,"ENOTCONN"); |
| 366 |
else if (err == EINPROGRESS ) strcpy(errstr,"EINPROGRESS"); |
| 367 |
else if (err == ENETRESET ) strcpy(errstr,"ENETRESET"); |
| 368 |
else if (err == ENOTSOCK ) strcpy(errstr,"ENOTSOCK"); |
| 369 |
else if (err == EOPNOTSUPP ) strcpy(errstr,"EOPNOTSUPP"); |
| 370 |
else if (err == ESHUTDOWN ) strcpy(errstr,"ESHUTDOWN"); |
| 371 |
else if (err == EWOULDBLOCK ) strcpy(errstr,"EWOULDBLOCK"); |
| 372 |
else if (err == EMSGSIZE ) strcpy(errstr,"EMSGSIZE"); |
| 373 |
else if (err == ETIMEDOUT ) strcpy(errstr,"ETIMEDOUT"); |
| 374 |
else if (err == ECONNABORTED ) strcpy(errstr,"ECONNABORTED"); |
| 375 |
else if (err == ECONNREFUSED ) strcpy(errstr,"ECONNREFUSED"); |
| 376 |
else if (err == ECONNRESET ) strcpy(errstr,"ECONNRESET"); |
| 377 |
else if (err == ENOBUFS ) strcpy(errstr,"ENOBUFS"); |
| 378 |
#endif |
| 379 |
|
| 380 |
else sprintf(errstr,"VIO_UNKNOWN_ERROR(%d)",err); |
| 381 |
return errstr; |
| 382 |
} |
| 383 |
|
| 384 |
/* |
| 385 |
* *************************************************************************** |
| 386 |
* Routine: Vio_ctor |
| 387 |
* |
| 388 |
* Purpose: Construct the [sdio/file/buff/unix/inet] container object. |
| 389 |
* |
| 390 |
* Author: Michael Holst |
| 391 |
* *************************************************************************** |
| 392 |
*/ |
| 393 |
VPUBLIC Vio* Vio_ctor(const char *socktype, const char *datafrmt, |
| 394 |
const char *hostname, const char *filename, const char *rwkey) |
| 395 |
{ |
| 396 |
Vio *thee = VNULL; |
| 397 |
|
| 398 |
/* make sure Vio was started */ |
| 399 |
VJMPERR1( VIOstarted ); |
| 400 |
|
| 401 |
thee = (Vio*)calloc( 1, sizeof(Vio) ); |
| 402 |
VJMPERR2( thee != VNULL ); |
| 403 |
VJMPERR3( Vio_ctor2(thee, socktype, datafrmt, hostname, filename, rwkey) ); |
| 404 |
|
| 405 |
/* normal return */ |
| 406 |
return thee; |
| 407 |
|
| 408 |
VERROR1: |
| 409 |
fprintf(stderr,"Vio_ctor: Vio library has not been started.\n"); |
| 410 |
return VNULL; |
| 411 |
|
| 412 |
VERROR2: |
| 413 |
fprintf(stderr,"Vio_ctor: malloc of Vio structure failed.\n"); |
| 414 |
return VNULL; |
| 415 |
|
| 416 |
VERROR3: |
| 417 |
fprintf(stderr,"Vio_ctor: Vio_ctor2() failed.\n"); |
| 418 |
Vio_dtor(&thee); |
| 419 |
return VNULL; |
| 420 |
} |
| 421 |
|
| 422 |
/* |
| 423 |
* *************************************************************************** |
| 424 |
* Routine: Vio_ctor2 |
| 425 |
* |
| 426 |
* Purpose: Construct the [sdio/file/buff/unix/inet] container object. |
| 427 |
* |
| 428 |
* Author: Michael Holst |
| 429 |
* *************************************************************************** |
| 430 |
*/ |
| 431 |
VPUBLIC int Vio_ctor2(Vio *thee, const char *socktype, const char *datafrmt, |
| 432 |
const char *hostname, const char *filename, const char *rwkey) |
| 433 |
{ |
| 434 |
int n, ival; |
| 435 |
char host[VMAX_ARGLEN]; |
| 436 |
struct linger ling; |
| 437 |
unsigned int len; |
| 438 |
struct hostent *hpTmp; |
| 439 |
#if defined(HAVE_WINSOCK_H) |
| 440 |
WSADATA wsaData; |
| 441 |
#endif |
| 442 |
#if defined(HAVE_SYS_UN_H) |
| 443 |
char username[VMAX_ARGLEN]; |
| 444 |
#endif |
| 445 |
|
| 446 |
/* make sure Vio was started */ |
| 447 |
VJMPERR1( VIOstarted ); |
| 448 |
|
| 449 |
/* initialize all Vio fields */ |
| 450 |
thee->type = VIO_NO_TYPE; |
| 451 |
thee->frmt = VIO_NO_FRMT; |
| 452 |
thee->rwkey = VIO_NO_RW; |
| 453 |
thee->error = 0; |
| 454 |
thee->dirty = 0; |
| 455 |
thee->fp = VNULL; |
| 456 |
thee->axdr = VNULL; |
| 457 |
thee->so = -1; |
| 458 |
thee->soc = -1; |
| 459 |
thee->name = VNULL; |
| 460 |
|
| 461 |
/* initialize the internal buffer (for BUFF datatype) */ |
| 462 |
thee->VIObuffer = VNULL; |
| 463 |
thee->VIObufferLen = 0; |
| 464 |
thee->VIObufferPtr = 0; |
| 465 |
|
| 466 |
/* initialize the socktype field */ |
| 467 |
if (!strcmp(socktype,"SDIO")) { |
| 468 |
thee->type = VIO_SDIO; |
| 469 |
} else if (!strcmp(socktype,"FILE")) { |
| 470 |
thee->type = VIO_FILE; |
| 471 |
} else if (!strcmp(socktype,"BUFF")) { |
| 472 |
thee->type = VIO_BUFF; |
| 473 |
} else if (!strcmp(socktype,"UNIX")) { |
| 474 |
thee->type = VIO_UNIX; |
| 475 |
} else if (!strcmp(socktype,"INET")) { |
| 476 |
thee->type = VIO_INET; |
| 477 |
} else { |
| 478 |
fprintf(stderr,"Vio_ctor2: Incorrect socktype given <%s>\n",socktype); |
| 479 |
VJMPERR2( 0 ); |
| 480 |
} |
| 481 |
|
| 482 |
/* initialize the datafrmt field */ |
| 483 |
if (!strcmp(datafrmt,"ASC")) { |
| 484 |
thee->frmt = VIO_ASC; |
| 485 |
} else if (!strcmp(datafrmt,"XDR")) { |
| 486 |
thee->frmt = VIO_XDR; |
| 487 |
} else { |
| 488 |
fprintf(stderr,"Vio_ctor2: Incorrect datafrmt given <%s>\n", datafrmt); |
| 489 |
VJMPERR2( 0 ); |
| 490 |
} |
| 491 |
|
| 492 |
/* initialize the r/w field */ |
| 493 |
if (!strcmp(rwkey,"r")) { |
| 494 |
thee->rwkey = VIO_R; |
| 495 |
} else if (!strcmp(rwkey,"w")) { |
| 496 |
thee->rwkey = VIO_W; |
| 497 |
} else { |
| 498 |
fprintf(stderr,"Vio_ctor2: Incorrect rwkey given <%s>\n", rwkey); |
| 499 |
VJMPERR2( 0 ); |
| 500 |
} |
| 501 |
|
| 502 |
/* need to call this stupid Win32 function before gethostname... */ |
| 503 |
#if defined(HAVE_WINSOCK_H) |
| 504 |
if ( WSAStartup(0x0101, &wsaData) != 0 ) { |
| 505 |
fprintf(stderr, "Vio_ctor2: WSAStartup fail INET sock <%s>" |
| 506 |
" dueto <%s>\n", thee->file,VIOstrerrno(errno)); |
| 507 |
VJMPERR2( 0 ); |
| 508 |
} |
| 509 |
#endif |
| 510 |
|
| 511 |
/* get "my" local hostname */ |
| 512 |
#if defined(FETK_STATIC_BUILD) |
| 513 |
strcpy(thee->lhost,"unknown"); |
| 514 |
#else |
| 515 |
if ((VIOgethostname(thee->lhost,sizeof(thee->lhost))) < 0) { |
| 516 |
fprintf(stderr, |
| 517 |
"Vio_ctor2: Gethostname fail INET sock <%s> dueto <%s>\n", |
| 518 |
thee->file, VIOstrerrno(errno)); |
| 519 |
strcpy(thee->lhost,"unknown"); |
| 520 |
} else if ((hpTmp=gethostbyname(thee->lhost))==VNULL) { |
| 521 |
fprintf(stderr, |
| 522 |
"Vio_ctor2: Gethostbyname fail INET sock <%s> dueto <%s>\n", |
| 523 |
thee->file, VIOstrerrno(errno)); |
| 524 |
strcpy(thee->lhost,"unknown"); |
| 525 |
} else strcpy(thee->lhost,hpTmp->h_name); |
| 526 |
#endif |
| 527 |
|
| 528 |
/* default remote hostname */ |
| 529 |
strcpy(thee->rhost,"unknown"); |
| 530 |
|
| 531 |
/* initialize the buffer space */ |
| 532 |
Vio_initIoPutBuffers(thee); |
| 533 |
|
| 534 |
/* SDIO READ/WRITE SETUP */ |
| 535 |
if (thee->type==VIO_SDIO) { |
| 536 |
|
| 537 |
if (thee->rwkey==VIO_R) { |
| 538 |
thee->fp = stdin; |
| 539 |
} else { /* (thee->rwkey==VIO_W) */ |
| 540 |
thee->fp = stdout; |
| 541 |
} |
| 542 |
VJMPERR2( thee->fp != VNULL ); |
| 543 |
|
| 544 |
/* FILE READ/WRITE SETUP */ |
| 545 |
} else if (thee->type==VIO_FILE) { |
| 546 |
|
| 547 |
/* filename is the i/o file name */ |
| 548 |
if (strlen(filename) >= VMAX_ARGLEN) { |
| 549 |
fprintf(stderr, "Vio_ctor2: Filename <%d> exceeds max <%d>!\n", |
| 550 |
(int)strlen(filename), VMAX_ARGLEN); |
| 551 |
VJMPERR2( 0 ); |
| 552 |
} |
| 553 |
strncpy(thee->file, filename, VMAX_ARGLEN); |
| 554 |
if (thee->rwkey==VIO_R) { |
| 555 |
thee->fp = fopen(thee->file, "r"); |
| 556 |
} else { /* (thee->rwkey==VIO_W) */ |
| 557 |
thee->fp = fopen(thee->file, "w"); |
| 558 |
} |
| 559 |
VJMPERR2( thee->fp != VNULL ); |
| 560 |
|
| 561 |
/* BUFF READ/WRITE SETUP */ |
| 562 |
} else if (thee->type==VIO_BUFF) { |
| 563 |
|
| 564 |
/* filename is the internal buffer number for the buffer */ |
| 565 |
thee->VIObufferPtr = 0; |
| 566 |
|
| 567 |
/* UNIX SOCKET READ/WRITE SETUP */ |
| 568 |
} else if (thee->type==VIO_UNIX) { |
| 569 |
|
| 570 |
#if defined(HAVE_SYS_UN_H) |
| 571 |
|
| 572 |
/* filename is socketName-userName in the directory /tmp */ |
| 573 |
|
| 574 |
VASSERT( Vnm_getuser(username, sizeof(username)) ); |
| 575 |
sprintf(thee->file, "/tmp/%s-%s", filename, username); |
| 576 |
|
| 577 |
/* create the socket address structure */ |
| 578 |
thee->name = (struct sockaddr_un *) |
| 579 |
calloc( 1, sizeof(struct sockaddr_un) ); |
| 580 |
VJMPERR2( thee->name != VNULL ); |
| 581 |
|
| 582 |
/* Get a socket structure */ |
| 583 |
if ((thee->so=socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { |
| 584 |
fprintf(stderr,"Vio_ctor2: fail to find UNIX sock dueto <%s>.\n", |
| 585 |
VIOstrerrno(errno)); |
| 586 |
VJMPERR2( 0 ); |
| 587 |
} else { |
| 588 |
|
| 589 |
/* set REUSEADDR so sockets can be closed and reopened */ |
| 590 |
ival = 1; /* just need a nonzero value */ |
| 591 |
if ( setsockopt(thee->so,SOL_SOCKET,SO_REUSEADDR, |
| 592 |
(void*)&ival,sizeof(ival)) < 0 ) { |
| 593 |
fprintf(stderr, "Vio_ctor2: Setsockopt1 fail UNIX sock <%s>" |
| 594 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 595 |
VJMPERR2( 0 ); |
| 596 |
} |
| 597 |
|
| 598 |
/* turn on LINGER so WRITES complete before socks close */ |
| 599 |
ling.l_onoff = 1; /* just need a nonzero value */ |
| 600 |
ling.l_linger = 30; /* linger time in seconds */ |
| 601 |
if ( setsockopt(thee->so,SOL_SOCKET,SO_LINGER, |
| 602 |
(void*)&ling,sizeof(ling)) < 0) { |
| 603 |
fprintf(stderr, "Vio_ctor2: Setsockopt2 fail UNIX sock <%s>" |
| 604 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 605 |
VJMPERR2( 0 ); |
| 606 |
} |
| 607 |
|
| 608 |
/* Setup for the socket */ |
| 609 |
memset(thee->name, '\0', sizeof(struct sockaddr_un)); |
| 610 |
((struct sockaddr_un *)(thee->name))->sun_family = AF_UNIX; |
| 611 |
strcpy(((struct sockaddr_un *)(thee->name))->sun_path, |
| 612 |
thee->file); |
| 613 |
|
| 614 |
/* if we are reader, WE are responsible for creating socket */ |
| 615 |
/* the reader must do: (unlink/)setsockopt/bind/listen */ |
| 616 |
if (thee->rwkey==VIO_R) { |
| 617 |
|
| 618 |
/* define socket file; remove previous socket */ |
| 619 |
unlink(thee->file); |
| 620 |
|
| 621 |
/* determine structure size; AF_UNIX is variable length */ |
| 622 |
len = sizeof(((struct sockaddr_un *) |
| 623 |
(thee->name))->sun_family) |
| 624 |
+ strlen(((struct sockaddr_un *) |
| 625 |
(thee->name))->sun_path); |
| 626 |
|
| 627 |
/* Bind socket to address (must setsockopts before bind) */ |
| 628 |
if (bind(thee->so,(struct sockaddr *)(thee->name),len)<0) { |
| 629 |
fprintf(stderr, |
| 630 |
"Vio_ctor2: Bind fail UNIX sock <%s> dueto <%s>\n", |
| 631 |
thee->file, VIOstrerrno(errno)); |
| 632 |
VJMPERR2( 0 ); |
| 633 |
} else { |
| 634 |
|
| 635 |
/* Tell socket to start listening for connections */ |
| 636 |
if (listen(thee->so,5) < 0) { |
| 637 |
fprintf(stderr, |
| 638 |
"Vio_ctor2: List fail UNIX sock <%s> dueto <%s>\n", |
| 639 |
thee->file, VIOstrerrno(errno)); |
| 640 |
VJMPERR2( 0 ); |
| 641 |
} |
| 642 |
} |
| 643 |
/* |
| 644 |
* if we got to here, we can assume reader has done |
| 645 |
* all of: (unlink/)setsockopt/bind/listen |
| 646 |
*/ |
| 647 |
} |
| 648 |
} |
| 649 |
#endif |
| 650 |
|
| 651 |
/* INET SOCKET READ/WRITE SETUP */ |
| 652 |
} else if (thee->type==VIO_INET) { |
| 653 |
|
| 654 |
/* filename is the port number for the socket */ |
| 655 |
if (strlen(filename) >= VMAX_ARGLEN) { |
| 656 |
fprintf(stderr, "Vio_ctor2: Filename <%d> exceeds max <%d>!\n", |
| 657 |
(int)strlen(filename), VMAX_ARGLEN); |
| 658 |
VJMPERR2( 0 ); |
| 659 |
} |
| 660 |
strncpy(thee->file, filename, VMAX_ARGLEN); |
| 661 |
|
| 662 |
/* create the socket address structure */ |
| 663 |
thee->name = (struct sockaddr_in *) |
| 664 |
calloc( 1, sizeof(struct sockaddr_in) ); |
| 665 |
VJMPERR2( thee->name != VNULL ); |
| 666 |
|
| 667 |
/* Look for sockets */ |
| 668 |
if ((thee->so=socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
| 669 |
fprintf(stderr,"Vio_ctor2: fail to find INET sock dueto <%s>\n", |
| 670 |
VIOstrerrno(errno)); |
| 671 |
VJMPERR2( 0 ); |
| 672 |
} else { |
| 673 |
|
| 674 |
/* set REUSEADDR so sockets can be closed and reopened */ |
| 675 |
ival = 1; /* just need a nonzero value */ |
| 676 |
if ( setsockopt(thee->so,SOL_SOCKET,SO_REUSEADDR, |
| 677 |
(void*)&ival,sizeof(ival)) < 0 ) { |
| 678 |
fprintf(stderr, "Vio_ctor2: Setsockopt3 fail INET sock <%s>" |
| 679 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 680 |
VJMPERR2( 0 ); |
| 681 |
} |
| 682 |
|
| 683 |
/* turn on LINGER so WRITES complete before sockets close */ |
| 684 |
ling.l_onoff = 1; /* just need a nonzero value */ |
| 685 |
ling.l_linger = 30; /* linger time in seconds */ |
| 686 |
if ( setsockopt(thee->so,SOL_SOCKET,SO_LINGER, |
| 687 |
(void*)&ling,sizeof(ling)) < 0 ) { |
| 688 |
fprintf(stderr, "Vio_ctor2: Setsockopt4 fail INET sock <%s>" |
| 689 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 690 |
VJMPERR2( 0 ); |
| 691 |
} |
| 692 |
|
| 693 |
/* Setup for the socket */ |
| 694 |
memset(thee->name, '\0', sizeof(struct sockaddr_in)); |
| 695 |
((struct sockaddr_in *)(thee->name))->sin_family = AF_INET; |
| 696 |
((struct sockaddr_in *)(thee->name))->sin_port |
| 697 |
= htons( (unsigned short) (VPORTNUMBER + atoi(thee->file)) ); |
| 698 |
|
| 699 |
/* if we are the reader, WE must create the socket */ |
| 700 |
/* the reader must do: setsockopt/bind/listen */ |
| 701 |
if (thee->rwkey==VIO_R) { |
| 702 |
|
| 703 |
/* use wildcard address */ |
| 704 |
n = INADDR_ANY; |
| 705 |
memcpy(&((struct sockaddr_in *)(thee->name))->sin_addr, |
| 706 |
&n, sizeof(long)); |
| 707 |
|
| 708 |
/* determine structure size; AF_INET is fixed length */ |
| 709 |
len = sizeof(struct sockaddr_in); |
| 710 |
|
| 711 |
/* Bind socket to address (must setsockopts before bind) */ |
| 712 |
if (bind(thee->so,(struct sockaddr *)(thee->name),len)<0) { |
| 713 |
fprintf(stderr, |
| 714 |
"Vio_ctor2: Bind fail INET sock <%s> dueto <%s>\n", |
| 715 |
thee->file, VIOstrerrno(errno)); |
| 716 |
VJMPERR2( 0 ); |
| 717 |
} else { |
| 718 |
|
| 719 |
/* Tell socket to start listening for connections */ |
| 720 |
if (listen(thee->so,5) < 0) { |
| 721 |
fprintf(stderr, |
| 722 |
"Vio_ctor2: List fail INET sock <%s> dueto <%s>\n", |
| 723 |
thee->file, VIOstrerrno(errno)); |
| 724 |
VJMPERR2( 0 ); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
/* assume reader has done: setsockopt/bind/listen */ |
| 729 |
} else { |
| 730 |
|
| 731 |
/* network address of port -- "localhost" means WE have port */ |
| 732 |
if (!strcmp(hostname,"localhost")) { |
| 733 |
strcpy(host,thee->lhost); |
| 734 |
} else { |
| 735 |
strcpy(host,hostname); |
| 736 |
} |
| 737 |
|
| 738 |
#if defined(FETK_STATIC_BUILD) |
| 739 |
strcpy(thee->rhost,"unknown"); |
| 740 |
#else |
| 741 |
/* get IP address corresponding to this server hostname */ |
| 742 |
if ((hpTmp=gethostbyname(host))==VNULL) { |
| 743 |
fprintf(stderr, |
| 744 |
"Vio_ctor2: Gethostbyname fail INET sock <%s>" |
| 745 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 746 |
VJMPERR2( 0 ); |
| 747 |
} else { |
| 748 |
|
| 749 |
/* just need to save address of host that has socket */ |
| 750 |
memcpy( |
| 751 |
&(((struct sockaddr_in *)(thee->name))->sin_addr), |
| 752 |
hpTmp->h_addr_list[0], (unsigned int)hpTmp->h_length); |
| 753 |
|
| 754 |
/* save the hostname for the port for later i/o */ |
| 755 |
strcpy(thee->rhost,hpTmp->h_name); |
| 756 |
} |
| 757 |
#endif |
| 758 |
} |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
/* initialize <asc,xdr> datastructures; must do almost at the end */ |
| 763 |
if (thee->frmt==VIO_ASC) { |
| 764 |
thee->axdr = (ASC*)calloc( 1, sizeof(ASC) ); |
| 765 |
VJMPERR2( thee->axdr != VNULL ); |
| 766 |
if (thee->rwkey==VIO_R) { |
| 767 |
ascmem_create(thee->axdr, thee->ioBuffer, VMAX_BUFSIZE, ASC_DECODE); |
| 768 |
} else { /* if (thee->rwkey==VIO_W) */ |
| 769 |
ascmem_create(thee->axdr, thee->ioBuffer, VMAX_BUFSIZE, ASC_ENCODE); |
| 770 |
} |
| 771 |
} else if (thee->frmt==VIO_XDR) { |
| 772 |
thee->axdr = (XDR*)calloc( 1, sizeof(XDR) ); |
| 773 |
VJMPERR2( thee->axdr != VNULL ); |
| 774 |
if (thee->rwkey==VIO_R) { |
| 775 |
xdrmem_create(thee->axdr, thee->ioBuffer, VMAX_BUFSIZE, XDR_DECODE); |
| 776 |
} else { /* if (thee->rwkey==VIO_W) */ |
| 777 |
xdrmem_create(thee->axdr, thee->ioBuffer, VMAX_BUFSIZE, XDR_ENCODE); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
/* lastly: default white/comment char sets (careful! propogates to axdr) */ |
| 782 |
Vio_setWhiteChars(thee, VIOwhiteChars); |
| 783 |
Vio_setCommChars(thee, VIOcommChars); |
| 784 |
|
| 785 |
/* return without error */ |
| 786 |
return 1; |
| 787 |
|
| 788 |
VERROR1: |
| 789 |
fprintf(stderr,"Vio_ctor2: Vio library has not been started.\n"); |
| 790 |
return 0; |
| 791 |
|
| 792 |
VERROR2: |
| 793 |
fprintf(stderr,"Vio_ctor2: some error occurred.\n"); |
| 794 |
return 0; |
| 795 |
} |
| 796 |
|
| 797 |
/* |
| 798 |
* *************************************************************************** |
| 799 |
* Routine: Vio_dtor |
| 800 |
* |
| 801 |
* Purpose: Destroy the [sdio/file/buff/unix/inet] container object. |
| 802 |
* |
| 803 |
* Author: Michael Holst |
| 804 |
* *************************************************************************** |
| 805 |
*/ |
| 806 |
VPUBLIC void Vio_dtor(Vio **thee) |
| 807 |
{ |
| 808 |
if ((*thee) != VNULL) { |
| 809 |
if ((*thee)->VIObuffer != VNULL) { |
| 810 |
free( (*thee)->VIObuffer ); |
| 811 |
(*thee)->VIObuffer = VNULL; |
| 812 |
} |
| 813 |
Vio_dtor2(*thee); |
| 814 |
free( (*thee) ); |
| 815 |
(*thee) = VNULL; |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
/* |
| 820 |
* *************************************************************************** |
| 821 |
* Routine: Vio_dtor2 |
| 822 |
* |
| 823 |
* Purpose: Destroy the [sdio/file/buff/unix/inet] container object. |
| 824 |
* |
| 825 |
* Author: Michael Holst |
| 826 |
* *************************************************************************** |
| 827 |
*/ |
| 828 |
VPUBLIC void Vio_dtor2(Vio *thee) |
| 829 |
{ |
| 830 |
if (thee != VNULL) { |
| 831 |
|
| 832 |
/* free the <ASC,XDR> structures */ |
| 833 |
if ( thee->axdr != VNULL ) { |
| 834 |
if ( thee->frmt == VIO_ASC ) { |
| 835 |
asc_destroy( (ASC*)(thee->axdr) ); |
| 836 |
} else if ( thee->frmt == VIO_XDR ) { |
| 837 |
xdr_destroy( (XDR*)(thee->axdr) ); |
| 838 |
} |
| 839 |
free( thee->axdr ); |
| 840 |
thee->axdr = VNULL; |
| 841 |
} |
| 842 |
|
| 843 |
/* finish up */ |
| 844 |
if (thee->type==VIO_SDIO) { |
| 845 |
/* no-op */ |
| 846 |
} else if (thee->type==VIO_FILE) { |
| 847 |
if ( thee->fp != VNULL ) { |
| 848 |
if ( fclose(thee->fp) != 0 ) { |
| 849 |
fprintf(stderr, "Vio_dtor2: fclose fail device <%s>" |
| 850 |
" dueto <%s>\n", thee->file,VIOstrerrno(errno)); |
| 851 |
} |
| 852 |
} |
| 853 |
} else if (thee->type==VIO_BUFF) { |
| 854 |
/* CMIKE: WHAT ABOUT FREEING THE BUFFER SPACE??? */ |
| 855 |
thee->VIObufferPtr = 0; |
| 856 |
} else if ( (thee->type==VIO_UNIX) |
| 857 |
|| (thee->type==VIO_INET) ) { |
| 858 |
if ( thee->soc >= 0 ) { |
| 859 |
#if defined(HAVE_WINSOCK_H) |
| 860 |
if ( closesocket(thee->soc) != 0 ) { |
| 861 |
fprintf(stderr, "Vio_dtor2: closesocket1 fail device <%s>" |
| 862 |
" dueto <%s>\n", thee->file, |
| 863 |
VIOstrerrno(errno)); |
| 864 |
} |
| 865 |
#else |
| 866 |
if ( close(thee->soc) != 0 ) { |
| 867 |
fprintf(stderr, "Vio_dtor2: close1 fail device <%s>" |
| 868 |
" dueto <%s>\n", thee->file, |
| 869 |
VIOstrerrno(errno)); |
| 870 |
} |
| 871 |
#endif |
| 872 |
} |
| 873 |
if ( thee->so >= 0 ) { |
| 874 |
#if defined(HAVE_WINSOCK_H) |
| 875 |
if ( closesocket(thee->so) != 0 ) { |
| 876 |
fprintf(stderr, "Vio_dtor2: closesocket2 fail device <%s>" |
| 877 |
" dueto <%s>\n", thee->file, |
| 878 |
VIOstrerrno(errno)); |
| 879 |
} |
| 880 |
#else |
| 881 |
if ( close(thee->so) != 0 ) { |
| 882 |
fprintf(stderr, "Vio_dtor2: close2 fail device <%s>" |
| 883 |
" dueto <%s>\n", thee->file, |
| 884 |
VIOstrerrno(errno)); |
| 885 |
} |
| 886 |
#endif |
| 887 |
} |
| 888 |
|
| 889 |
/* remove the device file for domain sockets */ |
| 890 |
if (thee->type==VIO_UNIX) |
| 891 |
if (thee->rwkey==VIO_R) |
| 892 |
unlink(thee->file); |
| 893 |
|
| 894 |
} else { |
| 895 |
fprintf(stderr,"Vio_dtor2: Bad type found <%d>\n", thee->type); |
| 896 |
} |
| 897 |
|
| 898 |
if ( (thee->type==VIO_UNIX) |
| 899 |
|| (thee->type==VIO_INET) ) { |
| 900 |
if (thee->name != VNULL) { |
| 901 |
free( thee->name ); |
| 902 |
} |
| 903 |
thee->name = VNULL; |
| 904 |
} |
| 905 |
|
| 906 |
/* we called WSAStartup() in constructor; must always be paired */ |
| 907 |
#if defined(HAVE_WINSOCK_H) |
| 908 |
WSACleanup(); |
| 909 |
#endif |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
/* |
| 914 |
* *************************************************************************** |
| 915 |
* Routine: Vio_setWhiteChars |
| 916 |
* |
| 917 |
* Purpose: Define the white character set. |
| 918 |
* |
| 919 |
* Author: Michael Holst |
| 920 |
* *************************************************************************** |
| 921 |
*/ |
| 922 |
VPUBLIC void Vio_setWhiteChars(Vio *thee, char *whiteChars) |
| 923 |
{ |
| 924 |
if (thee != VNULL) { |
| 925 |
strncpy(thee->whiteChars, whiteChars, VMAX_ARGNUM); |
| 926 |
|
| 927 |
/* propogate the character set down to the ASC structure */ |
| 928 |
VASSERT( thee->axdr != VNULL ); |
| 929 |
if (thee->frmt == VIO_ASC) { |
| 930 |
asc_setWhiteChars(thee->axdr, whiteChars); |
| 931 |
} else if (thee->frmt == VIO_XDR) { |
| 932 |
#if !defined(HAVE_XDR) |
| 933 |
asc_setWhiteChars(thee->axdr, whiteChars); |
| 934 |
#endif |
| 935 |
} else { VASSERT( 0 ); } |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
/* |
| 940 |
* *************************************************************************** |
| 941 |
* Routine: Vio_setCommChars |
| 942 |
* |
| 943 |
* Purpose: Define the comment character set. |
| 944 |
* |
| 945 |
* Author: Michael Holst |
| 946 |
* *************************************************************************** |
| 947 |
*/ |
| 948 |
VPUBLIC void Vio_setCommChars(Vio *thee, char *commChars) |
| 949 |
{ |
| 950 |
if (thee != VNULL) { |
| 951 |
strncpy(thee->commChars, commChars, VMAX_ARGNUM); |
| 952 |
|
| 953 |
/* propogate the character set down to the ASC structure */ |
| 954 |
VASSERT( thee->axdr != VNULL ); |
| 955 |
if (thee->frmt == VIO_ASC) { |
| 956 |
asc_setCommChars(thee->axdr, commChars); |
| 957 |
} else if (thee->frmt == VIO_XDR) { |
| 958 |
#if !defined(HAVE_XDR) |
| 959 |
asc_setCommChars(thee->axdr, commChars); |
| 960 |
#endif |
| 961 |
} else { VASSERT( 0 ); } |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
/* |
| 966 |
* *************************************************************************** |
| 967 |
* Routine: Vio_accept |
| 968 |
* |
| 969 |
* Purpose: Accept any waiting connect attempt to our socket on our machine. |
| 970 |
* |
| 971 |
* Notes: The nonblock parameter has the following interpretation: |
| 972 |
* (Only for <UNIX/INET>; othewise it is ignored.) |
| 973 |
* |
| 974 |
* nonblock==0 ==> block until a connect is attempted |
| 975 |
* nonblock==1 ==> DO NOT block at all |
| 976 |
* |
| 977 |
* Author: Michael Holst |
| 978 |
* *************************************************************************** |
| 979 |
*/ |
| 980 |
VPUBLIC int Vio_accept(Vio *thee, int nonblock) |
| 981 |
{ |
| 982 |
int rc; |
| 983 |
|
| 984 |
#if defined(HAVE_WINSOCK_H) |
| 985 |
unsigned long blockKey; |
| 986 |
#else |
| 987 |
int flags = 0; |
| 988 |
struct sockaddr_in peer; |
| 989 |
struct hostent *hpTmp; |
| 990 |
#endif |
| 991 |
|
| 992 |
#if defined(ACCEPT_USES_ULONG) |
| 993 |
unsigned long len; |
| 994 |
#elif defined(ACCEPT_USES_UINT) |
| 995 |
unsigned int len; |
| 996 |
#elif defined(ACCEPT_USES_INT) |
| 997 |
int len; |
| 998 |
#else |
| 999 |
unsigned int len; |
| 1000 |
#endif |
| 1001 |
|
| 1002 |
/* reset error tag */ |
| 1003 |
thee->error = 0; |
| 1004 |
|
| 1005 |
thee->soc = -1; |
| 1006 |
rc = -1; |
| 1007 |
|
| 1008 |
Vio_initIoPutBuffers(thee); |
| 1009 |
VJMPERR2( thee->rwkey == VIO_R ); |
| 1010 |
|
| 1011 |
if ( (thee->type==VIO_SDIO) |
| 1012 |
|| (thee->type==VIO_FILE) |
| 1013 |
|| (thee->type==VIO_BUFF) ) { |
| 1014 |
|
| 1015 |
/* ONLY for file i/o, we need to look at and set the dirty bit */ |
| 1016 |
/* (this keeps us from reading the file twice) */ |
| 1017 |
if (thee->type==VIO_FILE) { |
| 1018 |
if ((!thee->dirty) && (!feof(thee->fp))) { |
| 1019 |
thee->dirty = 1; |
| 1020 |
rc = 1; |
| 1021 |
} |
| 1022 |
} else { |
| 1023 |
rc = 1; |
| 1024 |
} |
| 1025 |
|
| 1026 |
} else if (thee->type==VIO_UNIX) { |
| 1027 |
|
| 1028 |
#if defined(HAVE_SYS_UN_H) |
| 1029 |
/* Make this a non-blocking socket just for the accept call */ |
| 1030 |
if (nonblock) { |
| 1031 |
flags = fcntl( thee->so, F_GETFL, 0 ); |
| 1032 |
fcntl( thee->so, F_SETFL, flags | VO_NONBLOCK ); |
| 1033 |
} |
| 1034 |
|
| 1035 |
/* accept */ |
| 1036 |
len = sizeof(struct sockaddr_un); |
| 1037 |
rc = accept(thee->so,(struct sockaddr *)(thee->name),&len); |
| 1038 |
thee->soc = rc; |
| 1039 |
if ((!nonblock) && (rc < 0)) { |
| 1040 |
fprintf(stderr, "Vio_accept: Accept fail UNIX sock <%s>" |
| 1041 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1042 |
VJMPERR2( 0 ); |
| 1043 |
} |
| 1044 |
|
| 1045 |
/* restore blocking -- must nonblock for LINGER to work! */ |
| 1046 |
if (nonblock) { |
| 1047 |
fcntl( thee->so, F_SETFL, flags ); |
| 1048 |
} |
| 1049 |
#endif |
| 1050 |
|
| 1051 |
} else if (thee->type==VIO_INET) { |
| 1052 |
|
| 1053 |
/* Make this non-blocking socket just for accept call */ |
| 1054 |
if (nonblock) { |
| 1055 |
#if defined(HAVE_WINSOCK_H) |
| 1056 |
blockKey = 1; |
| 1057 |
if ( ioctlsocket( thee->so, FIONBIO, &blockKey ) != 0 ) { |
| 1058 |
fprintf(stderr, "Vio_accept: Ioctlsocket1 fail INET sock <%s>" |
| 1059 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1060 |
VJMPERR2( 0 ); |
| 1061 |
} |
| 1062 |
#else |
| 1063 |
flags = fcntl( thee->so, F_GETFL, 0 ); |
| 1064 |
fcntl( thee->so, F_SETFL, flags | VO_NONBLOCK ); |
| 1065 |
#endif |
| 1066 |
} |
| 1067 |
|
| 1068 |
len = sizeof(struct sockaddr_in); |
| 1069 |
rc = accept(thee->so, (struct sockaddr *)(thee->name), &len); |
| 1070 |
thee->soc = rc; |
| 1071 |
if ((!nonblock) && (rc < 0)) { |
| 1072 |
fprintf(stderr, "Vio_accept: Accept fail INET sock <%s>" |
| 1073 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1074 |
VJMPERR2( 0 ); |
| 1075 |
} |
| 1076 |
|
| 1077 |
/* restore blocking -- must nonblock for LINGER to work! */ |
| 1078 |
if (nonblock) { |
| 1079 |
#if defined(HAVE_WINSOCK_H) |
| 1080 |
blockKey = 0; |
| 1081 |
if ( ioctlsocket( thee->so, FIONBIO, &blockKey ) != 0 ) { |
| 1082 |
fprintf(stderr, "Vio_accept: Ioctlsocket2 fail INET sock <%s>" |
| 1083 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1084 |
VJMPERR2( 0 ); |
| 1085 |
} |
| 1086 |
#else |
| 1087 |
fcntl( thee->so, F_SETFL, flags ); |
| 1088 |
#endif |
| 1089 |
} |
| 1090 |
|
| 1091 |
/* if we found a writer, get his hostname (just for i/o) */ |
| 1092 |
if (rc >= 0) { |
| 1093 |
#if defined(HAVE_WINSOCK_H) || defined(FETK_STATIC_BUILD) |
| 1094 |
strcpy(thee->rhost,"unknown"); |
| 1095 |
#else |
| 1096 |
len = sizeof(struct sockaddr_in); |
| 1097 |
if (getpeername(thee->soc,(struct sockaddr *)(&peer),&len)<0) { |
| 1098 |
fprintf(stderr, "Vio_accept: Getpeername fail INET <%s>" |
| 1099 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1100 |
VJMPERR2( 0 ); |
| 1101 |
} else if (VNULL==(hpTmp=gethostbyname(inet_ntoa(peer.sin_addr)))){ |
| 1102 |
fprintf(stderr, "Vio_accept: Gethostbyname fail INET <%s>" |
| 1103 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1104 |
VJMPERR2( 0 ); |
| 1105 |
} else { |
| 1106 |
strcpy(thee->rhost,hpTmp->h_name); |
| 1107 |
} |
| 1108 |
#endif |
| 1109 |
} |
| 1110 |
|
| 1111 |
} else { |
| 1112 |
fprintf(stderr,"Vio_accept: Bad type found <%d>\n", thee->type); |
| 1113 |
VJMPERR2( 0 ); |
| 1114 |
} |
| 1115 |
|
| 1116 |
/* normal return */ |
| 1117 |
return rc; |
| 1118 |
|
| 1119 |
VERROR2: |
| 1120 |
thee->error = 1; |
| 1121 |
return -1; |
| 1122 |
} |
| 1123 |
|
| 1124 |
/* |
| 1125 |
* *************************************************************************** |
| 1126 |
* Routine: Vio_acceptFree |
| 1127 |
* |
| 1128 |
* Purpose: Free the socket child that was used for the last accept. |
| 1129 |
* |
| 1130 |
* Author: Michael Holst |
| 1131 |
* *************************************************************************** |
| 1132 |
*/ |
| 1133 |
VPUBLIC void Vio_acceptFree(Vio *thee) |
| 1134 |
{ |
| 1135 |
/* VJMPERR2( !thee->error ); */ /* Need to close the socket... */ |
| 1136 |
VJMPERR2( thee->rwkey == VIO_R ); |
| 1137 |
|
| 1138 |
if ( (thee->type==VIO_SDIO) |
| 1139 |
|| (thee->type==VIO_FILE) |
| 1140 |
|| (thee->type==VIO_BUFF) ) { |
| 1141 |
/* no-op */ |
| 1142 |
} else if ( (thee->type==VIO_UNIX) |
| 1143 |
|| (thee->type==VIO_INET) ) { |
| 1144 |
if ( thee->soc >= 0 ) { |
| 1145 |
#if defined(HAVE_WINSOCK_H) |
| 1146 |
if ( closesocket(thee->soc) != 0 ) { |
| 1147 |
fprintf(stderr, "Vio_acceptFree: closesocket fail device <%s>" |
| 1148 |
" dueto <%s>\n", thee->file,VIOstrerrno(errno)); |
| 1149 |
VJMPERR2( 0 ); |
| 1150 |
} |
| 1151 |
#else |
| 1152 |
if ( close(thee->soc) != 0 ) { |
| 1153 |
fprintf(stderr, "Vio_acceptFree: close fail device <%s>" |
| 1154 |
" dueto <%s>\n", thee->file,VIOstrerrno(errno)); |
| 1155 |
VJMPERR2( 0 ); |
| 1156 |
} |
| 1157 |
#endif |
| 1158 |
} |
| 1159 |
} else { |
| 1160 |
fprintf(stderr,"Vio_acceptFree: Bad type found <%d>\n", thee->type); |
| 1161 |
VJMPERR2( 0 ); |
| 1162 |
} |
| 1163 |
|
| 1164 |
thee->soc = -1; |
| 1165 |
|
| 1166 |
/* normal return */ |
| 1167 |
Vio_initIoPutBuffers(thee); |
| 1168 |
return; |
| 1169 |
|
| 1170 |
VERROR2: |
| 1171 |
Vio_initIoPutBuffers(thee); |
| 1172 |
thee->error = 1; |
| 1173 |
return; |
| 1174 |
} |
| 1175 |
|
| 1176 |
/* |
| 1177 |
* *************************************************************************** |
| 1178 |
* Routine: Vio_connect |
| 1179 |
* |
| 1180 |
* Purpose: Connect to some socket on a remote machine (or on our machine). |
| 1181 |
* |
| 1182 |
* Notes: The nonblock parameter has the following interpretation: |
| 1183 |
* (Only for <UNIX/INET>; othewise it is ignored.) |
| 1184 |
* |
| 1185 |
* nonblock==0 ==> block until our connection is accepted |
| 1186 |
* nonblock==1 ==> DO NOT block at all |
| 1187 |
* |
| 1188 |
* Author: Michael Holst |
| 1189 |
* *************************************************************************** |
| 1190 |
*/ |
| 1191 |
VPUBLIC int Vio_connect(Vio *thee, int nonblock) |
| 1192 |
{ |
| 1193 |
int rc; |
| 1194 |
#if defined(HAVE_WINSOCK_H) |
| 1195 |
unsigned long len; |
| 1196 |
#else |
| 1197 |
int len; |
| 1198 |
int flags = 0; |
| 1199 |
#endif |
| 1200 |
|
| 1201 |
/* reset error tag */ |
| 1202 |
thee->error = 0; |
| 1203 |
|
| 1204 |
rc = -1; |
| 1205 |
|
| 1206 |
Vio_initIoPutBuffers(thee); |
| 1207 |
VJMPERR2( thee->rwkey == VIO_W ); |
| 1208 |
|
| 1209 |
if ( (thee->type==VIO_SDIO) |
| 1210 |
|| (thee->type==VIO_FILE) |
| 1211 |
|| (thee->type==VIO_BUFF) ) { |
| 1212 |
rc = 1; |
| 1213 |
} else if (thee->type==VIO_UNIX) { |
| 1214 |
|
| 1215 |
#if defined(HAVE_SYS_UN_H) |
| 1216 |
/* Make this a non-blocking socket just for the connect call */ |
| 1217 |
if (nonblock) { |
| 1218 |
flags = fcntl( thee->so, F_GETFL, 0 ); |
| 1219 |
fcntl( thee->so, F_SETFL, flags | VO_NONBLOCK ); |
| 1220 |
} |
| 1221 |
|
| 1222 |
/* blocking connect */ |
| 1223 |
len = sizeof(struct sockaddr_un); |
| 1224 |
rc = connect(thee->so, (struct sockaddr *)(thee->name),len); |
| 1225 |
if ((!nonblock) && (rc < 0)) { |
| 1226 |
fprintf(stderr, "Vio_connect: Conn fail UNIX sock <%s>" |
| 1227 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1228 |
VJMPERR2( 0 ); |
| 1229 |
} |
| 1230 |
|
| 1231 |
/* restore blocking -- must nonblock for LINGER to work! */ |
| 1232 |
if (nonblock) { |
| 1233 |
fcntl( thee->so, F_SETFL, flags ); |
| 1234 |
} |
| 1235 |
#endif |
| 1236 |
|
| 1237 |
} else if (thee->type==VIO_INET) { |
| 1238 |
|
| 1239 |
/* make this a non-blocking socket just for the connect call */ |
| 1240 |
if (nonblock) { |
| 1241 |
#if defined(HAVE_WINSOCK_H) |
| 1242 |
len = 1; |
| 1243 |
if ( ioctlsocket( thee->so, FIONBIO, &len ) != 0 ) { |
| 1244 |
fprintf(stderr, "Vio_connect: Ioctlsocket1 fail INET sock <%s>" |
| 1245 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1246 |
VJMPERR2( 0 ); |
| 1247 |
} |
| 1248 |
#else |
| 1249 |
flags = fcntl( thee->so, F_GETFL, 0 ); |
| 1250 |
fcntl( thee->so, F_SETFL, flags | VO_NONBLOCK ); |
| 1251 |
#endif |
| 1252 |
} |
| 1253 |
|
| 1254 |
/* blocking connect */ |
| 1255 |
len = sizeof(struct sockaddr_in); |
| 1256 |
rc = connect(thee->so, (struct sockaddr *)(thee->name),len); |
| 1257 |
if ((!nonblock) && (rc < 0)) { |
| 1258 |
fprintf(stderr, "Vio_connect: Conn fail INET sock <%s>" |
| 1259 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1260 |
VJMPERR2( 0 ); |
| 1261 |
} |
| 1262 |
|
| 1263 |
/* restore blocking -- must nonblock for LINGER to work! */ |
| 1264 |
if (nonblock) { |
| 1265 |
#if defined(HAVE_WINSOCK_H) |
| 1266 |
len = 0; |
| 1267 |
if ( ioctlsocket( thee->so, FIONBIO, &len ) != 0 ) { |
| 1268 |
fprintf(stderr, "Vio_connect: Ioctlsocket2 fail INET sock <%s>" |
| 1269 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1270 |
VJMPERR2( 0 ); |
| 1271 |
} |
| 1272 |
#else |
| 1273 |
fcntl( thee->so, F_SETFL, flags ); |
| 1274 |
#endif |
| 1275 |
} |
| 1276 |
|
| 1277 |
} else { |
| 1278 |
fprintf(stderr,"Vio_connect: Bad type found <%d>\n", thee->type); |
| 1279 |
VJMPERR2( 0 ); |
| 1280 |
} |
| 1281 |
|
| 1282 |
/* normal return */ |
| 1283 |
return rc; |
| 1284 |
|
| 1285 |
VERROR2: |
| 1286 |
thee->error = 1; |
| 1287 |
return -1; |
| 1288 |
} |
| 1289 |
|
| 1290 |
/* |
| 1291 |
* *************************************************************************** |
| 1292 |
* Routine: Vio_connectFree |
| 1293 |
* |
| 1294 |
* Purpose: Purge any output buffers (for <UNIX/INET>, else a no-op). |
| 1295 |
* |
| 1296 |
* Author: Michael Holst |
| 1297 |
* *************************************************************************** |
| 1298 |
*/ |
| 1299 |
VPUBLIC void Vio_connectFree(Vio *thee) |
| 1300 |
{ |
| 1301 |
/* VJMPERR2( !thee->error ); */ /* Need to close the socket... */ |
| 1302 |
VJMPERR2( thee->rwkey == VIO_W ); |
| 1303 |
|
| 1304 |
if ( (thee->type==VIO_SDIO) |
| 1305 |
|| (thee->type==VIO_FILE) |
| 1306 |
|| (thee->type==VIO_BUFF) ) { |
| 1307 |
/* no-op */ |
| 1308 |
} else if ( (thee->type==VIO_UNIX) |
| 1309 |
|| (thee->type==VIO_INET) ) { |
| 1310 |
Vio_purgePutBuffer(thee); |
| 1311 |
} else { |
| 1312 |
fprintf(stderr,"Vio_connectFree: Bad type found <%d>\n", thee->type); |
| 1313 |
VJMPERR2( 0 ); |
| 1314 |
} |
| 1315 |
|
| 1316 |
/* normal return */ |
| 1317 |
Vio_initIoPutBuffers(thee); |
| 1318 |
return; |
| 1319 |
|
| 1320 |
VERROR2: |
| 1321 |
Vio_initIoPutBuffers(thee); |
| 1322 |
thee->error = 1; |
| 1323 |
return; |
| 1324 |
} |
| 1325 |
|
| 1326 |
/* |
| 1327 |
* *************************************************************************** |
| 1328 |
* Routine: Vio_findNewline |
| 1329 |
* |
| 1330 |
* Purpose: finds the new line char if any from current position in iobuffer |
| 1331 |
* |
| 1332 |
* Author: Juan Brandi |
| 1333 |
* *************************************************************************** |
| 1334 |
*/ |
| 1335 |
VPUBLIC int Vio_findNewLine(Vio *thee){ |
| 1336 |
|
| 1337 |
char nullChar = '\n'; |
| 1338 |
int pntr = asc_getpos((ASC*)thee->axdr); |
| 1339 |
int isNewline = 0; |
| 1340 |
|
| 1341 |
if(thee == VNULL){ |
| 1342 |
Vnm_print(2, "Vio_findNewLine: Got NULL pointer!\n"); |
| 1343 |
return isNewline; |
| 1344 |
} |
| 1345 |
|
| 1346 |
char *buf; |
| 1347 |
buf = thee->ioBuffer; |
| 1348 |
|
| 1349 |
while(pntr < thee->ioBufferLen && isNewline == 0){ |
| 1350 |
if(memcmp(&buf[pntr], &nullChar,sizeof(char)) == 0){ |
| 1351 |
asc_setpos((ASC*)thee->axdr, pntr); |
| 1352 |
isNewline = 1; |
| 1353 |
} |
| 1354 |
pntr++; |
| 1355 |
} |
| 1356 |
|
| 1357 |
return isNewline; |
| 1358 |
|
| 1359 |
} |
| 1360 |
|
| 1361 |
/* |
| 1362 |
* *************************************************************************** |
| 1363 |
* Routine: Vio_scanf |
| 1364 |
* |
| 1365 |
* Purpose: Mimic "scanf" from an arbitrary Vio device. |
| 1366 |
* |
| 1367 |
* Author: Michael Holst |
| 1368 |
* *************************************************************************** |
| 1369 |
*/ |
| 1370 |
VPUBLIC int Vio_scanf(Vio *thee, char *parms, ... ) |
| 1371 |
{ |
| 1372 |
va_list ap; |
| 1373 |
char arg0, arg1, arg2, *cval, *sval, buf[VMAX_BUFSIZE]; |
| 1374 |
int i, len, tokCount, *ival; |
| 1375 |
float *fval; |
| 1376 |
double *dval; |
| 1377 |
|
| 1378 |
VJMPERR2( !thee->error ); |
| 1379 |
VJMPERR2( thee->rwkey == VIO_R ); |
| 1380 |
|
| 1381 |
/* get the value of the current pointer that points into the ioBuffer */ |
| 1382 |
len = 0; |
| 1383 |
if (thee->frmt == VIO_ASC) { |
| 1384 |
len = asc_getpos((ASC*)thee->axdr); |
| 1385 |
} else if (thee->frmt == VIO_XDR) { |
| 1386 |
len = xdr_getpos((XDR*)thee->axdr); |
| 1387 |
} else { VASSERT( 0 ); } |
| 1388 |
|
| 1389 |
/* if the buffer is completely empty (i.e., first time here) fill it up */ |
| 1390 |
if ( thee->ioBufferLen == 0 ) { |
| 1391 |
|
| 1392 |
/* read the data */ |
| 1393 |
thee->ioBufferLen = Vio_read( thee, thee->ioBuffer, VMAX_BUFSIZE ); |
| 1394 |
|
| 1395 |
/* set the buffer point to 0 */ |
| 1396 |
if (thee->frmt == VIO_ASC) { |
| 1397 |
VJMPERR1( asc_setpos((ASC*)thee->axdr, 0) ); |
| 1398 |
} else if (thee->frmt == VIO_XDR) { |
| 1399 |
VJMPERR1( xdr_setpos((XDR*)thee->axdr, 0) ); |
| 1400 |
} else { VASSERT( 0 ); } |
| 1401 |
|
| 1402 |
/* if current point is more than halfway through buf, read in more data */ |
| 1403 |
} else if ( len > (VMAX_BUFSIZE/2) ) { |
| 1404 |
|
| 1405 |
/* sanity check */ |
| 1406 |
VJMPERR1( len <= thee->ioBufferLen ); |
| 1407 |
|
| 1408 |
/* copy unread part of ioBuffer into temp buf and clear ioBuffer */ |
| 1409 |
for (i=len; i<thee->ioBufferLen; i++) |
| 1410 |
buf[i-len] = thee->ioBuffer[i]; |
| 1411 |
memset(thee->ioBuffer, '\0', sizeof(thee->ioBuffer)); |
| 1412 |
|
| 1413 |
/* read temp buffer back, reseting to the beginning of ioBuffer */ |
| 1414 |
thee->ioBufferLen = thee->ioBufferLen - len; |
| 1415 |
for (i=0; i<thee->ioBufferLen; i++) |
| 1416 |
thee->ioBuffer[i] = buf[i]; |
| 1417 |
|
| 1418 |
/* reset the buffer point to 0 */ |
| 1419 |
if (thee->frmt == VIO_ASC) { |
| 1420 |
VJMPERR1( asc_setpos((ASC*)thee->axdr, 0) ); |
| 1421 |
} else if (thee->frmt == VIO_XDR) { |
| 1422 |
VJMPERR1( xdr_setpos((XDR*)thee->axdr, 0) ); |
| 1423 |
} else { VASSERT( 0 ); } |
| 1424 |
|
| 1425 |
/* finally, read in the new data, starting at end of current data */ |
| 1426 |
thee->ioBufferLen += Vio_read(thee, |
| 1427 |
thee->ioBuffer+thee->ioBufferLen, VMAX_BUFSIZE-thee->ioBufferLen ); |
| 1428 |
|
| 1429 |
/* we (hopefully?) have enough in buffer to work with; do nothing here */ |
| 1430 |
} else { |
| 1431 |
/* no-op */ |
| 1432 |
} |
| 1433 |
|
| 1434 |
/* we ALWAYS have to pick the format specifier apart <ASC,XDR> ... */ |
| 1435 |
tokCount = 0; |
| 1436 |
len = strlen(parms); |
| 1437 |
va_start(ap, parms); |
| 1438 |
i = 0; |
| 1439 |
while ( i < len ) { |
| 1440 |
arg0 = parms[i]; |
| 1441 |
if ( arg0 == ' ' ) { |
| 1442 |
i+=1; |
| 1443 |
} else if ( arg0 == '\n' ) { |
| 1444 |
i+=1; |
| 1445 |
} else if ( i+1 < len ) { |
| 1446 |
arg1 = parms[i+1]; |
| 1447 |
if ( arg1 == 's' ) { |
| 1448 |
sval = va_arg(ap, char*); |
| 1449 |
if ((i == len-3) && ( parms[len-1] == '\n' )) { |
| 1450 |
if (thee->frmt == VIO_ASC) { |
| 1451 |
VASSERT( 0 ); /* is this ever executed??? */ |
| 1452 |
} else if (thee->frmt == VIO_XDR) { |
| 1453 |
VASSERT( 0 ); /* is this ever executed??? */ |
| 1454 |
} else { VASSERT( 0 ); } |
| 1455 |
} else { |
| 1456 |
if (thee->frmt == VIO_ASC) { |
| 1457 |
VJMPERR1( asc_string(thee->axdr, &sval, VMAX_BUFSIZE) ); |
| 1458 |
} else if (thee->frmt == VIO_XDR) { |
| 1459 |
VJMPERR1( xdr_string(thee->axdr, &sval, VMAX_BUFSIZE) ); |
| 1460 |
} else { VASSERT( 0 ); } |
| 1461 |
} |
| 1462 |
tokCount++; |
| 1463 |
i+=2; |
| 1464 |
} else if ( arg1 == 'c' ) { |
| 1465 |
cval = va_arg(ap, char*); |
| 1466 |
if (thee->frmt == VIO_ASC) { |
| 1467 |
VJMPERR1( asc_char( thee->axdr, cval ) ); |
| 1468 |
} else if (thee->frmt == VIO_XDR) { |
| 1469 |
VJMPERR1( xdr_char( thee->axdr, cval ) ); |
| 1470 |
} else { VASSERT( 0 ); } |
| 1471 |
tokCount++; |
| 1472 |
i+=2; |
| 1473 |
} else if ( arg1 == 'd' ) { |
| 1474 |
ival = va_arg(ap, int*); |
| 1475 |
if (thee->frmt == VIO_ASC) { |
| 1476 |
VJMPERR1( asc_int( thee->axdr, ival ) ); |
| 1477 |
} else if (thee->frmt == VIO_XDR) { |
| 1478 |
VJMPERR1( xdr_int( thee->axdr, ival ) ); |
| 1479 |
} else { VASSERT( 0 ); } |
| 1480 |
tokCount++; |
| 1481 |
i+=2; |
| 1482 |
} else if ( arg1 == 'f' ) { |
| 1483 |
fval = va_arg(ap, float*); |
| 1484 |
if (thee->frmt == VIO_ASC) { |
| 1485 |
VJMPERR1( asc_float( thee->axdr, fval ) ); |
| 1486 |
} else if (thee->frmt == VIO_XDR) { |
| 1487 |
VJMPERR1( xdr_float( thee->axdr, fval ) ); |
| 1488 |
} else { VASSERT( 0 ); } |
| 1489 |
tokCount++; |
| 1490 |
i+=2; |
| 1491 |
} else if ( arg1 == 'e' ) { |
| 1492 |
fval = va_arg(ap, float*); |
| 1493 |
if (thee->frmt == VIO_ASC) { |
| 1494 |
VJMPERR1( asc_float( thee->axdr, fval ) ); |
| 1495 |
} else if (thee->frmt == VIO_XDR) { |
| 1496 |
VJMPERR1( xdr_float( thee->axdr, fval ) ); |
| 1497 |
} else { VASSERT( 0 ); } |
| 1498 |
tokCount++; |
| 1499 |
i+=2; |
| 1500 |
} else if (( arg1 == 'l' ) && ( i+2 < len )) { |
| 1501 |
arg2 = parms[i+2]; |
| 1502 |
if ( arg2 == 'e' ) { |
| 1503 |
dval = va_arg(ap, double*); |
| 1504 |
if (thee->frmt == VIO_ASC) { |
| 1505 |
VJMPERR1( asc_double( thee->axdr, dval ) ); |
| 1506 |
} else if (thee->frmt == VIO_XDR) { |
| 1507 |
VJMPERR1( xdr_double( thee->axdr, dval ) ); |
| 1508 |
} else { VASSERT( 0 ); } |
| 1509 |
tokCount++; |
| 1510 |
i+=3; |
| 1511 |
} else { VJMPERR1( 0 ); } |
| 1512 |
} else { VJMPERR1( 0 ); } |
| 1513 |
} else { VJMPERR1( 0 ); } |
| 1514 |
} |
| 1515 |
va_end(ap); |
| 1516 |
|
| 1517 |
/* return without error */ |
| 1518 |
return tokCount; |
| 1519 |
|
| 1520 |
VERROR1: |
| 1521 |
va_end(ap); |
| 1522 |
/* |
| 1523 |
* APBS-KTS: Check to see if we are here because of an EOF condition. If |
| 1524 |
* so, then just quietly go away. |
| 1525 |
*/ |
| 1526 |
if (thee->frmt == VIO_ASC && ((ASC *)thee->axdr)->error != EOF) |
| 1527 |
fprintf(stderr,"Vio_scanf: Format problem with input.\n"); |
| 1528 |
VERROR2: |
| 1529 |
thee->error = 1; |
| 1530 |
return 0; |
| 1531 |
} |
| 1532 |
|
| 1533 |
/* |
| 1534 |
* *************************************************************************** |
| 1535 |
* Routine: Vio_printf |
| 1536 |
* |
| 1537 |
* Purpose: Mimic "printf" to an arbitrary Vio device. |
| 1538 |
* |
| 1539 |
* Author: Michael Holst |
| 1540 |
* *************************************************************************** |
| 1541 |
*/ |
| 1542 |
VPUBLIC int Vio_printf(Vio *thee, char *parms, ... ) |
| 1543 |
{ |
| 1544 |
va_list ap; |
| 1545 |
char buf[VMAX_BUFSIZE]; |
| 1546 |
int len; |
| 1547 |
|
| 1548 |
char arg0, arg1, arg2, cval, *sval; |
| 1549 |
int i, tokCount, ival; |
| 1550 |
float fval; |
| 1551 |
double dval; |
| 1552 |
|
| 1553 |
VJMPERR2( !thee->error ); |
| 1554 |
VJMPERR2( thee->rwkey == VIO_W ); |
| 1555 |
|
| 1556 |
/* if ASCII data then use vsprintf to handle format specifier exactly */ |
| 1557 |
if (thee->frmt == VIO_ASC) { |
| 1558 |
va_start(ap, parms); |
| 1559 |
vsprintf(buf, parms, ap); |
| 1560 |
va_end(ap); |
| 1561 |
len = strlen(buf); |
| 1562 |
return Vio_writePutBuffer(thee,buf,len); |
| 1563 |
} |
| 1564 |
|
| 1565 |
/* if XDR data then we have to pick the format specifier apart... */ |
| 1566 |
len = strlen(parms); |
| 1567 |
va_start(ap, parms); |
| 1568 |
tokCount = 0; |
| 1569 |
i = 0; |
| 1570 |
while ( i < len ) { |
| 1571 |
arg0 = parms[i]; |
| 1572 |
if ((arg0 == '%') && (i+1 < len)) { |
| 1573 |
arg1 = parms[i+1]; |
| 1574 |
if ( arg1 == '%' ) { |
| 1575 |
i+=2; |
| 1576 |
} else { |
| 1577 |
while (!strchr("scdfel",arg1)) { |
| 1578 |
i+=1; |
| 1579 |
arg1 = parms[i+1]; |
| 1580 |
VJMPERR1( i+1 < len ); |
| 1581 |
} |
| 1582 |
if ( arg1 == 's' ) { |
| 1583 |
sval = va_arg(ap, char*); |
| 1584 |
/* don't put comment strings into xdr files */ |
| 1585 |
if (!strchr(thee->commChars,sval[0])) { |
| 1586 |
VJMPERR1( xdr_string(thee->axdr, &sval, strlen(sval)) ); |
| 1587 |
tokCount++; |
| 1588 |
} |
| 1589 |
i+=2; |
| 1590 |
} else if ( arg1 == 'c' ) { |
| 1591 |
/* are char args always passed as int? ... */ |
| 1592 |
cval = (char)va_arg(ap, int); /* CAST FROM INT */ |
| 1593 |
VJMPERR1( xdr_char( thee->axdr, &cval ) ); |
| 1594 |
tokCount++; |
| 1595 |
i+=2; |
| 1596 |
} else if ( arg1 == 'd' ) { |
| 1597 |
ival = va_arg(ap, int); |
| 1598 |
VJMPERR1( xdr_int( thee->axdr, &ival ) ); |
| 1599 |
tokCount++; |
| 1600 |
i+=2; |
| 1601 |
} else if ( arg1 == 'f' ) { |
| 1602 |
/* are float args always passed as double? ... */ |
| 1603 |
fval = (float)va_arg(ap, double); /* CAST FROM DOUBLE */ |
| 1604 |
VJMPERR1( xdr_float( thee->axdr, &fval ) ); |
| 1605 |
tokCount++; |
| 1606 |
i+=2; |
| 1607 |
} else if ( arg1 == 'e' ) { |
| 1608 |
/* are float args always passed as double? ... */ |
| 1609 |
fval = (float)va_arg(ap, double); /* CAST FROM DOUBLE */ |
| 1610 |
VJMPERR1( xdr_float( thee->axdr, &fval ) ); |
| 1611 |
tokCount++; |
| 1612 |
i+=2; |
| 1613 |
} else if (( arg1 == 'l' ) && ( i+2 < len )) { |
| 1614 |
arg2 = parms[i+2]; |
| 1615 |
if ( arg2 == 'e' ) { |
| 1616 |
dval = va_arg(ap, double); |
| 1617 |
VJMPERR1( xdr_double( thee->axdr, &dval ) ); |
| 1618 |
tokCount++; |
| 1619 |
i+=3; |
| 1620 |
} else { VJMPERR1( 0 ); } |
| 1621 |
} else { VJMPERR1( 0 ); } |
| 1622 |
} |
| 1623 |
} else { |
| 1624 |
i+=1; |
| 1625 |
} |
| 1626 |
} |
| 1627 |
va_end(ap); |
| 1628 |
|
| 1629 |
/* finally write out the XDR buffer */ |
| 1630 |
VJMPERR1( 0<=(len=xdr_getpos((XDR*)thee->axdr)) ); |
| 1631 |
VJMPERR1( Vio_writePutBuffer(thee,thee->ioBuffer,len) == len ); |
| 1632 |
VJMPERR1( xdr_setpos((XDR*)thee->axdr, 0) ); |
| 1633 |
|
| 1634 |
/* return without error */ |
| 1635 |
return len; |
| 1636 |
|
| 1637 |
VERROR1: |
| 1638 |
va_end(ap); |
| 1639 |
fprintf(stderr,"Vio_printf: Format problem with output.\n"); |
| 1640 |
|
| 1641 |
VERROR2: |
| 1642 |
thee->error = 1; |
| 1643 |
return 0; |
| 1644 |
} |
| 1645 |
|
| 1646 |
/* |
| 1647 |
* *************************************************************************** |
| 1648 |
* Routine: Vio_read |
| 1649 |
* |
| 1650 |
* Purpose: Read (up to) bufsize characters into buf from input device. |
| 1651 |
* |
| 1652 |
* Notes: The number of bytes read is returned. |
| 1653 |
* |
| 1654 |
* It is not necessarily an error if the number of bytes read |
| 1655 |
* is less than bufsize (EOF may have been encountered). |
| 1656 |
* |
| 1657 |
* Acts exactly like fread() or read(). |
| 1658 |
* |
| 1659 |
* Author: Michael Holst |
| 1660 |
* *************************************************************************** |
| 1661 |
*/ |
| 1662 |
VPUBLIC int Vio_read(Vio *thee, char *buf, int bufsize) |
| 1663 |
{ |
| 1664 |
int rc, i, ilen; |
| 1665 |
|
| 1666 |
VJMPERR2( !thee->error ); |
| 1667 |
VJMPERR2( thee->rwkey == VIO_R ); |
| 1668 |
|
| 1669 |
rc = 0; |
| 1670 |
if (bufsize > 0) { |
| 1671 |
if ( (thee->type==VIO_SDIO) |
| 1672 |
|| (thee->type==VIO_FILE) ) { |
| 1673 |
rc = fread(buf, sizeof(char), (unsigned int)bufsize, thee->fp); |
| 1674 |
/* CMIKE: if (rc!=bufsize), make SURE EOF was reached! */ |
| 1675 |
} else if (thee->type==VIO_BUFF) { |
| 1676 |
ilen = VMIN2( bufsize, thee->VIObufferLen - thee->VIObufferPtr ); |
| 1677 |
for (i=0; i<ilen; i++) |
| 1678 |
buf[i] = thee->VIObuffer[thee->VIObufferPtr + i]; |
| 1679 |
thee->VIObufferPtr += ilen; |
| 1680 |
rc = ilen; |
| 1681 |
} else if ( (thee->type==VIO_UNIX) |
| 1682 |
|| (thee->type==VIO_INET) ) { |
| 1683 |
rc = readn(thee->soc, buf, (unsigned int)bufsize); |
| 1684 |
/* CMIKE: if (rc!=bufsize), make SURE EOF was reached! */ |
| 1685 |
} else { |
| 1686 |
fprintf(stderr,"Vio_read: Bad type found <%d>\n", thee->type); |
| 1687 |
rc = 0; |
| 1688 |
VJMPERR2( 0 ); |
| 1689 |
} |
| 1690 |
} |
| 1691 |
|
| 1692 |
/* return without error */ |
| 1693 |
return rc; |
| 1694 |
|
| 1695 |
VERROR2: |
| 1696 |
thee->error = 1; |
| 1697 |
return 0; |
| 1698 |
} |
| 1699 |
|
| 1700 |
/* |
| 1701 |
* *************************************************************************** |
| 1702 |
* Routine: Vio_write |
| 1703 |
* |
| 1704 |
* Purpose: Write bufsize characters from buf to output device. |
| 1705 |
* |
| 1706 |
* Notes: The number of bytes written is returned. |
| 1707 |
* |
| 1708 |
* On success, the returned bytecount is the same as the number |
| 1709 |
* of bytes in the input buffer. |
| 1710 |
* |
| 1711 |
* On failure, the returned bytecount is less than the number |
| 1712 |
* of bytes in the input buffer. |
| 1713 |
* |
| 1714 |
* Acts exactly like fwrite() or write(). |
| 1715 |
* |
| 1716 |
* Author: Michael Holst |
| 1717 |
* *************************************************************************** |
| 1718 |
*/ |
| 1719 |
VPUBLIC int Vio_write(Vio *thee, char *buf, int bufsize) |
| 1720 |
{ |
| 1721 |
int rc, i, isize; |
| 1722 |
char *tmpBuf; |
| 1723 |
|
| 1724 |
VJMPERR2( !thee->error ); |
| 1725 |
VJMPERR2( thee->rwkey == VIO_W ); |
| 1726 |
|
| 1727 |
rc = 0; |
| 1728 |
if (bufsize > 0) { |
| 1729 |
if ( (thee->type==VIO_SDIO) |
| 1730 |
|| (thee->type==VIO_FILE) ) { |
| 1731 |
rc = fwrite(buf, sizeof(char), (unsigned int)bufsize, thee->fp); |
| 1732 |
VJMPERR1( rc == bufsize ); |
| 1733 |
} else if (thee->type==VIO_BUFF) { |
| 1734 |
while ( bufsize > (thee->VIObufferLen - thee->VIObufferPtr) ) { |
| 1735 |
isize = VMAX2( 1, 2*(thee->VIObufferLen) ); |
| 1736 |
tmpBuf = (char*)calloc( isize, sizeof(char) ); |
| 1737 |
VJMPERR1( tmpBuf != VNULL ); |
| 1738 |
for (i=0; i<thee->VIObufferLen; i++) |
| 1739 |
tmpBuf[i] = thee->VIObuffer[i]; |
| 1740 |
free( thee->VIObuffer ); |
| 1741 |
thee->VIObuffer = tmpBuf; |
| 1742 |
thee->VIObufferLen = isize; |
| 1743 |
} |
| 1744 |
for (i=0; i<bufsize; i++) |
| 1745 |
thee->VIObuffer[thee->VIObufferPtr + i] = buf[i]; |
| 1746 |
thee->VIObufferPtr += bufsize; |
| 1747 |
rc = bufsize; |
| 1748 |
VJMPERR1( rc == bufsize ); |
| 1749 |
} else if ( (thee->type==VIO_UNIX) |
| 1750 |
|| (thee->type==VIO_INET) ) { |
| 1751 |
rc = writen(thee->so, buf, (unsigned int)bufsize); |
| 1752 |
VJMPERR1( rc == bufsize ); |
| 1753 |
} else { |
| 1754 |
fprintf(stderr,"Vio_write: Bad type found <%d>\n", thee->type); |
| 1755 |
rc = 0; |
| 1756 |
VJMPERR2( 0 ); |
| 1757 |
} |
| 1758 |
} |
| 1759 |
|
| 1760 |
/* return without error */ |
| 1761 |
return rc; |
| 1762 |
|
| 1763 |
VERROR1: |
| 1764 |
fprintf(stderr,"Vio_write: Error occurred (bailing out).\n"); |
| 1765 |
VERROR2: |
| 1766 |
thee->error = 1; |
| 1767 |
return 0; |
| 1768 |
} |
| 1769 |
|
| 1770 |
/* |
| 1771 |
* *************************************************************************** |
| 1772 |
* Routine: Vio_initIoPutBuffers |
| 1773 |
* |
| 1774 |
* Purpose: Initialize the internal buffer. |
| 1775 |
* |
| 1776 |
* Author: Michael Holst |
| 1777 |
* *************************************************************************** |
| 1778 |
*/ |
| 1779 |
VPRIVATE void Vio_initIoPutBuffers(Vio *thee) |
| 1780 |
{ |
| 1781 |
/* initialize the buffer space */ |
| 1782 |
memset(thee->ioBuffer, '\0', sizeof(thee->ioBuffer)); |
| 1783 |
memset(thee->putBuffer, '\0', sizeof(thee->putBuffer)); |
| 1784 |
thee->ioBufferLen = 0; |
| 1785 |
thee->putBufferLen = 0; |
| 1786 |
} |
| 1787 |
|
| 1788 |
/* |
| 1789 |
* *************************************************************************** |
| 1790 |
* Routine: Vio_purgePutBuffer |
| 1791 |
* |
| 1792 |
* Purpose: Purge the internal buffer. |
| 1793 |
* |
| 1794 |
* Author: Michael Holst |
| 1795 |
* *************************************************************************** |
| 1796 |
*/ |
| 1797 |
VPRIVATE void Vio_purgePutBuffer(Vio *thee) |
| 1798 |
{ |
| 1799 |
int len; |
| 1800 |
|
| 1801 |
VJMPERR2( !thee->error ); |
| 1802 |
VJMPERR2( thee->rwkey == VIO_W ); |
| 1803 |
|
| 1804 |
len = thee->putBufferLen; |
| 1805 |
if ( (thee->type==VIO_UNIX) |
| 1806 |
|| (thee->type==VIO_INET) ) { |
| 1807 |
if ( Vio_write(thee,thee->putBuffer,len) != len ) { |
| 1808 |
fprintf(stderr, |
| 1809 |
"Vio_purgePutBuffer: Vio_write fail UNIX/INET sock <%s>" |
| 1810 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1811 |
VJMPERR2( 0 ); |
| 1812 |
} |
| 1813 |
memset(thee->putBuffer, '\0', sizeof(thee->putBuffer)); |
| 1814 |
} else { |
| 1815 |
fprintf(stderr,"Vio_purgePutBuffer: Bad type found <%d>\n",thee->type); |
| 1816 |
VJMPERR2( 0 ); |
| 1817 |
} |
| 1818 |
|
| 1819 |
/* return without error */ |
| 1820 |
return; |
| 1821 |
|
| 1822 |
VERROR2: |
| 1823 |
thee->error = 1; |
| 1824 |
return; |
| 1825 |
} |
| 1826 |
|
| 1827 |
/* |
| 1828 |
* *************************************************************************** |
| 1829 |
* Routine: Vio_writePutBuffer |
| 1830 |
* |
| 1831 |
* Purpose: Write bufsize characters from buf to output device. |
| 1832 |
* |
| 1833 |
* Notes: The number of bytes written is returned. |
| 1834 |
* |
| 1835 |
* On success, the returned bytecount is the same as the number |
| 1836 |
* of bytes in the input buffer. |
| 1837 |
* |
| 1838 |
* On failure, the returned bytecount is less than the number |
| 1839 |
* of bytes in the input buffer. |
| 1840 |
* |
| 1841 |
* Acts exactly like fwrite() or write(). |
| 1842 |
* |
| 1843 |
* Comment: This is simply a buffered version of Vio_write(). |
| 1844 |
* The Vio object maintains the buffer safely internally. |
| 1845 |
* |
| 1846 |
* Author: Michael Holst |
| 1847 |
* *************************************************************************** |
| 1848 |
*/ |
| 1849 |
VPRIVATE int Vio_writePutBuffer(Vio *thee, char *buf, int bufsize) |
| 1850 |
{ |
| 1851 |
int i, curLen; |
| 1852 |
|
| 1853 |
VJMPERR2( !thee->error ); |
| 1854 |
VJMPERR2( thee->rwkey == VIO_W ); |
| 1855 |
|
| 1856 |
/* attempt to buffer the i/o to get some speed */ |
| 1857 |
if ( (thee->type==VIO_SDIO) |
| 1858 |
|| (thee->type==VIO_FILE) |
| 1859 |
|| (thee->type==VIO_BUFF) ) { |
| 1860 |
|
| 1861 |
if ( Vio_write(thee,buf,bufsize) != bufsize ) { |
| 1862 |
fprintf(stderr, |
| 1863 |
"Vio_writePutBuffer: Vio_write(1) fail FILE sock <%s>" |
| 1864 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1865 |
VJMPERR2( 0 ); |
| 1866 |
} |
| 1867 |
|
| 1868 |
} else if ( (thee->type==VIO_UNIX) |
| 1869 |
|| (thee->type==VIO_INET) ) { |
| 1870 |
|
| 1871 |
/* incoming data is larger than our buffer */ |
| 1872 |
if (bufsize > (int)sizeof(thee->putBuffer)) { |
| 1873 |
|
| 1874 |
/* just do a normal unbuffered socket write */ |
| 1875 |
if ( Vio_write(thee,buf,bufsize) != bufsize ) { |
| 1876 |
fprintf(stderr, "Vio_writePutBuffer: Vio_write(2) fail" |
| 1877 |
" UNIX/INET sock <%s>" |
| 1878 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1879 |
VJMPERR2( 0 ); |
| 1880 |
} |
| 1881 |
|
| 1882 |
/* incoming data will fit in our buffer */ |
| 1883 |
} else { |
| 1884 |
|
| 1885 |
curLen = thee->putBufferLen; |
| 1886 |
|
| 1887 |
/* it fits in now -- just cat it to the end of the buffer */ |
| 1888 |
if ( (curLen + bufsize) <= (int)sizeof(thee->putBuffer) ) { |
| 1889 |
for (i=0; i<bufsize; i++) |
| 1890 |
thee->putBuffer[curLen+i] = buf[i]; |
| 1891 |
thee->putBufferLen += bufsize; |
| 1892 |
|
| 1893 |
/* it won't fit until we write out the existing buffer */ |
| 1894 |
} else { |
| 1895 |
if ( Vio_write(thee,thee->putBuffer,curLen) != curLen ) { |
| 1896 |
fprintf(stderr, "Vio_writePutBuffer: Vio_write(3)" |
| 1897 |
" fail UNIX/INET sock <%s>" |
| 1898 |
" dueto <%s>\n", thee->file, VIOstrerrno(errno)); |
| 1899 |
VJMPERR2( 0 ); |
| 1900 |
} |
| 1901 |
thee->putBufferLen = 0; |
| 1902 |
memset(thee->putBuffer, '\0', sizeof(thee->putBuffer)); |
| 1903 |
for (i=0; i<bufsize; i++) |
| 1904 |
thee->putBuffer[i] = buf[i]; |
| 1905 |
thee->putBufferLen += bufsize; |
| 1906 |
} |
| 1907 |
} |
| 1908 |
|
| 1909 |
} else { |
| 1910 |
fprintf(stderr,"Vio_writePutBuffer: Bad type found <%d>\n",thee->type); |
| 1911 |
VJMPERR2( 0 ); |
| 1912 |
} |
| 1913 |
|
| 1914 |
/* return without error */ |
| 1915 |
return bufsize; |
| 1916 |
|
| 1917 |
VERROR2: |
| 1918 |
thee->error = 1; |
| 1919 |
return 0; |
| 1920 |
} |
| 1921 |
|
| 1922 |
/* |
| 1923 |
* *************************************************************************** |
| 1924 |
* Routine: ascmem_create, asc_destroy, asc_getpos, asc_setpos |
| 1925 |
* asc_string, asc_char, asc_int, asc_float, asc_double |
| 1926 |
* asc_setWhiteChars, asc_setCommChars, asc_getToken |
| 1927 |
* |
| 1928 |
* Purpose: An ASC (i.e. ASCII) dual to the XDR routines. |
| 1929 |
* |
| 1930 |
* Notes: These routines basically function idential to the XDR routines, |
| 1931 |
* except that after calling the constructor <ascmem_create>, one |
| 1932 |
* must call two additional routines, <asc_setWhiteChars> and |
| 1933 |
* <asc_setCommChars>, to specify the strings representing the |
| 1934 |
* whitespace in the ASCII stream separating the tokens, and a set |
| 1935 |
* of possible comment characters which generate skips to a newline. |
| 1936 |
* |
| 1937 |
* The only complicated routine is <asc_genToken>, on which |
| 1938 |
* most of the other things rest. |
| 1939 |
* |
| 1940 |
* Both ASC_ENCODE (write) and ASC_DECODE (read) directions work. |
| 1941 |
* In ASC_ENCODE mode, the tokens are separated by a single newline |
| 1942 |
* (for lack of anything more intelligent to do...). |
| 1943 |
* |
| 1944 |
* Author: Michael Holst |
| 1945 |
* *************************************************************************** |
| 1946 |
*/ |
| 1947 |
|
| 1948 |
/* |
| 1949 |
* *************************************************************************** |
| 1950 |
* Routine: ascmem_create |
| 1951 |
* |
| 1952 |
* Purpose: Create the ASC structure. |
| 1953 |
* |
| 1954 |
* Author: Michael Holst |
| 1955 |
* *************************************************************************** |
| 1956 |
*/ |
| 1957 |
VPRIVATE void ascmem_create(ASC *thee, char *buf, int size, ASCmode mode) |
| 1958 |
{ |
| 1959 |
thee->mode = mode; |
| 1960 |
thee->pos = 0; |
| 1961 |
thee->size = size; |
| 1962 |
thee->buf = buf; |
| 1963 |
memset(thee->whiteChars, '\0', VMAX_ARGNUM); |
| 1964 |
memset(thee->commChars, '\0', VMAX_ARGNUM); |
| 1965 |
thee->error = 0; /* APBS-KTS: initialize to "no error" */ |
| 1966 |
} |
| 1967 |
|
| 1968 |
/* |
| 1969 |
* *************************************************************************** |
| 1970 |
* Routine: asc_destroy |
| 1971 |
* |
| 1972 |
* Purpose: Destroy the ASC structure. |
| 1973 |
* |
| 1974 |
* Author: Michael Holst |
| 1975 |
* *************************************************************************** |
| 1976 |
*/ |
| 1977 |
VPRIVATE void asc_destroy(ASC *thee) |
| 1978 |
{ |
| 1979 |
thee->mode = ASC_NO_MODE; |
| 1980 |
thee->pos = 0; |
| 1981 |
thee->size = 0; |
| 1982 |
thee->buf = VNULL; |
| 1983 |
memset(thee->whiteChars, '\0', VMAX_ARGNUM); |
| 1984 |
memset(thee->commChars, '\0', VMAX_ARGNUM); |
| 1985 |
thee->error = 0; /* APBS-KTS: reset to "no error" */ |
| 1986 |
} |
| 1987 |
|
| 1988 |
/* |
| 1989 |
* *************************************************************************** |
| 1990 |
* Routine: asc_getpos |
| 1991 |
* |
| 1992 |
* Purpose: Return the current position in the ASC stream. |
| 1993 |
* |
| 1994 |
* Author: Michael Holst |
| 1995 |
* *************************************************************************** |
| 1996 |
*/ |
| 1997 |
VPRIVATE int asc_getpos(ASC *thee) |
| 1998 |
{ |
| 1999 |
return thee->pos; |
| 2000 |
} |
| 2001 |
|
| 2002 |
/* |
| 2003 |
* *************************************************************************** |
| 2004 |
* Routine: asc_setpos |
| 2005 |
* |
| 2006 |
* Purpose: Set the current position in the ASC stream. |
| 2007 |
* |
| 2008 |
* Author: Michael Holst |
| 2009 |
* *************************************************************************** |
| 2010 |
*/ |
| 2011 |
VPRIVATE int asc_setpos(ASC *thee, int pos) |
| 2012 |
{ |
| 2013 |
thee->pos = pos; |
| 2014 |
return 1; |
| 2015 |
} |
| 2016 |
|
| 2017 |
/* |
| 2018 |
* *************************************************************************** |
| 2019 |
* Routine: asc_string |
| 2020 |
* |
| 2021 |
* Purpose: DECODE or ENCODE a string. |
| 2022 |
* |
| 2023 |
* Author: Michael Holst |
| 2024 |
* *************************************************************************** |
| 2025 |
*/ |
| 2026 |
VPRIVATE int asc_string(ASC *thee, char **sval, int size) |
| 2027 |
{ |
| 2028 |
int i, len; |
| 2029 |
char tok[VMAX_BUFSIZE]; |
| 2030 |
|
| 2031 |
if (thee->mode == ASC_DECODE) { |
| 2032 |
VJMPERR1( VNULL != asc_getToken(thee, tok, VMAX_BUFSIZE) ); |
| 2033 |
sscanf(tok,"%s",(*sval)); |
| 2034 |
//memcpy((*sval), tok, strlen(tok)); |
| 2035 |
} else if (thee->mode == ASC_ENCODE) { |
| 2036 |
sprintf(tok,"%s\n",*sval); |
| 2037 |
len = strlen(tok); |
| 2038 |
for (i=0; i<len; i++) |
| 2039 |
thee->buf[thee->pos+i] = tok[i]; |
| 2040 |
thee->pos += len; |
| 2041 |
} |
| 2042 |
return 1; |
| 2043 |
|
| 2044 |
VERROR1: |
| 2045 |
return 0; |
| 2046 |
} |
| 2047 |
|
| 2048 |
/* |
| 2049 |
* *************************************************************************** |
| 2050 |
* Routine: asc_char |
| 2051 |
* |
| 2052 |
* Purpose: DECODE or ENCODE a char. |
| 2053 |
* |
| 2054 |
* Author: Michael Holst |
| 2055 |
* *************************************************************************** |
| 2056 |
*/ |
| 2057 |
VPRIVATE int asc_char(ASC *thee, char *cval) |
| 2058 |
{ |
| 2059 |
int i, len; |
| 2060 |
char tok[VMAX_BUFSIZE]; |
| 2061 |
|
| 2062 |
if (thee->mode == ASC_DECODE) { |
| 2063 |
VJMPERR1( VNULL != asc_getToken(thee, tok, VMAX_BUFSIZE) ); |
| 2064 |
sscanf(tok,"%c",cval); |
| 2065 |
} else if (thee->mode == ASC_ENCODE) { |
| 2066 |
sprintf(tok,"%c\n",*cval); |
| 2067 |
len = strlen(tok); |
| 2068 |
for (i=0; i<len; i++) |
| 2069 |
thee->buf[thee->pos+i] = tok[i]; |
| 2070 |
thee->pos += len; |
| 2071 |
} |
| 2072 |
return 1; |
| 2073 |
|
| 2074 |
VERROR1: |
| 2075 |
return 0; |
| 2076 |
} |
| 2077 |
|
| 2078 |
/* |
| 2079 |
* *************************************************************************** |
| 2080 |
* Routine: asc_int |
| 2081 |
* |
| 2082 |
* Purpose: DECODE or ENCODE an int. |
| 2083 |
* |
| 2084 |
* Author: Michael Holst |
| 2085 |
* *************************************************************************** |
| 2086 |
*/ |
| 2087 |
VPRIVATE int asc_int(ASC *thee, int *ival) |
| 2088 |
{ |
| 2089 |
int i, len; |
| 2090 |
char tok[VMAX_BUFSIZE]; |
| 2091 |
|
| 2092 |
if (thee->mode == ASC_DECODE) { |
| 2093 |
VJMPERR1( VNULL != asc_getToken(thee, tok, VMAX_BUFSIZE) ); |
| 2094 |
sscanf(tok,"%d",ival); |
| 2095 |
} else if (thee->mode == ASC_ENCODE) { |
| 2096 |
sprintf(tok,"%d\n",*ival); |
| 2097 |
len = strlen(tok); |
| 2098 |
for (i=0; i<len; i++) |
| 2099 |
thee->buf[thee->pos+i] = tok[i]; |
| 2100 |
thee->pos += len; |
| 2101 |
} |
| 2102 |
return 1; |
| 2103 |
|
| 2104 |
VERROR1: |
| 2105 |
return 0; |
| 2106 |
} |
| 2107 |
|
| 2108 |
/* |
| 2109 |
* *************************************************************************** |
| 2110 |
* Routine: asc_float |
| 2111 |
* |
| 2112 |
* Purpose: DECODE or ENCODE a float. |
| 2113 |
* |
| 2114 |
* Author: Michael Holst |
| 2115 |
* *************************************************************************** |
| 2116 |
*/ |
| 2117 |
VPRIVATE int asc_float(ASC *thee, float *fval) |
| 2118 |
{ |
| 2119 |
int i, len; |
| 2120 |
char tok[VMAX_BUFSIZE]; |
| 2121 |
|
| 2122 |
if (thee->mode == ASC_DECODE) { |
| 2123 |
VJMPERR1( VNULL != asc_getToken(thee, tok, VMAX_BUFSIZE) ); |
| 2124 |
sscanf(tok,"%e",fval); |
| 2125 |
} else if (thee->mode == ASC_ENCODE) { |
| 2126 |
sprintf(tok,"%e\n",*fval); |
| 2127 |
len = strlen(tok); |
| 2128 |
for (i=0; i<len; i++) |
| 2129 |
thee->buf[thee->pos+i] = tok[i]; |
| 2130 |
thee->pos += len; |
| 2131 |
} |
| 2132 |
return 1; |
| 2133 |
|
| 2134 |
VERROR1: |
| 2135 |
return 0; |
| 2136 |
} |
| 2137 |
|
| 2138 |
/* |
| 2139 |
* *************************************************************************** |
| 2140 |
* Routine: asc_double |
| 2141 |
* |
| 2142 |
* Purpose: DECODE or ENCODE a double. |
| 2143 |
* |
| 2144 |
* Author: Michael Holst |
| 2145 |
* *************************************************************************** |
| 2146 |
*/ |
| 2147 |
VPRIVATE int asc_double(ASC *thee, double *dval) |
| 2148 |
{ |
| 2149 |
int i, len; |
| 2150 |
char tok[VMAX_BUFSIZE]; |
| 2151 |
|
| 2152 |
if (thee->mode == ASC_DECODE) { |
| 2153 |
VJMPERR1( VNULL != asc_getToken(thee, tok, VMAX_BUFSIZE) ); |
| 2154 |
sscanf(tok,"%le",dval); |
| 2155 |
} else if (thee->mode == ASC_ENCODE) { |
| 2156 |
sprintf(tok,"%e\n",*dval); |
| 2157 |
len = strlen(tok); |
| 2158 |
for (i=0; i<len; i++) |
| 2159 |
thee->buf[thee->pos+i] = tok[i]; |
| 2160 |
thee->pos += len; |
| 2161 |
} |
| 2162 |
return 1; |
| 2163 |
|
| 2164 |
VERROR1: |
| 2165 |
return 0; |
| 2166 |
} |
| 2167 |
|
| 2168 |
/* |
| 2169 |
* *************************************************************************** |
| 2170 |
* Routine: asc_setWhiteChars |
| 2171 |
* |
| 2172 |
* Purpose: Define the white character set. |
| 2173 |
* |
| 2174 |
* Author: Michael Holst |
| 2175 |
* *************************************************************************** |
| 2176 |
*/ |
| 2177 |
VPRIVATE void asc_setWhiteChars(ASC *thee, char *whiteChars) |
| 2178 |
{ |
| 2179 |
strncpy(thee->whiteChars, whiteChars, VMAX_ARGNUM); |
| 2180 |
} |
| 2181 |
|
| 2182 |
/* |
| 2183 |
* *************************************************************************** |
| 2184 |
* Routine: asc_setCommChars |
| 2185 |
* |
| 2186 |
* Purpose: Define the comment character set. |
| 2187 |
* |
| 2188 |
* Author: Michael Holst |
| 2189 |
* *************************************************************************** |
| 2190 |
*/ |
| 2191 |
VPRIVATE void asc_setCommChars(ASC *thee, char *commChars) |
| 2192 |
{ |
| 2193 |
strncpy(thee->commChars, commChars, VMAX_ARGNUM); |
| 2194 |
} |
| 2195 |
|
| 2196 |
/* |
| 2197 |
* *************************************************************************** |
| 2198 |
* Routine: asc_getToken |
| 2199 |
* |
| 2200 |
* Purpose: Get the next token from the input stream. |
| 2201 |
* |
| 2202 |
* Author: Michael Holst |
| 2203 |
* *************************************************************************** |
| 2204 |
*/ |
| 2205 |
VPRIVATE char* asc_getToken(ASC *thee, char *tok, int toksize) |
| 2206 |
{ |
| 2207 |
/* |
| 2208 |
* APBS-KTS: In general all of the tests that check if we are less than |
| 2209 |
* thee->size are lame. It should be checking against the amount of data |
| 2210 |
* in the buffer, not the size of the buffer. |
| 2211 |
*/ |
| 2212 |
int i, ii, jj, done; |
| 2213 |
if (thee->mode == ASC_DECODE) { |
| 2214 |
|
| 2215 |
/* first clear the token buffer */ |
| 2216 |
memset(tok, '\0', toksize); |
| 2217 |
|
| 2218 |
/* set "ii" ptr to the first token character */ |
| 2219 |
ii = thee->pos; |
| 2220 |
done = 0; |
| 2221 |
while ( !done ) { |
| 2222 |
|
| 2223 |
/* if whiteChar then just skip that character */ |
| 2224 |
if ( strchr(thee->whiteChars,thee->buf[ii]) ) { |
| 2225 |
ii++; |
| 2226 |
VJMPERR1( ii < thee->size ); |
| 2227 |
|
| 2228 |
/* if commChar then skip to the next newline and keep going */ |
| 2229 |
} else if ( strchr(thee->commChars,thee->buf[ii]) ) { |
| 2230 |
ii++; |
| 2231 |
VJMPERR1( ii < thee->size ); |
| 2232 |
while ( thee->buf[ii] != '\n' ) { |
| 2233 |
ii++; |
| 2234 |
VJMPERR1( ii < thee->size ); |
| 2235 |
} |
| 2236 |
|
| 2237 |
/* this must be the first token character */ |
| 2238 |
} else { |
| 2239 |
done = 1; |
| 2240 |
} |
| 2241 |
} |
| 2242 |
|
| 2243 |
/* set "jj" ptr to the first character (white or comm) after token */ |
| 2244 |
jj = ii+1; |
| 2245 |
done = 0; |
| 2246 |
while ( !done ) { |
| 2247 |
/* |
| 2248 |
* APBS-KTS: The below line is sort of dumb. It has the effect of |
| 2249 |
* requiring that there be a whiteChar or commChar character after |
| 2250 |
* the final token. But only if the file data just happens to |
| 2251 |
* completely fill up thee->buf. This would be unlikely, but if it |
| 2252 |
* did happen, it would be a pain to debug. |
| 2253 |
*/ |
| 2254 |
VJMPERR1( jj < thee->size ); |
| 2255 |
|
| 2256 |
/* if whiteChar then we are done */ |
| 2257 |
if ( strchr(thee->whiteChars,thee->buf[jj]) ) { |
| 2258 |
done = 1; |
| 2259 |
|
| 2260 |
/* if commChar then we are done */ |
| 2261 |
} else if ( strchr(thee->commChars,thee->buf[jj]) ) { |
| 2262 |
done = 1; |
| 2263 |
|
| 2264 |
/* this must be another token character */ |
| 2265 |
} else { |
| 2266 |
jj++; |
| 2267 |
} |
| 2268 |
} |
| 2269 |
|
| 2270 |
/* error control */ |
| 2271 |
VJMPERR1( (jj-ii) <= toksize ); |
| 2272 |
VJMPERR1( jj <= thee->size ); |
| 2273 |
|
| 2274 |
/* copy the characters between ii and jj to the output string */ |
| 2275 |
for (i=ii; i<jj; i++) |
| 2276 |
tok[i-ii] = thee->buf[i]; |
| 2277 |
/* tok[jj] = '\0'; |
| 2278 |
* |
| 2279 |
* APBS-KTS: Strictly speaking the above line is a bug. It's also unnecessary. |
| 2280 |
* It only works, and is redundant, because of the memset on tok above. |
| 2281 |
* For what it's worth, it should be, assuming you didn't have the memset, |
| 2282 |
* |
| 2283 |
* tok[jj-ii] = '\0'; |
| 2284 |
*/ |
| 2285 |
|
| 2286 |
/* update the position pointer */ |
| 2287 |
thee->pos = jj; |
| 2288 |
|
| 2289 |
} else if (thee->mode == ASC_ENCODE) { |
| 2290 |
fprintf(stderr,"asc_getToken: Don't know how to ENCODE yet!\n"); |
| 2291 |
} |
| 2292 |
|
| 2293 |
return tok; |
| 2294 |
|
| 2295 |
VERROR1: |
| 2296 |
/* |
| 2297 |
* APBS-KTS: Getting to the end of the buffer without a token should not be |
| 2298 |
* a big deal. It's an EOF condition. Mark it thus and get on with it. |
| 2299 |
* |
| 2300 |
* fprintf(stderr,"asc_getToken: Error occurred (bailing out).\n"); |
| 2301 |
*/ |
| 2302 |
thee->error = EOF; |
| 2303 |
return VNULL; |
| 2304 |
} |
| 2305 |
|
| 2306 |
/* |
| 2307 |
* *************************************************************************** |
| 2308 |
* Routine: readn |
| 2309 |
* |
| 2310 |
* Purpose: A fixed-up file-descriptor read (for UNIX/INET). |
| 2311 |
* |
| 2312 |
* Notes: Fixes the "short read" problem if the operating system |
| 2313 |
* is interrupted during the read. Calls the usual |
| 2314 |
* file-descriptor read repeatedly until n characters are |
| 2315 |
* actually read in. Returns the number of characters |
| 2316 |
* actually read in. Returns -1 on error. |
| 2317 |
* |
| 2318 |
* Includes my WINSOCK fixes (err, rather hacks). |
| 2319 |
* |
| 2320 |
* Author: Michael Holst (first of two jewels from Rick Stevens' book) |
| 2321 |
* *************************************************************************** |
| 2322 |
*/ |
| 2323 |
VPRIVATE int readn(int fd, void *vptr, unsigned int n) |
| 2324 |
{ |
| 2325 |
char *ptr; |
| 2326 |
unsigned int nleft; |
| 2327 |
int nread; |
| 2328 |
|
| 2329 |
ptr = vptr; |
| 2330 |
nleft = n; |
| 2331 |
while (nleft > 0) { |
| 2332 |
if ((nread = recv(fd,ptr,nleft,0)) < 0) { |
| 2333 |
#if defined(HAVE_WINSOCK_H) |
| 2334 |
if (WSAGetLastError() == WSAEINTR) { |
| 2335 |
nread = 0; |
| 2336 |
} else if (WSAGetLastError() == WSAEWOULDBLOCK) { |
| 2337 |
nread = 0; |
| 2338 |
} else { return(-1); } |
| 2339 |
#else |
| 2340 |
if (errno == EINTR) { |
| 2341 |
nread = 0; |
| 2342 |
} else if (errno == EWOULDBLOCK) { |
| 2343 |
nread = 0; |
| 2344 |
} else { return(-1); } |
| 2345 |
#endif |
| 2346 |
} else if (nread == 0) { |
| 2347 |
break; |
| 2348 |
} |
| 2349 |
nleft -= nread; |
| 2350 |
ptr += nread; |
| 2351 |
} |
| 2352 |
return (n-nleft); |
| 2353 |
} |
| 2354 |
|
| 2355 |
/* |
| 2356 |
* *************************************************************************** |
| 2357 |
* Routine: writen |
| 2358 |
* |
| 2359 |
* Purpose: A fixed-up file-descriptor write (for UNIX/INET). |
| 2360 |
* |
| 2361 |
* Notes: Fixes the "short write" problem if the operating system |
| 2362 |
* has buffer overflow problems. Calls the usual |
| 2363 |
* file-descriptor write repeatedly until the input buffer |
| 2364 |
* actually gets written out. Returns the number of |
| 2365 |
* characters actually written out. Returns -1 on error. |
| 2366 |
* |
| 2367 |
* Author: Michael Holst (second of two jewels from Rick Stevens' book) |
| 2368 |
* *************************************************************************** |
| 2369 |
*/ |
| 2370 |
VPRIVATE int writen(int fd, void *vptr, unsigned int n) |
| 2371 |
{ |
| 2372 |
char *ptr; |
| 2373 |
unsigned int nleft; |
| 2374 |
int nwritten; |
| 2375 |
|
| 2376 |
ptr = vptr; |
| 2377 |
nleft = n; |
| 2378 |
while (nleft > 0) { |
| 2379 |
if ((nwritten = send(fd,ptr,nleft,0)) <= 0) { |
| 2380 |
if (errno == EINTR) { |
| 2381 |
nwritten = 0; |
| 2382 |
} else { |
| 2383 |
return(-1); |
| 2384 |
} |
| 2385 |
} |
| 2386 |
nleft -= nwritten; |
| 2387 |
ptr += nwritten; |
| 2388 |
} |
| 2389 |
return(n); |
| 2390 |
} |
| 2391 |
|
| 2392 |
/* |
| 2393 |
* *************************************************************************** |
| 2394 |
* Routine: Vio_bufTake |
| 2395 |
* |
| 2396 |
* Purpose: Set the pointer to the internal buffer. |
| 2397 |
* |
| 2398 |
* Author: Michael Holst |
| 2399 |
* *************************************************************************** |
| 2400 |
*/ |
| 2401 |
VPUBLIC void Vio_bufTake(Vio *thee, char *buf, int bufsize) |
| 2402 |
{ |
| 2403 |
/* make sure Vio was started */ |
| 2404 |
VJMPERR1( VIOstarted ); |
| 2405 |
|
| 2406 |
/* clear the internal buffer */ |
| 2407 |
if (thee->VIObuffer != VNULL) { |
| 2408 |
free( thee->VIObuffer ); |
| 2409 |
thee->VIObuffer = VNULL; |
| 2410 |
} |
| 2411 |
|
| 2412 |
/* now set the buffer */ |
| 2413 |
thee->VIObuffer = buf; |
| 2414 |
thee->VIObufferLen = bufsize; |
| 2415 |
thee->VIObufferPtr = 0; |
| 2416 |
|
| 2417 |
/* return without error */ |
| 2418 |
return; |
| 2419 |
|
| 2420 |
VERROR1: |
| 2421 |
fprintf(stderr,"Vio_bufTake: Vio library has not been started.\n"); |
| 2422 |
return; |
| 2423 |
} |
| 2424 |
|
| 2425 |
/* |
| 2426 |
* *************************************************************************** |
| 2427 |
* Routine: Vio_bufGive |
| 2428 |
* |
| 2429 |
* Purpose: Return the pointer to the internal buffer. |
| 2430 |
* |
| 2431 |
* Author: Michael Holst |
| 2432 |
* *************************************************************************** |
| 2433 |
*/ |
| 2434 |
VPUBLIC char* Vio_bufGive(Vio *thee) |
| 2435 |
{ |
| 2436 |
char *tmp; |
| 2437 |
|
| 2438 |
/* make sure Vio was started */ |
| 2439 |
VJMPERR1( VIOstarted ); |
| 2440 |
|
| 2441 |
/* grab the pointer */ |
| 2442 |
tmp = thee->VIObuffer; |
| 2443 |
|
| 2444 |
/* reset things for the hand-off */ |
| 2445 |
thee->VIObufferLen = 0; |
| 2446 |
thee->VIObuffer = VNULL; |
| 2447 |
|
| 2448 |
/* return without error */ |
| 2449 |
return tmp; |
| 2450 |
|
| 2451 |
VERROR1: |
| 2452 |
fprintf(stderr,"Vio_bufGive: Vio library has not been started.\n"); |
| 2453 |
return VNULL; |
| 2454 |
} |
| 2455 |
|
| 2456 |
/* |
| 2457 |
* *************************************************************************** |
| 2458 |
* Routine: Vio_bufSize |
| 2459 |
* |
| 2460 |
* Purpose: Return the length of the internal buffer. |
| 2461 |
* |
| 2462 |
* Author: Michael Holst |
| 2463 |
* *************************************************************************** |
| 2464 |
*/ |
| 2465 |
VPUBLIC int Vio_bufSize(Vio *thee) |
| 2466 |
{ |
| 2467 |
/* make sure Vio was started */ |
| 2468 |
VJMPERR1( VIOstarted ); |
| 2469 |
|
| 2470 |
/* return without error */ |
| 2471 |
return thee->VIObufferLen; |
| 2472 |
|
| 2473 |
VERROR1: |
| 2474 |
fprintf(stderr,"Vio_bufSize: Vio library has not been started.\n"); |
| 2475 |
return 0; |
| 2476 |
} |
| 2477 |
|
| 2478 |
/* |
| 2479 |
* *************************************************************************** |
| 2480 |
* Routine: Vio_socketOpen |
| 2481 |
* |
| 2482 |
* Purpose: Socket open for read or write. |
| 2483 |
* |
| 2484 |
* Author: Michael Holst |
| 2485 |
* *************************************************************************** |
| 2486 |
*/ |
| 2487 |
VPUBLIC Vio *Vio_socketOpen(char *key, |
| 2488 |
const char *iodev, const char *iofmt, |
| 2489 |
const char *iohost, const char *iofile) |
| 2490 |
{ |
| 2491 |
static Vio *sock; |
| 2492 |
|
| 2493 |
/* make sure Vio was started */ |
| 2494 |
VJMPERR1( VIOstarted ); |
| 2495 |
|
| 2496 |
/* setup for a read */ |
| 2497 |
if (!strcmp("r",key)) { |
| 2498 |
|
| 2499 |
/* Open device for READ */ |
| 2500 |
if ( VNULL == (sock=Vio_ctor(iodev,iofmt,iohost,iofile,"r")) ) { |
| 2501 |
fprintf(stderr,"Vio_socketOpen: Problem opening(read) <%s>\n", |
| 2502 |
iofile); |
| 2503 |
VJMPERR2( 0 ); |
| 2504 |
} |
| 2505 |
|
| 2506 |
/* START READ (blocking accept) */ |
| 2507 |
if ( 0 > Vio_accept(sock,0) ) { |
| 2508 |
fprintf(stderr,"Vio_socketOpen: Problem accepting(read) <%s>\n", |
| 2509 |
iofile); |
| 2510 |
/* destroy the socket before we return */ |
| 2511 |
Vio_dtor( &sock ); |
| 2512 |
VJMPERR2( 0 ); |
| 2513 |
} |
| 2514 |
|
| 2515 |
/* setup for a write */ |
| 2516 |
} else if (!strcmp("w",key)) { |
| 2517 |
|
| 2518 |
/* Open device for WRITE */ |
| 2519 |
if ( VNULL == (sock=Vio_ctor(iodev,iofmt,iohost,iofile,"w")) ) { |
| 2520 |
fprintf(stderr,"Vio_socketOpen: Problem opening(write) <%s>\n", |
| 2521 |
iofile); |
| 2522 |
VJMPERR2( 0 ); |
| 2523 |
} |
| 2524 |
|
| 2525 |
/* START WRITE (blocking connect) */ |
| 2526 |
if ( 0 > Vio_connect(sock,0) ) { |
| 2527 |
fprintf(stderr,"Vio_socketOpen: Problem connecting(write) <%s>\n", |
| 2528 |
iofile); |
| 2529 |
/* destroy the socket before we return */ |
| 2530 |
Vio_dtor( &sock ); |
| 2531 |
VJMPERR2( 0 ); |
| 2532 |
} |
| 2533 |
|
| 2534 |
} else { |
| 2535 |
fprintf(stderr,"Vio_socketOpen: Internal logic error.\n"); |
| 2536 |
VJMPERR2( 0 ); |
| 2537 |
} |
| 2538 |
|
| 2539 |
/* some i/o */ |
| 2540 |
#if 0 |
| 2541 |
fprintf(stderr,"Vio_socketOpen: iodev =<%s>\n", iodev); |
| 2542 |
fprintf(stderr,"Vio_socketOpen: iofmt =<%s>\n", iofmt); |
| 2543 |
fprintf(stderr,"Vio_socketOpen: iohost=<%s>\n", iohost); |
| 2544 |
fprintf(stderr,"Vio_socketOpen: iofile=<%s>\n", iofile); |
| 2545 |
#endif |
| 2546 |
|
| 2547 |
/* return without error */ |
| 2548 |
return sock; |
| 2549 |
|
| 2550 |
VERROR1: |
| 2551 |
fprintf(stderr,"Vio_socketOpen: Vio library has not been started.\n"); |
| 2552 |
return VNULL; |
| 2553 |
|
| 2554 |
VERROR2: |
| 2555 |
fprintf(stderr,"Vio_socketOpen: bailing out.\n"); |
| 2556 |
return VNULL; |
| 2557 |
} |
| 2558 |
|
| 2559 |
/* |
| 2560 |
* *************************************************************************** |
| 2561 |
* Routine: Vio_socketClose |
| 2562 |
* |
| 2563 |
* Purpose: Socket close from read or write. |
| 2564 |
* |
| 2565 |
* Author: Michael Holst |
| 2566 |
* *************************************************************************** |
| 2567 |
*/ |
| 2568 |
VPUBLIC void Vio_socketClose(Vio **sock) |
| 2569 |
{ |
| 2570 |
/* make sure Vio was started */ |
| 2571 |
VJMPERR1( VIOstarted ); |
| 2572 |
|
| 2573 |
VJMPERR2( VNULL != *sock ); |
| 2574 |
|
| 2575 |
/* FINISH READ (release subsocket if we had one) */ |
| 2576 |
if ((*sock)->rwkey == VIO_R) { |
| 2577 |
Vio_acceptFree(*sock); |
| 2578 |
|
| 2579 |
/* FINISH WRITE */ |
| 2580 |
} else if ((*sock)->rwkey == VIO_W) { |
| 2581 |
Vio_connectFree(*sock); |
| 2582 |
|
| 2583 |
/* Problems... */ |
| 2584 |
} else { |
| 2585 |
VJMPERR2( 0 ); |
| 2586 |
} |
| 2587 |
|
| 2588 |
/* return without error */ |
| 2589 |
Vio_dtor(sock); |
| 2590 |
return; |
| 2591 |
|
| 2592 |
VERROR1: |
| 2593 |
fprintf(stderr,"Vio_socketClose: Vio library has not been started.\n"); |
| 2594 |
return; |
| 2595 |
|
| 2596 |
VERROR2: |
| 2597 |
fprintf(stderr,"Vio_socketClose: bailing out.\n"); |
| 2598 |
return; |
| 2599 |
} |
| 2600 |
|