1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
#ifndef _WIN32
// io.c
// includes
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>
#include "io.h"
#include "util.h"
// constants
static const bool UseDebug = FALSE;
static const bool UseCR = FALSE;
static const char LF = '\n';
static const char CR = '\r';
// prototypes
static int my_read (int fd, char string[], int size);
static void my_write (int fd, const char string[], int size);
// functions
// io_is_ok()
bool io_is_ok(const io_t * io) {
if (io == NULL) return FALSE;
if (io->name == NULL) return FALSE;
if (io->in_eof != TRUE && io->in_eof != FALSE) return FALSE;
if (io->in_size < 0 || io->in_size > BufferSize) return FALSE;
if (io->out_size < 0 || io->out_size > BufferSize) return FALSE;
return TRUE;
}
// io_init()
void io_init(io_t * io) {
ASSERT(io!=NULL);
io->in_eof = FALSE;
io->in_size = 0;
io->out_size = 0;
ASSERT(io_is_ok(io));
}
// io_peek()
bool io_peek(io_t * io){
fd_set set[1];
int fd_max;
int ret;
struct timeval tv;
tv.tv_sec=0;
tv.tv_usec=0;
FD_ZERO(set);
FD_SET(io->in_fd,set);
fd_max=io->in_fd;
ret=select(fd_max+1,set,NULL,NULL,&tv);
if(ret>0){
return TRUE;
}else{
return FALSE;
}
}
// io_close()
void io_close(io_t * io) {
ASSERT(io_is_ok(io));
ASSERT(io->out_fd>=0);
my_log("Adapter->%s: EOF\n",io->name);
if (close(io->out_fd) == -1) {
my_fatal("io_close(): close(): %s\n",strerror(errno));
}
io->out_fd = -1;
}
// io_get_update()
void io_get_update(io_t * io) {
int pos, size;
int n;
ASSERT(io_is_ok(io));
ASSERT(io->in_fd>=0);
ASSERT(!io->in_eof);
// init
pos = io->in_size;
size = BufferSize - pos;
if (size <= 0){
// io->in_buffer[FormatBufferSize-20]='\0';
// my_log("%s","io_get_update(): buffer too small; content starts with:\n");
// my_log("[%s...]\n",io->in_buffer);
my_fatal("io_get_update(): buffer overflow\n");
}
// read as many data as possible
n = my_read(io->in_fd,&io->in_buffer[pos],size);
if (UseDebug) my_log("POLYGLOT read %d byte%s from %s\n",n,(n>1)?"s":"",io->name);
if (n > 0) { // at least one character was read
// update buffer size
ASSERT(n>=1&&n<=size);
io->in_size += n;
ASSERT(io->in_size>=0&&io->in_size<=BufferSize);
} else { // EOF
ASSERT(n==0);
io->in_eof = TRUE;
}
}
// io_line_ready()
bool io_line_ready(const io_t * io) {
ASSERT(io_is_ok(io));
if (io->in_eof) return TRUE;
if (memchr(io->in_buffer,LF,io->in_size) != NULL) return TRUE; // buffer contains LF
return FALSE;
}
// io_get_line()
bool io_get_line(io_t * io, char string[], int size) {
int src, dst;
int c;
ASSERT(io_is_ok(io));
ASSERT(string!=NULL);
ASSERT(size>=256);
src = 0;
dst = 0;
while (TRUE) {
// test for end of buffer
if (src >= io->in_size) {
if (io->in_eof) {
my_log("%s->Adapter: EOF\n",io->name);
return FALSE;
} else {
my_fatal("io_get_line(): no EOL in buffer\n");
}
}
// test for end of string
if (dst >= size) my_fatal("io_get_line(): buffer overflow\n");
// copy the next character
c = io->in_buffer[src++];
if (c == LF) { // LF => line complete
string[dst] = '\0';
break;
} else if (c != CR) { // skip CRs
string[dst++] = c;
}
}
// shift the buffer
ASSERT(src>0);
io->in_size -= src;
ASSERT(io->in_size>=0);
if (io->in_size > 0) memmove(&io->in_buffer[0],&io->in_buffer[src],io->in_size);
// return
my_log("%s->Adapter: %s\n",io->name,string);
return TRUE;
}
// io_send()
void io_send(io_t * io, const char format[], ...) {
char string[FormatBufferSize];
int len;
ASSERT(io_is_ok(io));
ASSERT(format!=NULL);
ASSERT(io->out_fd>=0);
// format
CONSTRUCT_ARG_STRING(format,string);
// append string to buffer
len = strlen(string);
if (io->out_size + len > BufferSize-2) my_fatal("io_send(): buffer overflow\n");
memcpy(&io->out_buffer[io->out_size],string,len);
io->out_size += len;
ASSERT(io->out_size>=0&&io->out_size<=BufferSize-2);
// log
io->out_buffer[io->out_size] = '\0';
my_log("Adapter->%s: %s\n",io->name,io->out_buffer);
// append EOL to buffer
if (UseCR) io->out_buffer[io->out_size++] = CR;
io->out_buffer[io->out_size++] = LF;
ASSERT(io->out_size>=0&&io->out_size<=BufferSize);
// flush buffer
if (UseDebug) my_log("POLYGLOT writing %d byte%s to %s\n",io->out_size,(io->out_size>1)?"s":"",io->name);
my_write(io->out_fd,io->out_buffer,io->out_size);
io->out_size = 0;
}
// io_send_queue()
void io_send_queue(io_t * io, const char format[], ...) {
char string[FormatBufferSize];
int len;
ASSERT(io_is_ok(io));
ASSERT(format!=NULL);
ASSERT(io->out_fd>=0);
// format
CONSTRUCT_ARG_STRING(format,string);
// append string to buffer
len = strlen(string);
if (io->out_size + len > BufferSize-2) my_fatal("io_send_queue(): buffer overflow\n");
memcpy(&io->out_buffer[io->out_size],string,len);
io->out_size += len;
ASSERT(io->out_size>=0&&io->out_size<=BufferSize-2);
}
// my_read()
static int my_read(int fd, char string[], int size) {
int n;
ASSERT(fd>=0);
ASSERT(string!=NULL);
ASSERT(size>0);
do {
n = read(fd,string,size);
} while (n == -1 && errno == EINTR);
if (n == -1) my_fatal("my_read(): read(): %s\n",strerror(errno));
ASSERT(n>=0);
return n;
}
// my_write()
static void my_write(int fd, const char string[], int size) {
int n;
ASSERT(fd>=0);
ASSERT(string!=NULL);
ASSERT(size>0);
do {
n = write(fd,string,size);
// if (n == -1 && errno != EINTR && errno != EPIPE) my_fatal("my_write(): write(): %s\n",strerror(errno));
if (n == -1) {
if (FALSE) {
} else if (errno == EINTR) {
n = 0; // nothing has been written
} else if (errno == EPIPE) {
n = size; // pretend everything has been written
} else {
my_fatal("my_write(): write(): %s\n",strerror(errno));
}
}
ASSERT(n>=0);
string += n;
size -= n;
} while (size > 0);
ASSERT(size==0);
}
// end of io.cpp
#endif
|