Commit | Line | Data |
---|---|---|
e20d7001 | 1 | /* Copyright (C) 1995-1999,2000,2001, 2002, 2003, 2004, 2006, 2008, 2009 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
4 | * modify it under the terms of the GNU Lesser General Public License |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
0f2d19dd | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
0f2d19dd | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
1bbd0b84 | 18 | |
1bbd0b84 | 19 | |
0f2d19dd | 20 | \f |
dbb605f5 LC |
21 | #ifdef HAVE_CONFIG_H |
22 | # include <config.h> | |
23 | #endif | |
0f2d19dd | 24 | |
e6e2e95a | 25 | #include <errno.h> |
eca29b02 | 26 | #include <uniconv.h> |
904a78f1 | 27 | #include <unictype.h> |
e6e2e95a | 28 | |
a0599745 MD |
29 | #include "libguile/_scm.h" |
30 | #include "libguile/chars.h" | |
a002f1a2 | 31 | #include "libguile/continuations.h" |
a0599745 MD |
32 | #include "libguile/smob.h" |
33 | #include "libguile/eval.h" | |
34 | #include "libguile/macros.h" | |
35 | #include "libguile/procprop.h" | |
36 | #include "libguile/read.h" | |
37 | #include "libguile/weaks.h" | |
2fb924f6 | 38 | #include "libguile/programs.h" |
a0599745 MD |
39 | #include "libguile/unif.h" |
40 | #include "libguile/alist.h" | |
41 | #include "libguile/struct.h" | |
42 | #include "libguile/objects.h" | |
43 | #include "libguile/ports.h" | |
44 | #include "libguile/root.h" | |
45 | #include "libguile/strings.h" | |
46 | #include "libguile/strports.h" | |
47 | #include "libguile/vectors.h" | |
c96d76b8 | 48 | #include "libguile/lang.h" |
327967ef | 49 | #include "libguile/numbers.h" |
a0599745 MD |
50 | |
51 | #include "libguile/validate.h" | |
52 | #include "libguile/print.h" | |
22fc179a HWN |
53 | |
54 | #include "libguile/private-options.h" | |
55 | ||
0f2d19dd JB |
56 | \f |
57 | ||
58 | /* {Names of immediate symbols} | |
59 | * | |
60 | * This table must agree with the declarations in scm.h: {Immediate Symbols}. | |
61 | */ | |
62 | ||
e17d318f DH |
63 | /* This table must agree with the list of flags in tags.h. */ |
64 | static const char *iflagnames[] = | |
65 | { | |
66 | "#f", | |
67 | "#t", | |
68 | "#<undefined>", | |
69 | "#<eof>", | |
70 | "()", | |
71 | "#<unspecified>", | |
72 | ||
73 | /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */ | |
74 | "#<unbound>", | |
75 | ||
76 | /* Elisp nil value. This is its Scheme name; whenever it's printed in | |
77 | * Elisp, it should appear as the symbol `nil'. */ | |
78 | "#nil" | |
79 | }; | |
80 | ||
475fa9a5 MV |
81 | SCM_SYMBOL (sym_reader, "reader"); |
82 | ||
92c2555f | 83 | scm_t_option scm_print_opts[] = { |
726d810a DH |
84 | { SCM_OPTION_SCM, "closure-hook", SCM_UNPACK (SCM_BOOL_F), |
85 | "Hook for printing closures (should handle macros as well)." }, | |
84f6a34a | 86 | { SCM_OPTION_BOOLEAN, "source", 0, |
81ae25da MV |
87 | "Print closures with source." }, |
88 | { SCM_OPTION_SCM, "highlight-prefix", (unsigned long)SCM_BOOL_F, | |
89 | "The string to print before highlighted values." }, | |
90 | { SCM_OPTION_SCM, "highlight-suffix", (unsigned long)SCM_BOOL_F, | |
475fa9a5 MV |
91 | "The string to print after highlighted values." }, |
92 | { SCM_OPTION_SCM, "quote-keywordish-symbols", (unsigned long)SCM_BOOL_F, | |
93 | "How to print symbols that have a colon as their first or last character. " | |
94 | "The value '#f' does not quote the colons; '#t' quotes them; " | |
95 | "'reader' quotes them when the reader option 'keywords' is not '#f'." | |
62560650 HWN |
96 | }, |
97 | { 0 }, | |
98 | ||
e6e4c9af MD |
99 | }; |
100 | ||
a1ec6916 | 101 | SCM_DEFINE (scm_print_options, "print-options-interface", 0, 1, 0, |
1bbd0b84 | 102 | (SCM setting), |
71331188 | 103 | "Option interface for the print options. Instead of using\n" |
1dd05fd8 MG |
104 | "this procedure directly, use the procedures\n" |
105 | "@code{print-enable}, @code{print-disable}, @code{print-set!}\n" | |
106 | "and @code{print-options}.") | |
1bbd0b84 | 107 | #define FUNC_NAME s_scm_print_options |
e6e4c9af | 108 | { |
a51ea417 | 109 | SCM ans = scm_options (setting, |
b7ff98dd | 110 | scm_print_opts, |
1bbd0b84 | 111 | FUNC_NAME); |
e6e4c9af MD |
112 | return ans; |
113 | } | |
1bbd0b84 | 114 | #undef FUNC_NAME |
e6e4c9af | 115 | |
0f2d19dd JB |
116 | \f |
117 | /* {Printing of Scheme Objects} | |
118 | */ | |
119 | ||
a51ea417 | 120 | /* Detection of circular references. |
c62fbfe1 MD |
121 | * |
122 | * Due to other constraints in the implementation, this code has bad | |
5d46ebe3 MD |
123 | * time complexity (O (depth * N)), The printer code can be |
124 | * rewritten to be O(N). | |
a51ea417 | 125 | */ |
dbb5de29 NJ |
126 | #define PUSH_REF(pstate, obj) \ |
127 | do \ | |
128 | { \ | |
129 | PSTATE_STACK_SET (pstate, pstate->top, obj); \ | |
130 | pstate->top++; \ | |
131 | if (pstate->top == pstate->ceiling) \ | |
132 | grow_ref_stack (pstate); \ | |
1bbd0b84 | 133 | } while(0) |
a51ea417 | 134 | |
dbb5de29 NJ |
135 | #define ENTER_NESTED_DATA(pstate, obj, label) \ |
136 | do \ | |
137 | { \ | |
138 | register unsigned long i; \ | |
139 | for (i = 0; i < pstate->top; ++i) \ | |
140 | if (scm_is_eq (PSTATE_STACK_REF (pstate, i), (obj))) \ | |
141 | goto label; \ | |
142 | if (pstate->fancyp) \ | |
143 | { \ | |
144 | if (pstate->top - pstate->list_offset >= pstate->level) \ | |
145 | { \ | |
146 | scm_putc ('#', port); \ | |
147 | return; \ | |
148 | } \ | |
149 | } \ | |
150 | PUSH_REF(pstate, obj); \ | |
1bbd0b84 | 151 | } while(0) |
a51ea417 | 152 | |
dbb5de29 NJ |
153 | #define EXIT_NESTED_DATA(pstate) \ |
154 | do \ | |
155 | { \ | |
156 | --pstate->top; \ | |
157 | PSTATE_STACK_SET (pstate, pstate->top, SCM_UNDEFINED); \ | |
158 | } \ | |
159 | while (0) | |
c62fbfe1 | 160 | |
d5cf5324 DH |
161 | SCM scm_print_state_vtable = SCM_BOOL_F; |
162 | static SCM print_state_pool = SCM_EOL; | |
9de87eea | 163 | scm_i_pthread_mutex_t print_state_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER; |
c4f37e80 | 164 | |
f843a84c | 165 | #ifdef GUILE_DEBUG /* Used for debugging purposes */ |
1cc91f1b | 166 | |
3b3b36dd | 167 | SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0, |
1bbd0b84 | 168 | (), |
d5cf5324 | 169 | "Return the current-pstate -- the car of the\n" |
5352393c MG |
170 | "@code{print_state_pool}. @code{current-pstate} is only\n" |
171 | "included in @code{--enable-guile-debug} builds.") | |
1bbd0b84 | 172 | #define FUNC_NAME s_scm_current_pstate |
c62fbfe1 | 173 | { |
d2e53ed6 | 174 | if (!scm_is_null (print_state_pool)) |
d5cf5324 | 175 | return SCM_CAR (print_state_pool); |
a0adfbf0 | 176 | else |
0a284a4e | 177 | return SCM_BOOL_F; |
c62fbfe1 | 178 | } |
1bbd0b84 GB |
179 | #undef FUNC_NAME |
180 | ||
c62fbfe1 MD |
181 | #endif |
182 | ||
183 | #define PSTATE_SIZE 50L | |
184 | ||
698c0295 | 185 | static SCM |
1bbd0b84 | 186 | make_print_state (void) |
698c0295 | 187 | { |
d5cf5324 DH |
188 | SCM print_state |
189 | = scm_make_struct (scm_print_state_vtable, SCM_INUM0, SCM_EOL); | |
bf685b6d | 190 | scm_print_state *pstate = SCM_PRINT_STATE (print_state); |
00ffa0e7 | 191 | pstate->ref_vect = scm_c_make_vector (PSTATE_SIZE, SCM_UNDEFINED); |
4057a3e0 | 192 | pstate->ceiling = SCM_SIMPLE_VECTOR_LENGTH (pstate->ref_vect); |
d232520a | 193 | pstate->highlight_objects = SCM_EOL; |
698c0295 MD |
194 | return print_state; |
195 | } | |
1cc91f1b | 196 | |
c62fbfe1 MD |
197 | SCM |
198 | scm_make_print_state () | |
c62fbfe1 | 199 | { |
230d095f | 200 | SCM answer = SCM_BOOL_F; |
698c0295 MD |
201 | |
202 | /* First try to allocate a print state from the pool */ | |
9de87eea | 203 | scm_i_pthread_mutex_lock (&print_state_mutex); |
d2e53ed6 | 204 | if (!scm_is_null (print_state_pool)) |
698c0295 | 205 | { |
d5cf5324 DH |
206 | answer = SCM_CAR (print_state_pool); |
207 | print_state_pool = SCM_CDR (print_state_pool); | |
698c0295 | 208 | } |
9de87eea | 209 | scm_i_pthread_mutex_unlock (&print_state_mutex); |
698c0295 | 210 | |
7888309b | 211 | return scm_is_false (answer) ? make_print_state () : answer; |
c62fbfe1 | 212 | } |
a51ea417 | 213 | |
698c0295 | 214 | void |
6e8d25a6 | 215 | scm_free_print_state (SCM print_state) |
698c0295 MD |
216 | { |
217 | SCM handle; | |
218 | scm_print_state *pstate = SCM_PRINT_STATE (print_state); | |
219 | /* Cleanup before returning print state to pool. | |
220 | * It is better to do it here. Doing it in scm_prin1 | |
221 | * would cost more since that function is called much more | |
222 | * often. | |
223 | */ | |
224 | pstate->fancyp = 0; | |
bb35f315 | 225 | pstate->revealed = 0; |
d232520a | 226 | pstate->highlight_objects = SCM_EOL; |
9de87eea | 227 | scm_i_pthread_mutex_lock (&print_state_mutex); |
16d4699b | 228 | handle = scm_cons (print_state, print_state_pool); |
d5cf5324 | 229 | print_state_pool = handle; |
9de87eea | 230 | scm_i_pthread_mutex_unlock (&print_state_mutex); |
dfd03fb9 MD |
231 | } |
232 | ||
233 | SCM | |
234 | scm_i_port_with_print_state (SCM port, SCM print_state) | |
235 | { | |
236 | if (SCM_UNBNDP (print_state)) | |
237 | { | |
238 | if (SCM_PORT_WITH_PS_P (port)) | |
239 | return port; | |
240 | else | |
241 | print_state = scm_make_print_state (); | |
242 | /* port does not need to be coerced since it doesn't have ps */ | |
243 | } | |
244 | else | |
245 | port = SCM_COERCE_OUTPORT (port); | |
246 | SCM_RETURN_NEWSMOB (scm_tc16_port_with_ps, | |
247 | SCM_UNPACK (scm_cons (port, print_state))); | |
698c0295 | 248 | } |
1cc91f1b | 249 | |
a51ea417 | 250 | static void |
1bbd0b84 | 251 | grow_ref_stack (scm_print_state *pstate) |
a51ea417 | 252 | { |
4057a3e0 MV |
253 | SCM old_vect = pstate->ref_vect; |
254 | size_t old_size = SCM_SIMPLE_VECTOR_LENGTH (old_vect); | |
255 | size_t new_size = 2 * pstate->ceiling; | |
00ffa0e7 | 256 | SCM new_vect = scm_c_make_vector (new_size, SCM_UNDEFINED); |
b17004b8 DH |
257 | unsigned long int i; |
258 | ||
259 | for (i = 0; i != old_size; ++i) | |
4057a3e0 | 260 | SCM_SIMPLE_VECTOR_SET (new_vect, i, SCM_SIMPLE_VECTOR_REF (old_vect, i)); |
b17004b8 DH |
261 | |
262 | pstate->ref_vect = new_vect; | |
bf685b6d | 263 | pstate->ceiling = new_size; |
a51ea417 MD |
264 | } |
265 | ||
509759dd MV |
266 | #define PSTATE_STACK_REF(p,i) SCM_SIMPLE_VECTOR_REF((p)->ref_vect, (i)) |
267 | #define PSTATE_STACK_SET(p,i,v) SCM_SIMPLE_VECTOR_SET((p)->ref_vect, (i), (v)) | |
1cc91f1b | 268 | |
a51ea417 | 269 | static void |
34d19ef6 | 270 | print_circref (SCM port, scm_print_state *pstate, SCM ref) |
a51ea417 | 271 | { |
c014a02e ML |
272 | register long i; |
273 | long self = pstate->top - 1; | |
c62fbfe1 | 274 | i = pstate->top - 1; |
509759dd | 275 | if (scm_is_pair (PSTATE_STACK_REF (pstate, i))) |
c62fbfe1 MD |
276 | { |
277 | while (i > 0) | |
278 | { | |
509759dd MV |
279 | if (!scm_is_pair (PSTATE_STACK_REF (pstate, i-1)) |
280 | || !scm_is_eq (SCM_CDR (PSTATE_STACK_REF (pstate, i-1)), | |
281 | SCM_CDR (PSTATE_STACK_REF (pstate, i)))) | |
c62fbfe1 MD |
282 | break; |
283 | --i; | |
284 | } | |
285 | self = i; | |
286 | } | |
287 | for (i = pstate->top - 1; 1; --i) | |
509759dd | 288 | if (scm_is_eq (PSTATE_STACK_REF(pstate, i), ref)) |
c62fbfe1 | 289 | break; |
b7f3516f | 290 | scm_putc ('#', port); |
c62fbfe1 | 291 | scm_intprint (i - self, 10, port); |
b7f3516f | 292 | scm_putc ('#', port); |
a51ea417 MD |
293 | } |
294 | ||
6662998f MV |
295 | /* Print the name of a symbol. */ |
296 | ||
475fa9a5 MV |
297 | static int |
298 | quote_keywordish_symbol (const char *str, size_t len) | |
299 | { | |
300 | SCM option; | |
301 | ||
302 | /* LEN is guaranteed to be > 0. | |
303 | */ | |
304 | if (str[0] != ':' && str[len-1] != ':') | |
305 | return 0; | |
306 | ||
307 | option = SCM_PRINT_KEYWORD_STYLE; | |
308 | if (scm_is_false (option)) | |
309 | return 0; | |
310 | if (scm_is_eq (option, sym_reader)) | |
311 | return scm_is_true (SCM_PACK (SCM_KEYWORD_STYLE)); | |
312 | return 1; | |
313 | } | |
314 | ||
6662998f MV |
315 | void |
316 | scm_print_symbol_name (const char *str, size_t len, SCM port) | |
317 | { | |
c6b49e89 MV |
318 | /* This points to the first character that has not yet been written to the |
319 | * port. */ | |
320 | size_t pos = 0; | |
321 | /* This points to the character we're currently looking at. */ | |
6662998f | 322 | size_t end; |
c6b49e89 MV |
323 | /* If the name contains weird characters, we'll escape them with |
324 | * backslashes and set this flag; it indicates that we should surround the | |
325 | * name with "#{" and "}#". */ | |
326 | int weird = 0; | |
327 | /* Backslashes are not sufficient to make a name weird, but if a name is | |
328 | * weird because of other characters, backslahes need to be escaped too. | |
329 | * The first time we see a backslash, we set maybe_weird, and mw_pos points | |
330 | * to the backslash. Then if the name turns out to be weird, we re-process | |
327967ef MV |
331 | * everything starting from mw_pos. |
332 | * We could instead make backslashes always weird. This is not necessary | |
333 | * to ensure that the output is (read)-able, but it would make this code | |
334 | * simpler and faster. */ | |
c6b49e89 | 335 | int maybe_weird = 0; |
6662998f | 336 | size_t mw_pos = 0; |
6662998f | 337 | |
475fa9a5 MV |
338 | if (len == 0 || str[0] == '\'' || str[0] == '`' || str[0] == ',' |
339 | || quote_keywordish_symbol (str, len) | |
340 | || (str[0] == '.' && len == 1) | |
2b829bbb | 341 | || scm_is_true (scm_c_locale_stringn_to_number (str, len, 10))) |
6662998f MV |
342 | { |
343 | scm_lfwrite ("#{", 2, port); | |
344 | weird = 1; | |
345 | } | |
c6b49e89 | 346 | |
6662998f MV |
347 | for (end = pos; end < len; ++end) |
348 | switch (str[end]) | |
349 | { | |
350 | #ifdef BRACKETS_AS_PARENS | |
351 | case '[': | |
352 | case ']': | |
353 | #endif | |
354 | case '(': | |
355 | case ')': | |
356 | case '"': | |
357 | case ';': | |
c6b49e89 | 358 | case '#': |
6662998f MV |
359 | case SCM_WHITE_SPACES: |
360 | case SCM_LINE_INCREMENTORS: | |
361 | weird_handler: | |
362 | if (maybe_weird) | |
363 | { | |
364 | end = mw_pos; | |
365 | maybe_weird = 0; | |
366 | } | |
367 | if (!weird) | |
368 | { | |
369 | scm_lfwrite ("#{", 2, port); | |
370 | weird = 1; | |
371 | } | |
372 | if (pos < end) | |
c6b49e89 | 373 | scm_lfwrite (str + pos, end - pos, port); |
6662998f MV |
374 | { |
375 | char buf[2]; | |
376 | buf[0] = '\\'; | |
377 | buf[1] = str[end]; | |
378 | scm_lfwrite (buf, 2, port); | |
379 | } | |
380 | pos = end + 1; | |
381 | break; | |
382 | case '\\': | |
383 | if (weird) | |
384 | goto weird_handler; | |
385 | if (!maybe_weird) | |
386 | { | |
387 | maybe_weird = 1; | |
388 | mw_pos = pos; | |
389 | } | |
390 | break; | |
6662998f MV |
391 | default: |
392 | break; | |
393 | } | |
394 | if (pos < end) | |
395 | scm_lfwrite (str + pos, end - pos, port); | |
396 | if (weird) | |
397 | scm_lfwrite ("}#", 2, port); | |
398 | } | |
399 | ||
c62fbfe1 | 400 | /* Print generally. Handles both write and display according to PSTATE. |
0f2d19dd | 401 | */ |
8b840115 MD |
402 | SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); |
403 | SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); | |
1cc91f1b | 404 | |
d232520a MV |
405 | static void iprin1 (SCM exp, SCM port, scm_print_state *pstate); |
406 | ||
0f2d19dd | 407 | void |
1bbd0b84 | 408 | scm_iprin1 (SCM exp, SCM port, scm_print_state *pstate) |
d232520a MV |
409 | { |
410 | if (pstate->fancyp | |
411 | && scm_is_true (scm_memq (exp, pstate->highlight_objects))) | |
412 | { | |
81ae25da | 413 | scm_display (SCM_PRINT_HIGHLIGHT_PREFIX, port); |
d232520a | 414 | iprin1 (exp, port, pstate); |
81ae25da | 415 | scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX, port); |
d232520a MV |
416 | } |
417 | else | |
418 | iprin1 (exp, port, pstate); | |
419 | } | |
420 | ||
421 | static void | |
422 | iprin1 (SCM exp, SCM port, scm_print_state *pstate) | |
0f2d19dd | 423 | { |
54778cd3 | 424 | switch (SCM_ITAG3 (exp)) |
0f2d19dd | 425 | { |
e34f941a DH |
426 | case scm_tc3_closure: |
427 | case scm_tc3_tc7_1: | |
428 | case scm_tc3_tc7_2: | |
429 | /* These tc3 tags should never occur in an immediate value. They are | |
430 | * only used in cell types of non-immediates, i. e. the value returned | |
431 | * by SCM_CELL_TYPE (exp) can use these tags. | |
432 | */ | |
433 | scm_ipruk ("immediate", exp, port); | |
434 | break; | |
435 | case scm_tc3_int_1: | |
436 | case scm_tc3_int_2: | |
e11e83f3 | 437 | scm_intprint (SCM_I_INUM (exp), 10, port); |
0f2d19dd | 438 | break; |
e34f941a | 439 | case scm_tc3_imm24: |
7866a09b | 440 | if (SCM_CHARP (exp)) |
0f2d19dd | 441 | { |
904a78f1 | 442 | scm_t_wchar i = SCM_CHAR (exp); |
77332b21 | 443 | const char *name; |
5ca6dc39 | 444 | |
b7f3516f TT |
445 | if (SCM_WRITINGP (pstate)) |
446 | { | |
447 | scm_puts ("#\\", port); | |
77332b21 MG |
448 | name = scm_i_charname (exp); |
449 | if (name != NULL) | |
450 | scm_puts (name, port); | |
904a78f1 MG |
451 | else if (uc_is_general_category_withtable (i, UC_CATEGORY_MASK_L |
452 | | UC_CATEGORY_MASK_M | |
453 | | UC_CATEGORY_MASK_N | |
454 | | UC_CATEGORY_MASK_P | |
455 | | UC_CATEGORY_MASK_S)) | |
456 | /* Print the character if is graphic character. */ | |
457 | { | |
458 | if (i<256) | |
f7118e35 MG |
459 | /* Character is graphic. Print it. */ |
460 | scm_putc (i, port); | |
904a78f1 | 461 | else |
f7118e35 MG |
462 | /* Character is graphic but unrepresentable in |
463 | this port's encoding. */ | |
464 | scm_intprint (i, 8, port); | |
904a78f1 MG |
465 | } |
466 | else | |
f7118e35 MG |
467 | /* Character is a non-graphical character. */ |
468 | scm_intprint (i, 8, port); | |
b7f3516f TT |
469 | } |
470 | else | |
471 | scm_putc (i, port); | |
0f2d19dd | 472 | } |
a51ea417 | 473 | else if (SCM_IFLAGP (exp) |
e17d318f DH |
474 | && ((size_t) SCM_IFLAGNUM (exp) < (sizeof iflagnames / sizeof (char *)))) |
475 | { | |
476 | scm_puts (iflagnames [SCM_IFLAGNUM (exp)], port); | |
477 | } | |
7e6e6b37 | 478 | else if (SCM_ISYMP (exp)) |
e17d318f | 479 | { |
7e6e6b37 | 480 | scm_i_print_isym (exp, port); |
e17d318f | 481 | } |
0f2d19dd JB |
482 | else if (SCM_ILOCP (exp)) |
483 | { | |
7e6e6b37 | 484 | scm_i_print_iloc (exp, port); |
0f2d19dd JB |
485 | } |
486 | else | |
e34f941a DH |
487 | { |
488 | /* unknown immediate value */ | |
489 | scm_ipruk ("immediate", exp, port); | |
490 | } | |
0f2d19dd | 491 | break; |
e34f941a | 492 | case scm_tc3_cons: |
0f2d19dd JB |
493 | switch (SCM_TYP7 (exp)) |
494 | { | |
904a077d MV |
495 | case scm_tcs_struct: |
496 | { | |
497 | ENTER_NESTED_DATA (pstate, exp, circref); | |
498 | if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS) | |
499 | { | |
500 | SCM pwps, print = pstate->writingp ? g_write : g_display; | |
501 | if (!print) | |
502 | goto print_struct; | |
dfd03fb9 | 503 | pwps = scm_i_port_with_print_state (port, pstate->handle); |
7663c008 | 504 | pstate->revealed = 1; |
904a077d MV |
505 | scm_call_generic_2 (print, exp, pwps); |
506 | } | |
507 | else | |
508 | { | |
509 | print_struct: | |
510 | scm_print_struct (exp, port, pstate); | |
511 | } | |
512 | EXIT_NESTED_DATA (pstate); | |
513 | } | |
514 | break; | |
0f2d19dd JB |
515 | case scm_tcs_cons_imcar: |
516 | case scm_tcs_cons_nimcar: | |
c62fbfe1 MD |
517 | ENTER_NESTED_DATA (pstate, exp, circref); |
518 | scm_iprlist ("(", exp, ')', port, pstate); | |
519 | EXIT_NESTED_DATA (pstate); | |
a51ea417 MD |
520 | break; |
521 | circref: | |
c62fbfe1 | 522 | print_circref (port, pstate, exp); |
0f2d19dd JB |
523 | break; |
524 | case scm_tcs_closures: | |
7888309b MV |
525 | if (scm_is_false (scm_procedure_p (SCM_PRINT_CLOSURE)) |
526 | || scm_is_false (scm_printer_apply (SCM_PRINT_CLOSURE, | |
77c25af7 | 527 | exp, port, pstate))) |
726d810a DH |
528 | { |
529 | SCM formals = SCM_CLOSURE_FORMALS (exp); | |
530 | scm_puts ("#<procedure", port); | |
531 | scm_putc (' ', port); | |
532 | scm_iprin1 (scm_procedure_name (exp), port, pstate); | |
533 | scm_putc (' ', port); | |
534 | if (SCM_PRINT_SOURCE_P) | |
535 | { | |
536 | SCM env = SCM_ENV (exp); | |
537 | SCM xenv = SCM_EXTEND_ENV (formals, SCM_EOL, env); | |
9fcf3cbb | 538 | SCM src = scm_i_unmemocopy_body (SCM_CODE (exp), xenv); |
726d810a DH |
539 | ENTER_NESTED_DATA (pstate, exp, circref); |
540 | scm_iprin1 (src, port, pstate); | |
541 | EXIT_NESTED_DATA (pstate); | |
542 | } | |
543 | else | |
544 | scm_iprin1 (formals, port, pstate); | |
b7f3516f | 545 | scm_putc ('>', port); |
726d810a | 546 | } |
0f2d19dd | 547 | break; |
534c55a9 DH |
548 | case scm_tc7_number: |
549 | switch SCM_TYP16 (exp) { | |
550 | case scm_tc16_big: | |
551 | scm_bigprint (exp, port, pstate); | |
552 | break; | |
553 | case scm_tc16_real: | |
554 | scm_print_real (exp, port, pstate); | |
555 | break; | |
556 | case scm_tc16_complex: | |
557 | scm_print_complex (exp, port, pstate); | |
558 | break; | |
f92e85f7 MV |
559 | case scm_tc16_fraction: |
560 | scm_i_print_fraction (exp, port, pstate); | |
561 | break; | |
534c55a9 DH |
562 | } |
563 | break; | |
9c44cd45 MG |
564 | case scm_tc7_string: |
565 | if (SCM_WRITINGP (pstate)) | |
566 | { | |
567 | size_t i, j, len; | |
568 | static char const hex[] = "0123456789abcdef"; | |
569 | char buf[8]; | |
5ca6dc39 | 570 | |
9c44cd45 MG |
571 | |
572 | scm_putc ('"', port); | |
573 | len = scm_i_string_length (exp); | |
574 | for (i = 0; i < len; ++i) | |
575 | { | |
576 | scm_t_wchar ch = scm_i_string_ref (exp, i); | |
577 | int printed = 0; | |
578 | ||
579 | if (ch == ' ' || ch == '\n') | |
580 | { | |
581 | scm_putc (ch, port); | |
582 | printed = 1; | |
583 | } | |
584 | else if (ch == '"' || ch == '\\') | |
585 | { | |
586 | scm_putc ('\\', port); | |
32be5735 | 587 | scm_i_charprint (ch, port); |
9c44cd45 MG |
588 | printed = 1; |
589 | } | |
590 | else | |
591 | if (uc_is_general_category_withtable | |
592 | (ch, | |
593 | UC_CATEGORY_MASK_L | UC_CATEGORY_MASK_M | | |
594 | UC_CATEGORY_MASK_N | UC_CATEGORY_MASK_P | | |
595 | UC_CATEGORY_MASK_S)) | |
596 | { | |
597 | /* Print the character since it is a graphic | |
598 | character. */ | |
599 | scm_t_wchar *wbuf; | |
600 | SCM wstr = scm_i_make_wide_string (1, &wbuf); | |
601 | char *buf; | |
602 | size_t len; | |
603 | ||
604 | wbuf[0] = ch; | |
605 | ||
606 | buf = u32_conv_to_encoding ("ISO-8859-1", | |
607 | iconveh_error, | |
608 | (scm_t_uint32 *) wbuf, | |
609 | 1, NULL, NULL, &len); | |
610 | if (buf != NULL) | |
611 | { | |
612 | /* Character is graphic and representable in | |
613 | this encoding. Print it. */ | |
614 | scm_lfwrite_str (wstr, port); | |
615 | free (buf); | |
616 | printed = 1; | |
617 | } | |
618 | } | |
619 | ||
620 | if (!printed) | |
621 | { | |
622 | /* Character is graphic but unrepresentable in | |
623 | this port's encoding or is not graphic. */ | |
624 | if (ch <= 0xFF) | |
625 | { | |
626 | buf[0] = '\\'; | |
627 | buf[1] = 'x'; | |
628 | buf[2] = hex[ch / 16]; | |
629 | buf[3] = hex[ch % 16]; | |
630 | scm_lfwrite (buf, 4, port); | |
631 | } | |
632 | else if (ch <= 0xFFFF) | |
633 | { | |
634 | buf[0] = '\\'; | |
635 | buf[1] = 'u'; | |
636 | buf[2] = hex[(ch & 0xF000) >> 12]; | |
637 | buf[3] = hex[(ch & 0xF00) >> 8]; | |
638 | buf[4] = hex[(ch & 0xF0) >> 4]; | |
639 | buf[5] = hex[(ch & 0xF)]; | |
640 | scm_lfwrite (buf, 6, port); | |
641 | j = i + 1; | |
642 | } | |
643 | else if (ch > 0xFFFF) | |
644 | { | |
645 | buf[0] = '\\'; | |
646 | buf[1] = 'U'; | |
647 | buf[2] = hex[(ch & 0xF00000) >> 20]; | |
648 | buf[3] = hex[(ch & 0xF0000) >> 16]; | |
649 | buf[4] = hex[(ch & 0xF000) >> 12]; | |
650 | buf[5] = hex[(ch & 0xF00) >> 8]; | |
651 | buf[6] = hex[(ch & 0xF0) >> 4]; | |
652 | buf[7] = hex[(ch & 0xF)]; | |
653 | scm_lfwrite (buf, 8, port); | |
654 | j = i + 1; | |
655 | } | |
656 | } | |
657 | } | |
658 | scm_putc ('"', port); | |
659 | scm_remember_upto_here_1 (exp); | |
660 | } | |
661 | else | |
6234ff20 | 662 | scm_lfwrite_str (exp, port); |
9c44cd45 MG |
663 | scm_remember_upto_here_1 (exp); |
664 | break; | |
28b06554 | 665 | case scm_tc7_symbol: |
cc95e00a | 666 | if (scm_i_symbol_is_interned (exp)) |
9ff28a13 | 667 | { |
cc95e00a | 668 | scm_print_symbol_name (scm_i_symbol_chars (exp), |
9c44cd45 | 669 | scm_i_symbol_length (exp), port); |
9ff28a13 MV |
670 | scm_remember_upto_here_1 (exp); |
671 | } | |
672 | else | |
673 | { | |
674 | scm_puts ("#<uninterned-symbol ", port); | |
cc95e00a MV |
675 | scm_print_symbol_name (scm_i_symbol_chars (exp), |
676 | scm_i_symbol_length (exp), | |
9ff28a13 MV |
677 | port); |
678 | scm_putc (' ', port); | |
0345e278 | 679 | scm_uintprint (SCM_UNPACK (exp), 16, port); |
9ff28a13 MV |
680 | scm_putc ('>', port); |
681 | } | |
6662998f | 682 | break; |
e5aca4b5 MV |
683 | case scm_tc7_variable: |
684 | scm_i_variable_print (exp, port, pstate); | |
685 | break; | |
2fb924f6 AW |
686 | case scm_tc7_program: |
687 | scm_i_program_print (exp, port, pstate); | |
688 | break; | |
0f2d19dd | 689 | case scm_tc7_wvect: |
c62fbfe1 | 690 | ENTER_NESTED_DATA (pstate, exp, circref); |
0f2d19dd | 691 | if (SCM_IS_WHVEC (exp)) |
b7f3516f | 692 | scm_puts ("#wh(", port); |
0f2d19dd | 693 | else |
b7f3516f | 694 | scm_puts ("#w(", port); |
0f2d19dd JB |
695 | goto common_vector_printer; |
696 | ||
697 | case scm_tc7_vector: | |
c62fbfe1 | 698 | ENTER_NESTED_DATA (pstate, exp, circref); |
b7f3516f | 699 | scm_puts ("#(", port); |
0f2d19dd | 700 | common_vector_printer: |
9fbaf27c | 701 | { |
c014a02e | 702 | register long i; |
4057a3e0 | 703 | long last = SCM_SIMPLE_VECTOR_LENGTH (exp) - 1; |
9fbaf27c | 704 | int cutp = 0; |
4057a3e0 MV |
705 | if (pstate->fancyp |
706 | && SCM_SIMPLE_VECTOR_LENGTH (exp) > pstate->length) | |
9fbaf27c MD |
707 | { |
708 | last = pstate->length - 1; | |
709 | cutp = 1; | |
710 | } | |
711 | for (i = 0; i < last; ++i) | |
712 | { | |
713 | /* CHECK_INTS; */ | |
4057a3e0 | 714 | scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp, i), port, pstate); |
b7f3516f | 715 | scm_putc (' ', port); |
9fbaf27c MD |
716 | } |
717 | if (i == last) | |
718 | { | |
719 | /* CHECK_INTS; */ | |
4057a3e0 | 720 | scm_iprin1 (SCM_SIMPLE_VECTOR_REF (exp, i), port, pstate); |
9fbaf27c MD |
721 | } |
722 | if (cutp) | |
b7f3516f TT |
723 | scm_puts (" ...", port); |
724 | scm_putc (')', port); | |
9fbaf27c | 725 | } |
c62fbfe1 | 726 | EXIT_NESTED_DATA (pstate); |
0f2d19dd | 727 | break; |
0f2d19dd | 728 | case scm_tcs_subrs: |
f76af603 | 729 | scm_puts (SCM_SUBR_GENERIC (exp) |
9de33deb MD |
730 | ? "#<primitive-generic " |
731 | : "#<primitive-procedure ", | |
732 | port); | |
ce471ab8 | 733 | scm_puts (scm_i_symbol_chars (SCM_SUBR_NAME (exp)), port); |
b7f3516f | 734 | scm_putc ('>', port); |
0f2d19dd | 735 | break; |
e20d7001 | 736 | |
c180dfd3 MD |
737 | case scm_tc7_pws: |
738 | scm_puts ("#<procedure-with-setter", port); | |
739 | { | |
740 | SCM name = scm_procedure_name (exp); | |
7888309b | 741 | if (scm_is_true (name)) |
c180dfd3 MD |
742 | { |
743 | scm_putc (' ', port); | |
b24b5e13 | 744 | scm_display (name, port); |
c180dfd3 MD |
745 | } |
746 | } | |
747 | scm_putc ('>', port); | |
748 | break; | |
0f2d19dd | 749 | case scm_tc7_port: |
5ca6dc39 JB |
750 | { |
751 | register long i = SCM_PTOBNUM (exp); | |
752 | if (i < scm_numptob | |
753 | && scm_ptobs[i].print | |
754 | && (scm_ptobs[i].print) (exp, port, pstate)) | |
a51ea417 | 755 | break; |
5ca6dc39 JB |
756 | goto punk; |
757 | } | |
758 | case scm_tc7_smob: | |
7a7f7c53 DH |
759 | ENTER_NESTED_DATA (pstate, exp, circref); |
760 | SCM_SMOB_DESCRIPTOR (exp).print (exp, port, pstate); | |
761 | EXIT_NESTED_DATA (pstate); | |
762 | break; | |
0f2d19dd | 763 | default: |
a51ea417 MD |
764 | punk: |
765 | scm_ipruk ("type", exp, port); | |
0f2d19dd JB |
766 | } |
767 | } | |
768 | } | |
769 | ||
c62fbfe1 MD |
770 | /* Print states are necessary for circular reference safe printing. |
771 | * They are also expensive to allocate. Therefore print states are | |
772 | * kept in a pool so that they can be reused. | |
773 | */ | |
1cc91f1b | 774 | |
bb35f315 MV |
775 | /* The PORT argument can also be a print-state/port pair, which will |
776 | * then be used instead of allocating a new print state. This is | |
777 | * useful for continuing a chain of print calls from Scheme. */ | |
778 | ||
a51ea417 | 779 | void |
1bbd0b84 | 780 | scm_prin1 (SCM exp, SCM port, int writingp) |
a51ea417 | 781 | { |
c4f37e80 MV |
782 | SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */ |
783 | SCM pstate_scm; | |
c62fbfe1 | 784 | scm_print_state *pstate; |
15635be5 | 785 | int old_writingp; |
c62fbfe1 | 786 | |
bb35f315 MV |
787 | /* If PORT is a print-state/port pair, use that. Else create a new |
788 | print-state. */ | |
c4f37e80 | 789 | |
0c95b57d | 790 | if (SCM_PORT_WITH_PS_P (port)) |
bb35f315 | 791 | { |
52235e71 MD |
792 | pstate_scm = SCM_PORT_WITH_PS_PS (port); |
793 | port = SCM_PORT_WITH_PS_PORT (port); | |
bb35f315 MV |
794 | } |
795 | else | |
c62fbfe1 | 796 | { |
c4f37e80 | 797 | /* First try to allocate a print state from the pool */ |
9de87eea | 798 | scm_i_pthread_mutex_lock (&print_state_mutex); |
d2e53ed6 | 799 | if (!scm_is_null (print_state_pool)) |
c4f37e80 | 800 | { |
d5cf5324 DH |
801 | handle = print_state_pool; |
802 | print_state_pool = SCM_CDR (print_state_pool); | |
c4f37e80 | 803 | } |
9de87eea | 804 | scm_i_pthread_mutex_unlock (&print_state_mutex); |
7888309b | 805 | if (scm_is_false (handle)) |
d5cf5324 | 806 | handle = scm_list_1 (make_print_state ()); |
c4f37e80 | 807 | pstate_scm = SCM_CAR (handle); |
c62fbfe1 | 808 | } |
c62fbfe1 | 809 | |
c4f37e80 | 810 | pstate = SCM_PRINT_STATE (pstate_scm); |
15635be5 | 811 | old_writingp = pstate->writingp; |
c62fbfe1 MD |
812 | pstate->writingp = writingp; |
813 | scm_iprin1 (exp, port, pstate); | |
15635be5 | 814 | pstate->writingp = old_writingp; |
c62fbfe1 | 815 | |
bb35f315 MV |
816 | /* Return print state to pool if it has been created above and |
817 | hasn't escaped to Scheme. */ | |
818 | ||
7888309b | 819 | if (scm_is_true (handle) && !pstate->revealed) |
c4f37e80 | 820 | { |
9de87eea | 821 | scm_i_pthread_mutex_lock (&print_state_mutex); |
d5cf5324 DH |
822 | SCM_SETCDR (handle, print_state_pool); |
823 | print_state_pool = handle; | |
9de87eea | 824 | scm_i_pthread_mutex_unlock (&print_state_mutex); |
c4f37e80 | 825 | } |
a51ea417 MD |
826 | } |
827 | ||
9c44cd45 MG |
828 | /* Print a character. |
829 | */ | |
830 | void | |
32be5735 | 831 | scm_i_charprint (scm_t_uint32 ch, SCM port) |
9c44cd45 MG |
832 | { |
833 | scm_t_wchar *wbuf; | |
834 | SCM wstr = scm_i_make_wide_string (1, &wbuf); | |
835 | ||
836 | wbuf[0] = ch; | |
837 | scm_lfwrite_str (wstr, port); | |
838 | } | |
0f2d19dd JB |
839 | |
840 | /* Print an integer. | |
841 | */ | |
1cc91f1b | 842 | |
0f2d19dd | 843 | void |
a406c9e9 | 844 | scm_intprint (scm_t_intmax n, int radix, SCM port) |
0f2d19dd JB |
845 | { |
846 | char num_buf[SCM_INTBUFLEN]; | |
b7f3516f | 847 | scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port); |
0f2d19dd JB |
848 | } |
849 | ||
a406c9e9 MV |
850 | void |
851 | scm_uintprint (scm_t_uintmax n, int radix, SCM port) | |
852 | { | |
853 | char num_buf[SCM_INTBUFLEN]; | |
854 | scm_lfwrite (num_buf, scm_iuint2str (n, radix, num_buf), port); | |
855 | } | |
856 | ||
0f2d19dd JB |
857 | /* Print an object of unrecognized type. |
858 | */ | |
1cc91f1b | 859 | |
0f2d19dd | 860 | void |
1bbd0b84 | 861 | scm_ipruk (char *hdr, SCM ptr, SCM port) |
0f2d19dd | 862 | { |
b7f3516f TT |
863 | scm_puts ("#<unknown-", port); |
864 | scm_puts (hdr, port); | |
c8a1bdc4 | 865 | if (scm_in_heap_p (ptr)) |
0f2d19dd | 866 | { |
b7f3516f | 867 | scm_puts (" (0x", port); |
0345e278 | 868 | scm_uintprint (SCM_CELL_WORD_0 (ptr), 16, port); |
b7f3516f | 869 | scm_puts (" . 0x", port); |
0345e278 | 870 | scm_uintprint (SCM_CELL_WORD_1 (ptr), 16, port); |
b7f3516f | 871 | scm_puts (") @", port); |
0f2d19dd | 872 | } |
b7f3516f | 873 | scm_puts (" 0x", port); |
0345e278 | 874 | scm_uintprint (SCM_UNPACK (ptr), 16, port); |
b7f3516f | 875 | scm_putc ('>', port); |
0f2d19dd JB |
876 | } |
877 | ||
1cc91f1b | 878 | |
904a077d | 879 | /* Print a list. |
22a52da1 | 880 | */ |
0f2d19dd | 881 | void |
34d19ef6 | 882 | scm_iprlist (char *hdr, SCM exp, int tlr, SCM port, scm_print_state *pstate) |
0f2d19dd | 883 | { |
c62fbfe1 | 884 | register SCM hare, tortoise; |
c014a02e | 885 | long floor = pstate->top - 2; |
b7f3516f | 886 | scm_puts (hdr, port); |
0f2d19dd | 887 | /* CHECK_INTS; */ |
c62fbfe1 MD |
888 | if (pstate->fancyp) |
889 | goto fancy_printing; | |
890 | ||
891 | /* Run a hare and tortoise so that total time complexity will be | |
892 | O(depth * N) instead of O(N^2). */ | |
893 | hare = SCM_CDR (exp); | |
894 | tortoise = exp; | |
d2e53ed6 | 895 | while (scm_is_pair (hare)) |
c62fbfe1 | 896 | { |
bc36d050 | 897 | if (scm_is_eq (hare, tortoise)) |
c62fbfe1 MD |
898 | goto fancy_printing; |
899 | hare = SCM_CDR (hare); | |
d2e53ed6 | 900 | if (!scm_is_pair (hare)) |
c62fbfe1 MD |
901 | break; |
902 | hare = SCM_CDR (hare); | |
903 | tortoise = SCM_CDR (tortoise); | |
904 | } | |
905 | ||
906 | /* No cdr cycles intrinsic to this list */ | |
907 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
d2e53ed6 | 908 | for (exp = SCM_CDR (exp); scm_is_pair (exp); exp = SCM_CDR (exp)) |
0f2d19dd | 909 | { |
c014a02e | 910 | register long i; |
5ca6dc39 | 911 | |
c62fbfe1 | 912 | for (i = floor; i >= 0; --i) |
509759dd | 913 | if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp)) |
c62fbfe1 MD |
914 | goto circref; |
915 | PUSH_REF (pstate, exp); | |
b7f3516f | 916 | scm_putc (' ', port); |
0f2d19dd | 917 | /* CHECK_INTS; */ |
c62fbfe1 | 918 | scm_iprin1 (SCM_CAR (exp), port, pstate); |
0f2d19dd | 919 | } |
c96d76b8 | 920 | if (!SCM_NULL_OR_NIL_P (exp)) |
0f2d19dd | 921 | { |
b7f3516f | 922 | scm_puts (" . ", port); |
c62fbfe1 | 923 | scm_iprin1 (exp, port, pstate); |
0f2d19dd | 924 | } |
c62fbfe1 | 925 | |
a51ea417 | 926 | end: |
b7f3516f | 927 | scm_putc (tlr, port); |
c62fbfe1 | 928 | pstate->top = floor + 2; |
a51ea417 | 929 | return; |
c62fbfe1 MD |
930 | |
931 | fancy_printing: | |
932 | { | |
c014a02e | 933 | long n = pstate->length; |
c62fbfe1 MD |
934 | |
935 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
936 | exp = SCM_CDR (exp); --n; | |
d2e53ed6 | 937 | for (; scm_is_pair (exp); exp = SCM_CDR (exp)) |
c62fbfe1 | 938 | { |
c014a02e | 939 | register unsigned long i; |
5ca6dc39 | 940 | |
c62fbfe1 | 941 | for (i = 0; i < pstate->top; ++i) |
509759dd | 942 | if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp)) |
c62fbfe1 MD |
943 | goto fancy_circref; |
944 | if (pstate->fancyp) | |
945 | { | |
946 | if (n == 0) | |
947 | { | |
b7f3516f | 948 | scm_puts (" ...", port); |
c62fbfe1 MD |
949 | goto skip_tail; |
950 | } | |
951 | else | |
952 | --n; | |
953 | } | |
954 | PUSH_REF(pstate, exp); | |
955 | ++pstate->list_offset; | |
b7f3516f | 956 | scm_putc (' ', port); |
c62fbfe1 MD |
957 | /* CHECK_INTS; */ |
958 | scm_iprin1 (SCM_CAR (exp), port, pstate); | |
959 | } | |
960 | } | |
c96d76b8 | 961 | if (!SCM_NULL_OR_NIL_P (exp)) |
c62fbfe1 | 962 | { |
b7f3516f | 963 | scm_puts (" . ", port); |
c62fbfe1 MD |
964 | scm_iprin1 (exp, port, pstate); |
965 | } | |
966 | skip_tail: | |
967 | pstate->list_offset -= pstate->top - floor - 2; | |
a51ea417 | 968 | goto end; |
a51ea417 | 969 | |
c62fbfe1 MD |
970 | fancy_circref: |
971 | pstate->list_offset -= pstate->top - floor - 2; | |
972 | ||
973 | circref: | |
b7f3516f | 974 | scm_puts (" . ", port); |
c62fbfe1 MD |
975 | print_circref (port, pstate, exp); |
976 | goto end; | |
0f2d19dd JB |
977 | } |
978 | ||
979 | \f | |
980 | ||
bb35f315 MV |
981 | int |
982 | scm_valid_oport_value_p (SCM val) | |
983 | { | |
368cf54d GB |
984 | return (SCM_OPOUTPORTP (val) |
985 | || (SCM_PORT_WITH_PS_P (val) | |
986 | && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val)))); | |
bb35f315 MV |
987 | } |
988 | ||
8b840115 | 989 | /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */ |
1cc91f1b | 990 | |
0f2d19dd | 991 | SCM |
1bbd0b84 | 992 | scm_write (SCM obj, SCM port) |
0f2d19dd JB |
993 | { |
994 | if (SCM_UNBNDP (port)) | |
9de87eea | 995 | port = scm_current_output_port (); |
3eb7e6ee JB |
996 | |
997 | SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write); | |
bb35f315 | 998 | |
a51ea417 | 999 | scm_prin1 (obj, port, 1); |
23d72566 | 1000 | #if 0 |
0f2d19dd JB |
1001 | #ifdef HAVE_PIPE |
1002 | # ifdef EPIPE | |
1003 | if (EPIPE == errno) | |
1004 | scm_close_port (port); | |
1005 | # endif | |
23d72566 | 1006 | #endif |
0f2d19dd JB |
1007 | #endif |
1008 | return SCM_UNSPECIFIED; | |
1009 | } | |
1010 | ||
1011 | ||
8b840115 | 1012 | /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */ |
1cc91f1b | 1013 | |
0f2d19dd | 1014 | SCM |
1bbd0b84 | 1015 | scm_display (SCM obj, SCM port) |
0f2d19dd JB |
1016 | { |
1017 | if (SCM_UNBNDP (port)) | |
9de87eea | 1018 | port = scm_current_output_port (); |
3eb7e6ee JB |
1019 | |
1020 | SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display); | |
bb35f315 | 1021 | |
a51ea417 | 1022 | scm_prin1 (obj, port, 0); |
23d72566 | 1023 | #if 0 |
0f2d19dd JB |
1024 | #ifdef HAVE_PIPE |
1025 | # ifdef EPIPE | |
1026 | if (EPIPE == errno) | |
1027 | scm_close_port (port); | |
1028 | # endif | |
23d72566 | 1029 | #endif |
0f2d19dd JB |
1030 | #endif |
1031 | return SCM_UNSPECIFIED; | |
1032 | } | |
1033 | ||
70d63753 GB |
1034 | |
1035 | SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1, | |
1036 | (SCM destination, SCM message, SCM args), | |
eca65e90 MG |
1037 | "Write @var{message} to @var{destination}, defaulting to\n" |
1038 | "the current output port.\n" | |
1039 | "@var{message} can contain @code{~A} (was @code{%s}) and\n" | |
1040 | "@code{~S} (was @code{%S}) escapes. When printed,\n" | |
1041 | "the escapes are replaced with corresponding members of\n" | |
1042 | "@var{ARGS}:\n" | |
1043 | "@code{~A} formats using @code{display} and @code{~S} formats\n" | |
1044 | "using @code{write}.\n" | |
1045 | "If @var{destination} is @code{#t}, then use the current output\n" | |
1046 | "port, if @var{destination} is @code{#f}, then return a string\n" | |
1047 | "containing the formatted text. Does not add a trailing newline.") | |
70d63753 GB |
1048 | #define FUNC_NAME s_scm_simple_format |
1049 | { | |
dfd03fb9 | 1050 | SCM port, answer = SCM_UNSPECIFIED; |
70d63753 GB |
1051 | int fReturnString = 0; |
1052 | int writingp; | |
cc95e00a MV |
1053 | const char *start; |
1054 | const char *end; | |
1055 | const char *p; | |
70d63753 | 1056 | |
bc36d050 | 1057 | if (scm_is_eq (destination, SCM_BOOL_T)) |
daba1a71 | 1058 | { |
9de87eea | 1059 | destination = port = scm_current_output_port (); |
daba1a71 | 1060 | } |
7888309b | 1061 | else if (scm_is_false (destination)) |
daba1a71 MD |
1062 | { |
1063 | fReturnString = 1; | |
dfd03fb9 MD |
1064 | port = scm_mkstrport (SCM_INUM0, |
1065 | scm_make_string (SCM_INUM0, SCM_UNDEFINED), | |
1066 | SCM_OPN | SCM_WRTNG, | |
1067 | FUNC_NAME); | |
1068 | destination = port; | |
daba1a71 MD |
1069 | } |
1070 | else | |
1071 | { | |
1072 | SCM_VALIDATE_OPORT_VALUE (1, destination); | |
dfd03fb9 | 1073 | port = SCM_COERCE_OUTPORT (destination); |
daba1a71 MD |
1074 | } |
1075 | SCM_VALIDATE_STRING (2, message); | |
af45e3b0 | 1076 | SCM_VALIDATE_REST_ARGUMENT (args); |
70d63753 | 1077 | |
cc95e00a MV |
1078 | start = scm_i_string_chars (message); |
1079 | end = start + scm_i_string_length (message); | |
b24b5e13 | 1080 | for (p = start; p != end; ++p) |
70d63753 GB |
1081 | if (*p == '~') |
1082 | { | |
b24b5e13 | 1083 | if (++p == end) |
6662998f MV |
1084 | break; |
1085 | ||
1086 | switch (*p) | |
1087 | { | |
1088 | case 'A': case 'a': | |
1089 | writingp = 0; | |
1090 | break; | |
1091 | case 'S': case 's': | |
1092 | writingp = 1; | |
1093 | break; | |
1094 | case '~': | |
dfd03fb9 | 1095 | scm_lfwrite (start, p - start, port); |
6662998f MV |
1096 | start = p + 1; |
1097 | continue; | |
1098 | case '%': | |
dfd03fb9 MD |
1099 | scm_lfwrite (start, p - start - 1, port); |
1100 | scm_newline (port); | |
6662998f MV |
1101 | start = p + 1; |
1102 | continue; | |
1103 | default: | |
1afff620 KN |
1104 | SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead", |
1105 | scm_list_1 (SCM_MAKE_CHAR (*p))); | |
6662998f MV |
1106 | |
1107 | } | |
70d63753 | 1108 | |
6662998f | 1109 | |
d2e53ed6 | 1110 | if (!scm_is_pair (args)) |
1afff620 KN |
1111 | SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A", |
1112 | scm_list_1 (SCM_MAKE_CHAR (*p))); | |
6662998f | 1113 | |
dfd03fb9 MD |
1114 | scm_lfwrite (start, p - start - 1, port); |
1115 | /* we pass destination here */ | |
70d63753 GB |
1116 | scm_prin1 (SCM_CAR (args), destination, writingp); |
1117 | args = SCM_CDR (args); | |
1118 | start = p + 1; | |
1119 | } | |
6662998f | 1120 | |
dfd03fb9 | 1121 | scm_lfwrite (start, p - start, port); |
bc36d050 | 1122 | if (!scm_is_eq (args, SCM_EOL)) |
1afff620 KN |
1123 | SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments", |
1124 | scm_list_1 (scm_length (args))); | |
70d63753 GB |
1125 | |
1126 | if (fReturnString) | |
1127 | answer = scm_strport_to_string (destination); | |
1128 | ||
daba1a71 | 1129 | return scm_return_first (answer, message); |
70d63753 GB |
1130 | } |
1131 | #undef FUNC_NAME | |
1132 | ||
1133 | ||
3b3b36dd | 1134 | SCM_DEFINE (scm_newline, "newline", 0, 1, 0, |
b450f070 | 1135 | (SCM port), |
8f85c0c6 NJ |
1136 | "Send a newline to @var{port}.\n" |
1137 | "If @var{port} is omitted, send to the current output port.") | |
1bbd0b84 | 1138 | #define FUNC_NAME s_scm_newline |
0f2d19dd JB |
1139 | { |
1140 | if (SCM_UNBNDP (port)) | |
9de87eea | 1141 | port = scm_current_output_port (); |
3eb7e6ee | 1142 | |
34d19ef6 | 1143 | SCM_VALIDATE_OPORT_VALUE (1, port); |
bb35f315 | 1144 | |
0ef4ae82 | 1145 | scm_putc ('\n', SCM_COERCE_OUTPORT (port)); |
0f2d19dd JB |
1146 | return SCM_UNSPECIFIED; |
1147 | } | |
1bbd0b84 | 1148 | #undef FUNC_NAME |
0f2d19dd | 1149 | |
3b3b36dd | 1150 | SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0, |
b450f070 | 1151 | (SCM chr, SCM port), |
eca65e90 | 1152 | "Send character @var{chr} to @var{port}.") |
1bbd0b84 | 1153 | #define FUNC_NAME s_scm_write_char |
0f2d19dd JB |
1154 | { |
1155 | if (SCM_UNBNDP (port)) | |
9de87eea | 1156 | port = scm_current_output_port (); |
3eb7e6ee | 1157 | |
34d19ef6 HWN |
1158 | SCM_VALIDATE_CHAR (1, chr); |
1159 | SCM_VALIDATE_OPORT_VALUE (2, port); | |
bb35f315 | 1160 | |
7866a09b | 1161 | scm_putc ((int) SCM_CHAR (chr), SCM_COERCE_OUTPORT (port)); |
23d72566 | 1162 | #if 0 |
0f2d19dd JB |
1163 | #ifdef HAVE_PIPE |
1164 | # ifdef EPIPE | |
1165 | if (EPIPE == errno) | |
1166 | scm_close_port (port); | |
1167 | # endif | |
23d72566 | 1168 | #endif |
0f2d19dd JB |
1169 | #endif |
1170 | return SCM_UNSPECIFIED; | |
1171 | } | |
1bbd0b84 | 1172 | #undef FUNC_NAME |
0f2d19dd | 1173 | |
0f2d19dd JB |
1174 | \f |
1175 | ||
bb35f315 | 1176 | /* Call back to Scheme code to do the printing of special objects |
c19bc088 MD |
1177 | * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob |
1178 | * containing PORT and PSTATE. This object can be used as the port for | |
1179 | * display/write etc to continue the current print chain. The REVEALED | |
1180 | * field of PSTATE is set to true to indicate that the print state has | |
1181 | * escaped to Scheme and thus has to be freed by the GC. | |
1182 | */ | |
1183 | ||
92c2555f | 1184 | scm_t_bits scm_tc16_port_with_ps; |
c19bc088 MD |
1185 | |
1186 | /* Print exactly as the port itself would */ | |
1187 | ||
1188 | static int | |
e841c3e0 | 1189 | port_with_ps_print (SCM obj, SCM port, scm_print_state *pstate) |
c19bc088 MD |
1190 | { |
1191 | obj = SCM_PORT_WITH_PS_PORT (obj); | |
1192 | return scm_ptobs[SCM_PTOBNUM (obj)].print (obj, port, pstate); | |
1193 | } | |
c4f37e80 MV |
1194 | |
1195 | SCM | |
1bbd0b84 | 1196 | scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate) |
c4f37e80 | 1197 | { |
bb35f315 | 1198 | pstate->revealed = 1; |
dfd03fb9 MD |
1199 | return scm_call_2 (proc, exp, |
1200 | scm_i_port_with_print_state (port, pstate->handle)); | |
c19bc088 MD |
1201 | } |
1202 | ||
dfd03fb9 | 1203 | SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 1, 1, 0, |
1bbd0b84 | 1204 | (SCM port, SCM pstate), |
71331188 | 1205 | "Create a new port which behaves like @var{port}, but with an\n" |
dfd03fb9 MD |
1206 | "included print state @var{pstate}. @var{pstate} is optional.\n" |
1207 | "If @var{pstate} isn't supplied and @var{port} already has\n" | |
1208 | "a print state, the old print state is reused.") | |
1bbd0b84 | 1209 | #define FUNC_NAME s_scm_port_with_print_state |
c19bc088 | 1210 | { |
34d19ef6 | 1211 | SCM_VALIDATE_OPORT_VALUE (1, port); |
dfd03fb9 MD |
1212 | if (!SCM_UNBNDP (pstate)) |
1213 | SCM_VALIDATE_PRINTSTATE (2, pstate); | |
1214 | return scm_i_port_with_print_state (port, pstate); | |
c19bc088 | 1215 | } |
1bbd0b84 | 1216 | #undef FUNC_NAME |
c19bc088 | 1217 | |
a1ec6916 | 1218 | SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0, |
1bbd0b84 | 1219 | (SCM port), |
71331188 MG |
1220 | "Return the print state of the port @var{port}. If @var{port}\n" |
1221 | "has no associated print state, @code{#f} is returned.") | |
1bbd0b84 | 1222 | #define FUNC_NAME s_scm_get_print_state |
c19bc088 | 1223 | { |
368cf54d GB |
1224 | if (SCM_PORT_WITH_PS_P (port)) |
1225 | return SCM_PORT_WITH_PS_PS (port); | |
f5f2dcff | 1226 | if (SCM_OUTPUT_PORT_P (port)) |
368cf54d | 1227 | return SCM_BOOL_F; |
276dd677 | 1228 | SCM_WRONG_TYPE_ARG (1, port); |
c4f37e80 | 1229 | } |
1bbd0b84 | 1230 | #undef FUNC_NAME |
bb35f315 | 1231 | |
c4f37e80 | 1232 | \f |
1cc91f1b | 1233 | |
0f2d19dd JB |
1234 | void |
1235 | scm_init_print () | |
0f2d19dd | 1236 | { |
c19bc088 | 1237 | SCM vtable, layout, type; |
d5cf5324 | 1238 | |
62560650 | 1239 | scm_init_opts (scm_print_options, scm_print_opts); |
d5cf5324 | 1240 | |
81ae25da MV |
1241 | scm_print_options (scm_list_4 (scm_from_locale_symbol ("highlight-prefix"), |
1242 | scm_from_locale_string ("{"), | |
1243 | scm_from_locale_symbol ("highlight-suffix"), | |
1244 | scm_from_locale_string ("}"))); | |
1245 | ||
d5cf5324 DH |
1246 | scm_gc_register_root (&print_state_pool); |
1247 | scm_gc_register_root (&scm_print_state_vtable); | |
3ce4544c | 1248 | vtable = scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL); |
cc95e00a MV |
1249 | layout = |
1250 | scm_make_struct_layout (scm_from_locale_string (SCM_PRINT_STATE_LAYOUT)); | |
1afff620 | 1251 | type = scm_make_struct (vtable, SCM_INUM0, scm_list_1 (layout)); |
cc95e00a | 1252 | scm_set_struct_vtable_name_x (type, scm_from_locale_symbol ("print-state")); |
bb35f315 | 1253 | scm_print_state_vtable = type; |
c4f37e80 | 1254 | |
c19bc088 MD |
1255 | /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */ |
1256 | scm_tc16_port_with_ps = scm_make_smob_type (0, 0); | |
1257 | scm_set_smob_mark (scm_tc16_port_with_ps, scm_markcdr); | |
e841c3e0 | 1258 | scm_set_smob_print (scm_tc16_port_with_ps, port_with_ps_print); |
81ae25da | 1259 | |
a0599745 | 1260 | #include "libguile/print.x" |
475fa9a5 MV |
1261 | |
1262 | scm_print_opts[SCM_PRINT_KEYWORD_STYLE_I].val = SCM_UNPACK (sym_reader); | |
0f2d19dd | 1263 | } |
89e00824 ML |
1264 | |
1265 | /* | |
1266 | Local Variables: | |
1267 | c-file-style: "gnu" | |
1268 | End: | |
1269 | */ |