Debian Codegraph @

src/c/stream.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * stream.d - stream interface
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 /* -- imports --------------------------------------------------------------- */
17
18 #include <ecl/ecl.h>
19 #include <ecl/internal.h>
20
21 #ifdef ECL_CLOS_STREAMS
22 extern const struct ecl_file_ops clos_stream_ops;
23 #endif
24
25 /* -- implementation -------------------------------------------------------- */
26
27 cl_object
28 ecl_alloc_stream(void)
29 {
30 cl_object x = ecl_alloc_object(t_stream);
31 x->stream.closed = 0;
32 x->stream.file.descriptor = -1;
33 x->stream.object0 =
34 x->stream.object1 = OBJNULL;
35 x->stream.int0 = x->stream.int1 = 0;
36 x->stream.format = ECL_NIL;
37 x->stream.flags = 0;
38 x->stream.byte_size = 8;
39 x->stream.last_byte = OBJNULL;
40 x->stream.buffer = NULL;
41 x->stream.encoder = NULL;
42 x->stream.decoder = NULL;
43 x->stream.byte_buffer = NULL;
44 x->stream.byte_encoder = NULL;
45 x->stream.byte_decoder = NULL;
46 x->stream.last_char = EOF;
47 x->stream.byte_stack = ECL_NIL;
48 x->stream.eof_char = EOF;
49 return x;
50 }
51
52 struct ecl_file_ops *
53 ecl_duplicate_dispatch_table(const struct ecl_file_ops *ops)
54 {
55 struct ecl_file_ops *new_ops = ecl_alloc_atomic(sizeof(*ops));
56 *new_ops = *ops;
57 return new_ops;
58 }
59
60 const struct ecl_file_ops *
61 ecl_stream_dispatch_table(cl_object strm)
62 {
63 #ifdef ECL_CLOS_STREAMS
64 if (ECL_INSTANCEP(strm)) {
65 return &clos_stream_ops;
66 }
67 #endif
68 if (!ECL_ANSI_STREAM_P(strm))
69 FEwrong_type_argument(@[stream], strm);
70 return (const struct ecl_file_ops *)strm->stream.ops;
71 }
72
73 cl_index
74 ecl_read_byte8(cl_object strm, unsigned char *c, cl_index n)
75 {
76 return ecl_stream_dispatch_table(strm)->read_byte8(strm, c, n);
77 }
78
79 cl_index
80 ecl_write_byte8(cl_object strm, unsigned char *c, cl_index n)
81 {
82 return ecl_stream_dispatch_table(strm)->write_byte8(strm, c, n);
83 }
84
85 cl_object
86 ecl_read_byte(cl_object strm)
87 {
88 return ecl_stream_dispatch_table(strm)->read_byte(strm);
89 }
90
91 void
92 ecl_write_byte(cl_object byte, cl_object strm)
93 {
94 ecl_stream_dispatch_table(strm)->write_byte(strm, byte);
95 }
96
97 void
98 ecl_unread_byte(cl_object byte, cl_object strm)
99 {
100 ecl_stream_dispatch_table(strm)->unread_byte(strm, byte);
101 }
102
103 cl_object
104 ecl_peek_byte(cl_object strm)
105 {
106 return ecl_stream_dispatch_table(strm)->peek_byte(strm);
107 }
108
109 ecl_character
110 ecl_read_char(cl_object strm)
111 {
112 return ecl_stream_dispatch_table(strm)->read_char(strm);
113 }
114
115 ecl_character
116 ecl_read_char_noeof(cl_object strm)
117 {
118 ecl_character c = ecl_read_char(strm);
119 if (c == EOF)
120 FEend_of_file(strm);
121 return c;
122 }
123
124 ecl_character
125 ecl_write_char(ecl_character c, cl_object strm)
126 {
127 return ecl_stream_dispatch_table(strm)->write_char(strm, c);
128 }
129
130 void
131 ecl_unread_char(ecl_character c, cl_object strm)
132 {
133 ecl_stream_dispatch_table(strm)->unread_char(strm, c);
134 }
135
136 ecl_character
137 ecl_peek_char(cl_object strm)
138 {
139 return ecl_stream_dispatch_table(strm)->peek_char(strm);
140 }
141
142 int
143 ecl_listen_stream(cl_object strm)
144 {
145 return ecl_stream_dispatch_table(strm)->listen(strm);
146 }
147
148 void
149 ecl_clear_input(cl_object strm)
150 {
151 ecl_stream_dispatch_table(strm)->clear_input(strm);
152 }
153
154 void
155 ecl_clear_output(cl_object strm)
156 {
157 ecl_stream_dispatch_table(strm)->clear_output(strm);
158 }
159
160 void
161 ecl_force_output(cl_object strm)
162 {
163 ecl_stream_dispatch_table(strm)->force_output(strm);
164 }
165
166 void
167 ecl_finish_output(cl_object strm)
168 {
169 ecl_stream_dispatch_table(strm)->finish_output(strm);
170 }
171
172 int
173 ecl_file_column(cl_object strm)
174 {
175 return ecl_stream_dispatch_table(strm)->column(strm);
176 }
177
178 cl_object
179 ecl_file_length(cl_object strm)
180 {
181 return ecl_stream_dispatch_table(strm)->length(strm);
182 }
183
184 cl_object
185 ecl_file_position(cl_object strm)
186 {
187 return ecl_stream_dispatch_table(strm)->get_position(strm);
188 }
189
190 cl_object
191 ecl_file_position_set(cl_object strm, cl_object pos)
192 {
193 return ecl_stream_dispatch_table(strm)->set_position(strm, pos);
194 }
195
196 cl_object
197 ecl_file_string_length(cl_object strm, cl_object string)
198 {
199 return ecl_stream_dispatch_table(strm)->string_length(strm, string);
200 }
201
202 bool
203 ecl_input_stream_p(cl_object strm)
204 {
205 return ecl_stream_dispatch_table(strm)->input_p(strm);
206 }
207
208 bool
209 ecl_output_stream_p(cl_object strm)
210 {
211 return ecl_stream_dispatch_table(strm)->output_p(strm);
212 }
213
214 cl_object
215 ecl_stream_element_type(cl_object strm)
216 {
217 return ecl_stream_dispatch_table(strm)->element_type(strm);
218 }
219
220 bool
221 ecl_interactive_stream_p(cl_object strm)
222 {
223 return ecl_stream_dispatch_table(strm)->interactive_p(strm);
224 }
225
226 cl_object
227 ecl_stream_pathname(cl_object strm)
228 {
229 return ecl_stream_dispatch_table(strm)->pathname(strm);
230 }
231
232 cl_object
233 ecl_stream_truename(cl_object strm)
234 {
235 return ecl_stream_dispatch_table(strm)->truename(strm);
236 }
237
238 /* -- Lisp interface -------------------------------------------------------- */
239
240 cl_object
241 si_read_char(cl_object strm, cl_object eof_value)
242 {
243 cl_env_ptr the_env = ecl_process_env();
244 ecl_character c = ecl_read_char(strm);
245 ecl_return1(the_env, (c==EOF) ? eof_value : ECL_CODE_CHAR(c));
246 }
247
248 cl_object
249 si_unread_char(cl_object strm, cl_object c)
250 {
251 cl_env_ptr the_env = ecl_process_env();
252 ecl_unread_char(ecl_char_code(c), strm);
253 ecl_return1(the_env, ECL_NIL);
254 }
255
256 cl_object
257 si_peek_char(cl_object strm, cl_object eof_value)
258 {
259 cl_env_ptr the_env = ecl_process_env();
260 ecl_character c = ecl_peek_char(strm);
261 ecl_return1(the_env, (c==EOF)? eof_value : ECL_CODE_CHAR(c));
262 }
263
264 cl_object
265 si_write_char(cl_object strm, cl_object c)
266 {
267 cl_env_ptr the_env = ecl_process_env();
268 ecl_write_char(ecl_char_code(c), strm);
269 ecl_return1(the_env, c);
270 }
271
272 cl_object
273 si_read_byte(cl_object strm, cl_object eof_value)
274 {
275 cl_env_ptr the_env = ecl_process_env();
276 cl_object byte = ecl_read_byte(strm);
277 ecl_return1(the_env, (byte == OBJNULL) ? eof_value : byte);
278 }
279
280 cl_object
281 si_unread_byte(cl_object strm, cl_object byte)
282 {
283 cl_env_ptr the_env = ecl_process_env();
284 ecl_unread_byte(byte, strm);
285 ecl_return1(the_env, ECL_NIL);
286 }
287
288 cl_object
289 si_peek_byte(cl_object strm, cl_object eof_value)
290 {
291 cl_env_ptr the_env = ecl_process_env();
292 cl_object byte = ecl_peek_byte(strm);
293 ecl_return1(the_env, (byte == OBJNULL) ? eof_value : byte);
294 }
295
296 cl_object
297 si_write_byte(cl_object strm, cl_object byte)
298 {
299 cl_env_ptr the_env = ecl_process_env();
300 ecl_write_byte(byte, strm);
301 ecl_return1(the_env, byte);
302 }
303
304 cl_object
305 si_listen(cl_object strm)
306 {
307 cl_env_ptr the_env = ecl_process_env();
308 ecl_return1(the_env, ((ecl_listen_stream(strm) == ECL_LISTEN_AVAILABLE)
309 ? ECL_T : ECL_NIL));
310 }
311
312 cl_object
313 si_clear_input(cl_object strm)
314 {
315 cl_env_ptr the_env = ecl_process_env();
316 ecl_clear_input(strm);
317 ecl_return1(the_env, ECL_NIL);
318 }
319
320 cl_object
321 si_finish_output(cl_object strm)
322 {
323 cl_env_ptr the_env = ecl_process_env();
324 ecl_finish_output(strm);
325 ecl_return1(the_env, ECL_NIL);
326 }
327
328 cl_object
329 si_force_output(cl_object strm)
330 {
331 cl_env_ptr the_env = ecl_process_env();
332 ecl_force_output(strm);
333 ecl_return1(the_env, ECL_NIL);
334 }
335
336 cl_object
337 si_clear_output(cl_object strm)
338 {
339 cl_env_ptr the_env = ecl_process_env();
340 ecl_clear_output(strm);
341 ecl_return1(the_env, ECL_NIL);
342 }
343
344 cl_object
345 si_file_column(cl_object strm)
346 {
347 int column = ecl_file_column(strm);
348 @(return (column >= 0 ? ecl_make_fixnum(column) : ECL_NIL));
349 }
350
351 cl_object
352 cl_file_length(cl_object strm)
353 {
354 @(return ecl_file_length(strm));
355 }
356
357 @(defun file-position (file_stream &o position)
358 cl_object output;
359 @
360 if (Null(position)) {
361 output = ecl_file_position(file_stream);
362 } else {
363 if (position == @':start') {
364 position = ecl_make_fixnum(0);
365 } else if (position == @':end') {
366 position = ECL_NIL;
367 }
368 output = ecl_file_position_set(file_stream, position);
369 }
370 @(return output);
371 @)
372
373 cl_object
374 cl_file_string_length(cl_object stream, cl_object string)
375 {
376 @(return ecl_file_string_length(stream, string));
377 }
378
379 cl_object
380 cl_input_stream_p(cl_object strm)
381 {
382 @(return (ecl_input_stream_p(strm) ? ECL_T : ECL_NIL));
383 }
384
385 cl_object
386 cl_output_stream_p(cl_object strm)
387 {
388 @(return (ecl_output_stream_p(strm) ? ECL_T : ECL_NIL));
389 }
390
391 cl_object
392 cl_interactive_stream_p(cl_object strm)
393 {
394 @(return (ecl_stream_dispatch_table(strm)->interactive_p(strm)? ECL_T : ECL_NIL));
395 }
396
397 cl_object
398 cl_open_stream_p(cl_object strm)
399 {
400 /* ANSI and Cltl2 specify that open-stream-p should work
401 on closed streams, and that a stream is only closed
402 when #'close has been applied on it */
403 #ifdef ECL_CLOS_STREAMS
404 if (ECL_INSTANCEP(strm)) {
405 return _ecl_funcall2(@'gray::open-stream-p', strm);
406 }
407 #endif
408 unlikely_if (!ECL_ANSI_STREAM_P(strm))
409 FEwrong_type_only_arg(@'open-stream-p', strm, @'stream');
410 @(return (strm->stream.closed ? ECL_NIL : ECL_T));
411 }
412
413 cl_object
414 cl_stream_element_type(cl_object strm)
415 {
416 @(return ecl_stream_element_type(strm));
417 }
418
419 cl_object
420 cl_stream_external_format(cl_object strm)
421 {
422 cl_object output;
423 cl_type t;
424 AGAIN:
425 t= ecl_t_of(strm);
426 #ifdef ECL_CLOS_STREAMS
427 if (t == t_instance)
428 output = @':default';
429 else
430 #endif
431 unlikely_if (t != t_stream)
432 FEwrong_type_only_arg(@[stream-external-format], strm, @[stream]);
433 if (strm->stream.mode == ecl_smm_synonym) {
434 strm = SYNONYM_STREAM_STREAM(strm);
435 goto AGAIN;
436 }
437 output = strm->stream.format;
438 @(return output);
439 }
440
441 cl_object
442 cl_streamp(cl_object strm)
443 {
444 #ifdef ECL_CLOS_STREAMS
445 if (ECL_INSTANCEP(strm)) {
446 return _ecl_funcall2(@'gray::streamp', strm);
447 }
448 #endif
449 @(return (ECL_ANSI_STREAM_P(strm) ? ECL_T : ECL_NIL));
450 }
451
452 /* -- Miscellaneous --------------------------------------------------------- */
453
454 cl_object
455 si_copy_stream(cl_object in, cl_object out, cl_object wait)
456 {
457 ecl_character c;
458 if ((wait == ECL_NIL) && !ecl_listen_stream(in)) {
459 return ECL_NIL;
460 }
461 for (c = ecl_read_char(in); c != EOF; c = ecl_read_char(in)) {
462 ecl_write_char(c, out);
463 if ((wait == ECL_NIL) && !ecl_listen_stream(in)) {
464 break;
465 }
466 }
467 ecl_force_output(out);
468 @(return ((c==EOF) ? ECL_T : ECL_NIL));
469 }
470
471 cl_object
472 si_file_stream_fd(cl_object s)
473 {
474 cl_object ret;
475
476 unlikely_if (!ECL_FILE_STREAM_P(s)) {
477 ecl_not_a_file_stream(s);
478 }
479
480 switch ((enum ecl_smmode)s->stream.mode) {
481 case ecl_smm_input:
482 case ecl_smm_output:
483 case ecl_smm_io:
484 ret = ecl_make_fixnum(fileno(IO_STREAM_FILE(s)));
485 break;
486 case ecl_smm_input_file:
487 case ecl_smm_output_file:
488 case ecl_smm_io_file:
489 ret = ecl_make_fixnum(IO_FILE_DESCRIPTOR(s));
490 break;
491 default:
492 ecl_internal_error("not a file stream");
493 }
494 @(return ret);
495 }