r6rs-ports.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. /* Copyright (C) 2009, 2010, 2011, 2013, 2014 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. #ifdef HAVE_UNISTD_H
  22. # include <unistd.h>
  23. #endif
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <assert.h>
  27. #include "libguile/_scm.h"
  28. #include "libguile/bytevectors.h"
  29. #include "libguile/chars.h"
  30. #include "libguile/eval.h"
  31. #include "libguile/r6rs-ports.h"
  32. #include "libguile/strings.h"
  33. #include "libguile/validate.h"
  34. #include "libguile/values.h"
  35. #include "libguile/vectors.h"
  36. /* Unimplemented features. */
  37. /* Transoders are currently not implemented since Guile 1.8 is not
  38. Unicode-capable. Thus, most of the code here assumes the use of the
  39. binary transcoder. */
  40. static inline void
  41. transcoders_not_implemented (void)
  42. {
  43. fprintf (stderr, "%s: warning: transcoders not implemented\n",
  44. PACKAGE_NAME);
  45. }
  46. /* End-of-file object. */
  47. SCM_DEFINE (scm_eof_object, "eof-object", 0, 0, 0,
  48. (void),
  49. "Return the end-of-file object.")
  50. #define FUNC_NAME s_scm_eof_object
  51. {
  52. return (SCM_EOF_VAL);
  53. }
  54. #undef FUNC_NAME
  55. /* Input ports. */
  56. #ifndef MIN
  57. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  58. #endif
  59. /* Bytevector input ports or "bip" for short. */
  60. static scm_t_bits bytevector_input_port_type = 0;
  61. static inline SCM
  62. make_bip (SCM bv)
  63. {
  64. SCM port;
  65. char *c_bv;
  66. unsigned c_len;
  67. scm_t_port *c_port;
  68. const unsigned long mode_bits = SCM_OPN | SCM_RDNG;
  69. port = scm_c_make_port_with_encoding (bytevector_input_port_type,
  70. mode_bits,
  71. NULL, /* encoding */
  72. SCM_FAILED_CONVERSION_ERROR,
  73. SCM_UNPACK (bv));
  74. c_port = SCM_PTAB_ENTRY (port);
  75. /* Have the port directly access the bytevector. */
  76. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  77. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  78. c_port->read_pos = c_port->read_buf = (unsigned char *) c_bv;
  79. c_port->read_end = (unsigned char *) c_bv + c_len;
  80. c_port->read_buf_size = c_len;
  81. return port;
  82. }
  83. static int
  84. bip_fill_input (SCM port)
  85. {
  86. int result;
  87. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  88. if (c_port->read_pos >= c_port->read_end)
  89. result = EOF;
  90. else
  91. result = (int) *c_port->read_pos;
  92. return result;
  93. }
  94. static scm_t_off
  95. bip_seek (SCM port, scm_t_off offset, int whence)
  96. #define FUNC_NAME "bip_seek"
  97. {
  98. scm_t_off c_result = 0;
  99. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  100. switch (whence)
  101. {
  102. case SEEK_CUR:
  103. offset += c_port->read_pos - c_port->read_buf;
  104. /* Fall through. */
  105. case SEEK_SET:
  106. if (c_port->read_buf + offset <= c_port->read_end)
  107. {
  108. c_port->read_pos = c_port->read_buf + offset;
  109. c_result = offset;
  110. }
  111. else
  112. scm_out_of_range (FUNC_NAME, scm_from_int (offset));
  113. break;
  114. case SEEK_END:
  115. if (c_port->read_end - offset >= c_port->read_buf)
  116. {
  117. c_port->read_pos = c_port->read_end - offset;
  118. c_result = c_port->read_pos - c_port->read_buf;
  119. }
  120. else
  121. scm_out_of_range (FUNC_NAME, scm_from_int (offset));
  122. break;
  123. default:
  124. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  125. "invalid `seek' parameter");
  126. }
  127. return c_result;
  128. }
  129. #undef FUNC_NAME
  130. /* Instantiate the bytevector input port type. */
  131. static inline void
  132. initialize_bytevector_input_ports (void)
  133. {
  134. bytevector_input_port_type =
  135. scm_make_port_type ("r6rs-bytevector-input-port", bip_fill_input,
  136. NULL);
  137. scm_set_port_seek (bytevector_input_port_type, bip_seek);
  138. }
  139. SCM_DEFINE (scm_open_bytevector_input_port,
  140. "open-bytevector-input-port", 1, 1, 0,
  141. (SCM bv, SCM transcoder),
  142. "Return an input port whose contents are drawn from "
  143. "bytevector @var{bv}.")
  144. #define FUNC_NAME s_scm_open_bytevector_input_port
  145. {
  146. SCM_VALIDATE_BYTEVECTOR (1, bv);
  147. if (!SCM_UNBNDP (transcoder) && !scm_is_false (transcoder))
  148. transcoders_not_implemented ();
  149. return (make_bip (bv));
  150. }
  151. #undef FUNC_NAME
  152. /* Custom binary ports. The following routines are shared by input and
  153. output custom binary ports. */
  154. #define SCM_CBP_GET_POSITION_PROC(_port) \
  155. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 1)
  156. #define SCM_CBP_SET_POSITION_PROC(_port) \
  157. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 2)
  158. #define SCM_CBP_CLOSE_PROC(_port) \
  159. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 3)
  160. static scm_t_off
  161. cbp_seek (SCM port, scm_t_off offset, int whence)
  162. #define FUNC_NAME "cbp_seek"
  163. {
  164. SCM result;
  165. scm_t_off c_result = 0;
  166. switch (whence)
  167. {
  168. case SEEK_CUR:
  169. {
  170. SCM get_position_proc;
  171. get_position_proc = SCM_CBP_GET_POSITION_PROC (port);
  172. if (SCM_LIKELY (scm_is_true (get_position_proc)))
  173. result = scm_call_0 (get_position_proc);
  174. else
  175. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  176. "R6RS custom binary port with "
  177. "`port-position' support");
  178. c_result = scm_to_int (result);
  179. if (offset == 0)
  180. /* We just want to know the current position. */
  181. break;
  182. offset += c_result;
  183. /* Fall through. */
  184. }
  185. case SEEK_SET:
  186. {
  187. SCM set_position_proc;
  188. set_position_proc = SCM_CBP_SET_POSITION_PROC (port);
  189. if (SCM_LIKELY (scm_is_true (set_position_proc)))
  190. result = scm_call_1 (set_position_proc, scm_from_int (offset));
  191. else
  192. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  193. "seekable R6RS custom binary port");
  194. /* Assuming setting the position succeeded. */
  195. c_result = offset;
  196. break;
  197. }
  198. default:
  199. /* `SEEK_END' cannot be supported. */
  200. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  201. "R6RS custom binary ports do not "
  202. "support `SEEK_END'");
  203. }
  204. return c_result;
  205. }
  206. #undef FUNC_NAME
  207. static int
  208. cbp_close (SCM port)
  209. {
  210. SCM close_proc;
  211. close_proc = SCM_CBP_CLOSE_PROC (port);
  212. if (scm_is_true (close_proc))
  213. /* Invoke the `close' thunk. */
  214. scm_call_0 (close_proc);
  215. return 1;
  216. }
  217. /* Custom binary input port ("cbip" for short). */
  218. static scm_t_bits custom_binary_input_port_type = 0;
  219. /* Size of the buffer embedded in custom binary input ports. */
  220. #define CBIP_BUFFER_SIZE 4096
  221. /* Return the bytevector associated with PORT. */
  222. #define SCM_CBIP_BYTEVECTOR(_port) \
  223. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 4)
  224. /* Return the various procedures of PORT. */
  225. #define SCM_CBIP_READ_PROC(_port) \
  226. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 0)
  227. static inline SCM
  228. make_cbip (SCM read_proc, SCM get_position_proc,
  229. SCM set_position_proc, SCM close_proc)
  230. {
  231. SCM port, bv, method_vector;
  232. char *c_bv;
  233. unsigned c_len;
  234. scm_t_port *c_port;
  235. const unsigned long mode_bits = SCM_OPN | SCM_RDNG;
  236. /* Use a bytevector as the underlying buffer. */
  237. c_len = CBIP_BUFFER_SIZE;
  238. bv = scm_c_make_bytevector (c_len);
  239. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  240. /* Store the various methods and bytevector in a vector. */
  241. method_vector = scm_c_make_vector (5, SCM_BOOL_F);
  242. SCM_SIMPLE_VECTOR_SET (method_vector, 4, bv);
  243. SCM_SIMPLE_VECTOR_SET (method_vector, 0, read_proc);
  244. SCM_SIMPLE_VECTOR_SET (method_vector, 1, get_position_proc);
  245. SCM_SIMPLE_VECTOR_SET (method_vector, 2, set_position_proc);
  246. SCM_SIMPLE_VECTOR_SET (method_vector, 3, close_proc);
  247. port = scm_c_make_port_with_encoding (custom_binary_input_port_type,
  248. mode_bits,
  249. NULL, /* encoding */
  250. SCM_FAILED_CONVERSION_ERROR,
  251. SCM_UNPACK (method_vector));
  252. c_port = SCM_PTAB_ENTRY (port);
  253. /* Have the port directly access the buffer (bytevector). */
  254. c_port->read_pos = c_port->read_buf = (unsigned char *) c_bv;
  255. c_port->read_end = (unsigned char *) c_bv;
  256. c_port->read_buf_size = c_len;
  257. return port;
  258. }
  259. static int
  260. cbip_fill_input (SCM port)
  261. #define FUNC_NAME "cbip_fill_input"
  262. {
  263. int result;
  264. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  265. again:
  266. if (c_port->read_pos >= c_port->read_end)
  267. {
  268. /* Invoke the user's `read!' procedure. */
  269. size_t c_octets, c_requested;
  270. SCM bv, read_proc, octets;
  271. c_requested = c_port->read_buf_size;
  272. /* Use the bytevector associated with PORT as the buffer passed to the
  273. `read!' procedure, thereby avoiding additional allocations. */
  274. bv = SCM_CBIP_BYTEVECTOR (port);
  275. read_proc = SCM_CBIP_READ_PROC (port);
  276. /* The assumption here is that C_PORT's internal buffer wasn't changed
  277. behind our back. */
  278. assert (c_port->read_buf ==
  279. (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv));
  280. assert ((unsigned) c_port->read_buf_size
  281. == SCM_BYTEVECTOR_LENGTH (bv));
  282. octets = scm_call_3 (read_proc, bv, SCM_INUM0,
  283. scm_from_size_t (c_requested));
  284. c_octets = scm_to_size_t (octets);
  285. if (SCM_UNLIKELY (c_octets > c_requested))
  286. scm_out_of_range (FUNC_NAME, octets);
  287. c_port->read_pos = (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv);
  288. c_port->read_end = (unsigned char *) c_port->read_pos + c_octets;
  289. if (c_octets > 0)
  290. goto again;
  291. else
  292. result = EOF;
  293. }
  294. else
  295. result = (int) *c_port->read_pos;
  296. return result;
  297. }
  298. #undef FUNC_NAME
  299. SCM_DEFINE (scm_make_custom_binary_input_port,
  300. "make-custom-binary-input-port", 5, 0, 0,
  301. (SCM id, SCM read_proc, SCM get_position_proc,
  302. SCM set_position_proc, SCM close_proc),
  303. "Return a new custom binary input port whose input is drained "
  304. "by invoking @var{read_proc} and passing it a bytevector, an "
  305. "index where octets should be written, and an octet count.")
  306. #define FUNC_NAME s_scm_make_custom_binary_input_port
  307. {
  308. SCM_VALIDATE_STRING (1, id);
  309. SCM_VALIDATE_PROC (2, read_proc);
  310. if (!scm_is_false (get_position_proc))
  311. SCM_VALIDATE_PROC (3, get_position_proc);
  312. if (!scm_is_false (set_position_proc))
  313. SCM_VALIDATE_PROC (4, set_position_proc);
  314. if (!scm_is_false (close_proc))
  315. SCM_VALIDATE_PROC (5, close_proc);
  316. return (make_cbip (read_proc, get_position_proc, set_position_proc,
  317. close_proc));
  318. }
  319. #undef FUNC_NAME
  320. /* Instantiate the custom binary input port type. */
  321. static inline void
  322. initialize_custom_binary_input_ports (void)
  323. {
  324. custom_binary_input_port_type =
  325. scm_make_port_type ("r6rs-custom-binary-input-port",
  326. cbip_fill_input, NULL);
  327. scm_set_port_seek (custom_binary_input_port_type, cbp_seek);
  328. scm_set_port_close (custom_binary_input_port_type, cbp_close);
  329. }
  330. /* Binary input. */
  331. /* We currently don't support specific binary input ports. */
  332. #define SCM_VALIDATE_BINARY_INPUT_PORT SCM_VALIDATE_OPINPORT
  333. SCM_DEFINE (scm_get_u8, "get-u8", 1, 0, 0,
  334. (SCM port),
  335. "Read an octet from @var{port}, a binary input port, "
  336. "blocking as necessary.")
  337. #define FUNC_NAME s_scm_get_u8
  338. {
  339. SCM result;
  340. int c_result;
  341. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  342. c_result = scm_get_byte_or_eof (port);
  343. if (c_result == EOF)
  344. result = SCM_EOF_VAL;
  345. else
  346. result = SCM_I_MAKINUM ((unsigned char) c_result);
  347. return result;
  348. }
  349. #undef FUNC_NAME
  350. SCM_DEFINE (scm_lookahead_u8, "lookahead-u8", 1, 0, 0,
  351. (SCM port),
  352. "Like @code{get-u8} but does not update @var{port} to "
  353. "point past the octet.")
  354. #define FUNC_NAME s_scm_lookahead_u8
  355. {
  356. int u8;
  357. SCM result;
  358. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  359. u8 = scm_peek_byte_or_eof (port);
  360. if (u8 == EOF)
  361. result = SCM_EOF_VAL;
  362. else
  363. result = SCM_I_MAKINUM ((scm_t_uint8) u8);
  364. return result;
  365. }
  366. #undef FUNC_NAME
  367. SCM_DEFINE (scm_get_bytevector_n, "get-bytevector-n", 2, 0, 0,
  368. (SCM port, SCM count),
  369. "Read @var{count} octets from @var{port}, blocking as "
  370. "necessary and return a bytevector containing the octets "
  371. "read. If fewer bytes are available, a bytevector smaller "
  372. "than @var{count} is returned.")
  373. #define FUNC_NAME s_scm_get_bytevector_n
  374. {
  375. SCM result;
  376. char *c_bv;
  377. unsigned c_count;
  378. size_t c_read;
  379. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  380. c_count = scm_to_uint (count);
  381. result = scm_c_make_bytevector (c_count);
  382. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (result);
  383. if (SCM_LIKELY (c_count > 0))
  384. /* XXX: `scm_c_read ()' does not update the port position. */
  385. c_read = scm_c_read_unlocked (port, c_bv, c_count);
  386. else
  387. /* Don't invoke `scm_c_read ()' since it may block. */
  388. c_read = 0;
  389. if (c_read < c_count)
  390. {
  391. if (c_read == 0)
  392. result = SCM_EOF_VAL;
  393. else
  394. result = scm_c_shrink_bytevector (result, c_read);
  395. }
  396. return result;
  397. }
  398. #undef FUNC_NAME
  399. SCM_DEFINE (scm_get_bytevector_n_x, "get-bytevector-n!", 4, 0, 0,
  400. (SCM port, SCM bv, SCM start, SCM count),
  401. "Read @var{count} bytes from @var{port} and store them "
  402. "in @var{bv} starting at index @var{start}. Return either "
  403. "the number of bytes actually read or the end-of-file "
  404. "object.")
  405. #define FUNC_NAME s_scm_get_bytevector_n_x
  406. {
  407. SCM result;
  408. char *c_bv;
  409. unsigned c_start, c_count, c_len;
  410. size_t c_read;
  411. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  412. SCM_VALIDATE_BYTEVECTOR (2, bv);
  413. c_start = scm_to_uint (start);
  414. c_count = scm_to_uint (count);
  415. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  416. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  417. if (SCM_UNLIKELY (c_start + c_count > c_len))
  418. scm_out_of_range (FUNC_NAME, count);
  419. if (SCM_LIKELY (c_count > 0))
  420. c_read = scm_c_read_unlocked (port, c_bv + c_start, c_count);
  421. else
  422. /* Don't invoke `scm_c_read ()' since it may block. */
  423. c_read = 0;
  424. if (c_read == 0 && c_count > 0)
  425. result = SCM_EOF_VAL;
  426. else
  427. result = scm_from_size_t (c_read);
  428. return result;
  429. }
  430. #undef FUNC_NAME
  431. SCM_DEFINE (scm_get_bytevector_some, "get-bytevector-some", 1, 0, 0,
  432. (SCM port),
  433. "Read from @var{port}, blocking as necessary, until bytes "
  434. "are available or an end-of-file is reached. Return either "
  435. "the end-of-file object or a new bytevector containing some "
  436. "of the available bytes (at least one), and update the port "
  437. "position to point just past these bytes.")
  438. #define FUNC_NAME s_scm_get_bytevector_some
  439. {
  440. scm_t_port *pt;
  441. size_t size;
  442. SCM bv;
  443. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  444. pt = SCM_PTAB_ENTRY (port);
  445. if (pt->rw_active == SCM_PORT_WRITE)
  446. scm_flush_unlocked (port);
  447. if (pt->rw_random)
  448. pt->rw_active = SCM_PORT_READ;
  449. if (pt->read_pos >= pt->read_end)
  450. {
  451. if (scm_fill_input_unlocked (port) == EOF)
  452. return SCM_EOF_VAL;
  453. }
  454. size = pt->read_end - pt->read_pos;
  455. if (pt->read_buf == pt->putback_buf)
  456. size += pt->saved_read_end - pt->saved_read_pos;
  457. bv = scm_c_make_bytevector (size);
  458. scm_take_from_input_buffers
  459. (port, (char *) SCM_BYTEVECTOR_CONTENTS (bv), size);
  460. return bv;
  461. }
  462. #undef FUNC_NAME
  463. SCM_DEFINE (scm_get_bytevector_all, "get-bytevector-all", 1, 0, 0,
  464. (SCM port),
  465. "Read from @var{port}, blocking as necessary, until "
  466. "the end-of-file is reached. Return either "
  467. "a new bytevector containing the data read or the "
  468. "end-of-file object (if no data were available).")
  469. #define FUNC_NAME s_scm_get_bytevector_all
  470. {
  471. SCM result;
  472. char *c_bv;
  473. unsigned c_len, c_count;
  474. size_t c_read, c_total;
  475. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  476. c_len = c_count = 4096;
  477. c_bv = (char *) scm_gc_malloc_pointerless (c_len, SCM_GC_BYTEVECTOR);
  478. c_total = c_read = 0;
  479. do
  480. {
  481. if (c_total + c_read > c_len)
  482. {
  483. /* Grow the bytevector. */
  484. c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_len * 2,
  485. SCM_GC_BYTEVECTOR);
  486. c_count = c_len;
  487. c_len *= 2;
  488. }
  489. /* `scm_c_read ()' blocks until C_COUNT bytes are available or EOF is
  490. reached. */
  491. c_read = scm_c_read_unlocked (port, c_bv + c_total, c_count);
  492. c_total += c_read, c_count -= c_read;
  493. }
  494. while (c_count == 0);
  495. if (c_total == 0)
  496. {
  497. result = SCM_EOF_VAL;
  498. scm_gc_free (c_bv, c_len, SCM_GC_BYTEVECTOR);
  499. }
  500. else
  501. {
  502. if (c_len > c_total)
  503. {
  504. /* Shrink the bytevector. */
  505. c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_total,
  506. SCM_GC_BYTEVECTOR);
  507. c_len = (unsigned) c_total;
  508. }
  509. result = scm_c_take_gc_bytevector ((signed char *) c_bv, c_len,
  510. SCM_BOOL_F);
  511. }
  512. return result;
  513. }
  514. #undef FUNC_NAME
  515. /* Binary output. */
  516. /* We currently don't support specific binary input ports. */
  517. #define SCM_VALIDATE_BINARY_OUTPUT_PORT SCM_VALIDATE_OPOUTPORT
  518. SCM_DEFINE (scm_put_u8, "put-u8", 2, 0, 0,
  519. (SCM port, SCM octet),
  520. "Write @var{octet} to binary port @var{port}.")
  521. #define FUNC_NAME s_scm_put_u8
  522. {
  523. scm_t_uint8 c_octet;
  524. SCM_VALIDATE_BINARY_OUTPUT_PORT (1, port);
  525. c_octet = scm_to_uint8 (octet);
  526. scm_putc_unlocked ((char) c_octet, port);
  527. return SCM_UNSPECIFIED;
  528. }
  529. #undef FUNC_NAME
  530. SCM_DEFINE (scm_put_bytevector, "put-bytevector", 2, 2, 0,
  531. (SCM port, SCM bv, SCM start, SCM count),
  532. "Write the contents of @var{bv} to @var{port}, optionally "
  533. "starting at index @var{start} and limiting to @var{count} "
  534. "octets.")
  535. #define FUNC_NAME s_scm_put_bytevector
  536. {
  537. char *c_bv;
  538. unsigned c_start, c_count, c_len;
  539. SCM_VALIDATE_BINARY_OUTPUT_PORT (1, port);
  540. SCM_VALIDATE_BYTEVECTOR (2, bv);
  541. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  542. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  543. if (!scm_is_eq (start, SCM_UNDEFINED))
  544. {
  545. c_start = scm_to_uint (start);
  546. if (!scm_is_eq (count, SCM_UNDEFINED))
  547. {
  548. c_count = scm_to_uint (count);
  549. if (SCM_UNLIKELY (c_start + c_count > c_len))
  550. scm_out_of_range (FUNC_NAME, count);
  551. }
  552. else
  553. {
  554. if (SCM_UNLIKELY (c_start >= c_len))
  555. scm_out_of_range (FUNC_NAME, start);
  556. else
  557. c_count = c_len - c_start;
  558. }
  559. }
  560. else
  561. c_start = 0, c_count = c_len;
  562. scm_c_write_unlocked (port, c_bv + c_start, c_count);
  563. return SCM_UNSPECIFIED;
  564. }
  565. #undef FUNC_NAME
  566. SCM_DEFINE (scm_unget_bytevector, "unget-bytevector", 2, 2, 0,
  567. (SCM port, SCM bv, SCM start, SCM count),
  568. "Unget the contents of @var{bv} to @var{port}, optionally "
  569. "starting at index @var{start} and limiting to @var{count} "
  570. "octets.")
  571. #define FUNC_NAME s_scm_unget_bytevector
  572. {
  573. unsigned char *c_bv;
  574. size_t c_start, c_count, c_len;
  575. SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
  576. SCM_VALIDATE_BYTEVECTOR (2, bv);
  577. c_len = SCM_BYTEVECTOR_LENGTH (bv);
  578. c_bv = (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv);
  579. if (!scm_is_eq (start, SCM_UNDEFINED))
  580. {
  581. c_start = scm_to_size_t (start);
  582. if (!scm_is_eq (count, SCM_UNDEFINED))
  583. {
  584. c_count = scm_to_size_t (count);
  585. if (SCM_UNLIKELY (c_start + c_count > c_len))
  586. scm_out_of_range (FUNC_NAME, count);
  587. }
  588. else
  589. {
  590. if (SCM_UNLIKELY (c_start >= c_len))
  591. scm_out_of_range (FUNC_NAME, start);
  592. else
  593. c_count = c_len - c_start;
  594. }
  595. }
  596. else
  597. c_start = 0, c_count = c_len;
  598. scm_unget_bytes (c_bv + c_start, c_count, port);
  599. return SCM_UNSPECIFIED;
  600. }
  601. #undef FUNC_NAME
  602. /* Bytevector output port ("bop" for short). */
  603. /* Implementation of "bops".
  604. Each bop has an internal buffer, of type `scm_t_bop_buffer', attached to
  605. it. The procedure returned along with the output port is actually an
  606. applicable SMOB. The SMOB holds a reference to the port. When applied,
  607. the SMOB swallows the port's internal buffer, turning it into a
  608. bytevector, and resets it.
  609. XXX: Access to a bop's internal buffer is not thread-safe. */
  610. static scm_t_bits bytevector_output_port_type = 0;
  611. SCM_SMOB (bytevector_output_port_procedure,
  612. "r6rs-bytevector-output-port-procedure",
  613. 0);
  614. #define SCM_GC_BOP "r6rs-bytevector-output-port"
  615. #define SCM_BOP_BUFFER_INITIAL_SIZE 4096
  616. /* Representation of a bop's internal buffer. */
  617. typedef struct
  618. {
  619. size_t total_len;
  620. size_t len;
  621. size_t pos;
  622. char *buffer;
  623. } scm_t_bop_buffer;
  624. /* Accessing a bop's buffer. */
  625. #define SCM_BOP_BUFFER(_port) \
  626. ((scm_t_bop_buffer *) SCM_STREAM (_port))
  627. #define SCM_SET_BOP_BUFFER(_port, _buf) \
  628. (SCM_SETSTREAM ((_port), (scm_t_bits) (_buf)))
  629. static inline void
  630. bop_buffer_init (scm_t_bop_buffer *buf)
  631. {
  632. buf->total_len = buf->len = buf->pos = 0;
  633. buf->buffer = NULL;
  634. }
  635. static inline void
  636. bop_buffer_grow (scm_t_bop_buffer *buf, size_t min_size)
  637. {
  638. char *new_buf;
  639. size_t new_size;
  640. for (new_size = buf->total_len
  641. ? buf->total_len : SCM_BOP_BUFFER_INITIAL_SIZE;
  642. new_size < min_size;
  643. new_size *= 2);
  644. if (buf->buffer)
  645. new_buf = scm_gc_realloc ((void *) buf->buffer, buf->total_len,
  646. new_size, SCM_GC_BOP);
  647. else
  648. new_buf = scm_gc_malloc_pointerless (new_size, SCM_GC_BOP);
  649. buf->buffer = new_buf;
  650. buf->total_len = new_size;
  651. }
  652. static inline SCM
  653. make_bop (void)
  654. {
  655. SCM port, bop_proc;
  656. scm_t_port *c_port;
  657. scm_t_bop_buffer *buf;
  658. const unsigned long mode_bits = SCM_OPN | SCM_WRTNG;
  659. buf = (scm_t_bop_buffer *) scm_gc_malloc (sizeof (* buf), SCM_GC_BOP);
  660. bop_buffer_init (buf);
  661. port = scm_c_make_port_with_encoding (bytevector_output_port_type,
  662. mode_bits,
  663. NULL, /* encoding */
  664. SCM_FAILED_CONVERSION_ERROR,
  665. (scm_t_bits)buf);
  666. c_port = SCM_PTAB_ENTRY (port);
  667. c_port->write_buf = c_port->write_pos = c_port->write_end = NULL;
  668. c_port->write_buf_size = 0;
  669. /* Make the bop procedure. */
  670. SCM_NEWSMOB (bop_proc, bytevector_output_port_procedure, buf);
  671. return (scm_values (scm_list_2 (port, bop_proc)));
  672. }
  673. /* Write SIZE octets from DATA to PORT. */
  674. static void
  675. bop_write (SCM port, const void *data, size_t size)
  676. {
  677. scm_t_bop_buffer *buf;
  678. buf = SCM_BOP_BUFFER (port);
  679. if (buf->pos + size > buf->total_len)
  680. bop_buffer_grow (buf, buf->pos + size);
  681. memcpy (buf->buffer + buf->pos, data, size);
  682. buf->pos += size;
  683. buf->len = (buf->len > buf->pos) ? buf->len : buf->pos;
  684. }
  685. static scm_t_off
  686. bop_seek (SCM port, scm_t_off offset, int whence)
  687. #define FUNC_NAME "bop_seek"
  688. {
  689. scm_t_bop_buffer *buf;
  690. buf = SCM_BOP_BUFFER (port);
  691. switch (whence)
  692. {
  693. case SEEK_CUR:
  694. offset += (scm_t_off) buf->pos;
  695. /* Fall through. */
  696. case SEEK_SET:
  697. if (offset < 0 || (unsigned) offset > buf->len)
  698. scm_out_of_range (FUNC_NAME, scm_from_int (offset));
  699. else
  700. buf->pos = offset;
  701. break;
  702. case SEEK_END:
  703. if (offset < 0 || (unsigned) offset >= buf->len)
  704. scm_out_of_range (FUNC_NAME, scm_from_int (offset));
  705. else
  706. buf->pos = buf->len - (offset + 1);
  707. break;
  708. default:
  709. scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
  710. "invalid `seek' parameter");
  711. }
  712. return buf->pos;
  713. }
  714. #undef FUNC_NAME
  715. /* Fetch data from a bop. */
  716. SCM_SMOB_APPLY (bytevector_output_port_procedure,
  717. bop_proc_apply, 0, 0, 0, (SCM bop_proc))
  718. {
  719. SCM bv;
  720. scm_t_bop_buffer *buf, result_buf;
  721. buf = (scm_t_bop_buffer *) SCM_SMOB_DATA (bop_proc);
  722. result_buf = *buf;
  723. bop_buffer_init (buf);
  724. if (result_buf.len == 0)
  725. bv = scm_c_take_gc_bytevector (NULL, 0, SCM_BOOL_F);
  726. else
  727. {
  728. if (result_buf.total_len > result_buf.len)
  729. /* Shrink the buffer. */
  730. result_buf.buffer = scm_gc_realloc ((void *) result_buf.buffer,
  731. result_buf.total_len,
  732. result_buf.len,
  733. SCM_GC_BOP);
  734. bv = scm_c_take_gc_bytevector ((signed char *) result_buf.buffer,
  735. result_buf.len, SCM_BOOL_F);
  736. }
  737. return bv;
  738. }
  739. SCM_DEFINE (scm_open_bytevector_output_port,
  740. "open-bytevector-output-port", 0, 1, 0,
  741. (SCM transcoder),
  742. "Return two values: an output port and a procedure. The latter "
  743. "should be called with zero arguments to obtain a bytevector "
  744. "containing the data accumulated by the port.")
  745. #define FUNC_NAME s_scm_open_bytevector_output_port
  746. {
  747. if (!SCM_UNBNDP (transcoder) && !scm_is_false (transcoder))
  748. transcoders_not_implemented ();
  749. return (make_bop ());
  750. }
  751. #undef FUNC_NAME
  752. static inline void
  753. initialize_bytevector_output_ports (void)
  754. {
  755. bytevector_output_port_type =
  756. scm_make_port_type ("r6rs-bytevector-output-port",
  757. NULL, bop_write);
  758. scm_set_port_seek (bytevector_output_port_type, bop_seek);
  759. }
  760. /* Custom binary output port ("cbop" for short). */
  761. static scm_t_bits custom_binary_output_port_type;
  762. /* Return the various procedures of PORT. */
  763. #define SCM_CBOP_WRITE_PROC(_port) \
  764. SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 0)
  765. static inline SCM
  766. make_cbop (SCM write_proc, SCM get_position_proc,
  767. SCM set_position_proc, SCM close_proc)
  768. {
  769. SCM port, method_vector;
  770. scm_t_port *c_port;
  771. const unsigned long mode_bits = SCM_OPN | SCM_WRTNG;
  772. /* Store the various methods and bytevector in a vector. */
  773. method_vector = scm_c_make_vector (4, SCM_BOOL_F);
  774. SCM_SIMPLE_VECTOR_SET (method_vector, 0, write_proc);
  775. SCM_SIMPLE_VECTOR_SET (method_vector, 1, get_position_proc);
  776. SCM_SIMPLE_VECTOR_SET (method_vector, 2, set_position_proc);
  777. SCM_SIMPLE_VECTOR_SET (method_vector, 3, close_proc);
  778. port = scm_c_make_port_with_encoding (custom_binary_output_port_type,
  779. mode_bits,
  780. NULL, /* encoding */
  781. SCM_FAILED_CONVERSION_ERROR,
  782. SCM_UNPACK (method_vector));
  783. c_port = SCM_PTAB_ENTRY (port);
  784. /* Have the port directly access the buffer (bytevector). */
  785. c_port->write_buf = c_port->write_pos = c_port->write_end = NULL;
  786. c_port->write_buf_size = c_port->read_buf_size = 0;
  787. return port;
  788. }
  789. /* Write SIZE octets from DATA to PORT. */
  790. static void
  791. cbop_write (SCM port, const void *data, size_t size)
  792. #define FUNC_NAME "cbop_write"
  793. {
  794. long int c_result;
  795. size_t c_written;
  796. SCM bv, write_proc, result;
  797. /* XXX: Allocating a new bytevector at each `write' call is inefficient,
  798. but necessary since (1) we don't control the lifetime of the buffer
  799. pointed to by DATA, and (2) the `write!' procedure could capture the
  800. bytevector it is passed. */
  801. bv = scm_c_make_bytevector (size);
  802. memcpy (SCM_BYTEVECTOR_CONTENTS (bv), data, size);
  803. write_proc = SCM_CBOP_WRITE_PROC (port);
  804. /* Since the `write' procedure of Guile's ports has type `void', it must
  805. try hard to write exactly SIZE bytes, regardless of how many bytes the
  806. sink can handle. */
  807. for (c_written = 0;
  808. c_written < size;
  809. c_written += c_result)
  810. {
  811. result = scm_call_3 (write_proc, bv,
  812. scm_from_size_t (c_written),
  813. scm_from_size_t (size - c_written));
  814. c_result = scm_to_long (result);
  815. if (SCM_UNLIKELY (c_result < 0
  816. || (size_t) c_result > (size - c_written)))
  817. scm_wrong_type_arg_msg (FUNC_NAME, 0, result,
  818. "R6RS custom binary output port `write!' "
  819. "returned a incorrect integer");
  820. }
  821. }
  822. #undef FUNC_NAME
  823. SCM_DEFINE (scm_make_custom_binary_output_port,
  824. "make-custom-binary-output-port", 5, 0, 0,
  825. (SCM id, SCM write_proc, SCM get_position_proc,
  826. SCM set_position_proc, SCM close_proc),
  827. "Return a new custom binary output port whose output is drained "
  828. "by invoking @var{write_proc} and passing it a bytevector, an "
  829. "index where octets should be written, and an octet count.")
  830. #define FUNC_NAME s_scm_make_custom_binary_output_port
  831. {
  832. SCM_VALIDATE_STRING (1, id);
  833. SCM_VALIDATE_PROC (2, write_proc);
  834. if (!scm_is_false (get_position_proc))
  835. SCM_VALIDATE_PROC (3, get_position_proc);
  836. if (!scm_is_false (set_position_proc))
  837. SCM_VALIDATE_PROC (4, set_position_proc);
  838. if (!scm_is_false (close_proc))
  839. SCM_VALIDATE_PROC (5, close_proc);
  840. return (make_cbop (write_proc, get_position_proc, set_position_proc,
  841. close_proc));
  842. }
  843. #undef FUNC_NAME
  844. /* Instantiate the custom binary output port type. */
  845. static inline void
  846. initialize_custom_binary_output_ports (void)
  847. {
  848. custom_binary_output_port_type =
  849. scm_make_port_type ("r6rs-custom-binary-output-port",
  850. NULL, cbop_write);
  851. scm_set_port_seek (custom_binary_output_port_type, cbp_seek);
  852. scm_set_port_close (custom_binary_output_port_type, cbp_close);
  853. }
  854. /* Transcoded ports ("tp" for short). */
  855. static scm_t_bits transcoded_port_type = 0;
  856. #define TP_INPUT_BUFFER_SIZE 4096
  857. #define SCM_TP_BINARY_PORT(_port) SCM_PACK (SCM_STREAM (_port))
  858. static inline SCM
  859. make_tp (SCM binary_port, unsigned long mode)
  860. {
  861. SCM port;
  862. scm_t_port *c_port;
  863. const unsigned long mode_bits = SCM_OPN | mode;
  864. port = scm_c_make_port (transcoded_port_type, mode_bits,
  865. SCM_UNPACK (binary_port));
  866. if (SCM_INPUT_PORT_P (port))
  867. {
  868. c_port = SCM_PTAB_ENTRY (port);
  869. c_port->read_buf = scm_gc_malloc_pointerless (TP_INPUT_BUFFER_SIZE,
  870. "port buffer");
  871. c_port->read_pos = c_port->read_end = c_port->read_buf;
  872. c_port->read_buf_size = TP_INPUT_BUFFER_SIZE;
  873. SCM_SET_CELL_WORD_0 (port, SCM_CELL_WORD_0 (port) & ~SCM_BUF0);
  874. }
  875. return port;
  876. }
  877. static void
  878. tp_write (SCM port, const void *data, size_t size)
  879. {
  880. scm_c_write_unlocked (SCM_TP_BINARY_PORT (port), data, size);
  881. }
  882. static int
  883. tp_fill_input (SCM port)
  884. {
  885. size_t count;
  886. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  887. SCM bport = SCM_TP_BINARY_PORT (port);
  888. scm_t_port *c_bport = SCM_PTAB_ENTRY (bport);
  889. /* We can't use `scm_c_read' here, since it blocks until the whole
  890. block has been read or EOF. */
  891. if (c_bport->rw_active == SCM_PORT_WRITE)
  892. scm_force_output (bport);
  893. if (c_bport->read_pos >= c_bport->read_end)
  894. scm_fill_input_unlocked (bport);
  895. count = c_bport->read_end - c_bport->read_pos;
  896. if (count > c_port->read_buf_size)
  897. count = c_port->read_buf_size;
  898. memcpy (c_port->read_buf, c_bport->read_pos, count);
  899. c_bport->read_pos += count;
  900. if (c_bport->rw_random)
  901. c_bport->rw_active = SCM_PORT_READ;
  902. if (count == 0)
  903. return EOF;
  904. else
  905. {
  906. c_port->read_pos = c_port->read_buf;
  907. c_port->read_end = c_port->read_buf + count;
  908. return *c_port->read_buf;
  909. }
  910. }
  911. static void
  912. tp_flush (SCM port)
  913. {
  914. SCM binary_port = SCM_TP_BINARY_PORT (port);
  915. scm_t_port *c_port = SCM_PTAB_ENTRY (port);
  916. size_t count = c_port->write_pos - c_port->write_buf;
  917. /* As the runtime will try to flush all ports upon exit, we test for
  918. the underlying port still being open here. Otherwise, when you
  919. would explicitly close the underlying port and the transcoded port
  920. still had data outstanding, you'd get an exception on Guile exit.
  921. We just throw away the data when the underlying port is closed. */
  922. if (SCM_OPOUTPORTP (binary_port))
  923. scm_c_write_unlocked (binary_port, c_port->write_buf, count);
  924. c_port->write_pos = c_port->write_buf;
  925. c_port->rw_active = SCM_PORT_NEITHER;
  926. if (SCM_OPOUTPORTP (binary_port))
  927. scm_force_output (binary_port);
  928. }
  929. static int
  930. tp_close (SCM port)
  931. {
  932. if (SCM_OUTPUT_PORT_P (port))
  933. tp_flush (port);
  934. return scm_is_true (scm_close_port (SCM_TP_BINARY_PORT (port))) ? 0 : -1;
  935. }
  936. static inline void
  937. initialize_transcoded_ports (void)
  938. {
  939. transcoded_port_type =
  940. scm_make_port_type ("r6rs-transcoded-port", tp_fill_input, tp_write);
  941. scm_set_port_flush (transcoded_port_type, tp_flush);
  942. scm_set_port_close (transcoded_port_type, tp_close);
  943. }
  944. SCM_INTERNAL SCM scm_i_make_transcoded_port (SCM);
  945. SCM_DEFINE (scm_i_make_transcoded_port,
  946. "%make-transcoded-port", 1, 0, 0,
  947. (SCM port),
  948. "Return a new port which reads and writes to @var{port}")
  949. #define FUNC_NAME s_scm_i_make_transcoded_port
  950. {
  951. SCM result;
  952. unsigned long mode = 0;
  953. SCM_VALIDATE_PORT (SCM_ARG1, port);
  954. if (scm_is_true (scm_output_port_p (port)))
  955. mode |= SCM_WRTNG;
  956. else if (scm_is_true (scm_input_port_p (port)))
  957. mode |= SCM_RDNG;
  958. result = make_tp (port, mode);
  959. /* FIXME: We should actually close `port' "in a special way" here,
  960. according to R6RS. As there is no way to do that in Guile without
  961. rendering the underlying port unusable for our purposes as well, we
  962. just leave it open. */
  963. return result;
  964. }
  965. #undef FUNC_NAME
  966. /* Textual I/O */
  967. SCM_DEFINE (scm_get_string_n_x,
  968. "get-string-n!", 4, 0, 0,
  969. (SCM port, SCM str, SCM start, SCM count),
  970. "Read up to @var{count} characters from @var{port} into "
  971. "@var{str}, starting at @var{start}. If no characters "
  972. "can be read before the end of file is encountered, the end "
  973. "of file object is returned. Otherwise, the number of "
  974. "characters read is returned.")
  975. #define FUNC_NAME s_scm_get_string_n_x
  976. {
  977. size_t c_start, c_count, c_len, c_end, j;
  978. scm_t_wchar c;
  979. SCM_VALIDATE_OPINPORT (1, port);
  980. SCM_VALIDATE_STRING (2, str);
  981. c_len = scm_c_string_length (str);
  982. c_start = scm_to_size_t (start);
  983. c_count = scm_to_size_t (count);
  984. c_end = c_start + c_count;
  985. if (SCM_UNLIKELY (c_end > c_len))
  986. scm_out_of_range (FUNC_NAME, count);
  987. for (j = c_start; j < c_end; j++)
  988. {
  989. c = scm_getc_unlocked (port);
  990. if (c == EOF)
  991. {
  992. size_t chars_read = j - c_start;
  993. return chars_read == 0 ? SCM_EOF_VAL : scm_from_size_t (chars_read);
  994. }
  995. scm_c_string_set_x (str, j, SCM_MAKE_CHAR (c));
  996. }
  997. return count;
  998. }
  999. #undef FUNC_NAME
  1000. /* Initialization. */
  1001. void
  1002. scm_register_r6rs_ports (void)
  1003. {
  1004. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  1005. "scm_init_r6rs_ports",
  1006. (scm_t_extension_init_func) scm_init_r6rs_ports,
  1007. NULL);
  1008. }
  1009. void
  1010. scm_init_r6rs_ports (void)
  1011. {
  1012. #include "libguile/r6rs-ports.x"
  1013. initialize_bytevector_input_ports ();
  1014. initialize_custom_binary_input_ports ();
  1015. initialize_bytevector_output_ports ();
  1016. initialize_custom_binary_output_ports ();
  1017. initialize_transcoded_ports ();
  1018. }