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