[590] | 1 | //vio.c: |
---|
| 2 | |
---|
[690] | 3 | /* |
---|
[1284] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008, 2009 |
---|
[690] | 5 | * |
---|
| 6 | * This file is part of libroar a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroar is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | * NOTE for everyone want's to change something and send patches: |
---|
| 24 | * read README and HACKING! There a addition information on |
---|
| 25 | * the license of this document you need to read before you send |
---|
| 26 | * any patches. |
---|
| 27 | * |
---|
| 28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 29 | * or libpulse*: |
---|
| 30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 33 | */ |
---|
| 34 | |
---|
[590] | 35 | #include "libroar.h" |
---|
| 36 | |
---|
[1474] | 37 | #ifdef ROAR_HAVE_IO_POSIX |
---|
| 38 | #define _CAN_OPERATE |
---|
| 39 | #endif |
---|
| 40 | |
---|
[591] | 41 | int roar_vio_init_calls (struct roar_vio_calls * calls) { |
---|
[1474] | 42 | #ifdef _CAN_OPERATE |
---|
[1118] | 43 | if ( calls == NULL ) |
---|
[591] | 44 | return -1; |
---|
| 45 | |
---|
| 46 | memset((void*)calls, 0, sizeof(struct roar_vio_calls)); |
---|
| 47 | |
---|
[881] | 48 | /* |
---|
[597] | 49 | calls->read = (ssize_t (*)(int fd, void *buf, size_t count, void * inst))read; |
---|
| 50 | calls->write = (ssize_t (*)(int fd, void *buf, size_t count, void * inst))write; |
---|
| 51 | calls->lseek = (off_t (*)(int fildes, off_t offset, int whence, void * inst))lseek; |
---|
[881] | 52 | */ |
---|
| 53 | |
---|
[1118] | 54 | calls->read = roar_vio_basic_read; |
---|
| 55 | calls->write = roar_vio_basic_write; |
---|
| 56 | calls->lseek = roar_vio_basic_lseek; |
---|
| 57 | calls->nonblock = roar_vio_basic_nonblock; |
---|
| 58 | calls->sync = roar_vio_basic_sync; |
---|
[1505] | 59 | calls->ctl = roar_vio_basic_ctl; |
---|
[1241] | 60 | calls->close = roar_vio_basic_close; |
---|
[881] | 61 | |
---|
| 62 | return 0; |
---|
[1474] | 63 | #else |
---|
| 64 | return -1; |
---|
| 65 | #endif |
---|
[881] | 66 | } |
---|
| 67 | |
---|
| 68 | int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) { |
---|
| 69 | if ( vio == NULL ) |
---|
| 70 | return -1; |
---|
| 71 | |
---|
| 72 | vio->inst = inst; |
---|
[591] | 73 | |
---|
| 74 | return 0; |
---|
| 75 | } |
---|
| 76 | |
---|
[881] | 77 | int roar_vio_set_fh (struct roar_vio_calls * vio, int fh) { |
---|
[989] | 78 | return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1)); |
---|
[881] | 79 | } |
---|
| 80 | |
---|
| 81 | int roar_vio_get_fh (struct roar_vio_calls * vio) { |
---|
| 82 | if ( vio == NULL ) |
---|
| 83 | return -1; |
---|
| 84 | |
---|
[989] | 85 | return ((int)(ROAR_INSTINT)vio->inst) - 1; |
---|
[881] | 86 | } |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
[1319] | 90 | ROAR_DBG("roar_vio_read(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count); |
---|
| 91 | |
---|
[881] | 92 | if ( vio == NULL ) |
---|
| 93 | return -1; |
---|
| 94 | |
---|
| 95 | if ( vio->read == NULL ) |
---|
| 96 | return -1; |
---|
| 97 | |
---|
| 98 | return vio->read(vio, buf, count); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 102 | if ( vio == NULL ) |
---|
| 103 | return -1; |
---|
| 104 | |
---|
| 105 | if ( vio->write == NULL ) |
---|
| 106 | return -1; |
---|
| 107 | |
---|
| 108 | return vio->write(vio, buf, count); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | off_t roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
| 112 | if ( vio == NULL ) |
---|
| 113 | return -1; |
---|
| 114 | |
---|
| 115 | if ( vio->lseek == NULL ) |
---|
| 116 | return -1; |
---|
| 117 | |
---|
| 118 | return vio->lseek(vio, offset, whence); |
---|
| 119 | } |
---|
| 120 | |
---|
[1118] | 121 | int roar_vio_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 122 | if ( vio == NULL ) |
---|
| 123 | return -1; |
---|
| 124 | |
---|
| 125 | if ( vio->nonblock == NULL ) |
---|
| 126 | return -1; |
---|
| 127 | |
---|
| 128 | return vio->nonblock(vio, state); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | int roar_vio_sync (struct roar_vio_calls * vio) { |
---|
| 132 | if ( vio == NULL ) |
---|
| 133 | return -1; |
---|
| 134 | |
---|
| 135 | if ( vio->sync == NULL ) |
---|
| 136 | return -1; |
---|
| 137 | |
---|
| 138 | return vio->sync(vio); |
---|
| 139 | } |
---|
| 140 | |
---|
[1140] | 141 | int roar_vio_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 142 | if ( vio == NULL ) |
---|
| 143 | return -1; |
---|
| 144 | |
---|
[1615] | 145 | ROAR_DBG("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p): vio->ctl=%p", vio, cmd, data, vio->ctl); |
---|
| 146 | |
---|
[1140] | 147 | if ( vio->ctl == NULL ) |
---|
| 148 | return -1; |
---|
| 149 | |
---|
| 150 | return vio->ctl(vio, cmd, data); |
---|
| 151 | } |
---|
| 152 | |
---|
[1241] | 153 | int roar_vio_close (struct roar_vio_calls * vio) { |
---|
| 154 | if ( vio == NULL ) |
---|
| 155 | return -1; |
---|
| 156 | |
---|
[1258] | 157 | if ( vio->close == NULL ) |
---|
[1241] | 158 | return -1; |
---|
| 159 | |
---|
| 160 | return vio->close(vio); |
---|
| 161 | } |
---|
| 162 | |
---|
[1314] | 163 | int roar_vio_putc (struct roar_vio_calls * vio, char c) { |
---|
| 164 | return roar_vio_write(vio, &c, 1); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | int roar_vio_getc (struct roar_vio_calls * vio) { |
---|
| 168 | unsigned char c; |
---|
| 169 | |
---|
| 170 | if ( roar_vio_read(vio, &c, 1) != 1 ) |
---|
| 171 | return EOF; |
---|
| 172 | |
---|
| 173 | return c; |
---|
| 174 | } |
---|
| 175 | |
---|
[1351] | 176 | int roar_vio_printf(struct roar_vio_calls * vio, const char *format, ...) { |
---|
| 177 | va_list ap; |
---|
| 178 | int ret; |
---|
| 179 | char buf[8192]; |
---|
| 180 | |
---|
| 181 | va_start(ap, format); |
---|
| 182 | ret = vsnprintf(buf, 8192, format, ap); |
---|
| 183 | va_end(ap); |
---|
| 184 | |
---|
| 185 | return roar_vio_write(vio, buf, ret); |
---|
| 186 | } |
---|
| 187 | |
---|
[1252] | 188 | // converters: |
---|
| 189 | int roar_vio_open_file (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) { |
---|
[1474] | 190 | #ifdef _CAN_OPERATE |
---|
[1252] | 191 | int fh; |
---|
| 192 | |
---|
| 193 | if ( calls == NULL || filename == NULL ) |
---|
| 194 | return -1; |
---|
| 195 | |
---|
| 196 | if ( (fh = open(filename, flags, mode)) == -1 ) |
---|
| 197 | return -1; |
---|
| 198 | |
---|
| 199 | if ( roar_vio_open_fh(calls, fh) == -1 ) { |
---|
| 200 | close(fh); |
---|
| 201 | return -1; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | return 0; |
---|
[1474] | 205 | #else |
---|
| 206 | return -1; |
---|
| 207 | #endif |
---|
[1252] | 208 | } |
---|
| 209 | |
---|
| 210 | int roar_vio_open_fh (struct roar_vio_calls * calls, int fh) { |
---|
| 211 | if ( calls == NULL ) |
---|
| 212 | return -1; |
---|
| 213 | |
---|
| 214 | if ( roar_vio_init_calls(calls) == -1 ) |
---|
| 215 | return -1; |
---|
| 216 | |
---|
| 217 | return roar_vio_set_fh(calls, fh); |
---|
| 218 | } |
---|
| 219 | |
---|
[1290] | 220 | int roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) { |
---|
| 221 | if ( calls == NULL ) |
---|
| 222 | return -1; |
---|
| 223 | |
---|
| 224 | return roar_vio_open_fh(calls, fh); |
---|
| 225 | } |
---|
| 226 | |
---|
[1291] | 227 | int roar_vio_open_socket (struct roar_vio_calls * calls, char * host, int port) { |
---|
| 228 | int fh; |
---|
| 229 | |
---|
| 230 | if ( calls == NULL ) |
---|
| 231 | return -1; |
---|
| 232 | |
---|
| 233 | if ( (fh = roar_socket_connect(host, port)) == -1 ) |
---|
| 234 | return -1; |
---|
| 235 | |
---|
| 236 | return roar_vio_open_fh_socket(calls, fh); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | int roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, char * host, int port) { |
---|
| 240 | int fh; |
---|
| 241 | |
---|
| 242 | if ( calls == NULL ) |
---|
| 243 | return -1; |
---|
| 244 | |
---|
| 245 | if ( (fh = roar_socket_listen(type, host, port)) == -1 ) |
---|
| 246 | return -1; |
---|
| 247 | |
---|
| 248 | return roar_vio_open_fh_socket(calls, fh); |
---|
| 249 | } |
---|
| 250 | |
---|
[1275] | 251 | int roar_vio_simple_stream (struct roar_vio_calls * calls, int rate, int channels, int bits, int codec, |
---|
| 252 | char * server, int dir, char * name) { |
---|
| 253 | int fh; |
---|
| 254 | |
---|
| 255 | if ( calls == NULL ) |
---|
| 256 | return -1; |
---|
| 257 | |
---|
| 258 | if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 ) |
---|
| 259 | return -1; |
---|
| 260 | |
---|
[1290] | 261 | return roar_vio_open_fh_socket(calls, fh); |
---|
[1275] | 262 | } |
---|
| 263 | |
---|
| 264 | |
---|
[1252] | 265 | int roar_vio_open_stdio (struct roar_vio_calls * calls, FILE * dst) { |
---|
[1376] | 266 | #ifndef ROAR_WITHOUT_VIO_STDIO |
---|
[1252] | 267 | if ( calls == NULL || dst == NULL ) |
---|
| 268 | return -1; |
---|
| 269 | |
---|
[1253] | 270 | memset(calls, 0, sizeof(struct roar_vio_calls)); |
---|
| 271 | |
---|
| 272 | calls->read = roar_vio_stdio_read; |
---|
| 273 | calls->write = roar_vio_stdio_write; |
---|
| 274 | calls->lseek = roar_vio_stdio_lseek; |
---|
| 275 | calls->sync = roar_vio_stdio_sync; |
---|
| 276 | calls->close = roar_vio_stdio_close; |
---|
| 277 | |
---|
| 278 | calls->inst = dst; |
---|
| 279 | |
---|
| 280 | return 0; |
---|
[1376] | 281 | #else |
---|
| 282 | return -1; |
---|
| 283 | #endif |
---|
[1252] | 284 | } |
---|
| 285 | |
---|
| 286 | FILE * roar_vio_to_stdio (struct roar_vio_calls * calls, int flags) { |
---|
| 287 | #ifdef ROAR_HAVE_FOPENCOOKIE |
---|
| 288 | cookie_io_functions_t foc_funcs; |
---|
| 289 | #endif |
---|
| 290 | |
---|
| 291 | if ( calls == NULL ) |
---|
| 292 | return NULL; |
---|
| 293 | |
---|
| 294 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
| 295 | memset(&foc_funcs, 0, sizeof(cookie_io_functions_t)); |
---|
| 296 | |
---|
[1293] | 297 | foc_funcs.close = roar_vio_to_stdio_close; |
---|
| 298 | foc_funcs.read = roar_vio_to_stdio_read; |
---|
| 299 | foc_funcs.write = roar_vio_to_stdio_write; |
---|
| 300 | |
---|
[1252] | 301 | return fopencookie((void*) calls, "rw", foc_funcs); |
---|
| 302 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
[1293] | 303 | return funopen((void*) calls, roar_vio_to_stdio_read, roar_vio_to_stdio_write, |
---|
| 304 | roar_vio_to_stdio_lseek, roar_vio_to_stdio_close); |
---|
[1252] | 305 | #else |
---|
| 306 | return NULL; |
---|
| 307 | #endif |
---|
| 308 | } |
---|
| 309 | |
---|
[1293] | 310 | #if defined(ROAR_HAVE_FOPENCOOKIE) || defined(ROAR_HAVE_FUNOPEN) |
---|
| 311 | int roar_vio_to_stdio_close (void *__cookie) { |
---|
| 312 | return roar_vio_close((struct roar_vio_calls *) __cookie); |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
| 316 | __ssize_t roar_vio_to_stdio_read (void *__cookie, char *__buf, size_t __nbytes) { |
---|
| 317 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
| 318 | int roar_vio_to_stdio_read(void *__cookie, char *__buf, int __nbytes) { |
---|
| 319 | #endif |
---|
| 320 | return roar_vio_read((struct roar_vio_calls *) __cookie, __buf, __nbytes); |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
| 324 | __ssize_t roar_vio_to_stdio_write (void *__cookie, __const char *__buf, size_t __n) { |
---|
| 325 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
| 326 | int roar_vio_to_stdio_write(void *__cookie, const char *__buf, int __n) { |
---|
| 327 | #endif |
---|
| 328 | return roar_vio_write((struct roar_vio_calls *) __cookie, (char *) __buf, __n); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
| 332 | int roar_vio_to_stdio_lseek (void *__cookie, _IO_off64_t *__pos, int __w); |
---|
| 333 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
| 334 | fpos_t roar_vio_to_stdio_lseek(void *__cookie, fpos_t __pos, int __w) { |
---|
| 335 | return roar_vio_lseek((struct roar_vio_calls *) __cookie, __pos, __w); |
---|
| 336 | } |
---|
| 337 | #endif |
---|
[1376] | 338 | #endif |
---|
[1293] | 339 | |
---|
[886] | 340 | // VIOs: |
---|
[881] | 341 | |
---|
[886] | 342 | // basic |
---|
[881] | 343 | ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
[1474] | 344 | #ifdef _CAN_OPERATE |
---|
[881] | 345 | return read(roar_vio_get_fh(vio), buf, count); |
---|
[1474] | 346 | #else |
---|
| 347 | return -1; |
---|
| 348 | #endif |
---|
[881] | 349 | } |
---|
| 350 | |
---|
| 351 | ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
[1474] | 352 | #ifdef _CAN_OPERATE |
---|
[881] | 353 | return write(roar_vio_get_fh(vio), buf, count); |
---|
[1474] | 354 | #else |
---|
| 355 | return -1; |
---|
| 356 | #endif |
---|
[881] | 357 | } |
---|
| 358 | |
---|
| 359 | off_t roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
[1474] | 360 | #ifdef _CAN_OPERATE |
---|
[881] | 361 | return lseek(roar_vio_get_fh(vio), offset, whence); |
---|
[1474] | 362 | #else |
---|
| 363 | return -1; |
---|
| 364 | #endif |
---|
[881] | 365 | } |
---|
| 366 | |
---|
[1118] | 367 | int roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 368 | if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 ) |
---|
| 369 | return -1; |
---|
| 370 | |
---|
| 371 | if ( state == ROAR_SOCKET_NONBLOCK ) |
---|
| 372 | return 0; |
---|
| 373 | |
---|
[1125] | 374 | roar_vio_sync(vio); |
---|
| 375 | |
---|
| 376 | return 0; |
---|
[1118] | 377 | } |
---|
| 378 | |
---|
| 379 | int roar_vio_basic_sync (struct roar_vio_calls * vio) { |
---|
[1397] | 380 | #ifdef ROAR_FDATASYNC |
---|
[1171] | 381 | return ROAR_FDATASYNC(roar_vio_get_fh(vio)); |
---|
[1397] | 382 | #else |
---|
| 383 | return 0; |
---|
| 384 | #endif |
---|
[1118] | 385 | } |
---|
| 386 | |
---|
[1505] | 387 | int roar_vio_basic_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 388 | |
---|
| 389 | if ( vio == NULL || cmd == -1 ) |
---|
| 390 | return -1; |
---|
| 391 | |
---|
[1615] | 392 | ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data); |
---|
| 393 | |
---|
[1505] | 394 | switch (cmd) { |
---|
| 395 | case ROAR_VIO_CTL_GET_FH: |
---|
| 396 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
| 397 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
[1615] | 398 | ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=ROAR_VIO_CTL_GET_*FH(0x%.8x), data=%p) = 0 // fh=%i", vio, cmd, data, roar_vio_get_fh(vio)); |
---|
[1505] | 399 | *(int*)data = roar_vio_get_fh(vio); |
---|
| 400 | return 0; |
---|
| 401 | break; |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | return -1; |
---|
| 405 | } |
---|
| 406 | |
---|
[1241] | 407 | int roar_vio_basic_close (struct roar_vio_calls * vio) { |
---|
[1474] | 408 | #ifdef _CAN_OPERATE |
---|
[1336] | 409 | if ( roar_vio_get_fh(vio) != -1 ) |
---|
| 410 | return close(roar_vio_get_fh(vio)); |
---|
| 411 | |
---|
| 412 | return 0; |
---|
[1474] | 413 | #else |
---|
| 414 | return -1; |
---|
| 415 | #endif |
---|
[1241] | 416 | } |
---|
| 417 | |
---|
[943] | 418 | // null |
---|
| 419 | ssize_t roar_vio_null_rw (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 420 | if ( vio == NULL || buf == NULL ) |
---|
| 421 | return -1; |
---|
| 422 | |
---|
| 423 | return 0; |
---|
| 424 | } |
---|
| 425 | |
---|
[886] | 426 | // pass |
---|
[1247] | 427 | int roar_vio_open_pass (struct roar_vio_calls * calls, struct roar_vio_calls * dst) { |
---|
| 428 | if ( calls == NULL ) |
---|
| 429 | return -1; |
---|
| 430 | |
---|
| 431 | memset((void*)calls, 0, sizeof(struct roar_vio_calls)); |
---|
| 432 | |
---|
| 433 | calls->read = roar_vio_pass_read; |
---|
| 434 | calls->write = roar_vio_pass_write; |
---|
| 435 | calls->lseek = roar_vio_pass_lseek; |
---|
| 436 | calls->nonblock = roar_vio_pass_nonblock; |
---|
| 437 | calls->sync = roar_vio_pass_sync; |
---|
[1615] | 438 | calls->ctl = roar_vio_pass_ctl; |
---|
[1247] | 439 | calls->close = roar_vio_pass_close; |
---|
| 440 | |
---|
| 441 | calls->inst = dst; |
---|
| 442 | |
---|
| 443 | return 0; |
---|
| 444 | } |
---|
| 445 | |
---|
[886] | 446 | ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 447 | return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count); |
---|
| 448 | } |
---|
| 449 | |
---|
| 450 | ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 451 | return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count); |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | off_t roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
| 455 | return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence); |
---|
| 456 | } |
---|
| 457 | |
---|
[1241] | 458 | int roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 459 | return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state); |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | int roar_vio_pass_sync (struct roar_vio_calls * vio) { |
---|
| 463 | return roar_vio_sync((struct roar_vio_calls *) vio->inst); |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | int roar_vio_pass_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
[1505] | 467 | if (vio == NULL || cmd == -1) |
---|
| 468 | return -1; |
---|
| 469 | |
---|
[1615] | 470 | ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data); |
---|
| 471 | |
---|
[1505] | 472 | switch (cmd) { |
---|
| 473 | case ROAR_VIO_CTL_GET_NEXT: |
---|
| 474 | *(struct roar_vio_calls **)data = vio->inst; |
---|
| 475 | return 0; |
---|
| 476 | break; |
---|
| 477 | case ROAR_VIO_CTL_SET_NEXT: |
---|
| 478 | vio->inst = *(struct roar_vio_calls **)data; |
---|
| 479 | return 0; |
---|
| 480 | break; |
---|
| 481 | } |
---|
| 482 | |
---|
[1241] | 483 | return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data); |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | int roar_vio_pass_close (struct roar_vio_calls * vio) { |
---|
| 487 | return roar_vio_close((struct roar_vio_calls *) vio->inst); |
---|
| 488 | } |
---|
| 489 | |
---|
[886] | 490 | |
---|
| 491 | // re |
---|
[1247] | 492 | int roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) { |
---|
| 493 | if ( roar_vio_open_pass(calls, dst) == -1 ) |
---|
| 494 | return -1; |
---|
| 495 | |
---|
| 496 | calls->read = roar_vio_re_read; |
---|
| 497 | calls->write = roar_vio_re_write; |
---|
| 498 | calls->lseek = roar_vio_re_lseek; |
---|
| 499 | |
---|
| 500 | return 0; |
---|
| 501 | } |
---|
[886] | 502 | ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 503 | size_t len = 0; |
---|
| 504 | ssize_t r = -1; |
---|
| 505 | |
---|
| 506 | if ( vio == NULL ) |
---|
| 507 | return -1; |
---|
| 508 | |
---|
| 509 | if ( vio->inst == NULL ) |
---|
| 510 | return -1; |
---|
| 511 | |
---|
| 512 | errno = 0; |
---|
| 513 | |
---|
| 514 | while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) { |
---|
| 515 | len += r; |
---|
| 516 | buf += r; |
---|
| 517 | count -= r; |
---|
| 518 | if ( count == 0 ) |
---|
| 519 | break; |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | if ( len == 0 && r == -1 ) |
---|
| 523 | return -1; |
---|
| 524 | |
---|
| 525 | return len; |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 529 | size_t len = 0; |
---|
| 530 | ssize_t r = -1; |
---|
| 531 | |
---|
| 532 | if ( vio == NULL ) |
---|
| 533 | return -1; |
---|
| 534 | |
---|
| 535 | if ( vio->inst == NULL ) |
---|
| 536 | return -1; |
---|
| 537 | |
---|
| 538 | errno = 0; |
---|
| 539 | |
---|
| 540 | while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) { |
---|
| 541 | len += r; |
---|
| 542 | buf += r; |
---|
| 543 | count -= r; |
---|
| 544 | if ( count == 0 ) |
---|
| 545 | break; |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | if ( len == 0 && r == -1 ) |
---|
| 549 | return -1; |
---|
| 550 | |
---|
| 551 | return len; |
---|
| 552 | } |
---|
| 553 | |
---|
[1247] | 554 | // TODO: we should do a some more intelgent thing here. |
---|
[886] | 555 | off_t roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
| 556 | return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence); |
---|
| 557 | } |
---|
| 558 | |
---|
[1253] | 559 | // stdio: |
---|
[1376] | 560 | #ifndef ROAR_WITHOUT_VIO_STDIO |
---|
[1253] | 561 | ssize_t roar_vio_stdio_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 562 | return fread(buf, 1, count, (FILE*)(vio->inst)); |
---|
| 563 | } |
---|
| 564 | |
---|
| 565 | ssize_t roar_vio_stdio_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 566 | return fwrite(buf, 1, count, (FILE*)(vio->inst)); |
---|
| 567 | } |
---|
| 568 | |
---|
| 569 | off_t roar_vio_stdio_lseek (struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
| 570 | if ( fseek((FILE*)(vio->inst), offset, whence) == -1 ) |
---|
| 571 | return -1; |
---|
| 572 | |
---|
| 573 | return ftell((FILE*)(vio->inst)); |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | int roar_vio_stdio_sync (struct roar_vio_calls * vio) { |
---|
| 577 | return fflush((FILE*)(vio->inst)); |
---|
| 578 | } |
---|
| 579 | |
---|
[1505] | 580 | int roar_vio_stdio_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 581 | |
---|
| 582 | if ( vio == NULL || cmd == -1 ) |
---|
| 583 | return -1; |
---|
| 584 | |
---|
| 585 | switch (cmd) { |
---|
| 586 | case ROAR_VIO_CTL_GET_FH: |
---|
| 587 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
| 588 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
| 589 | *(int*)data = fileno((FILE*)(vio->inst)); |
---|
| 590 | return 0; |
---|
| 591 | break; |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | return -1; |
---|
| 595 | } |
---|
| 596 | |
---|
[1253] | 597 | int roar_vio_stdio_close (struct roar_vio_calls * vio) { |
---|
| 598 | return fclose((FILE*)(vio->inst)); |
---|
| 599 | } |
---|
[1376] | 600 | #endif |
---|
[886] | 601 | |
---|
[590] | 602 | //ll |
---|