strports.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* Copyright (C) 1995,1996,1998,1999, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program 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
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include "libguile/_scm.h"
  42. #include <stdio.h>
  43. #ifdef HAVE_UNISTD_H
  44. #include <unistd.h>
  45. #endif
  46. #include "libguile/unif.h"
  47. #include "libguile/eval.h"
  48. #include "libguile/ports.h"
  49. #include "libguile/read.h"
  50. #include "libguile/root.h"
  51. #include "libguile/strings.h"
  52. #include "libguile/vectors.h"
  53. #include "libguile/strports.h"
  54. #ifdef HAVE_STRING_H
  55. #include <string.h>
  56. #endif
  57. /* {Ports - string ports}
  58. *
  59. */
  60. /* NOTES:
  61. write_buf/write_end point to the ends of the allocated string.
  62. read_buf/read_end in principle point to the part of the string which
  63. has been written to, but this is only updated after a flush.
  64. read_pos and write_pos in principle should be equal, but this is only true
  65. when rw_active is SCM_PORT_NEITHER.
  66. */
  67. static int
  68. stfill_buffer (SCM port)
  69. {
  70. scm_port *pt = SCM_PTAB_ENTRY (port);
  71. if (pt->read_pos >= pt->read_end)
  72. return EOF;
  73. else
  74. return scm_return_first_int (*pt->read_pos, port);
  75. }
  76. /* change the size of a port's string to new_size. this doesn't
  77. change read_buf_size. */
  78. static void
  79. st_resize_port (scm_port *pt, off_t new_size)
  80. {
  81. SCM stream = SCM_PACK (pt->stream);
  82. off_t index = pt->write_pos - pt->write_buf;
  83. pt->write_buf_size = new_size;
  84. scm_vector_set_length_x (stream, SCM_MAKINUM (new_size));
  85. /* reset buffer in case reallocation moved the string. */
  86. {
  87. pt->read_buf = pt->write_buf = SCM_UCHARS (stream);
  88. pt->read_pos = pt->write_pos = pt->write_buf + index;
  89. pt->write_end = pt->write_buf + pt->write_buf_size;
  90. pt->read_end = pt->read_buf + pt->read_buf_size;
  91. }
  92. }
  93. /* amount by which write_buf is expanded. */
  94. #define SCM_WRITE_BLOCK 80
  95. /* ensure that write_pos < write_end by enlarging the buffer when
  96. necessary. update read_buf to account for written chars. */
  97. static void
  98. st_flush (SCM port)
  99. {
  100. scm_port *pt = SCM_PTAB_ENTRY (port);
  101. if (pt->write_pos == pt->write_end)
  102. {
  103. st_resize_port (pt, pt->write_buf_size + SCM_WRITE_BLOCK);
  104. }
  105. pt->read_pos = pt->write_pos;
  106. if (pt->read_pos > pt->read_end)
  107. {
  108. pt->read_end = (unsigned char *) pt->read_pos;
  109. pt->read_buf_size = pt->read_end - pt->read_buf;
  110. }
  111. pt->rw_active = SCM_PORT_NEITHER;
  112. }
  113. static void
  114. st_write (SCM port, const void *data, size_t size)
  115. {
  116. scm_port *pt = SCM_PTAB_ENTRY (port);
  117. const char *input = (char *) data;
  118. while (size > 0)
  119. {
  120. int space = pt->write_end - pt->write_pos;
  121. int write_len = (size > space) ? space : size;
  122. strncpy ((char *) pt->write_pos, input, write_len);
  123. pt->write_pos += write_len;
  124. size -= write_len;
  125. input += write_len;
  126. if (write_len == space)
  127. st_flush (port);
  128. }
  129. }
  130. static void
  131. st_end_input (SCM port, int offset)
  132. {
  133. scm_port *pt = SCM_PTAB_ENTRY (port);
  134. if (pt->read_pos - pt->read_buf < offset)
  135. scm_misc_error ("st_end_input", "negative position", SCM_EOL);
  136. pt->write_pos = (unsigned char *) (pt->read_pos = pt->read_pos - offset);
  137. pt->rw_active = SCM_PORT_NEITHER;
  138. }
  139. static off_t
  140. st_seek (SCM port, off_t offset, int whence)
  141. {
  142. scm_port *pt = SCM_PTAB_ENTRY (port);
  143. off_t target;
  144. if (pt->rw_active == SCM_PORT_READ && offset == 0 && whence == SEEK_CUR)
  145. /* special case to avoid disturbing the unread-char buffer. */
  146. {
  147. if (pt->read_buf == pt->putback_buf)
  148. {
  149. target = pt->saved_read_pos - pt->saved_read_buf
  150. - (pt->read_end - pt->read_pos);
  151. }
  152. else
  153. {
  154. target = pt->read_pos - pt->read_buf;
  155. }
  156. }
  157. else
  158. /* all other cases. */
  159. {
  160. if (pt->rw_active == SCM_PORT_WRITE)
  161. st_flush (port);
  162. if (pt->rw_active == SCM_PORT_READ)
  163. scm_end_input (port);
  164. switch (whence)
  165. {
  166. case SEEK_CUR:
  167. target = pt->read_pos - pt->read_buf + offset;
  168. break;
  169. case SEEK_END:
  170. target = pt->read_end - pt->read_buf + offset;
  171. break;
  172. default: /* SEEK_SET */
  173. target = offset;
  174. break;
  175. }
  176. if (target < 0)
  177. scm_misc_error ("st_seek", "negative offset", SCM_EOL);
  178. if (target >= pt->write_buf_size)
  179. {
  180. if (!(SCM_CELL_WORD_0 (port) & SCM_WRTNG))
  181. {
  182. if (target > pt->write_buf_size)
  183. {
  184. scm_misc_error ("st_seek",
  185. "seek past end of read-only strport",
  186. SCM_EOL);
  187. }
  188. }
  189. else
  190. {
  191. st_resize_port (pt, target + (target == pt->write_buf_size
  192. ? SCM_WRITE_BLOCK
  193. : 0));
  194. }
  195. }
  196. pt->read_pos = pt->write_pos = pt->read_buf + target;
  197. if (pt->read_pos > pt->read_end)
  198. {
  199. pt->read_end = (unsigned char *) pt->read_pos;
  200. pt->read_buf_size = pt->read_end - pt->read_buf;
  201. }
  202. }
  203. return target;
  204. }
  205. static void
  206. st_truncate (SCM port, off_t length)
  207. {
  208. scm_port *pt = SCM_PTAB_ENTRY (port);
  209. if (length > pt->write_buf_size)
  210. st_resize_port (pt, length);
  211. pt->read_buf_size = length;
  212. pt->read_end = pt->read_buf + length;
  213. if (pt->read_pos > pt->read_end)
  214. pt->read_pos = pt->read_end;
  215. if (pt->write_pos > pt->read_end)
  216. pt->write_pos = pt->read_end;
  217. }
  218. SCM
  219. scm_mkstrport (SCM pos, SCM str, long modes, const char *caller)
  220. {
  221. SCM z;
  222. scm_port *pt;
  223. int str_len;
  224. SCM_ASSERT (SCM_INUMP(pos) && SCM_INUM(pos) >= 0, pos, SCM_ARG1, caller);
  225. SCM_ASSERT (SCM_ROSTRINGP(str), str, SCM_ARG1, caller);
  226. str_len = SCM_ROLENGTH (str);
  227. if (SCM_INUM (pos) > str_len)
  228. scm_out_of_range (caller, pos);
  229. if (!((modes & SCM_WRTNG) || (modes & SCM_RDNG)))
  230. scm_misc_error ("scm_mkstrport", "port must read or write", SCM_EOL);
  231. SCM_NEWCELL (z);
  232. SCM_DEFER_INTS;
  233. pt = scm_add_to_port_table (z);
  234. SCM_SET_CELL_TYPE (z, scm_tc16_strport | modes);
  235. SCM_SETPTAB_ENTRY (z, pt);
  236. SCM_SETSTREAM (z, SCM_UNPACK (str));
  237. pt->write_buf = pt->read_buf = SCM_ROUCHARS (str);
  238. pt->read_pos = pt->write_pos = pt->read_buf + SCM_INUM (pos);
  239. pt->write_buf_size = pt->read_buf_size = str_len;
  240. pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size;
  241. pt->rw_random = 1;
  242. SCM_ALLOW_INTS;
  243. /* ensure write_pos is writable. */
  244. if ((modes & SCM_WRTNG) && pt->write_pos == pt->write_end)
  245. st_flush (z);
  246. return z;
  247. }
  248. /* create a new string from a string port's buffer. */
  249. SCM scm_strport_to_string (SCM port)
  250. {
  251. scm_port *pt = SCM_PTAB_ENTRY (port);
  252. if (pt->rw_active == SCM_PORT_WRITE)
  253. st_flush (port);
  254. return scm_makfromstr ((char *) pt->read_buf, pt->read_buf_size, 0);
  255. }
  256. SCM_DEFINE (scm_call_with_output_string, "call-with-output-string", 1, 0, 0,
  257. (SCM proc),
  258. "Calls the one-argument procedure @var{proc} with a newly created output\n"
  259. "port. When the function returns, the string composed of the characters\n"
  260. "written into the port is returned.")
  261. #define FUNC_NAME s_scm_call_with_output_string
  262. {
  263. SCM p;
  264. p = scm_mkstrport (SCM_INUM0,
  265. scm_make_string (SCM_INUM0, SCM_UNDEFINED),
  266. SCM_OPN | SCM_WRTNG,
  267. FUNC_NAME);
  268. scm_apply (proc, p, scm_listofnull);
  269. return scm_strport_to_string (p);
  270. }
  271. #undef FUNC_NAME
  272. /* Return a Scheme string obtained by printing a given object.
  273. */
  274. SCM
  275. scm_strprint_obj (SCM obj)
  276. {
  277. SCM str;
  278. SCM port;
  279. str = scm_makstr (0, 0);
  280. port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_WRTNG, "scm_strprint_obj");
  281. scm_prin1 (obj, port, 1);
  282. {
  283. return scm_strport_to_string (port);
  284. }
  285. }
  286. SCM_DEFINE (scm_call_with_input_string, "call-with-input-string", 2, 0, 0,
  287. (SCM str, SCM proc),
  288. "Calls the one-argument procedure @var{proc} with a newly created input\n"
  289. "port from which @var{string}'s contents may be read. The value yielded\n"
  290. "by the @var{proc} is returned.")
  291. #define FUNC_NAME s_scm_call_with_input_string
  292. {
  293. SCM p = scm_mkstrport(SCM_INUM0, str, SCM_OPN | SCM_RDNG, FUNC_NAME);
  294. return scm_apply (proc, p, scm_listofnull);
  295. }
  296. #undef FUNC_NAME
  297. /* Given a null-terminated string EXPR containing a Scheme expression
  298. read it, and return it as an SCM value. */
  299. SCM
  300. scm_read_0str (char *expr)
  301. {
  302. SCM port = scm_mkstrport (SCM_INUM0,
  303. scm_makfrom0str (expr),
  304. SCM_OPN | SCM_RDNG,
  305. "scm_eval_0str");
  306. SCM form;
  307. /* Read expressions from that port; ignore the values. */
  308. form = scm_read (port);
  309. scm_close_port (port);
  310. return form;
  311. }
  312. /* Given a null-terminated string EXPR containing Scheme program text,
  313. evaluate it, and return the result of the last expression evaluated. */
  314. SCM
  315. scm_eval_0str (const char *expr)
  316. {
  317. return scm_eval_string (scm_makfrom0str (expr));
  318. }
  319. SCM_DEFINE (scm_eval_string, "eval-string", 1, 0, 0,
  320. (SCM string),
  321. "Evaluate @var{string} as the text representation of a Scheme form\n"
  322. "or forms, and return whatever value they produce.")
  323. #define FUNC_NAME s_scm_eval_string
  324. {
  325. SCM port = scm_mkstrport (SCM_INUM0, string, SCM_OPN | SCM_RDNG,
  326. "scm_eval_0str");
  327. SCM form;
  328. SCM ans = SCM_UNSPECIFIED;
  329. /* Read expressions from that port; ignore the values. */
  330. while (!SCM_EOF_OBJECT_P (form = scm_read (port)))
  331. ans = scm_eval_x (form);
  332. /* Don't close the port here; if we re-enter this function via a
  333. continuation, then the next time we enter it, we'll get an error.
  334. It's a string port anyway, so there's no advantage to closing it
  335. early. */
  336. return ans;
  337. }
  338. #undef FUNC_NAME
  339. void scm_make_stptob (void); /* Called from ports.c */
  340. void
  341. scm_make_stptob ()
  342. {
  343. long tc = scm_make_port_type ("string", stfill_buffer, st_write);
  344. scm_set_port_mark (tc, scm_markstream);
  345. scm_set_port_end_input (tc, st_end_input);
  346. scm_set_port_flush (tc, st_flush);
  347. scm_set_port_seek (tc, st_seek);
  348. scm_set_port_truncate (tc, st_truncate);
  349. }
  350. void
  351. scm_init_strports ()
  352. {
  353. #include "libguile/strports.x"
  354. }
  355. /*
  356. Local Variables:
  357. c-file-style: "gnu"
  358. End:
  359. */