| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* strm_composite.d - Composite Streams dispatch tables |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* Copyright (c) 2025 Daniel Kochmanski |
| 11 |
* |
| 12 |
* See file 'LICENSE' for the copyright details. |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <ecl/ecl.h> |
| 17 |
#include <ecl/internal.h> |
| 18 |
|
| 19 |
/********************************************************************** |
| 20 |
* TWO WAY STREAM |
| 21 |
*/ |
| 22 |
|
| 23 |
static cl_index |
| 24 |
two_way_read_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 25 |
{ |
| 26 |
if (strm == cl_core.terminal_io) |
| 27 |
ecl_force_output(TWO_WAY_STREAM_OUTPUT(cl_core.terminal_io)); |
| 28 |
return ecl_read_byte8(TWO_WAY_STREAM_INPUT(strm), c, n); |
| 29 |
} |
| 30 |
|
| 31 |
static cl_index |
| 32 |
two_way_write_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 33 |
{ |
| 34 |
return ecl_write_byte8(TWO_WAY_STREAM_OUTPUT(strm), c, n); |
| 35 |
} |
| 36 |
|
| 37 |
static void |
| 38 |
two_way_write_byte(cl_object strm, cl_object byte) |
| 39 |
{ |
| 40 |
ecl_write_byte(byte, TWO_WAY_STREAM_OUTPUT(strm)); |
| 41 |
} |
| 42 |
|
| 43 |
static cl_object |
| 44 |
two_way_read_byte(cl_object stream) |
| 45 |
{ |
| 46 |
return ecl_read_byte(TWO_WAY_STREAM_INPUT(stream)); |
| 47 |
} |
| 48 |
|
| 49 |
static void |
| 50 |
two_way_unread_byte(cl_object strm, cl_object byte) |
| 51 |
{ |
| 52 |
ecl_unread_byte(byte, TWO_WAY_STREAM_INPUT(strm)); |
| 53 |
} |
| 54 |
|
| 55 |
static cl_object |
| 56 |
two_way_peek_byte(cl_object strm) |
| 57 |
{ |
| 58 |
return ecl_peek_byte(TWO_WAY_STREAM_INPUT(strm)); |
| 59 |
} |
| 60 |
|
| 61 |
static ecl_character |
| 62 |
two_way_read_char(cl_object strm) |
| 63 |
{ |
| 64 |
return ecl_read_char(TWO_WAY_STREAM_INPUT(strm)); |
| 65 |
} |
| 66 |
|
| 67 |
static ecl_character |
| 68 |
two_way_write_char(cl_object strm, ecl_character c) |
| 69 |
{ |
| 70 |
return ecl_write_char(c, TWO_WAY_STREAM_OUTPUT(strm)); |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
two_way_unread_char(cl_object strm, ecl_character c) |
| 75 |
{ |
| 76 |
ecl_unread_char(c, TWO_WAY_STREAM_INPUT(strm)); |
| 77 |
} |
| 78 |
|
| 79 |
static ecl_character |
| 80 |
two_way_peek_char(cl_object strm) |
| 81 |
{ |
| 82 |
return ecl_peek_char(TWO_WAY_STREAM_INPUT(strm)); |
| 83 |
} |
| 84 |
|
| 85 |
static cl_index |
| 86 |
two_way_read_vector(cl_object strm, cl_object data, cl_index start, cl_index n) |
| 87 |
{ |
| 88 |
strm = TWO_WAY_STREAM_INPUT(strm); |
| 89 |
return ecl_stream_dispatch_table(strm)->read_vector(strm, data, start, n); |
| 90 |
} |
| 91 |
|
| 92 |
static cl_index |
| 93 |
two_way_write_vector(cl_object strm, cl_object data, cl_index start, cl_index n) |
| 94 |
{ |
| 95 |
strm = TWO_WAY_STREAM_OUTPUT(strm); |
| 96 |
return ecl_stream_dispatch_table(strm)->write_vector(strm, data, start, n); |
| 97 |
} |
| 98 |
|
| 99 |
static int |
| 100 |
two_way_listen(cl_object strm) |
| 101 |
{ |
| 102 |
return ecl_listen_stream(TWO_WAY_STREAM_INPUT(strm)); |
| 103 |
} |
| 104 |
|
| 105 |
static void |
| 106 |
two_way_clear_input(cl_object strm) |
| 107 |
{ |
| 108 |
ecl_clear_input(TWO_WAY_STREAM_INPUT(strm)); |
| 109 |
} |
| 110 |
|
| 111 |
static void |
| 112 |
two_way_clear_output(cl_object strm) |
| 113 |
{ |
| 114 |
ecl_clear_output(TWO_WAY_STREAM_OUTPUT(strm)); |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
two_way_force_output(cl_object strm) |
| 119 |
{ |
| 120 |
ecl_force_output(TWO_WAY_STREAM_OUTPUT(strm)); |
| 121 |
} |
| 122 |
|
| 123 |
static void |
| 124 |
two_way_finish_output(cl_object strm) |
| 125 |
{ |
| 126 |
ecl_finish_output(TWO_WAY_STREAM_OUTPUT(strm)); |
| 127 |
} |
| 128 |
|
| 129 |
static int |
| 130 |
two_way_interactive_p(cl_object strm) |
| 131 |
{ |
| 132 |
return ecl_interactive_stream_p(TWO_WAY_STREAM_INPUT(strm)); |
| 133 |
} |
| 134 |
|
| 135 |
static cl_object |
| 136 |
two_way_element_type(cl_object strm) |
| 137 |
{ |
| 138 |
return ecl_stream_element_type(TWO_WAY_STREAM_INPUT(strm)); |
| 139 |
} |
| 140 |
|
| 141 |
static int |
| 142 |
two_way_column(cl_object strm) |
| 143 |
{ |
| 144 |
return ecl_file_column(TWO_WAY_STREAM_OUTPUT(strm)); |
| 145 |
} |
| 146 |
|
| 147 |
static cl_object |
| 148 |
two_way_close(cl_object strm) |
| 149 |
{ |
| 150 |
if (strm->stream.flags & ECL_STREAM_CLOSE_COMPONENTS) { |
| 151 |
cl_close(1, TWO_WAY_STREAM_INPUT(strm)); |
| 152 |
cl_close(1, TWO_WAY_STREAM_OUTPUT(strm)); |
| 153 |
} |
| 154 |
return ecl_generic_close(strm); |
| 155 |
} |
| 156 |
|
| 157 |
const struct ecl_file_ops two_way_ops = { |
| 158 |
two_way_read_byte8, |
| 159 |
two_way_write_byte8, |
| 160 |
|
| 161 |
two_way_read_byte, |
| 162 |
two_way_write_byte, |
| 163 |
two_way_unread_byte, |
| 164 |
two_way_peek_byte, |
| 165 |
|
| 166 |
two_way_read_char, |
| 167 |
two_way_write_char, |
| 168 |
two_way_unread_char, |
| 169 |
two_way_peek_char, |
| 170 |
|
| 171 |
two_way_read_vector, |
| 172 |
two_way_write_vector, |
| 173 |
|
| 174 |
two_way_listen, |
| 175 |
two_way_clear_input, |
| 176 |
two_way_clear_output, |
| 177 |
two_way_finish_output, |
| 178 |
two_way_force_output, |
| 179 |
|
| 180 |
ecl_generic_always_true, /* input_p */ |
| 181 |
ecl_generic_always_true, /* output_p */ |
| 182 |
two_way_interactive_p, |
| 183 |
two_way_element_type, |
| 184 |
|
| 185 |
ecl_not_a_file_stream, /* length */ |
| 186 |
ecl_generic_always_nil, /* get_position */ |
| 187 |
ecl_generic_set_position, |
| 188 |
ecl_not_file_string_length, |
| 189 |
two_way_column, |
| 190 |
|
| 191 |
ecl_not_a_file_stream, |
| 192 |
ecl_not_a_file_stream, |
| 193 |
|
| 194 |
two_way_close |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
cl_object |
| 199 |
cl_make_two_way_stream(cl_object istrm, cl_object ostrm) |
| 200 |
{ |
| 201 |
cl_object strm; |
| 202 |
if (!ecl_input_stream_p(istrm)) |
| 203 |
ecl_not_an_input_stream(istrm); |
| 204 |
if (!ecl_output_stream_p(ostrm)) |
| 205 |
ecl_not_an_output_stream(ostrm); |
| 206 |
strm = ecl_alloc_stream(); |
| 207 |
strm->stream.format = cl_stream_external_format(istrm); |
| 208 |
strm->stream.mode = (short)ecl_smm_two_way; |
| 209 |
strm->stream.ops = ecl_duplicate_dispatch_table(&two_way_ops); |
| 210 |
TWO_WAY_STREAM_INPUT(strm) = istrm; |
| 211 |
TWO_WAY_STREAM_OUTPUT(strm) = ostrm; |
| 212 |
@(return strm); |
| 213 |
} |
| 214 |
|
| 215 |
cl_object |
| 216 |
cl_two_way_stream_input_stream(cl_object strm) |
| 217 |
{ |
| 218 |
unlikely_if (!ECL_ANSI_STREAM_TYPE_P(strm,ecl_smm_two_way)) |
| 219 |
FEwrong_type_only_arg(@[two-way-stream-input-stream], |
| 220 |
strm, @[two-way-stream]); |
| 221 |
@(return TWO_WAY_STREAM_INPUT(strm)); |
| 222 |
} |
| 223 |
|
| 224 |
cl_object |
| 225 |
cl_two_way_stream_output_stream(cl_object strm) |
| 226 |
{ |
| 227 |
unlikely_if (!ECL_ANSI_STREAM_TYPE_P(strm, ecl_smm_two_way)) |
| 228 |
FEwrong_type_only_arg(@[two-way-stream-output-stream], |
| 229 |
strm, @[two-way-stream]); |
| 230 |
@(return TWO_WAY_STREAM_OUTPUT(strm)); |
| 231 |
} |
| 232 |
|
| 233 |
/********************************************************************** |
| 234 |
* BROADCAST STREAM |
| 235 |
*/ |
| 236 |
|
| 237 |
static cl_index |
| 238 |
broadcast_write_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 239 |
{ |
| 240 |
cl_object l; |
| 241 |
cl_index out = n; |
| 242 |
for (l = BROADCAST_STREAM_LIST(strm); !Null(l); l = ECL_CONS_CDR(l)) { |
| 243 |
out = ecl_write_byte8(ECL_CONS_CAR(l), c, n); |
| 244 |
} |
| 245 |
return out; |
| 246 |
} |
| 247 |
|
| 248 |
static ecl_character |
| 249 |
broadcast_write_char(cl_object strm, ecl_character c) |
| 250 |
{ |
| 251 |
cl_object l; |
| 252 |
for (l = BROADCAST_STREAM_LIST(strm); !Null(l); l = ECL_CONS_CDR(l)) { |
| 253 |
ecl_write_char(c, ECL_CONS_CAR(l)); |
| 254 |
} |
| 255 |
return c; |
| 256 |
} |
| 257 |
|
| 258 |
static void |
| 259 |
broadcast_write_byte(cl_object strm, cl_object byte) |
| 260 |
{ |
| 261 |
cl_object l; |
| 262 |
for (l = BROADCAST_STREAM_LIST(strm); !Null(l); l = ECL_CONS_CDR(l)) { |
| 263 |
ecl_write_byte(byte, ECL_CONS_CAR(l)); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
static void |
| 268 |
broadcast_clear_output(cl_object strm) |
| 269 |
{ |
| 270 |
cl_object l; |
| 271 |
for (l = BROADCAST_STREAM_LIST(strm); !Null(l); l = ECL_CONS_CDR(l)) { |
| 272 |
ecl_clear_output(ECL_CONS_CAR(l)); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
static void |
| 277 |
broadcast_force_output(cl_object strm) |
| 278 |
{ |
| 279 |
cl_object l; |
| 280 |
for (l = BROADCAST_STREAM_LIST(strm); !Null(l); l = ECL_CONS_CDR(l)) { |
| 281 |
ecl_force_output(ECL_CONS_CAR(l)); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
static void |
| 286 |
broadcast_finish_output(cl_object strm) |
| 287 |
{ |
| 288 |
cl_object l; |
| 289 |
for (l = BROADCAST_STREAM_LIST(strm); !Null(l); l = ECL_CONS_CDR(l)) { |
| 290 |
ecl_finish_output(ECL_CONS_CAR(l)); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
static cl_object |
| 295 |
broadcast_element_type(cl_object strm) |
| 296 |
{ |
| 297 |
cl_object l = BROADCAST_STREAM_LIST(strm); |
| 298 |
if (Null(l)) |
| 299 |
return ECL_T; |
| 300 |
return ecl_stream_element_type(ECL_CONS_CAR(l)); |
| 301 |
} |
| 302 |
|
| 303 |
static cl_object |
| 304 |
broadcast_length(cl_object strm) |
| 305 |
{ |
| 306 |
cl_object l = BROADCAST_STREAM_LIST(strm); |
| 307 |
if (Null(l)) |
| 308 |
return ecl_make_fixnum(0); |
| 309 |
return ecl_file_length(ECL_CONS_CAR(ecl_last(l, 1))); |
| 310 |
} |
| 311 |
|
| 312 |
static cl_object |
| 313 |
broadcast_get_position(cl_object strm) |
| 314 |
{ |
| 315 |
cl_object l = BROADCAST_STREAM_LIST(strm); |
| 316 |
if (Null(l)) |
| 317 |
return ecl_make_fixnum(0); |
| 318 |
return ecl_file_position(ECL_CONS_CAR(ecl_last(l, 1))); |
| 319 |
} |
| 320 |
|
| 321 |
static cl_object |
| 322 |
broadcast_set_position(cl_object strm, cl_object pos) |
| 323 |
{ |
| 324 |
cl_object l = BROADCAST_STREAM_LIST(strm); |
| 325 |
if (Null(l)) |
| 326 |
return ECL_NIL; |
| 327 |
return ecl_file_position_set(ECL_CONS_CAR(l), pos); |
| 328 |
} |
| 329 |
|
| 330 |
cl_object |
| 331 |
broadcast_string_length(cl_object strm, cl_object string) |
| 332 |
{ |
| 333 |
cl_object l = BROADCAST_STREAM_LIST(strm); |
| 334 |
if (Null(l)) |
| 335 |
return ecl_make_fixnum(1); |
| 336 |
return ecl_file_string_length(ECL_CONS_CAR(ecl_last(l, 1)), string); |
| 337 |
} |
| 338 |
|
| 339 |
static int |
| 340 |
broadcast_column(cl_object strm) |
| 341 |
{ |
| 342 |
cl_object l = BROADCAST_STREAM_LIST(strm); |
| 343 |
if (Null(l)) |
| 344 |
return 0; |
| 345 |
return ecl_file_column(ECL_CONS_CAR(l)); |
| 346 |
} |
| 347 |
|
| 348 |
static cl_object |
| 349 |
broadcast_close(cl_object strm) |
| 350 |
{ |
| 351 |
if (strm->stream.flags & ECL_STREAM_CLOSE_COMPONENTS) { |
| 352 |
cl_mapc(2, @'close', BROADCAST_STREAM_LIST(strm)); |
| 353 |
} |
| 354 |
return ecl_generic_close(strm); |
| 355 |
} |
| 356 |
|
| 357 |
const struct ecl_file_ops broadcast_ops = { |
| 358 |
ecl_not_input_read_byte8, |
| 359 |
broadcast_write_byte8, |
| 360 |
|
| 361 |
ecl_not_input_read_byte, |
| 362 |
broadcast_write_byte, |
| 363 |
ecl_not_input_unread_byte, |
| 364 |
ecl_generic_peek_byte, |
| 365 |
|
| 366 |
ecl_not_input_read_char, |
| 367 |
broadcast_write_char, |
| 368 |
ecl_not_input_unread_char, |
| 369 |
ecl_generic_peek_char, |
| 370 |
|
| 371 |
ecl_generic_read_vector, |
| 372 |
ecl_generic_write_vector, |
| 373 |
|
| 374 |
ecl_not_input_listen, |
| 375 |
broadcast_force_output, /* clear_input */ /* FIXME! This is legacy behaviour */ |
| 376 |
broadcast_clear_output, |
| 377 |
broadcast_finish_output, |
| 378 |
broadcast_force_output, |
| 379 |
|
| 380 |
ecl_generic_always_false, /* input_p */ |
| 381 |
ecl_generic_always_true, /* output_p */ |
| 382 |
ecl_generic_always_false, |
| 383 |
broadcast_element_type, |
| 384 |
|
| 385 |
broadcast_length, |
| 386 |
broadcast_get_position, |
| 387 |
broadcast_set_position, |
| 388 |
broadcast_string_length, |
| 389 |
broadcast_column, |
| 390 |
|
| 391 |
ecl_not_a_file_stream, |
| 392 |
ecl_not_a_file_stream, |
| 393 |
|
| 394 |
broadcast_close |
| 395 |
}; |
| 396 |
|
| 397 |
@(defun make_broadcast_stream (&rest ap) |
| 398 |
cl_object x, streams; |
| 399 |
int i; |
| 400 |
@ |
| 401 |
streams = ECL_NIL; |
| 402 |
for (i = 0; i < narg; i++) { |
| 403 |
x = ecl_va_arg(ap); |
| 404 |
unlikely_if (!ecl_output_stream_p(x)) |
| 405 |
ecl_not_an_output_stream(x); |
| 406 |
streams = CONS(x, streams); |
| 407 |
} |
| 408 |
x = ecl_alloc_stream(); |
| 409 |
x->stream.format = @':default'; |
| 410 |
x->stream.ops = ecl_duplicate_dispatch_table(&broadcast_ops); |
| 411 |
x->stream.mode = (short)ecl_smm_broadcast; |
| 412 |
BROADCAST_STREAM_LIST(x) = cl_nreverse(streams); |
| 413 |
@(return x); |
| 414 |
@) |
| 415 |
|
| 416 |
cl_object |
| 417 |
cl_broadcast_stream_streams(cl_object strm) |
| 418 |
{ |
| 419 |
unlikely_if (!ECL_ANSI_STREAM_TYPE_P(strm, ecl_smm_broadcast)) |
| 420 |
FEwrong_type_only_arg(@[broadcast-stream-streams], |
| 421 |
strm, @[broadcast-stream]); |
| 422 |
return cl_copy_list(BROADCAST_STREAM_LIST(strm)); |
| 423 |
} |
| 424 |
|
| 425 |
/********************************************************************** |
| 426 |
* ECHO STREAM |
| 427 |
*/ |
| 428 |
|
| 429 |
static cl_index |
| 430 |
echo_read_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 431 |
{ |
| 432 |
cl_index out = ecl_read_byte8(ECHO_STREAM_INPUT(strm), c, n); |
| 433 |
return ecl_write_byte8(ECHO_STREAM_OUTPUT(strm), c, out); |
| 434 |
} |
| 435 |
|
| 436 |
static cl_index |
| 437 |
echo_write_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 438 |
{ |
| 439 |
return ecl_write_byte8(ECHO_STREAM_OUTPUT(strm), c, n); |
| 440 |
} |
| 441 |
|
| 442 |
static cl_object |
| 443 |
echo_read_byte(cl_object strm) |
| 444 |
{ |
| 445 |
cl_object byte = strm->stream.last_byte; |
| 446 |
if (byte != OBJNULL) { |
| 447 |
strm->stream.last_byte = OBJNULL; |
| 448 |
byte = ecl_read_byte(ECHO_STREAM_INPUT(strm)); |
| 449 |
} else { |
| 450 |
byte = ecl_read_byte(ECHO_STREAM_INPUT(strm)); |
| 451 |
if (byte != OBJNULL) |
| 452 |
ecl_write_byte(byte, ECHO_STREAM_OUTPUT(strm)); |
| 453 |
} |
| 454 |
return byte; |
| 455 |
} |
| 456 |
|
| 457 |
static void |
| 458 |
echo_write_byte(cl_object strm, cl_object byte) |
| 459 |
{ |
| 460 |
ecl_write_byte(byte, ECHO_STREAM_OUTPUT(strm)); |
| 461 |
} |
| 462 |
|
| 463 |
static void |
| 464 |
echo_unread_byte(cl_object strm, cl_object byte) |
| 465 |
{ |
| 466 |
unlikely_if (strm->stream.last_byte != OBJNULL) { |
| 467 |
ecl_unread_twice(strm); |
| 468 |
} |
| 469 |
strm->stream.last_byte = ECL_T; |
| 470 |
ecl_unread_byte(byte, ECHO_STREAM_INPUT(strm)); |
| 471 |
} |
| 472 |
|
| 473 |
static cl_object |
| 474 |
echo_peek_byte(cl_object strm) |
| 475 |
{ |
| 476 |
return ecl_peek_byte(ECHO_STREAM_INPUT(strm)); |
| 477 |
} |
| 478 |
|
| 479 |
static ecl_character |
| 480 |
echo_read_char(cl_object strm) |
| 481 |
{ |
| 482 |
cl_object byte = strm->stream.last_byte; |
| 483 |
ecl_character c; |
| 484 |
if (byte != OBJNULL) { |
| 485 |
strm->stream.last_byte = OBJNULL; |
| 486 |
c = ecl_read_char(ECHO_STREAM_INPUT(strm)); |
| 487 |
} else { |
| 488 |
c = ecl_read_char(ECHO_STREAM_INPUT(strm)); |
| 489 |
if (c != EOF) |
| 490 |
ecl_write_char(c, ECHO_STREAM_OUTPUT(strm)); |
| 491 |
} |
| 492 |
return c; |
| 493 |
} |
| 494 |
|
| 495 |
static ecl_character |
| 496 |
echo_write_char(cl_object strm, ecl_character c) |
| 497 |
{ |
| 498 |
return ecl_write_char(c, ECHO_STREAM_OUTPUT(strm)); |
| 499 |
} |
| 500 |
|
| 501 |
static void |
| 502 |
echo_unread_char(cl_object strm, ecl_character c) |
| 503 |
{ |
| 504 |
unlikely_if (strm->stream.last_byte != OBJNULL) { |
| 505 |
ecl_unread_twice(strm); |
| 506 |
} |
| 507 |
strm->stream.last_byte = ECL_T;; |
| 508 |
ecl_unread_char(c, ECHO_STREAM_INPUT(strm)); |
| 509 |
} |
| 510 |
|
| 511 |
static ecl_character |
| 512 |
echo_peek_char(cl_object strm) |
| 513 |
{ |
| 514 |
return ecl_peek_char(ECHO_STREAM_INPUT(strm)); |
| 515 |
} |
| 516 |
|
| 517 |
static int |
| 518 |
echo_listen(cl_object strm) |
| 519 |
{ |
| 520 |
return ecl_listen_stream(ECHO_STREAM_INPUT(strm)); |
| 521 |
} |
| 522 |
|
| 523 |
static void |
| 524 |
echo_clear_input(cl_object strm) |
| 525 |
{ |
| 526 |
ecl_clear_input(ECHO_STREAM_INPUT(strm)); |
| 527 |
} |
| 528 |
|
| 529 |
static void |
| 530 |
echo_clear_output(cl_object strm) |
| 531 |
{ |
| 532 |
ecl_clear_output(ECHO_STREAM_OUTPUT(strm)); |
| 533 |
} |
| 534 |
|
| 535 |
static void |
| 536 |
echo_force_output(cl_object strm) |
| 537 |
{ |
| 538 |
ecl_force_output(ECHO_STREAM_OUTPUT(strm)); |
| 539 |
} |
| 540 |
|
| 541 |
static void |
| 542 |
echo_finish_output(cl_object strm) |
| 543 |
{ |
| 544 |
ecl_finish_output(ECHO_STREAM_OUTPUT(strm)); |
| 545 |
} |
| 546 |
|
| 547 |
static cl_object |
| 548 |
echo_element_type(cl_object strm) |
| 549 |
{ |
| 550 |
return ecl_stream_element_type(ECHO_STREAM_INPUT(strm)); |
| 551 |
} |
| 552 |
|
| 553 |
static int |
| 554 |
echo_column(cl_object strm) |
| 555 |
{ |
| 556 |
return ecl_file_column(ECHO_STREAM_OUTPUT(strm)); |
| 557 |
} |
| 558 |
|
| 559 |
static cl_object |
| 560 |
echo_close(cl_object strm) |
| 561 |
{ |
| 562 |
if (strm->stream.flags & ECL_STREAM_CLOSE_COMPONENTS) { |
| 563 |
cl_close(1, ECHO_STREAM_INPUT(strm)); |
| 564 |
cl_close(1, ECHO_STREAM_OUTPUT(strm)); |
| 565 |
} |
| 566 |
return ecl_generic_close(strm); |
| 567 |
} |
| 568 |
|
| 569 |
const struct ecl_file_ops echo_ops = { |
| 570 |
echo_read_byte8, |
| 571 |
echo_write_byte8, |
| 572 |
|
| 573 |
echo_read_byte, |
| 574 |
echo_write_byte, |
| 575 |
echo_unread_byte, |
| 576 |
echo_peek_byte, |
| 577 |
|
| 578 |
echo_read_char, |
| 579 |
echo_write_char, |
| 580 |
echo_unread_char, |
| 581 |
echo_peek_char, |
| 582 |
|
| 583 |
ecl_generic_read_vector, |
| 584 |
ecl_generic_write_vector, |
| 585 |
|
| 586 |
echo_listen, |
| 587 |
echo_clear_input, |
| 588 |
echo_clear_output, |
| 589 |
echo_finish_output, |
| 590 |
echo_force_output, |
| 591 |
|
| 592 |
ecl_generic_always_true, /* input_p */ |
| 593 |
ecl_generic_always_true, /* output_p */ |
| 594 |
ecl_generic_always_false, |
| 595 |
echo_element_type, |
| 596 |
|
| 597 |
ecl_not_a_file_stream, /* length */ |
| 598 |
ecl_generic_always_nil, /* get_position */ |
| 599 |
ecl_generic_set_position, |
| 600 |
ecl_not_file_string_length, |
| 601 |
echo_column, |
| 602 |
|
| 603 |
ecl_not_a_file_stream, |
| 604 |
ecl_not_a_file_stream, |
| 605 |
|
| 606 |
echo_close |
| 607 |
}; |
| 608 |
|
| 609 |
cl_object |
| 610 |
cl_make_echo_stream(cl_object strm1, cl_object strm2) |
| 611 |
{ |
| 612 |
cl_object strm; |
| 613 |
unlikely_if (!ecl_input_stream_p(strm1)) |
| 614 |
ecl_not_an_input_stream(strm1); |
| 615 |
unlikely_if (!ecl_output_stream_p(strm2)) |
| 616 |
ecl_not_an_output_stream(strm2); |
| 617 |
strm = ecl_alloc_stream(); |
| 618 |
strm->stream.format = cl_stream_external_format(strm1); |
| 619 |
strm->stream.mode = (short)ecl_smm_echo; |
| 620 |
strm->stream.ops = ecl_duplicate_dispatch_table(&echo_ops); |
| 621 |
ECHO_STREAM_INPUT(strm) = strm1; |
| 622 |
ECHO_STREAM_OUTPUT(strm) = strm2; |
| 623 |
@(return strm); |
| 624 |
} |
| 625 |
|
| 626 |
cl_object |
| 627 |
cl_echo_stream_input_stream(cl_object strm) |
| 628 |
{ |
| 629 |
unlikely_if (!ECL_ANSI_STREAM_TYPE_P(strm, ecl_smm_echo)) |
| 630 |
FEwrong_type_only_arg(@[echo-stream-input-stream], |
| 631 |
strm, @[echo-stream]); |
| 632 |
@(return ECHO_STREAM_INPUT(strm)); |
| 633 |
} |
| 634 |
|
| 635 |
cl_object |
| 636 |
cl_echo_stream_output_stream(cl_object strm) |
| 637 |
{ |
| 638 |
unlikely_if (!ECL_ANSI_STREAM_TYPE_P(strm, ecl_smm_echo)) |
| 639 |
FEwrong_type_only_arg(@[echo-stream-output-stream], |
| 640 |
strm, @[echo-stream]); |
| 641 |
@(return ECHO_STREAM_OUTPUT(strm)); |
| 642 |
} |
| 643 |
|
| 644 |
/********************************************************************** |
| 645 |
* CONCATENATED STREAM |
| 646 |
*/ |
| 647 |
|
| 648 |
static cl_index |
| 649 |
concatenated_read_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 650 |
{ |
| 651 |
cl_object l = CONCATENATED_STREAM_LIST(strm); |
| 652 |
cl_index out = 0; |
| 653 |
while (out < n && !Null(l)) { |
| 654 |
cl_index delta = ecl_read_byte8(ECL_CONS_CAR(l), c + out, n - out); |
| 655 |
out += delta; |
| 656 |
if (out == n) break; |
| 657 |
CONCATENATED_STREAM_LIST(strm) = l = ECL_CONS_CDR(l); |
| 658 |
} |
| 659 |
return out; |
| 660 |
} |
| 661 |
|
| 662 |
static cl_object |
| 663 |
concatenated_read_byte(cl_object strm) |
| 664 |
{ |
| 665 |
cl_object l = CONCATENATED_STREAM_LIST(strm); |
| 666 |
cl_object c = OBJNULL; |
| 667 |
while (!Null(l)) { |
| 668 |
c = ecl_read_byte(ECL_CONS_CAR(l)); |
| 669 |
if (c != OBJNULL) break; |
| 670 |
CONCATENATED_STREAM_LIST(strm) = l = ECL_CONS_CDR(l); |
| 671 |
} |
| 672 |
return c; |
| 673 |
} |
| 674 |
|
| 675 |
static void |
| 676 |
concatenated_unread_byte(cl_object strm, cl_object byte) |
| 677 |
{ |
| 678 |
cl_object l = CONCATENATED_STREAM_LIST(strm); |
| 679 |
unlikely_if (Null(l)) { |
| 680 |
ecl_unread_error(strm); |
| 681 |
} |
| 682 |
ecl_unread_byte(byte, ECL_CONS_CAR(l)); |
| 683 |
} |
| 684 |
|
| 685 |
static ecl_character |
| 686 |
concatenated_read_char(cl_object strm) |
| 687 |
{ |
| 688 |
cl_object l = CONCATENATED_STREAM_LIST(strm); |
| 689 |
ecl_character c = EOF; |
| 690 |
while (!Null(l)) { |
| 691 |
c = ecl_read_char(ECL_CONS_CAR(l)); |
| 692 |
if (c != EOF) break; |
| 693 |
CONCATENATED_STREAM_LIST(strm) = l = ECL_CONS_CDR(l); |
| 694 |
} |
| 695 |
return c; |
| 696 |
} |
| 697 |
|
| 698 |
static void |
| 699 |
concatenated_unread_char(cl_object strm, ecl_character c) |
| 700 |
{ |
| 701 |
cl_object l = CONCATENATED_STREAM_LIST(strm); |
| 702 |
unlikely_if (Null(l)) { |
| 703 |
ecl_unread_error(strm); |
| 704 |
} |
| 705 |
ecl_unread_char(c, ECL_CONS_CAR(l)); |
| 706 |
} |
| 707 |
|
| 708 |
static int |
| 709 |
concatenated_listen(cl_object strm) |
| 710 |
{ |
| 711 |
cl_object l = CONCATENATED_STREAM_LIST(strm); |
| 712 |
while (!Null(l)) { |
| 713 |
int f = ecl_listen_stream(ECL_CONS_CAR(l)); |
| 714 |
l = ECL_CONS_CDR(l); |
| 715 |
if (f == ECL_LISTEN_EOF) { |
| 716 |
CONCATENATED_STREAM_LIST(strm) = l; |
| 717 |
} else { |
| 718 |
return f; |
| 719 |
} |
| 720 |
} |
| 721 |
return ECL_LISTEN_EOF; |
| 722 |
} |
| 723 |
|
| 724 |
static cl_object |
| 725 |
concatenated_close(cl_object strm) |
| 726 |
{ |
| 727 |
if (strm->stream.flags & ECL_STREAM_CLOSE_COMPONENTS) { |
| 728 |
cl_mapc(2, @'close', CONCATENATED_STREAM_LIST(strm)); |
| 729 |
} |
| 730 |
return ecl_generic_close(strm); |
| 731 |
} |
| 732 |
|
| 733 |
const struct ecl_file_ops concatenated_ops = { |
| 734 |
concatenated_read_byte8, |
| 735 |
ecl_not_output_write_byte8, |
| 736 |
|
| 737 |
concatenated_read_byte, |
| 738 |
ecl_not_output_write_byte, |
| 739 |
concatenated_unread_byte, |
| 740 |
ecl_generic_peek_byte, |
| 741 |
|
| 742 |
concatenated_read_char, |
| 743 |
ecl_not_output_write_char, |
| 744 |
concatenated_unread_char, |
| 745 |
ecl_generic_peek_char, |
| 746 |
|
| 747 |
ecl_generic_read_vector, |
| 748 |
ecl_generic_write_vector, |
| 749 |
|
| 750 |
concatenated_listen, |
| 751 |
ecl_generic_void, /* clear_input */ |
| 752 |
ecl_not_output_clear_output, |
| 753 |
ecl_not_output_finish_output, |
| 754 |
ecl_not_output_force_output, |
| 755 |
|
| 756 |
ecl_generic_always_true, /* input_p */ |
| 757 |
ecl_generic_always_false, /* output_p */ |
| 758 |
ecl_generic_always_false, |
| 759 |
broadcast_element_type, |
| 760 |
|
| 761 |
ecl_not_a_file_stream, /* length */ |
| 762 |
ecl_generic_always_nil, /* get_position */ |
| 763 |
ecl_generic_set_position, |
| 764 |
ecl_not_output_string_length, |
| 765 |
ecl_unknown_column, |
| 766 |
|
| 767 |
ecl_not_a_file_stream, |
| 768 |
ecl_not_a_file_stream, |
| 769 |
|
| 770 |
concatenated_close |
| 771 |
}; |
| 772 |
|
| 773 |
@(defun make_concatenated_stream (&rest ap) |
| 774 |
cl_object x, streams; |
| 775 |
int i; |
| 776 |
@ |
| 777 |
streams = ECL_NIL; |
| 778 |
for (i = 0; i < narg; i++) { |
| 779 |
x = ecl_va_arg(ap); |
| 780 |
unlikely_if (!ecl_input_stream_p(x)) |
| 781 |
ecl_not_an_input_stream(x); |
| 782 |
streams = CONS(x, streams); |
| 783 |
} |
| 784 |
x = ecl_alloc_stream(); |
| 785 |
if (Null(streams)) { |
| 786 |
x->stream.format = @':pass-through'; |
| 787 |
} else { |
| 788 |
x->stream.format = cl_stream_external_format(ECL_CONS_CAR(streams)); |
| 789 |
} |
| 790 |
x->stream.mode = (short)ecl_smm_concatenated; |
| 791 |
x->stream.ops = ecl_duplicate_dispatch_table(&concatenated_ops); |
| 792 |
CONCATENATED_STREAM_LIST(x) = cl_nreverse(streams); |
| 793 |
@(return x); |
| 794 |
@) |
| 795 |
|
| 796 |
cl_object |
| 797 |
cl_concatenated_stream_streams(cl_object strm) |
| 798 |
{ |
| 799 |
unlikely_if (!ECL_ANSI_STREAM_TYPE_P(strm, ecl_smm_concatenated)) |
| 800 |
FEwrong_type_only_arg(@[concatenated-stream-streams], |
| 801 |
strm, @[concatenated-stream]); |
| 802 |
return cl_copy_list(CONCATENATED_STREAM_LIST(strm)); |
| 803 |
} |
| 804 |
|
| 805 |
/********************************************************************** |
| 806 |
* SYNONYM STREAM |
| 807 |
*/ |
| 808 |
|
| 809 |
static cl_index |
| 810 |
synonym_read_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 811 |
{ |
| 812 |
return ecl_read_byte8(SYNONYM_STREAM_STREAM(strm), c, n); |
| 813 |
} |
| 814 |
|
| 815 |
static cl_index |
| 816 |
synonym_write_byte8(cl_object strm, unsigned char *c, cl_index n) |
| 817 |
{ |
| 818 |
return ecl_write_byte8(SYNONYM_STREAM_STREAM(strm), c, n); |
| 819 |
} |
| 820 |
|
| 821 |
static void |
| 822 |
synonym_write_byte(cl_object strm, cl_object byte) |
| 823 |
{ |
| 824 |
ecl_write_byte(byte, SYNONYM_STREAM_STREAM(strm)); |
| 825 |
} |
| 826 |
|
| 827 |
static cl_object |
| 828 |
synonym_read_byte(cl_object strm) |
| 829 |
{ |
| 830 |
return ecl_read_byte(SYNONYM_STREAM_STREAM(strm)); |
| 831 |
} |
| 832 |
|
| 833 |
static void |
| 834 |
synonym_unread_byte(cl_object strm, cl_object byte) |
| 835 |
{ |
| 836 |
ecl_unread_byte(SYNONYM_STREAM_STREAM(strm), byte); |
| 837 |
} |
| 838 |
|
| 839 |
static cl_object |
| 840 |
synonym_peek_byte(cl_object strm) |
| 841 |
{ |
| 842 |
return ecl_peek_byte(SYNONYM_STREAM_STREAM(strm)); |
| 843 |
} |
| 844 |
|
| 845 |
static ecl_character |
| 846 |
synonym_read_char(cl_object strm) |
| 847 |
{ |
| 848 |
return ecl_read_char(SYNONYM_STREAM_STREAM(strm)); |
| 849 |
} |
| 850 |
|
| 851 |
static ecl_character |
| 852 |
synonym_write_char(cl_object strm, ecl_character c) |
| 853 |
{ |
| 854 |
return ecl_write_char(c, SYNONYM_STREAM_STREAM(strm)); |
| 855 |
} |
| 856 |
|
| 857 |
static void |
| 858 |
synonym_unread_char(cl_object strm, ecl_character c) |
| 859 |
{ |
| 860 |
ecl_unread_char(c, SYNONYM_STREAM_STREAM(strm)); |
| 861 |
} |
| 862 |
|
| 863 |
static ecl_character |
| 864 |
synonym_peek_char(cl_object strm) |
| 865 |
{ |
| 866 |
return ecl_peek_char(SYNONYM_STREAM_STREAM(strm)); |
| 867 |
} |
| 868 |
|
| 869 |
static cl_index |
| 870 |
synonym_read_vector(cl_object strm, cl_object data, cl_index start, cl_index n) |
| 871 |
{ |
| 872 |
strm = SYNONYM_STREAM_STREAM(strm); |
| 873 |
return ecl_stream_dispatch_table(strm)->read_vector(strm, data, start, n); |
| 874 |
} |
| 875 |
|
| 876 |
static cl_index |
| 877 |
synonym_write_vector(cl_object strm, cl_object data, cl_index start, cl_index n) |
| 878 |
{ |
| 879 |
strm = SYNONYM_STREAM_STREAM(strm); |
| 880 |
return ecl_stream_dispatch_table(strm)->write_vector(strm, data, start, n); |
| 881 |
} |
| 882 |
|
| 883 |
static int |
| 884 |
synonym_listen(cl_object strm) |
| 885 |
{ |
| 886 |
return ecl_listen_stream(SYNONYM_STREAM_STREAM(strm)); |
| 887 |
} |
| 888 |
|
| 889 |
static void |
| 890 |
synonym_clear_input(cl_object strm) |
| 891 |
{ |
| 892 |
ecl_clear_input(SYNONYM_STREAM_STREAM(strm)); |
| 893 |
} |
| 894 |
|
| 895 |
static void |
| 896 |
synonym_clear_output(cl_object strm) |
| 897 |
{ |
| 898 |
ecl_clear_output(SYNONYM_STREAM_STREAM(strm)); |
| 899 |
} |
| 900 |
|
| 901 |
static void |
| 902 |
synonym_force_output(cl_object strm) |
| 903 |
{ |
| 904 |
ecl_force_output(SYNONYM_STREAM_STREAM(strm)); |
| 905 |
} |
| 906 |
|
| 907 |
static void |
| 908 |
synonym_finish_output(cl_object strm) |
| 909 |
{ |
| 910 |
ecl_finish_output(SYNONYM_STREAM_STREAM(strm)); |
| 911 |
} |
| 912 |
|
| 913 |
static int |
| 914 |
synonym_input_p(cl_object strm) |
| 915 |
{ |
| 916 |
return ecl_input_stream_p(SYNONYM_STREAM_STREAM(strm)); |
| 917 |
} |
| 918 |
|
| 919 |
static int |
| 920 |
synonym_output_p(cl_object strm) |
| 921 |
{ |
| 922 |
return ecl_output_stream_p(SYNONYM_STREAM_STREAM(strm)); |
| 923 |
} |
| 924 |
|
| 925 |
static int |
| 926 |
synonym_interactive_p(cl_object strm) |
| 927 |
{ |
| 928 |
return ecl_interactive_stream_p(SYNONYM_STREAM_STREAM(strm)); |
| 929 |
} |
| 930 |
|
| 931 |
static cl_object |
| 932 |
synonym_element_type(cl_object strm) |
| 933 |
{ |
| 934 |
return ecl_stream_element_type(SYNONYM_STREAM_STREAM(strm)); |
| 935 |
} |
| 936 |
|
| 937 |
static cl_object |
| 938 |
synonym_length(cl_object strm) |
| 939 |
{ |
| 940 |
return ecl_file_length(SYNONYM_STREAM_STREAM(strm)); |
| 941 |
} |
| 942 |
|
| 943 |
static cl_object |
| 944 |
synonym_get_position(cl_object strm) |
| 945 |
{ |
| 946 |
return ecl_file_position(SYNONYM_STREAM_STREAM(strm)); |
| 947 |
} |
| 948 |
|
| 949 |
static cl_object |
| 950 |
synonym_set_position(cl_object strm, cl_object pos) |
| 951 |
{ |
| 952 |
return ecl_file_position_set(SYNONYM_STREAM_STREAM(strm), pos); |
| 953 |
} |
| 954 |
|
| 955 |
static cl_object |
| 956 |
synonym_string_length(cl_object strm, cl_object string) |
| 957 |
{ |
| 958 |
return ecl_file_string_length(SYNONYM_STREAM_STREAM(strm), string); |
| 959 |
} |
| 960 |
|
| 961 |
static int |
| 962 |
synonym_column(cl_object strm) |
| 963 |
{ |
| 964 |
return ecl_file_column(SYNONYM_STREAM_STREAM(strm)); |
| 965 |
} |
| 966 |
|
| 967 |
static cl_object |
| 968 |
synonym_pathname(cl_object strm) |
| 969 |
{ |
| 970 |
return ecl_stream_pathname(SYNONYM_STREAM_STREAM(strm)); |
| 971 |
} |
| 972 |
|
| 973 |
static cl_object |
| 974 |
synonym_truename(cl_object strm) |
| 975 |
{ |
| 976 |
return ecl_stream_truename(SYNONYM_STREAM_STREAM(strm)); |
| 977 |
} |
| 978 |
|
| 979 |
const struct ecl_file_ops synonym_ops = { |
| 980 |
synonym_read_byte8, |
| 981 |
synonym_write_byte8, |
| 982 |
|
| 983 |
synonym_read_byte, |
| 984 |
synonym_write_byte, |
| 985 |
synonym_unread_byte, |
| 986 |
synonym_peek_byte, |
| 987 |
|
| 988 |
synonym_read_char, |
| 989 |
synonym_write_char, |
| 990 |
synonym_unread_char, |
| 991 |
synonym_peek_char, |
| 992 |
|
| 993 |
synonym_read_vector, |
| 994 |
synonym_write_vector, |
| 995 |
|
| 996 |
synonym_listen, |
| 997 |
synonym_clear_input, |
| 998 |
synonym_clear_output, |
| 999 |
synonym_finish_output, |
| 1000 |
synonym_force_output, |
| 1001 |
|
| 1002 |
synonym_input_p, |
| 1003 |
synonym_output_p, |
| 1004 |
synonym_interactive_p, |
| 1005 |
synonym_element_type, |
| 1006 |
|
| 1007 |
synonym_length, |
| 1008 |
synonym_get_position, |
| 1009 |
synonym_set_position, |
| 1010 |
synonym_string_length, |
| 1011 |
synonym_column, |
| 1012 |
|
| 1013 |
synonym_pathname, |
| 1014 |
synonym_truename, |
| 1015 |
|
| 1016 |
ecl_generic_close |
| 1017 |
}; |
| 1018 |
|
| 1019 |
cl_object |
| 1020 |
cl_make_synonym_stream(cl_object sym) |
| 1021 |
{ |
| 1022 |
cl_object x; |
| 1023 |
|
| 1024 |
sym = ecl_check_cl_type(@'make-synonym-stream',sym,t_symbol); |
| 1025 |
x = ecl_alloc_stream(); |
| 1026 |
x->stream.ops = ecl_duplicate_dispatch_table(&synonym_ops); |
| 1027 |
x->stream.mode = (short)ecl_smm_synonym; |
| 1028 |
SYNONYM_STREAM_SYMBOL(x) = sym; |
| 1029 |
@(return x); |
| 1030 |
} |
| 1031 |
|
| 1032 |
cl_object |
| 1033 |
cl_synonym_stream_symbol(cl_object strm) |
| 1034 |
{ |
| 1035 |
unlikely_if (!ECL_ANSI_STREAM_TYPE_P(strm, ecl_smm_synonym)) |
| 1036 |
FEwrong_type_only_arg(@[synonym-stream-symbol], |
| 1037 |
strm, @[synonym-stream]); |
| 1038 |
@(return SYNONYM_STREAM_SYMBOL(strm)); |
| 1039 |
} |