strports.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006,
  2. * 2009, 2010, 2011, 2012, 2013, 2014 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 "libguile/_scm.h"
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include "libguile/bytevectors.h"
  26. #include "libguile/eval.h"
  27. #include "libguile/ports.h"
  28. #include "libguile/read.h"
  29. #include "libguile/root.h"
  30. #include "libguile/strings.h"
  31. #include "libguile/modules.h"
  32. #include "libguile/validate.h"
  33. #include "libguile/deprecation.h"
  34. #include "libguile/srfi-4.h"
  35. #include "libguile/strports.h"
  36. #ifdef HAVE_STRING_H
  37. #include <string.h>
  38. #endif
  39. /* {Ports - string ports}
  40. *
  41. */
  42. /* NOTES:
  43. write_buf/write_end point to the ends of the allocated bytevector.
  44. read_buf/read_end point to the part of the bytevector which has been
  45. written to. read_pos and write_pos are always equal.
  46. ENHANCE-ME - output blocks:
  47. The current code keeps an output string as a single block. That means
  48. when the size is increased the entire old contents must be copied. It'd
  49. be more efficient to begin a new block when the old one is full, so
  50. there's no re-copying of previous data.
  51. To make seeking efficient, keeping the pieces in a vector might be best,
  52. though appending is probably the most common operation. The size of each
  53. block could be progressively increased, so the bigger the string the
  54. bigger the blocks.
  55. When `get-output-string' is called the blocks have to be coalesced into a
  56. string, the result could be kept as a single big block. If blocks were
  57. strings then `get-output-string' could notice when there's just one and
  58. return that with a copy-on-write (though repeated calls to
  59. `get-output-string' are probably unlikely).
  60. Another possibility would be to extend the port mechanism to let SCM
  61. strings come through directly from `display' and friends. That way if a
  62. big string is written it can be kept as a copy-on-write, saving time
  63. copying and maybe saving some space. */
  64. scm_t_bits scm_tc16_strport;
  65. static int
  66. st_fill_input (SCM port)
  67. {
  68. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  69. if (pt->read_pos >= pt->read_end)
  70. return EOF;
  71. else
  72. return *pt->read_pos;
  73. }
  74. /* Change the size of a port's bytevector to NEW_SIZE. This doesn't
  75. change `read_buf_size'. */
  76. static void
  77. st_resize_port (scm_t_port *pt, scm_t_off new_size)
  78. {
  79. SCM old_stream = SCM_PACK (pt->stream);
  80. const signed char *src = SCM_BYTEVECTOR_CONTENTS (old_stream);
  81. SCM new_stream = scm_c_make_bytevector (new_size);
  82. signed char *dst = SCM_BYTEVECTOR_CONTENTS (new_stream);
  83. unsigned long int old_size = SCM_BYTEVECTOR_LENGTH (old_stream);
  84. unsigned long int min_size = min (old_size, new_size);
  85. scm_t_off offset = pt->write_pos - pt->write_buf;
  86. pt->write_buf_size = new_size;
  87. memcpy (dst, src, min_size);
  88. scm_remember_upto_here_1 (old_stream);
  89. /* reset buffer. */
  90. {
  91. pt->stream = SCM_UNPACK (new_stream);
  92. pt->read_buf = pt->write_buf = (unsigned char *)dst;
  93. pt->read_pos = pt->write_pos = pt->write_buf + offset;
  94. pt->write_end = pt->write_buf + pt->write_buf_size;
  95. pt->read_end = pt->read_buf + pt->read_buf_size;
  96. }
  97. }
  98. static void
  99. st_write (SCM port, const void *data, size_t size)
  100. {
  101. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  102. if (size > pt->write_end - pt->write_pos)
  103. st_resize_port (pt, max (pt->write_buf_size * 2,
  104. pt->write_end - pt->write_pos + size));
  105. memcpy ((char *) pt->write_pos, data, size);
  106. pt->read_pos = (pt->write_pos += size);
  107. if (pt->read_pos > pt->read_end)
  108. {
  109. pt->read_end = (unsigned char *) pt->read_pos;
  110. pt->read_buf_size = pt->read_end - pt->read_buf;
  111. }
  112. }
  113. static void
  114. st_end_input (SCM port, int offset)
  115. {
  116. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  117. if (pt->read_pos - pt->read_buf < offset)
  118. scm_misc_error ("st_end_input", "negative position", SCM_EOL);
  119. pt->write_pos = (unsigned char *) (pt->read_pos = pt->read_pos - offset);
  120. pt->rw_active = SCM_PORT_NEITHER;
  121. }
  122. static scm_t_off
  123. st_seek (SCM port, scm_t_off offset, int whence)
  124. {
  125. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  126. scm_t_off target;
  127. if (pt->rw_active == SCM_PORT_READ && offset == 0 && whence == SEEK_CUR)
  128. /* special case to avoid disturbing the unread-char buffer. */
  129. {
  130. if (pt->read_buf == pt->putback_buf)
  131. {
  132. target = pt->saved_read_pos - pt->saved_read_buf
  133. - (pt->read_end - pt->read_pos);
  134. }
  135. else
  136. {
  137. target = pt->read_pos - pt->read_buf;
  138. }
  139. }
  140. else
  141. /* all other cases. */
  142. {
  143. if (pt->rw_active == SCM_PORT_READ)
  144. scm_end_input_unlocked (port);
  145. pt->rw_active = SCM_PORT_NEITHER;
  146. switch (whence)
  147. {
  148. case SEEK_CUR:
  149. target = pt->read_pos - pt->read_buf + offset;
  150. break;
  151. case SEEK_END:
  152. target = pt->read_end - pt->read_buf + offset;
  153. break;
  154. default: /* SEEK_SET */
  155. target = offset;
  156. break;
  157. }
  158. if (target < 0)
  159. scm_misc_error ("st_seek", "negative offset", SCM_EOL);
  160. if (target >= pt->write_buf_size)
  161. {
  162. if (!(SCM_CELL_WORD_0 (port) & SCM_WRTNG))
  163. {
  164. if (target > pt->write_buf_size)
  165. {
  166. scm_misc_error ("st_seek",
  167. "seek past end of read-only strport",
  168. SCM_EOL);
  169. }
  170. }
  171. else if (target == pt->write_buf_size)
  172. st_resize_port (pt, target * 2);
  173. }
  174. pt->read_pos = pt->write_pos = pt->read_buf + target;
  175. if (pt->read_pos > pt->read_end)
  176. {
  177. pt->read_end = (unsigned char *) pt->read_pos;
  178. pt->read_buf_size = pt->read_end - pt->read_buf;
  179. }
  180. }
  181. return target;
  182. }
  183. static void
  184. st_truncate (SCM port, scm_t_off length)
  185. {
  186. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  187. if (length > pt->write_buf_size)
  188. st_resize_port (pt, length);
  189. pt->read_buf_size = length;
  190. pt->read_end = pt->read_buf + length;
  191. if (pt->read_pos > pt->read_end)
  192. pt->read_pos = pt->write_pos = pt->read_end;
  193. }
  194. /* The initial size in bytes of a string port's buffer. */
  195. #define INITIAL_BUFFER_SIZE 128
  196. /* Return a new string port with MODES. If STR is #f, a new backing
  197. buffer is allocated; otherwise STR must be a string and a copy of it
  198. serves as the buffer for the new port. */
  199. SCM
  200. scm_mkstrport (SCM pos, SCM str, long modes, const char *caller)
  201. {
  202. SCM z, buf;
  203. scm_t_port *pt;
  204. size_t read_buf_size, num_bytes, c_byte_pos;
  205. char *c_buf;
  206. if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG)))
  207. scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL);
  208. if (scm_is_false (str))
  209. {
  210. /* Allocate a new buffer to write to. */
  211. num_bytes = INITIAL_BUFFER_SIZE;
  212. buf = scm_c_make_bytevector (num_bytes);
  213. c_buf = (char *) SCM_BYTEVECTOR_CONTENTS (buf);
  214. /* Reset `read_buf_size'. It will contain the actual number of
  215. bytes written to the port. */
  216. read_buf_size = 0;
  217. c_byte_pos = 0;
  218. }
  219. else
  220. {
  221. char *copy;
  222. SCM_ASSERT (scm_is_string (str), str, SCM_ARG1, caller);
  223. /* STR is a string. */
  224. /* Create a copy of STR in UTF-8. */
  225. copy = scm_to_utf8_stringn (str, &num_bytes);
  226. buf = scm_c_make_bytevector (num_bytes);
  227. c_buf = (char *) SCM_BYTEVECTOR_CONTENTS (buf);
  228. memcpy (c_buf, copy, num_bytes);
  229. free (copy);
  230. read_buf_size = num_bytes;
  231. if (scm_is_eq (pos, SCM_INUM0))
  232. c_byte_pos = 0;
  233. else
  234. /* Inefficient but simple way to convert the character position
  235. POS into a byte position C_BYTE_POS. */
  236. free (scm_to_utf8_stringn (scm_substring (str, SCM_INUM0, pos),
  237. &c_byte_pos));
  238. }
  239. z = scm_c_make_port_with_encoding (scm_tc16_strport, modes,
  240. "UTF-8",
  241. scm_i_default_port_conversion_handler (),
  242. SCM_UNPACK (buf));
  243. pt = SCM_PTAB_ENTRY (z);
  244. pt->write_buf = pt->read_buf = (unsigned char *) c_buf;
  245. pt->read_pos = pt->write_pos = pt->read_buf + c_byte_pos;
  246. pt->read_buf_size = read_buf_size;
  247. pt->write_buf_size = num_bytes;
  248. pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size;
  249. pt->rw_random = 1;
  250. return z;
  251. }
  252. /* Create a new string from the buffer of PORT, a string port, converting from
  253. PORT's encoding to the standard string representation. */
  254. SCM
  255. scm_strport_to_string (SCM port)
  256. {
  257. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  258. if (pt->read_buf_size == 0)
  259. return scm_nullstr;
  260. return scm_from_port_stringn ((char *)pt->read_buf, pt->read_buf_size, port);
  261. }
  262. SCM_DEFINE (scm_object_to_string, "object->string", 1, 1, 0,
  263. (SCM obj, SCM printer),
  264. "Return a Scheme string obtained by printing @var{obj}.\n"
  265. "Printing function can be specified by the optional second\n"
  266. "argument @var{printer} (default: @code{write}).")
  267. #define FUNC_NAME s_scm_object_to_string
  268. {
  269. SCM port, result;
  270. if (!SCM_UNBNDP (printer))
  271. SCM_VALIDATE_PROC (2, printer);
  272. port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
  273. SCM_OPN | SCM_WRTNG, FUNC_NAME);
  274. if (SCM_UNBNDP (printer))
  275. scm_write (obj, port);
  276. else
  277. scm_call_2 (printer, obj, port);
  278. result = scm_strport_to_string (port);
  279. /* Explicitly close PORT so that the iconv CDs associated with it are
  280. deallocated right away. This is important because CDs use a lot of
  281. memory that's not visible to the GC, so not freeing them can lead
  282. to almost large heap usage. See
  283. <http://wingolog.org/archives/2011/02/25/ports-weaks-gc-and-dark-matter>
  284. for details. */
  285. scm_close_port (port);
  286. return result;
  287. }
  288. #undef FUNC_NAME
  289. SCM
  290. scm_call_with_output_string (SCM proc)
  291. {
  292. static SCM var = SCM_BOOL_F;
  293. if (scm_is_false (var))
  294. var = scm_c_private_lookup ("guile", "call-with-output-string");
  295. return scm_call_1 (scm_variable_ref (var), proc);
  296. }
  297. SCM
  298. scm_call_with_input_string (SCM string, SCM proc)
  299. {
  300. static SCM var = SCM_BOOL_F;
  301. if (scm_is_false (var))
  302. var = scm_c_private_lookup ("guile", "call-with-input-string");
  303. return scm_call_2 (scm_variable_ref (var), string, proc);
  304. }
  305. SCM_DEFINE (scm_open_input_string, "open-input-string", 1, 0, 0,
  306. (SCM str),
  307. "Take a string and return an input port that delivers characters\n"
  308. "from the string. The port can be closed by\n"
  309. "@code{close-input-port}, though its storage will be reclaimed\n"
  310. "by the garbage collector if it becomes inaccessible.")
  311. #define FUNC_NAME s_scm_open_input_string
  312. {
  313. SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, FUNC_NAME);
  314. return p;
  315. }
  316. #undef FUNC_NAME
  317. SCM_DEFINE (scm_open_output_string, "open-output-string", 0, 0, 0,
  318. (void),
  319. "Return an output port that will accumulate characters for\n"
  320. "retrieval by @code{get-output-string}. The port can be closed\n"
  321. "by the procedure @code{close-output-port}, though its storage\n"
  322. "will be reclaimed by the garbage collector if it becomes\n"
  323. "inaccessible.")
  324. #define FUNC_NAME s_scm_open_output_string
  325. {
  326. SCM p;
  327. p = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
  328. SCM_OPN | SCM_WRTNG,
  329. FUNC_NAME);
  330. return p;
  331. }
  332. #undef FUNC_NAME
  333. SCM_DEFINE (scm_get_output_string, "get-output-string", 1, 0, 0,
  334. (SCM port),
  335. "Given an output port created by @code{open-output-string},\n"
  336. "return a string consisting of the characters that have been\n"
  337. "output to the port so far.")
  338. #define FUNC_NAME s_scm_get_output_string
  339. {
  340. SCM_VALIDATE_OPOUTSTRPORT (1, port);
  341. return scm_strport_to_string (port);
  342. }
  343. #undef FUNC_NAME
  344. /* Given a null-terminated string EXPR containing a Scheme expression
  345. read it, and return it as an SCM value. */
  346. SCM
  347. scm_c_read_string (const char *expr)
  348. {
  349. SCM port = scm_mkstrport (SCM_INUM0,
  350. scm_from_locale_string (expr),
  351. SCM_OPN | SCM_RDNG,
  352. "scm_c_read_string");
  353. SCM form;
  354. form = scm_read (port);
  355. scm_close_port (port);
  356. return form;
  357. }
  358. /* Given a null-terminated string EXPR containing Scheme program text,
  359. evaluate it, and return the result of the last expression evaluated. */
  360. SCM
  361. scm_c_eval_string (const char *expr)
  362. {
  363. return scm_eval_string (scm_from_locale_string (expr));
  364. }
  365. SCM
  366. scm_c_eval_string_in_module (const char *expr, SCM module)
  367. {
  368. return scm_eval_string_in_module (scm_from_locale_string (expr), module);
  369. }
  370. static SCM eval_string_var;
  371. static SCM k_module;
  372. static void
  373. init_eval_string_var_and_k_module (void)
  374. {
  375. eval_string_var = scm_c_public_variable ("ice-9 eval-string", "eval-string");
  376. k_module = scm_from_locale_keyword ("module");
  377. }
  378. SCM_DEFINE (scm_eval_string_in_module, "eval-string", 1, 1, 0,
  379. (SCM string, SCM module),
  380. "Evaluate @var{string} as the text representation of a Scheme\n"
  381. "form or forms, and return whatever value they produce.\n"
  382. "Evaluation takes place in the given module, or the current\n"
  383. "module when no module is given.\n"
  384. "While the code is evaluated, the given module is made the\n"
  385. "current one. The current module is restored when this\n"
  386. "procedure returns.")
  387. #define FUNC_NAME s_scm_eval_string_in_module
  388. {
  389. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  390. scm_i_pthread_once (&once, init_eval_string_var_and_k_module);
  391. if (SCM_UNBNDP (module))
  392. module = scm_current_module ();
  393. else
  394. SCM_VALIDATE_MODULE (2, module);
  395. return scm_call_3 (scm_variable_ref (eval_string_var),
  396. string, k_module, module);
  397. }
  398. #undef FUNC_NAME
  399. SCM
  400. scm_eval_string (SCM string)
  401. {
  402. return scm_eval_string_in_module (string, SCM_UNDEFINED);
  403. }
  404. static scm_t_bits
  405. scm_make_stptob ()
  406. {
  407. scm_t_bits tc = scm_make_port_type ("string", st_fill_input, st_write);
  408. scm_set_port_end_input (tc, st_end_input);
  409. scm_set_port_seek (tc, st_seek);
  410. scm_set_port_truncate (tc, st_truncate);
  411. return tc;
  412. }
  413. void
  414. scm_init_strports ()
  415. {
  416. scm_tc16_strport = scm_make_stptob ();
  417. #include "libguile/strports.x"
  418. }
  419. /*
  420. Local Variables:
  421. c-file-style: "gnu"
  422. End:
  423. */