strports.c 16 KB

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