strports.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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/strings.h"
  30. #include "libguile/modules.h"
  31. #include "libguile/validate.h"
  32. #include "libguile/deprecation.h"
  33. #include "libguile/srfi-4.h"
  34. #include "libguile/strports.h"
  35. #ifdef HAVE_STRING_H
  36. #include <string.h>
  37. #endif
  38. /* {Ports - string ports}
  39. *
  40. */
  41. SCM_SYMBOL (sym_UTF_8, "UTF-8");
  42. scm_t_port_type *scm_string_port_type;
  43. struct string_port {
  44. SCM bytevector;
  45. size_t pos;
  46. size_t len;
  47. };
  48. static size_t
  49. string_port_read (SCM port, SCM dst, size_t start, size_t count)
  50. {
  51. struct string_port *stream = (void *) SCM_STREAM (port);
  52. if (stream->pos >= stream->len)
  53. return 0;
  54. if (count > stream->len - stream->pos)
  55. count = stream->len - stream->pos;
  56. memcpy (SCM_BYTEVECTOR_CONTENTS (dst) + start,
  57. SCM_BYTEVECTOR_CONTENTS (stream->bytevector) + stream->pos,
  58. count);
  59. stream->pos += count;
  60. return count;
  61. }
  62. static size_t
  63. string_port_write (SCM port, SCM src, size_t start, size_t count)
  64. {
  65. struct string_port *stream = (void *) SCM_STREAM (port);
  66. if (SCM_BYTEVECTOR_LENGTH (stream->bytevector) < stream->pos + count)
  67. {
  68. SCM new_bv;
  69. size_t new_size;
  70. new_size = max (SCM_BYTEVECTOR_LENGTH (stream->bytevector) * 2,
  71. stream->pos + count);
  72. new_bv = scm_c_make_bytevector (new_size);
  73. memcpy (SCM_BYTEVECTOR_CONTENTS (new_bv),
  74. SCM_BYTEVECTOR_CONTENTS (stream->bytevector),
  75. stream->len);
  76. stream->bytevector = new_bv;
  77. }
  78. memcpy (SCM_BYTEVECTOR_CONTENTS (stream->bytevector) + stream->pos,
  79. SCM_BYTEVECTOR_CONTENTS (src) + start,
  80. count);
  81. stream->pos += count;
  82. if (stream->pos > stream->len)
  83. stream->len = stream->pos;
  84. return count;
  85. }
  86. static scm_t_off
  87. string_port_seek (SCM port, scm_t_off offset, int whence)
  88. #define FUNC_NAME "string_port_seek"
  89. {
  90. struct string_port *stream = (void *) SCM_STREAM (port);
  91. scm_t_off target;
  92. if (whence == SEEK_CUR)
  93. target = offset + stream->pos;
  94. else if (whence == SEEK_SET)
  95. target = offset;
  96. else if (whence == SEEK_END)
  97. target = offset + stream->len;
  98. else
  99. scm_wrong_type_arg_msg (FUNC_NAME, 0, port, "invalid `seek' parameter");
  100. if (target >= 0 && target <= stream->len)
  101. stream->pos = target;
  102. else
  103. scm_out_of_range (FUNC_NAME, scm_from_long (offset));
  104. return target;
  105. }
  106. #undef FUNC_NAME
  107. static void
  108. string_port_truncate (SCM port, scm_t_off length)
  109. #define FUNC_NAME "string_port_truncate"
  110. {
  111. struct string_port *stream = (void *) SCM_STREAM (port);
  112. if (0 <= length && stream->pos <= length && length <= stream->len)
  113. stream->len = length;
  114. else
  115. scm_out_of_range (FUNC_NAME, scm_from_off_t_or_off64_t (length));
  116. }
  117. #undef FUNC_NAME
  118. /* The initial size in bytes of a string port's buffer. */
  119. #define INITIAL_BUFFER_SIZE 128
  120. /* Return a new string port with MODES. If STR is #f, a new backing
  121. buffer is allocated; otherwise STR must be a string and a copy of it
  122. serves as the buffer for the new port. */
  123. SCM
  124. scm_mkstrport (SCM pos, SCM str, long modes, const char *caller)
  125. {
  126. SCM buf;
  127. size_t len, byte_pos;
  128. struct string_port *stream;
  129. if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG)))
  130. scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL);
  131. if (scm_is_false (str))
  132. {
  133. /* Allocate a new buffer to write to. */
  134. buf = scm_c_make_bytevector (INITIAL_BUFFER_SIZE);
  135. len = byte_pos = 0;
  136. }
  137. else
  138. {
  139. SCM_ASSERT (scm_is_string (str), str, SCM_ARG1, caller);
  140. buf = scm_string_to_utf8 (str);
  141. len = scm_c_bytevector_length (buf);
  142. if (scm_is_eq (pos, SCM_INUM0))
  143. byte_pos = 0;
  144. else
  145. /* Inefficient but simple way to convert the character position
  146. POS into a byte position BYTE_POS. */
  147. free (scm_to_utf8_stringn (scm_substring (str, SCM_INUM0, pos),
  148. &byte_pos));
  149. }
  150. stream = scm_gc_typed_calloc (struct string_port);
  151. stream->bytevector = buf;
  152. stream->pos = byte_pos;
  153. stream->len = len;
  154. return
  155. scm_c_make_port_with_encoding (scm_string_port_type, modes, sym_UTF_8,
  156. scm_i_default_port_conversion_strategy (),
  157. (scm_t_bits) stream);
  158. }
  159. /* Create a new string from the buffer of PORT, a string port, converting from
  160. PORT's encoding to the standard string representation. */
  161. SCM
  162. scm_strport_to_string (SCM port)
  163. {
  164. signed char *ptr;
  165. struct string_port *stream = (void *) SCM_STREAM (port);
  166. scm_flush (port);
  167. if (stream->len == 0)
  168. return scm_nullstr;
  169. ptr = SCM_BYTEVECTOR_CONTENTS (stream->bytevector);
  170. return scm_from_port_stringn ((char *) ptr, stream->len, port);
  171. }
  172. SCM_DEFINE (scm_object_to_string, "object->string", 1, 1, 0,
  173. (SCM obj, SCM printer),
  174. "Return a Scheme string obtained by printing @var{obj}.\n"
  175. "Printing function can be specified by the optional second\n"
  176. "argument @var{printer} (default: @code{write}).")
  177. #define FUNC_NAME s_scm_object_to_string
  178. {
  179. SCM port, result;
  180. if (!SCM_UNBNDP (printer))
  181. SCM_VALIDATE_PROC (2, printer);
  182. port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, SCM_WRTNG, FUNC_NAME);
  183. if (SCM_UNBNDP (printer))
  184. scm_write (obj, port);
  185. else
  186. scm_call_2 (printer, obj, port);
  187. result = scm_strport_to_string (port);
  188. /* Explicitly close PORT so that the iconv CDs associated with it are
  189. deallocated right away. This is important because CDs use a lot of
  190. memory that's not visible to the GC, so not freeing them can lead
  191. to almost large heap usage. See
  192. <http://wingolog.org/archives/2011/02/25/ports-weaks-gc-and-dark-matter>
  193. for details. */
  194. scm_close_port (port);
  195. return result;
  196. }
  197. #undef FUNC_NAME
  198. SCM
  199. scm_call_with_output_string (SCM proc)
  200. {
  201. static SCM var = SCM_BOOL_F;
  202. if (scm_is_false (var))
  203. var = scm_c_private_lookup ("guile", "call-with-output-string");
  204. return scm_call_1 (scm_variable_ref (var), proc);
  205. }
  206. SCM
  207. scm_call_with_input_string (SCM string, SCM proc)
  208. {
  209. static SCM var = SCM_BOOL_F;
  210. if (scm_is_false (var))
  211. var = scm_c_private_lookup ("guile", "call-with-input-string");
  212. return scm_call_2 (scm_variable_ref (var), string, proc);
  213. }
  214. SCM_DEFINE (scm_open_input_string, "open-input-string", 1, 0, 0,
  215. (SCM str),
  216. "Take a string and return an input port that delivers characters\n"
  217. "from the string. The port can be closed by\n"
  218. "@code{close-input-port}, though its storage will be reclaimed\n"
  219. "by the garbage collector if it becomes inaccessible.")
  220. #define FUNC_NAME s_scm_open_input_string
  221. {
  222. return scm_mkstrport (SCM_INUM0, str, SCM_RDNG, FUNC_NAME);
  223. }
  224. #undef FUNC_NAME
  225. SCM_DEFINE (scm_open_output_string, "open-output-string", 0, 0, 0,
  226. (void),
  227. "Return an output port that will accumulate characters for\n"
  228. "retrieval by @code{get-output-string}. The port can be closed\n"
  229. "by the procedure @code{close-output-port}, though its storage\n"
  230. "will be reclaimed by the garbage collector if it becomes\n"
  231. "inaccessible.")
  232. #define FUNC_NAME s_scm_open_output_string
  233. {
  234. return scm_mkstrport (SCM_INUM0, SCM_BOOL_F, SCM_WRTNG, FUNC_NAME);
  235. }
  236. #undef FUNC_NAME
  237. SCM_DEFINE (scm_get_output_string, "get-output-string", 1, 0, 0,
  238. (SCM port),
  239. "Given an output port created by @code{open-output-string},\n"
  240. "return a string consisting of the characters that have been\n"
  241. "output to the port so far.")
  242. #define FUNC_NAME s_scm_get_output_string
  243. {
  244. SCM_VALIDATE_OPOUTSTRPORT (1, port);
  245. return scm_strport_to_string (port);
  246. }
  247. #undef FUNC_NAME
  248. /* Given a null-terminated string EXPR containing a Scheme expression
  249. read it, and return it as an SCM value. */
  250. SCM
  251. scm_c_read_string (const char *expr)
  252. {
  253. SCM port, form;
  254. port = scm_mkstrport (SCM_INUM0, scm_from_locale_string (expr),
  255. SCM_RDNG, "scm_c_read_string");
  256. form = scm_read (port);
  257. scm_close_port (port);
  258. return form;
  259. }
  260. /* Given a null-terminated string EXPR containing Scheme program text,
  261. evaluate it, and return the result of the last expression evaluated. */
  262. SCM
  263. scm_c_eval_string (const char *expr)
  264. {
  265. return scm_eval_string (scm_from_locale_string (expr));
  266. }
  267. SCM
  268. scm_c_eval_string_in_module (const char *expr, SCM module)
  269. {
  270. return scm_eval_string_in_module (scm_from_locale_string (expr), module);
  271. }
  272. static SCM eval_string_var;
  273. static SCM k_module;
  274. static void
  275. init_eval_string_var_and_k_module (void)
  276. {
  277. eval_string_var = scm_c_public_variable ("ice-9 eval-string", "eval-string");
  278. k_module = scm_from_locale_keyword ("module");
  279. }
  280. SCM_DEFINE (scm_eval_string_in_module, "eval-string", 1, 1, 0,
  281. (SCM string, SCM module),
  282. "Evaluate @var{string} as the text representation of a Scheme\n"
  283. "form or forms, and return whatever value they produce.\n"
  284. "Evaluation takes place in the given module, or the current\n"
  285. "module when no module is given.\n"
  286. "While the code is evaluated, the given module is made the\n"
  287. "current one. The current module is restored when this\n"
  288. "procedure returns.")
  289. #define FUNC_NAME s_scm_eval_string_in_module
  290. {
  291. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  292. scm_i_pthread_once (&once, init_eval_string_var_and_k_module);
  293. if (SCM_UNBNDP (module))
  294. module = scm_current_module ();
  295. else
  296. SCM_VALIDATE_MODULE (2, module);
  297. return scm_call_3 (scm_variable_ref (eval_string_var),
  298. string, k_module, module);
  299. }
  300. #undef FUNC_NAME
  301. SCM
  302. scm_eval_string (SCM string)
  303. {
  304. return scm_eval_string_in_module (string, SCM_UNDEFINED);
  305. }
  306. static scm_t_port_type *
  307. scm_make_string_port_type ()
  308. {
  309. scm_t_port_type *ptob = scm_make_port_type ("string",
  310. string_port_read,
  311. string_port_write);
  312. scm_set_port_seek (ptob, string_port_seek);
  313. scm_set_port_truncate (ptob, string_port_truncate);
  314. return ptob;
  315. }
  316. void
  317. scm_init_strports ()
  318. {
  319. scm_string_port_type = scm_make_string_port_type ();
  320. #include "libguile/strports.x"
  321. }
  322. /*
  323. Local Variables:
  324. c-file-style: "gnu"
  325. End:
  326. */