print.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. /* Copyright (C) 1995-1999, 2000, 2001, 2002, 2003, 2004, 2006, 2008,
  2. * 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <errno.h>
  23. #include <iconv.h>
  24. #include <stdio.h>
  25. #include <assert.h>
  26. #include <uniconv.h>
  27. #include <unictype.h>
  28. #include "libguile/_scm.h"
  29. #include "libguile/chars.h"
  30. #include "libguile/continuations.h"
  31. #include "libguile/smob.h"
  32. #include "libguile/control.h"
  33. #include "libguile/eval.h"
  34. #include "libguile/macros.h"
  35. #include "libguile/procprop.h"
  36. #include "libguile/read.h"
  37. #include "libguile/programs.h"
  38. #include "libguile/alist.h"
  39. #include "libguile/struct.h"
  40. #include "libguile/ports.h"
  41. #include "libguile/root.h"
  42. #include "libguile/strings.h"
  43. #include "libguile/strports.h"
  44. #include "libguile/vectors.h"
  45. #include "libguile/numbers.h"
  46. #include "libguile/vm.h"
  47. #include "libguile/validate.h"
  48. #include "libguile/print.h"
  49. #include "libguile/private-options.h"
  50. /* Character printers. */
  51. static size_t display_string (const void *, int, size_t, SCM,
  52. scm_t_string_failed_conversion_handler);
  53. static int display_character (scm_t_wchar, SCM,
  54. scm_t_string_failed_conversion_handler);
  55. static void write_character (scm_t_wchar, SCM, int);
  56. static void write_character_escaped (scm_t_wchar, int, SCM);
  57. /* {Names of immediate symbols}
  58. *
  59. * This table must agree with the declarations in scm.h: {Immediate Symbols}.
  60. */
  61. /* This table must agree with the list of flags in tags.h. */
  62. static const char *iflagnames[] =
  63. {
  64. "#f",
  65. "#nil", /* Elisp nil value. Should print from elisp as symbol `nil'. */
  66. "#<XXX UNUSED LISP FALSE -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
  67. "()",
  68. "#t",
  69. "#<XXX UNUSED BOOLEAN 0 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
  70. "#<XXX UNUSED BOOLEAN 1 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
  71. "#<XXX UNUSED BOOLEAN 2 -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>",
  72. "#<unspecified>",
  73. "#<undefined>",
  74. "#<eof>",
  75. /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */
  76. "#<unbound>",
  77. };
  78. SCM_SYMBOL (sym_reader, "reader");
  79. scm_t_option scm_print_opts[] = {
  80. { SCM_OPTION_SCM, "highlight-prefix", (scm_t_bits)SCM_BOOL_F_BITS,
  81. "The string to print before highlighted values." },
  82. { SCM_OPTION_SCM, "highlight-suffix", (scm_t_bits)SCM_BOOL_F_BITS,
  83. "The string to print after highlighted values." },
  84. { SCM_OPTION_SCM, "quote-keywordish-symbols", (scm_t_bits)SCM_BOOL_F_BITS,
  85. "How to print symbols that have a colon as their first or last character. "
  86. "The value '#f' does not quote the colons; '#t' quotes them; "
  87. "'reader' quotes them when the reader option 'keywords' is not '#f'." },
  88. { SCM_OPTION_BOOLEAN, "escape-newlines", 1,
  89. "Render newlines as \\n when printing using `write'." },
  90. { 0 },
  91. };
  92. SCM_DEFINE (scm_print_options, "print-options-interface", 0, 1, 0,
  93. (SCM setting),
  94. "Option interface for the print options. Instead of using\n"
  95. "this procedure directly, use the procedures\n"
  96. "@code{print-enable}, @code{print-disable}, @code{print-set!}\n"
  97. "and @code{print-options}.")
  98. #define FUNC_NAME s_scm_print_options
  99. {
  100. SCM ans = scm_options (setting,
  101. scm_print_opts,
  102. FUNC_NAME);
  103. return ans;
  104. }
  105. #undef FUNC_NAME
  106. /* {Printing of Scheme Objects}
  107. */
  108. /* Detection of circular references.
  109. *
  110. * Due to other constraints in the implementation, this code has bad
  111. * time complexity (O (depth * N)), The printer code can be
  112. * rewritten to be O(N).
  113. */
  114. #define PUSH_REF(pstate, obj) \
  115. do \
  116. { \
  117. PSTATE_STACK_SET (pstate, pstate->top, obj); \
  118. pstate->top++; \
  119. if (pstate->top == pstate->ceiling) \
  120. grow_ref_stack (pstate); \
  121. } while(0)
  122. #define ENTER_NESTED_DATA(pstate, obj, label) \
  123. do \
  124. { \
  125. register unsigned long i; \
  126. for (i = 0; i < pstate->top; ++i) \
  127. if (scm_is_eq (PSTATE_STACK_REF (pstate, i), (obj))) \
  128. goto label; \
  129. if (pstate->fancyp) \
  130. { \
  131. if (pstate->top - pstate->list_offset >= pstate->level) \
  132. { \
  133. scm_putc_unlocked ('#', port); \
  134. return; \
  135. } \
  136. } \
  137. PUSH_REF(pstate, obj); \
  138. } while(0)
  139. #define EXIT_NESTED_DATA(pstate) \
  140. do \
  141. { \
  142. --pstate->top; \
  143. PSTATE_STACK_SET (pstate, pstate->top, SCM_UNDEFINED); \
  144. } \
  145. while (0)
  146. SCM scm_print_state_vtable = SCM_BOOL_F;
  147. static SCM print_state_pool = SCM_EOL;
  148. scm_i_pthread_mutex_t print_state_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  149. #ifdef GUILE_DEBUG /* Used for debugging purposes */
  150. SCM_DEFINE (scm_current_pstate, "current-pstate", 0, 0, 0,
  151. (),
  152. "Return the current-pstate -- the car of the\n"
  153. "@code{print_state_pool}. @code{current-pstate} is only\n"
  154. "included in @code{--enable-guile-debug} builds.")
  155. #define FUNC_NAME s_scm_current_pstate
  156. {
  157. if (!scm_is_null (print_state_pool))
  158. return SCM_CAR (print_state_pool);
  159. else
  160. return SCM_BOOL_F;
  161. }
  162. #undef FUNC_NAME
  163. #endif
  164. #define PSTATE_SIZE 50L
  165. static SCM
  166. make_print_state (void)
  167. {
  168. SCM print_state
  169. = scm_make_struct (scm_print_state_vtable, SCM_INUM0, SCM_EOL);
  170. scm_print_state *pstate = SCM_PRINT_STATE (print_state);
  171. pstate->ref_vect = scm_c_make_vector (PSTATE_SIZE, SCM_UNDEFINED);
  172. pstate->ceiling = SCM_SIMPLE_VECTOR_LENGTH (pstate->ref_vect);
  173. pstate->highlight_objects = SCM_EOL;
  174. return print_state;
  175. }
  176. SCM
  177. scm_make_print_state ()
  178. {
  179. SCM answer = SCM_BOOL_F;
  180. /* First try to allocate a print state from the pool */
  181. scm_i_pthread_mutex_lock (&print_state_mutex);
  182. if (!scm_is_null (print_state_pool))
  183. {
  184. answer = SCM_CAR (print_state_pool);
  185. print_state_pool = SCM_CDR (print_state_pool);
  186. }
  187. scm_i_pthread_mutex_unlock (&print_state_mutex);
  188. return scm_is_false (answer) ? make_print_state () : answer;
  189. }
  190. void
  191. scm_free_print_state (SCM print_state)
  192. {
  193. SCM handle;
  194. scm_print_state *pstate = SCM_PRINT_STATE (print_state);
  195. /* Cleanup before returning print state to pool.
  196. * It is better to do it here. Doing it in scm_prin1
  197. * would cost more since that function is called much more
  198. * often.
  199. */
  200. pstate->fancyp = 0;
  201. pstate->revealed = 0;
  202. pstate->highlight_objects = SCM_EOL;
  203. scm_i_pthread_mutex_lock (&print_state_mutex);
  204. handle = scm_cons (print_state, print_state_pool);
  205. print_state_pool = handle;
  206. scm_i_pthread_mutex_unlock (&print_state_mutex);
  207. }
  208. SCM
  209. scm_i_port_with_print_state (SCM port, SCM print_state)
  210. {
  211. if (SCM_UNBNDP (print_state))
  212. {
  213. if (SCM_PORT_WITH_PS_P (port))
  214. return port;
  215. else
  216. print_state = scm_make_print_state ();
  217. /* port does not need to be coerced since it doesn't have ps */
  218. }
  219. else
  220. port = SCM_COERCE_OUTPORT (port);
  221. SCM_RETURN_NEWSMOB (scm_tc16_port_with_ps,
  222. SCM_UNPACK (scm_cons (port, print_state)));
  223. }
  224. static void
  225. grow_ref_stack (scm_print_state *pstate)
  226. {
  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;
  230. SCM new_vect = scm_c_make_vector (new_size, SCM_UNDEFINED);
  231. unsigned long int i;
  232. for (i = 0; i != old_size; ++i)
  233. SCM_SIMPLE_VECTOR_SET (new_vect, i, SCM_SIMPLE_VECTOR_REF (old_vect, i));
  234. pstate->ref_vect = new_vect;
  235. pstate->ceiling = new_size;
  236. }
  237. #define PSTATE_STACK_REF(p,i) SCM_SIMPLE_VECTOR_REF((p)->ref_vect, (i))
  238. #define PSTATE_STACK_SET(p,i,v) SCM_SIMPLE_VECTOR_SET((p)->ref_vect, (i), (v))
  239. static void
  240. print_circref (SCM port, scm_print_state *pstate, SCM ref)
  241. {
  242. register long i;
  243. long self = pstate->top - 1;
  244. i = pstate->top - 1;
  245. if (scm_is_pair (PSTATE_STACK_REF (pstate, i)))
  246. {
  247. while (i > 0)
  248. {
  249. if (!scm_is_pair (PSTATE_STACK_REF (pstate, i-1))
  250. || !scm_is_eq (SCM_CDR (PSTATE_STACK_REF (pstate, i-1)),
  251. SCM_CDR (PSTATE_STACK_REF (pstate, i))))
  252. break;
  253. --i;
  254. }
  255. self = i;
  256. }
  257. for (i = pstate->top - 1; 1; --i)
  258. if (scm_is_eq (PSTATE_STACK_REF(pstate, i), ref))
  259. break;
  260. scm_putc_unlocked ('#', port);
  261. scm_intprint (i - self, 10, port);
  262. scm_putc_unlocked ('#', port);
  263. }
  264. /* Print the name of a symbol. */
  265. static int
  266. quote_keywordish_symbols (void)
  267. {
  268. SCM option = SCM_PRINT_KEYWORD_STYLE;
  269. if (scm_is_false (option))
  270. return 0;
  271. if (scm_is_eq (option, sym_reader))
  272. return scm_is_true (SCM_PACK (SCM_KEYWORD_STYLE));
  273. return 1;
  274. }
  275. #define INITIAL_IDENTIFIER_MASK \
  276. (UC_CATEGORY_MASK_Lu | UC_CATEGORY_MASK_Ll | UC_CATEGORY_MASK_Lt \
  277. | UC_CATEGORY_MASK_Lm | UC_CATEGORY_MASK_Lo | UC_CATEGORY_MASK_Mn \
  278. | UC_CATEGORY_MASK_Nl | UC_CATEGORY_MASK_No | UC_CATEGORY_MASK_Pd \
  279. | UC_CATEGORY_MASK_Pc | UC_CATEGORY_MASK_Po | UC_CATEGORY_MASK_Sc \
  280. | UC_CATEGORY_MASK_Sm | UC_CATEGORY_MASK_Sk | UC_CATEGORY_MASK_So \
  281. | UC_CATEGORY_MASK_Co)
  282. #define SUBSEQUENT_IDENTIFIER_MASK \
  283. (INITIAL_IDENTIFIER_MASK \
  284. | UC_CATEGORY_MASK_Nd | UC_CATEGORY_MASK_Mc | UC_CATEGORY_MASK_Me)
  285. /* FIXME: Cache this information on the symbol, somehow. */
  286. static int
  287. symbol_has_extended_read_syntax (SCM sym)
  288. {
  289. size_t pos, len = scm_i_symbol_length (sym);
  290. scm_t_wchar c;
  291. /* The empty symbol. */
  292. if (len == 0)
  293. return 1;
  294. c = scm_i_symbol_ref (sym, 0);
  295. switch (c)
  296. {
  297. case '\'':
  298. case '`':
  299. case ',':
  300. case '"':
  301. case ';':
  302. case '#':
  303. /* Some initial-character constraints. */
  304. return 1;
  305. case ':':
  306. /* Symbols that look like keywords. */
  307. return quote_keywordish_symbols ();
  308. case '.':
  309. /* Single dot conflicts with dotted-pair notation. */
  310. if (len == 1)
  311. return 1;
  312. /* Fall through to check numbers. */
  313. case '+':
  314. case '-':
  315. case '0':
  316. case '1':
  317. case '2':
  318. case '3':
  319. case '4':
  320. case '5':
  321. case '6':
  322. case '7':
  323. case '8':
  324. case '9':
  325. /* Number-ish symbols. Numbers with radixes already caught be #
  326. above. */
  327. if (scm_is_true (scm_i_string_to_number (scm_symbol_to_string (sym), 10)))
  328. return 1;
  329. break;
  330. default:
  331. break;
  332. }
  333. /* Other disallowed first characters. */
  334. if (!uc_is_general_category_withtable (c, INITIAL_IDENTIFIER_MASK))
  335. return 1;
  336. /* Keywords can be identified by trailing colons too. */
  337. if (scm_i_symbol_ref (sym, len - 1) == ':')
  338. return quote_keywordish_symbols ();
  339. /* Otherwise, any character that's in the identifier category mask is
  340. fine to pass through as-is, provided it's not one of the ASCII
  341. delimiters like `;'. */
  342. for (pos = 1; pos < len; pos++)
  343. {
  344. c = scm_i_symbol_ref (sym, pos);
  345. if (!uc_is_general_category_withtable (c, SUBSEQUENT_IDENTIFIER_MASK))
  346. return 1;
  347. else if (c == '"' || c == ';' || c == '#')
  348. return 1;
  349. }
  350. return 0;
  351. }
  352. static void
  353. print_normal_symbol (SCM sym, SCM port)
  354. {
  355. size_t len;
  356. scm_t_string_failed_conversion_handler strategy;
  357. len = scm_i_symbol_length (sym);
  358. strategy = scm_i_get_conversion_strategy (port);
  359. if (scm_i_is_narrow_symbol (sym))
  360. display_string (scm_i_symbol_chars (sym), 1, len, port, strategy);
  361. else
  362. display_string (scm_i_symbol_wide_chars (sym), 0, len, port, strategy);
  363. }
  364. static void
  365. print_extended_symbol (SCM sym, SCM port)
  366. {
  367. size_t pos, len;
  368. scm_t_string_failed_conversion_handler strategy;
  369. len = scm_i_symbol_length (sym);
  370. strategy = scm_i_get_conversion_strategy (port);
  371. scm_lfwrite_unlocked ("#{", 2, port);
  372. for (pos = 0; pos < len; pos++)
  373. {
  374. scm_t_wchar c = scm_i_symbol_ref (sym, pos);
  375. if (uc_is_general_category_withtable (c,
  376. SUBSEQUENT_IDENTIFIER_MASK
  377. | UC_CATEGORY_MASK_Zs))
  378. {
  379. if (!display_character (c, port, strategy))
  380. scm_encoding_error ("print_extended_symbol", errno,
  381. "cannot convert to output locale",
  382. port, SCM_MAKE_CHAR (c));
  383. }
  384. else
  385. {
  386. display_string ("\\x", 1, 2, port, iconveh_question_mark);
  387. scm_intprint (c, 16, port);
  388. display_character (';', port, iconveh_question_mark);
  389. }
  390. }
  391. scm_lfwrite_unlocked ("}#", 2, port);
  392. }
  393. /* FIXME: allow R6RS hex escapes instead of #{...}#. */
  394. static void
  395. print_symbol (SCM sym, SCM port)
  396. {
  397. if (symbol_has_extended_read_syntax (sym))
  398. print_extended_symbol (sym, port);
  399. else
  400. print_normal_symbol (sym, port);
  401. }
  402. void
  403. scm_print_symbol_name (const char *str, size_t len, SCM port)
  404. {
  405. SCM symbol = scm_from_utf8_symboln (str, len);
  406. print_symbol (symbol, port);
  407. }
  408. /* Print generally. Handles both write and display according to PSTATE.
  409. */
  410. SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write);
  411. SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display);
  412. static void iprin1 (SCM exp, SCM port, scm_print_state *pstate);
  413. /* Print a character as an octal or hex escape. */
  414. #define PRINT_CHAR_ESCAPE(i, port) \
  415. do \
  416. { \
  417. if (!SCM_R6RS_ESCAPES_P) \
  418. scm_intprint (i, 8, port); \
  419. else \
  420. { \
  421. scm_puts_unlocked ("x", port); \
  422. scm_intprint (i, 16, port); \
  423. } \
  424. } \
  425. while (0)
  426. void
  427. scm_iprin1 (SCM exp, SCM port, scm_print_state *pstate)
  428. {
  429. if (pstate->fancyp
  430. && scm_is_true (scm_memq (exp, pstate->highlight_objects)))
  431. {
  432. scm_display (SCM_PRINT_HIGHLIGHT_PREFIX, port);
  433. iprin1 (exp, port, pstate);
  434. scm_display (SCM_PRINT_HIGHLIGHT_SUFFIX, port);
  435. }
  436. else
  437. iprin1 (exp, port, pstate);
  438. }
  439. static void
  440. iprin1 (SCM exp, SCM port, scm_print_state *pstate)
  441. {
  442. switch (SCM_ITAG3 (exp))
  443. {
  444. case scm_tc3_tc7_1:
  445. case scm_tc3_tc7_2:
  446. /* These tc3 tags should never occur in an immediate value. They are
  447. * only used in cell types of non-immediates, i. e. the value returned
  448. * by SCM_CELL_TYPE (exp) can use these tags.
  449. */
  450. scm_ipruk ("immediate", exp, port);
  451. break;
  452. case scm_tc3_int_1:
  453. case scm_tc3_int_2:
  454. scm_intprint (SCM_I_INUM (exp), 10, port);
  455. break;
  456. case scm_tc3_imm24:
  457. if (SCM_CHARP (exp))
  458. {
  459. if (SCM_WRITINGP (pstate))
  460. write_character (SCM_CHAR (exp), port, 0);
  461. else
  462. {
  463. if (!display_character (SCM_CHAR (exp), port,
  464. scm_i_get_conversion_strategy (port)))
  465. scm_encoding_error (__func__, errno,
  466. "cannot convert to output locale",
  467. port, exp);
  468. }
  469. }
  470. else if (SCM_IFLAGP (exp)
  471. && ((size_t) SCM_IFLAGNUM (exp) < (sizeof iflagnames / sizeof (char *))))
  472. {
  473. scm_puts_unlocked (iflagnames [SCM_IFLAGNUM (exp)], port);
  474. }
  475. else
  476. {
  477. /* unknown immediate value */
  478. scm_ipruk ("immediate", exp, port);
  479. }
  480. break;
  481. case scm_tc3_cons:
  482. switch (SCM_TYP7 (exp))
  483. {
  484. case scm_tcs_struct:
  485. {
  486. ENTER_NESTED_DATA (pstate, exp, circref);
  487. if (SCM_OBJ_CLASS_FLAGS (exp) & SCM_CLASSF_GOOPS)
  488. {
  489. SCM pwps, print = pstate->writingp ? g_write : g_display;
  490. if (SCM_UNPACK (print) == 0)
  491. goto print_struct;
  492. pwps = scm_i_port_with_print_state (port, pstate->handle);
  493. pstate->revealed = 1;
  494. scm_call_2 (print, exp, pwps);
  495. }
  496. else
  497. {
  498. print_struct:
  499. scm_print_struct (exp, port, pstate);
  500. }
  501. EXIT_NESTED_DATA (pstate);
  502. }
  503. break;
  504. case scm_tcs_cons_imcar:
  505. case scm_tcs_cons_nimcar:
  506. ENTER_NESTED_DATA (pstate, exp, circref);
  507. scm_iprlist ("(", exp, ')', port, pstate);
  508. EXIT_NESTED_DATA (pstate);
  509. break;
  510. circref:
  511. print_circref (port, pstate, exp);
  512. break;
  513. case scm_tc7_number:
  514. switch SCM_TYP16 (exp) {
  515. case scm_tc16_big:
  516. scm_bigprint (exp, port, pstate);
  517. break;
  518. case scm_tc16_real:
  519. scm_print_real (exp, port, pstate);
  520. break;
  521. case scm_tc16_complex:
  522. scm_print_complex (exp, port, pstate);
  523. break;
  524. case scm_tc16_fraction:
  525. scm_i_print_fraction (exp, port, pstate);
  526. break;
  527. }
  528. break;
  529. case scm_tc7_string:
  530. if (SCM_WRITINGP (pstate))
  531. {
  532. size_t len, i;
  533. display_character ('"', port, iconveh_question_mark);
  534. len = scm_i_string_length (exp);
  535. for (i = 0; i < len; ++i)
  536. write_character (scm_i_string_ref (exp, i), port, 1);
  537. display_character ('"', port, iconveh_question_mark);
  538. scm_remember_upto_here_1 (exp);
  539. }
  540. else
  541. {
  542. size_t len, printed;
  543. len = scm_i_string_length (exp);
  544. printed = display_string (scm_i_string_data (exp),
  545. scm_i_is_narrow_string (exp),
  546. len, port,
  547. scm_i_get_conversion_strategy (port));
  548. if (SCM_UNLIKELY (printed < len))
  549. scm_encoding_error (__func__, errno,
  550. "cannot convert to output locale",
  551. port, scm_c_string_ref (exp, printed));
  552. }
  553. scm_remember_upto_here_1 (exp);
  554. break;
  555. case scm_tc7_symbol:
  556. if (scm_i_symbol_is_interned (exp))
  557. {
  558. print_symbol (exp, port);
  559. scm_remember_upto_here_1 (exp);
  560. }
  561. else
  562. {
  563. scm_puts_unlocked ("#<uninterned-symbol ", port);
  564. print_symbol (exp, port);
  565. scm_putc_unlocked (' ', port);
  566. scm_uintprint (SCM_UNPACK (exp), 16, port);
  567. scm_putc_unlocked ('>', port);
  568. }
  569. break;
  570. case scm_tc7_variable:
  571. scm_i_variable_print (exp, port, pstate);
  572. break;
  573. case scm_tc7_program:
  574. scm_i_program_print (exp, port, pstate);
  575. break;
  576. case scm_tc7_pointer:
  577. scm_i_pointer_print (exp, port, pstate);
  578. break;
  579. case scm_tc7_hashtable:
  580. scm_i_hashtable_print (exp, port, pstate);
  581. break;
  582. case scm_tc7_weak_set:
  583. scm_i_weak_set_print (exp, port, pstate);
  584. break;
  585. case scm_tc7_weak_table:
  586. scm_i_weak_table_print (exp, port, pstate);
  587. break;
  588. case scm_tc7_fluid:
  589. scm_i_fluid_print (exp, port, pstate);
  590. break;
  591. case scm_tc7_dynamic_state:
  592. scm_i_dynamic_state_print (exp, port, pstate);
  593. break;
  594. case scm_tc7_frame:
  595. scm_i_frame_print (exp, port, pstate);
  596. break;
  597. case scm_tc7_objcode:
  598. scm_i_objcode_print (exp, port, pstate);
  599. break;
  600. case scm_tc7_vm:
  601. scm_i_vm_print (exp, port, pstate);
  602. break;
  603. case scm_tc7_vm_cont:
  604. scm_i_vm_cont_print (exp, port, pstate);
  605. break;
  606. case scm_tc7_array:
  607. ENTER_NESTED_DATA (pstate, exp, circref);
  608. scm_i_print_array (exp, port, pstate);
  609. EXIT_NESTED_DATA (pstate);
  610. break;
  611. case scm_tc7_bytevector:
  612. scm_i_print_bytevector (exp, port, pstate);
  613. break;
  614. case scm_tc7_bitvector:
  615. scm_i_print_bitvector (exp, port, pstate);
  616. break;
  617. case scm_tc7_wvect:
  618. ENTER_NESTED_DATA (pstate, exp, circref);
  619. scm_puts_unlocked ("#w(", port);
  620. goto common_vector_printer;
  621. case scm_tc7_vector:
  622. ENTER_NESTED_DATA (pstate, exp, circref);
  623. scm_puts_unlocked ("#(", port);
  624. common_vector_printer:
  625. {
  626. register long i;
  627. long last = SCM_SIMPLE_VECTOR_LENGTH (exp) - 1;
  628. int cutp = 0;
  629. if (pstate->fancyp
  630. && SCM_SIMPLE_VECTOR_LENGTH (exp) > pstate->length)
  631. {
  632. last = pstate->length - 1;
  633. cutp = 1;
  634. }
  635. for (i = 0; i < last; ++i)
  636. {
  637. scm_iprin1 (scm_c_vector_ref (exp, i), port, pstate);
  638. scm_putc_unlocked (' ', port);
  639. }
  640. if (i == last)
  641. {
  642. /* CHECK_INTS; */
  643. scm_iprin1 (scm_c_vector_ref (exp, i), port, pstate);
  644. }
  645. if (cutp)
  646. scm_puts_unlocked (" ...", port);
  647. scm_putc_unlocked (')', port);
  648. }
  649. EXIT_NESTED_DATA (pstate);
  650. break;
  651. case scm_tc7_port:
  652. {
  653. scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (exp);
  654. if (ptob->print && ptob->print (exp, port, pstate))
  655. break;
  656. goto punk;
  657. }
  658. case scm_tc7_smob:
  659. ENTER_NESTED_DATA (pstate, exp, circref);
  660. SCM_SMOB_DESCRIPTOR (exp).print (exp, port, pstate);
  661. EXIT_NESTED_DATA (pstate);
  662. break;
  663. default:
  664. /* case scm_tcs_closures: */
  665. punk:
  666. scm_ipruk ("type", exp, port);
  667. }
  668. }
  669. }
  670. /* Print states are necessary for circular reference safe printing.
  671. * They are also expensive to allocate. Therefore print states are
  672. * kept in a pool so that they can be reused.
  673. */
  674. /* The PORT argument can also be a print-state/port pair, which will
  675. * then be used instead of allocating a new print state. This is
  676. * useful for continuing a chain of print calls from Scheme. */
  677. void
  678. scm_prin1 (SCM exp, SCM port, int writingp)
  679. {
  680. SCM handle = SCM_BOOL_F; /* Will GC protect the handle whilst unlinked */
  681. SCM pstate_scm;
  682. scm_print_state *pstate;
  683. int old_writingp;
  684. /* If PORT is a print-state/port pair, use that. Else create a new
  685. print-state. */
  686. if (SCM_PORT_WITH_PS_P (port))
  687. {
  688. pstate_scm = SCM_PORT_WITH_PS_PS (port);
  689. port = SCM_PORT_WITH_PS_PORT (port);
  690. }
  691. else
  692. {
  693. /* First try to allocate a print state from the pool */
  694. scm_i_pthread_mutex_lock (&print_state_mutex);
  695. if (!scm_is_null (print_state_pool))
  696. {
  697. handle = print_state_pool;
  698. print_state_pool = SCM_CDR (print_state_pool);
  699. }
  700. scm_i_pthread_mutex_unlock (&print_state_mutex);
  701. if (scm_is_false (handle))
  702. handle = scm_list_1 (make_print_state ());
  703. pstate_scm = SCM_CAR (handle);
  704. }
  705. pstate = SCM_PRINT_STATE (pstate_scm);
  706. old_writingp = pstate->writingp;
  707. pstate->writingp = writingp;
  708. scm_iprin1 (exp, port, pstate);
  709. pstate->writingp = old_writingp;
  710. /* Return print state to pool if it has been created above and
  711. hasn't escaped to Scheme. */
  712. if (scm_is_true (handle) && !pstate->revealed)
  713. {
  714. scm_i_pthread_mutex_lock (&print_state_mutex);
  715. SCM_SETCDR (handle, print_state_pool);
  716. print_state_pool = handle;
  717. scm_i_pthread_mutex_unlock (&print_state_mutex);
  718. }
  719. }
  720. /* Convert codepoint CH to UTF-8 and store the result in UTF8. Return
  721. the number of bytes of the UTF-8-encoded string. */
  722. static size_t
  723. codepoint_to_utf8 (scm_t_wchar ch, scm_t_uint8 utf8[4])
  724. {
  725. size_t len;
  726. scm_t_uint32 codepoint;
  727. codepoint = (scm_t_uint32) ch;
  728. if (codepoint <= 0x7f)
  729. {
  730. len = 1;
  731. utf8[0] = (scm_t_uint8) codepoint;
  732. }
  733. else if (codepoint <= 0x7ffUL)
  734. {
  735. len = 2;
  736. utf8[0] = 0xc0 | (codepoint >> 6);
  737. utf8[1] = 0x80 | (codepoint & 0x3f);
  738. }
  739. else if (codepoint <= 0xffffUL)
  740. {
  741. len = 3;
  742. utf8[0] = 0xe0 | (codepoint >> 12);
  743. utf8[1] = 0x80 | ((codepoint >> 6) & 0x3f);
  744. utf8[2] = 0x80 | (codepoint & 0x3f);
  745. }
  746. else
  747. {
  748. len = 4;
  749. utf8[0] = 0xf0 | (codepoint >> 18);
  750. utf8[1] = 0x80 | ((codepoint >> 12) & 0x3f);
  751. utf8[2] = 0x80 | ((codepoint >> 6) & 0x3f);
  752. utf8[3] = 0x80 | (codepoint & 0x3f);
  753. }
  754. return len;
  755. }
  756. #define STR_REF(s, x) \
  757. (narrow_p \
  758. ? (scm_t_wchar) ((unsigned char *) (s))[x] \
  759. : ((scm_t_wchar *) (s))[x])
  760. /* Write STR to PORT as UTF-8. STR is a LEN-codepoint string; it is
  761. narrow if NARROW_P is true, wide otherwise. Return LEN. */
  762. static size_t
  763. display_string_as_utf8 (const void *str, int narrow_p, size_t len,
  764. SCM port)
  765. {
  766. size_t printed = 0;
  767. while (len > printed)
  768. {
  769. size_t utf8_len, i;
  770. char *input, utf8_buf[256];
  771. /* Convert STR to UTF-8. */
  772. for (i = printed, utf8_len = 0, input = utf8_buf;
  773. i < len && utf8_len + 4 < sizeof (utf8_buf);
  774. i++)
  775. {
  776. utf8_len += codepoint_to_utf8 (STR_REF (str, i),
  777. (scm_t_uint8 *) input);
  778. input = utf8_buf + utf8_len;
  779. }
  780. /* INPUT was successfully converted, entirely; print the
  781. result. */
  782. scm_lfwrite_unlocked (utf8_buf, utf8_len, port);
  783. printed += i - printed;
  784. }
  785. assert (printed == len);
  786. return len;
  787. }
  788. /* Write STR to PORT as ISO-8859-1. STR is a LEN-codepoint string; it
  789. is narrow if NARROW_P is true, wide otherwise. Return LEN. */
  790. static size_t
  791. display_string_as_latin1 (const void *str, int narrow_p, size_t len,
  792. SCM port,
  793. scm_t_string_failed_conversion_handler strategy)
  794. {
  795. size_t printed = 0;
  796. if (narrow_p)
  797. {
  798. scm_lfwrite_unlocked (str, len, port);
  799. return len;
  800. }
  801. while (printed < len)
  802. {
  803. char buf[256];
  804. size_t i;
  805. for (i = 0; i < sizeof(buf) && printed < len; i++, printed++)
  806. {
  807. scm_t_wchar c = STR_REF (str, printed);
  808. if (c < 256)
  809. buf[i] = c;
  810. else
  811. break;
  812. }
  813. scm_lfwrite_unlocked (buf, i, port);
  814. if (i < sizeof(buf) && printed < len)
  815. {
  816. if (strategy == SCM_FAILED_CONVERSION_ERROR)
  817. break;
  818. else if (strategy == SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE)
  819. write_character_escaped (STR_REF (str, printed), 1, port);
  820. else
  821. /* STRATEGY is `SCM_FAILED_CONVERSION_QUESTION_MARK'. */
  822. display_string ("?", 1, 1, port, strategy);
  823. printed++;
  824. }
  825. }
  826. return printed;
  827. }
  828. /* Convert STR through PORT's output conversion descriptor and write the
  829. output to PORT. Return the number of codepoints written. */
  830. static size_t
  831. display_string_using_iconv (const void *str, int narrow_p, size_t len,
  832. SCM port,
  833. scm_t_string_failed_conversion_handler strategy)
  834. {
  835. size_t printed;
  836. scm_t_iconv_descriptors *id;
  837. id = scm_i_port_iconv_descriptors (port);
  838. printed = 0;
  839. while (len > printed)
  840. {
  841. size_t done, utf8_len, input_left, output_left, i;
  842. size_t codepoints_read, output_len;
  843. char *input, *output;
  844. char utf8_buf[256], encoded_output[256];
  845. size_t offsets[256];
  846. /* Convert STR to UTF-8. */
  847. for (i = printed, utf8_len = 0, input = utf8_buf;
  848. i < len && utf8_len + 4 < sizeof (utf8_buf);
  849. i++)
  850. {
  851. offsets[utf8_len] = i;
  852. utf8_len += codepoint_to_utf8 (STR_REF (str, i),
  853. (scm_t_uint8 *) input);
  854. input = utf8_buf + utf8_len;
  855. }
  856. input = utf8_buf;
  857. input_left = utf8_len;
  858. output = encoded_output;
  859. output_left = sizeof (encoded_output);
  860. done = iconv (id->output_cd, &input, &input_left,
  861. &output, &output_left);
  862. output_len = sizeof (encoded_output) - output_left;
  863. if (SCM_UNLIKELY (done == (size_t) -1))
  864. {
  865. int errno_save = errno;
  866. /* Reset the `iconv' state. */
  867. iconv (id->output_cd, NULL, NULL, NULL, NULL);
  868. /* Print the OUTPUT_LEN bytes successfully converted. */
  869. scm_lfwrite_unlocked (encoded_output, output_len, port);
  870. /* See how many input codepoints these OUTPUT_LEN bytes
  871. corresponds to. */
  872. codepoints_read = offsets[input - utf8_buf] - printed;
  873. printed += codepoints_read;
  874. if (errno_save == EILSEQ &&
  875. strategy != SCM_FAILED_CONVERSION_ERROR)
  876. {
  877. /* Conversion failed somewhere in INPUT and we want to
  878. escape or substitute the offending input character. */
  879. if (strategy == SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE)
  880. {
  881. scm_t_wchar ch;
  882. /* Find CH, the offending codepoint, and escape it. */
  883. ch = STR_REF (str, offsets[input - utf8_buf]);
  884. write_character_escaped (ch, 1, port);
  885. }
  886. else
  887. /* STRATEGY is `SCM_FAILED_CONVERSION_QUESTION_MARK'. */
  888. display_string ("?", 1, 1, port, strategy);
  889. printed++;
  890. }
  891. else
  892. /* Something bad happened that we can't handle: bail out. */
  893. break;
  894. }
  895. else
  896. {
  897. /* INPUT was successfully converted, entirely; print the
  898. result. */
  899. scm_lfwrite_unlocked (encoded_output, output_len, port);
  900. codepoints_read = i - printed;
  901. printed += codepoints_read;
  902. }
  903. }
  904. return printed;
  905. }
  906. #undef STR_REF
  907. /* Display the LEN codepoints in STR to PORT according to STRATEGY;
  908. return the number of codepoints successfully displayed. If NARROW_P,
  909. then STR is interpreted as a sequence of `char', denoting a Latin-1
  910. string; otherwise it's interpreted as a sequence of
  911. `scm_t_wchar'. */
  912. static size_t
  913. display_string (const void *str, int narrow_p,
  914. size_t len, SCM port,
  915. scm_t_string_failed_conversion_handler strategy)
  916. {
  917. scm_t_port *pt;
  918. pt = SCM_PTAB_ENTRY (port);
  919. if (pt->encoding_mode == SCM_PORT_ENCODING_MODE_UTF8)
  920. return display_string_as_utf8 (str, narrow_p, len, port);
  921. else if (pt->encoding_mode == SCM_PORT_ENCODING_MODE_LATIN1)
  922. return display_string_as_latin1 (str, narrow_p, len, port, strategy);
  923. else
  924. return display_string_using_iconv (str, narrow_p, len, port, strategy);
  925. }
  926. /* Attempt to display CH to PORT according to STRATEGY. Return non-zero
  927. if CH was successfully displayed, zero otherwise (e.g., if it was not
  928. representable in PORT's encoding.) */
  929. static int
  930. display_character (scm_t_wchar ch, SCM port,
  931. scm_t_string_failed_conversion_handler strategy)
  932. {
  933. return display_string (&ch, 0, 1, port, strategy) == 1;
  934. }
  935. /* Attempt to pretty-print CH, a combining character, to PORT. Return
  936. zero upon failure, non-zero otherwise. The idea is to print CH above
  937. a dotted circle to make it more visible. */
  938. static int
  939. write_combining_character (scm_t_wchar ch, SCM port)
  940. {
  941. scm_t_wchar str[2];
  942. str[0] = SCM_CODEPOINT_DOTTED_CIRCLE;
  943. str[1] = ch;
  944. return display_string (str, 0, 2, port, iconveh_error) == 2;
  945. }
  946. /* Write CH to PORT in its escaped form, using the string escape syntax
  947. if STRING_ESCAPES_P is non-zero. */
  948. static void
  949. write_character_escaped (scm_t_wchar ch, int string_escapes_p, SCM port)
  950. {
  951. if (string_escapes_p)
  952. {
  953. /* Represent CH using the in-string escape syntax. */
  954. static const char hex[] = "0123456789abcdef";
  955. static const char escapes[7] = "abtnvfr";
  956. char buf[9];
  957. if (ch >= 0x07 && ch <= 0x0D && ch != 0x0A)
  958. {
  959. /* Use special escapes for some C0 controls. */
  960. buf[0] = '\\';
  961. buf[1] = escapes[ch - 0x07];
  962. scm_lfwrite_unlocked (buf, 2, port);
  963. }
  964. else if (!SCM_R6RS_ESCAPES_P)
  965. {
  966. if (ch <= 0xFF)
  967. {
  968. buf[0] = '\\';
  969. buf[1] = 'x';
  970. buf[2] = hex[ch / 16];
  971. buf[3] = hex[ch % 16];
  972. scm_lfwrite_unlocked (buf, 4, port);
  973. }
  974. else if (ch <= 0xFFFF)
  975. {
  976. buf[0] = '\\';
  977. buf[1] = 'u';
  978. buf[2] = hex[(ch & 0xF000) >> 12];
  979. buf[3] = hex[(ch & 0xF00) >> 8];
  980. buf[4] = hex[(ch & 0xF0) >> 4];
  981. buf[5] = hex[(ch & 0xF)];
  982. scm_lfwrite_unlocked (buf, 6, port);
  983. }
  984. else if (ch > 0xFFFF)
  985. {
  986. buf[0] = '\\';
  987. buf[1] = 'U';
  988. buf[2] = hex[(ch & 0xF00000) >> 20];
  989. buf[3] = hex[(ch & 0xF0000) >> 16];
  990. buf[4] = hex[(ch & 0xF000) >> 12];
  991. buf[5] = hex[(ch & 0xF00) >> 8];
  992. buf[6] = hex[(ch & 0xF0) >> 4];
  993. buf[7] = hex[(ch & 0xF)];
  994. scm_lfwrite_unlocked (buf, 8, port);
  995. }
  996. }
  997. else
  998. {
  999. /* Print an R6RS variable-length hex escape: "\xNNNN;". */
  1000. scm_t_wchar ch2 = ch;
  1001. int i = 8;
  1002. buf[i] = ';';
  1003. i --;
  1004. if (ch == 0)
  1005. buf[i--] = '0';
  1006. else
  1007. while (ch2 > 0)
  1008. {
  1009. buf[i] = hex[ch2 & 0xF];
  1010. ch2 >>= 4;
  1011. i --;
  1012. }
  1013. buf[i] = 'x';
  1014. i --;
  1015. buf[i] = '\\';
  1016. scm_lfwrite_unlocked (buf + i, 9 - i, port);
  1017. }
  1018. }
  1019. else
  1020. {
  1021. /* Represent CH using the character escape syntax. */
  1022. const char *name;
  1023. name = scm_i_charname (SCM_MAKE_CHAR (ch));
  1024. if (name != NULL)
  1025. scm_puts_unlocked (name, port);
  1026. else
  1027. PRINT_CHAR_ESCAPE (ch, port);
  1028. }
  1029. }
  1030. /* Write CH to PORT, escaping it if it's non-graphic or not
  1031. representable in PORT's encoding. If STRING_ESCAPES_P is true and CH
  1032. needs to be escaped, it is escaped using the in-string escape syntax;
  1033. otherwise the character escape syntax is used. */
  1034. static void
  1035. write_character (scm_t_wchar ch, SCM port, int string_escapes_p)
  1036. {
  1037. int printed = 0;
  1038. scm_t_string_failed_conversion_handler strategy;
  1039. strategy = scm_i_get_conversion_strategy (port);
  1040. if (string_escapes_p)
  1041. {
  1042. /* Check if CH deserves special treatment. */
  1043. if (ch == '"' || ch == '\\')
  1044. {
  1045. display_character ('\\', port, iconveh_question_mark);
  1046. display_character (ch, port, strategy);
  1047. printed = 1;
  1048. }
  1049. else if (ch == '\n' && SCM_PRINT_ESCAPE_NEWLINES_P)
  1050. {
  1051. display_character ('\\', port, iconveh_question_mark);
  1052. display_character ('n', port, strategy);
  1053. printed = 1;
  1054. }
  1055. else if (ch == ' ' || ch == '\n')
  1056. {
  1057. display_character (ch, port, strategy);
  1058. printed = 1;
  1059. }
  1060. }
  1061. else
  1062. {
  1063. display_string ("#\\", 1, 2, port, iconveh_question_mark);
  1064. if (uc_combining_class (ch) != UC_CCC_NR)
  1065. /* Character is a combining character, so attempt to
  1066. pretty-print it. */
  1067. printed = write_combining_character (ch, port);
  1068. }
  1069. if (!printed
  1070. && uc_is_general_category_withtable (ch,
  1071. UC_CATEGORY_MASK_L |
  1072. UC_CATEGORY_MASK_M |
  1073. UC_CATEGORY_MASK_N |
  1074. UC_CATEGORY_MASK_P |
  1075. UC_CATEGORY_MASK_S))
  1076. /* CH is graphic; attempt to display it. */
  1077. printed = display_character (ch, port, iconveh_error);
  1078. if (!printed)
  1079. /* CH isn't graphic or cannot be represented in PORT's encoding. */
  1080. write_character_escaped (ch, string_escapes_p, port);
  1081. }
  1082. /* Print an integer.
  1083. */
  1084. void
  1085. scm_intprint (scm_t_intmax n, int radix, SCM port)
  1086. {
  1087. char num_buf[SCM_INTBUFLEN];
  1088. scm_lfwrite_unlocked (num_buf, scm_iint2str (n, radix, num_buf), port);
  1089. }
  1090. void
  1091. scm_uintprint (scm_t_uintmax n, int radix, SCM port)
  1092. {
  1093. char num_buf[SCM_INTBUFLEN];
  1094. scm_lfwrite_unlocked (num_buf, scm_iuint2str (n, radix, num_buf), port);
  1095. }
  1096. /* Print an object of unrecognized type.
  1097. */
  1098. void
  1099. scm_ipruk (char *hdr, SCM ptr, SCM port)
  1100. {
  1101. scm_puts_unlocked ("#<unknown-", port);
  1102. scm_puts_unlocked (hdr, port);
  1103. if (1) /* (scm_in_heap_p (ptr)) */ /* FIXME */
  1104. {
  1105. scm_puts_unlocked (" (0x", port);
  1106. scm_uintprint (SCM_CELL_WORD_0 (ptr), 16, port);
  1107. scm_puts_unlocked (" . 0x", port);
  1108. scm_uintprint (SCM_CELL_WORD_1 (ptr), 16, port);
  1109. scm_puts_unlocked (") @", port);
  1110. }
  1111. scm_puts_unlocked (" 0x", port);
  1112. scm_uintprint (SCM_UNPACK (ptr), 16, port);
  1113. scm_putc_unlocked ('>', port);
  1114. }
  1115. /* Print a list.
  1116. */
  1117. void
  1118. scm_iprlist (char *hdr, SCM exp, int tlr, SCM port, scm_print_state *pstate)
  1119. {
  1120. register SCM hare, tortoise;
  1121. long floor = pstate->top - 2;
  1122. scm_puts_unlocked (hdr, port);
  1123. /* CHECK_INTS; */
  1124. if (pstate->fancyp)
  1125. goto fancy_printing;
  1126. /* Run a hare and tortoise so that total time complexity will be
  1127. O(depth * N) instead of O(N^2). */
  1128. hare = SCM_CDR (exp);
  1129. tortoise = exp;
  1130. while (scm_is_pair (hare))
  1131. {
  1132. if (scm_is_eq (hare, tortoise))
  1133. goto fancy_printing;
  1134. hare = SCM_CDR (hare);
  1135. if (!scm_is_pair (hare))
  1136. break;
  1137. hare = SCM_CDR (hare);
  1138. tortoise = SCM_CDR (tortoise);
  1139. }
  1140. /* No cdr cycles intrinsic to this list */
  1141. scm_iprin1 (SCM_CAR (exp), port, pstate);
  1142. for (exp = SCM_CDR (exp); scm_is_pair (exp); exp = SCM_CDR (exp))
  1143. {
  1144. register long i;
  1145. for (i = floor; i >= 0; --i)
  1146. if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp))
  1147. goto circref;
  1148. PUSH_REF (pstate, exp);
  1149. scm_putc_unlocked (' ', port);
  1150. /* CHECK_INTS; */
  1151. scm_iprin1 (SCM_CAR (exp), port, pstate);
  1152. }
  1153. if (!SCM_NULL_OR_NIL_P (exp))
  1154. {
  1155. scm_puts_unlocked (" . ", port);
  1156. scm_iprin1 (exp, port, pstate);
  1157. }
  1158. end:
  1159. scm_putc_unlocked (tlr, port);
  1160. pstate->top = floor + 2;
  1161. return;
  1162. fancy_printing:
  1163. {
  1164. long n = pstate->length;
  1165. scm_iprin1 (SCM_CAR (exp), port, pstate);
  1166. exp = SCM_CDR (exp); --n;
  1167. for (; scm_is_pair (exp); exp = SCM_CDR (exp))
  1168. {
  1169. register unsigned long i;
  1170. for (i = 0; i < pstate->top; ++i)
  1171. if (scm_is_eq (PSTATE_STACK_REF(pstate, i), exp))
  1172. goto fancy_circref;
  1173. if (pstate->fancyp)
  1174. {
  1175. if (n == 0)
  1176. {
  1177. scm_puts_unlocked (" ...", port);
  1178. goto skip_tail;
  1179. }
  1180. else
  1181. --n;
  1182. }
  1183. PUSH_REF(pstate, exp);
  1184. ++pstate->list_offset;
  1185. scm_putc_unlocked (' ', port);
  1186. /* CHECK_INTS; */
  1187. scm_iprin1 (SCM_CAR (exp), port, pstate);
  1188. }
  1189. }
  1190. if (!SCM_NULL_OR_NIL_P (exp))
  1191. {
  1192. scm_puts_unlocked (" . ", port);
  1193. scm_iprin1 (exp, port, pstate);
  1194. }
  1195. skip_tail:
  1196. pstate->list_offset -= pstate->top - floor - 2;
  1197. goto end;
  1198. fancy_circref:
  1199. pstate->list_offset -= pstate->top - floor - 2;
  1200. circref:
  1201. scm_puts_unlocked (" . ", port);
  1202. print_circref (port, pstate, exp);
  1203. goto end;
  1204. }
  1205. int
  1206. scm_valid_oport_value_p (SCM val)
  1207. {
  1208. return (SCM_OPOUTPORTP (val)
  1209. || (SCM_PORT_WITH_PS_P (val)
  1210. && SCM_OPOUTPORTP (SCM_PORT_WITH_PS_PORT (val))));
  1211. }
  1212. /* SCM_GPROC(s_write, "write", 1, 1, 0, scm_write, g_write); */
  1213. SCM
  1214. scm_write (SCM obj, SCM port)
  1215. {
  1216. if (SCM_UNBNDP (port))
  1217. port = scm_current_output_port ();
  1218. SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_write);
  1219. scm_dynwind_begin (0);
  1220. scm_dynwind_lock_port (SCM_COERCE_OUTPORT (port));
  1221. scm_prin1 (obj, port, 1);
  1222. scm_dynwind_end ();
  1223. return SCM_UNSPECIFIED;
  1224. }
  1225. /* SCM_GPROC(s_display, "display", 1, 1, 0, scm_display, g_display); */
  1226. SCM
  1227. scm_display (SCM obj, SCM port)
  1228. {
  1229. if (SCM_UNBNDP (port))
  1230. port = scm_current_output_port ();
  1231. SCM_ASSERT (scm_valid_oport_value_p (port), port, SCM_ARG2, s_display);
  1232. scm_dynwind_begin (0);
  1233. scm_dynwind_lock_port (SCM_COERCE_OUTPORT (port));
  1234. scm_prin1 (obj, port, 0);
  1235. scm_dynwind_end ();
  1236. return SCM_UNSPECIFIED;
  1237. }
  1238. SCM_DEFINE (scm_simple_format, "simple-format", 2, 0, 1,
  1239. (SCM destination, SCM message, SCM args),
  1240. "Write @var{message} to @var{destination}, defaulting to\n"
  1241. "the current output port.\n"
  1242. "@var{message} can contain @code{~A} (was @code{%s}) and\n"
  1243. "@code{~S} (was @code{%S}) escapes. When printed,\n"
  1244. "the escapes are replaced with corresponding members of\n"
  1245. "@var{args}:\n"
  1246. "@code{~A} formats using @code{display} and @code{~S} formats\n"
  1247. "using @code{write}.\n"
  1248. "If @var{destination} is @code{#t}, then use the current output\n"
  1249. "port, if @var{destination} is @code{#f}, then return a string\n"
  1250. "containing the formatted text. Does not add a trailing newline.")
  1251. #define FUNC_NAME s_scm_simple_format
  1252. {
  1253. SCM port, answer = SCM_UNSPECIFIED;
  1254. int fReturnString = 0;
  1255. int writingp;
  1256. size_t start, p, end;
  1257. if (scm_is_eq (destination, SCM_BOOL_T))
  1258. {
  1259. destination = port = scm_current_output_port ();
  1260. }
  1261. else if (scm_is_false (destination))
  1262. {
  1263. fReturnString = 1;
  1264. port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
  1265. SCM_OPN | SCM_WRTNG,
  1266. FUNC_NAME);
  1267. destination = port;
  1268. }
  1269. else
  1270. {
  1271. SCM_VALIDATE_OPORT_VALUE (1, destination);
  1272. port = SCM_COERCE_OUTPORT (destination);
  1273. }
  1274. SCM_VALIDATE_STRING (2, message);
  1275. SCM_VALIDATE_REST_ARGUMENT (args);
  1276. p = 0;
  1277. start = 0;
  1278. end = scm_i_string_length (message);
  1279. for (p = start; p != end; ++p)
  1280. if (scm_i_string_ref (message, p) == '~')
  1281. {
  1282. if (++p == end)
  1283. break;
  1284. switch (scm_i_string_ref (message, p))
  1285. {
  1286. case 'A': case 'a':
  1287. writingp = 0;
  1288. break;
  1289. case 'S': case 's':
  1290. writingp = 1;
  1291. break;
  1292. case '~':
  1293. scm_lfwrite_substr (message, start, p, port);
  1294. start = p + 1;
  1295. continue;
  1296. case '%':
  1297. scm_lfwrite_substr (message, start, p - 1, port);
  1298. scm_newline (port);
  1299. start = p + 1;
  1300. continue;
  1301. default:
  1302. SCM_MISC_ERROR ("FORMAT: Unsupported format option ~~~A - use (ice-9 format) instead",
  1303. scm_list_1 (SCM_MAKE_CHAR (scm_i_string_ref (message, p))));
  1304. }
  1305. if (!scm_is_pair (args))
  1306. SCM_MISC_ERROR ("FORMAT: Missing argument for ~~~A",
  1307. scm_list_1 (SCM_MAKE_CHAR (scm_i_string_ref (message, p))));
  1308. scm_lfwrite_substr (message, start, p - 1, port);
  1309. /* we pass destination here */
  1310. scm_prin1 (SCM_CAR (args), destination, writingp);
  1311. args = SCM_CDR (args);
  1312. start = p + 1;
  1313. }
  1314. scm_lfwrite_substr (message, start, p, port);
  1315. if (!scm_is_eq (args, SCM_EOL))
  1316. SCM_MISC_ERROR ("FORMAT: ~A superfluous arguments",
  1317. scm_list_1 (scm_length (args)));
  1318. if (fReturnString)
  1319. answer = scm_strport_to_string (destination);
  1320. return scm_return_first (answer, message);
  1321. }
  1322. #undef FUNC_NAME
  1323. SCM_DEFINE (scm_newline, "newline", 0, 1, 0,
  1324. (SCM port),
  1325. "Send a newline to @var{port}.\n"
  1326. "If @var{port} is omitted, send to the current output port.")
  1327. #define FUNC_NAME s_scm_newline
  1328. {
  1329. if (SCM_UNBNDP (port))
  1330. port = scm_current_output_port ();
  1331. SCM_VALIDATE_OPORT_VALUE (1, port);
  1332. scm_putc_unlocked ('\n', SCM_COERCE_OUTPORT (port));
  1333. return SCM_UNSPECIFIED;
  1334. }
  1335. #undef FUNC_NAME
  1336. SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0,
  1337. (SCM chr, SCM port),
  1338. "Send character @var{chr} to @var{port}.")
  1339. #define FUNC_NAME s_scm_write_char
  1340. {
  1341. if (SCM_UNBNDP (port))
  1342. port = scm_current_output_port ();
  1343. SCM_VALIDATE_CHAR (1, chr);
  1344. SCM_VALIDATE_OPORT_VALUE (2, port);
  1345. port = SCM_COERCE_OUTPORT (port);
  1346. if (!display_character (SCM_CHAR (chr), port,
  1347. scm_i_get_conversion_strategy (port)))
  1348. scm_encoding_error (__func__, errno,
  1349. "cannot convert to output locale",
  1350. port, chr);
  1351. return SCM_UNSPECIFIED;
  1352. }
  1353. #undef FUNC_NAME
  1354. /* Call back to Scheme code to do the printing of special objects
  1355. * (like structs). SCM_PRINTER_APPLY applies PROC to EXP and a smob
  1356. * containing PORT and PSTATE. This object can be used as the port for
  1357. * display/write etc to continue the current print chain. The REVEALED
  1358. * field of PSTATE is set to true to indicate that the print state has
  1359. * escaped to Scheme and thus has to be freed by the GC.
  1360. */
  1361. scm_t_bits scm_tc16_port_with_ps;
  1362. /* Print exactly as the port itself would */
  1363. static int
  1364. port_with_ps_print (SCM obj, SCM port, scm_print_state *pstate)
  1365. {
  1366. obj = SCM_PORT_WITH_PS_PORT (obj);
  1367. return SCM_PORT_DESCRIPTOR (obj)->print (obj, port, pstate);
  1368. }
  1369. SCM
  1370. scm_printer_apply (SCM proc, SCM exp, SCM port, scm_print_state *pstate)
  1371. {
  1372. pstate->revealed = 1;
  1373. return scm_call_2 (proc, exp,
  1374. scm_i_port_with_print_state (port, pstate->handle));
  1375. }
  1376. SCM_DEFINE (scm_port_with_print_state, "port-with-print-state", 1, 1, 0,
  1377. (SCM port, SCM pstate),
  1378. "Create a new port which behaves like @var{port}, but with an\n"
  1379. "included print state @var{pstate}. @var{pstate} is optional.\n"
  1380. "If @var{pstate} isn't supplied and @var{port} already has\n"
  1381. "a print state, the old print state is reused.")
  1382. #define FUNC_NAME s_scm_port_with_print_state
  1383. {
  1384. SCM_VALIDATE_OPORT_VALUE (1, port);
  1385. if (!SCM_UNBNDP (pstate))
  1386. SCM_VALIDATE_PRINTSTATE (2, pstate);
  1387. return scm_i_port_with_print_state (port, pstate);
  1388. }
  1389. #undef FUNC_NAME
  1390. SCM_DEFINE (scm_get_print_state, "get-print-state", 1, 0, 0,
  1391. (SCM port),
  1392. "Return the print state of the port @var{port}. If @var{port}\n"
  1393. "has no associated print state, @code{#f} is returned.")
  1394. #define FUNC_NAME s_scm_get_print_state
  1395. {
  1396. if (SCM_PORT_WITH_PS_P (port))
  1397. return SCM_PORT_WITH_PS_PS (port);
  1398. if (SCM_OUTPUT_PORT_P (port))
  1399. return SCM_BOOL_F;
  1400. SCM_WRONG_TYPE_ARG (1, port);
  1401. }
  1402. #undef FUNC_NAME
  1403. void
  1404. scm_init_print ()
  1405. {
  1406. SCM type;
  1407. type = scm_make_vtable (scm_from_locale_string (SCM_PRINT_STATE_LAYOUT),
  1408. SCM_BOOL_F);
  1409. scm_set_struct_vtable_name_x (type, scm_from_latin1_symbol ("print-state"));
  1410. scm_print_state_vtable = type;
  1411. /* Don't want to bind a wrapper class in GOOPS, so pass 0 as arg1. */
  1412. scm_tc16_port_with_ps = scm_make_smob_type (0, 0);
  1413. scm_set_smob_print (scm_tc16_port_with_ps, port_with_ps_print);
  1414. #include "libguile/print.x"
  1415. scm_init_opts (scm_print_options, scm_print_opts);
  1416. scm_print_opts[SCM_PRINT_HIGHLIGHT_PREFIX_I].val =
  1417. SCM_UNPACK (scm_from_locale_string ("{"));
  1418. scm_print_opts[SCM_PRINT_HIGHLIGHT_SUFFIX_I].val =
  1419. SCM_UNPACK (scm_from_locale_string ("}"));
  1420. scm_print_opts[SCM_PRINT_KEYWORD_STYLE_I].val = SCM_UNPACK (sym_reader);
  1421. }
  1422. /*
  1423. Local Variables:
  1424. c-file-style: "gnu"
  1425. End:
  1426. */