print.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. /* Copyright 1995-2004,2006,2008-2019
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <errno.h>
  19. #include <stdio.h>
  20. #include <assert.h>
  21. #include <uniconv.h>
  22. #include <unictype.h>
  23. #include "alist.h"
  24. #include "arrays.h"
  25. #include "atomic.h"
  26. #include "bitvectors.h"
  27. #include "boolean.h"
  28. #include "chars.h"
  29. #include "continuations.h"
  30. #include "control.h"
  31. #include "eval.h"
  32. #include "fluids.h"
  33. #include "foreign.h"
  34. #include "frames.h"
  35. #include "goops.h"
  36. #include "gsubr.h"
  37. #include "hashtab.h"
  38. #include "keywords.h"
  39. #include "macros.h"
  40. #include "numbers.h"
  41. #include "pairs.h"
  42. #include "ports-internal.h"
  43. #include "ports.h"
  44. #include "private-options.h"
  45. #include "procprop.h"
  46. #include "programs.h"
  47. #include "read.h"
  48. #include "smob.h"
  49. #include "strings.h"
  50. #include "strports.h"
  51. #include "struct.h"
  52. #include "symbols.h"
  53. #include "syntax.h"
  54. #include "threads.h"
  55. #include "values.h"
  56. #include "variable.h"
  57. #include "vectors.h"
  58. #include "vm.h"
  59. #include "weak-set.h"
  60. #include "weak-table.h"
  61. #include "weak-vector.h"
  62. #include "print.h"
  63. /* Character printers. */
  64. static void write_string (const void *, int, size_t, SCM);
  65. static void write_character (scm_t_wchar, SCM);
  66. /* {Names of immediate symbols}
  67. *
  68. * This table must agree with the declarations in scm.h: {Immediate Symbols}.
  69. */
  70. /* This table must agree with the list of flags in scm.h. */
  71. static const char *iflagnames[] =
  72. {
  73. "#f",
  74. "#nil", /* Elisp nil value. Should print from elisp as symbol `nil'. */
  75. "#<XXX UNUSED LISP FALSE -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
  76. "()",
  77. "#t",
  78. "#<XXX UNUSED BOOLEAN 0 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
  79. "#<XXX UNUSED BOOLEAN 1 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
  80. "#<XXX UNUSED BOOLEAN 2 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
  81. "#<unspecified>",
  82. "#<undefined>",
  83. "#<eof>",
  84. /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */
  85. "#<unbound>",
  86. };
  87. SCM_SYMBOL (sym_reader, "reader");
  88. scm_t_option scm_print_opts[] = {
  89. { SCM_OPTION_SCM, "highlight-prefix", (scm_t_bits)SCM_BOOL_F_BITS,
  90. "The string to print before highlighted values." },
  91. { SCM_OPTION_SCM, "highlight-suffix", (scm_t_bits)SCM_BOOL_F_BITS,
  92. "The string to print after highlighted values." },
  93. { SCM_OPTION_SCM, "quote-keywordish-symbols", (scm_t_bits)SCM_BOOL_F_BITS,
  94. "How to print symbols that have a colon as their first or last character. "
  95. "The value '#f' does not quote the colons; '#t' quotes them; "
  96. "'reader' quotes them when the reader option 'keywords' is not '#f'." },
  97. { SCM_OPTION_BOOLEAN, "escape-newlines", 1,
  98. "Render newlines as \\n when printing using `write'." },
  99. { SCM_OPTION_BOOLEAN, "r7rs-symbols", 0,
  100. "Escape symbols using R7RS |...| symbol notation." },
  101. { 0 },
  102. };
  103. SCM_DEFINE (scm_print_options, "print-options-interface", 0, 1, 0,
  104. (SCM setting),
  105. "Option interface for the print options. Instead of using\n"
  106. "this procedure directly, use the procedures\n"
  107. "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
  108. "and @code{print-options}.")
  109. #define FUNC_NAME s_scm_print_options
  110. {
  111. SCM ans = scm_options (setting,
  112. scm_print_opts,
  113. FUNC_NAME);
  114. return ans;
  115. }
  116. #undef FUNC_NAME
  117. /* {Printing of Scheme Objects}
  118. */
  119. /* Detection of circular references.
  120. *
  121. * Due to other constraints in the implementation, this code has bad
  122. * time complexity (O (depth * N)), The printer code can be
  123. * rewritten to be O(N).
  124. */
  125. #define PUSH_REF(pstate, obj) \
  126. do \
  127. { \
  128. PSTATE_STACK_SET (pstate, pstate->top, obj); \
  129. pstate->top++; \
  130. if (pstate->top == pstate->ceiling) \
  131. grow_ref_stack (pstate); \
  132. } while(0)
  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); \
  149. } while(0)
  150. #define EXIT_NESTED_DATA(pstate) \
  151. do \
  152. { \
  153. --pstate->top; \
  154. PSTATE_STACK_SET (pstate, pstate->top, SCM_UNDEFINED); \
  155. } \
  156. while (0)
  157. SCM scm_print_state_vtable = SCM_BOOL_F;
  158. static SCM print_state_pool = SCM_EOL;
  159. scm_i_pthread_mutex_t print_state_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  160. #ifdef GUILE_DEBUG /* Used for debugging purposes */
  161. SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0,
  162. (),
  163. "Return the current-pstate -- the car of the\n"
  164. "@code{print_state_pool}. @code{current-pstate} is only\n"
  165. "included in @code{--enable-guile-debug} builds.")
  166. #define FUNC_NAME s_scm_current_pstate
  167. {
  168. if (!scm_is_null (print_state_pool))
  169. return SCM_CAR (print_state_pool);
  170. else
  171. return SCM_BOOL_F;
  172. }
  173. #undef FUNC_NAME
  174. #endif
  175. #define PSTATE_SIZE 50L
  176. static SCM
  177. make_print_state (void)
  178. {
  179. SCM print_state = scm_make_struct_no_tail (scm_print_state_vtable, SCM_EOL);
  180. scm_print_state *pstate = SCM_PRINT_STATE (print_state);
  181. pstate->handle = print_state;
  182. pstate->ref_vect = scm_c_make_vector (PSTATE_SIZE, SCM_UNDEFINED);
  183. pstate->ceiling = SCM_SIMPLE_VECTOR_LENGTH (pstate->ref_vect);
  184. pstate->highlight_objects = SCM_EOL;
  185. return print_state;
  186. }
  187. SCM
  188. scm_make_print_state ()
  189. {
  190. SCM answer = SCM_BOOL_F;
  191. /* First try to allocate a print state from the pool */
  192. scm_i_pthread_mutex_lock (&print_state_mutex);
  193. if (!scm_is_null (print_state_pool))
  194. {
  195. answer = SCM_CAR (print_state_pool);
  196. print_state_pool = SCM_CDR (print_state_pool);
  197. }
  198. scm_i_pthread_mutex_unlock (&print_state_mutex);
  199. return scm_is_false (answer) ? make_print_state () : answer;
  200. }
  201. void
  202. scm_free_print_state (SCM print_state)
  203. {
  204. SCM handle;
  205. scm_print_state *pstate = SCM_PRINT_STATE (print_state);
  206. /* Cleanup before returning print state to pool.
  207. * It is better to do it here. Doing it in scm_prin1
  208. * would cost more since that function is called much more
  209. * often.
  210. */
  211. pstate->fancyp = 0;
  212. pstate->revealed = 0;
  213. pstate->highlight_objects = SCM_EOL;
  214. scm_i_pthread_mutex_lock (&print_state_mutex);
  215. handle = scm_cons (print_state, print_state_pool);
  216. print_state_pool = handle;
  217. scm_i_pthread_mutex_unlock (&print_state_mutex);
  218. }
  219. SCM
  220. scm_i_port_with_print_state (SCM port, SCM print_state)
  221. {
  222. if (SCM_UNBNDP (print_state))
  223. {
  224. if (SCM_PORT_WITH_PS_P (port))
  225. return port;
  226. else
  227. print_state = scm_make_print_state ();
  228. /* port does not need to be coerced since it doesn't have ps */
  229. }
  230. else
  231. port = SCM_COERCE_OUTPORT (port);
  232. return scm_new_double_smob (scm_tc16_port_with_ps,
  233. SCM_UNPACK (port), SCM_UNPACK (print_state), 0);
  234. }
  235. static void
  236. grow_ref_stack (scm_print_state *pstate)
  237. {
  238. SCM old_vect = pstate->ref_vect;
  239. size_t old_size = SCM_SIMPLE_VECTOR_LENGTH (old_vect);
  240. size_t new_size = 2 * pstate->ceiling;
  241. SCM new_vect = scm_c_make_vector (new_size, SCM_UNDEFINED);
  242. unsigned long int i;
  243. for (i = 0; i != old_size; ++i)
  244. SCM_SIMPLE_VECTOR_SET (new_vect, i, SCM_SIMPLE_VECTOR_REF (old_vect, i));
  245. pstate->ref_vect = new_vect;
  246. pstate->ceiling = new_size;
  247. }
  248. #define PSTATE_STACK_REF(p,i) SCM_SIMPLE_VECTOR_REF((p)->ref_vect, (i))
  249. #define PSTATE_STACK_SET(p,i,v) SCM_SIMPLE_VECTOR_SET((p)->ref_vect, (i), (v))
  250. static void
  251. print_circref (SCM port, scm_print_state *pstate, SCM ref)
  252. {
  253. register long i;
  254. long self = pstate->top - 1;
  255. i = pstate->top - 1;
  256. if (scm_is_pair (PSTATE_STACK_REF (pstate, i)))
  257. {
  258. while (i > 0)
  259. {
  260. if (!scm_is_pair (PSTATE_STACK_REF (pstate, i-1))
  261. || !scm_is_eq (SCM_CDR (PSTATE_STACK_REF (pstate, i-1)),
  262. SCM_CDR (PSTATE_STACK_REF (pstate, i))))
  263. break;
  264. --i;
  265. }
  266. self = i;
  267. }
  268. for (i = pstate->top - 1; 1; --i)
  269. if (scm_is_eq (PSTATE_STACK_REF(pstate, i), ref))
  270. break;
  271. scm_putc ('#', port);
  272. scm_intprint (i - self, 10, port);
  273. scm_putc ('#', port);
  274. }
  275. /* Print the name of a symbol. */
  276. static int
  277. quote_keywordish_symbols (void)
  278. {
  279. SCM option = SCM_PRINT_KEYWORD_STYLE;
  280. if (scm_is_false (option))
  281. return 0;
  282. if (scm_is_eq (option, sym_reader))
  283. return scm_is_true (SCM_PACK (SCM_KEYWORD_STYLE));
  284. return 1;
  285. }
  286. #define INITIAL_IDENTIFIER_MASK \
  287. (UC_CATEGORY_MASK_Lu | UC_CATEGORY_MASK_Ll | UC_CATEGORY_MASK_Lt \
  288. | UC_CATEGORY_MASK_Lm | UC_CATEGORY_MASK_Lo | UC_CATEGORY_MASK_Mn \
  289. | UC_CATEGORY_MASK_Nl | UC_CATEGORY_MASK_No | UC_CATEGORY_MASK_Pd \
  290. | UC_CATEGORY_MASK_Pc | UC_CATEGORY_MASK_Po | UC_CATEGORY_MASK_Sc \
  291. | UC_CATEGORY_MASK_Sm | UC_CATEGORY_MASK_Sk | UC_CATEGORY_MASK_So \
  292. | UC_CATEGORY_MASK_Co)
  293. #define SUBSEQUENT_IDENTIFIER_MASK \
  294. (INITIAL_IDENTIFIER_MASK \
  295. | UC_CATEGORY_MASK_Nd | UC_CATEGORY_MASK_Mc | UC_CATEGORY_MASK_Me)
  296. /* FIXME: Cache this information on the symbol, somehow. */
  297. static int
  298. symbol_has_extended_read_syntax (SCM sym)
  299. {
  300. size_t pos, len = scm_i_symbol_length (sym);
  301. scm_t_wchar c;
  302. /* The empty symbol. */
  303. if (len == 0)
  304. return 1;
  305. c = scm_i_symbol_ref (sym, 0);
  306. switch (c)
  307. {
  308. case '\'':
  309. case '`':
  310. case ',':
  311. case '"':
  312. case ';':
  313. case '#':
  314. /* Some initial-character constraints. */
  315. return 1;
  316. case '|':
  317. case '\\':
  318. /* R7RS allows neither '|' nor '\' in bare symbols. */
  319. if (SCM_PRINT_R7RS_SYMBOLS_P)
  320. return 1;
  321. break;
  322. case ':':
  323. /* Symbols that look like keywords. */
  324. return quote_keywordish_symbols ();
  325. case '.':
  326. /* Single dot conflicts with dotted-pair notation. */
  327. if (len == 1)
  328. return 1;
  329. /* Fall through to check numbers. */
  330. case '+':
  331. case '-':
  332. case '0':
  333. case '1':
  334. case '2':
  335. case '3':
  336. case '4':
  337. case '5':
  338. case '6':
  339. case '7':
  340. case '8':
  341. case '9':
  342. /* Number-ish symbols. Numbers with radixes already caught be #
  343. above. */
  344. if (scm_is_true (scm_i_string_to_number (scm_symbol_to_string (sym), 10)))
  345. return 1;
  346. break;
  347. default:
  348. break;
  349. }
  350. /* Other disallowed first characters. */
  351. if (!uc_is_general_category_withtable (c, INITIAL_IDENTIFIER_MASK))
  352. return 1;
  353. /* Keywords can be identified by trailing colons too. */
  354. if (scm_i_symbol_ref (sym, len - 1) == ':')
  355. return quote_keywordish_symbols ();
  356. /* Otherwise, any character that's in the identifier category mask is
  357. fine to pass through as-is, provided it's not one of the ASCII
  358. delimiters like `;'. */
  359. for (pos = 1; pos < len; pos++)
  360. {
  361. c = scm_i_symbol_ref (sym, pos);
  362. if (!uc_is_general_category_withtable (c, SUBSEQUENT_IDENTIFIER_MASK))
  363. return 1;
  364. else if (c == '"' || c == ';' || c == '#')
  365. return 1;
  366. else if ((c == '|' || c == '\\') && SCM_PRINT_R7RS_SYMBOLS_P)
  367. /* R7RS allows neither '|' nor '\' in bare symbols. */
  368. return 1;
  369. }
  370. return 0;
  371. }
  372. static void
  373. print_normal_symbol (SCM sym, SCM port)
  374. {
  375. size_t len = scm_i_symbol_length (sym);
  376. if (scm_i_is_narrow_symbol (sym))
  377. {
  378. const char *ptr = scm_i_symbol_chars (sym);
  379. scm_c_put_latin1_chars (port, (const uint8_t *) ptr, len);
  380. }
  381. else
  382. {
  383. const scm_t_wchar *ptr = scm_i_symbol_wide_chars (sym);
  384. scm_c_put_utf32_chars (port, (const uint32_t *) ptr, len);
  385. }
  386. }
  387. static void
  388. print_extended_symbol (SCM sym, SCM port)
  389. {
  390. size_t pos, len;
  391. len = scm_i_symbol_length (sym);
  392. scm_lfwrite ("#{", 2, port);
  393. for (pos = 0; pos < len; pos++)
  394. {
  395. scm_t_wchar c = scm_i_symbol_ref (sym, pos);
  396. if (uc_is_general_category_withtable (c,
  397. SUBSEQUENT_IDENTIFIER_MASK
  398. | UC_CATEGORY_MASK_Zs))
  399. scm_c_put_char (port, c);
  400. else
  401. {
  402. scm_lfwrite ("\\x", 2, port);
  403. scm_intprint (c, 16, port);
  404. scm_putc (';', port);
  405. }
  406. }
  407. scm_lfwrite ("}#", 2, port);
  408. }
  409. static void
  410. print_r7rs_extended_symbol (SCM sym, SCM port)
  411. {
  412. size_t pos, len;
  413. len = scm_i_symbol_length (sym);
  414. scm_putc ('|', port);
  415. for (pos = 0; pos < len; pos++)
  416. {
  417. scm_t_wchar c = scm_i_symbol_ref (sym, pos);
  418. switch (c)
  419. {
  420. case '\a': scm_lfwrite ("\\a", 2, port); break;
  421. case '\b': scm_lfwrite ("\\b", 2, port); break;
  422. case '\t': scm_lfwrite ("\\t", 2, port); break;
  423. case '\n': scm_lfwrite ("\\n", 2, port); break;
  424. case '\r': scm_lfwrite ("\\r", 2, port); break;
  425. case '|': scm_lfwrite ("\\|", 2, port); break;
  426. case '\\': scm_lfwrite ("\\x5c;", 5, port); break;
  427. default:
  428. if (uc_is_general_category_withtable (c,
  429. UC_CATEGORY_MASK_L
  430. | UC_CATEGORY_MASK_M
  431. | UC_CATEGORY_MASK_N
  432. | UC_CATEGORY_MASK_P
  433. | UC_CATEGORY_MASK_S)
  434. || (c == ' '))
  435. scm_c_put_char (port, c);
  436. else
  437. {
  438. scm_lfwrite ("\\x", 2, port);
  439. scm_intprint (c, 16, port);
  440. scm_putc (';', port);
  441. }
  442. break;
  443. }
  444. }
  445. scm_putc ('|', port);
  446. }
  447. /* FIXME: allow R6RS hex escapes instead of #{...}# or |...|. */
  448. static void
  449. print_symbol (SCM sym, SCM port)
  450. {
  451. if (!symbol_has_extended_read_syntax (sym))
  452. print_normal_symbol (sym, port);
  453. else if (SCM_PRINT_R7RS_SYMBOLS_P)
  454. print_r7rs_extended_symbol (sym, port);
  455. else
  456. print_extended_symbol (sym, port);
  457. }
  458. void
  459. scm_print_symbol_name (const char *str, size_t len, SCM port)
  460. {
  461. SCM symbol = scm_from_utf8_symboln (str, len);
  462. print_symbol (symbol, port);
  463. }
  464. /* Print generally. Handles both write and display according to PSTATE.
  465. */
  466. SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write);
  467. SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display);
  468. static void iprin1 (SCM exp, SCM port, scm_print_state *pstate);
  469. void
  470. scm_iprin1 (SCM exp, SCM port, scm_print_state *pstate)
  471. {
  472. if (pstate->fancyp
  473. && scm_is_true (scm_memq (exp, pstate->highlight_objects)))
  474. {
  475. scm_display (SCM_PRINT_HIGHLIGHT_PREFIX, port);
  476. iprin1 (exp, port, pstate);
  477. scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX, port);
  478. }
  479. else
  480. iprin1 (exp, port, pstate);
  481. }
  482. static void
  483. print_vector_or_weak_vector (SCM v, size_t len, SCM (*ref) (SCM, size_t),
  484. SCM port, scm_print_state *pstate)
  485. {
  486. long i;
  487. long last = len - 1;
  488. int cutp = 0;
  489. if (pstate->fancyp && len > pstate->length)
  490. {
  491. last = pstate->length - 1;
  492. cutp = 1;
  493. }
  494. for (i = 0; i < last; ++i)
  495. {
  496. scm_iprin1 (ref (v, i), port, pstate);
  497. scm_putc (' ', port);
  498. }
  499. if (i == last)
  500. {
  501. /* CHECK_INTS; */
  502. scm_iprin1 (ref (v, i), port, pstate);
  503. }
  504. if (cutp)
  505. scm_puts (" ...", port);
  506. scm_putc (')', port);
  507. }
  508. static void
  509. iprin1 (SCM exp, SCM port, scm_print_state *pstate)
  510. {
  511. switch (SCM_ITAG3 (exp))
  512. {
  513. case scm_tcs_fixnums:
  514. if (SCM_I_INUMP (exp))
  515. scm_intprint (SCM_I_INUM (exp), 10, port);
  516. else
  517. scm_i_print_fraction (exp, port, pstate);
  518. break;
  519. #ifdef scm_tcs_iflo
  520. case scm_tcs_iflo:
  521. scm_print_real (exp, port, pstate);
  522. break;
  523. #endif
  524. case scm_tc3_imm24:
  525. if (SCM_CHARP (exp))
  526. {
  527. if (SCM_WRITINGP (pstate))
  528. write_character (SCM_CHAR (exp), port);
  529. else
  530. scm_c_put_char (port, SCM_CHAR (exp));
  531. }
  532. else if (SCM_IFLAGP (exp)
  533. && ((size_t) SCM_IFLAGNUM (exp) < (sizeof iflagnames / sizeof (char *))))
  534. {
  535. scm_puts (iflagnames [SCM_IFLAGNUM (exp)], port);
  536. }
  537. else
  538. {
  539. /* unknown immediate value */
  540. scm_ipruk ("immediate", exp, port);
  541. }
  542. break;
  543. case scm_tc3_cons:
  544. switch (SCM_TYP11 (exp))
  545. {
  546. case scm_tcs_struct:
  547. {
  548. ENTER_NESTED_DATA (pstate, exp, circref);
  549. if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS)
  550. {
  551. SCM pwps, print = pstate->writingp ? g_write : g_display;
  552. if (SCM_UNPACK (print) == 0)
  553. goto print_struct;
  554. pwps = scm_i_port_with_print_state (port, pstate->handle);
  555. pstate->revealed = 1;
  556. scm_call_2 (print, exp, pwps);
  557. }
  558. else
  559. {
  560. print_struct:
  561. scm_print_struct (exp, port, pstate);
  562. }
  563. EXIT_NESTED_DATA (pstate);
  564. }
  565. break;
  566. circref:
  567. print_circref (port, pstate, exp);
  568. break;
  569. case scm_tc11_number:
  570. switch SCM_TYP16 (exp) {
  571. case scm_tc16_big:
  572. scm_bigprint (exp, port, pstate);
  573. break;
  574. case scm_tc16_real:
  575. scm_print_real (exp, port, pstate);
  576. break;
  577. case scm_tc16_complex:
  578. scm_print_complex (exp, port, pstate);
  579. break;
  580. case scm_tc16_fraction:
  581. scm_i_print_fraction (exp, port, pstate);
  582. break;
  583. }
  584. break;
  585. case scm_tc11_stringbuf:
  586. scm_i_print_stringbuf (exp, port, pstate);
  587. break;
  588. case scm_tc11_string:
  589. {
  590. size_t len = scm_i_string_length (exp);
  591. if (SCM_WRITINGP (pstate))
  592. write_string (scm_i_string_data (exp),
  593. scm_i_is_narrow_string (exp),
  594. len, port);
  595. else
  596. scm_c_put_string (port, exp, 0, len);
  597. }
  598. scm_remember_upto_here_1 (exp);
  599. break;
  600. case scm_tc11_symbol:
  601. if (scm_i_symbol_is_interned (exp))
  602. {
  603. print_symbol (exp, port);
  604. scm_remember_upto_here_1 (exp);
  605. }
  606. else
  607. {
  608. scm_puts ("#<uninterned-symbol ", port);
  609. print_symbol (exp, port);
  610. scm_putc (' ', port);
  611. scm_uintprint (SCM_UNPACK (exp), 16, port);
  612. scm_putc ('>', port);
  613. }
  614. break;
  615. case scm_tc11_variable:
  616. scm_i_variable_print (exp, port, pstate);
  617. break;
  618. case scm_tc11_values:
  619. scm_puts ("#<values (", port);
  620. print_vector_or_weak_vector (exp, scm_i_nvalues (exp),
  621. scm_c_value_ref, port, pstate);
  622. scm_puts (">", port);
  623. break;
  624. case scm_tc11_program:
  625. scm_i_program_print (exp, port, pstate);
  626. break;
  627. case scm_tc11_pointer:
  628. scm_i_pointer_print (exp, port, pstate);
  629. break;
  630. case scm_tc11_hashtable:
  631. scm_i_hashtable_print (exp, port, pstate);
  632. break;
  633. case scm_tc11_weak_set:
  634. scm_i_weak_set_print (exp, port, pstate);
  635. break;
  636. case scm_tc11_weak_table:
  637. scm_i_weak_table_print (exp, port, pstate);
  638. break;
  639. case scm_tc11_fluid:
  640. scm_i_fluid_print (exp, port, pstate);
  641. break;
  642. case scm_tc11_dynamic_state:
  643. scm_i_dynamic_state_print (exp, port, pstate);
  644. break;
  645. case scm_tc11_frame:
  646. scm_i_frame_print (exp, port, pstate);
  647. break;
  648. case scm_tc11_keyword:
  649. scm_puts ("#:", port);
  650. scm_iprin1 (scm_keyword_to_symbol (exp), port, pstate);
  651. break;
  652. case scm_tc11_syntax:
  653. scm_i_syntax_print (exp, port, pstate);
  654. break;
  655. case scm_tc11_atomic_box:
  656. scm_i_atomic_box_print (exp, port, pstate);
  657. break;
  658. case scm_tc11_vm_cont:
  659. scm_i_vm_cont_print (exp, port, pstate);
  660. break;
  661. case scm_tc11_array:
  662. ENTER_NESTED_DATA (pstate, exp, circref);
  663. scm_i_print_array (exp, port, pstate);
  664. EXIT_NESTED_DATA (pstate);
  665. break;
  666. case scm_tc11_bytevector:
  667. scm_i_print_bytevector (exp, port, pstate);
  668. break;
  669. case scm_tc11_bitvector:
  670. scm_i_print_bitvector (exp, port, pstate);
  671. break;
  672. case scm_tc11_wvect:
  673. ENTER_NESTED_DATA (pstate, exp, circref);
  674. scm_puts ("#w(", port);
  675. print_vector_or_weak_vector (exp, scm_c_weak_vector_length (exp),
  676. scm_c_weak_vector_ref, port, pstate);
  677. EXIT_NESTED_DATA (pstate);
  678. break;
  679. case scm_tc11_vector:
  680. ENTER_NESTED_DATA (pstate, exp, circref);
  681. scm_puts ("#(", port);
  682. print_vector_or_weak_vector (exp, SCM_SIMPLE_VECTOR_LENGTH (exp),
  683. scm_c_vector_ref, port, pstate);
  684. EXIT_NESTED_DATA (pstate);
  685. break;
  686. case scm_tc11_port:
  687. {
  688. scm_t_port_type *ptob = SCM_PORT_TYPE (exp);
  689. if (ptob->print && ptob->print (exp, port, pstate))
  690. break;
  691. goto punk;
  692. }
  693. case scm_tcs_smob:
  694. ENTER_NESTED_DATA (pstate, exp, circref);
  695. SCM_SMOB_DESCRIPTOR (exp).print (exp, port, pstate);
  696. EXIT_NESTED_DATA (pstate);
  697. break;
  698. default:
  699. if (scm_is_pair (exp))
  700. {
  701. ENTER_NESTED_DATA (pstate, exp, circref);
  702. scm_iprlist ("(", exp, ')', port, pstate);
  703. EXIT_NESTED_DATA (pstate);
  704. break;
  705. }
  706. /* fall through */
  707. punk:
  708. scm_ipruk ("type", exp, port);
  709. }
  710. }
  711. }
  712. /* Print states are necessary for circular reference safe printing.
  713. * They are also expensive to allocate. Therefore print states are
  714. * kept in a pool so that they can be reused.
  715. */
  716. /* The PORT argument can also be a print-state/port pair, which will
  717. * then be used instead of allocating a new print state. This is
  718. * useful for continuing a chain of print calls from Scheme. */
  719. void
  720. scm_prin1 (SCM exp, SCM port, int writingp)
  721. {
  722. SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */
  723. SCM pstate_scm;
  724. scm_print_state *pstate;
  725. int old_writingp;
  726. /* If PORT is a print-state/port pair, use that. Else create a new
  727. print-state. */
  728. if (SCM_PORT_WITH_PS_P (port))
  729. {
  730. pstate_scm = SCM_PORT_WITH_PS_PS (port);
  731. port = SCM_PORT_WITH_PS_PORT (port);
  732. }
  733. else
  734. {
  735. /* First try to allocate a print state from the pool */
  736. scm_i_pthread_mutex_lock (&print_state_mutex);
  737. if (!scm_is_null (print_state_pool))
  738. {
  739. handle = print_state_pool;
  740. print_state_pool = SCM_CDR (print_state_pool);
  741. }
  742. scm_i_pthread_mutex_unlock (&print_state_mutex);
  743. if (scm_is_false (handle))
  744. handle = scm_list_1 (make_print_state ());
  745. pstate_scm = SCM_CAR (handle);
  746. }
  747. pstate = SCM_PRINT_STATE (pstate_scm);
  748. old_writingp = pstate->writingp;
  749. pstate->writingp = writingp;
  750. scm_iprin1 (exp, port, pstate);
  751. pstate->writingp = old_writingp;
  752. /* Return print state to pool if it has been created above and
  753. hasn't escaped to Scheme. */
  754. if (scm_is_true (handle) && !pstate->revealed)
  755. {
  756. scm_i_pthread_mutex_lock (&print_state_mutex);
  757. SCM_SETCDR (handle, print_state_pool);
  758. print_state_pool = handle;
  759. scm_i_pthread_mutex_unlock (&print_state_mutex);
  760. }
  761. }
  762. static void
  763. write_string (const void *str, int narrow_p, size_t len, SCM port)
  764. {
  765. size_t i;
  766. scm_c_put_char (port, (uint8_t) '"');
  767. for (i = 0; i < len; ++i)
  768. {
  769. scm_t_wchar ch;
  770. if (narrow_p)
  771. ch = (scm_t_wchar) ((unsigned char *) (str))[i];
  772. else
  773. ch = ((scm_t_wchar *) (str))[i];
  774. /* Write CH to PORT, escaping it if it's non-graphic or not
  775. representable in PORT's encoding. If CH needs to be escaped,
  776. it is escaped using the in-string escape syntax. */
  777. if (ch == '"')
  778. scm_c_put_latin1_chars (port, (const uint8_t *) "\\\"", 2);
  779. else if (ch == '\\')
  780. scm_c_put_latin1_chars (port, (const uint8_t *) "\\\\", 2);
  781. else if (ch == '\n' && SCM_PRINT_ESCAPE_NEWLINES_P)
  782. scm_c_put_latin1_chars (port, (const uint8_t *) "\\n", 2);
  783. else if (ch == ' ' || ch == '\n'
  784. || (uc_is_general_category_withtable (ch,
  785. UC_CATEGORY_MASK_L |
  786. UC_CATEGORY_MASK_M |
  787. UC_CATEGORY_MASK_N |
  788. UC_CATEGORY_MASK_P |
  789. UC_CATEGORY_MASK_S)
  790. && scm_c_can_put_char (port, ch)))
  791. scm_c_put_char (port, ch);
  792. else
  793. scm_c_put_escaped_char (port, ch);
  794. }
  795. scm_c_put_char (port, (uint8_t) '"');
  796. }
  797. /* Write CH to PORT, escaping it if it's non-graphic or not
  798. representable in PORT's encoding. The character escape syntax is
  799. used. */
  800. static void
  801. write_character (scm_t_wchar ch, SCM port)
  802. {
  803. scm_puts ("#\\", port);
  804. /* Pretty-print a combining characters over dotted circles, if
  805. possible, to make them more visible. */
  806. if (uc_combining_class (ch) != UC_CCC_NR
  807. && scm_c_can_put_char (port, SCM_CODEPOINT_DOTTED_CIRCLE)
  808. && scm_c_can_put_char (port, ch))
  809. {
  810. scm_c_put_char (port, SCM_CODEPOINT_DOTTED_CIRCLE);
  811. scm_c_put_char (port, ch);
  812. }
  813. else if (uc_is_general_category_withtable (ch,
  814. UC_CATEGORY_MASK_L |
  815. UC_CATEGORY_MASK_M |
  816. UC_CATEGORY_MASK_N |
  817. UC_CATEGORY_MASK_P |
  818. UC_CATEGORY_MASK_S)
  819. && scm_c_can_put_char (port, ch))
  820. /* CH is graphic and encodeable; display it. */
  821. scm_c_put_char (port, ch);
  822. else
  823. /* CH isn't graphic or cannot be represented in PORT's encoding. */
  824. {
  825. /* Represent CH using the character escape syntax. */
  826. const char *name;
  827. name = scm_i_charname (SCM_MAKE_CHAR (ch));
  828. if (name != NULL)
  829. scm_puts (name, port);
  830. else if (!SCM_R6RS_ESCAPES_P)
  831. scm_intprint (ch, 8, port);
  832. else
  833. {
  834. scm_puts ("x", port);
  835. scm_intprint (ch, 16, port);
  836. }
  837. }
  838. }
  839. /* Print an integer.
  840. */
  841. void
  842. scm_intprint (intmax_t n, int radix, SCM port)
  843. {
  844. char num_buf[SCM_INTBUFLEN];
  845. scm_lfwrite (num_buf, scm_iint2str (n, radix, num_buf), port);
  846. }
  847. void
  848. scm_uintprint (uintmax_t n, int radix, SCM port)
  849. {
  850. char num_buf[SCM_INTBUFLEN];
  851. scm_lfwrite (num_buf, scm_iuint2str (n, radix, num_buf), port);
  852. }
  853. /* Print an object of unrecognized type.
  854. */
  855. void
  856. scm_ipruk (char *hdr, SCM ptr, SCM port)
  857. {
  858. scm_puts ("#<unknown-", port);
  859. scm_puts (hdr, port);
  860. if (1) /* (scm_in_heap_p (ptr)) */ /* FIXME */
  861. {
  862. scm_puts (" (0x", port);
  863. scm_uintprint (SCM_CELL_WORD_0 (ptr), 16, port);
  864. scm_puts (" . 0x", port);
  865. scm_uintprint (SCM_CELL_WORD_1 (ptr), 16, port);
  866. scm_puts (") @", port);
  867. }
  868. scm_puts (" 0x", port);
  869. scm_uintprint (SCM_UNPACK (ptr), 16, port);
  870. scm_putc ('>', port);
  871. }
  872. /* Print a list.
  873. */
  874. void
  875. scm_iprlist (char *hdr, SCM exp, int tlr, SCM port, scm_print_state *pstate)
  876. {
  877. register SCM hare, tortoise;
  878. long floor = pstate->top - 2;
  879. scm_puts (hdr, port);
  880. /* CHECK_INTS; */
  881. if (pstate->fancyp)
  882. goto fancy_printing;
  883. /* Run a hare and tortoise so that total time complexity will be
  884. O(depth * N) instead of O(N^2). */
  885. hare = SCM_CDR (exp);
  886. tortoise = exp;
  887. while (scm_is_pair (hare))
  888. {
  889. if (scm_is_eq (hare, tortoise))
  890. goto fancy_printing;
  891. hare = SCM_CDR (hare);
  892. if (!scm_is_pair (hare))
  893. break;
  894. hare = SCM_CDR (hare);
  895. tortoise = SCM_CDR (tortoise);
  896. }
  897. /* No cdr cycles intrinsic to this list */
  898. scm_iprin1 (SCM_CAR (exp), port, pstate);
  899. for (exp = SCM_CDR (exp); scm_is_pair (exp); exp = SCM_CDR (exp))
  900. {
  901. register long i;
  902. for (i = floor; i >= 0; --i)
  903. if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp))
  904. goto circref;
  905. PUSH_REF (pstate, exp);
  906. scm_putc (' ', port);
  907. /* CHECK_INTS; */
  908. scm_iprin1 (SCM_CAR (exp), port, pstate);
  909. }
  910. if (!SCM_NULL_OR_NIL_P (exp))
  911. {
  912. scm_puts (" . ", port);
  913. scm_iprin1 (exp, port, pstate);
  914. }
  915. end:
  916. scm_putc (tlr, port);
  917. pstate->top = floor + 2;
  918. return;
  919. fancy_printing:
  920. {
  921. long n = pstate->length;
  922. scm_iprin1 (SCM_CAR (exp), port, pstate);
  923. exp = SCM_CDR (exp); --n;
  924. for (; scm_is_pair (exp); exp = SCM_CDR (exp))
  925. {
  926. register unsigned long i;
  927. for (i = 0; i < pstate->top; ++i)
  928. if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp))
  929. goto fancy_circref;
  930. if (pstate->fancyp)
  931. {
  932. if (n == 0)
  933. {
  934. scm_puts (" ...", port);
  935. goto skip_tail;
  936. }
  937. else
  938. --n;
  939. }
  940. PUSH_REF(pstate, exp);
  941. ++pstate->list_offset;
  942. scm_putc (' ', port);
  943. /* CHECK_INTS; */
  944. scm_iprin1 (SCM_CAR (exp), port, pstate);
  945. }
  946. }
  947. if (!SCM_NULL_OR_NIL_P (exp))
  948. {
  949. scm_puts (" . ", port);
  950. scm_iprin1 (exp, port, pstate);
  951. }
  952. skip_tail:
  953. pstate->list_offset -= pstate->top - floor - 2;
  954. goto end;
  955. fancy_circref:
  956. pstate->list_offset -= pstate->top - floor - 2;
  957. circref:
  958. scm_puts (" . ", port);
  959. print_circref (port, pstate, exp);
  960. goto end;
  961. }
  962. int
  963. scm_valid_oport_value_p (SCM val)
  964. {
  965. return (SCM_OPOUTPORTP (val)
  966. || (SCM_PORT_WITH_PS_P (val)
  967. && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val))));
  968. }
  969. /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
  970. SCM
  971. scm_write (SCM obj, SCM port)
  972. {
  973. if (SCM_UNBNDP (port))
  974. port = scm_current_output_port ();
  975. SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write);
  976. scm_prin1 (obj, port, 1);
  977. return SCM_UNSPECIFIED;
  978. }
  979. /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
  980. SCM
  981. scm_display (SCM obj, SCM port)
  982. {
  983. if (SCM_UNBNDP (port))
  984. port = scm_current_output_port ();
  985. SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display);
  986. scm_prin1 (obj, port, 0);
  987. return SCM_UNSPECIFIED;
  988. }
  989. SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1,
  990. (SCM destination, SCM message, SCM args),
  991. "Write @var{message} to @var{destination}, defaulting to\n"
  992. "the current output port.\n"
  993. "@var{message} can contain @code{~A} (was @code{%s}) and\n"
  994. "@code{~S} (was @code{%S}) escapes. When printed,\n"
  995. "the escapes are replaced with corresponding members of\n"
  996. "@var{args}:\n"
  997. "@code{~A} formats using @code{display} and @code{~S} formats\n"
  998. "using @code{write}.\n"
  999. "If @var{destination} is @code{#t}, then use the current output\n"
  1000. "port, if @var{destination} is @code{#f}, then return a string\n"
  1001. "containing the formatted text. Does not add a trailing newline.")
  1002. #define FUNC_NAME s_scm_simple_format
  1003. {
  1004. SCM port, answer = SCM_UNSPECIFIED;
  1005. int fReturnString = 0;
  1006. int writingp;
  1007. size_t start, p, end;
  1008. if (scm_is_eq (destination, SCM_BOOL_T))
  1009. {
  1010. destination = port = scm_current_output_port ();
  1011. SCM_VALIDATE_OPORT_VALUE (1, destination);
  1012. }
  1013. else if (scm_is_false (destination))
  1014. {
  1015. fReturnString = 1;
  1016. port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, SCM_WRTNG, FUNC_NAME);
  1017. destination = port;
  1018. }
  1019. else
  1020. {
  1021. SCM_VALIDATE_OPORT_VALUE (1, destination);
  1022. port = SCM_COERCE_OUTPORT (destination);
  1023. }
  1024. SCM_VALIDATE_STRING (2, message);
  1025. SCM_VALIDATE_REST_ARGUMENT (args);
  1026. p = 0;
  1027. start = 0;
  1028. end = scm_i_string_length (message);
  1029. for (p = start; p != end; ++p)
  1030. if (scm_i_string_ref (message, p) == '~')
  1031. {
  1032. if (++p == end)
  1033. break;
  1034. switch (scm_i_string_ref (message, p))
  1035. {
  1036. case 'A': case 'a':
  1037. writingp = 0;
  1038. break;
  1039. case 'S': case 's':
  1040. writingp = 1;
  1041. break;
  1042. case '~':
  1043. scm_lfwrite_substr (message, start, p, port);
  1044. start = p + 1;
  1045. continue;
  1046. case '%':
  1047. scm_lfwrite_substr (message, start, p - 1, port);
  1048. scm_newline (port);
  1049. start = p + 1;
  1050. continue;
  1051. default:
  1052. SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
  1053. scm_list_1 (scm_c_make_char (scm_i_string_ref (message, p))));
  1054. }
  1055. if (!scm_is_pair (args))
  1056. SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
  1057. scm_list_1 (scm_c_make_char (scm_i_string_ref (message, p))));
  1058. scm_lfwrite_substr (message, start, p - 1, port);
  1059. /* we pass destination here */
  1060. scm_prin1 (SCM_CAR (args), destination, writingp);
  1061. args = SCM_CDR (args);
  1062. start = p + 1;
  1063. }
  1064. scm_lfwrite_substr (message, start, p, port);
  1065. if (!scm_is_eq (args, SCM_EOL))
  1066. SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
  1067. scm_list_1 (scm_length (args)));
  1068. if (fReturnString)
  1069. answer = scm_strport_to_string (destination);
  1070. return scm_return_first (answer, message);
  1071. }
  1072. #undef FUNC_NAME
  1073. SCM_DEFINE (scm_newline, "newline", 0, 1, 0,
  1074. (SCM port),
  1075. "Send a newline to @var{port}.\n"
  1076. "If @var{port} is omitted, send to the current output port.")
  1077. #define FUNC_NAME s_scm_newline
  1078. {
  1079. if (SCM_UNBNDP (port))
  1080. port = scm_current_output_port ();
  1081. SCM_VALIDATE_OPORT_VALUE (1, port);
  1082. scm_putc ('\n', SCM_COERCE_OUTPORT (port));
  1083. return SCM_UNSPECIFIED;
  1084. }
  1085. #undef FUNC_NAME
  1086. SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0,
  1087. (SCM chr, SCM port),
  1088. "Send character @var{chr} to @var{port}.")
  1089. #define FUNC_NAME s_scm_write_char
  1090. {
  1091. if (SCM_UNBNDP (port))
  1092. port = scm_current_output_port ();
  1093. else
  1094. port = SCM_COERCE_OUTPORT (port);
  1095. SCM_VALIDATE_CHAR (1, chr);
  1096. SCM_VALIDATE_OPOUTPORT (2, port);
  1097. scm_c_put_char (port, SCM_CHAR (chr));
  1098. return SCM_UNSPECIFIED;
  1099. }
  1100. #undef FUNC_NAME
  1101. /* Call back to Scheme code to do the printing of special objects
  1102. * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
  1103. * containing PORT and PSTATE. This object can be used as the port for
  1104. * display/write etc to continue the current print chain. The REVEALED
  1105. * field of PSTATE is set to true to indicate that the print state has
  1106. * escaped to Scheme and thus has to be freed by the GC.
  1107. */
  1108. scm_t_bits scm_tc16_port_with_ps;
  1109. /* Print exactly as the port itself would */
  1110. static int
  1111. port_with_ps_print (SCM obj, SCM port, scm_print_state *pstate)
  1112. {
  1113. obj = SCM_PORT_WITH_PS_PORT (obj);
  1114. return SCM_PORT_TYPE (obj)->print (obj, port, pstate);
  1115. }
  1116. SCM
  1117. scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate)
  1118. {
  1119. pstate->revealed = 1;
  1120. return scm_call_2 (proc, exp,
  1121. scm_i_port_with_print_state (port, pstate->handle));
  1122. }
  1123. SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 1, 1, 0,
  1124. (SCM port, SCM pstate),
  1125. "Create a new port which behaves like @var{port}, but with an\n"
  1126. "included print state @var{pstate}. @var{pstate} is optional.\n"
  1127. "If @var{pstate} isn't supplied and @var{port} already has\n"
  1128. "a print state, the old print state is reused.")
  1129. #define FUNC_NAME s_scm_port_with_print_state
  1130. {
  1131. SCM_VALIDATE_OPORT_VALUE (1, port);
  1132. if (!SCM_UNBNDP (pstate))
  1133. SCM_VALIDATE_PRINTSTATE (2, pstate);
  1134. return scm_i_port_with_print_state (port, pstate);
  1135. }
  1136. #undef FUNC_NAME
  1137. SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0,
  1138. (SCM port),
  1139. "Return the print state of the port @var{port}. If @var{port}\n"
  1140. "has no associated print state, @code{#f} is returned.")
  1141. #define FUNC_NAME s_scm_get_print_state
  1142. {
  1143. if (SCM_PORT_WITH_PS_P (port))
  1144. return SCM_PORT_WITH_PS_PS (port);
  1145. if (SCM_OUTPUT_PORT_P (port))
  1146. return SCM_BOOL_F;
  1147. SCM_WRONG_TYPE_ARG (1, port);
  1148. }
  1149. #undef FUNC_NAME
  1150. void
  1151. scm_init_print ()
  1152. {
  1153. SCM type;
  1154. type = scm_make_vtable (scm_from_utf8_string (SCM_PRINT_STATE_LAYOUT),
  1155. SCM_BOOL_F);
  1156. scm_set_struct_vtable_name_x (type, scm_from_utf8_symbol ("print-state"));
  1157. scm_print_state_vtable = type;
  1158. /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
  1159. scm_tc16_port_with_ps = scm_make_smob_type (0, 0);
  1160. scm_set_smob_print (scm_tc16_port_with_ps, port_with_ps_print);
  1161. #include "print.x"
  1162. scm_init_opts (scm_print_options, scm_print_opts);
  1163. scm_print_opts[SCM_PRINT_HIGHLIGHT_PREFIX_I].val =
  1164. SCM_UNPACK (scm_from_utf8_string ("{"));
  1165. scm_print_opts[SCM_PRINT_HIGHLIGHT_SUFFIX_I].val =
  1166. SCM_UNPACK (scm_from_utf8_string ("}"));
  1167. scm_print_opts[SCM_PRINT_KEYWORD_STYLE_I].val = SCM_UNPACK (sym_reader);
  1168. }