strports.c 14 KB

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