strports.c 11 KB

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