strports.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /* Copyright (C) 1995,1996,1998,1999,2000,2001,2002, 2003, 2005, 2006, 2009 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include <stdio.h>
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #include "libguile/unif.h"
  27. #include "libguile/eval.h"
  28. #include "libguile/ports.h"
  29. #include "libguile/read.h"
  30. #include "libguile/root.h"
  31. #include "libguile/strings.h"
  32. #include "libguile/modules.h"
  33. #include "libguile/validate.h"
  34. #include "libguile/deprecation.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. We break the rules set forth by strings.h about accessing the
  44. internals of strings here. We can do this since we can guarantee
  45. that the string used as pt->stream is not in use by anyone else.
  46. Thus, it's representation will not change asynchronously.
  47. (Ports aren't thread-safe yet anyway...)
  48. write_buf/write_end point to the ends of the allocated string.
  49. read_buf/read_end in principle point to the part of the string which
  50. has been written to, but this is only updated after a flush.
  51. read_pos and write_pos in principle should be equal, but this is only true
  52. when rw_active is SCM_PORT_NEITHER.
  53. ENHANCE-ME - output blocks:
  54. The current code keeps an output string as a single block. That means
  55. when the size is increased the entire old contents must be copied. It'd
  56. be more efficient to begin a new block when the old one is full, so
  57. there's no re-copying of previous data.
  58. To make seeking efficient, keeping the pieces in a vector might be best,
  59. though appending is probably the most common operation. The size of each
  60. block could be progressively increased, so the bigger the string the
  61. bigger the blocks.
  62. When `get-output-string' is called the blocks have to be coalesced into a
  63. string, the result could be kept as a single big block. If blocks were
  64. strings then `get-output-string' could notice when there's just one and
  65. return that with a copy-on-write (though repeated calls to
  66. `get-output-string' are probably unlikely).
  67. Another possibility would be to extend the port mechanism to let SCM
  68. strings come through directly from `display' and friends. That way if a
  69. big string is written it can be kept as a copy-on-write, saving time
  70. copying and maybe saving some space. */
  71. scm_t_bits scm_tc16_strport;
  72. static int
  73. stfill_buffer (SCM port)
  74. {
  75. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  76. if (pt->read_pos >= pt->read_end)
  77. return EOF;
  78. else
  79. return scm_return_first_int (*pt->read_pos, port);
  80. }
  81. /* change the size of a port's string to new_size. this doesn't
  82. change read_buf_size. */
  83. static void
  84. st_resize_port (scm_t_port *pt, scm_t_off new_size)
  85. {
  86. SCM old_stream = SCM_PACK (pt->stream);
  87. const char *src = scm_i_string_chars (old_stream);
  88. char *dst;
  89. SCM new_stream = scm_i_make_string (new_size, &dst);
  90. unsigned long int old_size = scm_i_string_length (old_stream);
  91. unsigned long int min_size = min (old_size, new_size);
  92. unsigned long int i;
  93. scm_t_off index = pt->write_pos - pt->write_buf;
  94. pt->write_buf_size = new_size;
  95. for (i = 0; i != min_size; ++i)
  96. dst[i] = src[i];
  97. scm_remember_upto_here_1 (old_stream);
  98. /* reset buffer. */
  99. {
  100. pt->stream = SCM_UNPACK (new_stream);
  101. pt->read_buf = pt->write_buf = (unsigned char *)dst;
  102. pt->read_pos = pt->write_pos = pt->write_buf + index;
  103. pt->write_end = pt->write_buf + pt->write_buf_size;
  104. pt->read_end = pt->read_buf + pt->read_buf_size;
  105. }
  106. }
  107. /* amount by which write_buf is expanded. */
  108. #define SCM_WRITE_BLOCK 80
  109. /* ensure that write_pos < write_end by enlarging the buffer when
  110. necessary. update read_buf to account for written chars.
  111. The buffer is enlarged by 1.5 times, plus SCM_WRITE_BLOCK. Adding just a
  112. fixed amount is no good, because there's a block copy for each increment,
  113. and that copying would take quadratic time. In the past it was found to
  114. be very slow just adding 80 bytes each time (eg. about 10 seconds for
  115. writing a 100kbyte string). */
  116. static void
  117. st_flush (SCM port)
  118. {
  119. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  120. if (pt->write_pos == pt->write_end)
  121. {
  122. st_resize_port (pt, pt->write_buf_size * 3 / 2 + SCM_WRITE_BLOCK);
  123. }
  124. pt->read_pos = pt->write_pos;
  125. if (pt->read_pos > pt->read_end)
  126. {
  127. pt->read_end = (unsigned char *) pt->read_pos;
  128. pt->read_buf_size = pt->read_end - pt->read_buf;
  129. }
  130. pt->rw_active = SCM_PORT_NEITHER;
  131. }
  132. static void
  133. st_write (SCM port, const void *data, size_t size)
  134. {
  135. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  136. const char *input = (char *) data;
  137. while (size > 0)
  138. {
  139. int space = pt->write_end - pt->write_pos;
  140. int write_len = (size > space) ? space : size;
  141. memcpy ((char *) pt->write_pos, input, write_len);
  142. pt->write_pos += write_len;
  143. size -= write_len;
  144. input += write_len;
  145. if (write_len == space)
  146. st_flush (port);
  147. }
  148. }
  149. static void
  150. st_end_input (SCM port, int offset)
  151. {
  152. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  153. if (pt->read_pos - pt->read_buf < offset)
  154. scm_misc_error ("st_end_input", "negative position", SCM_EOL);
  155. pt->write_pos = (unsigned char *) (pt->read_pos = pt->read_pos - offset);
  156. pt->rw_active = SCM_PORT_NEITHER;
  157. }
  158. static scm_t_off
  159. st_seek (SCM port, scm_t_off offset, int whence)
  160. {
  161. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  162. scm_t_off target;
  163. if (pt->rw_active == SCM_PORT_READ && offset == 0 && whence == SEEK_CUR)
  164. /* special case to avoid disturbing the unread-char buffer. */
  165. {
  166. if (pt->read_buf == pt->putback_buf)
  167. {
  168. target = pt->saved_read_pos - pt->saved_read_buf
  169. - (pt->read_end - pt->read_pos);
  170. }
  171. else
  172. {
  173. target = pt->read_pos - pt->read_buf;
  174. }
  175. }
  176. else
  177. /* all other cases. */
  178. {
  179. if (pt->rw_active == SCM_PORT_WRITE)
  180. st_flush (port);
  181. if (pt->rw_active == SCM_PORT_READ)
  182. scm_end_input (port);
  183. switch (whence)
  184. {
  185. case SEEK_CUR:
  186. target = pt->read_pos - pt->read_buf + offset;
  187. break;
  188. case SEEK_END:
  189. target = pt->read_end - pt->read_buf + offset;
  190. break;
  191. default: /* SEEK_SET */
  192. target = offset;
  193. break;
  194. }
  195. if (target < 0)
  196. scm_misc_error ("st_seek", "negative offset", SCM_EOL);
  197. if (target >= pt->write_buf_size)
  198. {
  199. if (!(SCM_CELL_WORD_0 (port) & SCM_WRTNG))
  200. {
  201. if (target > pt->write_buf_size)
  202. {
  203. scm_misc_error ("st_seek",
  204. "seek past end of read-only strport",
  205. SCM_EOL);
  206. }
  207. }
  208. else
  209. {
  210. st_resize_port (pt, target + (target == pt->write_buf_size
  211. ? SCM_WRITE_BLOCK
  212. : 0));
  213. }
  214. }
  215. pt->read_pos = pt->write_pos = pt->read_buf + target;
  216. if (pt->read_pos > pt->read_end)
  217. {
  218. pt->read_end = (unsigned char *) pt->read_pos;
  219. pt->read_buf_size = pt->read_end - pt->read_buf;
  220. }
  221. }
  222. return target;
  223. }
  224. static void
  225. st_truncate (SCM port, scm_t_off length)
  226. {
  227. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  228. if (length > pt->write_buf_size)
  229. st_resize_port (pt, length);
  230. pt->read_buf_size = length;
  231. pt->read_end = pt->read_buf + length;
  232. if (pt->read_pos > pt->read_end)
  233. pt->read_pos = pt->read_end;
  234. if (pt->write_pos > pt->read_end)
  235. pt->write_pos = pt->read_end;
  236. }
  237. SCM
  238. scm_mkstrport (SCM pos, SCM str, long modes, const char *caller)
  239. {
  240. SCM z;
  241. scm_t_port *pt;
  242. size_t str_len, c_pos;
  243. SCM_ASSERT (scm_is_string (str), str, SCM_ARG1, caller);
  244. str_len = scm_i_string_length (str);
  245. c_pos = scm_to_unsigned_integer (pos, 0, str_len);
  246. if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG)))
  247. scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL);
  248. /* XXX
  249. Make a new string to isolate us from changes to the original.
  250. This is done so that we can rely on scm_i_string_chars to stay in
  251. place even across SCM_TICKs.
  252. Additionally, when we are going to write to the string, we make a
  253. copy so that we can write to it without having to use
  254. scm_i_string_writable_chars.
  255. */
  256. if (modes & SCM_WRTNG)
  257. str = scm_c_substring_copy (str, 0, str_len);
  258. else
  259. str = scm_c_substring (str, 0, str_len);
  260. scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex);
  261. z = scm_new_port_table_entry (scm_tc16_strport);
  262. pt = SCM_PTAB_ENTRY(z);
  263. SCM_SETSTREAM (z, SCM_UNPACK (str));
  264. SCM_SET_CELL_TYPE(z, scm_tc16_strport|modes);
  265. /* see above why we can use scm_i_string_chars here. */
  266. pt->write_buf = pt->read_buf = (unsigned char *) scm_i_string_chars (str);
  267. pt->read_pos = pt->write_pos = pt->read_buf + c_pos;
  268. pt->write_buf_size = pt->read_buf_size = str_len;
  269. pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size;
  270. pt->rw_random = 1;
  271. scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
  272. /* ensure write_pos is writable. */
  273. if ((modes & SCM_WRTNG) && pt->write_pos == pt->write_end)
  274. st_flush (z);
  275. return z;
  276. }
  277. /* create a new string from a string port's buffer. */
  278. SCM scm_strport_to_string (SCM port)
  279. {
  280. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  281. SCM str;
  282. char *dst;
  283. if (pt->rw_active == SCM_PORT_WRITE)
  284. st_flush (port);
  285. str = scm_i_make_string (pt->read_buf_size, &dst);
  286. memcpy (dst, (char *) pt->read_buf, pt->read_buf_size);
  287. scm_remember_upto_here_1 (port);
  288. return str;
  289. }
  290. SCM_DEFINE (scm_object_to_string, "object->string", 1, 1, 0,
  291. (SCM obj, SCM printer),
  292. "Return a Scheme string obtained by printing @var{obj}.\n"
  293. "Printing function can be specified by the optional second\n"
  294. "argument @var{printer} (default: @code{write}).")
  295. #define FUNC_NAME s_scm_object_to_string
  296. {
  297. SCM str, port;
  298. if (!SCM_UNBNDP (printer))
  299. SCM_VALIDATE_PROC (2, printer);
  300. str = scm_c_make_string (0, SCM_UNDEFINED);
  301. port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_WRTNG, FUNC_NAME);
  302. if (SCM_UNBNDP (printer))
  303. scm_write (obj, port);
  304. else
  305. scm_call_2 (printer, obj, port);
  306. return scm_strport_to_string (port);
  307. }
  308. #undef FUNC_NAME
  309. SCM_DEFINE (scm_call_with_output_string, "call-with-output-string", 1, 0, 0,
  310. (SCM proc),
  311. "Calls the one-argument procedure @var{proc} with a newly created output\n"
  312. "port. When the function returns, the string composed of the characters\n"
  313. "written into the port is returned.")
  314. #define FUNC_NAME s_scm_call_with_output_string
  315. {
  316. SCM p;
  317. p = scm_mkstrport (SCM_INUM0,
  318. scm_make_string (SCM_INUM0, SCM_UNDEFINED),
  319. SCM_OPN | SCM_WRTNG,
  320. FUNC_NAME);
  321. scm_call_1 (proc, p);
  322. return scm_get_output_string (p);
  323. }
  324. #undef FUNC_NAME
  325. SCM_DEFINE (scm_call_with_input_string, "call-with-input-string", 2, 0, 0,
  326. (SCM string, SCM proc),
  327. "Calls the one-argument procedure @var{proc} with a newly\n"
  328. "created input port from which @var{string}'s contents may be\n"
  329. "read. The value yielded by the @var{proc} is returned.")
  330. #define FUNC_NAME s_scm_call_with_input_string
  331. {
  332. SCM p = scm_mkstrport(SCM_INUM0, string, SCM_OPN | SCM_RDNG, FUNC_NAME);
  333. return scm_call_1 (proc, p);
  334. }
  335. #undef FUNC_NAME
  336. SCM_DEFINE (scm_open_input_string, "open-input-string", 1, 0, 0,
  337. (SCM str),
  338. "Take a string and return an input port that delivers characters\n"
  339. "from the string. The port can be closed by\n"
  340. "@code{close-input-port}, though its storage will be reclaimed\n"
  341. "by the garbage collector if it becomes inaccessible.")
  342. #define FUNC_NAME s_scm_open_input_string
  343. {
  344. SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, FUNC_NAME);
  345. return p;
  346. }
  347. #undef FUNC_NAME
  348. SCM_DEFINE (scm_open_output_string, "open-output-string", 0, 0, 0,
  349. (void),
  350. "Return an output port that will accumulate characters for\n"
  351. "retrieval by @code{get-output-string}. The port can be closed\n"
  352. "by the procedure @code{close-output-port}, though its storage\n"
  353. "will be reclaimed by the garbage collector if it becomes\n"
  354. "inaccessible.")
  355. #define FUNC_NAME s_scm_open_output_string
  356. {
  357. SCM p;
  358. p = scm_mkstrport (SCM_INUM0,
  359. scm_make_string (SCM_INUM0, SCM_UNDEFINED),
  360. SCM_OPN | SCM_WRTNG,
  361. FUNC_NAME);
  362. return p;
  363. }
  364. #undef FUNC_NAME
  365. SCM_DEFINE (scm_get_output_string, "get-output-string", 1, 0, 0,
  366. (SCM port),
  367. "Given an output port created by @code{open-output-string},\n"
  368. "return a string consisting of the characters that have been\n"
  369. "output to the port so far.")
  370. #define FUNC_NAME s_scm_get_output_string
  371. {
  372. SCM_VALIDATE_OPOUTSTRPORT (1, port);
  373. return scm_strport_to_string (port);
  374. }
  375. #undef FUNC_NAME
  376. /* Given a null-terminated string EXPR containing a Scheme expression
  377. read it, and return it as an SCM value. */
  378. SCM
  379. scm_c_read_string (const char *expr)
  380. {
  381. SCM port = scm_mkstrport (SCM_INUM0,
  382. scm_from_locale_string (expr),
  383. SCM_OPN | SCM_RDNG,
  384. "scm_c_read_string");
  385. SCM form;
  386. /* Read expressions from that port; ignore the values. */
  387. form = scm_read (port);
  388. scm_close_port (port);
  389. return form;
  390. }
  391. /* Given a null-terminated string EXPR containing Scheme program text,
  392. evaluate it, and return the result of the last expression evaluated. */
  393. SCM
  394. scm_c_eval_string (const char *expr)
  395. {
  396. return scm_eval_string (scm_from_locale_string (expr));
  397. }
  398. SCM
  399. scm_c_eval_string_in_module (const char *expr, SCM module)
  400. {
  401. return scm_eval_string_in_module (scm_from_locale_string (expr), module);
  402. }
  403. static SCM
  404. inner_eval_string (void *data)
  405. {
  406. SCM port = (SCM)data;
  407. SCM form;
  408. SCM ans = SCM_UNSPECIFIED;
  409. /* Read expressions from that port; ignore the values. */
  410. while (!SCM_EOF_OBJECT_P (form = scm_read (port)))
  411. ans = scm_primitive_eval_x (form);
  412. /* Don't close the port here; if we re-enter this function via a
  413. continuation, then the next time we enter it, we'll get an error.
  414. It's a string port anyway, so there's no advantage to closing it
  415. early. */
  416. return ans;
  417. }
  418. SCM_DEFINE (scm_eval_string_in_module, "eval-string", 1, 1, 0,
  419. (SCM string, SCM module),
  420. "Evaluate @var{string} as the text representation of a Scheme\n"
  421. "form or forms, and return whatever value they produce.\n"
  422. "Evaluation takes place in the given module, or the current\n"
  423. "module when no module is given.\n"
  424. "While the code is evaluated, the given module is made the\n"
  425. "current one. The current module is restored when this\n"
  426. "procedure returns.")
  427. #define FUNC_NAME s_scm_eval_string_in_module
  428. {
  429. SCM port = scm_mkstrport (SCM_INUM0, string, SCM_OPN | SCM_RDNG,
  430. FUNC_NAME);
  431. if (SCM_UNBNDP (module))
  432. module = scm_current_module ();
  433. else
  434. SCM_VALIDATE_MODULE (2, module);
  435. return scm_c_call_with_current_module (module,
  436. inner_eval_string, (void *)port);
  437. }
  438. #undef FUNC_NAME
  439. SCM
  440. scm_eval_string (SCM string)
  441. {
  442. return scm_eval_string_in_module (string, SCM_UNDEFINED);
  443. }
  444. static scm_t_bits
  445. scm_make_stptob ()
  446. {
  447. scm_t_bits tc = scm_make_port_type ("string", stfill_buffer, st_write);
  448. scm_set_port_mark (tc, scm_markstream);
  449. scm_set_port_end_input (tc, st_end_input);
  450. scm_set_port_flush (tc, st_flush);
  451. scm_set_port_seek (tc, st_seek);
  452. scm_set_port_truncate (tc, st_truncate);
  453. return tc;
  454. }
  455. void
  456. scm_init_strports ()
  457. {
  458. scm_tc16_strport = scm_make_stptob ();
  459. #include "libguile/strports.x"
  460. }
  461. /*
  462. Local Variables:
  463. c-file-style: "gnu"
  464. End:
  465. */