ports.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2006 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., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 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. /* Headers. */
  42. #include <stdio.h>
  43. #include <errno.h>
  44. #include "libguile/_scm.h"
  45. #include "libguile/eval.h"
  46. #include "libguile/objects.h"
  47. #include "libguile/smob.h"
  48. #include "libguile/chars.h"
  49. #include "libguile/keywords.h"
  50. #include "libguile/root.h"
  51. #include "libguile/strings.h"
  52. #include "libguile/validate.h"
  53. #include "libguile/ports.h"
  54. #ifdef HAVE_STRING_H
  55. #include <string.h>
  56. #endif
  57. #ifdef HAVE_MALLOC_H
  58. #include <malloc.h>
  59. #endif
  60. #ifdef HAVE_UNISTD_H
  61. #include <unistd.h>
  62. #endif
  63. #ifdef HAVE_SYS_IOCTL_H
  64. #include <sys/ioctl.h>
  65. #endif
  66. #ifdef __MINGW32__
  67. #include <fcntl.h>
  68. #define ftruncate(fd, size) chsize (fd, size)
  69. #endif
  70. /* The port kind table --- a dynamically resized array of port types. */
  71. /* scm_ptobs scm_numptob
  72. * implement a dynamicly resized array of ptob records.
  73. * Indexes into this table are used when generating type
  74. * tags for smobjects (if you know a tag you can get an index and conversely).
  75. */
  76. scm_t_ptob_descriptor *scm_ptobs;
  77. long scm_numptob;
  78. /* GC marker for a port with stream of SCM type. */
  79. SCM
  80. scm_markstream (SCM ptr)
  81. {
  82. int openp;
  83. openp = SCM_CELL_WORD_0 (ptr) & SCM_OPN;
  84. if (openp)
  85. return SCM_PACK (SCM_STREAM (ptr));
  86. else
  87. return SCM_BOOL_F;
  88. }
  89. /*
  90. * We choose to use an interface similar to the smob interface with
  91. * fill_input and write as standard fields, passed to the port
  92. * type constructor, and optional fields set by setters.
  93. */
  94. static void
  95. flush_port_default (SCM port SCM_UNUSED)
  96. {
  97. }
  98. static void
  99. end_input_default (SCM port SCM_UNUSED, int offset SCM_UNUSED)
  100. {
  101. }
  102. scm_t_bits
  103. scm_make_port_type (char *name,
  104. int (*fill_input) (SCM port),
  105. void (*write) (SCM port, const void *data, size_t size))
  106. {
  107. char *tmp;
  108. if (255 <= scm_numptob)
  109. goto ptoberr;
  110. SCM_DEFER_INTS;
  111. SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_ptobs,
  112. (1 + scm_numptob)
  113. * sizeof (scm_t_ptob_descriptor)));
  114. if (tmp)
  115. {
  116. scm_ptobs = (scm_t_ptob_descriptor *) tmp;
  117. scm_ptobs[scm_numptob].name = name;
  118. scm_ptobs[scm_numptob].mark = 0;
  119. scm_ptobs[scm_numptob].free = scm_free0;
  120. scm_ptobs[scm_numptob].print = scm_port_print;
  121. scm_ptobs[scm_numptob].equalp = 0;
  122. scm_ptobs[scm_numptob].close = 0;
  123. scm_ptobs[scm_numptob].write = write;
  124. scm_ptobs[scm_numptob].flush = flush_port_default;
  125. scm_ptobs[scm_numptob].end_input = end_input_default;
  126. scm_ptobs[scm_numptob].fill_input = fill_input;
  127. scm_ptobs[scm_numptob].input_waiting = 0;
  128. scm_ptobs[scm_numptob].seek = 0;
  129. scm_ptobs[scm_numptob].truncate = 0;
  130. scm_numptob++;
  131. }
  132. SCM_ALLOW_INTS;
  133. if (!tmp)
  134. {
  135. ptoberr:
  136. scm_memory_error ("scm_make_port_type");
  137. }
  138. /* Make a class object if Goops is present */
  139. if (scm_port_class)
  140. scm_make_port_classes (scm_numptob - 1, SCM_PTOBNAME (scm_numptob - 1));
  141. return scm_tc7_port + (scm_numptob - 1) * 256;
  142. }
  143. void
  144. scm_set_port_mark (long tc, SCM (*mark) (SCM))
  145. {
  146. scm_ptobs[SCM_TC2PTOBNUM (tc)].mark = mark;
  147. }
  148. void
  149. scm_set_port_free (long tc, size_t (*free) (SCM))
  150. {
  151. scm_ptobs[SCM_TC2PTOBNUM (tc)].free = free;
  152. }
  153. void
  154. scm_set_port_print (long tc, int (*print) (SCM exp, SCM port,
  155. scm_print_state *pstate))
  156. {
  157. scm_ptobs[SCM_TC2PTOBNUM (tc)].print = print;
  158. }
  159. void
  160. scm_set_port_equalp (long tc, SCM (*equalp) (SCM, SCM))
  161. {
  162. scm_ptobs[SCM_TC2PTOBNUM (tc)].equalp = equalp;
  163. }
  164. void
  165. scm_set_port_flush (long tc, void (*flush) (SCM port))
  166. {
  167. scm_ptobs[SCM_TC2PTOBNUM (tc)].flush = flush;
  168. }
  169. void
  170. scm_set_port_end_input (long tc, void (*end_input) (SCM port, int offset))
  171. {
  172. scm_ptobs[SCM_TC2PTOBNUM (tc)].end_input = end_input;
  173. }
  174. void
  175. scm_set_port_close (long tc, int (*close) (SCM))
  176. {
  177. scm_ptobs[SCM_TC2PTOBNUM (tc)].close = close;
  178. }
  179. void
  180. scm_set_port_seek (long tc, off_t (*seek) (SCM port,
  181. off_t OFFSET,
  182. int WHENCE))
  183. {
  184. scm_ptobs[SCM_TC2PTOBNUM (tc)].seek = seek;
  185. }
  186. void
  187. scm_set_port_truncate (long tc, void (*truncate) (SCM port, off_t length))
  188. {
  189. scm_ptobs[SCM_TC2PTOBNUM (tc)].truncate = truncate;
  190. }
  191. void
  192. scm_set_port_input_waiting (long tc, int (*input_waiting) (SCM))
  193. {
  194. scm_ptobs[SCM_TC2PTOBNUM (tc)].input_waiting = input_waiting;
  195. }
  196. SCM_DEFINE (scm_char_ready_p, "char-ready?", 0, 1, 0,
  197. (SCM port),
  198. "Return @code{#t} if a character is ready on input @var{port}\n"
  199. "and return @code{#f} otherwise. If @code{char-ready?} returns\n"
  200. "@code{#t} then the next @code{read-char} operation on\n"
  201. "@var{port} is guaranteed not to hang. If @var{port} is a file\n"
  202. "port at end of file then @code{char-ready?} returns @code{#t}.\n"
  203. "@footnote{@code{char-ready?} exists to make it possible for a\n"
  204. "program to accept characters from interactive ports without\n"
  205. "getting stuck waiting for input. Any input editors associated\n"
  206. "with such ports must make sure that characters whose existence\n"
  207. "has been asserted by @code{char-ready?} cannot be rubbed out.\n"
  208. "If @code{char-ready?} were to return @code{#f} at end of file,\n"
  209. "a port at end of file would be indistinguishable from an\n"
  210. "interactive port that has no ready characters.}")
  211. #define FUNC_NAME s_scm_char_ready_p
  212. {
  213. scm_t_port *pt;
  214. if (SCM_UNBNDP (port))
  215. port = scm_cur_inp;
  216. else
  217. SCM_VALIDATE_OPINPORT (1,port);
  218. pt = SCM_PTAB_ENTRY (port);
  219. /* if the current read buffer is filled, or the
  220. last pushed-back char has been read and the saved buffer is
  221. filled, result is true. */
  222. if (pt->read_pos < pt->read_end
  223. || (pt->read_buf == pt->putback_buf
  224. && pt->saved_read_pos < pt->saved_read_end))
  225. return SCM_BOOL_T;
  226. else
  227. {
  228. scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
  229. if (ptob->input_waiting)
  230. return SCM_BOOL(ptob->input_waiting (port));
  231. else
  232. return SCM_BOOL_T;
  233. }
  234. }
  235. #undef FUNC_NAME
  236. /* move up to read_len chars from port's putback and/or read buffers
  237. into memory starting at dest. returns the number of chars moved. */
  238. size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len)
  239. {
  240. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  241. size_t chars_read = 0;
  242. size_t from_buf = min (pt->read_end - pt->read_pos, read_len);
  243. if (from_buf > 0)
  244. {
  245. memcpy (dest, pt->read_pos, from_buf);
  246. pt->read_pos += from_buf;
  247. chars_read += from_buf;
  248. read_len -= from_buf;
  249. dest += from_buf;
  250. }
  251. /* if putback was active, try the real input buffer too. */
  252. if (pt->read_buf == pt->putback_buf)
  253. {
  254. from_buf = min (pt->saved_read_end - pt->saved_read_pos, read_len);
  255. if (from_buf > 0)
  256. {
  257. memcpy (dest, pt->saved_read_pos, from_buf);
  258. pt->saved_read_pos += from_buf;
  259. chars_read += from_buf;
  260. }
  261. }
  262. return chars_read;
  263. }
  264. /* Clear a port's read buffers, returning the contents. */
  265. SCM_DEFINE (scm_drain_input, "drain-input", 1, 0, 0,
  266. (SCM port),
  267. "This procedure clears a port's input buffers, similar\n"
  268. "to the way that force-output clears the output buffer. The\n"
  269. "contents of the buffers are returned as a single string, e.g.,\n"
  270. "\n"
  271. "@lisp\n"
  272. "(define p (open-input-file ...))\n"
  273. "(drain-input p) => empty string, nothing buffered yet.\n"
  274. "(unread-char (read-char p) p)\n"
  275. "(drain-input p) => initial chars from p, up to the buffer size.\n"
  276. "@end lisp\n\n"
  277. "Draining the buffers may be useful for cleanly finishing\n"
  278. "buffered I/O so that the file descriptor can be used directly\n"
  279. "for further input.")
  280. #define FUNC_NAME s_scm_drain_input
  281. {
  282. SCM result;
  283. scm_t_port *pt;
  284. long count;
  285. SCM_VALIDATE_OPINPORT (1,port);
  286. pt = SCM_PTAB_ENTRY (port);
  287. count = pt->read_end - pt->read_pos;
  288. if (pt->read_buf == pt->putback_buf)
  289. count += pt->saved_read_end - pt->saved_read_pos;
  290. result = scm_allocate_string (count);
  291. scm_take_from_input_buffers (port, SCM_STRING_CHARS (result), count);
  292. return result;
  293. }
  294. #undef FUNC_NAME
  295. /* Standard ports --- current input, output, error, and more(!). */
  296. SCM_DEFINE (scm_current_input_port, "current-input-port", 0, 0, 0,
  297. (),
  298. "Return the current input port. This is the default port used\n"
  299. "by many input procedures. Initially, @code{current-input-port}\n"
  300. "returns the @dfn{standard input} in Unix and C terminology.")
  301. #define FUNC_NAME s_scm_current_input_port
  302. {
  303. return scm_cur_inp;
  304. }
  305. #undef FUNC_NAME
  306. SCM_DEFINE (scm_current_output_port, "current-output-port", 0, 0, 0,
  307. (),
  308. "Return the current output port. This is the default port used\n"
  309. "by many output procedures. Initially,\n"
  310. "@code{current-output-port} returns the @dfn{standard output} in\n"
  311. "Unix and C terminology.")
  312. #define FUNC_NAME s_scm_current_output_port
  313. {
  314. return scm_cur_outp;
  315. }
  316. #undef FUNC_NAME
  317. SCM_DEFINE (scm_current_error_port, "current-error-port", 0, 0, 0,
  318. (),
  319. "Return the port to which errors and warnings should be sent (the\n"
  320. "@dfn{standard error} in Unix and C terminology).")
  321. #define FUNC_NAME s_scm_current_error_port
  322. {
  323. return scm_cur_errp;
  324. }
  325. #undef FUNC_NAME
  326. SCM_DEFINE (scm_current_load_port, "current-load-port", 0, 0, 0,
  327. (),
  328. "Return the current-load-port.\n"
  329. "The load port is used internally by @code{primitive-load}.")
  330. #define FUNC_NAME s_scm_current_load_port
  331. {
  332. return scm_cur_loadp;
  333. }
  334. #undef FUNC_NAME
  335. SCM_DEFINE (scm_set_current_input_port, "set-current-input-port", 1, 0, 0,
  336. (SCM port),
  337. "@deffnx {Scheme Procedure} set-current-output-port port\n"
  338. "@deffnx {Scheme Procedure} set-current-error-port port\n"
  339. "Change the ports returned by @code{current-input-port},\n"
  340. "@code{current-output-port} and @code{current-error-port}, respectively,\n"
  341. "so that they use the supplied @var{port} for input or output.")
  342. #define FUNC_NAME s_scm_set_current_input_port
  343. {
  344. SCM oinp = scm_cur_inp;
  345. SCM_VALIDATE_OPINPORT (1,port);
  346. scm_cur_inp = port;
  347. return oinp;
  348. }
  349. #undef FUNC_NAME
  350. SCM_DEFINE (scm_set_current_output_port, "set-current-output-port", 1, 0, 0,
  351. (SCM port),
  352. "Set the current default output port to @var{port}.")
  353. #define FUNC_NAME s_scm_set_current_output_port
  354. {
  355. SCM ooutp = scm_cur_outp;
  356. port = SCM_COERCE_OUTPORT (port);
  357. SCM_VALIDATE_OPOUTPORT (1,port);
  358. scm_cur_outp = port;
  359. return ooutp;
  360. }
  361. #undef FUNC_NAME
  362. SCM_DEFINE (scm_set_current_error_port, "set-current-error-port", 1, 0, 0,
  363. (SCM port),
  364. "Set the current default error port to @var{port}.")
  365. #define FUNC_NAME s_scm_set_current_error_port
  366. {
  367. SCM oerrp = scm_cur_errp;
  368. port = SCM_COERCE_OUTPORT (port);
  369. SCM_VALIDATE_OPOUTPORT (1,port);
  370. scm_cur_errp = port;
  371. return oerrp;
  372. }
  373. #undef FUNC_NAME
  374. /* The port table --- an array of pointers to ports. */
  375. scm_t_port **scm_port_table;
  376. long scm_port_table_size = 0; /* Number of ports in scm_port_table. */
  377. long scm_port_table_room = 20; /* Size of the array. */
  378. /* Add a port to the table. */
  379. scm_t_port *
  380. scm_add_to_port_table (SCM port)
  381. #define FUNC_NAME "scm_add_to_port_table"
  382. {
  383. scm_t_port *entry;
  384. if (scm_port_table_size == scm_port_table_room)
  385. {
  386. /* initial malloc is in gc.c. this doesn't use scm_must_malloc etc.,
  387. since it can never be freed during gc. */
  388. void *newt = realloc ((char *) scm_port_table,
  389. (size_t) (sizeof (scm_t_port *)
  390. * scm_port_table_room * 2));
  391. if (newt == NULL)
  392. scm_memory_error ("scm_add_to_port_table");
  393. scm_port_table = (scm_t_port **) newt;
  394. scm_port_table_room *= 2;
  395. }
  396. entry = (scm_t_port *) scm_must_malloc (sizeof (scm_t_port), FUNC_NAME);
  397. entry->port = port;
  398. entry->entry = scm_port_table_size;
  399. entry->revealed = 0;
  400. entry->stream = 0;
  401. entry->file_name = SCM_BOOL_F;
  402. entry->line_number = 0;
  403. entry->column_number = 0;
  404. entry->putback_buf = 0;
  405. entry->putback_buf_size = 0;
  406. entry->rw_active = SCM_PORT_NEITHER;
  407. entry->rw_random = 0;
  408. scm_port_table[scm_port_table_size] = entry;
  409. scm_port_table_size++;
  410. return entry;
  411. }
  412. #undef FUNC_NAME
  413. /* Remove a port from the table and destroy it. */
  414. void
  415. scm_remove_from_port_table (SCM port)
  416. #define FUNC_NAME "scm_remove_from_port_table"
  417. {
  418. scm_t_port *p = SCM_PTAB_ENTRY (port);
  419. long i = p->entry;
  420. if (i >= scm_port_table_size)
  421. SCM_MISC_ERROR ("Port not in table: ~S", scm_list_1 (port));
  422. if (p->putback_buf)
  423. scm_must_free (p->putback_buf);
  424. scm_must_free (p);
  425. scm_done_free (sizeof (*p)); /* matching scm_must_malloc above */
  426. /* Since we have just freed slot i we can shrink the table by moving
  427. the last entry to that slot... */
  428. if (i < scm_port_table_size - 1)
  429. {
  430. scm_port_table[i] = scm_port_table[scm_port_table_size - 1];
  431. scm_port_table[i]->entry = i;
  432. }
  433. SCM_SETPTAB_ENTRY (port, 0);
  434. scm_port_table_size--;
  435. }
  436. #undef FUNC_NAME
  437. #ifdef GUILE_DEBUG
  438. /* Functions for debugging. */
  439. SCM_DEFINE (scm_pt_size, "pt-size", 0, 0, 0,
  440. (),
  441. "Return the number of ports in the port table. @code{pt-size}\n"
  442. "is only included in @code{--enable-guile-debug} builds.")
  443. #define FUNC_NAME s_scm_pt_size
  444. {
  445. return SCM_MAKINUM (scm_port_table_size);
  446. }
  447. #undef FUNC_NAME
  448. SCM_DEFINE (scm_pt_member, "pt-member", 1, 0, 0,
  449. (SCM index),
  450. "Return the port at @var{index} in the port table.\n"
  451. "@code{pt-member} is only included in\n"
  452. "@code{--enable-guile-debug} builds.")
  453. #define FUNC_NAME s_scm_pt_member
  454. {
  455. long i;
  456. SCM_VALIDATE_INUM_COPY (1,index,i);
  457. if (i < 0 || i >= scm_port_table_size)
  458. return SCM_BOOL_F;
  459. else
  460. return scm_port_table[i]->port;
  461. }
  462. #undef FUNC_NAME
  463. #endif
  464. void
  465. scm_port_non_buffer (scm_t_port *pt)
  466. {
  467. pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
  468. pt->write_buf = pt->write_pos = &pt->shortbuf;
  469. pt->read_buf_size = pt->write_buf_size = 1;
  470. pt->write_end = pt->write_buf + pt->write_buf_size;
  471. }
  472. /* Revealed counts --- an oddity inherited from SCSH. */
  473. /* Find a port in the table and return its revealed count.
  474. Also used by the garbage collector.
  475. */
  476. int
  477. scm_revealed_count (SCM port)
  478. {
  479. return SCM_REVEALED(port);
  480. }
  481. /* Return the revealed count for a port. */
  482. SCM_DEFINE (scm_port_revealed, "port-revealed", 1, 0, 0,
  483. (SCM port),
  484. "Return the revealed count for @var{port}.")
  485. #define FUNC_NAME s_scm_port_revealed
  486. {
  487. port = SCM_COERCE_OUTPORT (port);
  488. SCM_VALIDATE_OPENPORT (1,port);
  489. return SCM_MAKINUM (scm_revealed_count (port));
  490. }
  491. #undef FUNC_NAME
  492. /* Set the revealed count for a port. */
  493. SCM_DEFINE (scm_set_port_revealed_x, "set-port-revealed!", 2, 0, 0,
  494. (SCM port, SCM rcount),
  495. "Sets the revealed count for a port to a given value.\n"
  496. "The return value is unspecified.")
  497. #define FUNC_NAME s_scm_set_port_revealed_x
  498. {
  499. port = SCM_COERCE_OUTPORT (port);
  500. SCM_VALIDATE_OPENPORT (1,port);
  501. SCM_VALIDATE_INUM (2,rcount);
  502. SCM_REVEALED (port) = SCM_INUM (rcount);
  503. return SCM_UNSPECIFIED;
  504. }
  505. #undef FUNC_NAME
  506. /* Retrieving a port's mode. */
  507. /* Return the flags that characterize a port based on the mode
  508. * string used to open a file for that port.
  509. *
  510. * See PORT FLAGS in scm.h
  511. */
  512. long
  513. scm_mode_bits (char *modes)
  514. {
  515. return (SCM_OPN
  516. | (strchr (modes, 'r') || strchr (modes, '+') ? SCM_RDNG : 0)
  517. | ( strchr (modes, 'w')
  518. || strchr (modes, 'a')
  519. || strchr (modes, '+') ? SCM_WRTNG : 0)
  520. | (strchr (modes, '0') ? SCM_BUF0 : 0)
  521. | (strchr (modes, 'l') ? SCM_BUFLINE : 0));
  522. }
  523. /* Return the mode flags from an open port.
  524. * Some modes such as "append" are only used when opening
  525. * a file and are not returned here. */
  526. SCM_DEFINE (scm_port_mode, "port-mode", 1, 0, 0,
  527. (SCM port),
  528. "Return the port modes associated with the open port @var{port}.\n"
  529. "These will not necessarily be identical to the modes used when\n"
  530. "the port was opened, since modes such as \"append\" which are\n"
  531. "used only during port creation are not retained.")
  532. #define FUNC_NAME s_scm_port_mode
  533. {
  534. char modes[4];
  535. modes[0] = '\0';
  536. port = SCM_COERCE_OUTPORT (port);
  537. SCM_VALIDATE_OPPORT (1,port);
  538. if (SCM_CELL_WORD_0 (port) & SCM_RDNG) {
  539. if (SCM_CELL_WORD_0 (port) & SCM_WRTNG)
  540. strcpy (modes, "r+");
  541. else
  542. strcpy (modes, "r");
  543. }
  544. else if (SCM_CELL_WORD_0 (port) & SCM_WRTNG)
  545. strcpy (modes, "w");
  546. if (SCM_CELL_WORD_0 (port) & SCM_BUF0)
  547. strcat (modes, "0");
  548. return scm_mem2string (modes, strlen (modes));
  549. }
  550. #undef FUNC_NAME
  551. /* Closing ports. */
  552. /* scm_close_port
  553. * Call the close operation on a port object.
  554. * see also scm_close.
  555. */
  556. SCM_DEFINE (scm_close_port, "close-port", 1, 0, 0,
  557. (SCM port),
  558. "Close the specified port object. Return @code{#t} if it\n"
  559. "successfully closes a port or @code{#f} if it was already\n"
  560. "closed. An exception may be raised if an error occurs, for\n"
  561. "example when flushing buffered output. See also @ref{Ports and\n"
  562. "File Descriptors, close}, for a procedure which can close file\n"
  563. "descriptors.")
  564. #define FUNC_NAME s_scm_close_port
  565. {
  566. size_t i;
  567. int rv;
  568. port = SCM_COERCE_OUTPORT (port);
  569. SCM_VALIDATE_PORT (1, port);
  570. if (SCM_CLOSEDP (port))
  571. return SCM_BOOL_F;
  572. i = SCM_PTOBNUM (port);
  573. if (scm_ptobs[i].close)
  574. rv = (scm_ptobs[i].close) (port);
  575. else
  576. rv = 0;
  577. scm_remove_from_port_table (port);
  578. SCM_CLR_PORT_OPEN_FLAG (port);
  579. return SCM_BOOL (rv >= 0);
  580. }
  581. #undef FUNC_NAME
  582. SCM_DEFINE (scm_close_input_port, "close-input-port", 1, 0, 0,
  583. (SCM port),
  584. "Close the specified input port object. The routine has no effect if\n"
  585. "the file has already been closed. An exception may be raised if an\n"
  586. "error occurs. The value returned is unspecified.\n\n"
  587. "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
  588. "which can close file descriptors.")
  589. #define FUNC_NAME s_scm_close_input_port
  590. {
  591. SCM_VALIDATE_INPUT_PORT (1, port);
  592. scm_close_port (port);
  593. return SCM_UNSPECIFIED;
  594. }
  595. #undef FUNC_NAME
  596. SCM_DEFINE (scm_close_output_port, "close-output-port", 1, 0, 0,
  597. (SCM port),
  598. "Close the specified output port object. The routine has no effect if\n"
  599. "the file has already been closed. An exception may be raised if an\n"
  600. "error occurs. The value returned is unspecified.\n\n"
  601. "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
  602. "which can close file descriptors.")
  603. #define FUNC_NAME s_scm_close_output_port
  604. {
  605. port = SCM_COERCE_OUTPORT (port);
  606. SCM_VALIDATE_OUTPUT_PORT (1, port);
  607. scm_close_port (port);
  608. return SCM_UNSPECIFIED;
  609. }
  610. #undef FUNC_NAME
  611. SCM_DEFINE (scm_port_for_each, "port-for-each", 1, 0, 0,
  612. (SCM proc),
  613. "Apply @var{proc} to each port in the Guile port table\n"
  614. "in turn. The return value is unspecified. More specifically,\n"
  615. "@var{proc} is applied exactly once to every port that exists\n"
  616. "in the system at the time @var{port-for-each} is invoked.\n"
  617. "Changes to the port table while @var{port-for-each} is running\n"
  618. "have no effect as far as @var{port-for-each} is concerned.")
  619. #define FUNC_NAME s_scm_port_for_each
  620. {
  621. long i;
  622. SCM ports;
  623. SCM_VALIDATE_PROC (1, proc);
  624. /* when pre-emptive multithreading is supported, access to the port
  625. table will need to be controlled by a mutex. */
  626. /* Even without pre-emptive multithreading, running arbitrary code
  627. while scanning the port table is unsafe because the port table
  628. can change arbitrarily (from a GC, for example). So we build a
  629. list in advance while blocking the GC. -mvo */
  630. SCM_DEFER_INTS;
  631. scm_block_gc++;
  632. ports = SCM_EOL;
  633. for (i = 0; i < scm_port_table_size; i++)
  634. ports = scm_cons (scm_port_table[i]->port, ports);
  635. scm_block_gc--;
  636. SCM_ALLOW_INTS;
  637. while (ports != SCM_EOL)
  638. {
  639. scm_call_1 (proc, SCM_CAR (ports));
  640. ports = SCM_CDR (ports);
  641. }
  642. return SCM_UNSPECIFIED;
  643. }
  644. #undef FUNC_NAME
  645. #if (SCM_DEBUG_DEPRECATED == 0)
  646. SCM_DEFINE (scm_close_all_ports_except, "close-all-ports-except", 0, 0, 1,
  647. (SCM ports),
  648. "[DEPRECATED] Close all open file ports used by the interpreter\n"
  649. "except for those supplied as arguments. This procedure\n"
  650. "was intended to be used before an exec call to close file descriptors\n"
  651. "which are not needed in the new process. However it has the\n"
  652. "undesirable side effect of flushing buffers, so it's deprecated.\n"
  653. "Use port-for-each instead.")
  654. #define FUNC_NAME s_scm_close_all_ports_except
  655. {
  656. long i = 0;
  657. SCM_VALIDATE_REST_ARGUMENT (ports);
  658. while (i < scm_port_table_size)
  659. {
  660. SCM thisport = scm_port_table[i]->port;
  661. int found = 0;
  662. SCM ports_ptr = ports;
  663. while (!SCM_NULLP (ports_ptr))
  664. {
  665. SCM port = SCM_COERCE_OUTPORT (SCM_CAR (ports_ptr));
  666. if (i == 0)
  667. SCM_VALIDATE_OPPORT (SCM_ARG1,port);
  668. if (SCM_EQ_P (port, thisport))
  669. found = 1;
  670. ports_ptr = SCM_CDR (ports_ptr);
  671. }
  672. if (found)
  673. i++;
  674. else
  675. /* i is not to be incremented here. */
  676. scm_close_port (thisport);
  677. }
  678. return SCM_UNSPECIFIED;
  679. }
  680. #undef FUNC_NAME
  681. #endif
  682. /* Utter miscellany. Gosh, we should clean this up some time. */
  683. SCM_DEFINE (scm_input_port_p, "input-port?", 1, 0, 0,
  684. (SCM x),
  685. "Return @code{#t} if @var{x} is an input port, otherwise return\n"
  686. "@code{#f}. Any object satisfying this predicate also satisfies\n"
  687. "@code{port?}.")
  688. #define FUNC_NAME s_scm_input_port_p
  689. {
  690. return SCM_BOOL (SCM_INPUT_PORT_P (x));
  691. }
  692. #undef FUNC_NAME
  693. SCM_DEFINE (scm_output_port_p, "output-port?", 1, 0, 0,
  694. (SCM x),
  695. "Return @code{#t} if @var{x} is an output port, otherwise return\n"
  696. "@code{#f}. Any object satisfying this predicate also satisfies\n"
  697. "@code{port?}.")
  698. #define FUNC_NAME s_scm_output_port_p
  699. {
  700. x = SCM_COERCE_OUTPORT (x);
  701. return SCM_BOOL (SCM_OUTPUT_PORT_P (x));
  702. }
  703. #undef FUNC_NAME
  704. SCM_DEFINE (scm_port_p, "port?", 1, 0, 0,
  705. (SCM x),
  706. "Return a boolean indicating whether @var{x} is a port.\n"
  707. "Equivalent to @code{(or (input-port? @var{x}) (output-port?\n"
  708. "@var{x}))}.")
  709. #define FUNC_NAME s_scm_port_p
  710. {
  711. return SCM_BOOL (SCM_PORTP (x));
  712. }
  713. #undef FUNC_NAME
  714. SCM_DEFINE (scm_port_closed_p, "port-closed?", 1, 0, 0,
  715. (SCM port),
  716. "Return @code{#t} if @var{port} is closed or @code{#f} if it is\n"
  717. "open.")
  718. #define FUNC_NAME s_scm_port_closed_p
  719. {
  720. SCM_VALIDATE_PORT (1,port);
  721. return SCM_BOOL (!SCM_OPPORTP (port));
  722. }
  723. #undef FUNC_NAME
  724. SCM_DEFINE (scm_eof_object_p, "eof-object?", 1, 0, 0,
  725. (SCM x),
  726. "Return @code{#t} if @var{x} is an end-of-file object; otherwise\n"
  727. "return @code{#f}.")
  728. #define FUNC_NAME s_scm_eof_object_p
  729. {
  730. return SCM_BOOL(SCM_EOF_OBJECT_P (x));
  731. }
  732. #undef FUNC_NAME
  733. SCM_DEFINE (scm_force_output, "force-output", 0, 1, 0,
  734. (SCM port),
  735. "Flush the specified output port, or the current output port if @var{port}\n"
  736. "is omitted. The current output buffer contents are passed to the\n"
  737. "underlying port implementation (e.g., in the case of fports, the\n"
  738. "data will be written to the file and the output buffer will be cleared.)\n"
  739. "It has no effect on an unbuffered port.\n\n"
  740. "The return value is unspecified.")
  741. #define FUNC_NAME s_scm_force_output
  742. {
  743. if (SCM_UNBNDP (port))
  744. port = scm_cur_outp;
  745. else
  746. {
  747. port = SCM_COERCE_OUTPORT (port);
  748. SCM_VALIDATE_OPOUTPORT (1,port);
  749. }
  750. scm_flush (port);
  751. return SCM_UNSPECIFIED;
  752. }
  753. #undef FUNC_NAME
  754. SCM_DEFINE (scm_flush_all_ports, "flush-all-ports", 0, 0, 0,
  755. (),
  756. "Equivalent to calling @code{force-output} on\n"
  757. "all open output ports. The return value is unspecified.")
  758. #define FUNC_NAME s_scm_flush_all_ports
  759. {
  760. size_t i;
  761. for (i = 0; i < scm_port_table_size; i++)
  762. {
  763. if (SCM_OPOUTPORTP (scm_port_table[i]->port))
  764. scm_flush (scm_port_table[i]->port);
  765. }
  766. return SCM_UNSPECIFIED;
  767. }
  768. #undef FUNC_NAME
  769. SCM_DEFINE (scm_read_char, "read-char", 0, 1, 0,
  770. (SCM port),
  771. "Return the next character available from @var{port}, updating\n"
  772. "@var{port} to point to the following character. If no more\n"
  773. "characters are available, the end-of-file object is returned.")
  774. #define FUNC_NAME s_scm_read_char
  775. {
  776. int c;
  777. if (SCM_UNBNDP (port))
  778. port = scm_cur_inp;
  779. SCM_VALIDATE_OPINPORT (1,port);
  780. c = scm_getc (port);
  781. if (EOF == c)
  782. return SCM_EOF_VAL;
  783. return SCM_MAKE_CHAR (c);
  784. }
  785. #undef FUNC_NAME
  786. /* this should only be called when the read buffer is empty. it
  787. tries to refill the read buffer. it returns the first char from
  788. the port, which is either EOF or *(pt->read_pos). */
  789. int
  790. scm_fill_input (SCM port)
  791. {
  792. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  793. if (pt->read_buf == pt->putback_buf)
  794. {
  795. /* finished reading put-back chars. */
  796. pt->read_buf = pt->saved_read_buf;
  797. pt->read_pos = pt->saved_read_pos;
  798. pt->read_end = pt->saved_read_end;
  799. pt->read_buf_size = pt->saved_read_buf_size;
  800. if (pt->read_pos < pt->read_end)
  801. return *(pt->read_pos);
  802. }
  803. return scm_ptobs[SCM_PTOBNUM (port)].fill_input (port);
  804. }
  805. int
  806. scm_getc (SCM port)
  807. {
  808. int c;
  809. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  810. if (pt->rw_active == SCM_PORT_WRITE)
  811. {
  812. /* may be marginally faster than calling scm_flush. */
  813. scm_ptobs[SCM_PTOBNUM (port)].flush (port);
  814. }
  815. if (pt->rw_random)
  816. pt->rw_active = SCM_PORT_READ;
  817. if (pt->read_pos >= pt->read_end)
  818. {
  819. if (scm_fill_input (port) == EOF)
  820. return EOF;
  821. }
  822. c = *(pt->read_pos++);
  823. switch (c)
  824. {
  825. case '\a':
  826. break;
  827. case '\b':
  828. SCM_DECCOL (port);
  829. break;
  830. case '\n':
  831. SCM_INCLINE (port);
  832. break;
  833. case '\r':
  834. SCM_ZEROCOL (port);
  835. break;
  836. case '\t':
  837. SCM_TABCOL (port);
  838. break;
  839. default:
  840. SCM_INCCOL (port);
  841. break;
  842. }
  843. return c;
  844. }
  845. void
  846. scm_putc (char c, SCM port)
  847. {
  848. scm_lfwrite (&c, 1, port);
  849. }
  850. void
  851. scm_puts (const char *s, SCM port)
  852. {
  853. scm_lfwrite (s, strlen (s), port);
  854. }
  855. /* scm_lfwrite
  856. *
  857. * This function differs from scm_c_write; it updates port line and
  858. * column. */
  859. void
  860. scm_lfwrite (const char *ptr, size_t size, SCM port)
  861. {
  862. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  863. scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
  864. if (pt->rw_active == SCM_PORT_READ)
  865. scm_end_input (port);
  866. ptob->write (port, ptr, size);
  867. for (; size; ptr++, size--) {
  868. if (*ptr == '\a') {
  869. }
  870. else if (*ptr == '\b') {
  871. SCM_DECCOL(port);
  872. }
  873. else if (*ptr == '\n') {
  874. SCM_INCLINE(port);
  875. }
  876. else if (*ptr == '\r') {
  877. SCM_ZEROCOL(port);
  878. }
  879. else if (*ptr == '\t') {
  880. SCM_TABCOL(port);
  881. }
  882. else {
  883. SCM_INCCOL(port);
  884. }
  885. }
  886. if (pt->rw_random)
  887. pt->rw_active = SCM_PORT_WRITE;
  888. }
  889. /* scm_c_read
  890. *
  891. * Used by an application to read arbitrary number of bytes from an
  892. * SCM port. Same semantics as libc read, except that scm_c_read only
  893. * returns less than SIZE bytes if at end-of-file.
  894. *
  895. * Warning: Doesn't update port line and column counts! */
  896. size_t
  897. scm_c_read (SCM port, void *buffer, size_t size)
  898. {
  899. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  900. size_t n_read = 0, n_available;
  901. if (pt->rw_active == SCM_PORT_WRITE)
  902. scm_ptobs[SCM_PTOBNUM (port)].flush (port);
  903. if (pt->rw_random)
  904. pt->rw_active = SCM_PORT_READ;
  905. if (SCM_READ_BUFFER_EMPTY_P (pt))
  906. {
  907. if (scm_fill_input (port) == EOF)
  908. return 0;
  909. }
  910. n_available = pt->read_end - pt->read_pos;
  911. while (n_available < size)
  912. {
  913. memcpy (buffer, pt->read_pos, n_available);
  914. buffer = (char *) buffer + n_available;
  915. pt->read_pos += n_available;
  916. n_read += n_available;
  917. if (SCM_READ_BUFFER_EMPTY_P (pt))
  918. {
  919. if (scm_fill_input (port) == EOF)
  920. return n_read;
  921. }
  922. size -= n_available;
  923. n_available = pt->read_end - pt->read_pos;
  924. }
  925. memcpy (buffer, pt->read_pos, size);
  926. pt->read_pos += size;
  927. return n_read + size;
  928. }
  929. /* scm_c_write
  930. *
  931. * Used by an application to write arbitrary number of bytes to an SCM
  932. * port. Similar semantics as libc write. However, unlike libc
  933. * write, scm_c_write writes the requested number of bytes and has no
  934. * return value.
  935. *
  936. * Warning: Doesn't update port line and column counts!
  937. */
  938. void
  939. scm_c_write (SCM port, const void *ptr, size_t size)
  940. {
  941. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  942. scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
  943. if (pt->rw_active == SCM_PORT_READ)
  944. scm_end_input (port);
  945. ptob->write (port, ptr, size);
  946. if (pt->rw_random)
  947. pt->rw_active = SCM_PORT_WRITE;
  948. }
  949. void
  950. scm_flush (SCM port)
  951. {
  952. long i = SCM_PTOBNUM (port);
  953. (scm_ptobs[i].flush) (port);
  954. }
  955. void
  956. scm_end_input (SCM port)
  957. {
  958. long offset;
  959. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  960. if (pt->read_buf == pt->putback_buf)
  961. {
  962. offset = pt->read_end - pt->read_pos;
  963. pt->read_buf = pt->saved_read_buf;
  964. pt->read_pos = pt->saved_read_pos;
  965. pt->read_end = pt->saved_read_end;
  966. pt->read_buf_size = pt->saved_read_buf_size;
  967. }
  968. else
  969. offset = 0;
  970. scm_ptobs[SCM_PTOBNUM (port)].end_input (port, offset);
  971. }
  972. void
  973. scm_ungetc (int c, SCM port)
  974. #define FUNC_NAME "scm_ungetc"
  975. {
  976. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  977. if (pt->read_buf == pt->putback_buf)
  978. /* already using the put-back buffer. */
  979. {
  980. /* enlarge putback_buf if necessary. */
  981. if (pt->read_end == pt->read_buf + pt->read_buf_size
  982. && pt->read_buf == pt->read_pos)
  983. {
  984. size_t new_size = pt->read_buf_size * 2;
  985. unsigned char *tmp = (unsigned char *)
  986. scm_must_realloc (pt->putback_buf, pt->read_buf_size, new_size,
  987. FUNC_NAME);
  988. pt->read_pos = pt->read_buf = pt->putback_buf = tmp;
  989. pt->read_end = pt->read_buf + pt->read_buf_size;
  990. pt->read_buf_size = pt->putback_buf_size = new_size;
  991. }
  992. /* shift any existing bytes to buffer + 1. */
  993. if (pt->read_pos == pt->read_end)
  994. pt->read_end = pt->read_buf + 1;
  995. else if (pt->read_pos != pt->read_buf + 1)
  996. {
  997. int count = pt->read_end - pt->read_pos;
  998. memmove (pt->read_buf + 1, pt->read_pos, count);
  999. pt->read_end = pt->read_buf + 1 + count;
  1000. }
  1001. pt->read_pos = pt->read_buf;
  1002. }
  1003. else
  1004. /* switch to the put-back buffer. */
  1005. {
  1006. if (pt->putback_buf == NULL)
  1007. {
  1008. pt->putback_buf
  1009. = (unsigned char *) scm_must_malloc (SCM_INITIAL_PUTBACK_BUF_SIZE,
  1010. FUNC_NAME);
  1011. pt->putback_buf_size = SCM_INITIAL_PUTBACK_BUF_SIZE;
  1012. }
  1013. pt->saved_read_buf = pt->read_buf;
  1014. pt->saved_read_pos = pt->read_pos;
  1015. pt->saved_read_end = pt->read_end;
  1016. pt->saved_read_buf_size = pt->read_buf_size;
  1017. pt->read_pos = pt->read_buf = pt->putback_buf;
  1018. pt->read_end = pt->read_buf + 1;
  1019. pt->read_buf_size = pt->putback_buf_size;
  1020. }
  1021. *pt->read_buf = c;
  1022. if (pt->rw_random)
  1023. pt->rw_active = SCM_PORT_READ;
  1024. if (c == '\n')
  1025. {
  1026. /* What should col be in this case?
  1027. * We'll leave it at -1.
  1028. */
  1029. SCM_LINUM (port) -= 1;
  1030. }
  1031. else
  1032. SCM_COL(port) -= 1;
  1033. }
  1034. #undef FUNC_NAME
  1035. void
  1036. scm_ungets (const char *s, int n, SCM port)
  1037. {
  1038. /* This is simple minded and inefficient, but unreading strings is
  1039. * probably not a common operation, and remember that line and
  1040. * column numbers have to be handled...
  1041. *
  1042. * Please feel free to write an optimized version!
  1043. */
  1044. while (n--)
  1045. scm_ungetc (s[n], port);
  1046. }
  1047. SCM_DEFINE (scm_peek_char, "peek-char", 0, 1, 0,
  1048. (SCM port),
  1049. "Return the next character available from @var{port},\n"
  1050. "@emph{without} updating @var{port} to point to the following\n"
  1051. "character. If no more characters are available, the\n"
  1052. "end-of-file object is returned.@footnote{The value returned by\n"
  1053. "a call to @code{peek-char} is the same as the value that would\n"
  1054. "have been returned by a call to @code{read-char} on the same\n"
  1055. "port. The only difference is that the very next call to\n"
  1056. "@code{read-char} or @code{peek-char} on that @var{port} will\n"
  1057. "return the value returned by the preceding call to\n"
  1058. "@code{peek-char}. In particular, a call to @code{peek-char} on\n"
  1059. "an interactive port will hang waiting for input whenever a call\n"
  1060. "to @code{read-char} would have hung.}")
  1061. #define FUNC_NAME s_scm_peek_char
  1062. {
  1063. int c;
  1064. if (SCM_UNBNDP (port))
  1065. port = scm_cur_inp;
  1066. else
  1067. SCM_VALIDATE_OPINPORT (1,port);
  1068. c = scm_getc (port);
  1069. if (EOF == c)
  1070. return SCM_EOF_VAL;
  1071. scm_ungetc (c, port);
  1072. return SCM_MAKE_CHAR (c);
  1073. }
  1074. #undef FUNC_NAME
  1075. SCM_DEFINE (scm_unread_char, "unread-char", 1, 1, 0,
  1076. (SCM cobj, SCM port),
  1077. "Place @var{char} in @var{port} so that it will be read by the\n"
  1078. "next read operation. If called multiple times, the unread characters\n"
  1079. "will be read again in last-in first-out order. If @var{port} is\n"
  1080. "not supplied, the current input port is used.")
  1081. #define FUNC_NAME s_scm_unread_char
  1082. {
  1083. int c;
  1084. SCM_VALIDATE_CHAR (1,cobj);
  1085. if (SCM_UNBNDP (port))
  1086. port = scm_cur_inp;
  1087. else
  1088. SCM_VALIDATE_OPINPORT (2,port);
  1089. c = SCM_CHAR (cobj);
  1090. scm_ungetc (c, port);
  1091. return cobj;
  1092. }
  1093. #undef FUNC_NAME
  1094. SCM_DEFINE (scm_unread_string, "unread-string", 2, 0, 0,
  1095. (SCM str, SCM port),
  1096. "Place the string @var{str} in @var{port} so that its characters will be\n"
  1097. "read in subsequent read operations. If called multiple times, the\n"
  1098. "unread characters will be read again in last-in first-out order. If\n"
  1099. "@var{port} is not supplied, the current-input-port is used.")
  1100. #define FUNC_NAME s_scm_unread_string
  1101. {
  1102. SCM_VALIDATE_STRING (1,str);
  1103. if (SCM_UNBNDP (port))
  1104. port = scm_cur_inp;
  1105. else
  1106. SCM_VALIDATE_OPINPORT (2,port);
  1107. scm_ungets (SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str), port);
  1108. return str;
  1109. }
  1110. #undef FUNC_NAME
  1111. SCM_DEFINE (scm_seek, "seek", 3, 0, 0,
  1112. (SCM fd_port, SCM offset, SCM whence),
  1113. "Sets the current position of @var{fd/port} to the integer\n"
  1114. "@var{offset}, which is interpreted according to the value of\n"
  1115. "@var{whence}.\n"
  1116. "\n"
  1117. "One of the following variables should be supplied for\n"
  1118. "@var{whence}:\n"
  1119. "@defvar SEEK_SET\n"
  1120. "Seek from the beginning of the file.\n"
  1121. "@end defvar\n"
  1122. "@defvar SEEK_CUR\n"
  1123. "Seek from the current position.\n"
  1124. "@end defvar\n"
  1125. "@defvar SEEK_END\n"
  1126. "Seek from the end of the file.\n"
  1127. "@end defvar\n"
  1128. "If @var{fd/port} is a file descriptor, the underlying system\n"
  1129. "call is @code{lseek}. @var{port} may be a string port.\n"
  1130. "\n"
  1131. "The value returned is the new position in the file. This means\n"
  1132. "that the current position of a port can be obtained using:\n"
  1133. "@lisp\n"
  1134. "(seek port 0 SEEK_CUR)\n"
  1135. "@end lisp")
  1136. #define FUNC_NAME s_scm_seek
  1137. {
  1138. off_t off;
  1139. off_t rv;
  1140. int how;
  1141. fd_port = SCM_COERCE_OUTPORT (fd_port);
  1142. off = SCM_NUM2LONG (2, offset);
  1143. SCM_VALIDATE_INUM_COPY (3, whence, how);
  1144. if (how != SEEK_SET && how != SEEK_CUR && how != SEEK_END)
  1145. SCM_OUT_OF_RANGE (3, whence);
  1146. if (SCM_OPPORTP (fd_port))
  1147. {
  1148. scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (fd_port);
  1149. if (!ptob->seek)
  1150. SCM_MISC_ERROR ("port is not seekable",
  1151. scm_cons (fd_port, SCM_EOL));
  1152. else
  1153. rv = ptob->seek (fd_port, off, how);
  1154. }
  1155. else /* file descriptor?. */
  1156. {
  1157. SCM_VALIDATE_INUM (1,fd_port);
  1158. rv = lseek (SCM_INUM (fd_port), off, how);
  1159. if (rv == -1)
  1160. SCM_SYSERROR;
  1161. }
  1162. return scm_long2num (rv);
  1163. }
  1164. #undef FUNC_NAME
  1165. #ifdef __MINGW32__
  1166. /* Define this function since it is not supported under Windows. */
  1167. static int truncate (char *file, int length)
  1168. {
  1169. int ret = -1, fdes;
  1170. if ((fdes = open (file, O_BINARY | O_WRONLY)) != -1)
  1171. {
  1172. ret = chsize (fdes, length);
  1173. close (fdes);
  1174. }
  1175. return ret;
  1176. }
  1177. #endif /* __MINGW32__ */
  1178. SCM_DEFINE (scm_truncate_file, "truncate-file", 1, 1, 0,
  1179. (SCM object, SCM length),
  1180. "Truncates the object referred to by @var{object} to at most\n"
  1181. "@var{length} bytes. @var{object} can be a string containing a\n"
  1182. "file name or an integer file descriptor or a port.\n"
  1183. "@var{length} may be omitted if @var{object} is not a file name,\n"
  1184. "in which case the truncation occurs at the current port.\n"
  1185. "position. The return value is unspecified.")
  1186. #define FUNC_NAME s_scm_truncate_file
  1187. {
  1188. int rv;
  1189. off_t c_length;
  1190. /* object can be a port, fdes or filename. */
  1191. if (SCM_UNBNDP (length))
  1192. {
  1193. /* must supply length if object is a filename. */
  1194. if (SCM_STRINGP (object))
  1195. SCM_MISC_ERROR("must supply length if OBJECT is a filename",SCM_EOL);
  1196. length = scm_seek (object, SCM_INUM0, SCM_MAKINUM (SEEK_CUR));
  1197. }
  1198. c_length = SCM_NUM2LONG (2,length);
  1199. if (c_length < 0)
  1200. SCM_MISC_ERROR ("negative offset", SCM_EOL);
  1201. object = SCM_COERCE_OUTPORT (object);
  1202. if (SCM_INUMP (object))
  1203. {
  1204. SCM_SYSCALL (rv = ftruncate (SCM_INUM (object), c_length));
  1205. }
  1206. else if (SCM_OPOUTPORTP (object))
  1207. {
  1208. scm_t_port *pt = SCM_PTAB_ENTRY (object);
  1209. scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (object);
  1210. if (!ptob->truncate)
  1211. SCM_MISC_ERROR ("port is not truncatable", SCM_EOL);
  1212. if (pt->rw_active == SCM_PORT_READ)
  1213. scm_end_input (object);
  1214. else if (pt->rw_active == SCM_PORT_WRITE)
  1215. ptob->flush (object);
  1216. ptob->truncate (object, c_length);
  1217. rv = 0;
  1218. }
  1219. else
  1220. {
  1221. SCM_VALIDATE_STRING (1, object);
  1222. SCM_STRING_COERCE_0TERMINATION_X (object);
  1223. SCM_SYSCALL (rv = truncate (SCM_STRING_CHARS (object), c_length));
  1224. }
  1225. if (rv == -1)
  1226. SCM_SYSERROR;
  1227. return SCM_UNSPECIFIED;
  1228. }
  1229. #undef FUNC_NAME
  1230. SCM_DEFINE (scm_port_line, "port-line", 1, 0, 0,
  1231. (SCM port),
  1232. "Return the current line number for @var{port}.")
  1233. #define FUNC_NAME s_scm_port_line
  1234. {
  1235. port = SCM_COERCE_OUTPORT (port);
  1236. SCM_VALIDATE_OPENPORT (1,port);
  1237. return SCM_MAKINUM (SCM_LINUM (port));
  1238. }
  1239. #undef FUNC_NAME
  1240. SCM_DEFINE (scm_set_port_line_x, "set-port-line!", 2, 0, 0,
  1241. (SCM port, SCM line),
  1242. "Set the current line number for @var{port} to @var{line}.")
  1243. #define FUNC_NAME s_scm_set_port_line_x
  1244. {
  1245. port = SCM_COERCE_OUTPORT (port);
  1246. SCM_VALIDATE_OPENPORT (1,port);
  1247. SCM_VALIDATE_INUM (2,line);
  1248. SCM_PTAB_ENTRY (port)->line_number = SCM_INUM (line);
  1249. return SCM_UNSPECIFIED;
  1250. }
  1251. #undef FUNC_NAME
  1252. SCM_DEFINE (scm_port_column, "port-column", 1, 0, 0,
  1253. (SCM port),
  1254. "@deffnx {Scheme Procedure} port-line port\n"
  1255. "Return the current column number or line number of @var{port},\n"
  1256. "using the current input port if none is specified. If the number is\n"
  1257. "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n"
  1258. "- i.e. the first character of the first line is line 0, column 0.\n"
  1259. "(However, when you display a file position, for example in an error\n"
  1260. "message, we recommend you add 1 to get 1-origin integers. This is\n"
  1261. "because lines and column numbers traditionally start with 1, and that is\n"
  1262. "what non-programmers will find most natural.)")
  1263. #define FUNC_NAME s_scm_port_column
  1264. {
  1265. port = SCM_COERCE_OUTPORT (port);
  1266. SCM_VALIDATE_OPENPORT (1,port);
  1267. return SCM_MAKINUM (SCM_COL (port));
  1268. }
  1269. #undef FUNC_NAME
  1270. SCM_DEFINE (scm_set_port_column_x, "set-port-column!", 2, 0, 0,
  1271. (SCM port, SCM column),
  1272. "@deffnx {Scheme Procedure} set-port-line! port line\n"
  1273. "Set the current column or line number of @var{port}, using the\n"
  1274. "current input port if none is specified.")
  1275. #define FUNC_NAME s_scm_set_port_column_x
  1276. {
  1277. port = SCM_COERCE_OUTPORT (port);
  1278. SCM_VALIDATE_OPENPORT (1,port);
  1279. SCM_VALIDATE_INUM (2,column);
  1280. SCM_PTAB_ENTRY (port)->column_number = SCM_INUM (column);
  1281. return SCM_UNSPECIFIED;
  1282. }
  1283. #undef FUNC_NAME
  1284. SCM_DEFINE (scm_port_filename, "port-filename", 1, 0, 0,
  1285. (SCM port),
  1286. "Return the filename associated with @var{port}. This function returns\n"
  1287. "the strings \"standard input\", \"standard output\" and \"standard error\"\n"
  1288. "when called on the current input, output and error ports respectively.")
  1289. #define FUNC_NAME s_scm_port_filename
  1290. {
  1291. port = SCM_COERCE_OUTPORT (port);
  1292. SCM_VALIDATE_OPENPORT (1,port);
  1293. return SCM_FILENAME (port);
  1294. }
  1295. #undef FUNC_NAME
  1296. SCM_DEFINE (scm_set_port_filename_x, "set-port-filename!", 2, 0, 0,
  1297. (SCM port, SCM filename),
  1298. "Change the filename associated with @var{port}, using the current input\n"
  1299. "port if none is specified. Note that this does not change the port's\n"
  1300. "source of data, but only the value that is returned by\n"
  1301. "@code{port-filename} and reported in diagnostic output.")
  1302. #define FUNC_NAME s_scm_set_port_filename_x
  1303. {
  1304. port = SCM_COERCE_OUTPORT (port);
  1305. SCM_VALIDATE_OPENPORT (1,port);
  1306. /* We allow the user to set the filename to whatever he likes. */
  1307. SCM_SET_FILENAME (port, filename);
  1308. return SCM_UNSPECIFIED;
  1309. }
  1310. #undef FUNC_NAME
  1311. #ifndef ttyname
  1312. extern char * ttyname();
  1313. #endif
  1314. void
  1315. scm_print_port_mode (SCM exp, SCM port)
  1316. {
  1317. scm_puts (SCM_CLOSEDP (exp)
  1318. ? "closed: "
  1319. : (SCM_RDNG & SCM_CELL_WORD_0 (exp)
  1320. ? (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
  1321. ? "input-output: "
  1322. : "input: ")
  1323. : (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
  1324. ? "output: "
  1325. : "bogus: ")),
  1326. port);
  1327. }
  1328. int
  1329. scm_port_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  1330. {
  1331. char *type = SCM_PTOBNAME (SCM_PTOBNUM (exp));
  1332. if (!type)
  1333. type = "port";
  1334. scm_puts ("#<", port);
  1335. scm_print_port_mode (exp, port);
  1336. scm_puts (type, port);
  1337. scm_putc (' ', port);
  1338. scm_intprint (SCM_CELL_WORD_1 (exp), 16, port);
  1339. scm_putc ('>', port);
  1340. return 1;
  1341. }
  1342. void
  1343. scm_ports_prehistory ()
  1344. {
  1345. scm_numptob = 0;
  1346. scm_ptobs = (scm_t_ptob_descriptor *) malloc (sizeof (scm_t_ptob_descriptor));
  1347. }
  1348. /* Void ports. */
  1349. scm_t_bits scm_tc16_void_port = 0;
  1350. static int fill_input_void_port (SCM port SCM_UNUSED)
  1351. {
  1352. return EOF;
  1353. }
  1354. static void
  1355. write_void_port (SCM port SCM_UNUSED,
  1356. const void *data SCM_UNUSED,
  1357. size_t size SCM_UNUSED)
  1358. {
  1359. }
  1360. SCM
  1361. scm_void_port (char *mode_str)
  1362. {
  1363. int mode_bits;
  1364. SCM answer;
  1365. scm_t_port * pt;
  1366. SCM_NEWCELL (answer);
  1367. SCM_DEFER_INTS;
  1368. mode_bits = scm_mode_bits (mode_str);
  1369. pt = scm_add_to_port_table (answer);
  1370. scm_port_non_buffer (pt);
  1371. SCM_SETPTAB_ENTRY (answer, pt);
  1372. SCM_SETSTREAM (answer, 0);
  1373. SCM_SET_CELL_TYPE (answer, scm_tc16_void_port | mode_bits);
  1374. SCM_ALLOW_INTS;
  1375. return answer;
  1376. }
  1377. SCM_DEFINE (scm_sys_make_void_port, "%make-void-port", 1, 0, 0,
  1378. (SCM mode),
  1379. "Create and return a new void port. A void port acts like\n"
  1380. "@file{/dev/null}. The @var{mode} argument\n"
  1381. "specifies the input/output modes for this port: see the\n"
  1382. "documentation for @code{open-file} in @ref{File Ports}.")
  1383. #define FUNC_NAME s_scm_sys_make_void_port
  1384. {
  1385. SCM_VALIDATE_STRING (1, mode);
  1386. SCM_STRING_COERCE_0TERMINATION_X (mode);
  1387. return scm_void_port (SCM_STRING_CHARS (mode));
  1388. }
  1389. #undef FUNC_NAME
  1390. /* Initialization. */
  1391. void
  1392. scm_init_ports ()
  1393. {
  1394. /* lseek() symbols. */
  1395. scm_c_define ("SEEK_SET", SCM_MAKINUM (SEEK_SET));
  1396. scm_c_define ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR));
  1397. scm_c_define ("SEEK_END", SCM_MAKINUM (SEEK_END));
  1398. scm_tc16_void_port = scm_make_port_type ("void", fill_input_void_port,
  1399. write_void_port);
  1400. #include "libguile/ports.x"
  1401. }
  1402. /*
  1403. Local Variables:
  1404. c-file-style: "gnu"
  1405. End:
  1406. */