Commit | Line | Data |
---|---|---|
22a52da1 | 1 | /* Copyright (C) 1995-1999,2000,2001 Free Software Foundation, Inc. |
0f2d19dd JB |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 | 41 | |
1bbd0b84 | 42 | |
0f2d19dd JB |
43 | \f |
44 | ||
e6e2e95a MD |
45 | #include <errno.h> |
46 | ||
a0599745 MD |
47 | #include "libguile/_scm.h" |
48 | #include "libguile/chars.h" | |
a002f1a2 | 49 | #include "libguile/continuations.h" |
a0599745 MD |
50 | #include "libguile/smob.h" |
51 | #include "libguile/eval.h" | |
52 | #include "libguile/macros.h" | |
53 | #include "libguile/procprop.h" | |
54 | #include "libguile/read.h" | |
55 | #include "libguile/weaks.h" | |
56 | #include "libguile/unif.h" | |
57 | #include "libguile/alist.h" | |
58 | #include "libguile/struct.h" | |
59 | #include "libguile/objects.h" | |
60 | #include "libguile/ports.h" | |
61 | #include "libguile/root.h" | |
62 | #include "libguile/strings.h" | |
63 | #include "libguile/strports.h" | |
64 | #include "libguile/vectors.h" | |
c96d76b8 | 65 | #include "libguile/lang.h" |
a0599745 MD |
66 | |
67 | #include "libguile/validate.h" | |
68 | #include "libguile/print.h" | |
0f2d19dd JB |
69 | \f |
70 | ||
71 | /* {Names of immediate symbols} | |
72 | * | |
73 | * This table must agree with the declarations in scm.h: {Immediate Symbols}. | |
74 | */ | |
75 | ||
76 | char *scm_isymnames[] = | |
77 | { | |
c96d76b8 | 78 | /* This table must agree with the list of SCM_IM_ constants in tags.h */ |
0f2d19dd JB |
79 | "#@and", |
80 | "#@begin", | |
81 | "#@case", | |
82 | "#@cond", | |
83 | "#@do", | |
84 | "#@if", | |
85 | "#@lambda", | |
86 | "#@let", | |
87 | "#@let*", | |
88 | "#@letrec", | |
89 | "#@or", | |
90 | "#@quote", | |
91 | "#@set!", | |
92 | "#@define", | |
93 | #if 0 | |
94 | "#@literal-variable-ref", | |
95 | "#@literal-variable-set!", | |
96 | #endif | |
97 | "#@apply", | |
98 | "#@call-with-current-continuation", | |
99 | ||
100 | /* user visible ISYMS */ | |
101 | /* other keywords */ | |
102 | /* Flags */ | |
103 | ||
104 | "#f", | |
105 | "#t", | |
106 | "#<undefined>", | |
107 | "#<eof>", | |
108 | "()", | |
4df91bd6 MD |
109 | "#<unspecified>", |
110 | "#@dispatch", | |
43c667e9 MD |
111 | "#@slot-ref", |
112 | "#@slot-set!", | |
159500fb | 113 | |
159500fb MD |
114 | /* Multi-language support */ |
115 | ||
116 | "#@nil-cond", | |
ccd0e478 MD |
117 | "#@bind", |
118 | ||
5623a9b4 | 119 | "#@delay", |
24d1fde8 MV |
120 | "#@call-with-values", |
121 | ||
c96d76b8 NJ |
122 | "#<unbound>", |
123 | ||
124 | /* Elisp nil value. This is its Scheme name; whenever it's printed | |
125 | in Elisp, it should appear as the symbol `nil'. */ | |
126 | ||
127 | "#nil" | |
0f2d19dd JB |
128 | }; |
129 | ||
92c2555f | 130 | scm_t_option scm_print_opts[] = { |
726d810a DH |
131 | { SCM_OPTION_SCM, "closure-hook", SCM_UNPACK (SCM_BOOL_F), |
132 | "Hook for printing closures (should handle macros as well)." }, | |
84f6a34a MD |
133 | { SCM_OPTION_BOOLEAN, "source", 0, |
134 | "Print closures with source." } | |
e6e4c9af MD |
135 | }; |
136 | ||
a1ec6916 | 137 | SCM_DEFINE (scm_print_options, "print-options-interface", 0, 1, 0, |
1bbd0b84 | 138 | (SCM setting), |
71331188 | 139 | "Option interface for the print options. Instead of using\n" |
1dd05fd8 MG |
140 | "this procedure directly, use the procedures\n" |
141 | "@code{print-enable}, @code{print-disable}, @code{print-set!}\n" | |
142 | "and @code{print-options}.") | |
1bbd0b84 | 143 | #define FUNC_NAME s_scm_print_options |
e6e4c9af | 144 | { |
a51ea417 | 145 | SCM ans = scm_options (setting, |
b7ff98dd MD |
146 | scm_print_opts, |
147 | SCM_N_PRINT_OPTIONS, | |
1bbd0b84 | 148 | FUNC_NAME); |
e6e4c9af MD |
149 | return ans; |
150 | } | |
1bbd0b84 | 151 | #undef FUNC_NAME |
e6e4c9af | 152 | |
0f2d19dd JB |
153 | \f |
154 | /* {Printing of Scheme Objects} | |
155 | */ | |
156 | ||
a51ea417 | 157 | /* Detection of circular references. |
c62fbfe1 MD |
158 | * |
159 | * Due to other constraints in the implementation, this code has bad | |
5d46ebe3 MD |
160 | * time complexity (O (depth * N)), The printer code can be |
161 | * rewritten to be O(N). | |
a51ea417 | 162 | */ |
c62fbfe1 | 163 | #define PUSH_REF(pstate, obj) \ |
1bbd0b84 | 164 | do { \ |
c62fbfe1 MD |
165 | pstate->ref_stack[pstate->top++] = (obj); \ |
166 | if (pstate->top == pstate->ceiling) \ | |
167 | grow_ref_stack (pstate); \ | |
1bbd0b84 | 168 | } while(0) |
a51ea417 | 169 | |
c62fbfe1 | 170 | #define ENTER_NESTED_DATA(pstate, obj, label) \ |
1bbd0b84 | 171 | do { \ |
5ca6dc39 | 172 | register unsigned long i; \ |
c62fbfe1 | 173 | for (i = 0; i < pstate->top; ++i) \ |
230d095f | 174 | if (SCM_EQ_P (pstate->ref_stack[i], (obj))) \ |
c62fbfe1 MD |
175 | goto label; \ |
176 | if (pstate->fancyp) \ | |
177 | { \ | |
178 | if (pstate->top - pstate->list_offset >= pstate->level) \ | |
179 | { \ | |
b7f3516f | 180 | scm_putc ('#', port); \ |
c62fbfe1 MD |
181 | return; \ |
182 | } \ | |
183 | } \ | |
184 | PUSH_REF(pstate, obj); \ | |
1bbd0b84 | 185 | } while(0) |
a51ea417 | 186 | |
c62fbfe1 MD |
187 | #define EXIT_NESTED_DATA(pstate) { --pstate->top; } |
188 | ||
d5cf5324 DH |
189 | SCM scm_print_state_vtable = SCM_BOOL_F; |
190 | static SCM print_state_pool = SCM_EOL; | |
c4f37e80 | 191 | |
f843a84c | 192 | #ifdef GUILE_DEBUG /* Used for debugging purposes */ |
1cc91f1b | 193 | |
3b3b36dd | 194 | SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0, |
1bbd0b84 | 195 | (), |
d5cf5324 | 196 | "Return the current-pstate -- the car of the\n" |
5352393c MG |
197 | "@code{print_state_pool}. @code{current-pstate} is only\n" |
198 | "included in @code{--enable-guile-debug} builds.") | |
1bbd0b84 | 199 | #define FUNC_NAME s_scm_current_pstate |
c62fbfe1 | 200 | { |
d5cf5324 DH |
201 | if (!SCM_NULLP (print_state_pool)) |
202 | return SCM_CAR (print_state_pool); | |
a0adfbf0 | 203 | else |
0a284a4e | 204 | return SCM_BOOL_F; |
c62fbfe1 | 205 | } |
1bbd0b84 GB |
206 | #undef FUNC_NAME |
207 | ||
c62fbfe1 MD |
208 | #endif |
209 | ||
210 | #define PSTATE_SIZE 50L | |
211 | ||
698c0295 | 212 | static SCM |
1bbd0b84 | 213 | make_print_state (void) |
698c0295 | 214 | { |
d5cf5324 DH |
215 | SCM print_state |
216 | = scm_make_struct (scm_print_state_vtable, SCM_INUM0, SCM_EOL); | |
bf685b6d | 217 | scm_print_state *pstate = SCM_PRINT_STATE (print_state); |
00ffa0e7 | 218 | pstate->ref_vect = scm_c_make_vector (PSTATE_SIZE, SCM_UNDEFINED); |
34d19ef6 | 219 | pstate->ref_stack = SCM_WRITABLE_VELTS (pstate->ref_vect); |
b17004b8 | 220 | pstate->ceiling = SCM_VECTOR_LENGTH (pstate->ref_vect); |
698c0295 MD |
221 | return print_state; |
222 | } | |
1cc91f1b | 223 | |
c62fbfe1 MD |
224 | SCM |
225 | scm_make_print_state () | |
c62fbfe1 | 226 | { |
230d095f | 227 | SCM answer = SCM_BOOL_F; |
698c0295 MD |
228 | |
229 | /* First try to allocate a print state from the pool */ | |
230 | SCM_DEFER_INTS; | |
d5cf5324 | 231 | if (!SCM_NULLP (print_state_pool)) |
698c0295 | 232 | { |
d5cf5324 DH |
233 | answer = SCM_CAR (print_state_pool); |
234 | print_state_pool = SCM_CDR (print_state_pool); | |
698c0295 MD |
235 | } |
236 | SCM_ALLOW_INTS; | |
237 | ||
230d095f | 238 | return SCM_FALSEP (answer) ? make_print_state () : answer; |
c62fbfe1 | 239 | } |
a51ea417 | 240 | |
698c0295 | 241 | void |
6e8d25a6 | 242 | scm_free_print_state (SCM print_state) |
698c0295 MD |
243 | { |
244 | SCM handle; | |
245 | scm_print_state *pstate = SCM_PRINT_STATE (print_state); | |
246 | /* Cleanup before returning print state to pool. | |
247 | * It is better to do it here. Doing it in scm_prin1 | |
248 | * would cost more since that function is called much more | |
249 | * often. | |
250 | */ | |
251 | pstate->fancyp = 0; | |
bb35f315 | 252 | pstate->revealed = 0; |
698c0295 | 253 | SCM_DEFER_INTS; |
16d4699b | 254 | handle = scm_cons (print_state, print_state_pool); |
d5cf5324 | 255 | print_state_pool = handle; |
698c0295 MD |
256 | SCM_ALLOW_INTS; |
257 | } | |
1cc91f1b | 258 | |
a51ea417 | 259 | static void |
1bbd0b84 | 260 | grow_ref_stack (scm_print_state *pstate) |
a51ea417 | 261 | { |
b17004b8 | 262 | unsigned long int old_size = SCM_VECTOR_LENGTH (pstate->ref_vect); |
34d19ef6 | 263 | SCM const *old_elts = SCM_VELTS (pstate->ref_vect); |
b17004b8 | 264 | unsigned long int new_size = 2 * pstate->ceiling; |
00ffa0e7 | 265 | SCM new_vect = scm_c_make_vector (new_size, SCM_UNDEFINED); |
b17004b8 DH |
266 | unsigned long int i; |
267 | ||
268 | for (i = 0; i != old_size; ++i) | |
34d19ef6 | 269 | SCM_VECTOR_SET (new_vect, i, old_elts [i]); |
b17004b8 DH |
270 | |
271 | pstate->ref_vect = new_vect; | |
34d19ef6 | 272 | pstate->ref_stack = SCM_WRITABLE_VELTS(new_vect); |
bf685b6d | 273 | pstate->ceiling = new_size; |
a51ea417 MD |
274 | } |
275 | ||
1cc91f1b | 276 | |
a51ea417 | 277 | static void |
34d19ef6 | 278 | print_circref (SCM port, scm_print_state *pstate, SCM ref) |
a51ea417 | 279 | { |
c014a02e ML |
280 | register long i; |
281 | long self = pstate->top - 1; | |
c62fbfe1 MD |
282 | i = pstate->top - 1; |
283 | if (SCM_CONSP (pstate->ref_stack[i])) | |
284 | { | |
285 | while (i > 0) | |
286 | { | |
d5cf5324 | 287 | if (!SCM_CONSP (pstate->ref_stack[i - 1]) |
230d095f DH |
288 | || !SCM_EQ_P (SCM_CDR (pstate->ref_stack[i - 1]), |
289 | pstate->ref_stack[i])) | |
c62fbfe1 MD |
290 | break; |
291 | --i; | |
292 | } | |
293 | self = i; | |
294 | } | |
295 | for (i = pstate->top - 1; 1; --i) | |
230d095f | 296 | if (SCM_EQ_P (pstate->ref_stack[i], ref)) |
c62fbfe1 | 297 | break; |
b7f3516f | 298 | scm_putc ('#', port); |
c62fbfe1 | 299 | scm_intprint (i - self, 10, port); |
b7f3516f | 300 | scm_putc ('#', port); |
a51ea417 MD |
301 | } |
302 | ||
6662998f MV |
303 | /* Print the name of a symbol. */ |
304 | ||
305 | void | |
306 | scm_print_symbol_name (const char *str, size_t len, SCM port) | |
307 | { | |
308 | size_t pos; | |
309 | size_t end; | |
310 | int weird; | |
311 | int maybe_weird; | |
312 | size_t mw_pos = 0; | |
313 | ||
314 | pos = 0; | |
315 | weird = 0; | |
316 | maybe_weird = 0; | |
317 | ||
318 | /* XXX - Lots of weird symbol names are missed, such as "12" or | |
319 | "'a". */ | |
320 | ||
321 | if (len == 0) | |
322 | scm_lfwrite ("#{}#", 4, port); | |
323 | else if (str[0] == '#' || str[0] == ':' || str[len-1] == ':') | |
324 | { | |
325 | scm_lfwrite ("#{", 2, port); | |
326 | weird = 1; | |
327 | } | |
328 | ||
329 | for (end = pos; end < len; ++end) | |
330 | switch (str[end]) | |
331 | { | |
332 | #ifdef BRACKETS_AS_PARENS | |
333 | case '[': | |
334 | case ']': | |
335 | #endif | |
336 | case '(': | |
337 | case ')': | |
338 | case '"': | |
339 | case ';': | |
340 | case SCM_WHITE_SPACES: | |
341 | case SCM_LINE_INCREMENTORS: | |
342 | weird_handler: | |
343 | if (maybe_weird) | |
344 | { | |
345 | end = mw_pos; | |
346 | maybe_weird = 0; | |
347 | } | |
348 | if (!weird) | |
349 | { | |
350 | scm_lfwrite ("#{", 2, port); | |
351 | weird = 1; | |
352 | } | |
353 | if (pos < end) | |
354 | { | |
355 | scm_lfwrite (str + pos, end - pos, port); | |
356 | } | |
357 | { | |
358 | char buf[2]; | |
359 | buf[0] = '\\'; | |
360 | buf[1] = str[end]; | |
361 | scm_lfwrite (buf, 2, port); | |
362 | } | |
363 | pos = end + 1; | |
364 | break; | |
365 | case '\\': | |
366 | if (weird) | |
367 | goto weird_handler; | |
368 | if (!maybe_weird) | |
369 | { | |
370 | maybe_weird = 1; | |
371 | mw_pos = pos; | |
372 | } | |
373 | break; | |
374 | case '}': | |
375 | case '#': | |
376 | if (weird) | |
377 | goto weird_handler; | |
378 | break; | |
379 | default: | |
380 | break; | |
381 | } | |
382 | if (pos < end) | |
383 | scm_lfwrite (str + pos, end - pos, port); | |
384 | if (weird) | |
385 | scm_lfwrite ("}#", 2, port); | |
386 | } | |
387 | ||
c62fbfe1 | 388 | /* Print generally. Handles both write and display according to PSTATE. |
0f2d19dd | 389 | */ |
8b840115 MD |
390 | SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); |
391 | SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); | |
1cc91f1b | 392 | |
0f2d19dd | 393 | void |
1bbd0b84 | 394 | scm_iprin1 (SCM exp, SCM port, scm_print_state *pstate) |
0f2d19dd | 395 | { |
54778cd3 | 396 | switch (SCM_ITAG3 (exp)) |
0f2d19dd | 397 | { |
e34f941a DH |
398 | case scm_tc3_closure: |
399 | case scm_tc3_tc7_1: | |
400 | case scm_tc3_tc7_2: | |
401 | /* These tc3 tags should never occur in an immediate value. They are | |
402 | * only used in cell types of non-immediates, i. e. the value returned | |
403 | * by SCM_CELL_TYPE (exp) can use these tags. | |
404 | */ | |
405 | scm_ipruk ("immediate", exp, port); | |
406 | break; | |
407 | case scm_tc3_int_1: | |
408 | case scm_tc3_int_2: | |
0f2d19dd JB |
409 | scm_intprint (SCM_INUM (exp), 10, port); |
410 | break; | |
e34f941a | 411 | case scm_tc3_imm24: |
7866a09b | 412 | if (SCM_CHARP (exp)) |
0f2d19dd | 413 | { |
e34f941a | 414 | long i = SCM_CHAR (exp); |
5ca6dc39 | 415 | |
b7f3516f TT |
416 | if (SCM_WRITINGP (pstate)) |
417 | { | |
418 | scm_puts ("#\\", port); | |
419 | if ((i >= 0) && (i <= ' ') && scm_charnames[i]) | |
420 | scm_puts (scm_charnames[i], port); | |
57b74422 GH |
421 | #ifndef EBCDIC |
422 | else if (i == '\177') | |
423 | scm_puts (scm_charnames[scm_n_charnames - 1], port); | |
424 | #endif | |
b7f3516f TT |
425 | else if (i < 0 || i > '\177') |
426 | scm_intprint (i, 8, port); | |
427 | else | |
428 | scm_putc (i, port); | |
429 | } | |
430 | else | |
431 | scm_putc (i, port); | |
0f2d19dd | 432 | } |
a51ea417 | 433 | else if (SCM_IFLAGP (exp) |
5ca6dc39 | 434 | && ((size_t) SCM_ISYMNUM (exp) < (sizeof scm_isymnames / sizeof (char *)))) |
b7f3516f | 435 | scm_puts (SCM_ISYMCHARS (exp), port); |
0f2d19dd JB |
436 | else if (SCM_ILOCP (exp)) |
437 | { | |
b7f3516f | 438 | scm_puts ("#@", port); |
1be6b49c | 439 | scm_intprint ((long) SCM_IFRAME (exp), 10, port); |
b7f3516f | 440 | scm_putc (SCM_ICDRP (exp) ? '-' : '+', port); |
1be6b49c | 441 | scm_intprint ((long) SCM_IDIST (exp), 10, port); |
0f2d19dd JB |
442 | } |
443 | else | |
e34f941a DH |
444 | { |
445 | /* unknown immediate value */ | |
446 | scm_ipruk ("immediate", exp, port); | |
447 | } | |
0f2d19dd | 448 | break; |
e34f941a | 449 | case scm_tc3_cons: |
0f2d19dd JB |
450 | switch (SCM_TYP7 (exp)) |
451 | { | |
904a077d MV |
452 | case scm_tcs_struct: |
453 | { | |
454 | ENTER_NESTED_DATA (pstate, exp, circref); | |
455 | if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS) | |
456 | { | |
457 | SCM pwps, print = pstate->writingp ? g_write : g_display; | |
458 | if (!print) | |
459 | goto print_struct; | |
460 | SCM_NEWSMOB (pwps, | |
461 | scm_tc16_port_with_ps, | |
462 | SCM_UNPACK (scm_cons (port, pstate->handle))); | |
7663c008 | 463 | pstate->revealed = 1; |
904a077d MV |
464 | scm_call_generic_2 (print, exp, pwps); |
465 | } | |
466 | else | |
467 | { | |
468 | print_struct: | |
469 | scm_print_struct (exp, port, pstate); | |
470 | } | |
471 | EXIT_NESTED_DATA (pstate); | |
472 | } | |
473 | break; | |
0f2d19dd JB |
474 | case scm_tcs_cons_imcar: |
475 | case scm_tcs_cons_nimcar: | |
c62fbfe1 MD |
476 | ENTER_NESTED_DATA (pstate, exp, circref); |
477 | scm_iprlist ("(", exp, ')', port, pstate); | |
478 | EXIT_NESTED_DATA (pstate); | |
a51ea417 MD |
479 | break; |
480 | circref: | |
c62fbfe1 | 481 | print_circref (port, pstate, exp); |
0f2d19dd JB |
482 | break; |
483 | case scm_tcs_closures: | |
bb35f315 MV |
484 | if (SCM_FALSEP (scm_procedure_p (SCM_PRINT_CLOSURE)) |
485 | || SCM_FALSEP (scm_printer_apply (SCM_PRINT_CLOSURE, | |
77c25af7 | 486 | exp, port, pstate))) |
726d810a DH |
487 | { |
488 | SCM formals = SCM_CLOSURE_FORMALS (exp); | |
489 | scm_puts ("#<procedure", port); | |
490 | scm_putc (' ', port); | |
491 | scm_iprin1 (scm_procedure_name (exp), port, pstate); | |
492 | scm_putc (' ', port); | |
493 | if (SCM_PRINT_SOURCE_P) | |
494 | { | |
495 | SCM env = SCM_ENV (exp); | |
496 | SCM xenv = SCM_EXTEND_ENV (formals, SCM_EOL, env); | |
497 | SCM src = scm_unmemocopy (SCM_CODE (exp), xenv); | |
498 | ENTER_NESTED_DATA (pstate, exp, circref); | |
499 | scm_iprin1 (src, port, pstate); | |
500 | EXIT_NESTED_DATA (pstate); | |
501 | } | |
502 | else | |
503 | scm_iprin1 (formals, port, pstate); | |
b7f3516f | 504 | scm_putc ('>', port); |
726d810a | 505 | } |
0f2d19dd | 506 | break; |
0f2d19dd | 507 | case scm_tc7_string: |
c62fbfe1 | 508 | if (SCM_WRITINGP (pstate)) |
0f2d19dd | 509 | { |
1be6b49c | 510 | size_t i; |
5ca6dc39 | 511 | |
b7f3516f | 512 | scm_putc ('"', port); |
d1ca2c64 | 513 | for (i = 0; i < SCM_STRING_LENGTH (exp); ++i) |
34f0f2b8 | 514 | switch (SCM_STRING_CHARS (exp)[i]) |
0f2d19dd | 515 | { |
dbef8851 | 516 | case '"': |
0f2d19dd | 517 | case '\\': |
b7f3516f | 518 | scm_putc ('\\', port); |
0f2d19dd | 519 | default: |
34f0f2b8 | 520 | scm_putc (SCM_STRING_CHARS (exp)[i], port); |
0f2d19dd | 521 | } |
b7f3516f | 522 | scm_putc ('"', port); |
0f2d19dd JB |
523 | break; |
524 | } | |
525 | else | |
34f0f2b8 | 526 | scm_lfwrite (SCM_STRING_CHARS (exp), SCM_STRING_LENGTH (exp), port); |
0f2d19dd | 527 | break; |
28b06554 | 528 | case scm_tc7_symbol: |
9ff28a13 MV |
529 | if (SCM_SYMBOL_INTERNED_P (exp)) |
530 | { | |
531 | scm_print_symbol_name (SCM_SYMBOL_CHARS (exp), | |
532 | SCM_SYMBOL_LENGTH (exp), | |
533 | port); | |
534 | scm_remember_upto_here_1 (exp); | |
535 | } | |
536 | else | |
537 | { | |
538 | scm_puts ("#<uninterned-symbol ", port); | |
539 | scm_print_symbol_name (SCM_SYMBOL_CHARS (exp), | |
540 | SCM_SYMBOL_LENGTH (exp), | |
541 | port); | |
542 | scm_putc (' ', port); | |
543 | scm_intprint ((long)exp, 16, port); | |
544 | scm_putc ('>', port); | |
545 | } | |
6662998f | 546 | break; |
e5aca4b5 MV |
547 | case scm_tc7_variable: |
548 | scm_i_variable_print (exp, port, pstate); | |
549 | break; | |
0f2d19dd | 550 | case scm_tc7_wvect: |
c62fbfe1 | 551 | ENTER_NESTED_DATA (pstate, exp, circref); |
0f2d19dd | 552 | if (SCM_IS_WHVEC (exp)) |
b7f3516f | 553 | scm_puts ("#wh(", port); |
0f2d19dd | 554 | else |
b7f3516f | 555 | scm_puts ("#w(", port); |
0f2d19dd JB |
556 | goto common_vector_printer; |
557 | ||
558 | case scm_tc7_vector: | |
c62fbfe1 | 559 | ENTER_NESTED_DATA (pstate, exp, circref); |
b7f3516f | 560 | scm_puts ("#(", port); |
0f2d19dd | 561 | common_vector_printer: |
9fbaf27c | 562 | { |
c014a02e ML |
563 | register long i; |
564 | long last = SCM_VECTOR_LENGTH (exp) - 1; | |
9fbaf27c | 565 | int cutp = 0; |
b17004b8 | 566 | if (pstate->fancyp && SCM_VECTOR_LENGTH (exp) > pstate->length) |
9fbaf27c MD |
567 | { |
568 | last = pstate->length - 1; | |
569 | cutp = 1; | |
570 | } | |
571 | for (i = 0; i < last; ++i) | |
572 | { | |
573 | /* CHECK_INTS; */ | |
574 | scm_iprin1 (SCM_VELTS (exp)[i], port, pstate); | |
b7f3516f | 575 | scm_putc (' ', port); |
9fbaf27c MD |
576 | } |
577 | if (i == last) | |
578 | { | |
579 | /* CHECK_INTS; */ | |
580 | scm_iprin1 (SCM_VELTS (exp)[i], port, pstate); | |
581 | } | |
582 | if (cutp) | |
b7f3516f TT |
583 | scm_puts (" ...", port); |
584 | scm_putc (')', port); | |
9fbaf27c | 585 | } |
c62fbfe1 | 586 | EXIT_NESTED_DATA (pstate); |
0f2d19dd | 587 | break; |
afe5177e | 588 | #ifdef HAVE_ARRAYS |
0f2d19dd JB |
589 | case scm_tc7_bvect: |
590 | case scm_tc7_byvect: | |
591 | case scm_tc7_svect: | |
592 | case scm_tc7_ivect: | |
593 | case scm_tc7_uvect: | |
594 | case scm_tc7_fvect: | |
595 | case scm_tc7_dvect: | |
596 | case scm_tc7_cvect: | |
5c11cc9d | 597 | #ifdef HAVE_LONG_LONGS |
0f2d19dd JB |
598 | case scm_tc7_llvect: |
599 | #endif | |
c62fbfe1 | 600 | scm_raprin1 (exp, port, pstate); |
0f2d19dd | 601 | break; |
afe5177e | 602 | #endif |
0f2d19dd | 603 | case scm_tcs_subrs: |
9de33deb MD |
604 | scm_puts (SCM_SUBR_GENERIC (exp) && *SCM_SUBR_GENERIC (exp) |
605 | ? "#<primitive-generic " | |
606 | : "#<primitive-procedure ", | |
607 | port); | |
a002f1a2 | 608 | scm_puts (SCM_SYMBOL_CHARS (SCM_SNAME (exp)), port); |
b7f3516f | 609 | scm_putc ('>', port); |
0f2d19dd JB |
610 | break; |
611 | #ifdef CCLO | |
612 | case scm_tc7_cclo: | |
56977059 MD |
613 | { |
614 | SCM proc = SCM_CCLO_SUBR (exp); | |
54778cd3 | 615 | if (SCM_EQ_P (proc, scm_f_gsubr_apply)) |
56977059 MD |
616 | { |
617 | /* Print gsubrs as primitives */ | |
c180dfd3 | 618 | SCM name = scm_procedure_name (exp); |
56977059 | 619 | scm_puts ("#<primitive-procedure", port); |
d5cf5324 | 620 | if (!SCM_FALSEP (name)) |
56977059 MD |
621 | { |
622 | scm_putc (' ', port); | |
a002f1a2 | 623 | scm_puts (SCM_SYMBOL_CHARS (name), port); |
56977059 MD |
624 | } |
625 | } | |
626 | else | |
627 | { | |
628 | scm_puts ("#<compiled-closure ", port); | |
629 | scm_iprin1 (proc, port, pstate); | |
630 | } | |
631 | scm_putc ('>', port); | |
632 | } | |
0f2d19dd JB |
633 | break; |
634 | #endif | |
c180dfd3 MD |
635 | case scm_tc7_pws: |
636 | scm_puts ("#<procedure-with-setter", port); | |
637 | { | |
638 | SCM name = scm_procedure_name (exp); | |
d5cf5324 | 639 | if (!SCM_FALSEP (name)) |
c180dfd3 MD |
640 | { |
641 | scm_putc (' ', port); | |
b24b5e13 | 642 | scm_display (name, port); |
c180dfd3 MD |
643 | } |
644 | } | |
645 | scm_putc ('>', port); | |
646 | break; | |
0f2d19dd | 647 | case scm_tc7_port: |
5ca6dc39 JB |
648 | { |
649 | register long i = SCM_PTOBNUM (exp); | |
650 | if (i < scm_numptob | |
651 | && scm_ptobs[i].print | |
652 | && (scm_ptobs[i].print) (exp, port, pstate)) | |
a51ea417 | 653 | break; |
5ca6dc39 JB |
654 | goto punk; |
655 | } | |
656 | case scm_tc7_smob: | |
7a7f7c53 DH |
657 | ENTER_NESTED_DATA (pstate, exp, circref); |
658 | SCM_SMOB_DESCRIPTOR (exp).print (exp, port, pstate); | |
659 | EXIT_NESTED_DATA (pstate); | |
660 | break; | |
0f2d19dd | 661 | default: |
a51ea417 MD |
662 | punk: |
663 | scm_ipruk ("type", exp, port); | |
0f2d19dd JB |
664 | } |
665 | } | |
666 | } | |
667 | ||
c62fbfe1 MD |
668 | /* Print states are necessary for circular reference safe printing. |
669 | * They are also expensive to allocate. Therefore print states are | |
670 | * kept in a pool so that they can be reused. | |
671 | */ | |
1cc91f1b | 672 | |
bb35f315 MV |
673 | /* The PORT argument can also be a print-state/port pair, which will |
674 | * then be used instead of allocating a new print state. This is | |
675 | * useful for continuing a chain of print calls from Scheme. */ | |
676 | ||
a51ea417 | 677 | void |
1bbd0b84 | 678 | scm_prin1 (SCM exp, SCM port, int writingp) |
a51ea417 | 679 | { |
c4f37e80 MV |
680 | SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */ |
681 | SCM pstate_scm; | |
c62fbfe1 MD |
682 | scm_print_state *pstate; |
683 | ||
bb35f315 MV |
684 | /* If PORT is a print-state/port pair, use that. Else create a new |
685 | print-state. */ | |
c4f37e80 | 686 | |
0c95b57d | 687 | if (SCM_PORT_WITH_PS_P (port)) |
bb35f315 | 688 | { |
52235e71 MD |
689 | pstate_scm = SCM_PORT_WITH_PS_PS (port); |
690 | port = SCM_PORT_WITH_PS_PORT (port); | |
bb35f315 MV |
691 | } |
692 | else | |
c62fbfe1 | 693 | { |
c4f37e80 MV |
694 | /* First try to allocate a print state from the pool */ |
695 | SCM_DEFER_INTS; | |
d5cf5324 | 696 | if (!SCM_NULLP (print_state_pool)) |
c4f37e80 | 697 | { |
d5cf5324 DH |
698 | handle = print_state_pool; |
699 | print_state_pool = SCM_CDR (print_state_pool); | |
c4f37e80 MV |
700 | } |
701 | SCM_ALLOW_INTS; | |
54778cd3 | 702 | if (SCM_FALSEP (handle)) |
d5cf5324 | 703 | handle = scm_list_1 (make_print_state ()); |
c4f37e80 | 704 | pstate_scm = SCM_CAR (handle); |
c62fbfe1 | 705 | } |
c62fbfe1 | 706 | |
c4f37e80 | 707 | pstate = SCM_PRINT_STATE (pstate_scm); |
c62fbfe1 MD |
708 | pstate->writingp = writingp; |
709 | scm_iprin1 (exp, port, pstate); | |
710 | ||
bb35f315 MV |
711 | /* Return print state to pool if it has been created above and |
712 | hasn't escaped to Scheme. */ | |
713 | ||
54778cd3 | 714 | if (!SCM_FALSEP (handle) && !pstate->revealed) |
c4f37e80 MV |
715 | { |
716 | SCM_DEFER_INTS; | |
d5cf5324 DH |
717 | SCM_SETCDR (handle, print_state_pool); |
718 | print_state_pool = handle; | |
c4f37e80 MV |
719 | SCM_ALLOW_INTS; |
720 | } | |
a51ea417 MD |
721 | } |
722 | ||
0f2d19dd JB |
723 | |
724 | /* Print an integer. | |
725 | */ | |
1cc91f1b | 726 | |
0f2d19dd | 727 | void |
1bbd0b84 | 728 | scm_intprint (long n, int radix, SCM port) |
0f2d19dd JB |
729 | { |
730 | char num_buf[SCM_INTBUFLEN]; | |
b7f3516f | 731 | scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port); |
0f2d19dd JB |
732 | } |
733 | ||
734 | /* Print an object of unrecognized type. | |
735 | */ | |
1cc91f1b | 736 | |
0f2d19dd | 737 | void |
1bbd0b84 | 738 | scm_ipruk (char *hdr, SCM ptr, SCM port) |
0f2d19dd | 739 | { |
b7f3516f TT |
740 | scm_puts ("#<unknown-", port); |
741 | scm_puts (hdr, port); | |
c8a1bdc4 | 742 | if (scm_in_heap_p (ptr)) |
0f2d19dd | 743 | { |
b7f3516f | 744 | scm_puts (" (0x", port); |
54778cd3 | 745 | scm_intprint (SCM_CELL_WORD_0 (ptr), 16, port); |
b7f3516f | 746 | scm_puts (" . 0x", port); |
54778cd3 | 747 | scm_intprint (SCM_CELL_WORD_1 (ptr), 16, port); |
b7f3516f | 748 | scm_puts (") @", port); |
0f2d19dd | 749 | } |
b7f3516f | 750 | scm_puts (" 0x", port); |
54778cd3 | 751 | scm_intprint (SCM_UNPACK (ptr), 16, port); |
b7f3516f | 752 | scm_putc ('>', port); |
0f2d19dd JB |
753 | } |
754 | ||
1cc91f1b | 755 | |
904a077d | 756 | /* Print a list. |
22a52da1 | 757 | */ |
0f2d19dd | 758 | void |
34d19ef6 | 759 | scm_iprlist (char *hdr, SCM exp, int tlr, SCM port, scm_print_state *pstate) |
0f2d19dd | 760 | { |
c62fbfe1 | 761 | register SCM hare, tortoise; |
c014a02e | 762 | long floor = pstate->top - 2; |
b7f3516f | 763 | scm_puts (hdr, port); |
0f2d19dd | 764 | /* CHECK_INTS; */ |
c62fbfe1 MD |
765 | if (pstate->fancyp) |
766 | goto fancy_printing; | |
767 | ||
768 | /* Run a hare and tortoise so that total time complexity will be | |
769 | O(depth * N) instead of O(N^2). */ | |
770 | hare = SCM_CDR (exp); | |
771 | tortoise = exp; | |
904a077d | 772 | while (SCM_CONSP (hare)) |
c62fbfe1 | 773 | { |
54778cd3 | 774 | if (SCM_EQ_P (hare, tortoise)) |
c62fbfe1 MD |
775 | goto fancy_printing; |
776 | hare = SCM_CDR (hare); | |
d5cf5324 | 777 | if (!SCM_CONSP (hare)) |
c62fbfe1 MD |
778 | break; |
779 | hare = SCM_CDR (hare); | |
780 | tortoise = SCM_CDR (tortoise); | |
781 | } | |
782 | ||
783 | /* No cdr cycles intrinsic to this list */ | |
784 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
904a077d | 785 | for (exp = SCM_CDR (exp); SCM_CONSP (exp); exp = SCM_CDR (exp)) |
0f2d19dd | 786 | { |
c014a02e | 787 | register long i; |
5ca6dc39 | 788 | |
c62fbfe1 | 789 | for (i = floor; i >= 0; --i) |
230d095f | 790 | if (SCM_EQ_P (pstate->ref_stack[i], exp)) |
c62fbfe1 MD |
791 | goto circref; |
792 | PUSH_REF (pstate, exp); | |
b7f3516f | 793 | scm_putc (' ', port); |
0f2d19dd | 794 | /* CHECK_INTS; */ |
c62fbfe1 | 795 | scm_iprin1 (SCM_CAR (exp), port, pstate); |
0f2d19dd | 796 | } |
c96d76b8 | 797 | if (!SCM_NULL_OR_NIL_P (exp)) |
0f2d19dd | 798 | { |
b7f3516f | 799 | scm_puts (" . ", port); |
c62fbfe1 | 800 | scm_iprin1 (exp, port, pstate); |
0f2d19dd | 801 | } |
c62fbfe1 | 802 | |
a51ea417 | 803 | end: |
b7f3516f | 804 | scm_putc (tlr, port); |
c62fbfe1 | 805 | pstate->top = floor + 2; |
a51ea417 | 806 | return; |
c62fbfe1 MD |
807 | |
808 | fancy_printing: | |
809 | { | |
c014a02e | 810 | long n = pstate->length; |
c62fbfe1 MD |
811 | |
812 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
813 | exp = SCM_CDR (exp); --n; | |
904a077d | 814 | for (; SCM_CONSP (exp); exp = SCM_CDR (exp)) |
c62fbfe1 | 815 | { |
c014a02e | 816 | register unsigned long i; |
5ca6dc39 | 817 | |
c62fbfe1 | 818 | for (i = 0; i < pstate->top; ++i) |
230d095f | 819 | if (SCM_EQ_P (pstate->ref_stack[i], exp)) |
c62fbfe1 MD |
820 | goto fancy_circref; |
821 | if (pstate->fancyp) | |
822 | { | |
823 | if (n == 0) | |
824 | { | |
b7f3516f | 825 | scm_puts (" ...", port); |
c62fbfe1 MD |
826 | goto skip_tail; |
827 | } | |
828 | else | |
829 | --n; | |
830 | } | |
831 | PUSH_REF(pstate, exp); | |
832 | ++pstate->list_offset; | |
b7f3516f | 833 | scm_putc (' ', port); |
c62fbfe1 MD |
834 | /* CHECK_INTS; */ |
835 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
836 | } | |
837 | } | |
c96d76b8 | 838 | if (!SCM_NULL_OR_NIL_P (exp)) |
c62fbfe1 | 839 | { |
b7f3516f | 840 | scm_puts (" . ", port); |
c62fbfe1 MD |
841 | scm_iprin1 (exp, port, pstate); |
842 | } | |
843 | skip_tail: | |
844 | pstate->list_offset -= pstate->top - floor - 2; | |
a51ea417 | 845 | goto end; |
a51ea417 | 846 | |
c62fbfe1 MD |
847 | fancy_circref: |
848 | pstate->list_offset -= pstate->top - floor - 2; | |
849 | ||
850 | circref: | |
b7f3516f | 851 | scm_puts (" . ", port); |
c62fbfe1 MD |
852 | print_circref (port, pstate, exp); |
853 | goto end; | |
0f2d19dd JB |
854 | } |
855 | ||
856 | \f | |
857 | ||
bb35f315 MV |
858 | int |
859 | scm_valid_oport_value_p (SCM val) | |
860 | { | |
368cf54d GB |
861 | return (SCM_OPOUTPORTP (val) |
862 | || (SCM_PORT_WITH_PS_P (val) | |
863 | && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val)))); | |
bb35f315 MV |
864 | } |
865 | ||
8b840115 | 866 | /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */ |
1cc91f1b | 867 | |
0f2d19dd | 868 | SCM |
1bbd0b84 | 869 | scm_write (SCM obj, SCM port) |
0f2d19dd JB |
870 | { |
871 | if (SCM_UNBNDP (port)) | |
872 | port = scm_cur_outp; | |
3eb7e6ee JB |
873 | |
874 | SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write); | |
bb35f315 | 875 | |
a51ea417 | 876 | scm_prin1 (obj, port, 1); |
0f2d19dd JB |
877 | #ifdef HAVE_PIPE |
878 | # ifdef EPIPE | |
879 | if (EPIPE == errno) | |
880 | scm_close_port (port); | |
881 | # endif | |
882 | #endif | |
883 | return SCM_UNSPECIFIED; | |
884 | } | |
885 | ||
886 | ||
8b840115 | 887 | /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */ |
1cc91f1b | 888 | |
0f2d19dd | 889 | SCM |
1bbd0b84 | 890 | scm_display (SCM obj, SCM port) |
0f2d19dd JB |
891 | { |
892 | if (SCM_UNBNDP (port)) | |
893 | port = scm_cur_outp; | |
3eb7e6ee JB |
894 | |
895 | SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display); | |
bb35f315 | 896 | |
a51ea417 | 897 | scm_prin1 (obj, port, 0); |
0f2d19dd JB |
898 | #ifdef HAVE_PIPE |
899 | # ifdef EPIPE | |
900 | if (EPIPE == errno) | |
901 | scm_close_port (port); | |
902 | # endif | |
903 | #endif | |
904 | return SCM_UNSPECIFIED; | |
905 | } | |
906 | ||
70d63753 GB |
907 | |
908 | SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1, | |
909 | (SCM destination, SCM message, SCM args), | |
eca65e90 MG |
910 | "Write @var{message} to @var{destination}, defaulting to\n" |
911 | "the current output port.\n" | |
912 | "@var{message} can contain @code{~A} (was @code{%s}) and\n" | |
913 | "@code{~S} (was @code{%S}) escapes. When printed,\n" | |
914 | "the escapes are replaced with corresponding members of\n" | |
915 | "@var{ARGS}:\n" | |
916 | "@code{~A} formats using @code{display} and @code{~S} formats\n" | |
917 | "using @code{write}.\n" | |
918 | "If @var{destination} is @code{#t}, then use the current output\n" | |
919 | "port, if @var{destination} is @code{#f}, then return a string\n" | |
920 | "containing the formatted text. Does not add a trailing newline.") | |
70d63753 GB |
921 | #define FUNC_NAME s_scm_simple_format |
922 | { | |
923 | SCM answer = SCM_UNSPECIFIED; | |
924 | int fReturnString = 0; | |
925 | int writingp; | |
926 | char *start; | |
b24b5e13 | 927 | char *end; |
70d63753 GB |
928 | char *p; |
929 | ||
daba1a71 MD |
930 | if (SCM_EQ_P (destination, SCM_BOOL_T)) |
931 | { | |
932 | destination = scm_cur_outp; | |
933 | } | |
934 | else if (SCM_FALSEP (destination)) | |
935 | { | |
936 | fReturnString = 1; | |
937 | destination = scm_mkstrport (SCM_INUM0, | |
938 | scm_make_string (SCM_INUM0, SCM_UNDEFINED), | |
939 | SCM_OPN | SCM_WRTNG, | |
940 | FUNC_NAME); | |
941 | } | |
942 | else | |
943 | { | |
944 | SCM_VALIDATE_OPORT_VALUE (1, destination); | |
945 | destination = SCM_COERCE_OUTPORT (destination); | |
946 | } | |
947 | SCM_VALIDATE_STRING (2, message); | |
af45e3b0 | 948 | SCM_VALIDATE_REST_ARGUMENT (args); |
70d63753 | 949 | |
34f0f2b8 | 950 | start = SCM_STRING_CHARS (message); |
b24b5e13 DH |
951 | end = start + SCM_STRING_LENGTH (message); |
952 | for (p = start; p != end; ++p) | |
70d63753 GB |
953 | if (*p == '~') |
954 | { | |
b24b5e13 | 955 | if (++p == end) |
6662998f MV |
956 | break; |
957 | ||
958 | switch (*p) | |
959 | { | |
960 | case 'A': case 'a': | |
961 | writingp = 0; | |
962 | break; | |
963 | case 'S': case 's': | |
964 | writingp = 1; | |
965 | break; | |
966 | case '~': | |
967 | scm_lfwrite (start, p - start, destination); | |
968 | start = p + 1; | |
969 | continue; | |
970 | case '%': | |
ba427176 | 971 | scm_lfwrite (start, p - start - 1, destination); |
6662998f MV |
972 | scm_newline (destination); |
973 | start = p + 1; | |
974 | continue; | |
975 | default: | |
1afff620 KN |
976 | SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead", |
977 | scm_list_1 (SCM_MAKE_CHAR (*p))); | |
6662998f MV |
978 | |
979 | } | |
70d63753 | 980 | |
6662998f MV |
981 | |
982 | if (!SCM_CONSP (args)) | |
1afff620 KN |
983 | SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A", |
984 | scm_list_1 (SCM_MAKE_CHAR (*p))); | |
6662998f | 985 | |
70d63753 GB |
986 | scm_lfwrite (start, p - start - 1, destination); |
987 | scm_prin1 (SCM_CAR (args), destination, writingp); | |
988 | args = SCM_CDR (args); | |
989 | start = p + 1; | |
990 | } | |
6662998f | 991 | |
70d63753 | 992 | scm_lfwrite (start, p - start, destination); |
d5cf5324 | 993 | if (!SCM_EQ_P (args, SCM_EOL)) |
1afff620 KN |
994 | SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments", |
995 | scm_list_1 (scm_length (args))); | |
70d63753 GB |
996 | |
997 | if (fReturnString) | |
998 | answer = scm_strport_to_string (destination); | |
999 | ||
daba1a71 | 1000 | return scm_return_first (answer, message); |
70d63753 GB |
1001 | } |
1002 | #undef FUNC_NAME | |
1003 | ||
1004 | ||
3b3b36dd | 1005 | SCM_DEFINE (scm_newline, "newline", 0, 1, 0, |
b450f070 | 1006 | (SCM port), |
8f85c0c6 NJ |
1007 | "Send a newline to @var{port}.\n" |
1008 | "If @var{port} is omitted, send to the current output port.") | |
1bbd0b84 | 1009 | #define FUNC_NAME s_scm_newline |
0f2d19dd JB |
1010 | { |
1011 | if (SCM_UNBNDP (port)) | |
bb35f315 | 1012 | port = scm_cur_outp; |
3eb7e6ee | 1013 | |
34d19ef6 | 1014 | SCM_VALIDATE_OPORT_VALUE (1, port); |
bb35f315 | 1015 | |
0ef4ae82 | 1016 | scm_putc ('\n', SCM_COERCE_OUTPORT (port)); |
0f2d19dd JB |
1017 | return SCM_UNSPECIFIED; |
1018 | } | |
1bbd0b84 | 1019 | #undef FUNC_NAME |
0f2d19dd | 1020 | |
3b3b36dd | 1021 | SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0, |
b450f070 | 1022 | (SCM chr, SCM port), |
eca65e90 | 1023 | "Send character @var{chr} to @var{port}.") |
1bbd0b84 | 1024 | #define FUNC_NAME s_scm_write_char |
0f2d19dd JB |
1025 | { |
1026 | if (SCM_UNBNDP (port)) | |
bb35f315 | 1027 | port = scm_cur_outp; |
3eb7e6ee | 1028 | |
34d19ef6 HWN |
1029 | SCM_VALIDATE_CHAR (1, chr); |
1030 | SCM_VALIDATE_OPORT_VALUE (2, port); | |
bb35f315 | 1031 | |
7866a09b | 1032 | scm_putc ((int) SCM_CHAR (chr), SCM_COERCE_OUTPORT (port)); |
0f2d19dd JB |
1033 | #ifdef HAVE_PIPE |
1034 | # ifdef EPIPE | |
1035 | if (EPIPE == errno) | |
1036 | scm_close_port (port); | |
1037 | # endif | |
1038 | #endif | |
1039 | return SCM_UNSPECIFIED; | |
1040 | } | |
1bbd0b84 | 1041 | #undef FUNC_NAME |
0f2d19dd | 1042 | |
0f2d19dd JB |
1043 | \f |
1044 | ||
bb35f315 | 1045 | /* Call back to Scheme code to do the printing of special objects |
c19bc088 MD |
1046 | * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob |
1047 | * containing PORT and PSTATE. This object can be used as the port for | |
1048 | * display/write etc to continue the current print chain. The REVEALED | |
1049 | * field of PSTATE is set to true to indicate that the print state has | |
1050 | * escaped to Scheme and thus has to be freed by the GC. | |
1051 | */ | |
1052 | ||
92c2555f | 1053 | scm_t_bits scm_tc16_port_with_ps; |
c19bc088 MD |
1054 | |
1055 | /* Print exactly as the port itself would */ | |
1056 | ||
1057 | static int | |
e841c3e0 | 1058 | port_with_ps_print (SCM obj, SCM port, scm_print_state *pstate) |
c19bc088 MD |
1059 | { |
1060 | obj = SCM_PORT_WITH_PS_PORT (obj); | |
1061 | return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate); | |
1062 | } | |
c4f37e80 MV |
1063 | |
1064 | SCM | |
1bbd0b84 | 1065 | scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate) |
c4f37e80 | 1066 | { |
c19bc088 | 1067 | SCM pwps; |
bb35f315 | 1068 | SCM pair = scm_cons (port, pstate->handle); |
54778cd3 | 1069 | SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, SCM_UNPACK (pair)); |
bb35f315 | 1070 | pstate->revealed = 1; |
fdc28395 | 1071 | return scm_call_2 (proc, exp, pwps); |
c19bc088 MD |
1072 | } |
1073 | ||
a1ec6916 | 1074 | SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 2, 0, 0, |
1bbd0b84 | 1075 | (SCM port, SCM pstate), |
71331188 MG |
1076 | "Create a new port which behaves like @var{port}, but with an\n" |
1077 | "included print state @var{pstate}.") | |
1bbd0b84 | 1078 | #define FUNC_NAME s_scm_port_with_print_state |
c19bc088 MD |
1079 | { |
1080 | SCM pwps; | |
34d19ef6 HWN |
1081 | SCM_VALIDATE_OPORT_VALUE (1, port); |
1082 | SCM_VALIDATE_PRINTSTATE (2, pstate); | |
c19bc088 | 1083 | port = SCM_COERCE_OUTPORT (port); |
54778cd3 | 1084 | SCM_NEWSMOB (pwps, scm_tc16_port_with_ps, SCM_UNPACK (scm_cons (port, pstate))); |
c19bc088 MD |
1085 | return pwps; |
1086 | } | |
1bbd0b84 | 1087 | #undef FUNC_NAME |
c19bc088 | 1088 | |
a1ec6916 | 1089 | SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0, |
1bbd0b84 | 1090 | (SCM port), |
71331188 MG |
1091 | "Return the print state of the port @var{port}. If @var{port}\n" |
1092 | "has no associated print state, @code{#f} is returned.") | |
1bbd0b84 | 1093 | #define FUNC_NAME s_scm_get_print_state |
c19bc088 | 1094 | { |
368cf54d GB |
1095 | if (SCM_PORT_WITH_PS_P (port)) |
1096 | return SCM_PORT_WITH_PS_PS (port); | |
f5f2dcff | 1097 | if (SCM_OUTPUT_PORT_P (port)) |
368cf54d | 1098 | return SCM_BOOL_F; |
276dd677 | 1099 | SCM_WRONG_TYPE_ARG (1, port); |
c4f37e80 | 1100 | } |
1bbd0b84 | 1101 | #undef FUNC_NAME |
bb35f315 | 1102 | |
c4f37e80 | 1103 | \f |
1cc91f1b | 1104 | |
0f2d19dd JB |
1105 | void |
1106 | scm_init_print () | |
0f2d19dd | 1107 | { |
c19bc088 | 1108 | SCM vtable, layout, type; |
d5cf5324 | 1109 | |
b7ff98dd | 1110 | scm_init_opts (scm_print_options, scm_print_opts, SCM_N_PRINT_OPTIONS); |
d5cf5324 DH |
1111 | |
1112 | scm_gc_register_root (&print_state_pool); | |
1113 | scm_gc_register_root (&scm_print_state_vtable); | |
3ce4544c | 1114 | vtable = scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL); |
4bfdf158 | 1115 | layout = scm_make_struct_layout (scm_makfrom0str (SCM_PRINT_STATE_LAYOUT)); |
1afff620 | 1116 | type = scm_make_struct (vtable, SCM_INUM0, scm_list_1 (layout)); |
38ae064c | 1117 | scm_set_struct_vtable_name_x (type, scm_str2symbol ("print-state")); |
bb35f315 | 1118 | scm_print_state_vtable = type; |
c4f37e80 | 1119 | |
c19bc088 MD |
1120 | /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */ |
1121 | scm_tc16_port_with_ps = scm_make_smob_type (0, 0); | |
1122 | scm_set_smob_mark (scm_tc16_port_with_ps, scm_markcdr); | |
e841c3e0 | 1123 | scm_set_smob_print (scm_tc16_port_with_ps, port_with_ps_print); |
c19bc088 | 1124 | |
a0599745 | 1125 | #include "libguile/print.x" |
0f2d19dd | 1126 | } |
89e00824 ML |
1127 | |
1128 | /* | |
1129 | Local Variables: | |
1130 | c-file-style: "gnu" | |
1131 | End: | |
1132 | */ |