My various dotfiles

chap-21.texi 111KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623
  1. @node Streams, Printer, Files, Top
  2. @chapter Streams
  3. @menu
  4. * Stream Concepts::
  5. * Streams Dictionary::
  6. @end menu
  7. @node Stream Concepts, Streams Dictionary, Streams, Streams
  8. @section Stream Concepts
  9. @c including concept-streams
  10. @menu
  11. * Introduction to Streams::
  12. * Stream Variables::
  13. * Stream Arguments to Standardized Functions::
  14. * Restrictions on Composite Streams::
  15. @end menu
  16. @node Introduction to Streams, Stream Variables, Stream Concepts, Stream Concepts
  17. @subsection Introduction to Streams
  18. A @i{stream}
  19. @IGindex{stream}
  20. is an @i{object} that can be used with an input or output
  21. function to identify an appropriate source or sink of @i{characters} or
  22. @i{bytes} for that operation.
  23. A @i{character}
  24. @IGindex{character}
  25. @i{stream}
  26. @IGindex{stream}
  27. is a source or sink of @i{characters}.
  28. A @i{binary}
  29. @IGindex{binary}
  30. @i{stream}
  31. @IGindex{stream}
  32. is a source or sink of @i{bytes}.
  33. Some operations may be performed on any kind of @i{stream};
  34. Figure 21--1 provides a list of @i{standardized} operations
  35. that are potentially useful with any kind of @i{stream}.
  36. @group
  37. @noindent
  38. @w{ close stream-element-type }
  39. @w{ input-stream-p streamp }
  40. @w{ interactive-stream-p with-open-stream }
  41. @w{ output-stream-p }
  42. @noindent
  43. @w{ Figure 21--1: Some General-Purpose Stream Operations}
  44. @end group
  45. Other operations are only meaningful on certain @i{stream} @i{types}.
  46. For example, @b{read-char} is only defined for @i{character} @i{streams}
  47. and @b{read-byte} is only defined for @i{binary} @i{streams}.
  48. @menu
  49. * Abstract Classifications of Streams (Introduction to Streams)::
  50. * Input::
  51. * Open and Closed Streams::
  52. * Interactive Streams::
  53. * Abstract Classifications of Streams::
  54. * File Streams::
  55. * Other Subclasses of Stream::
  56. @end menu
  57. @node Abstract Classifications of Streams (Introduction to Streams), Input, Introduction to Streams, Introduction to Streams
  58. @subsubsection Abstract Classifications of Streams
  59. @node Input, Open and Closed Streams, Abstract Classifications of Streams (Introduction to Streams), Introduction to Streams
  60. @subsubsection Input, Output, and Bidirectional Streams
  61. A @i{stream}, whether a @i{character} @i{stream} or a @i{binary} @i{stream},
  62. can be an @i{input}
  63. @IGindex{input}
  64. @i{stream}
  65. @IGindex{stream}
  66. (source of data),
  67. an @i{output}
  68. @IGindex{output}
  69. @i{stream}
  70. @IGindex{stream}
  71. (sink for data),
  72. both,
  73. or (@i{e.g.}, when ``@t{:direction :probe}'' is given to @b{open}) neither.
  74. Figure 21--2 shows @i{operators} relating to
  75. @i{input} @i{streams}.
  76. @group
  77. @noindent
  78. @w{ clear-input read-byte read-from-string }
  79. @w{ listen read-char read-line }
  80. @w{ peek-char read-char-no-hang read-preserving-whitespace }
  81. @w{ read read-delimited-list unread-char }
  82. @noindent
  83. @w{ Figure 21--2: Operators relating to Input Streams. }
  84. @end group
  85. Figure 21--3 shows @i{operators} relating to
  86. @i{output} @i{streams}.
  87. @group
  88. @noindent
  89. @w{ clear-output prin1 write }
  90. @w{ finish-output prin1-to-string write-byte }
  91. @w{ force-output princ write-char }
  92. @w{ format princ-to-string write-line }
  93. @w{ fresh-line print write-string }
  94. @w{ pprint terpri write-to-string }
  95. @noindent
  96. @w{ Figure 21--3: Operators relating to Output Streams.}
  97. @end group
  98. A @i{stream} that is both an @i{input} @i{stream} and an @i{output} @i{stream}
  99. is called a @i{bidirectional}
  100. @IGindex{bidirectional}
  101. @i{stream}
  102. @IGindex{stream}
  103. .
  104. See the @i{functions} @b{input-stream-p} and @b{output-stream-p}.
  105. Any of the @i{operators} listed in @i{Figure~21--2} or @i{Figure~21--3}
  106. can be used with @i{bidirectional} @i{streams}. In addition, Figure 21--4
  107. shows a list of @i{operators} that relate specificaly to
  108. @i{bidirectional} @i{streams}.
  109. @group
  110. @noindent
  111. @w{ y-or-n-p yes-or-no-p }
  112. @noindent
  113. @w{ Figure 21--4: Operators relating to Bidirectional Streams.}
  114. @end group
  115. @node Open and Closed Streams, Interactive Streams, Input, Introduction to Streams
  116. @subsubsection Open and Closed Streams
  117. @i{Streams} are either @i{open}
  118. @IGindex{open}
  119. or @i{closed}
  120. @IGindex{closed}
  121. .
  122. Except as explicitly specified otherwise,
  123. operations that create and return @i{streams} return @i{open} @i{streams}.
  124. The action of @i{closing} a @i{stream} marks the end of its use as a source
  125. or sink of data, permitting the @i{implementation} to reclaim its internal data
  126. structures, and to free any external resources which might have been locked by the
  127. @i{stream} when it was opened.
  128. Except as explicitly specified otherwise,
  129. the consequences are undefined when a @i{closed} @i{stream}
  130. is used where a @i{stream} is called for.
  131. Coercion of @i{streams} to @i{pathnames}
  132. is permissible for @i{closed} @i{streams};
  133. in some situations, such as for a @i{truename} computation,
  134. the result might be different for an @i{open} @i{stream}
  135. and for that same @i{stream} once it has been @i{closed}.
  136. @node Interactive Streams, Abstract Classifications of Streams, Open and Closed Streams, Introduction to Streams
  137. @subsubsection Interactive Streams
  138. An @i{interactive stream}
  139. @IGindex{interactive stream}
  140. is one on which it makes sense to perform
  141. interactive querying.
  142. The precise meaning of an @i{interactive stream} is
  143. @i{implementation-defined}, and may depend on the underlying
  144. operating system. Some examples of the things that an
  145. @i{implementation} might choose to use as identifying characteristics
  146. of an @i{interactive stream} include:
  147. @table @asis
  148. @item @t{*}
  149. The @i{stream} is connected to a person (or equivalent) in such a way
  150. that the program can prompt for information and expect to receive different
  151. input depending on the prompt.
  152. @item @t{*}
  153. The program is expected to prompt for input and support ``normal input editing''.
  154. @item @t{*}
  155. @b{read-char} might wait for the user to type something before returning
  156. instead of immediately returning a character or end-of-file.
  157. @end table
  158. The general intent of having some @i{streams} be classified as
  159. @i{interactive streams} is to allow them to be distinguished from
  160. streams containing batch (or background or command-file) input.
  161. Output to batch streams is typically discarded or saved for later viewing,
  162. so interactive queries to such streams might not have the expected effect.
  163. @i{Terminal I/O} might or might not be an @i{interactive stream}.
  164. @node Abstract Classifications of Streams, File Streams, Interactive Streams, Introduction to Streams
  165. @subsubsection Abstract Classifications of Streams
  166. @node File Streams, Other Subclasses of Stream, Abstract Classifications of Streams, Introduction to Streams
  167. @subsubsection File Streams
  168. Some @i{streams}, called @i{file streams}
  169. @IGindex{file stream}
  170. , provide access to @i{files}.
  171. An @i{object} of @i{class} @b{file-stream} is used to represent a @i{file stream}.
  172. The basic operation for opening a @i{file} is @b{open},
  173. which typically returns a @i{file stream}
  174. (see its dictionary entry for details).
  175. The basic operation for closing a @i{stream} is @b{close}.
  176. The macro @b{with-open-file} is useful
  177. to express the common idiom of opening a @i{file}
  178. for the duration of a given body of @i{code},
  179. and assuring that the resulting @i{stream} is closed upon exit from that body.
  180. @node Other Subclasses of Stream, , File Streams, Introduction to Streams
  181. @subsubsection Other Subclasses of Stream
  182. The @i{class} @b{stream} has a number of @i{subclasses} defined
  183. by this specification. Figure 21--5 shows some information
  184. about these subclasses.
  185. @group
  186. @noindent
  187. @w{ Class Related Operators }
  188. @w{ @b{broadcast-stream} @b{make-broadcast-stream} }
  189. @w{ @b{broadcast-stream-streams} }
  190. @w{ @b{concatenated-stream} @b{make-concatenated-stream} }
  191. @w{ @b{concatenated-stream-streams} }
  192. @w{ @b{echo-stream} @b{make-echo-stream} }
  193. @w{ @b{echo-stream-input-stream} }
  194. @w{ @b{echo-stream-output-stream} }
  195. @w{ @b{string-stream} @b{make-string-input-stream} }
  196. @w{ @b{with-input-from-string} }
  197. @w{ @b{make-string-output-stream} }
  198. @w{ @b{with-output-to-string} }
  199. @w{ @b{get-output-stream-string} }
  200. @w{ @b{synonym-stream} @b{make-synonym-stream} }
  201. @w{ @b{synonym-stream-symbol} }
  202. @w{ @b{two-way-stream} @b{make-two-way-stream} }
  203. @w{ @b{two-way-stream-input-stream} }
  204. @w{ @b{two-way-stream-output-stream} }
  205. @noindent
  206. @w{ Figure 21--5: Defined Names related to Specialized Streams}
  207. @end group
  208. @node Stream Variables, Stream Arguments to Standardized Functions, Introduction to Streams, Stream Concepts
  209. @subsection Stream Variables
  210. @i{Variables} whose @i{values} must be @i{streams} are sometimes called
  211. @i{stream variables}
  212. @IGindex{stream variable}
  213. .
  214. Certain @i{stream variables} are defined by this specification
  215. to be the proper source of input or output in various @i{situations}
  216. where no specific @i{stream} has been specified instead.
  217. A complete list of such @i{standardized} @i{stream variables}
  218. appears in Figure 21--6.
  219. The consequences are undefined if at any time
  220. the @i{value} of any of these @i{variables} is not an @i{open} @i{stream}.
  221. @group
  222. @noindent
  223. @w{ Glossary Term Variable Name }
  224. @w{ @i{debug I/O} @b{*debug-io*} }
  225. @w{ @i{error output} @b{*error-output*} }
  226. @w{ @i{query I/O} @b{*query-io*} }
  227. @w{ @i{standard input} @b{*standard-input*} }
  228. @w{ @i{standard output} @b{*standard-output*} }
  229. @w{ @i{terminal I/O} @b{*terminal-io*} }
  230. @w{ @i{trace output} @b{*trace-output*} }
  231. @noindent
  232. @w{ Figure 21--6: Standardized Stream Variables}
  233. @end group
  234. Note that, by convention, @i{standardized} @i{stream variables} have names
  235. ending in ``@t{-input*}'' if they must be @i{input} @i{streams},
  236. ending in ``@t{-output*}'' if they must be @i{output} @i{streams},
  237. or ending in ``@t{-io*}'' if they must be @i{bidirectional} @i{streams}.
  238. User programs may @i{assign} or @i{bind} any @i{standardized} @i{stream variable}
  239. except @b{*terminal-io*}.
  240. @node Stream Arguments to Standardized Functions, Restrictions on Composite Streams, Stream Variables, Stream Concepts
  241. @subsection Stream Arguments to Standardized Functions
  242. The @i{operators} in Figure 21--7 accept @i{stream} @i{arguments} that
  243. might be either @i{open} or @i{closed} @i{streams}.
  244. @group
  245. @noindent
  246. @w{ broadcast-stream-streams file-author pathnamep }
  247. @w{ close file-namestring probe-file }
  248. @w{ compile-file file-write-date rename-file }
  249. @w{ compile-file-pathname host-namestring streamp }
  250. @w{ concatenated-stream-streams load synonym-stream-symbol }
  251. @w{ delete-file logical-pathname translate-logical-pathname }
  252. @w{ directory merge-pathnames translate-pathname }
  253. @w{ directory-namestring namestring truename }
  254. @w{ dribble open two-way-stream-input-stream }
  255. @w{ echo-stream-input-stream open-stream-p two-way-stream-output-stream }
  256. @w{ echo-stream-ouput-stream parse-namestring wild-pathname-p }
  257. @w{ ed pathname with-open-file }
  258. @w{ enough-namestring pathname-match-p }
  259. @noindent
  260. @w{ Figure 21--7: Operators that accept either Open or Closed Streams }
  261. @end group
  262. The @i{operators} in Figure 21--8 accept @i{stream} @i{arguments} that
  263. must be @i{open} @i{streams}.
  264. @group
  265. @noindent
  266. @w{ clear-input output-stream-p read-char-no-hang }
  267. @w{ clear-output peek-char read-delimited-list }
  268. @w{ file-length pprint read-line }
  269. @w{ file-position pprint-fill read-preserving-whitespace }
  270. @w{ file-string-length pprint-indent stream-element-type }
  271. @w{ finish-output pprint-linear stream-external-format }
  272. @w{ force-output pprint-logical-block terpri }
  273. @w{ format pprint-newline unread-char }
  274. @w{ fresh-line pprint-tab with-open-stream }
  275. @w{ get-output-stream-string pprint-tabular write }
  276. @w{ input-stream-p prin1 write-byte }
  277. @w{ interactive-stream-p princ write-char }
  278. @w{ listen print write-line }
  279. @w{ make-broadcast-stream print-object write-string }
  280. @w{ make-concatenated-stream print-unreadable-object y-or-n-p }
  281. @w{ make-echo-stream read yes-or-no-p }
  282. @w{ make-synonym-stream read-byte }
  283. @w{ make-two-way-stream read-char }
  284. @noindent
  285. @w{ Figure 21--8: Operators that accept Open Streams only }
  286. @end group
  287. @node Restrictions on Composite Streams, , Stream Arguments to Standardized Functions, Stream Concepts
  288. @subsection Restrictions on Composite Streams
  289. The consequences are undefined if any @i{component} of a @i{composite stream}
  290. is @i{closed} before the @i{composite stream} is @i{closed}.
  291. The consequences are undefined if the @i{synonym stream symbol} is not @i{bound}
  292. to an @i{open} @i{stream} from the time of the @i{synonym stream}'s creation
  293. until the time it is @i{closed}.
  294. @c end of including concept-streams
  295. @node Streams Dictionary, , Stream Concepts, Streams
  296. @section Streams Dictionary
  297. @c including dict-streams
  298. @menu
  299. * stream::
  300. * broadcast-stream::
  301. * concatenated-stream::
  302. * echo-stream::
  303. * file-stream::
  304. * string-stream::
  305. * synonym-stream::
  306. * two-way-stream::
  307. * input-stream-p::
  308. * interactive-stream-p::
  309. * open-stream-p::
  310. * stream-element-type::
  311. * streamp::
  312. * read-byte::
  313. * write-byte::
  314. * peek-char::
  315. * read-char::
  316. * read-char-no-hang::
  317. * terpri::
  318. * unread-char::
  319. * write-char::
  320. * read-line::
  321. * write-string::
  322. * read-sequence::
  323. * write-sequence::
  324. * file-length::
  325. * file-position::
  326. * file-string-length::
  327. * open::
  328. * stream-external-format::
  329. * with-open-file::
  330. * close::
  331. * with-open-stream::
  332. * listen::
  333. * clear-input::
  334. * finish-output::
  335. * y-or-n-p::
  336. * make-synonym-stream::
  337. * synonym-stream-symbol::
  338. * broadcast-stream-streams::
  339. * make-broadcast-stream::
  340. * make-two-way-stream::
  341. * two-way-stream-input-stream::
  342. * echo-stream-input-stream::
  343. * make-echo-stream::
  344. * concatenated-stream-streams::
  345. * make-concatenated-stream::
  346. * get-output-stream-string::
  347. * make-string-input-stream::
  348. * make-string-output-stream::
  349. * with-input-from-string::
  350. * with-output-to-string::
  351. * *debug-io*::
  352. * *terminal-io*::
  353. * stream-error::
  354. * stream-error-stream::
  355. * end-of-file::
  356. @end menu
  357. @node stream, broadcast-stream, Streams Dictionary, Streams Dictionary
  358. @subsection stream [System Class]
  359. @subsubheading Class Precedence List::
  360. @b{stream},
  361. @b{t}
  362. @subsubheading Description::
  363. A @i{stream} is an @i{object} that can be used with an input or output
  364. function to identify an appropriate source or sink of @i{characters} or
  365. @i{bytes} for that operation.
  366. For more complete information, see @ref{Stream Concepts}.
  367. @subsubheading See Also::
  368. @ref{Stream Concepts},
  369. @ref{Printing Other Objects},
  370. {@ref{Printer}},
  371. {@ref{Reader}}
  372. @node broadcast-stream, concatenated-stream, stream, Streams Dictionary
  373. @subsection broadcast-stream [System Class]
  374. @subsubheading Class Precedence List::
  375. @b{broadcast-stream},
  376. @b{stream},
  377. @b{t}
  378. @subsubheading Description::
  379. A @i{broadcast stream} is an @i{output} @i{stream} which
  380. has associated with it a set of zero or more @i{output} @i{streams}
  381. such that any output sent to the @i{broadcast stream} gets passed on
  382. as output to each of the associated @i{output} @i{streams}.
  383. (If a @i{broadcast stream} has no @i{component streams},
  384. then all output to the @i{broadcast stream} is discarded.)
  385. The set of operations that may be performed on a @i{broadcast stream}
  386. is the intersection of those for its associated @i{output} @i{streams}.
  387. Some output operations (@i{e.g.}, @b{fresh-line}) return @i{values} based on the
  388. state of the @i{stream} at the time of the operation.
  389. Since these @i{values} might differ for each of the @i{component streams},
  390. it is necessary to describe their return value specifically:
  391. @table @asis
  392. @item @t{*}
  393. @b{stream-element-type} returns
  394. the value from the last component stream,
  395. or @b{t} if there are no component streams.
  396. @item @t{*}
  397. @b{fresh-line} returns
  398. the value from the last component stream,
  399. or @b{nil} if there are no component streams.
  400. @item @t{*}
  401. The functions
  402. @b{file-length},
  403. @b{file-position},
  404. @b{file-string-length},
  405. and @b{stream-external-format}
  406. return the value from the last component stream;
  407. if there are no component streams,
  408. @b{file-length} and @b{file-position} return @t{0},
  409. @b{file-string-length} returns @t{1},
  410. and @b{stream-external-format} returns @t{:default}.
  411. @item @t{*}
  412. The functions @b{streamp} and @b{output-stream-p}
  413. always return @i{true} for @i{broadcast streams}.
  414. @item @t{*}
  415. The functions @b{open-stream-p} tests whether the @i{broadcast stream}
  416. is @i{open}_2, not whether its component streams are @i{open}.
  417. @item @t{*}
  418. The functions @b{input-stream-p} and @i{interactive-stream-p}
  419. return an @i{implementation-defined}, @i{generalized boolean} value.
  420. @item @t{*}
  421. For the input operations
  422. @b{clear-input}
  423. @b{listen},
  424. @b{peek-char},
  425. @b{read-byte},
  426. @b{read-char-no-hang},
  427. @b{read-char},
  428. @b{read-line},
  429. and @b{unread-char},
  430. the consequences are undefined if the indicated operation is performed.
  431. However, an @i{implementation} is permitted
  432. to define such a behavior as an @i{implementation-dependent} extension.
  433. @end table
  434. For any output operations not having their return values explicitly specified above
  435. or elsewhere in this document, it is defined that
  436. the @i{values} returned by such an operation are
  437. the @i{values} resulting from performing the operation
  438. on the last of its @i{component streams};
  439. the @i{values} resulting from performing the operation
  440. on all preceding @i{streams} are discarded.
  441. If there are no @i{component streams},
  442. the value is @i{implementation-dependent}.
  443. @subsubheading See Also::
  444. @ref{broadcast-stream-streams}
  445. ,
  446. @ref{make-broadcast-stream}
  447. @node concatenated-stream, echo-stream, broadcast-stream, Streams Dictionary
  448. @subsection concatenated-stream [System Class]
  449. @subsubheading Class Precedence List::
  450. @b{concatenated-stream},
  451. @b{stream},
  452. @b{t}
  453. @subsubheading Description::
  454. A @i{concatenated stream} is an @i{input} @i{stream} which
  455. is a @i{composite stream} of zero or more other @i{input} @i{streams},
  456. such that the sequence of data which can be read from the
  457. @i{concatenated stream} is the same as the concatenation of the
  458. sequences of data which could be read from each of the
  459. constituent @i{streams}.
  460. Input from a @i{concatenated stream} is taken from the first
  461. of the associated @i{input streams} until it reaches @i{end of file}_1;
  462. then that @i{stream} is discarded, and subsequent input is taken
  463. from the next @i{input stream}, and so on.
  464. An @i{end of file} on the associated @i{input streams} is always managed
  465. invisibly by the @i{concatenated stream}---the only time a client of
  466. a @i{concatenated stream} sees an @i{end of file} is when an attempt is
  467. made to obtain data from the @i{concatenated stream} but it has no
  468. remaining @i{input streams} from which to obtain such data.
  469. @subsubheading See Also::
  470. @ref{concatenated-stream-streams}
  471. ,
  472. @ref{make-concatenated-stream}
  473. @node echo-stream, file-stream, concatenated-stream, Streams Dictionary
  474. @subsection echo-stream [System Class]
  475. @subsubheading Class Precedence List::
  476. @b{echo-stream},
  477. @b{stream},
  478. @b{t}
  479. @subsubheading Description::
  480. An @i{echo stream} is a @i{bidirectional} @i{stream}
  481. that gets its input from an associated @i{input} @i{stream}
  482. and sends its output to an associated @i{output} @i{stream}.
  483. All input taken from the @i{input} @i{stream}
  484. is echoed to the @i{output} @i{stream}.
  485. Whether the input is echoed immediately after it is encountered,
  486. or after it has been read from the @i{input stream}
  487. is @i{implementation-dependent}.
  488. @subsubheading See Also::
  489. @ref{echo-stream-input-stream; echo-stream-output-stream}
  490. ,
  491. @b{echo-stream-output-stream},
  492. @ref{make-echo-stream}
  493. @node file-stream, string-stream, echo-stream, Streams Dictionary
  494. @subsection file-stream [System Class]
  495. @subsubheading Class Precedence List::
  496. @b{file-stream},
  497. @b{stream},
  498. @b{t}
  499. @subsubheading Description::
  500. An @i{object} of @i{type} @b{file-stream} is a @i{stream} the direct
  501. source or sink of which is a @i{file}. Such a @i{stream} is
  502. created explicitly by @b{open} and @b{with-open-file}, and
  503. implicitly by @i{functions} such as @b{load} that process @i{files}.
  504. @subsubheading See Also::
  505. @ref{load}
  506. ,
  507. @ref{open}
  508. ,
  509. @ref{with-open-file}
  510. @node string-stream, synonym-stream, file-stream, Streams Dictionary
  511. @subsection string-stream [System Class]
  512. @subsubheading Class Precedence List::
  513. @b{string-stream},
  514. @b{stream},
  515. @b{t}
  516. @subsubheading Description::
  517. A @i{string stream} is a @i{stream}
  518. which reads input from or writes output to an associated @i{string}.
  519. The @i{stream element type} of a @i{string stream} is always
  520. a @i{subtype} of @i{type} @b{character}.
  521. @subsubheading See Also::
  522. @ref{make-string-input-stream}
  523. ,
  524. @ref{make-string-output-stream}
  525. ,
  526. @ref{with-input-from-string}
  527. ,
  528. @ref{with-output-to-string}
  529. @node synonym-stream, two-way-stream, string-stream, Streams Dictionary
  530. @subsection synonym-stream [System Class]
  531. @subsubheading Class Precedence List::
  532. @b{synonym-stream},
  533. @b{stream},
  534. @b{t}
  535. @subsubheading Description::
  536. A @i{stream} that is an alias for another @i{stream},
  537. which is the @i{value} of a @i{dynamic variable}
  538. whose @i{name} is the @i{synonym stream symbol} of the @i{synonym stream}.
  539. Any operations on a @i{synonym stream} will be performed
  540. on the @i{stream} that is then the @i{value} of the
  541. @i{dynamic variable} named by the @i{synonym stream symbol}.
  542. If the @i{value} of the @i{variable} should change,
  543. or if the @i{variable} should be @i{bound},
  544. then the @i{stream} will operate on the new @i{value} of the @i{variable}.
  545. @subsubheading See Also::
  546. @ref{make-synonym-stream}
  547. ,
  548. @ref{synonym-stream-symbol}
  549. @node two-way-stream, input-stream-p, synonym-stream, Streams Dictionary
  550. @subsection two-way-stream [System Class]
  551. @subsubheading Class Precedence List::
  552. @b{two-way-stream},
  553. @b{stream},
  554. @b{t}
  555. @subsubheading Description::
  556. A @i{bidirectional} @i{composite stream} that
  557. receives its input from an associated @i{input} @i{stream}
  558. and sends its output to an associated @i{output} @i{stream}.
  559. @subsubheading See Also::
  560. @ref{make-two-way-stream}
  561. ,
  562. @ref{two-way-stream-input-stream; two-way-stream-output-stream}
  563. ,
  564. @b{two-way-stream-output-stream}
  565. @node input-stream-p, interactive-stream-p, two-way-stream, Streams Dictionary
  566. @subsection input-stream-p, output-stream-p [Function]
  567. @code{input-stream-p} @i{stream} @result{} @i{generalized-boolean}
  568. @code{output-stream-p} @i{stream} @result{} @i{generalized-boolean}
  569. @subsubheading Arguments and Values::
  570. @i{stream}---a @i{stream}.
  571. @i{generalized-boolean}---a @i{generalized boolean}.
  572. @subsubheading Description::
  573. @b{input-stream-p} returns @i{true} if @i{stream} is an @i{input} @i{stream};
  574. otherwise, returns @i{false}.
  575. @b{output-stream-p} returns @i{true} if @i{stream} is an @i{output} @i{stream};
  576. otherwise, returns @i{false}.
  577. @subsubheading Examples::
  578. @example
  579. (input-stream-p *standard-input*) @result{} @i{true}
  580. (input-stream-p *terminal-io*) @result{} @i{true}
  581. (input-stream-p (make-string-output-stream)) @result{} @i{false}
  582. (output-stream-p *standard-output*) @result{} @i{true}
  583. (output-stream-p *terminal-io*) @result{} @i{true}
  584. (output-stream-p (make-string-input-stream "jr")) @result{} @i{false}
  585. @end example
  586. @subsubheading Exceptional Situations::
  587. Should signal an error of @i{type} @b{type-error}
  588. if @i{stream} is not a @i{stream}.
  589. @node interactive-stream-p, open-stream-p, input-stream-p, Streams Dictionary
  590. @subsection interactive-stream-p [Function]
  591. @code{interactive-stream-p} @i{stream} @result{} @i{generalized-boolean}
  592. @subsubheading Arguments and Values::
  593. @i{stream}---a @i{stream}.
  594. @i{generalized-boolean}---a @i{generalized boolean}.
  595. @subsubheading Description::
  596. Returns @i{true} if @i{stream} is an @i{interactive stream};
  597. otherwise, returns @i{false}.
  598. @subsubheading Examples::
  599. @example
  600. (when (> measured limit)
  601. (let ((error (round (* (- measured limit) 100)
  602. limit)))
  603. (unless (if (interactive-stream-p *query-io*)
  604. (yes-or-no-p "The frammis is out of tolerance by ~D
  605. Is it safe to proceed? " error)
  606. (< error 15)) ;15
  607. (error "The frammis is out of tolerance by ~D
  608. @end example
  609. @subsubheading Exceptional Situations::
  610. Should signal an error of @i{type} @b{type-error}
  611. if @i{stream} is not a @i{stream}.
  612. @subsubheading See Also::
  613. @ref{Stream Concepts}
  614. @node open-stream-p, stream-element-type, interactive-stream-p, Streams Dictionary
  615. @subsection open-stream-p [Function]
  616. @code{open-stream-p} @i{stream} @result{} @i{generalized-boolean}
  617. @subsubheading Arguments and Values::
  618. @i{stream}---a @i{stream}.
  619. @i{generalized-boolean}---a @i{generalized boolean}.
  620. @subsubheading Description::
  621. Returns @i{true} if @i{stream} is an @i{open} @i{stream};
  622. otherwise, returns @i{false}.
  623. @i{Streams} are open until they have been explicitly closed with @b{close},
  624. or until they are implicitly closed due to exit from a
  625. @b{with-output-to-string},
  626. @b{with-open-file},
  627. @b{with-input-from-string}, or
  628. @b{with-open-stream} @i{form}.
  629. @subsubheading Examples::
  630. @example
  631. (open-stream-p *standard-input*) @result{} @i{true}
  632. @end example
  633. @subsubheading Affected By::
  634. @b{close}.
  635. @subsubheading Exceptional Situations::
  636. Should signal an error of @i{type} @b{type-error}
  637. if @i{stream} is not a @i{stream}.
  638. @node stream-element-type, streamp, open-stream-p, Streams Dictionary
  639. @subsection stream-element-type [Function]
  640. @code{stream-element-type} @i{stream} @result{} @i{typespec}
  641. @subsubheading Arguments and Values::
  642. @i{stream}---a @i{stream}.
  643. @i{typespec}---a @i{type specifier}.
  644. @subsubheading Description::
  645. @b{stream-element-type} returns a @i{type specifier} that
  646. indicates the @i{types} of @i{objects} that may be read from
  647. or written to @i{stream}.
  648. @i{Streams} created by @b{open} have an @i{element type}
  649. restricted to @b{integer} or a @i{subtype} of @i{type} @b{character}.
  650. @subsubheading Examples::
  651. @example
  652. ;; Note that the stream must accomodate at least the specified type,
  653. ;; but might accomodate other types. Further note that even if it does
  654. ;; accomodate exactly the specified type, the type might be specified in
  655. ;; any of several ways.
  656. (with-open-file (s "test" :element-type '(integer 0 1)
  657. :if-exists :error
  658. :direction :output)
  659. (stream-element-type s))
  660. @result{} INTEGER
  661. @i{OR}@result{} (UNSIGNED-BYTE 16)
  662. @i{OR}@result{} (UNSIGNED-BYTE 8)
  663. @i{OR}@result{} BIT
  664. @i{OR}@result{} (UNSIGNED-BYTE 1)
  665. @i{OR}@result{} (INTEGER 0 1)
  666. @i{OR}@result{} (INTEGER 0 (2))
  667. @end example
  668. @subsubheading Exceptional Situations::
  669. Should signal an error of @i{type} @b{type-error}
  670. if @i{stream} is not a @i{stream}.
  671. @node streamp, read-byte, stream-element-type, Streams Dictionary
  672. @subsection streamp [Function]
  673. @code{streamp} @i{object} @result{} @i{generalized-boolean}
  674. @subsubheading Arguments and Values::
  675. @i{object}---an @i{object}.
  676. @i{generalized-boolean}---a @i{generalized boolean}.
  677. @subsubheading Description::
  678. Returns @i{true} if @i{object} is of @i{type} @b{stream};
  679. otherwise, returns @i{false}.
  680. @b{streamp} is unaffected by whether @i{object},
  681. if it is a @i{stream}, is @i{open} or closed.
  682. @subsubheading Examples::
  683. @example
  684. (streamp *terminal-io*) @result{} @i{true}
  685. (streamp 1) @result{} @i{false}
  686. @end example
  687. @subsubheading Notes::
  688. @example
  689. (streamp @i{object}) @equiv{} (typep @i{object} 'stream)
  690. @end example
  691. @node read-byte, write-byte, streamp, Streams Dictionary
  692. @subsection read-byte [Function]
  693. @code{read-byte} @i{stream {&optional} eof-error-p eof-value} @result{} @i{byte}
  694. @subsubheading Arguments and Values::
  695. @i{stream}---a @i{binary} @i{input} @i{stream}.
  696. @i{eof-error-p}---a @i{generalized boolean}.
  697. The default is @i{true}.
  698. @i{eof-value}---an @i{object}.
  699. The default is @b{nil}.
  700. @i{byte}---an @i{integer},
  701. or the @i{eof-value}.
  702. @subsubheading Description::
  703. @b{read-byte} reads and returns one byte from @i{stream}.
  704. If an @i{end of file}_2 occurs and @i{eof-error-p} is @i{false},
  705. the @i{eof-value} is returned.
  706. @subsubheading Examples::
  707. @example
  708. (with-open-file (s "temp-bytes"
  709. :direction :output
  710. :element-type 'unsigned-byte)
  711. (write-byte 101 s)) @result{} 101
  712. (with-open-file (s "temp-bytes" :element-type 'unsigned-byte)
  713. (format t "~S ~S" (read-byte s) (read-byte s nil 'eof)))
  714. @t{ |> } 101 EOF
  715. @result{} NIL
  716. @end example
  717. @subsubheading Side Effects::
  718. Modifies @i{stream}.
  719. @subsubheading Exceptional Situations::
  720. Should signal an error of @i{type} @b{type-error}
  721. if @i{stream} is not a @i{stream}.
  722. Should signal an error of @i{type} @b{error}
  723. if @i{stream} is not a @i{binary} @i{input} @i{stream}.
  724. If there are no @i{bytes} remaining in the @i{stream}
  725. and @i{eof-error-p} is @i{true}, an error of @i{type} @b{end-of-file} is signaled.
  726. @subsubheading See Also::
  727. @ref{read-char}
  728. ,
  729. @ref{read-sequence}
  730. ,
  731. @ref{write-byte}
  732. @node write-byte, peek-char, read-byte, Streams Dictionary
  733. @subsection write-byte [Function]
  734. @code{write-byte} @i{byte stream} @result{} @i{byte}
  735. @subsubheading Arguments and Values::
  736. @i{byte}---an @i{integer} of the @i{stream element type}
  737. of @i{stream}.
  738. @i{stream}---a @i{binary} @i{output} @i{stream}.
  739. @subsubheading Description::
  740. @b{write-byte} writes one byte, @i{byte}, to @i{stream}.
  741. @subsubheading Examples::
  742. @example
  743. (with-open-file (s "temp-bytes"
  744. :direction :output
  745. :element-type 'unsigned-byte)
  746. (write-byte 101 s)) @result{} 101
  747. @end example
  748. @subsubheading Side Effects::
  749. @i{stream} is modified.
  750. @subsubheading Affected By::
  751. The @i{element type} of the @i{stream}.
  752. @subsubheading Exceptional Situations::
  753. Should signal an error of @i{type} @b{type-error}
  754. if @i{stream} is not a @i{stream}.
  755. Should signal an error of @i{type} @b{error}
  756. if @i{stream} is not a @i{binary} @i{output} @i{stream}.
  757. Might signal an error of @i{type} @b{type-error} if @i{byte} is not
  758. an @i{integer} of the @i{stream element type} of @i{stream}.
  759. @subsubheading See Also::
  760. @ref{read-byte}
  761. ,
  762. @ref{write-char}
  763. ,
  764. @ref{write-sequence}
  765. @node peek-char, read-char, write-byte, Streams Dictionary
  766. @subsection peek-char [Function]
  767. @code{peek-char} @i{{&optional} peek-type input-stream eof-error-p
  768. eof-value recursive-p} @result{} @i{char}
  769. @subsubheading Arguments and Values::
  770. @i{peek-type}---a @i{character} or @b{t} or @b{nil}.
  771. @i{input-stream}---@i{input} @i{stream designator}.
  772. The default is @i{standard input}.
  773. @i{eof-error-p}---a @i{generalized boolean}.
  774. The default is @i{true}.
  775. @i{eof-value}---an @i{object}.
  776. The default is @b{nil}.
  777. @i{recursive-p}---a @i{generalized boolean}.
  778. The default is @i{false}.
  779. @i{char}---a @i{character} or the @i{eof-value}.
  780. @subsubheading Description::
  781. @b{peek-char} obtains the next character in @i{input-stream}
  782. without actually reading it, thus leaving the character
  783. to be read at a later time. It can
  784. also be used to skip over and discard intervening
  785. characters in the @i{input-stream}
  786. until a particular character is found.
  787. If @i{peek-type} is not supplied or @b{nil},
  788. @b{peek-char} returns the next character to be read from
  789. @i{input-stream}, without actually removing it from
  790. @i{input-stream}.
  791. The next time input is done from @i{input-stream}, the character will still
  792. be there.
  793. If @i{peek-type} is @b{t},
  794. then @b{peek-char} skips over @i{whitespace}_2 @i{characters},
  795. but not comments,
  796. and then performs the peeking operation on the next
  797. character.
  798. The last character examined, the one that starts an @i{object},
  799. is not removed from @i{input-stream}.
  800. If @i{peek-type} is a @i{character},
  801. then @b{peek-char} skips
  802. over input characters until a character that
  803. is @b{char=} to that @i{character} is found;
  804. that character is left in @i{input-stream}.
  805. If an @i{end of file}_2 occurs and @i{eof-error-p} is @i{false},
  806. @i{eof-value} is returned.
  807. { }{If @i{recursive-p} is @i{true},
  808. this call is expected to be embedded in a higher-level call to @b{read}
  809. or a similar @i{function} used by the @i{Lisp reader}.}
  810. When @i{input-stream} is an @i{echo stream},
  811. characters that are only peeked at are not echoed. In the
  812. case that @i{peek-type} is not @b{nil},
  813. the characters that are passed by @b{peek-char}
  814. are treated as if by @b{read-char},
  815. and so are echoed unless they have been marked otherwise by @b{unread-char}.
  816. @subsubheading Examples::
  817. @example
  818. (with-input-from-string (input-stream " 1 2 3 4 5")
  819. (format t "~S ~S ~S"
  820. (peek-char t input-stream)
  821. (peek-char #\4 input-stream)
  822. (peek-char nil input-stream)))
  823. @t{ |> } #\1 #\4 #\4
  824. @result{} NIL
  825. @end example
  826. @subsubheading Affected By::
  827. @b{*readtable*},
  828. @b{*standard-input*},
  829. @b{*terminal-io*}.
  830. @subsubheading Exceptional Situations::
  831. If @i{eof-error-p} is @i{true} and an @i{end of file}_2 occurs
  832. an error of @i{type} @b{end-of-file} is signaled.
  833. If @i{peek-type} is a @i{character},
  834. an @i{end of file}_2 occurs,
  835. and @i{eof-error-p} is @i{true},
  836. an error of @i{type} @b{end-of-file} is signaled.
  837. If @i{recursive-p} is @i{true}
  838. and an @i{end of file}_2 occurs,
  839. an error of @i{type} @b{end-of-file} is signaled.
  840. @node read-char, read-char-no-hang, peek-char, Streams Dictionary
  841. @subsection read-char [Function]
  842. @code{read-char} @i{{&optional} input-stream eof-error-p eof-value recursive-p} @result{} @i{char}
  843. @subsubheading Arguments and Values::
  844. @i{input-stream}---an @i{input} @i{stream designator}.
  845. The default is @i{standard input}.
  846. @i{eof-error-p}---a @i{generalized boolean}.
  847. The default is @i{true}.
  848. @i{eof-value}---an @i{object}.
  849. The default is @b{nil}.
  850. @i{recursive-p}---a @i{generalized boolean}.
  851. The default is @i{false}.
  852. @i{char}---a @i{character} or the @i{eof-value}.
  853. @subsubheading Description::
  854. @b{read-char} returns the next @i{character} from @i{input-stream}.
  855. When @i{input-stream} is an @i{echo stream},
  856. the character is echoed on @i{input-stream} the first time the character is
  857. seen.
  858. Characters that are not echoed by @b{read-char}
  859. are those that were
  860. put there by @b{unread-char}
  861. and hence are assumed to have been echoed
  862. already by a previous call to @b{read-char}.
  863. { }{If @i{recursive-p} is @i{true},
  864. this call is expected to be embedded in a higher-level call to @b{read}
  865. or a similar @i{function} used by the @i{Lisp reader}.}
  866. If an @i{end of file}_2 occurs and @i{eof-error-p} is @i{false},
  867. @i{eof-value} is returned.
  868. @subsubheading Examples::
  869. @example
  870. (with-input-from-string (is "0123")
  871. (do ((c (read-char is) (read-char is nil 'the-end)))
  872. ((not (characterp c)))
  873. (format t "~S " c)))
  874. @t{ |> } #\0 #\1 #\2 #\3
  875. @result{} NIL
  876. @end example
  877. @subsubheading Affected By::
  878. @b{*standard-input*},
  879. @b{*terminal-io*}.
  880. @subsubheading Exceptional Situations::
  881. If an @i{end of file}_2 occurs before a character can be read, and
  882. @i{eof-error-p} is @i{true},
  883. an error of @i{type} @b{end-of-file} is signaled.
  884. @subsubheading See Also::
  885. @ref{read-byte}
  886. ,
  887. @ref{read-sequence}
  888. ,
  889. @ref{write-char}
  890. ,
  891. @ref{read; read-preserving-whitespace}
  892. @subsubheading Notes::
  893. The corresponding output function is @b{write-char}.
  894. @node read-char-no-hang, terpri, read-char, Streams Dictionary
  895. @subsection read-char-no-hang [Function]
  896. @code{read-char-no-hang} @i{{&optional} input-stream eof-error-p
  897. eof-value recursive-p} @result{} @i{char}
  898. @subsubheading Arguments and Values::
  899. @i{input-stream} -- an @i{input} @i{stream designator}.
  900. The default is @i{standard input}.
  901. @i{eof-error-p}---a @i{generalized boolean}.
  902. The default is @i{true}.
  903. @i{eof-value}---an @i{object}.
  904. The default is @b{nil}.
  905. @i{recursive-p}---a @i{generalized boolean}.
  906. The default is @i{false}.
  907. @i{char}---a @i{character} or @b{nil} or the @i{eof-value}.
  908. @subsubheading Description::
  909. @b{read-char-no-hang} returns a character
  910. from @i{input-stream} if such a character is available. If no character
  911. is available, @b{read-char-no-hang} returns @b{nil}.
  912. { }{If @i{recursive-p} is @i{true},
  913. this call is expected to be embedded in a higher-level call to @b{read}
  914. or a similar @i{function} used by the @i{Lisp reader}.}
  915. If an @i{end of file}_2 occurs and @i{eof-error-p} is @i{false},
  916. @i{eof-value} is returned.
  917. @subsubheading Examples::
  918. @example
  919. ;; This code assumes an implementation in which a newline is not
  920. ;; required to terminate input from the console.
  921. (defun test-it ()
  922. (unread-char (read-char))
  923. (list (read-char-no-hang)
  924. (read-char-no-hang)
  925. (read-char-no-hang)))
  926. @result{} TEST-IT
  927. ;; Implementation A, where a Newline is not required to terminate
  928. ;; interactive input on the console.
  929. (test-it)
  930. @t{ |> } @b{|>>}@t{a}@b{<<|}
  931. @result{} (#\a NIL NIL)
  932. ;; Implementation B, where a Newline is required to terminate
  933. ;; interactive input on the console, and where that Newline remains
  934. ;; on the input stream.
  935. (test-it)
  936. @t{ |> } @b{|>>}@t{a{@i{[<--}~]}}@b{<<|}
  937. @result{} (#\a #\Newline NIL)
  938. @end example
  939. @subsubheading Affected By::
  940. @b{*standard-input*},
  941. @b{*terminal-io*}.
  942. @subsubheading Exceptional Situations::
  943. If an @i{end of file}_2 occurs
  944. when @i{eof-error-p} is @i{true},
  945. an error of @i{type} @b{end-of-file} is signaled .
  946. @subsubheading See Also::
  947. @ref{listen}
  948. @subsubheading Notes::
  949. @b{read-char-no-hang} is exactly like @b{read-char}, except
  950. that if it would be necessary to wait in order to get a character (as
  951. from a keyboard), @b{nil} is immediately returned without waiting.
  952. @node terpri, unread-char, read-char-no-hang, Streams Dictionary
  953. @subsection terpri, fresh-line [Function]
  954. @code{terpri} @i{{&optional} output-stream} @result{} @i{@b{nil}}
  955. @code{fresh-line} @i{{&optional} output-stream} @result{} @i{generalized-boolean}
  956. @subsubheading Arguments and Values::
  957. @i{output-stream} -- an @i{output} @i{stream designator}.
  958. The default is @i{standard output}.
  959. @i{generalized-boolean}---a @i{generalized boolean}.
  960. @subsubheading Description::
  961. @b{terpri} outputs a @i{newline} to @i{output-stream}.
  962. @b{fresh-line} is similar to @b{terpri} but outputs a @i{newline}
  963. only if the @i{output-stream} is not already at the start of a line.
  964. If for some reason this cannot be determined, then a @i{newline} is output anyway.
  965. @b{fresh-line} returns @i{true} if it outputs a @i{newline};
  966. otherwise it returns @i{false}.
  967. @subsubheading Examples::
  968. @example
  969. (with-output-to-string (s)
  970. (write-string "some text" s)
  971. (terpri s)
  972. (terpri s)
  973. (write-string "more text" s))
  974. @result{} "some text
  975. more text"
  976. (with-output-to-string (s)
  977. (write-string "some text" s)
  978. (fresh-line s)
  979. (fresh-line s)
  980. (write-string "more text" s))
  981. @result{} "some text
  982. more text"
  983. @end example
  984. @subsubheading Side Effects::
  985. The @i{output-stream} is modified.
  986. @subsubheading Affected By::
  987. @b{*standard-output*},
  988. @b{*terminal-io*}.
  989. @subsubheading Exceptional Situations::
  990. None.
  991. [Reviewer Note by Barmar: What if stream is closed?]
  992. @subsubheading Notes::
  993. @b{terpri} is identical in effect to
  994. @example
  995. (write-char #\Newline output-stream)
  996. @end example
  997. @node unread-char, write-char, terpri, Streams Dictionary
  998. @subsection unread-char [Function]
  999. @code{unread-char} @i{character {&optional} input-stream} @result{} @i{@b{nil}}
  1000. @subsubheading Arguments and Values::
  1001. @i{character}---a @i{character};
  1002. must be the last @i{character} that was read from @i{input-stream}.
  1003. @i{input-stream}---an @i{input} @i{stream designator}.
  1004. The default is @i{standard input}.
  1005. @subsubheading Description::
  1006. @b{unread-char} places @i{character} back onto the front of
  1007. @i{input-stream} so that it will again be the next character
  1008. in @i{input-stream}.
  1009. When @i{input-stream} is an @i{echo stream},
  1010. no attempt is made to undo any echoing of the character that might already
  1011. have been done on @i{input-stream}. However, characters placed on
  1012. @i{input-stream} by @b{unread-char} are marked in such a way
  1013. as to inhibit later re-echo by @b{read-char}.
  1014. It is an error to invoke @b{unread-char}
  1015. twice consecutively on the same @i{stream}
  1016. without an intervening call to @b{read-char}
  1017. (or some other input operation which implicitly reads characters)
  1018. on that @i{stream}.
  1019. Invoking @b{peek-char} or @b{read-char} commits all previous characters.
  1020. The consequences of invoking @b{unread-char}
  1021. on any character preceding that which is returned by
  1022. @b{peek-char} (including those passed over by
  1023. @b{peek-char} that has a @i{non-nil} @i{peek-type})
  1024. are unspecified.
  1025. In particular, the consequences of
  1026. invoking @b{unread-char} after @b{peek-char}
  1027. are unspecified.
  1028. @subsubheading Examples::
  1029. @example
  1030. (with-input-from-string (is "0123")
  1031. (dotimes (i 6)
  1032. (let ((c (read-char is)))
  1033. (if (evenp i) (format t "~&~S ~S~
  1034. @t{ |> } 0 #\0
  1035. @t{ |> } 2 #\1
  1036. @t{ |> } 4 #\2
  1037. @result{} NIL
  1038. @end example
  1039. @subsubheading Affected By::
  1040. @b{*standard-input*},
  1041. @b{*terminal-io*}.
  1042. @subsubheading See Also::
  1043. @ref{peek-char}
  1044. ,
  1045. @ref{read-char}
  1046. ,
  1047. @ref{Stream Concepts}
  1048. @subsubheading Notes::
  1049. @b{unread-char} is intended to be an efficient mechanism for allowing
  1050. the @i{Lisp reader} and other parsers to perform one-character lookahead
  1051. in @i{input-stream}.
  1052. @node write-char, read-line, unread-char, Streams Dictionary
  1053. @subsection write-char [Function]
  1054. @code{write-char} @i{character {&optional} output-stream} @result{} @i{character}
  1055. @subsubheading Arguments and Values::
  1056. @i{character}---a @i{character}.
  1057. @i{output-stream} -- an @i{output} @i{stream designator}.
  1058. The default is @i{standard output}.
  1059. @subsubheading Description::
  1060. @b{write-char} outputs @i{character} to @i{output-stream}.
  1061. @subsubheading Examples::
  1062. @example
  1063. (write-char #\a)
  1064. @t{ |> } a
  1065. @result{} #\a
  1066. (with-output-to-string (s)
  1067. (write-char #\a s)
  1068. (write-char #\Space s)
  1069. (write-char #\b s))
  1070. @result{} "a b"
  1071. @end example
  1072. @subsubheading Side Effects::
  1073. The @i{output-stream} is modified.
  1074. @subsubheading Affected By::
  1075. @b{*standard-output*},
  1076. @b{*terminal-io*}.
  1077. @subsubheading See Also::
  1078. @ref{read-char}
  1079. ,
  1080. @ref{write-byte}
  1081. ,
  1082. @ref{write-sequence}
  1083. @node read-line, write-string, write-char, Streams Dictionary
  1084. @subsection read-line [Function]
  1085. @code{read-line} @i{{&optional} input-stream eof-error-p eof-value recursive-p}@*
  1086. @result{} @i{line, missing-newline-p}
  1087. @subsubheading Arguments and Values::
  1088. @i{input-stream}---an @i{input} @i{stream designator}.
  1089. The default is @i{standard input}.
  1090. @i{eof-error-p}---a @i{generalized boolean}.
  1091. The default is @i{true}.
  1092. @i{eof-value}---an @i{object}.
  1093. The default is @b{nil}.
  1094. @i{recursive-p}---a @i{generalized boolean}.
  1095. The default is @i{false}.
  1096. @i{line}---a @i{string} or the @i{eof-value}.
  1097. @i{missing-newline-p}---a @i{generalized boolean}.
  1098. @subsubheading Description::
  1099. Reads from @i{input-stream} a line of text
  1100. that is terminated by a @i{newline} or @i{end of file}.
  1101. { }{If @i{recursive-p} is @i{true},
  1102. this call is expected to be embedded in a higher-level call to @b{read}
  1103. or a similar @i{function} used by the @i{Lisp reader}.}
  1104. The @i{primary value}, @i{line}, is the line that is read,
  1105. represented as a @i{string} (without the trailing @i{newline}, if any).
  1106. If @i{eof-error-p} is @i{false}
  1107. and the @i{end of file} for @i{input-stream} is reached
  1108. before any @i{characters} are read,
  1109. @i{eof-value} is returned as the @i{line}.
  1110. The @i{secondary value}, @i{missing-newline-p},
  1111. is a @i{generalized boolean} that is
  1112. @i{false} if the @i{line} was terminated by a @i{newline},
  1113. or @i{true} if the @i{line} was terminated by
  1114. the @i{end of file} for @i{input-stream}
  1115. (or if the @i{line} is the @i{eof-value}).
  1116. @subsubheading Examples::
  1117. @example
  1118. (setq a "line 1
  1119. line2")
  1120. @result{} "line 1
  1121. line2"
  1122. (read-line (setq input-stream (make-string-input-stream a)))
  1123. @result{} "line 1", @i{false}
  1124. (read-line input-stream)
  1125. @result{} "line2", @i{true}
  1126. (read-line input-stream nil nil)
  1127. @result{} NIL, @i{true}
  1128. @end example
  1129. @subsubheading Affected By::
  1130. @b{*standard-input*},
  1131. @b{*terminal-io*}.
  1132. @subsubheading Exceptional Situations::
  1133. If an @i{end of file}_2 occurs before any characters are read in the line,
  1134. an error is signaled if @i{eof-error-p} is @i{true}.
  1135. @subsubheading See Also::
  1136. @ref{read; read-preserving-whitespace}
  1137. @subsubheading Notes::
  1138. The corresponding output function is @b{write-line}.
  1139. @node write-string, read-sequence, read-line, Streams Dictionary
  1140. @subsection write-string, write-line [Function]
  1141. @code{write-string} @i{string {&optional} output-stream {&key} start end} @result{} @i{string}
  1142. @code{write-line} @i{string {&optional} output-stream {&key} start end} @result{} @i{string}
  1143. @subsubheading Arguments and Values::
  1144. @i{string}---a @i{string}.
  1145. @i{output-stream} -- an @i{output} @i{stream designator}.
  1146. The default is @i{standard output}.
  1147. @i{start}, @i{end}---@i{bounding index designators} of @i{string}.
  1148. The defaults for @i{start} and @i{end} are @t{0} and @b{nil}, respectively.
  1149. @subsubheading Description::
  1150. @b{write-string} writes the @i{characters} of
  1151. the subsequence of @i{string} @i{bounded} by @i{start} and @i{end}
  1152. to @i{output-stream}.
  1153. @b{write-line} does the same thing,
  1154. but then outputs a newline afterwards.
  1155. @subsubheading Examples::
  1156. @example
  1157. (prog1 (write-string "books" nil :end 4) (write-string "worms"))
  1158. @t{ |> } bookworms
  1159. @result{} "books"
  1160. (progn (write-char #\*)
  1161. (write-line "test12" *standard-output* :end 5)
  1162. (write-line "*test2")
  1163. (write-char #\*)
  1164. nil)
  1165. @t{ |> } *test1
  1166. @t{ |> } *test2
  1167. @t{ |> } *
  1168. @result{} NIL
  1169. @end example
  1170. @subsubheading Affected By::
  1171. @b{*standard-output*},
  1172. @b{*terminal-io*}.
  1173. @subsubheading See Also::
  1174. @ref{read-line}
  1175. ,
  1176. @ref{write-char}
  1177. @subsubheading Notes::
  1178. @b{write-line} and @b{write-string} return @i{string},
  1179. not the substring @i{bounded} by @i{start} and @i{end}.
  1180. @example
  1181. (write-string string)
  1182. @equiv{} (dotimes (i (length string)
  1183. (write-char (char string i)))
  1184. (write-line string)
  1185. @equiv{} (prog1 (write-string string) (terpri))
  1186. @end example
  1187. @node read-sequence, write-sequence, write-string, Streams Dictionary
  1188. @subsection read-sequence [Function]
  1189. @code{read-sequence} @i{sequence stream {&key} start end} @result{} @i{position}
  1190. @i{sequence}---a @i{sequence}.
  1191. @i{stream}---an @i{input} @i{stream}.
  1192. @i{start}, @i{end}---@i{bounding index designators} of
  1193. @i{sequence}. The defaults for @i{start} and @i{end} are @t{0} and @b{nil}, respectively.
  1194. @i{position}---an @i{integer} greater than or equal to zero, and
  1195. less than or equal to the @i{length} of the @i{sequence}.
  1196. @subsubheading Description::
  1197. Destructively modifies @i{sequence} by replacing the @i{elements}
  1198. of @i{sequence} @i{bounded} by @i{start} and @i{end} with
  1199. @i{elements} read from @i{stream}.
  1200. @i{Sequence} is destructively modified by copying successive
  1201. @i{elements} into it from @i{stream}. If the @i{end of file} for
  1202. @i{stream} is reached before copying all @i{elements} of the
  1203. subsequence, then the extra @i{elements} near the end of @i{sequence}
  1204. are not updated.
  1205. @i{Position} is the index of the first @i{element} of @i{sequence}
  1206. that was not updated, which might be less than @i{end} because the
  1207. @i{end of file} was reached.
  1208. @subsubheading Examples::
  1209. @example
  1210. (defvar *data* (make-array 15 :initial-element nil))
  1211. (values (read-sequence *data* (make-string-input-stream "test string")) *data*)
  1212. @result{} 11, #(#\t #\e #\s #\t #\Space #\s #\t #\r #\i #\n #\g NIL NIL NIL NIL)
  1213. @end example
  1214. @subsubheading Side Effects::
  1215. Modifies @i{stream} and @i{sequence}.
  1216. @subsubheading Exceptional Situations::
  1217. Should be prepared to signal an error of @i{type} @b{type-error}
  1218. if @i{sequence} is not a @i{proper sequence}.
  1219. Should signal an error of @i{type} @b{type-error}
  1220. if @i{start} is not a non-negative @i{integer}.
  1221. Should signal an error of @i{type} @b{type-error}
  1222. if @i{end} is not a non-negative @i{integer} or @b{nil}.
  1223. Might signal an error of @i{type} @b{type-error} if an @i{element} read from
  1224. the @i{stream} is not a member of the @i{element type} of the
  1225. @i{sequence}.
  1226. @subsubheading See Also::
  1227. @ref{Compiler Terminology},
  1228. @ref{write-sequence}
  1229. ,
  1230. @ref{read-line}
  1231. @subsubheading Notes::
  1232. @b{read-sequence} is identical in effect to iterating over the indicated
  1233. subsequence and reading one @i{element} at a time from @i{stream} and
  1234. storing it into @i{sequence}, but may be more efficient than the
  1235. equivalent loop. An efficient implementation is more likely to exist
  1236. for the case where the @i{sequence} is a @i{vector} with the same
  1237. @i{element type} as the @i{stream}.
  1238. @node write-sequence, file-length, read-sequence, Streams Dictionary
  1239. @subsection write-sequence [Function]
  1240. @code{write-sequence} @i{sequence stream {&key} start end} @result{} @i{sequence}
  1241. @i{sequence}---a @i{sequence}.
  1242. @i{stream}---an @i{output} @i{stream}.
  1243. @i{start}, @i{end}---@i{bounding index designators} of
  1244. @i{sequence}. The defaults for @i{start} and @i{end} are @t{0} and @b{nil}, respectively.
  1245. @subsubheading Description::
  1246. @b{write-sequence} writes the @i{elements} of the subsequence
  1247. of @i{sequence} @i{bounded} by @i{start} and @i{end} to
  1248. @i{stream}.
  1249. @subsubheading Examples::
  1250. @example
  1251. (write-sequence "bookworms" *standard-output* :end 4)
  1252. @t{ |> } book
  1253. @result{} "bookworms"
  1254. @end example
  1255. @subsubheading Side Effects::
  1256. Modifies @i{stream}.
  1257. @subsubheading Exceptional Situations::
  1258. Should be prepared to signal an error of @i{type} @b{type-error}
  1259. if @i{sequence} is not a @i{proper sequence}.
  1260. Should signal an error of @i{type} @b{type-error}
  1261. if @i{start} is not a non-negative @i{integer}.
  1262. Should signal an error of @i{type} @b{type-error}
  1263. if @i{end} is not a non-negative @i{integer} or @b{nil}.
  1264. Might signal an error of @i{type} @b{type-error} if an @i{element} of the
  1265. @i{bounded} @i{sequence} is not a member of the
  1266. @i{stream element type} of the @i{stream}.
  1267. @subsubheading See Also::
  1268. @ref{Compiler Terminology},
  1269. @ref{read-sequence}
  1270. ,
  1271. @ref{write-string; write-line}
  1272. ,
  1273. @b{write-line}
  1274. @subsubheading Notes::
  1275. @b{write-sequence} is identical in effect to iterating over the indicated
  1276. subsequence and writing one @i{element} at a time to @i{stream}, but
  1277. may be more efficient than the equivalent loop. An efficient implementation
  1278. is more likely to exist for the case where the @i{sequence} is a
  1279. @i{vector} with the same @i{element type} as the @i{stream}.
  1280. @node file-length, file-position, write-sequence, Streams Dictionary
  1281. @subsection file-length [Function]
  1282. @code{file-length} @i{stream} @result{} @i{length}
  1283. @subsubheading Arguments and Values::
  1284. @i{stream}---a @i{stream associated with a file}.
  1285. @i{length}---a non-negative @i{integer} or @b{nil}.
  1286. @subsubheading Description::
  1287. @b{file-length} returns the length of @i{stream},
  1288. or @b{nil} if the length cannot be determined.
  1289. For a binary file, the length is measured in units of
  1290. the @i{element type} of the @i{stream}.
  1291. @subsubheading Examples::
  1292. @example
  1293. (with-open-file (s "decimal-digits.text"
  1294. :direction :output :if-exists :error)
  1295. (princ "0123456789" s)
  1296. (truename s))
  1297. @result{} #P"A:>Joe>decimal-digits.text.1"
  1298. (with-open-file (s "decimal-digits.text")
  1299. (file-length s))
  1300. @result{} 10
  1301. @end example
  1302. @subsubheading Exceptional Situations::
  1303. Should signal an error of @i{type} @b{type-error}
  1304. if @i{stream} is not a @i{stream associated with a file}.
  1305. @subsubheading See Also::
  1306. @ref{open}
  1307. @node file-position, file-string-length, file-length, Streams Dictionary
  1308. @subsection file-position [Function]
  1309. @code{file-position} @i{stream} @result{} @i{position}
  1310. @code{file-position} @i{stream position-spec} @result{} @i{success-p}
  1311. @subsubheading Arguments and Values::
  1312. @i{stream}---a @i{stream}.
  1313. @i{position-spec}---a @i{file position designator}.
  1314. @i{position}---a @i{file position} or @b{nil}.
  1315. @i{success-p}---a @i{generalized boolean}.
  1316. @subsubheading Description::
  1317. Returns or changes the current position within a @i{stream}.
  1318. When @i{position-spec} is not supplied,
  1319. @b{file-position} returns the current @i{file position} in the @i{stream},
  1320. or @b{nil} if this cannot be determined.
  1321. When @i{position-spec} is supplied,
  1322. the @i{file position} in @i{stream} is set to that @i{file position} (if possible).
  1323. @b{file-position} returns @i{true}
  1324. if the repositioning is performed successfully,
  1325. or @i{false} if it is not.
  1326. An @i{integer} returned by @b{file-position} of one argument
  1327. should be acceptable as @i{position-spec} for use with the same file.
  1328. For a character file,
  1329. performing a single @b{read-char} or @b{write-char} operation
  1330. may cause the file position to be increased by more than 1 because of
  1331. character-set translations (such as translating between the @r{Common Lisp}
  1332. @t{#\Newline} character and an external ASCII
  1333. carriage-return/line-feed sequence) and other aspects of the
  1334. implementation. For a binary file, every @b{read-byte}
  1335. or @b{write-byte}
  1336. operation increases the file position by 1.
  1337. @subsubheading Examples::
  1338. @example
  1339. (defun tester ()
  1340. (let ((noticed '()) file-written)
  1341. (flet ((notice (x) (push x noticed) x))
  1342. (with-open-file (s "test.bin"
  1343. :element-type '(unsigned-byte 8)
  1344. :direction :output
  1345. :if-exists :error)
  1346. (notice (file-position s)) ;1
  1347. (write-byte 5 s)
  1348. (write-byte 6 s)
  1349. (let ((p (file-position s)))
  1350. (notice p) ;2
  1351. (notice (when p (file-position s (1- p))))) ;3
  1352. (write-byte 7 s)
  1353. (notice (file-position s)) ;4
  1354. (setq file-written (truename s)))
  1355. (with-open-file (s file-written
  1356. :element-type '(unsigned-byte 8)
  1357. :direction :input)
  1358. (notice (file-position s)) ;5
  1359. (let ((length (file-length s)))
  1360. (notice length) ;6
  1361. (when length
  1362. (dotimes (i length)
  1363. (notice (read-byte s)))))) ;7,...
  1364. (nreverse noticed))))
  1365. @result{} tester
  1366. (tester)
  1367. @result{} (0 2 T 2 0 2 5 7)
  1368. @i{OR}@result{} (0 2 NIL 3 0 3 5 6 7)
  1369. @i{OR}@result{} (NIL NIL NIL NIL NIL NIL)
  1370. @end example
  1371. @subsubheading Side Effects::
  1372. When the @i{position-spec} argument is supplied,
  1373. the @i{file position} in the @i{stream} might be moved.
  1374. @subsubheading Affected By::
  1375. The value returned by @b{file-position} increases monotonically
  1376. as input or output operations are performed.
  1377. @subsubheading Exceptional Situations::
  1378. If @i{position-spec} is supplied, but is too large or otherwise inappropriate,
  1379. an error is signaled.
  1380. @subsubheading See Also::
  1381. @ref{file-length}
  1382. ,
  1383. @ref{file-string-length}
  1384. ,
  1385. @ref{open}
  1386. @subsubheading Notes::
  1387. Implementations that have character files represented
  1388. as a sequence of records of bounded size might choose to encode the
  1389. file position as, for example,
  1390. <<@i{record-number}>>*<<@i{max-record-size}>>+<<@i{character-within-record}>>.
  1391. This is a valid encoding because it increases monotonically as
  1392. each character is read or written, though not necessarily by 1 at
  1393. each step. An @i{integer} might then be considered ``inappropriate''
  1394. as @i{position-spec} to @b{file-position} if, when decoded into
  1395. record number and character number, it turned out that the
  1396. supplied record was too short for the specified character number.
  1397. @node file-string-length, open, file-position, Streams Dictionary
  1398. @subsection file-string-length [Function]
  1399. @code{file-string-length} @i{stream object} @result{} @i{length}
  1400. @subsubheading Arguments and Values::
  1401. @i{stream}---an @i{output} @i{character} @i{file stream}.
  1402. @i{object}---a @i{string} or a @i{character}.
  1403. @i{length}---a non-negative @i{integer}, or @b{nil}.
  1404. @subsubheading Description::
  1405. @b{file-string-length} returns the difference between what
  1406. @t{(file-position @i{stream})} would be after writing
  1407. @i{object} and its current value, or @b{nil} if this cannot be determined.
  1408. The returned value corresponds to the current state of @i{stream}
  1409. at the time of the call and might not be
  1410. the same if it is called again
  1411. when the state of the @i{stream} has changed.
  1412. @node open, stream-external-format, file-string-length, Streams Dictionary
  1413. @subsection open [Function]
  1414. @code{open} @i{filespec {&key} direction element-type
  1415. if-exists if-does-not-exist
  1416. external-format}@*
  1417. @result{} @i{stream}
  1418. @subsubheading Arguments and Values::
  1419. @i{filespec}---a @i{pathname designator}.
  1420. @i{direction}---one of @t{:input}, @t{:output}, @t{:io}, or @t{:probe}.
  1421. The default is @t{:input}.
  1422. @i{element-type}---a @i{type specifier}
  1423. for @i{recognizable subtype} of @b{character};
  1424. or a @i{type specifier}
  1425. for a @i{finite} @i{recognizable subtype} of @i{integer};
  1426. or one of the @i{symbols}
  1427. @b{signed-byte},
  1428. @b{unsigned-byte},
  1429. or @t{:default}.
  1430. The default is @b{character}.
  1431. @i{if-exists}---one of @t{:error}, @t{:new-version}, @t{:rename},
  1432. @t{:rename-and-delete}, @t{:overwrite}, @t{:append},
  1433. @t{:supersede}, or @b{nil}.
  1434. The default is @t{:new-version} if the version component of @i{filespec} is @t{:newest},
  1435. or @t{:error} otherwise.
  1436. @i{if-does-not-exist}---one of @t{:error}, @t{:create}, or @b{nil}.
  1437. The default is @t{:error} if @i{direction} is @t{:input}
  1438. or @i{if-exists} is @t{:overwrite} or @t{:append};
  1439. @t{:create} if @i{direction} is @t{:output} or @t{:io},
  1440. and @i{if-exists} is neither @t{:overwrite} nor @t{:append};
  1441. or @b{nil} when @i{direction} is @t{:probe}.
  1442. @i{external-format}---an @i{external file format designator}.
  1443. The default is @t{:default}.
  1444. @i{stream}---a @i{file stream} or @b{nil}.
  1445. @subsubheading Description::
  1446. @b{open} creates, opens, and returns a @i{file stream}
  1447. that is connected to the file specified by @i{filespec}.
  1448. @i{Filespec} is the name of the file to be opened.
  1449. If the @i{filespec} @i{designator} is a @i{stream},
  1450. that @i{stream} is not closed first or otherwise affected.
  1451. The keyword arguments to @b{open} specify the characteristics
  1452. of the @i{file stream} that is returned, and how to handle errors.
  1453. If @i{direction} is @t{:input}
  1454. or @t{:probe},
  1455. or if @i{if-exists} is not @t{:new-version}
  1456. and the version component of the @i{filespec} is @t{:newest},
  1457. then the file opened is that file already existing in the file system
  1458. that has a version greater than that of any other file in the file system
  1459. whose other pathname components are the same as those of @i{filespec}.
  1460. An implementation is required to recognize all of
  1461. the @b{open} keyword options
  1462. and to do something reasonable in the context of the host operating
  1463. system.
  1464. For example, if a file system does not support distinct file
  1465. versions and does not distinguish the notions of deletion and expunging,
  1466. @t{:new-version} might be treated the same as
  1467. @t{:rename} or @t{:supersede}, and @t{:rename-and-delete} might
  1468. be treated the same as @t{:supersede}.
  1469. @table @asis
  1470. @item @t{:direction}
  1471. These are the possible values for @i{direction},
  1472. and how they affect the nature of the @i{stream} that is created:
  1473. @table @asis
  1474. @item @t{:input}
  1475. Causes the creation of an @i{input} @i{file stream}.
  1476. @item @t{:output}
  1477. Causes the creation of an @i{output} @i{file stream}.
  1478. @item @t{:io}
  1479. Causes the creation of a @i{bidirectional} @i{file stream}.
  1480. @item @t{:probe}
  1481. Causes the creation of a ``no-directional'' @i{file stream};
  1482. in effect, the @i{file stream} is created
  1483. and then closed prior to being returned by @b{open}.
  1484. @end table
  1485. @item @t{:element-type}
  1486. The @i{element-type} specifies the unit of transaction for the @i{file stream}.
  1487. If it is @t{:default},
  1488. the unit is determined by @i{file system},
  1489. possibly based on the @i{file}.
  1490. @item @t{:if-exists}
  1491. @i{if-exists} specifies the action to be taken if @i{direction} is
  1492. @t{:output} or @t{:io} and a file of the name @i{filespec}
  1493. already exists.
  1494. If @i{direction} is @t{:input}, not supplied, or @t{:probe},
  1495. @i{if-exists} is ignored.
  1496. These are the results of @b{open} as modified by @i{if-exists}:
  1497. @table @asis
  1498. @item @t{:error}
  1499. An error of @i{type} @b{file-error} is signaled.
  1500. @item @t{:new-version}
  1501. A new file is created with a larger version number.
  1502. @item @t{:rename}
  1503. The existing file is renamed to some other name and then a new file is created.
  1504. @item @t{:rename-and-delete}
  1505. The existing file is renamed to some other name,
  1506. then it is deleted but not expunged, and then a new file is created.
  1507. @item @t{:overwrite}
  1508. Output operations on the @i{stream} destructively modify the existing file.
  1509. If @i{direction} is @t{:io} the file is opened in a bidirectional mode
  1510. that allows both reading and writing. The file pointer is initially
  1511. positioned at the beginning of the file; however, the file is not truncated
  1512. back to length zero when it is opened.
  1513. @item @t{:append}
  1514. Output operations on the @i{stream} destructively modify the existing file.
  1515. The file pointer is initially positioned at the end of the file.
  1516. If @i{direction} is @t{:io},
  1517. the file is opened in a bidirectional mode that allows both reading and writing.
  1518. @item @t{:supersede}
  1519. The existing file is superseded;
  1520. that is, a new file with the same name as the old one is created.
  1521. If possible, the implementation should not destroy the old file until the new
  1522. @i{stream} is closed.
  1523. @item @b{nil}
  1524. No file or @i{stream} is created;
  1525. instead, @b{nil} is returned to indicate failure.
  1526. @end table
  1527. @item @t{:if-does-not-exist}
  1528. @i{if-does-not-exist}
  1529. specifies the action to be taken if
  1530. a file of name @i{filespec} does not already exist.
  1531. These are the results of @b{open} as modified by @i{if-does-not-exist}:
  1532. @table @asis
  1533. @item @t{:error}
  1534. An error of @i{type} @b{file-error} is signaled.
  1535. @item @t{:create}
  1536. An empty file is created.
  1537. Processing continues as if the file
  1538. had already existed but no processing as
  1539. directed by @i{if-exists} is performed.
  1540. @item @b{nil}
  1541. No file or @i{stream} is created;
  1542. instead, @b{nil} is returned to indicate failure.
  1543. @end table
  1544. @item @t{:external-format}
  1545. This option selects an @i{external file format} for the @i{file}:
  1546. The only @i{standardized} value for this option is @t{:default},
  1547. although @i{implementations} are permitted to define additional
  1548. @i{external file formats} and @i{implementation-dependent} values
  1549. returned by @b{stream-external-format} can also be used by @i{conforming programs}.
  1550. The @i{external-format} is meaningful for
  1551. any kind of @i{file stream} whose @i{element type}
  1552. is a @i{subtype} of @i{character}.
  1553. This option is ignored for @i{streams} for which it is not meaningful;
  1554. however, @i{implementations} may define other @i{element types}
  1555. for which it is meaningful.
  1556. The consequences are unspecified if a @i{character} is written
  1557. that cannot be represented by the given @i{external file format}.
  1558. @end table
  1559. When a file is opened, a @i{file stream} is constructed to serve
  1560. as the file system's ambassador to the @r{Lisp} environment;
  1561. operations on the @i{file stream} are reflected by operations on the file
  1562. in the file system.
  1563. A file can be deleted, renamed, or destructively modified by @b{open}.
  1564. For information about opening relative pathnames,
  1565. see @ref{Merging Pathnames}.
  1566. @subsubheading Examples::
  1567. @example
  1568. (open @i{filespec} :direction :probe) @result{} #<Closed Probe File Stream...>
  1569. (setq q (merge-pathnames (user-homedir-pathname) "test"))
  1570. @result{} #<PATHNAME :HOST NIL :DEVICE @i{device-name} :DIRECTORY @i{directory-name}
  1571. :NAME "test" :TYPE NIL :VERSION :NEWEST>
  1572. (open @i{filespec} :if-does-not-exist :create) @result{} #<Input File Stream...>
  1573. (setq s (open @i{filespec} :direction :probe)) @result{} #<Closed Probe File Stream...>
  1574. (truename s) @result{} #<PATHNAME :HOST NIL :DEVICE @i{device-name} :DIRECTORY
  1575. @i{directory-name} :NAME @i{filespec} :TYPE @i{extension} :VERSION 1>
  1576. (open s :direction :output :if-exists nil) @result{} NIL
  1577. @end example
  1578. @subsubheading Affected By::
  1579. The nature and state of the host computer's @i{file system}.
  1580. @subsubheading Exceptional Situations::
  1581. If @i{if-exists} is @t{:error}, (subject to the
  1582. constraints on the meaning of @i{if-exists} listed above),
  1583. an error of @i{type} @b{file-error} is signaled.
  1584. If @i{if-does-not-exist} is @t{:error} (subject to the
  1585. constraints on the meaning of @i{if-does-not-exist} listed above),
  1586. an error of @i{type} @b{file-error} is signaled.
  1587. If it is impossible for an implementation to handle some option
  1588. in a manner close to what is specified here,
  1589. an error of @i{type} @b{error} might be signaled.
  1590. An error of @i{type} @b{file-error} is signaled if
  1591. @t{(wild-pathname-p @i{filespec})} returns true.
  1592. An error of @i{type} @b{error} is signaled if the @i{external-format}
  1593. is not understood by the @i{implementation}.
  1594. The various @i{file systems} in existence today have widely differing capabilities,
  1595. and some aspects of the @i{file system} are beyond the scope of this specification
  1596. to define. A given @i{implementation} might not be able to support all of these options
  1597. in exactly the manner stated. An @i{implementation} is required to recognize all of
  1598. these option keywords and to try to do something ``reasonable'' in the context of the
  1599. host @i{file system}. Where necessary to accomodate the @i{file system},
  1600. an @i{implementation} deviate slightly from the semantics specified here without
  1601. being disqualified for consideration as a @i{conforming implementation}.
  1602. If it is utterly impossible for an @i{implementation} to handle some option
  1603. in a manner similar to what is specified here, it may simply signal an error.
  1604. With regard to the @t{:element-type} option, if a @i{type} is
  1605. requested that is not supported by the @i{file system}, a substitution of types
  1606. such as that which goes on in @i{upgrading} is permissible. As a minimum
  1607. requirement, it should be the case that opening an @i{output} @i{stream}
  1608. to a @i{file} in a given @i{element type} and later opening
  1609. an @i{input} @i{stream} to the same @i{file} in the same @i{element type}
  1610. should work compatibly.
  1611. @subsubheading See Also::
  1612. @ref{with-open-file}
  1613. ,
  1614. @ref{close}
  1615. ,
  1616. @b{pathname},
  1617. @b{logical-pathname},
  1618. @ref{Merging Pathnames},
  1619. @ref{Pathnames as Filenames}
  1620. @subsubheading Notes::
  1621. @b{open} does not automatically close the file when an abnormal
  1622. exit occurs.
  1623. When @i{element-type} is a @i{subtype} of @b{character},
  1624. @b{read-char} and/or @b{write-char} can be
  1625. used on the resulting @i{file stream}.
  1626. When @i{element-type} is a @i{subtype} of @i{integer},
  1627. @b{read-byte} and/or @b{write-byte} can be used on the resulting @i{file stream}.
  1628. When @i{element-type} is @t{:default},
  1629. the @i{type} can be determined by using @b{stream-element-type}.
  1630. @node stream-external-format, with-open-file, open, Streams Dictionary
  1631. @subsection stream-external-format [Function]
  1632. @code{stream-external-format} @i{stream} @result{} @i{format}
  1633. @subsubheading Arguments and Values::
  1634. @i{stream}---a @i{file stream}.
  1635. @i{format}---an @i{external file format}.
  1636. @subsubheading Description::
  1637. Returns an @i{external file format designator} for the @i{stream}.
  1638. @subsubheading Examples::
  1639. @example
  1640. (with-open-file (stream "test" :direction :output)
  1641. (stream-external-format stream))
  1642. @result{} :DEFAULT
  1643. @i{OR}@result{} :ISO8859/1-1987
  1644. @i{OR}@result{} (:ASCII :SAIL)
  1645. @i{OR}@result{} ACME::PROPRIETARY-FILE-FORMAT-17
  1646. @i{OR}@result{} #<FILE-FORMAT :ISO646-1983 2343673>
  1647. @end example
  1648. @subsubheading See Also::
  1649. the @t{:external-format} @i{argument} to the @i{function}
  1650. @ref{open}
  1651. and
  1652. the
  1653. @ref{with-open-file}
  1654. @i{macro}.
  1655. @subsubheading Notes::
  1656. The @i{format} returned is not necessarily meaningful
  1657. to other @i{implementations}.
  1658. @node with-open-file, close, stream-external-format, Streams Dictionary
  1659. @subsection with-open-file [macro]
  1660. @subsubheading Syntax::
  1661. @code{with-open-file} @i{@r{(}stream filespec @{@i{options}@}{*}@r{)}
  1662. @{@i{declaration}@}{*}
  1663. @{@i{form}@}{*}}@*
  1664. @result{} @i{results}
  1665. @subsubheading Arguments and Values::
  1666. @i{stream} -- a variable.
  1667. @i{filespec}---a @i{pathname designator}.
  1668. @i{options} -- @i{forms}; evaluated.
  1669. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  1670. @i{forms}---an @i{implicit progn}.
  1671. @i{results}---the @i{values} returned by the @i{forms}.
  1672. @subsubheading Description::
  1673. @b{with-open-file} uses @b{open} to create a @i{file stream}
  1674. to @i{file} named by @i{filespec}.
  1675. @i{Filespec} is the name of the file to be opened.
  1676. @i{Options} are used as keyword arguments to @b{open}.
  1677. The @i{stream} @i{object} to which the @i{stream} @i{variable}
  1678. is @i{bound} has @i{dynamic extent};
  1679. its @i{extent} ends when the @i{form} is exited.
  1680. @b{with-open-file} evaluates the @i{forms} as an @i{implicit progn}
  1681. with @i{stream} bound to
  1682. the value returned by @b{open}.
  1683. When control leaves the body, either normally or abnormally (such as by
  1684. use of @b{throw}), the file is automatically closed. If a new
  1685. output file is being written, and control leaves abnormally, the file is
  1686. aborted and the file system is left, so far as possible, as if the file
  1687. had never been opened.
  1688. It is possible by the use of @t{:if-exists nil}
  1689. or @t{:if-does-not-exist nil} for
  1690. @i{stream} to be bound to @b{nil}.
  1691. Users of @t{:if-does-not-exist nil} should check for a valid @i{stream}.
  1692. The consequences are undefined if an attempt is made to @i{assign} the
  1693. @i{stream} @i{variable}. The compiler may choose to issue a
  1694. warning if such an attempt is detected.
  1695. @subsubheading Examples::
  1696. @example
  1697. (setq p (merge-pathnames "test"))
  1698. @result{} #<PATHNAME :HOST NIL :DEVICE @i{device-name} :DIRECTORY @i{directory-name}
  1699. :NAME "test" :TYPE NIL :VERSION :NEWEST>
  1700. (with-open-file (s p :direction :output :if-exists :supersede)
  1701. (format s "Here are a couple~
  1702. (with-open-file (s p)
  1703. (do ((l (read-line s) (read-line s nil 'eof)))
  1704. ((eq l 'eof) "Reached end of file.")
  1705. (format t "~&*** ~A~
  1706. @t{ |> } *** Here are a couple
  1707. @t{ |> } *** of test data lines
  1708. @result{} "Reached end of file."
  1709. @end example
  1710. @example
  1711. ;; Normally one would not do this intentionally because it is
  1712. ;; not perspicuous, but beware when using :IF-DOES-NOT-EXIST NIL
  1713. ;; that this doesn't happen to you accidentally...
  1714. (with-open-file (foo "no-such-file" :if-does-not-exist nil)
  1715. (read foo))
  1716. @t{ |> } @b{|>>}@t{hello?}@b{<<|}
  1717. @result{} HELLO? ;This value was read from the terminal, not a file!
  1718. ;; Here's another bug to avoid...
  1719. (with-open-file (foo "no-such-file" :direction :output :if-does-not-exist nil)
  1720. (format foo "Hello"))
  1721. @result{} "Hello" ;FORMAT got an argument of NIL!
  1722. @end example
  1723. @subsubheading Side Effects::
  1724. Creates a @i{stream} to the @i{file} named by @i{filename} (upon entry),
  1725. and closes the @i{stream} (upon exit).
  1726. In some @i{implementations},
  1727. the @i{file} might be locked in some way while it is open.
  1728. If the @i{stream} is an @i{output} @i{stream},
  1729. a @i{file} might be created.
  1730. @subsubheading Affected By::
  1731. The host computer's file system.
  1732. @subsubheading Exceptional Situations::
  1733. See the @i{function} @b{open}.
  1734. @subsubheading See Also::
  1735. @ref{open}
  1736. ,
  1737. @ref{close}
  1738. ,
  1739. @b{pathname},
  1740. @b{logical-pathname},
  1741. @ref{Pathnames as Filenames}
  1742. @node close, with-open-stream, with-open-file, Streams Dictionary
  1743. @subsection close [Function]
  1744. @code{close} @i{stream {&key} abort} @result{} @i{result}
  1745. @subsubheading Arguments and Values::
  1746. @i{stream}---a @i{stream} (either @i{open} or @i{closed}).
  1747. @i{abort}---a @i{generalized boolean}.
  1748. The default is @i{false}.
  1749. @i{result}---@b{t} if the @i{stream} was @i{open} at the time it was
  1750. received as an @i{argument},
  1751. or @i{implementation-dependent} otherwise.
  1752. @subsubheading Description::
  1753. @b{close} closes @i{stream}.
  1754. Closing a @i{stream} means
  1755. that it may no longer be used in input or output operations.
  1756. The act of @i{closing} a @i{file stream}
  1757. ends the association between the @i{stream} and its associated @i{file};
  1758. the transaction with the @i{file system} is terminated,
  1759. and input/output may no longer be performed on the @i{stream}.
  1760. If @i{abort} is @i{true}, an attempt is made to clean up any side
  1761. effects of having created @i{stream}.
  1762. If @i{stream} performs output to a file
  1763. that was created when the @i{stream} was created, the
  1764. file is deleted and any previously existing file is not superseded.
  1765. It is permissible to close an already closed @i{stream},
  1766. but in that case the @i{result} is @i{implementation-dependent}.
  1767. After @i{stream} is closed, it is still possible to perform
  1768. the following query operations upon it:
  1769. @b{streamp}, @b{pathname}, @b{truename},
  1770. @b{merge-pathnames}, @b{pathname-host}, @b{pathname-device},
  1771. @b{pathname-directory},@b{pathname-name},
  1772. @b{pathname-type}, @b{pathname-version}, @b{namestring},
  1773. @b{file-namestring}, @b{directory-namestring},
  1774. @b{host-namestring}, @b{enough-namestring}, @b{open},
  1775. @b{probe-file}, and @b{directory}.
  1776. The effect of @b{close} on a @i{constructed stream} is
  1777. to close the argument @i{stream} only.
  1778. There is no effect on the @i{constituents} of @i{composite streams}.
  1779. For a @i{stream} created with @b{make-string-output-stream},
  1780. the result of @b{get-output-stream-string} is unspecified after @b{close}.
  1781. @subsubheading Examples::
  1782. @example
  1783. (setq s (make-broadcast-stream)) @result{} #<BROADCAST-STREAM>
  1784. (close s) @result{} T
  1785. (output-stream-p s) @result{} @i{true}
  1786. @end example
  1787. @subsubheading Side Effects::
  1788. The @i{stream} is @i{closed} (if necessary).
  1789. If @i{abort} is @i{true} and the @i{stream} is
  1790. an @i{output} @i{file stream}, its associated @i{file}
  1791. might be deleted.
  1792. @subsubheading See Also::
  1793. @ref{open}
  1794. @node with-open-stream, listen, close, Streams Dictionary
  1795. @subsection with-open-stream [Macro]
  1796. @code{with-open-stream} @i{@r{(}var stream@r{)}
  1797. @{@i{declaration}@}{*}
  1798. @{@i{form}@}{*}}@*
  1799. @result{} @i{@{@i{result}@}{*}}
  1800. @subsubheading Arguments and Values::
  1801. @i{var}---a @i{variable} @i{name}.
  1802. @i{stream}---a @i{form}; evaluated to produce a @i{stream}.
  1803. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  1804. @i{forms}---an @i{implicit progn}.
  1805. @i{results}---the @i{values} returned by the @i{forms}.
  1806. @subsubheading Description::
  1807. @b{with-open-stream} performs a series of operations on
  1808. @i{stream}, returns a value, and then closes the @i{stream}.
  1809. @i{Var} is bound to the value of @i{stream},
  1810. and then @i{forms} are executed
  1811. as an @i{implicit progn}.
  1812. @i{stream}
  1813. is automatically closed on exit from @b{with-open-stream},
  1814. no matter whether the exit is normal or abnormal.
  1815. The @i{stream} has @i{dynamic extent};
  1816. its @i{extent} ends when the @i{form} is exited.
  1817. The consequences are undefined if an attempt is made to @i{assign} the
  1818. the @i{variable} @i{var} with the @i{forms}.
  1819. @subsubheading Examples::
  1820. @example
  1821. (with-open-stream (s (make-string-input-stream "1 2 3 4"))
  1822. (+ (read s) (read s) (read s))) @result{} 6
  1823. @end example
  1824. @subsubheading Side Effects::
  1825. The @i{stream} is closed (upon exit).
  1826. @subsubheading See Also::
  1827. @ref{close}
  1828. @node listen, clear-input, with-open-stream, Streams Dictionary
  1829. @subsection listen [Function]
  1830. @code{listen} @i{{&optional} input-stream} @result{} @i{generalized-boolean}
  1831. @subsubheading Arguments and Values::
  1832. @i{input-stream}---an @i{input} @i{stream designator}.
  1833. The default is @i{standard input}.
  1834. @i{generalized-boolean}---a @i{generalized boolean}.
  1835. @subsubheading Description::
  1836. Returns @i{true} if
  1837. there is a character immediately available from @i{input-stream};
  1838. otherwise, returns @i{false}.
  1839. On a non-interactive @i{input-stream},
  1840. @b{listen} returns @i{true} except when at @i{end of file}_1.
  1841. If an @i{end of file} is encountered, @b{listen} returns @i{false}.
  1842. @b{listen} is intended to be used
  1843. when @i{input-stream} obtains characters
  1844. from an interactive device such as a keyboard.
  1845. @subsubheading Examples::
  1846. @example
  1847. (progn (unread-char (read-char)) (list (listen) (read-char)))
  1848. @t{ |> } @b{|>>}@t{1}@b{<<|}
  1849. @result{} (T #\1)
  1850. (progn (clear-input) (listen))
  1851. @result{} NIL ;Unless you're a very fast typist!
  1852. @end example
  1853. @subsubheading Affected By::
  1854. @b{*standard-input*}
  1855. @subsubheading See Also::
  1856. @ref{interactive-stream-p}
  1857. ,
  1858. @ref{read-char-no-hang}
  1859. @node clear-input, finish-output, listen, Streams Dictionary
  1860. @subsection clear-input [Function]
  1861. @code{clear-input} @i{{&optional} input-stream} @result{} @i{@b{nil}}
  1862. @subsubheading Arguments and Values::
  1863. @i{input-stream}---an @i{input} @i{stream designator}.
  1864. The default is @i{standard input}.
  1865. @subsubheading Description::
  1866. Clears any available input from @i{input-stream}.
  1867. If @b{clear-input} does not make sense for @i{input-stream},
  1868. then @b{clear-input} does nothing.
  1869. @subsubheading Examples::
  1870. @example
  1871. ;; The exact I/O behavior of this example might vary from implementation
  1872. ;; to implementation depending on the kind of interactive buffering that
  1873. ;; occurs. (The call to SLEEP here is intended to help even out the
  1874. ;; differences in implementations which do not do line-at-a-time buffering.)
  1875. (defun read-sleepily (&optional (clear-p nil) (zzz 0))
  1876. (list (progn (print '>) (read))
  1877. ;; Note that input typed within the first ZZZ seconds
  1878. ;; will be discarded.
  1879. (progn (print '>)
  1880. (if zzz (sleep zzz))
  1881. (print '>>)
  1882. (if clear-p (clear-input))
  1883. (read))))
  1884. (read-sleepily)
  1885. @t{ |> } > @b{|>>}@t{10}@b{<<|}
  1886. @t{ |> } >
  1887. @t{ |> } >> @b{|>>}@t{20}@b{<<|}
  1888. @result{} (10 20)
  1889. (read-sleepily t)
  1890. @t{ |> } > @b{|>>}@t{10}@b{<<|}
  1891. @t{ |> } >
  1892. @t{ |> } >> @b{|>>}@t{20}@b{<<|}
  1893. @result{} (10 20)
  1894. (read-sleepily t 10)
  1895. @t{ |> } > @b{|>>}@t{10}@b{<<|}
  1896. @t{ |> } > @b{|>>}@t{20}@b{<<|} ; Some implementations won't echo typeahead here.
  1897. @t{ |> } >> @b{|>>}@t{30}@b{<<|}
  1898. @result{} (10 30)
  1899. @end example
  1900. @subsubheading Side Effects::
  1901. The @i{input-stream} is modified.
  1902. @subsubheading Affected By::
  1903. @b{*standard-input*}
  1904. @subsubheading Exceptional Situations::
  1905. Should signal an error of @i{type} @b{type-error}
  1906. if @i{input-stream} is not a @i{stream designator}.
  1907. @subsubheading See Also::
  1908. @b{clear-output}
  1909. @node finish-output, y-or-n-p, clear-input, Streams Dictionary
  1910. @subsection finish-output, force-output, clear-output [Function]
  1911. @code{finish-output} @i{{&optional} output-stream} @result{} @i{@b{nil}}
  1912. @code{force-output} @i{{&optional} output-stream} @result{} @i{@b{nil}}
  1913. @code{clear-output} @i{{&optional} output-stream} @result{} @i{@b{nil}}
  1914. @subsubheading Arguments and Values::
  1915. @i{output-stream}---an @i{output} @i{stream designator}.
  1916. The default is @i{standard output}.
  1917. @subsubheading Description::
  1918. @b{finish-output}, @b{force-output}, and @b{clear-output}
  1919. exercise control over the internal handling of buffered stream output.
  1920. @b{finish-output} attempts to ensure that any buffered output
  1921. sent to @i{output-stream} has reached its destination, and then returns.
  1922. @b{force-output} initiates the emptying of any
  1923. internal buffers but does not wait for completion
  1924. or acknowledgment to return.
  1925. @b{clear-output} attempts to abort any
  1926. outstanding output operation in progress in order
  1927. to allow as little output as possible
  1928. to continue to the destination.
  1929. If any of these operations does not make sense for @i{output-stream},
  1930. then it does nothing.
  1931. The precise actions of these @i{functions} are @i{implementation-dependent}.
  1932. @subsubheading Examples::
  1933. @example
  1934. ;; Implementation A
  1935. (progn (princ "am i seen?") (clear-output))
  1936. @result{} NIL
  1937. ;; Implementation B
  1938. (progn (princ "am i seen?") (clear-output))
  1939. @t{ |> } am i seen?
  1940. @result{} NIL
  1941. @end example
  1942. @subsubheading Affected By::
  1943. @b{*standard-output*}
  1944. @subsubheading Exceptional Situations::
  1945. Should signal an error of @i{type} @b{type-error}
  1946. if @i{output-stream} is not a @i{stream designator}.
  1947. @subsubheading See Also::
  1948. @ref{clear-input}
  1949. @node y-or-n-p, make-synonym-stream, finish-output, Streams Dictionary
  1950. @subsection y-or-n-p, yes-or-no-p [Function]
  1951. @code{y-or-n-p} @i{{&optional} control {&rest} arguments} @result{} @i{generalized-boolean}
  1952. @code{yes-or-no-p} @i{{&optional} control {&rest} arguments} @result{} @i{generalized-boolean}
  1953. @subsubheading Arguments and Values::
  1954. @i{control}---a @i{format control}.
  1955. @i{arguments}---@i{format arguments} for @i{control}.
  1956. @i{generalized-boolean}---a @i{generalized boolean}.
  1957. @subsubheading Description::
  1958. These functions ask a question and parse a response from the user.
  1959. They return @i{true} if the answer is affirmative,
  1960. or @i{false} if the answer is negative.
  1961. @b{y-or-n-p} is for asking the user a question whose answer is either
  1962. ``yes'' or ``no.''
  1963. It is intended that the reply require
  1964. the user to answer a yes-or-no question with a single
  1965. character.
  1966. @b{yes-or-no-p} is also for asking the user a question
  1967. whose answer is either ``Yes'' or ``No.''
  1968. It is intended that the reply require
  1969. the user to take more action than just a single keystroke, such as typing
  1970. the full word @t{yes} or @t{no} followed by a newline.
  1971. @b{y-or-n-p} types out a message (if supplied), reads an answer
  1972. in some @i{implementation-dependent} manner (intended to be short and simple,
  1973. such as reading a single character such as @t{Y} or @t{N}).
  1974. @b{yes-or-no-p} types out a message (if supplied),
  1975. attracts the user's attention (for example, by ringing
  1976. the terminal's bell),
  1977. and reads an answer
  1978. in some @i{implementation-dependent} manner (intended to be multiple characters,
  1979. such as @t{YES} or @t{NO}).
  1980. If @i{format-control} is supplied and not @b{nil},
  1981. then a @b{fresh-line} operation is performed; then
  1982. a message is printed as if @i{format-control} and @i{arguments}
  1983. were given to @b{format}.
  1984. In any case, @b{yes-or-no-p} and @b{y-or-n-p} will provide
  1985. a prompt such as ``@t{(Y or N)}'' or ``@t{(Yes or No)}'' if appropriate.
  1986. All input and output are performed using @i{query I/O}.
  1987. @subsubheading Examples::
  1988. @example
  1989. (y-or-n-p "(t or nil) given by")
  1990. @t{ |> } (t or nil) given by (Y or N) @b{|>>}@t{Y}@b{<<|}
  1991. @result{} @i{true}
  1992. (yes-or-no-p "a ~S message" 'frightening)
  1993. @t{ |> } a FRIGHTENING message (Yes or No) @b{|>>}@t{no}@b{<<|}
  1994. @result{} @i{false}
  1995. (y-or-n-p "Produce listing file?")
  1996. @t{ |> } Produce listing file?
  1997. @t{ |> } Please respond with Y or N. @b{|>>}@t{n}@b{<<|}
  1998. @result{} @i{false}
  1999. @end example
  2000. @subsubheading Side Effects::
  2001. Output to and input from @i{query I/O} will occur.
  2002. @subsubheading Affected By::
  2003. @b{*query-io*}.
  2004. @subsubheading See Also::
  2005. @ref{format}
  2006. @subsubheading Notes::
  2007. @b{yes-or-no-p} and @b{yes-or-no-p} do not add question marks
  2008. to the end of the prompt string, so any desired question mark or other
  2009. punctuation should be explicitly included in the text query.
  2010. @node make-synonym-stream, synonym-stream-symbol, y-or-n-p, Streams Dictionary
  2011. @subsection make-synonym-stream [Function]
  2012. @code{make-synonym-stream} @i{symbol} @result{} @i{synonym-stream}
  2013. @subsubheading Arguments and Values::
  2014. @i{symbol}---a @i{symbol} that names a @i{dynamic variable}.
  2015. @i{synonym-stream}---a @i{synonym stream}.
  2016. @subsubheading Description::
  2017. Returns a @i{synonym stream} whose @i{synonym stream symbol} is @i{symbol}.
  2018. @subsubheading Examples::
  2019. @example
  2020. (setq a-stream (make-string-input-stream "a-stream")
  2021. b-stream (make-string-input-stream "b-stream"))
  2022. @result{} #<String Input Stream>
  2023. (setq s-stream (make-synonym-stream 'c-stream))
  2024. @result{} #<SYNONYM-STREAM for C-STREAM>
  2025. (setq c-stream a-stream)
  2026. @result{} #<String Input Stream>
  2027. (read s-stream) @result{} A-STREAM
  2028. (setq c-stream b-stream)
  2029. @result{} #<String Input Stream>
  2030. (read s-stream) @result{} B-STREAM
  2031. @end example
  2032. @subsubheading Exceptional Situations::
  2033. Should signal @b{type-error} if its argument is not a @i{symbol}.
  2034. @subsubheading See Also::
  2035. @ref{Stream Concepts}
  2036. @node synonym-stream-symbol, broadcast-stream-streams, make-synonym-stream, Streams Dictionary
  2037. @subsection synonym-stream-symbol [Function]
  2038. @code{synonym-stream-symbol} @i{synonym-stream} @result{} @i{symbol}
  2039. @subsubheading Arguments and Values::
  2040. @i{synonym-stream}---a @i{synonym stream}.
  2041. @i{symbol}---a @i{symbol}.
  2042. @subsubheading Description::
  2043. Returns the @i{symbol} whose @b{symbol-value} the @i{synonym-stream} is using.
  2044. @subsubheading See Also::
  2045. @ref{make-synonym-stream}
  2046. @node broadcast-stream-streams, make-broadcast-stream, synonym-stream-symbol, Streams Dictionary
  2047. @subsection broadcast-stream-streams [Function]
  2048. @code{broadcast-stream-streams} @i{broadcast-stream} @result{} @i{streams}
  2049. @subsubheading Arguments and Values::
  2050. @i{broadcast-stream}---a @i{broadcast stream}.
  2051. @i{streams}---a @i{list} of @i{streams}.
  2052. @subsubheading Description::
  2053. Returns a @i{list} of output @i{streams} that constitute
  2054. all the @i{streams} to which the @i{broadcast-stream} is broadcasting.
  2055. @node make-broadcast-stream, make-two-way-stream, broadcast-stream-streams, Streams Dictionary
  2056. @subsection make-broadcast-stream [Function]
  2057. @code{make-broadcast-stream} @i{{&rest} streams} @result{} @i{broadcast-stream}
  2058. @subsubheading Arguments and Values::
  2059. @i{stream}---an @i{output} @i{stream}.
  2060. @i{broadcast-stream}---a @i{broadcast stream}.
  2061. @subsubheading Description::
  2062. Returns a @i{broadcast stream}.
  2063. @subsubheading Examples::
  2064. @example
  2065. (setq a-stream (make-string-output-stream)
  2066. b-stream (make-string-output-stream)) @result{} #<String Output Stream>
  2067. (format (make-broadcast-stream a-stream b-stream)
  2068. "this will go to both streams") @result{} NIL
  2069. (get-output-stream-string a-stream) @result{} "this will go to both streams"
  2070. (get-output-stream-string b-stream) @result{} "this will go to both streams"
  2071. @end example
  2072. @subsubheading Exceptional Situations::
  2073. Should signal an error of @i{type} @b{type-error}
  2074. if any @i{stream} is not an @i{output} @i{stream}.
  2075. @subsubheading See Also::
  2076. @ref{broadcast-stream-streams}
  2077. @node make-two-way-stream, two-way-stream-input-stream, make-broadcast-stream, Streams Dictionary
  2078. @subsection make-two-way-stream [Function]
  2079. @code{make-two-way-stream} @i{input-stream output-stream} @result{} @i{two-way-stream}
  2080. @subsubheading Arguments and Values::
  2081. @i{input-stream}---a @i{stream}.
  2082. @i{output-stream}---a @i{stream}.
  2083. @i{two-way-stream}---a @i{two-way stream}.
  2084. @subsubheading Description::
  2085. Returns a @i{two-way stream}
  2086. that gets its input from @i{input-stream}
  2087. and sends its output to @i{output-stream}.
  2088. @subsubheading Examples::
  2089. @example
  2090. (with-output-to-string (out)
  2091. (with-input-from-string (in "input...")
  2092. (let ((two (make-two-way-stream in out)))
  2093. (format two "output...")
  2094. (setq what-is-read (read two))))) @result{} "output..."
  2095. what-is-read @result{} INPUT...
  2096. @end example
  2097. @subsubheading Exceptional Situations::
  2098. Should signal an error of @i{type} @b{type-error}
  2099. if @i{input-stream} is not an @i{input} @i{stream}.
  2100. Should signal an error of @i{type} @b{type-error}
  2101. if @i{output-stream} is not an @i{output} @i{stream}.
  2102. @node two-way-stream-input-stream, echo-stream-input-stream, make-two-way-stream, Streams Dictionary
  2103. @subsection two-way-stream-input-stream, two-way-stream-output-stream
  2104. @flushright
  2105. @i{[Function]}
  2106. @end flushright
  2107. @code{two-way-stream-input-stream} @i{two-way-stream} @result{} @i{input-stream}
  2108. @code{two-way-stream-output-stream} @i{two-way-stream} @result{} @i{output-stream}
  2109. @subsubheading Arguments and Values::
  2110. @i{two-way-stream}---a @i{two-way stream}.
  2111. @i{input-stream}---an @i{input} @i{stream}.
  2112. @i{output-stream}---an @i{output} @i{stream}.
  2113. @subsubheading Description::
  2114. @b{two-way-stream-input-stream} returns the @i{stream}
  2115. from which @i{two-way-stream} receives input.
  2116. @b{two-way-stream-output-stream} returns the @i{stream}
  2117. to which @i{two-way-stream} sends output.
  2118. @node echo-stream-input-stream, make-echo-stream, two-way-stream-input-stream, Streams Dictionary
  2119. @subsection echo-stream-input-stream, echo-stream-output-stream [Function]
  2120. @code{echo-stream-input-stream} @i{echo-stream} @result{} @i{input-stream}
  2121. @code{echo-stream-output-stream} @i{echo-stream} @result{} @i{output-stream}
  2122. @subsubheading Arguments and Values::
  2123. @i{echo-stream}---an @i{echo stream}.
  2124. @i{input-stream}---an @i{input} @i{stream}.
  2125. @b{output-stream}---an @i{output} @i{stream}.
  2126. @subsubheading Description::
  2127. @b{echo-stream-input-stream} returns the @i{input} @i{stream}
  2128. from which @i{echo-stream} receives input.
  2129. @b{echo-stream-output-stream} returns the @i{output} @i{stream}
  2130. to which @i{echo-stream} sends output.
  2131. @node make-echo-stream, concatenated-stream-streams, echo-stream-input-stream, Streams Dictionary
  2132. @subsection make-echo-stream [Function]
  2133. @code{make-echo-stream} @i{input-stream output-stream} @result{} @i{echo-stream}
  2134. @subsubheading Arguments and Values::
  2135. @i{input-stream}---an @i{input} @i{stream}.
  2136. @i{output-stream}---an @i{output} @i{stream}.
  2137. @i{echo-stream}---an @i{echo stream}.
  2138. @subsubheading Description::
  2139. Creates and returns an @i{echo stream}
  2140. that takes input from @i{input-stream}
  2141. and sends output to @i{output-stream}.
  2142. @subsubheading Examples::
  2143. @example
  2144. (let ((out (make-string-output-stream)))
  2145. (with-open-stream
  2146. (s (make-echo-stream
  2147. (make-string-input-stream "this-is-read-and-echoed")
  2148. out))
  2149. (read s)
  2150. (format s " * this-is-direct-output")
  2151. (get-output-stream-string out)))
  2152. @result{} "this-is-read-and-echoed * this-is-direct-output"
  2153. @end example
  2154. @subsubheading See Also::
  2155. @ref{echo-stream-input-stream; echo-stream-output-stream}
  2156. ,
  2157. @b{echo-stream-output-stream},
  2158. @ref{make-two-way-stream}
  2159. @node concatenated-stream-streams, make-concatenated-stream, make-echo-stream, Streams Dictionary
  2160. @subsection concatenated-stream-streams [Function]
  2161. @code{concatenated-stream-streams} @i{concatenated-stream} @result{} @i{streams}
  2162. @subsubheading Arguments and Values::
  2163. @i{concatenated-stream} -- a @i{concatenated stream}.
  2164. @i{streams}---a @i{list} of @i{input} @i{streams}.
  2165. @subsubheading Description::
  2166. Returns a @i{list} of @i{input} @i{streams} that constitute the
  2167. ordered set of @i{streams} the @i{concatenated-stream} still
  2168. has to read from, starting with the current one it is reading from.
  2169. The list may be @i{empty} if no more @i{streams} remain to be read.
  2170. The consequences are undefined if the @i{list structure} of the @i{streams}
  2171. is ever modified.
  2172. @node make-concatenated-stream, get-output-stream-string, concatenated-stream-streams, Streams Dictionary
  2173. @subsection make-concatenated-stream [Function]
  2174. @code{make-concatenated-stream} @i{{&rest} input-streams} @result{} @i{concatenated-stream}
  2175. @subsubheading Arguments and Values::
  2176. @i{input-stream}---an @i{input} @i{stream}.
  2177. @i{concatenated-stream}---a @i{concatenated stream}.
  2178. @subsubheading Description::
  2179. Returns a @i{concatenated stream} that has the indicated @i{input-streams}
  2180. initially associated with it.
  2181. @subsubheading Examples::
  2182. @example
  2183. (read (make-concatenated-stream
  2184. (make-string-input-stream "1")
  2185. (make-string-input-stream "2"))) @result{} 12
  2186. @end example
  2187. @subsubheading Exceptional Situations::
  2188. Should signal @b{type-error} if any argument is not an @i{input} @i{stream}.
  2189. @subsubheading See Also::
  2190. @ref{concatenated-stream-streams}
  2191. @node get-output-stream-string, make-string-input-stream, make-concatenated-stream, Streams Dictionary
  2192. @subsection get-output-stream-string [Function]
  2193. @code{get-output-stream-string} @i{string-output-stream} @result{} @i{string}
  2194. @subsubheading Arguments and Values::
  2195. @i{string-output-stream}---a @i{stream}.
  2196. @i{string}---a @i{string}.
  2197. @subsubheading Description::
  2198. Returns a @i{string} containing, in order, all the @i{characters}
  2199. that have been output to @i{string-output-stream}.
  2200. This operation clears any @i{characters} on @i{string-output-stream},
  2201. so the @i{string} contains only those @i{characters} which have been output
  2202. since the last call to @b{get-output-stream-string}
  2203. or since the creation of the @i{string-output-stream},
  2204. whichever occurred most recently.
  2205. @subsubheading Examples::
  2206. @example
  2207. (setq a-stream (make-string-output-stream)
  2208. a-string "abcdefghijklm") @result{} "abcdefghijklm"
  2209. (write-string a-string a-stream) @result{} "abcdefghijklm"
  2210. (get-output-stream-string a-stream) @result{} "abcdefghijklm"
  2211. (get-output-stream-string a-stream) @result{} ""
  2212. @end example
  2213. @subsubheading Side Effects::
  2214. The @i{string-output-stream} is cleared.
  2215. @subsubheading Exceptional Situations::
  2216. The consequences are undefined if @i{stream-output-string} is @i{closed}.
  2217. The consequences are undefined if @i{string-output-stream} is a @i{stream} that
  2218. was not produced by @b{make-string-output-stream}.
  2219. The consequences are undefined if @i{string-output-stream} was
  2220. created implicitly by @b{with-output-to-string} or @b{format}.
  2221. @subsubheading See Also::
  2222. @ref{make-string-output-stream}
  2223. @node make-string-input-stream, make-string-output-stream, get-output-stream-string, Streams Dictionary
  2224. @subsection make-string-input-stream [Function]
  2225. @code{make-string-input-stream} @i{string {&optional} start end} @result{} @i{string-stream}
  2226. @subsubheading Arguments and Values::
  2227. @i{string}---a @i{string}.
  2228. @i{start}, @i{end}---@i{bounding index designators} of @i{string}.
  2229. The defaults for @i{start} and @i{end} are @t{0} and @b{nil}, respectively.
  2230. @i{string-stream}---an @i{input} @i{string stream}.
  2231. @subsubheading Description::
  2232. Returns an @i{input} @i{string stream}.
  2233. This @i{stream} will supply, in order, the @i{characters} in the substring
  2234. of @i{string} @i{bounded} by @i{start} and @i{end}.
  2235. After the last @i{character} has been supplied,
  2236. the @i{string stream} will then be at @i{end of file}.
  2237. @subsubheading Examples::
  2238. @example
  2239. (let ((string-stream (make-string-input-stream "1 one ")))
  2240. (list (read string-stream nil nil)
  2241. (read string-stream nil nil)
  2242. (read string-stream nil nil)))
  2243. @result{} (1 ONE NIL)
  2244. (read (make-string-input-stream "prefixtargetsuffix" 6 12)) @result{} TARGET
  2245. @end example
  2246. @subsubheading See Also::
  2247. @ref{with-input-from-string}
  2248. @node make-string-output-stream, with-input-from-string, make-string-input-stream, Streams Dictionary
  2249. @subsection make-string-output-stream [Function]
  2250. @code{make-string-output-stream} @i{{&key} element-type} @result{} @i{string-stream}
  2251. @subsubheading Arguments and Values::
  2252. @i{element-type}---a @i{type specifier}.
  2253. The default is @b{character}.
  2254. @i{string-stream}---an @i{output} @i{string stream}.
  2255. @subsubheading Description::
  2256. Returns
  2257. an @i{output} @i{string stream} that accepts @i{characters}
  2258. and makes available (via @b{get-output-stream-string})
  2259. a @i{string} that contains the @i{characters} that were actually output.
  2260. The @i{element-type} names the @i{type} of the @i{elements}
  2261. of the @i{string}; a @i{string} is constructed of the most specialized
  2262. @i{type} that can accommodate @i{elements} of that @i{element-type}.
  2263. @subsubheading Examples::
  2264. @example
  2265. (let ((s (make-string-output-stream)))
  2266. (write-string "testing... " s)
  2267. (prin1 1234 s)
  2268. (get-output-stream-string s))
  2269. @result{} "testing... 1234"
  2270. @end example
  2271. None..
  2272. @subsubheading See Also::
  2273. @ref{get-output-stream-string}
  2274. ,
  2275. @ref{with-output-to-string}
  2276. @node with-input-from-string, with-output-to-string, make-string-output-stream, Streams Dictionary
  2277. @subsection with-input-from-string [Macro]
  2278. @code{with-input-from-string} @i{@r{(}var string {&key} index start end@r{)}
  2279. @{@i{declaration}@}{*}
  2280. @{@i{form}@}{*}}@*
  2281. @result{} @i{@{@i{result}@}{*}}
  2282. @subsubheading Arguments and Values::
  2283. @i{var}---a @i{variable} @i{name}.
  2284. @i{string}---a @i{form}; evaluated to produce a @i{string}.
  2285. @i{index}---a @i{place}.
  2286. @i{start}, @i{end}---@i{bounding index designators} of @i{string}.
  2287. The defaults for @i{start} and @i{end} are @t{0} and @b{nil}, respectively.
  2288. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  2289. @i{forms}---an @i{implicit progn}.
  2290. @i{result}---the @i{values} returned by the @i{forms}.
  2291. @subsubheading Description::
  2292. Creates an
  2293. @i{input} @i{string stream},
  2294. provides an opportunity to perform operations on the @i{stream}
  2295. (returning zero or more @i{values}),
  2296. and then closes the @i{string stream}.
  2297. @i{String} is evaluated first, and @i{var} is bound to
  2298. a character @i{input} @i{string stream} that supplies
  2299. @i{characters} from the subsequence of the resulting @i{string} @i{bounded} by
  2300. @i{start} and @i{end}.
  2301. The body is executed as an @i{implicit progn}.
  2302. The @i{input} @i{string stream} is automatically closed on exit from
  2303. @b{with-input-from-string}, no matter whether the exit is normal or abnormal.
  2304. The @i{input} @i{string stream} to which the @i{variable} @i{var}
  2305. is @i{bound} has @i{dynamic extent};
  2306. its @i{extent} ends when the @i{form} is exited.
  2307. The @i{index} is a pointer within the @i{string} to be advanced.
  2308. If @b{with-input-from-string}
  2309. is exited normally, then @i{index} will have
  2310. as its @i{value}
  2311. the index into the @i{string} indicating the first character not read
  2312. which is @t{(length @i{string})} if all characters were used.
  2313. The place specified by @i{index}
  2314. is not updated as reading progresses, but only at the
  2315. end of the operation.
  2316. @i{start} and @i{index} may both specify the same variable,
  2317. which is a pointer within the @i{string} to be advanced,
  2318. perhaps repeatedly by some containing loop.
  2319. The consequences are undefined if an attempt is made to @i{assign}
  2320. the @i{variable} @i{var}.
  2321. @subsubheading Examples::
  2322. @example
  2323. (with-input-from-string (s "XXX1 2 3 4xxx"
  2324. :index ind
  2325. :start 3 :end 10)
  2326. (+ (read s) (read s) (read s))) @result{} 6
  2327. ind @result{} 9
  2328. (with-input-from-string (s "Animal Crackers" :index j :start 6)
  2329. (read s)) @result{} CRACKERS
  2330. @end example
  2331. The variable @t{j} is set to @t{15}.
  2332. @subsubheading Side Effects::
  2333. The @i{value} of the @i{place} named by @i{index}, if any, is modified.
  2334. @subsubheading See Also::
  2335. @ref{make-string-input-stream}
  2336. ,
  2337. @ref{Traversal Rules and Side Effects}
  2338. @node with-output-to-string, *debug-io*, with-input-from-string, Streams Dictionary
  2339. @subsection with-output-to-string [Macro]
  2340. @code{with-output-to-string} @i{@r{(}var {&optional} string-form {&key} element-type@r{)}
  2341. @{@i{declaration}@}{*}
  2342. @{@i{form}@}{*}}@*
  2343. @result{} @i{@{@i{result}@}{*}}
  2344. @subsubheading Arguments and Values::
  2345. @i{var}---a @i{variable} @i{name}.
  2346. @i{string-form}---a @i{form} or @b{nil};
  2347. if @i{non-nil}, evaluated to produce @i{string}.
  2348. @i{string}---a @i{string} that has a @i{fill pointer}.
  2349. @i{element-type}---a @i{type specifier}; evaluated.
  2350. The default is @b{character}.
  2351. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  2352. @i{forms}---an @i{implicit progn}.
  2353. @i{results}---If a @i{string-form} is not supplied or @b{nil},
  2354. a @i{string}; otherwise,
  2355. the @i{values} returned by the @i{forms}.
  2356. @subsubheading Description::
  2357. @b{with-output-to-string} creates a
  2358. character @i{output} @i{stream}, performs a series of operations
  2359. that may send results to this @i{stream}, and then closes the @i{stream}.
  2360. The @i{element-type} names the @i{type} of the elements
  2361. of the @i{stream}; a @i{stream} is constructed of the most specialized
  2362. @i{type} that can accommodate elements of the given @i{type}.
  2363. The body is executed as an @i{implicit progn} with @i{var}
  2364. bound to an @i{output} @i{string stream}.
  2365. All output to that @i{string stream} is saved in a @i{string}.
  2366. If @i{string} is supplied, @i{element-type} is ignored,
  2367. and the output is incrementally appended to @i{string} as
  2368. if by use of @b{vector-push-extend}.
  2369. The @i{output} @i{stream}
  2370. is automatically closed on exit from @b{with-output-from-string},
  2371. no matter whether the exit is normal or abnormal.
  2372. The @i{output} @i{string stream} to which the @i{variable} @i{var}
  2373. is @i{bound} has @i{dynamic extent};
  2374. its @i{extent} ends when the @i{form} is exited.
  2375. If no @i{string} is provided, then @b{with-output-from-string}
  2376. produces a @i{stream} that accepts characters and returns a @i{string}
  2377. of the indicated @i{element-type}.
  2378. If @i{string} is provided,
  2379. @b{with-output-to-string} returns the results of evaluating the last @i{form}.
  2380. The consequences are undefined if an attempt is made to @i{assign}
  2381. the @i{variable} @i{var}.
  2382. @subsubheading Examples::
  2383. @example
  2384. (setq fstr (make-array '(0) :element-type 'base-char
  2385. :fill-pointer 0 :adjustable t)) @result{} ""
  2386. (with-output-to-string (s fstr)
  2387. (format s "here's some output")
  2388. (input-stream-p s)) @result{} @i{false}
  2389. fstr @result{} "here's some output"
  2390. @end example
  2391. @subsubheading Side Effects::
  2392. The @i{string} is modified.
  2393. @subsubheading Exceptional Situations::
  2394. The consequences are undefined if destructive modifications are performed
  2395. directly on the @i{string} during the @i{dynamic extent} of the call.
  2396. @subsubheading See Also::
  2397. @ref{make-string-output-stream}
  2398. ,
  2399. @b{vector-push-extend},
  2400. @ref{Traversal Rules and Side Effects}
  2401. @node *debug-io*, *terminal-io*, with-output-to-string, Streams Dictionary
  2402. @subsection *debug-io*, *error-output*, *query-io*,
  2403. @subheading *standard-input*, *standard-output*,
  2404. @subheading *trace-output*
  2405. @flushright
  2406. @i{[Variable]}
  2407. @end flushright
  2408. @subsubheading Value Type::
  2409. For @b{*standard-input*}:
  2410. an @i{input} @i{stream}
  2411. For @b{*error-output*}, @b{*standard-output*}, and @b{*trace-output*}:
  2412. an @i{output} @i{stream}.
  2413. For @b{*debug-io*}, @b{*query-io*}:
  2414. a @i{bidirectional} @i{stream}.
  2415. @subsubheading Initial Value::
  2416. @i{implementation-dependent}, but
  2417. it must be an @i{open} @i{stream}
  2418. that is not a @i{generalized synonym stream}
  2419. to an @i{I/O customization variables}
  2420. but that might be a @i{generalized synonym stream} to
  2421. the value of some @i{I/O customization variable}.
  2422. The initial value might also be a @i{generalized synonym stream}
  2423. to either the @i{symbol} @b{*terminal-io*} or to the @i{stream}
  2424. that is its @i{value}.
  2425. @subsubheading Description::
  2426. These @i{variables} are collectively called the
  2427. @i{standardized} @i{I/O customization variables}.
  2428. They can be @i{bound} or @i{assigned} in order to
  2429. change the default destinations for input and/or output
  2430. used by various @i{standardized} @i{operators} and facilities.
  2431. The @i{value} of @b{*debug-io*}, called @i{debug I/O},
  2432. is a @i{stream} to be used for interactive debugging purposes.
  2433. The @i{value} of @b{*error-output*}, called @i{error output},
  2434. is a @i{stream} to which warnings and non-interactive error messages should be sent.
  2435. The @i{value} of @b{*query-io*}, called @i{query I/O},
  2436. is a @i{bidirectional} @i{stream}
  2437. to be used when asking questions of the user. The question should be output
  2438. to this @i{stream}, and the answer read from it.
  2439. The @i{value} of @b{*standard-input*}, called @i{standard input},
  2440. is a @i{stream} that is used by many @i{operators}
  2441. as a default source of input when no specific @i{input} @i{stream}
  2442. is explicitly supplied.
  2443. The @i{value} of @b{*standard-output*}, called @i{standard output},
  2444. is a @i{stream} that is used by many @i{operators}
  2445. as a default destination for output when no specific @i{output} @i{stream}
  2446. is explicitly supplied.
  2447. The @i{value} of @b{*trace-output*}, called @i{trace output},
  2448. is the @i{stream} on which traced functions (see @b{trace})
  2449. and the @b{time} @i{macro} print their output.
  2450. @subsubheading Examples::
  2451. @example
  2452. (with-output-to-string (*error-output*)
  2453. (warn "this string is sent to *error-output*"))
  2454. @result{} "Warning: this string is sent to *error-output*
  2455. " ;The exact format of this string is @i{implementation-dependent}.
  2456. (with-input-from-string (*standard-input* "1001")
  2457. (+ 990 (read))) @result{} 1991
  2458. (progn (setq out (with-output-to-string (*standard-output*)
  2459. (print "print and format t send things to")
  2460. (format t "*standard-output* now going to a string")))
  2461. :done)
  2462. @result{} :DONE
  2463. out
  2464. @result{} "
  2465. \"print and format t send things to\" *standard-output* now going to a string"
  2466. (defun fact (n) (if (< n 2) 1 (* n (fact (- n 1)))))
  2467. @result{} FACT
  2468. (trace fact)
  2469. @result{} (FACT)
  2470. ;; Of course, the format of traced output is implementation-dependent.
  2471. (with-output-to-string (*trace-output*)
  2472. (fact 3))
  2473. @result{} "
  2474. 1 Enter FACT 3
  2475. | 2 Enter FACT 2
  2476. | 3 Enter FACT 1
  2477. | 3 Exit FACT 1
  2478. | 2 Exit FACT 2
  2479. 1 Exit FACT 6"
  2480. @end example
  2481. @subsubheading See Also::
  2482. @b{*terminal-io*},
  2483. @b{synonym-stream},
  2484. @ref{Time}
  2485. ,
  2486. @ref{trace; untrace}
  2487. ,
  2488. {@ref{Conditions}},
  2489. {@ref{Reader}},
  2490. {@ref{Printer}}
  2491. @subsubheading Notes::
  2492. The intent of the constraints on the initial @i{value}
  2493. of the @i{I/O customization variables} is to ensure that it
  2494. is always safe to @i{bind} or @i{assign} such a @i{variable} to
  2495. the @i{value} of another @i{I/O customization variable}, without
  2496. unduly restricting @i{implementation} flexibility.
  2497. It is common for an @i{implementation} to make
  2498. the initial @i{values} of @b{*debug-io*} and @b{*query-io*}
  2499. be the @i{same} @i{stream},
  2500. and to make
  2501. the initial @i{values} of @b{*error-output*} and @b{*standard-output*}
  2502. be the @i{same} @i{stream}.
  2503. The functions @b{y-or-n-p} and @b{yes-or-no-p} use @i{query I/O}
  2504. for their input and output.
  2505. In the normal @i{Lisp read-eval-print loop},
  2506. input is read from @i{standard input}.
  2507. Many input functions, including @b{read} and @b{read-char},
  2508. take a @i{stream} argument that defaults to @i{standard input}.
  2509. In the normal @i{Lisp read-eval-print loop}, output is sent to @i{standard output}.
  2510. Many output functions, including @b{print} and @b{write-char},
  2511. take a @i{stream} argument that defaults to @i{standard output}.
  2512. A program that wants, for example, to divert output to a file should do so by
  2513. @i{binding} @b{*standard-output*}; that way error messages sent to
  2514. @b{*error-output*} can still get to the user by going through
  2515. @b{*terminal-io*} (if @b{*error-output*} is bound to @b{*terminal-io*}),
  2516. which is usually what is desired.
  2517. @node *terminal-io*, stream-error, *debug-io*, Streams Dictionary
  2518. @subsection *terminal-io* [Variable]
  2519. @subsubheading Value Type::
  2520. a @i{bidirectional} @i{stream}.
  2521. @subsubheading Initial Value::
  2522. @i{implementation-dependent}, but
  2523. it must be an @i{open} @i{stream}
  2524. that is not a @i{generalized synonym stream}
  2525. to an @i{I/O customization variables}
  2526. but that might be a @i{generalized synonym stream} to
  2527. the @i{value} of some @i{I/O customization variable}.
  2528. @subsubheading Description::
  2529. The @i{value} of @b{*terminal-io*}, called @i{terminal I/O}, is ordinarily
  2530. a @i{bidirectional} @i{stream} that connects to the user's console.
  2531. Typically, writing to this @i{stream}
  2532. would cause the output to appear
  2533. on a display screen, for example, and reading from the @i{stream} would
  2534. accept input from a keyboard. It is intended
  2535. that standard input functions such as @b{read} and @b{read-char},
  2536. when used with this @i{stream}, cause echoing of the input
  2537. into the output side of the @i{stream}. The means by which this is
  2538. accomplished are @i{implementation-dependent}.
  2539. The effect of changing the @i{value} of @b{*terminal-io*},
  2540. either by @i{binding} or @i{assignment},
  2541. is @i{implementation-defined}.
  2542. @subsubheading Examples::
  2543. @example
  2544. (progn (prin1 'foo) (prin1 'bar *terminal-io*))
  2545. @t{ |> } FOOBAR
  2546. @result{} BAR
  2547. (with-output-to-string (*standard-output*)
  2548. (prin1 'foo)
  2549. (prin1 'bar *terminal-io*))
  2550. @t{ |> } BAR
  2551. @result{} "FOO"
  2552. @end example
  2553. @subsubheading See Also::
  2554. @b{*debug-io*},
  2555. @b{*error-output*},
  2556. @b{*query-io*},
  2557. @b{*standard-input*},
  2558. @b{*standard-output*},
  2559. @b{*trace-output*}
  2560. @node stream-error, stream-error-stream, *terminal-io*, Streams Dictionary
  2561. @subsection stream-error [Condition Type]
  2562. @subsubheading Class Precedence List::
  2563. @b{stream-error},
  2564. @b{error},
  2565. @b{serious-condition},
  2566. @b{condition},
  2567. @b{t}
  2568. @subsubheading Description::
  2569. The @i{type} @b{stream-error}
  2570. consists of error conditions that are related to receiving input from
  2571. or sending output to a @i{stream}.
  2572. The ``offending stream'' is initialized by
  2573. the @t{:stream} initialization argument to @b{make-condition},
  2574. and is @i{accessed} by the @i{function} @b{stream-error-stream}.
  2575. @subsubheading See Also::
  2576. @ref{stream-error-stream}
  2577. @node stream-error-stream, end-of-file, stream-error, Streams Dictionary
  2578. @subsection stream-error-stream [Function]
  2579. @code{stream-error-stream} @i{condition} @result{} @i{stream}
  2580. @subsubheading Arguments and Values::
  2581. @i{condition}---a @i{condition} of @i{type} @b{stream-error}.
  2582. @i{stream}---a @i{stream}.
  2583. @subsubheading Description::
  2584. Returns the offending @i{stream} of a @i{condition} of @i{type} @b{stream-error}.
  2585. @subsubheading Examples::
  2586. @example
  2587. (with-input-from-string (s "(FOO")
  2588. (handler-case (read s)
  2589. (end-of-file (c)
  2590. (format nil "~&End of file on ~S." (stream-error-stream c)))))
  2591. "End of file on #<String Stream>."
  2592. @end example
  2593. @subsubheading See Also::
  2594. @b{stream-error},
  2595. @ref{Conditions}
  2596. @node end-of-file, , stream-error-stream, Streams Dictionary
  2597. @subsection end-of-file [Condition Type]
  2598. @subsubheading Class Precedence List::
  2599. @b{end-of-file},
  2600. @b{stream-error},
  2601. @b{error},
  2602. @b{serious-condition},
  2603. @b{condition},
  2604. @b{t}
  2605. @subsubheading Description::
  2606. The @i{type} @b{end-of-file} consists of
  2607. error conditions related to read operations that are done on
  2608. @i{streams} that have no more data.
  2609. @subsubheading See Also::
  2610. @ref{stream-error-stream}
  2611. @c end of including dict-streams
  2612. @c %**end of chapter