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