cccp.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040
  1. /* C Compatible Compiler Preprocessor (CCCP)
  2. Copyright (C) 1986, 1987, Free Software Foundation, Inc.
  3. Written by Paul Rubin, June 1986
  4. Adapted to ANSI C, Richard Stallman, Jan 1987
  5. NO WARRANTY
  6. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  17. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  18. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  19. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  20. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  21. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  22. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  23. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  24. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  25. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  26. GENERAL PUBLIC LICENSE TO COPY
  27. 1. You may copy and distribute verbatim copies of this source file
  28. as you receive it, in any medium, provided that you conspicuously
  29. and appropriately publish on each copy a valid copyright notice
  30. "Copyright (C) 1987, Free Software Foundation"; and include
  31. following the copyright notice a verbatim copy of the above disclaimer
  32. of warranty and of this License. You may charge a distribution fee for the
  33. physical act of transferring a copy.
  34. 2. You may modify your copy or copies of this source file or
  35. any portion of it, and copy and distribute such modifications under
  36. the terms of Paragraph 1 above, provided that you also do the following:
  37. a) cause the modified files to carry prominent notices stating
  38. that you changed the files and the date of any change; and
  39. b) cause the whole of any work that you distribute or publish,
  40. that in whole or in part contains or is a derivative of this
  41. program or any part thereof, to be licensed at no charge to all
  42. third parties on terms identical to those contained in this
  43. License Agreement (except that you may choose to grant more
  44. extensive warranty protection to third parties, at your option).
  45. c) You may charge a distribution fee for the physical act of
  46. transferring a copy, and you may at your option offer warranty
  47. protection in exchange for a fee.
  48. 3. You may copy and distribute this program or any portion of it in
  49. compiled, executable or object code form under the terms of Paragraphs
  50. 1 and 2 above provided that you do the following:
  51. a) cause each such copy to be accompanied by the
  52. corresponding machine-readable source code, which must
  53. be distributed under the terms of Paragraphs 1 and 2 above; or,
  54. b) cause each such copy to be accompanied by a
  55. written offer, with no time limit, to give any third party
  56. free (except for a nominal shipping charge) a machine readable
  57. copy of the corresponding source code, to be distributed
  58. under the terms of Paragraphs 1 and 2 above; or,
  59. c) in the case of a recipient of this program in compiled, executable
  60. or object code form (without the corresponding source code) you
  61. shall cause copies you distribute to be accompanied by a copy
  62. of the written offer of source code which you received along
  63. with the copy you received.
  64. 4. You may not copy, sublicense, distribute or transfer this program
  65. except as expressly provided under this License Agreement. Any attempt
  66. otherwise to copy, sublicense, distribute or transfer this program is void and
  67. your rights to use the program under this License agreement shall be
  68. automatically terminated. However, parties who have received computer
  69. software programs from you with this License Agreement will not have
  70. their licenses terminated so long as such parties remain in full compliance.
  71. 5. If you wish to incorporate parts of this program into other free
  72. programs whose distribution conditions are different, write to the Free
  73. Software Foundation at 1000 Mass Ave, Cambridge, MA 02138. We have not yet
  74. worked out a simple rule that can be stated here, but we will often permit
  75. this. We will be guided by the two goals of preserving the free status of
  76. all derivatives our free software and of promoting the sharing and reuse of
  77. software.
  78. In other words, you are welcome to use, share and improve this program.
  79. You are forbidden to forbid anyone else to use, share and improve
  80. what you give them. Help stamp out software-hoarding! */
  81. typedef unsigned char U_CHAR;
  82. #ifdef EMACS
  83. #define NO_SHORTNAMES
  84. #include "../src/config.h"
  85. #ifdef open
  86. #undef open
  87. #undef read
  88. #undef write
  89. #endif /* open */
  90. #endif /* EMACS */
  91. #include <sys/types.h>
  92. #include <sys/stat.h>
  93. #include <sys/file.h>
  94. #include <ctype.h>
  95. #include <stdio.h>
  96. #ifndef USG
  97. #include <sys/time.h> /* for __DATE__ and __TIME__ */
  98. #else
  99. #define index strchr
  100. #define rindex strrchr
  101. #include <time.h>
  102. #include <fcntl.h>
  103. #endif /* USG */
  104. /* External declarations. */
  105. void bcopy (), bzero ();
  106. int bcmp ();
  107. /* Forward declarations. */
  108. int do_define (), do_line (), do_include (), do_undef (), do_error (),
  109. do_pragma (), do_if (), do_xifdef (), do_else (),
  110. do_elif (), do_endif ();
  111. struct hashnode *install ();
  112. struct hashnode *lookup ();
  113. char *xmalloc (), *xrealloc (), *xcalloc ();
  114. void fatal (), pfatal_with_name (), perror_with_name ();
  115. U_CHAR *grow_outbuf ();
  116. int handle_directive ();
  117. U_CHAR *macarg1 ();
  118. char *macarg ();
  119. U_CHAR *skip_to_end_of_comment ();
  120. U_CHAR *skip_quoted_string ();
  121. #define FATAL_EXIT_CODE 33 /* gnu cc command understands this */
  122. /* Name under which this program was invoked. */
  123. char *progname;
  124. /* Maximum length of directory names in the search path
  125. for include files. */
  126. int max_include_len = 14;
  127. /* Nonzero means copy comments into the output file. */
  128. int put_out_comments = 0;
  129. /* Nonzero means don't process the ANSI trigraph sequences. */
  130. int no_trigraphs = 0;
  131. /* Nonzero means don't output line number information. */
  132. int no_line_commands;
  133. /* Nonzero means inhibit output of the preprocessed text
  134. and instead output the definitions of all user-defined macros
  135. in a form suitable for use as input to cccp. */
  136. int dump_macros;
  137. /* Nonzero means give all the error messages the ANSI standard requires. */
  138. int pedantic;
  139. /* Nonzero causes output not to be done,
  140. but directives such as #define that have side effects
  141. are still obeyed. */
  142. int no_output;
  143. /* I/O buffer structure.
  144. The `fname' field is nonzero for source files and #include files
  145. and for the dummy text used for -D and -U.
  146. It is zero for rescanning results of macro expansion
  147. and for expanding macro arguments. */
  148. #define INPUT_STACK_MAX 100
  149. struct file_buf {
  150. char *fname;
  151. int lineno;
  152. int length;
  153. U_CHAR *buf;
  154. U_CHAR *bufp;
  155. /* Macro that this level is the expansion of.
  156. Included so that we can reenable the macro
  157. at the end of this level. */
  158. struct hashnode *macro;
  159. /* Object to be freed at end of input at this level. */
  160. U_CHAR *free;
  161. } instack[INPUT_STACK_MAX];
  162. /* Current nesting level of input sources.
  163. `instack[indepth]' is the level currently being read. */
  164. int indepth = -1;
  165. typedef struct file_buf FILE_BUF;
  166. /* The output buffer. Its LENGTH field is the amount of room allocated
  167. for the buffer, not the number of chars actually present. To get
  168. that, subtract outbuf.buf from outbuf.bufp. */
  169. #define OUTBUF_SIZE 10 /* initial size of output buffer */
  170. FILE_BUF outbuf;
  171. /* Grow output buffer OBUF points at
  172. so it can hold at least NEEDED more chars. */
  173. #define check_expand(OBUF, NEEDED) \
  174. (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
  175. ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  176. struct directory_stack
  177. {
  178. struct directory_stack *next;
  179. char *fname;
  180. };
  181. /* #include "file" starts with the first entry in the stack */
  182. /* #include <file> starts with the second. */
  183. /* -I directories are added after the first */
  184. struct directory_stack default_includes[2] =
  185. {
  186. { &default_includes[1], "." },
  187. { 0, "/usr/include" }
  188. };
  189. struct directory_stack *include = &default_includes[0];
  190. /* Structure allocated for every #define. For a simple replacement
  191. such as
  192. #define foo bar ,
  193. nargs = -1, the `pattern' list is null, and the expansion is just
  194. the replacement text. Nargs = 0 means a functionlike macro with no args,
  195. e.g.,
  196. #define getchar() getc (stdin) .
  197. When there are args, the expansion is the replacement text with the
  198. args squashed out, and the reflist is a list describing how to
  199. build the output from the input: e.g., "3 chars, then the 1st arg,
  200. then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  201. The chars here come from the expansion. Whatever is left of the
  202. expansion after the last arg-occurrence is copied after that arg.
  203. Note that the reflist can be arbitrarily long---
  204. its length depends on the number of times the arguments appear in
  205. the replacement text, not how many args there are. Example:
  206. #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  207. pattern list
  208. { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  209. where (x, y) means (nchars, argno). */
  210. typedef struct definition DEFINITION;
  211. struct definition {
  212. int nargs;
  213. int length; /* length of expansion string */
  214. U_CHAR *expansion;
  215. struct reflist {
  216. struct reflist *next;
  217. char stringify; /* nonzero if this arg was preceded by a
  218. # operator. */
  219. char raw_before; /* Nonzero if a ## operator before arg. */
  220. char raw_after; /* Nonzero if a ## operator after arg. */
  221. int nchars; /* Number of literal chars to copy before
  222. this arg occurrence. */
  223. int argno; /* Number of arg to substitute (origin-0) */
  224. } *pattern;
  225. /* Names of macro args, concatenated in reverse order
  226. with comma-space between them.
  227. The only use of this is that we warn on redefinition
  228. if this differs between the old and new definitions. */
  229. U_CHAR *argnames;
  230. };
  231. /* different kinds of things that can appear in the value field
  232. of a hash node. Actually, this may be useless now. */
  233. union hashval {
  234. int ival;
  235. char *cpval;
  236. DEFINITION *defn;
  237. };
  238. /* The structure of a node in the hash table. The hash table
  239. has entries for all tokens defined by #define commands (type T_MACRO),
  240. plus some special tokens like __LINE__ (these each have their own
  241. type, and the appropriate code is run when that type of node is seen.
  242. It does not contain control words like "#define", which are recognized
  243. by a separate piece of code. */
  244. /* different flavors of hash nodes --- also used in keyword table */
  245. enum node_type {
  246. T_DEFINE = 1, /* the "#define" keyword */
  247. T_INCLUDE, /* the "#include" keyword */
  248. T_IFDEF, /* the "#ifdef" keyword */
  249. T_IFNDEF, /* the "#ifndef" keyword */
  250. T_IF, /* the "#if" keyword */
  251. T_ELSE, /* "#else" */
  252. T_PRAGMA, /* "#pragma" */
  253. T_ELIF, /* "#else" */
  254. T_UNDEF, /* "#undef" */
  255. T_LINE, /* "#line" */
  256. T_ERROR, /* "#error" */
  257. T_ENDIF, /* "#endif" */
  258. T_SPECLINE, /* special symbol "__LINE__" */
  259. T_DATE, /* "__DATE__" */
  260. T_FILE, /* "__FILE__" */
  261. T_TIME, /* "__TIME__" */
  262. T_CONST, /* Constant value, used by "__STDC__" */
  263. T_MACRO, /* macro defined by "#define" */
  264. T_DISABLED, /* macro temporarily turned off for rescan */
  265. T_SPEC_DEFINED /* special `defined' macro for use in #if statements */
  266. };
  267. struct hashnode {
  268. struct hashnode *next; /* double links for easy deletion */
  269. struct hashnode *prev;
  270. struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
  271. chain is kept, in case the node is the head
  272. of the chain and gets deleted. */
  273. enum node_type type; /* type of special token */
  274. int length; /* length of token, for quick comparison */
  275. U_CHAR *name; /* the actual name */
  276. union hashval value; /* pointer to expansion, or whatever */
  277. };
  278. typedef struct hashnode HASHNODE;
  279. /* Some definitions for the hash table. The hash function MUST be
  280. computed as shown in hashf () below. That is because the rescan
  281. loop computes the hash value `on the fly' for most tokens,
  282. in order to avoid the overhead of a lot of procedure calls to
  283. the hashf () function. Hashf () only exists for the sake of
  284. politeness, for use when speed isn't so important. */
  285. #define HASHSIZE 1403
  286. HASHNODE *hashtab[HASHSIZE];
  287. #define HASHSTEP(old, c) ((old << 2) + c)
  288. #define MAKE_POS(v) (v & ~0x80000000) /* make number positive */
  289. /* Symbols to predefine. */
  290. #ifdef PREDEFS
  291. char *predefs = PREDEFS;
  292. #else
  293. char *predefs = "";
  294. #endif
  295. /* `struct directive' defines one #-directive, including how to handle it. */
  296. struct directive {
  297. int length; /* Length of name */
  298. int (*func)(); /* Function to handle directive */
  299. char *name; /* Name of directive */
  300. enum node_type type; /* Code which describes which directive. */
  301. };
  302. /* Here is the actual list of #-directives, most-often-used first. */
  303. struct directive directive_table[] = {
  304. { 6, do_define, "define", T_DEFINE},
  305. { 2, do_if, "if", T_IF},
  306. { 5, do_xifdef, "ifdef", T_IFDEF},
  307. { 6, do_xifdef, "ifndef", T_IFNDEF},
  308. { 5, do_endif, "endif", T_ENDIF},
  309. { 4, do_else, "else", T_ELSE},
  310. { 4, do_elif, "elif", T_ELIF},
  311. { 4, do_line, "line", T_LINE},
  312. { 7, do_include, "include", T_INCLUDE},
  313. { 5, do_undef, "undef", T_UNDEF},
  314. { 5, do_error, "error", T_ERROR},
  315. { 6, do_pragma, "pragma", T_PRAGMA},
  316. { -1, 0, "", (enum node_type) -1},
  317. };
  318. /* table to tell if char can be part of a C identifier. */
  319. U_CHAR is_idchar[256];
  320. /* table to tell if char can be first char of a c identifier. */
  321. U_CHAR is_idstart[256];
  322. /* table to tell if c is horizontal space. isspace () thinks that
  323. newline is space; this is not a good idea for this program. */
  324. U_CHAR is_hor_space[256];
  325. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  326. #define SKIP_ALL_WHITE_SPACE(p) do { while (isspace (*p)) p++; } while (0)
  327. FILE_BUF expand_to_temp_buffer ();
  328. DEFINITION *collect_expansion ();
  329. int
  330. main (argc, argv)
  331. int argc;
  332. char **argv;
  333. {
  334. struct stat sbuf;
  335. char *in_fname, *out_fname;
  336. int f, i;
  337. FILE_BUF *fp;
  338. char **pend_files = (char **) alloca (argc * sizeof (char *));
  339. char **pend_defs = (char **) alloca (argc * sizeof (char *));
  340. char **pend_undefs = (char **) alloca (argc * sizeof (char *));
  341. int inhibit_predefs = 0;
  342. progname = argv[0];
  343. in_fname = NULL;
  344. out_fname = NULL;
  345. initialize_random_junk ();
  346. no_line_commands = 0;
  347. no_trigraphs = 1;
  348. dump_macros = 0;
  349. no_output = 0;
  350. bzero (pend_files, argc * sizeof (char *));
  351. bzero (pend_defs, argc * sizeof (char *));
  352. bzero (pend_undefs, argc * sizeof (char *));
  353. /* Process switches and find input file name. */
  354. for (i = 1; i < argc; i++) {
  355. if (argv[i][0] != '-') {
  356. if (out_fname != NULL)
  357. fatal ("Usage: %s [switches] input output\n", argv[0]);
  358. else if (in_fname != NULL) {
  359. out_fname = argv[i];
  360. if (! freopen (out_fname, "w", stdout))
  361. pfatal_with_name (out_fname);
  362. } else
  363. in_fname = argv[i];
  364. } else {
  365. switch (argv[i][1]) {
  366. case 'i':
  367. if (argv[i][2] != 0)
  368. pend_files[i] = argv[i] + 2;
  369. else
  370. pend_files[i] = argv[++i];
  371. break;
  372. case 'p':
  373. pedantic = 1;
  374. break;
  375. case 'd':
  376. dump_macros = 1;
  377. no_output = 1;
  378. break;
  379. case 'D':
  380. {
  381. char *p, *p1;
  382. if (argv[i][2] != 0)
  383. p = argv[i] + 2;
  384. else
  385. p = argv[++i];
  386. if ((p1 = (char *) index (p, '=')) != NULL)
  387. *p1 = ' ';
  388. pend_defs[i] = p;
  389. }
  390. break;
  391. case 'U': /* JF #undef something */
  392. if (argv[i][2] != 0)
  393. pend_undefs[i] = argv[i] + 2;
  394. else
  395. pend_undefs[i] = argv[++i];
  396. break;
  397. case 'C':
  398. put_out_comments = 1;
  399. break;
  400. case 'E': /* -E comes from cc -E; ignore it. */
  401. break;
  402. case 'P':
  403. no_line_commands = 1;
  404. break;
  405. case 'T': /* Enable ANSI trigraphs */
  406. no_trigraphs = 0;
  407. break;
  408. case 'I': /* JF handle directory path right */
  409. {
  410. struct directory_stack *dirtmp;
  411. dirtmp = (struct directory_stack *)
  412. xmalloc (sizeof (struct directory_stack));
  413. dirtmp->next = include->next;
  414. include->next = dirtmp;
  415. if (argv[i][2] != 0)
  416. dirtmp->fname = argv[i] + 2;
  417. else
  418. dirtmp->fname = argv[++i];
  419. include = dirtmp;
  420. if (strlen (dirtmp->fname) > max_include_len)
  421. max_include_len = strlen (dirtmp->fname);
  422. }
  423. break;
  424. case 'u':
  425. /* Sun compiler passes undocumented switch "-undef".
  426. Let's assume it means to inhibit the predefined symbols. */
  427. inhibit_predefs = 1;
  428. break;
  429. case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  430. if (in_fname == NULL) {
  431. in_fname = "";
  432. break;
  433. } else if (out_fname == NULL) {
  434. out_fname = "stdout";
  435. break;
  436. } /* else fall through into error */
  437. default:
  438. fatal ("Illegal option %s\n", argv[i]);
  439. }
  440. }
  441. }
  442. /* Do standard #defines that identify processor type. */
  443. if (!inhibit_predefs) {
  444. char *p = (char *) alloca (strlen (predefs) + 1);
  445. strcpy (p, predefs);
  446. while (*p) {
  447. char *q = p;
  448. while (*p && *p != ',') p++;
  449. if (*p != 0)
  450. *p++= 0;
  451. make_definition (q);
  452. }
  453. }
  454. /* Do defines specified with -D. */
  455. for (i = 1; i < argc; i++)
  456. if (pend_defs[i])
  457. make_definition (pend_defs[i]);
  458. /* Do undefines specified with -U. */
  459. for (i = 1; i < argc; i++)
  460. if (pend_undefs[i])
  461. make_undef (pend_undefs[i]);
  462. /* Initialize output buffer */
  463. outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  464. outbuf.bufp = outbuf.buf;
  465. outbuf.length = OUTBUF_SIZE;
  466. /* Scan the -i files before the main input.
  467. Much like #including them, but with no_output set
  468. so that only their macro definitions matter. */
  469. no_output++;
  470. for (i = 1; i < argc; i++)
  471. if (pend_files[i]) {
  472. int fd = open (pend_files[i], O_RDONLY, 0666);
  473. if (fd < 0) {
  474. perror_with_name (pend_files[i]);
  475. return FATAL_EXIT_CODE;
  476. }
  477. finclude (fd, pend_files[i], &outbuf);
  478. }
  479. no_output--;
  480. /* Create an input stack level for the main input file
  481. and copy the entire contents of the file into it. */
  482. fp = &instack[++indepth];
  483. /* JF check for stdin */
  484. if (in_fname == NULL || *in_fname == 0) {
  485. in_fname = "";
  486. f = 0;
  487. } else if ((f = open (in_fname, O_RDONLY)) < 0)
  488. goto perror;
  489. fstat (f, &sbuf);
  490. fp->fname = in_fname;
  491. fp->lineno = 1;
  492. /* JF all this is mine about reading pipes and ttys */
  493. if ((sbuf.st_mode & S_IFMT) != S_IFREG) {
  494. /* Read input from a file that is not a normal disk file.
  495. We cannot preallocate a buffer with the correct size,
  496. so we must read in the file a piece at the time and make it bigger. */
  497. int size;
  498. int bsize;
  499. int cnt;
  500. U_CHAR *bufp;
  501. bsize = 2000;
  502. size = 0;
  503. fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  504. bufp = fp->buf;
  505. for (;;) {
  506. cnt = read (f, bufp, bsize - size);
  507. if (cnt < 0) goto perror; /* error! */
  508. if (cnt == 0) break; /* End of file */
  509. size += cnt;
  510. bufp += cnt;
  511. if (bsize == size) { /* Buffer is full! */
  512. bsize *= 2;
  513. fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  514. bufp = fp->buf + size; /* May have moved */
  515. }
  516. }
  517. fp->length = size;
  518. } else {
  519. /* Read a file whose size we can determine in advance. */
  520. fp->length = sbuf.st_size;
  521. fp->buf = (U_CHAR *) alloca (sbuf.st_size + 2);
  522. if (read (f, fp->buf, sbuf.st_size) != sbuf.st_size)
  523. goto perror;
  524. }
  525. fp->bufp = fp->buf;
  526. /* Make sure data ends with a newline. And put a null after it. */
  527. if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
  528. fp->buf[fp->length++] = '\n';
  529. fp->buf[fp->length] = '\0';
  530. /* Unless inhibited, convert trigraphs in the input. */
  531. if (!no_trigraphs)
  532. trigraph_pcp (fp);
  533. output_line_command (fp, &outbuf, 0);
  534. /* Scan the input, processing macros and directives. */
  535. rescan (&outbuf, 0);
  536. /* Now we have processed the entire input
  537. Write whichever kind of output has been requested. */
  538. if (dump_macros)
  539. dump_all_macros ();
  540. else {
  541. write (fileno (stdout), outbuf.buf, outbuf.bufp - outbuf.buf);
  542. }
  543. return 0;
  544. perror:
  545. pfatal_with_name (argv[1]);
  546. }
  547. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  548. before main CCCP processing. Name `pcp' is also in honor of the
  549. drugs the trigraph designers must have been on.
  550. Using an extra pass through the buffer takes a little extra time,
  551. but is infinitely less hairy than trying to handle ??/" inside
  552. strings, etc. everywhere, and also makes sure that trigraphs are
  553. only translated in the top level of processing. */
  554. trigraph_pcp (buf)
  555. FILE_BUF *buf;
  556. {
  557. register U_CHAR c, *fptr, *bptr, *sptr;
  558. int len;
  559. fptr = bptr = sptr = buf->buf;
  560. while ((sptr = (U_CHAR *) index (sptr, '?')) != NULL) {
  561. if (*++sptr != '?')
  562. continue;
  563. switch (*++sptr) {
  564. case '=':
  565. c = '#';
  566. break;
  567. case '(':
  568. c = '[';
  569. break;
  570. case '/':
  571. c = '\\';
  572. break;
  573. case ')':
  574. c = ']';
  575. break;
  576. case '\'':
  577. c = '^';
  578. break;
  579. case '<':
  580. c = '{';
  581. break;
  582. case '!':
  583. c = '|';
  584. break;
  585. case '>':
  586. c = '}';
  587. break;
  588. case '-':
  589. c = '~';
  590. break;
  591. case '?':
  592. sptr--;
  593. continue;
  594. default:
  595. continue;
  596. }
  597. len = sptr - fptr - 2;
  598. if (bptr != fptr && len > 0)
  599. bcopy (fptr, bptr, len); /* BSD doc says bcopy () works right
  600. for overlapping strings. In ANSI
  601. C, this will be memmove (). */
  602. bptr += len;
  603. *bptr++ = c;
  604. fptr = ++sptr;
  605. }
  606. len = buf->length - (fptr - buf->buf);
  607. if (bptr != fptr && len > 0)
  608. bcopy (fptr, bptr, len);
  609. buf->length -= fptr - bptr;
  610. buf->buf[buf->length] = '\0';
  611. }
  612. /*
  613. * The main loop of the program.
  614. *
  615. * Read characters from the input stack, transferring them to the
  616. * output buffer OP.
  617. *
  618. * Macros are expanded and push levels on the input stack.
  619. * At the end of such a level it is popped off and we keep reading.
  620. * At the end of any other kind of level, we return.
  621. * #-directives are handled, except within macros.
  622. *
  623. * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  624. * and insert them when appropriate. This is set while scanning macro
  625. * arguments before substitution. It is zero when scanning for final output.
  626. * There are three types of Newline markers:
  627. * * Newline - follows a macro name that was not expanded
  628. * because it appeared inside an expansion of the same macro.
  629. * This marker prevents future expansion of that identifier.
  630. * When the input is rescanned into the final output, these are deleted.
  631. * These are also deleted by ## concatenation.
  632. * * Newline Space (or Newline and any other whitespace character)
  633. * stands for a place that tokens must be separated or whitespace
  634. * is otherwise desirable, but where the ANSI standard specifies there
  635. * is no whitespace. This marker turns into a Space (or whichever other
  636. * whitespace char appears in the marker) in the final output,
  637. * but it turns into nothing in an argument that is stringified with #.
  638. * Such stringified arguments are the only place where the ANSI standard
  639. * specifies with precision that whitespace may not appear.
  640. *
  641. * During this function, IP->bufp is kept cached in IBP for speed of access.
  642. * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
  643. * IBP, IP and OBP must be copied back to memory. IP and IBP are
  644. * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
  645. * explicitly, and before RECACHE, since RECACHE uses OBP.
  646. */
  647. rescan (op, output_marks)
  648. FILE_BUF *op;
  649. int output_marks;
  650. {
  651. /* Character being scanned in main loop. */
  652. register U_CHAR c;
  653. /* Length of pending accumulated identifier. */
  654. register int ident_length = 0;
  655. /* Hash code of pending accumulated identifier. */
  656. register int hash = 0;
  657. /* Current input level (&instack[indepth]). */
  658. FILE_BUF *ip;
  659. /* Pointer for scanning input. */
  660. register U_CHAR *ibp;
  661. /* Pointer to end of input. End of scan is controlled by LIMIT. */
  662. register U_CHAR *limit;
  663. /* Pointer for storing output. */
  664. register U_CHAR *obp;
  665. /* REDO_CHAR is nonzero if we are processing an identifier
  666. after backing up over the terminating character.
  667. Sometimes we process an identifier without backing up over
  668. the terminating character, if the terminating character
  669. is not special. Backing up is done so that the terminating character
  670. will be dispatched on again once the identifier is dealt with. */
  671. int redo_char = 0;
  672. /* 1 if within an identifier inside of which a concatenation
  673. marker (Newline -) has been seen. */
  674. int concatenated = 0;
  675. /* While scanning a comment or a string constant,
  676. this records the line it started on, for error messages. */
  677. int start_line;
  678. /* Pop the innermost input stack level, assuming it is a macro expansion. */
  679. #define POPMACRO \
  680. do { ip->macro->type = T_MACRO; \
  681. if (ip->free) free (ip->free); \
  682. --indepth; } while (0)
  683. /* Reload `rescan's local variables that describe the current
  684. level of the input stack. */
  685. #define RECACHE \
  686. do { ip = &instack[indepth]; \
  687. ibp = ip->bufp; \
  688. limit = ip->buf + ip->length; \
  689. op->bufp = obp; \
  690. check_expand (op, limit - ibp); \
  691. obp = op->bufp; } while (0)
  692. if (no_output && instack[indepth].fname != 0)
  693. skip_if_group (&instack[indepth], 1);
  694. obp = op->bufp;
  695. RECACHE;
  696. /* Our caller must always put a null after the end of
  697. the input at each input stack level. */
  698. if (*limit != 0)
  699. abort ();
  700. while (1) {
  701. c = *ibp++;
  702. *obp++ = c;
  703. switch (c) {
  704. case '\\':
  705. if (ibp >= limit)
  706. break;
  707. if (*ibp == '\n') {
  708. /* Always merge lines ending with backslash-newline,
  709. even in middle of identifier. */
  710. ++ibp;
  711. ++ip->lineno;
  712. --obp; /* remove backslash from obuf */
  713. break;
  714. }
  715. /* Otherwise, backslash suppresses specialness of following char,
  716. so copy it here to prevent the switch from seeing it.
  717. But first get any pending identifier processed. */
  718. if (ident_length > 0)
  719. goto specialchar;
  720. *obp++ = *ibp++;
  721. break;
  722. case '#':
  723. /* If this is a macro definition, don't recognize
  724. preprocessor directives. */
  725. if (ip->macro != 0)
  726. goto randomchar;
  727. if (ident_length)
  728. goto specialchar;
  729. /* # keyword: a # must be first nonblank char on the line */
  730. {
  731. U_CHAR *bp;
  732. for (bp = ibp - 1; bp >= ip->buf; bp--)
  733. if (*bp == '\n')
  734. break;
  735. bp++; /* Skip nl or move back into buffer */
  736. SKIP_WHITE_SPACE (bp);
  737. if (*bp != '#')
  738. goto randomchar;
  739. }
  740. --obp; /* Don't copy the '#' */
  741. ip->bufp = ibp;
  742. op->bufp = obp;
  743. if (! handle_directive (ip, op)) {
  744. if (no_output && instack[indepth].fname) {
  745. skip_if_group (&instack[indepth], 1);
  746. break;
  747. }
  748. ++obp; /* Copy the '#' after all */
  749. goto randomchar;
  750. }
  751. if (no_output && instack[indepth].fname)
  752. skip_if_group (&instack[indepth], 1);
  753. obp = op->bufp;
  754. RECACHE;
  755. break;
  756. case '\"': /* skip quoted string */
  757. case '\'':
  758. /* A single quoted string is treated like a double -- some
  759. programs (e.g., troff) are perverse this way */
  760. if (ident_length)
  761. goto specialchar;
  762. start_line = ip->lineno;
  763. /* Skip ahead to a matching quote. */
  764. while (1) {
  765. if (ibp >= limit)
  766. {
  767. error ("Unterminated string constant starts at line %d",
  768. line_for_error (start_line));
  769. break;
  770. }
  771. *obp++ = *ibp;
  772. switch (*ibp++) {
  773. case '\n':
  774. ++ip->lineno;
  775. ++op->lineno;
  776. break;
  777. case '\\':
  778. if (ibp >= limit)
  779. break;
  780. if (*ibp == '\n')
  781. {
  782. /* Backslash newline is replaced by nothing at all,
  783. but keep the line counts correct. */
  784. --obp;
  785. ++ibp;
  786. ++ip->lineno;
  787. }
  788. else
  789. *obp++ = *ibp++;
  790. break;
  791. case '\"':
  792. case '\'':
  793. if (ibp[-1] == c)
  794. goto while2end;
  795. break;
  796. }
  797. }
  798. while2end:
  799. break;
  800. case '/':
  801. if (*ibp != '*')
  802. goto randomchar;
  803. if (ip->macro != 0)
  804. goto randomchar;
  805. if (ident_length)
  806. goto specialchar;
  807. /* We have a comment. Skip it, optionally copying it to output. */
  808. start_line = ip->lineno;
  809. --ibp; /* Back over the slash. */
  810. --obp;
  811. /* Comments are equivalent to spaces. */
  812. if (! put_out_comments)
  813. *obp++ = ' ';
  814. {
  815. U_CHAR *before_bp = ibp;
  816. while (ibp < limit) {
  817. switch (*ibp++) {
  818. case '*':
  819. if (ibp >= limit || *ibp == '/')
  820. goto comment_end;
  821. break;
  822. case '\n':
  823. ++ip->lineno;
  824. /* Copy the newline into the output buffer, in order to
  825. avoid the pain of a #line every time a multiline comment
  826. is seen. */
  827. if (!put_out_comments)
  828. *obp++ = '\n';
  829. ++op->lineno;
  830. }
  831. }
  832. comment_end:
  833. if (ibp >= limit)
  834. error ("Unterminated comment starts on line %d",
  835. line_for_error (start_line));
  836. else {
  837. ibp++;
  838. if (put_out_comments) {
  839. bcopy (before_bp, obp, ibp - before_bp);
  840. obp += ibp - before_bp;
  841. }
  842. }
  843. }
  844. break;
  845. case '0': case '1': case '2': case '3': case '4':
  846. case '5': case '6': case '7': case '8': case '9':
  847. /* If digit is not part of identifier, it starts a number,
  848. which means that following letters are not an identifier.
  849. "0x5" does not refer to an identifier "x5".
  850. So copy all alphanumerics that follow without accumulating
  851. as an identifier. Periods also, for sake of "3.e7". */
  852. if (ident_length == 0) {
  853. while (ibp < limit) {
  854. c = *ibp++;
  855. if (!isalnum (c) && c != '.') {
  856. --ibp;
  857. break;
  858. }
  859. *obp++ = c;
  860. }
  861. break;
  862. }
  863. /* fall through */
  864. case '_':
  865. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  866. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  867. case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  868. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  869. case 'y': case 'z':
  870. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  871. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  872. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  873. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  874. case 'Y': case 'Z':
  875. ident_length++;
  876. /* Compute step of hash function, to avoid a proc call on every token */
  877. hash = HASHSTEP (hash, c);
  878. break;
  879. case '\n':
  880. /* If reprocessing a macro expansion, newline is a special marker. */
  881. if (ip->macro != 0) {
  882. /* Newline White is a "funny space" to separate tokens that are
  883. supposed to be separate but without space between.
  884. Here While means any horizontal whitespace character.
  885. Newline - marks a recursive macro use that is not
  886. supposed to be expandable. */
  887. if (*ibp == '-') {
  888. /* Newline - inhibits expansion of preceding token.
  889. If expanding a macro arg, we keep the newline -.
  890. In final output, it is deleted. */
  891. if (! concatenated) {
  892. ident_length = 0;
  893. hash = 0;
  894. }
  895. ibp++;
  896. if (!output_marks) {
  897. obp--;
  898. } else {
  899. /* If expanding a macro arg, keep the newline -. */
  900. *obp++ = '-';
  901. }
  902. } else if (isspace (*ibp)) {
  903. /* Newline Space does not prevent expansion of preceding token
  904. so expand the preceding token and then come back. */
  905. if (ident_length > 0)
  906. goto specialchar;
  907. /* If generating final output, newline space makes a space. */
  908. if (!output_marks) {
  909. obp[-1] = *ibp++;
  910. /* And Newline Newline makes a newline, so count it. */
  911. if (obp[-1] == '\n')
  912. op->lineno++;
  913. } else {
  914. /* If expanding a macro arg, keep the newline space.
  915. If the arg gets stringified, newline space makes nothing. */
  916. *obp++ = *ibp++;
  917. }
  918. } else abort (); /* Newline followed by something random? */
  919. break;
  920. }
  921. /* If there is a pending identifier, handle it and come back here. */
  922. if (ident_length > 0)
  923. goto specialchar;
  924. /* Update the line counts and output a #line if necessary. */
  925. ++ip->lineno;
  926. ++op->lineno;
  927. if (ip->lineno != op->lineno) {
  928. op->bufp = obp;
  929. output_line_command (ip, op, 1);
  930. check_expand (op, ip->length - (ip->bufp - ip->buf));
  931. obp = op->bufp;
  932. }
  933. break;
  934. /* Come here either after (1) a null character that is part of the input
  935. or (2) at the end of the input, because there is a null there. */
  936. case 0:
  937. if (ibp <= limit)
  938. /* Our input really contains a null character. */
  939. goto randomchar;
  940. /* At end of a macro-expansion level, pop it and read next level. */
  941. if (ip->macro != 0) {
  942. obp--;
  943. ibp--;
  944. POPMACRO;
  945. RECACHE;
  946. break;
  947. }
  948. /* If we don't have a pending identifier,
  949. return at end of input. */
  950. if (ident_length == 0)
  951. {
  952. obp--;
  953. ibp--;
  954. op->bufp = obp;
  955. ip->bufp = ibp;
  956. return;
  957. }
  958. /* If we do have a pending identifier, just consider this null
  959. a special character and arrange to dispatch on it again.
  960. The second time, IDENT_LENGTH will be zero so we will return. */
  961. /* Fall through */
  962. specialchar:
  963. /* Handle the case of a character such as /, ', " or null
  964. seen following an identifier. Back over it so that
  965. after the identifier is processed the special char
  966. will be dispatched on again. */
  967. ibp--;
  968. obp--;
  969. redo_char = 1;
  970. default:
  971. randomchar:
  972. if (ident_length > 0) {
  973. register HASHNODE *hp;
  974. /* We have just seen an identifier end. If it's a macro, expand it.
  975. IDENT_LENGTH is the length of the identifier
  976. and HASH is its hash code.
  977. The identifier has already been copied to the output,
  978. so if it is a macro we must remove it.
  979. If REDO_CHAR is 0, the char that terminated the identifier
  980. has been skipped in the output and the input.
  981. OBP-IDENT_LENGTH-1 points to the identifier.
  982. If the identifier is a macro, we must back over the terminator.
  983. If REDO_CHAR is 1, the terminating char has already been
  984. backed over. OBP-IDENT_LENGTH points to the identifier. */
  985. for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  986. hp = hp->next) {
  987. if (hp->length == ident_length) {
  988. U_CHAR *obufp_before_macroname;
  989. int op_lineno_before_macroname;
  990. register int i = ident_length;
  991. register U_CHAR *p = hp->name;
  992. register U_CHAR *q = obp - i;
  993. int disabled;
  994. if (! redo_char)
  995. q--;
  996. do { /* All this to avoid a strncmp () */
  997. if (*p++ != *q++)
  998. goto hashcollision;
  999. } while (--i);
  1000. /* We found a use of a macro name.
  1001. see if the context shows it is a macro call. */
  1002. /* Back up over terminating character if not already done. */
  1003. if (! redo_char)
  1004. {
  1005. ibp--;
  1006. obp--;
  1007. }
  1008. obufp_before_macroname = obp - ident_length;
  1009. op_lineno_before_macroname = op->lineno;
  1010. /* Record whether the macro is disabled now,
  1011. even though we don't act on it until later,
  1012. because popping levels while looking for the `('
  1013. could reenable the macro. */
  1014. disabled = hp->type == T_DISABLED;
  1015. /* If macro wants an arglist, verify that a '(' follows.
  1016. first skip all whitespace, copying it to the output
  1017. after the macro name. then, if there is no '(',
  1018. decide this is not a macro call and leave things that way. */
  1019. if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  1020. && hp->value.defn->nargs >= 0)
  1021. {
  1022. while (1) {
  1023. /* Scan forward over whitespace, copying it to the output. */
  1024. if (ibp == limit && ip->macro != 0) {
  1025. POPMACRO;
  1026. RECACHE;
  1027. }
  1028. else if (isspace (*ibp)) {
  1029. *obp++ = *ibp++;
  1030. if (ibp[-1] == '\n')
  1031. {
  1032. if (ip->macro == 0) {
  1033. ++ip->lineno;
  1034. ++op->lineno;
  1035. }
  1036. else if (!output_marks)
  1037. obp--;
  1038. }
  1039. }
  1040. else break;
  1041. }
  1042. if (*ibp != '(')
  1043. break;
  1044. }
  1045. /* This looks like a macro call, but if the macro was disabled,
  1046. just copy its name and put in a marker if requested. */
  1047. if (disabled)
  1048. {
  1049. if (output_marks) {
  1050. check_expand (op, limit - ibp + 2);
  1051. *obp++ = '\n';
  1052. *obp++ = '-';
  1053. }
  1054. break;
  1055. }
  1056. /* This is now known to be a macro call.
  1057. Discard the macro name from the output,
  1058. along with any following whitespace just copied. */
  1059. obp = obufp_before_macroname;
  1060. op->lineno = op_lineno_before_macroname;
  1061. /* Expand the macro, reading arguments as needed,
  1062. and push the expansion on the input stack. */
  1063. ip->bufp = ibp;
  1064. op->bufp = obp;
  1065. macroexpand (hp, op);
  1066. /* Reexamine input stack, since macroexpand has pushed
  1067. a new level on it. */
  1068. obp = op->bufp;
  1069. RECACHE;
  1070. break;
  1071. }
  1072. hashcollision:
  1073. ;
  1074. } /* End hash-table-search loop */
  1075. ident_length = hash = 0; /* Stop collecting identifier */
  1076. redo_char = 0;
  1077. concatenated = 0;
  1078. } /* End if (ident_length > 0) */
  1079. } /* End switch */
  1080. } /* End per-char loop */
  1081. }
  1082. /*
  1083. * Rescan a string into a temporary buffer and return the result
  1084. * as a FILE_BUF. Note this function returns a struct, not a pointer.
  1085. *
  1086. * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  1087. * and insert such markers when appropriate. See `rescan' for details.
  1088. * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  1089. * before substitution; it is 0 for other uses.
  1090. */
  1091. static FILE_BUF
  1092. expand_to_temp_buffer (buf, limit, output_marks)
  1093. U_CHAR *buf, *limit;
  1094. int output_marks;
  1095. {
  1096. register FILE_BUF *ip;
  1097. FILE_BUF obuf;
  1098. int value;
  1099. int length = limit - buf;
  1100. U_CHAR *buf1;
  1101. int odepth = indepth;
  1102. if (length < 0)
  1103. abort ();
  1104. /* Set up the input on the input stack. */
  1105. buf1 = (U_CHAR *) alloca (length + 1);
  1106. {
  1107. register U_CHAR *p1 = buf;
  1108. register U_CHAR *p2 = buf1;
  1109. while (p1 != limit)
  1110. *p2++ = *p1++;
  1111. }
  1112. buf1[length] = 0;
  1113. ++indepth;
  1114. ip = &instack[indepth];
  1115. ip->fname = 0;
  1116. ip->macro = 0;
  1117. ip->free = 0;
  1118. ip->length = length;
  1119. ip->buf = ip->bufp = buf1;
  1120. /* Set up to receive the output. */
  1121. obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
  1122. obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  1123. obuf.fname = 0;
  1124. obuf.macro = 0;
  1125. obuf.free = 0;
  1126. ip->lineno = obuf.lineno = 1;
  1127. /* Scan the input, create the output. */
  1128. rescan (&obuf, output_marks);
  1129. /* Pop input stack to original state. */
  1130. --indepth;
  1131. if (indepth != odepth)
  1132. abort ();
  1133. /* Record the output. */
  1134. obuf.length = obuf.bufp - obuf.buf;
  1135. return obuf;
  1136. }
  1137. /*
  1138. * Process a # directive. Expects IP->bufp to point to the '#', as in
  1139. * "#define foo bar". Passes to the command handler
  1140. * (do_define, do_include, etc.): the addresses of the 1st and
  1141. * last chars of the command (starting immediately after the #
  1142. * keyword), plus op and the keyword table pointer. If the command
  1143. * contains comments it is copied into a temporary buffer sans comments
  1144. * and the temporary buffer is passed to the command handler instead.
  1145. * Likewise for backslash-newlines.
  1146. *
  1147. * Returns nonzero if this was a known # directive.
  1148. * Otherwise, returns zero, without advancing the input pointer.
  1149. */
  1150. int
  1151. handle_directive (ip, op)
  1152. FILE_BUF *ip, *op;
  1153. {
  1154. register U_CHAR *bp, *cp;
  1155. register struct directive *kt;
  1156. register int ident_length;
  1157. U_CHAR *resume_p;
  1158. /* Nonzero means we must copy the entire command
  1159. to get rid of comments or backslash-newlines. */
  1160. int copy_command = 0;
  1161. bp = ip->bufp;
  1162. SKIP_WHITE_SPACE (bp);
  1163. cp = bp;
  1164. while (is_idchar[*cp])
  1165. cp++;
  1166. ident_length = cp - bp;
  1167. /*
  1168. * Decode the keyword and call the appropriate expansion
  1169. * routine, after moving the input pointer up to the next line.
  1170. */
  1171. for (kt = directive_table; kt->length > 0; kt++) {
  1172. if (kt->length == ident_length && !strncmp (kt->name, bp, ident_length)) {
  1173. register U_CHAR *buf;
  1174. register U_CHAR *limit = ip->buf + ip->length;
  1175. /* Find the end of this command (first newline not backslashed
  1176. and not in a string or comment).
  1177. Set COPY_COMMAND if the command must be copied
  1178. (it contains a backslash-newline or a comment). */
  1179. buf = bp = bp + ident_length;
  1180. while (bp < limit) {
  1181. register U_CHAR c = *bp++;
  1182. switch (c) {
  1183. case '\\':
  1184. if (bp < limit) {
  1185. if (*bp == '\n')
  1186. {
  1187. ip->lineno++;
  1188. copy_command = 1;
  1189. }
  1190. bp++;
  1191. }
  1192. break;
  1193. case '\'':
  1194. case '\"':
  1195. bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, &copy_command);
  1196. break;
  1197. case '/':
  1198. if (*bp == '*') {
  1199. U_CHAR *obp = bp - 1;
  1200. ip->bufp = bp + 1;
  1201. skip_to_end_of_comment (ip, &ip->lineno);
  1202. bp = ip->bufp;
  1203. /* No need to copy the command because of a comment at the end;
  1204. just don't include the comment in the directive. */
  1205. if (bp == limit || *bp == '\n') {
  1206. bp = obp;
  1207. goto endloop1;
  1208. }
  1209. copy_command++;
  1210. }
  1211. break;
  1212. case '\n':
  1213. --bp; /* Point to the newline */
  1214. ip->bufp = bp;
  1215. goto endloop1;
  1216. }
  1217. }
  1218. ip->bufp = bp;
  1219. endloop1:
  1220. resume_p = ip->bufp;
  1221. /* BP is the end of the directive.
  1222. RESUME_P is the next interesting data after the directive.
  1223. A comment may come between. */
  1224. if (copy_command) {
  1225. register U_CHAR *xp = buf;
  1226. register U_CHAR *cp1;
  1227. /* Need to copy entire command into temp buffer before dispatching */
  1228. cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
  1229. some slop */
  1230. buf = cp;
  1231. /* Copy to the new buffer, deleting comments. */
  1232. while (xp < bp) {
  1233. register U_CHAR c = *xp++;
  1234. *cp++ = c;
  1235. switch (c) {
  1236. case '\n':
  1237. break;
  1238. case '\'':
  1239. case '\"':
  1240. {
  1241. register U_CHAR *bp1
  1242. = skip_quoted_string (xp - 1, limit, ip->lineno, 0, 0);
  1243. while (xp != bp1)
  1244. *cp++ = *xp++;
  1245. }
  1246. break;
  1247. case '/':
  1248. if (*xp == '*') {
  1249. cp--;
  1250. ip->bufp = xp + 1;
  1251. skip_to_end_of_comment (ip, 0);
  1252. xp = ip->bufp;
  1253. }
  1254. }
  1255. }
  1256. /* Now copy new buffer to itself, deleting
  1257. backslash-newline. */
  1258. cp1 = cp;
  1259. *cp1 = 0;
  1260. xp = buf;
  1261. cp = buf;
  1262. while (xp < cp1) {
  1263. register U_CHAR c = *xp++;
  1264. *cp++ = c;
  1265. if (c == '\\' && *xp == '\n') {
  1266. --cp;
  1267. ++xp;
  1268. if (cp != buf && isspace (cp[-1])) {
  1269. while (cp != buf && isspace (cp[-1])) cp--;
  1270. cp++;
  1271. SKIP_WHITE_SPACE (xp);
  1272. } else if (isspace (*xp)) {
  1273. *cp++ = *xp++;
  1274. SKIP_WHITE_SPACE (xp);
  1275. }
  1276. }
  1277. }
  1278. }
  1279. else
  1280. cp = bp;
  1281. ip->bufp = resume_p;
  1282. /* Call the appropriate command handler. buf now points to
  1283. either the appropriate place in the input buffer, or to
  1284. the temp buffer if it was necessary to make one. cp
  1285. points to the first char after the contents of the (possibly
  1286. copied) command, in either case. */
  1287. (*kt->func) (buf, cp, op, kt);
  1288. check_expand (op, ip->length - (ip->bufp - ip->buf));
  1289. return 1;
  1290. }
  1291. }
  1292. return 0;
  1293. }
  1294. static char *monthnames[] = {"jan", "feb", "mar", "apr", "may", "jun",
  1295. "jul", "aug", "sep", "oct", "nov", "dec",
  1296. };
  1297. /*
  1298. * expand things like __FILE__. Place the expansion into the output
  1299. * buffer *without* rescanning.
  1300. */
  1301. special_symbol (hp, op)
  1302. HASHNODE *hp;
  1303. FILE_BUF *op;
  1304. {
  1305. char *buf;
  1306. int i, len;
  1307. FILE_BUF *ip = NULL;
  1308. static struct tm *timebuf = NULL;
  1309. struct tm *localtime ();
  1310. int paren = 0; /* For special `defined' keyword */
  1311. for (i = indepth; i >= 0; i--)
  1312. if (instack[i].fname != NULL) {
  1313. ip = &instack[i];
  1314. break;
  1315. }
  1316. if (ip == NULL) {
  1317. error ("cccp error: not in any file?!");
  1318. return; /* the show must go on */
  1319. }
  1320. switch (hp->type) {
  1321. case T_FILE:
  1322. buf = (char *) alloca (3 + strlen (ip->fname));
  1323. sprintf (buf, "\"%s\"", ip->fname);
  1324. break;
  1325. case T_CONST:
  1326. buf = (char *) alloca (4 * sizeof (int));
  1327. sprintf (buf, "%d", hp->value.ival);
  1328. break;
  1329. case T_SPECLINE:
  1330. buf = (char *) alloca (10);
  1331. sprintf (buf, "%d", ip->lineno);
  1332. break;
  1333. case T_DATE:
  1334. case T_TIME:
  1335. if (timebuf == NULL) {
  1336. i = time (0);
  1337. timebuf = localtime (&i);
  1338. }
  1339. buf = (char *) alloca (20);
  1340. if (hp->type == T_DATE)
  1341. sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon - 1],
  1342. timebuf->tm_mday, timebuf->tm_year + 1900);
  1343. else
  1344. sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  1345. timebuf->tm_sec);
  1346. break;
  1347. case T_SPEC_DEFINED:
  1348. buf = " 0 "; /* Assume symbol is not defined */
  1349. ip = &instack[indepth];
  1350. SKIP_WHITE_SPACE (ip->bufp);
  1351. if (*ip->bufp == '(') {
  1352. paren++;
  1353. ip->bufp++; /* Skip over the paren */
  1354. SKIP_WHITE_SPACE (ip->bufp);
  1355. }
  1356. if (!is_idstart[*ip->bufp])
  1357. goto oops;
  1358. if (lookup (ip->bufp, -1, -1))
  1359. buf = " 1 ";
  1360. while (is_idchar[*ip->bufp])
  1361. ++ip->bufp;
  1362. SKIP_WHITE_SPACE (ip->bufp);
  1363. if (paren) {
  1364. if (*ip->bufp != ')')
  1365. goto oops;
  1366. ++ip->bufp;
  1367. }
  1368. break;
  1369. oops:
  1370. error ("`defined' must be followed by ident or (ident)");
  1371. break;
  1372. default:
  1373. error ("cccp error: illegal special hash type"); /* time for gdb */
  1374. abort ();
  1375. }
  1376. len = strlen (buf);
  1377. check_expand (op, len);
  1378. bcopy (buf, op->bufp, len);
  1379. op->bufp += len;
  1380. return;
  1381. }
  1382. /* Routines to handle #directives */
  1383. /*
  1384. * Process include file by reading it in and calling rescan.
  1385. * Expects to see "fname" or <fname> on the input.
  1386. * add error checking and -i option later.
  1387. */
  1388. do_include (buf, limit, op, keyword)
  1389. U_CHAR *buf, *limit;
  1390. FILE_BUF *op;
  1391. struct directive *keyword;
  1392. {
  1393. char *fname; /* Dynamically allocated fname buffer */
  1394. U_CHAR *fbeg, *fend; /* Beginning and end of fname */
  1395. U_CHAR term; /* Terminator for fname */
  1396. int err = 0; /* Some error has happened */
  1397. struct directory_stack *stackp;
  1398. int flen;
  1399. int f; /* file number */
  1400. char *other_dir;
  1401. int retried = 0; /* Have already tried macro
  1402. expanding the include line*/
  1403. FILE_BUF trybuf; /* It got expanded into here */
  1404. f= -1; /* JF we iz paranoid! */
  1405. get_filename:
  1406. fbeg = buf;
  1407. SKIP_WHITE_SPACE (fbeg);
  1408. /* Discard trailing whitespace so we can easily see
  1409. if we have parsed all the significant chars we were given. */
  1410. while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  1411. switch (*fbeg++) {
  1412. case '\"':
  1413. fend = fbeg;
  1414. while (fend != limit && *fend != '\"') {
  1415. if (*fend == '\\') {
  1416. if (fend + 1 == limit)
  1417. break;
  1418. fend++;
  1419. }
  1420. fend++;
  1421. }
  1422. if (*fend == '\"' && fend + 1 == limit) {
  1423. stackp = include;
  1424. break;
  1425. }
  1426. goto fail;
  1427. case '<':
  1428. fend = fbeg;
  1429. while (fend != limit && *fend != '>') fend++;
  1430. if (*fend == '>' && fend + 1 == limit) {
  1431. stackp = include->next;
  1432. break;
  1433. }
  1434. goto fail;
  1435. default:
  1436. fail:
  1437. if (retried) {
  1438. error ("#include expects \"fname\" or <fname>");
  1439. return;
  1440. } else {
  1441. trybuf = expand_to_temp_buffer (buf, limit, 0);
  1442. buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  1443. bcopy (trybuf.buf, buf, trybuf.bufp - trybuf.buf);
  1444. limit = buf + (trybuf.bufp - trybuf.buf);
  1445. free (trybuf.buf);
  1446. retried++;
  1447. goto get_filename;
  1448. }
  1449. }
  1450. flen = fend - fbeg;
  1451. /* Set OTHER_DIR to the directory the current input file is in.
  1452. That is one of the directories searched, sometimes. */
  1453. other_dir = NULL;
  1454. if (stackp == include)
  1455. {
  1456. FILE_BUF *fp;
  1457. for (fp = &instack[indepth]; fp >= instack; fp--)
  1458. {
  1459. int n;
  1460. char *ep,*nam;
  1461. extern char *rindex ();
  1462. if ((nam = fp->fname) != NULL)
  1463. {
  1464. if ((ep = rindex (nam, '/')) != NULL)
  1465. {
  1466. n = ep - nam;
  1467. other_dir = (char *) alloca (n + 1);
  1468. strncpy (other_dir, nam, n);
  1469. other_dir[n] = '\0';
  1470. }
  1471. break;
  1472. }
  1473. }
  1474. }
  1475. /* Search directory path, trying to open the file.
  1476. Copy each filename tried into FNAME. */
  1477. fname = (char *) alloca (max_include_len + flen);
  1478. for (; stackp; stackp = stackp->next)
  1479. {
  1480. if (other_dir)
  1481. {
  1482. strcpy (fname, other_dir);
  1483. other_dir = 0;
  1484. }
  1485. else
  1486. strcpy (fname, stackp->fname);
  1487. strcat (fname, "/");
  1488. strncat (fname, fbeg, flen);
  1489. if ((f = open (fname, O_RDONLY)) >= 0)
  1490. break;
  1491. }
  1492. if (f < 0)
  1493. perror (fname);
  1494. else {
  1495. finclude (f, fname, op);
  1496. close (f);
  1497. }
  1498. }
  1499. /* Process the contents of include file FNAME, already open on descriptor F,
  1500. with output to OP. */
  1501. finclude (f, fname, op)
  1502. int f;
  1503. char *fname;
  1504. FILE_BUF *op;
  1505. {
  1506. struct stat sbuf; /* To stat the include file */
  1507. FILE_BUF *fp; /* For input stack frame */
  1508. int success = 0;
  1509. if (fstat (f, &sbuf) < 0)
  1510. goto nope; /* Impossible? */
  1511. fp = &instack[indepth + 1];
  1512. bzero (fp, sizeof (FILE_BUF));
  1513. fp->buf = (U_CHAR *) alloca (sbuf.st_size + 2);
  1514. fp->fname = fname;
  1515. fp->length = sbuf.st_size;
  1516. fp->lineno = 1;
  1517. fp->bufp = fp->buf;
  1518. if (read (f, fp->buf, sbuf.st_size) != sbuf.st_size)
  1519. goto nope;
  1520. if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
  1521. fp->buf[fp->length++] = '\n';
  1522. fp->buf[fp->length] = '\0';
  1523. if (!no_trigraphs)
  1524. trigraph_pcp (fp);
  1525. success = 1;
  1526. indepth++;
  1527. output_line_command (fp, op, 0);
  1528. rescan (op, 0);
  1529. indepth--;
  1530. output_line_command (&instack[indepth], op, 0);
  1531. nope:
  1532. close (f);
  1533. if (!success) {
  1534. perror_with_name (fname);
  1535. }
  1536. }
  1537. /* The arglist structure is built by do_define to tell
  1538. collect_definition where the argument names begin. That
  1539. is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  1540. would contain pointers to the strings x, y, and z.
  1541. Collect_definition would then build a DEFINITION node,
  1542. with reflist nodes pointing to the places x, y, and z had
  1543. appeared. So the arglist is just convenience data passed
  1544. between these two routines. It is not kept around after
  1545. the current #define has been processed and entered into the
  1546. hash table. */
  1547. struct arglist {
  1548. struct arglist *next;
  1549. U_CHAR *name;
  1550. int length;
  1551. int argno;
  1552. };
  1553. /* Process a #define command.
  1554. BUF points to the contents of the #define command, as a continguous string.
  1555. LIMIT points to the first character past the end of the definition.
  1556. KEYWORD is the keyword-table entry for #define. */
  1557. do_define (buf, limit, op, keyword)
  1558. U_CHAR *buf, *limit;
  1559. FILE_BUF *op;
  1560. struct directive *keyword;
  1561. {
  1562. U_CHAR *bp; /* temp ptr into input buffer */
  1563. U_CHAR *symname; /* remember where symbol name starts */
  1564. int sym_length; /* and how long it is */
  1565. U_CHAR *def; /* beginning of expansion */
  1566. DEFINITION *defn;
  1567. int arglengths = 0; /* Accumulate lengths of arg names
  1568. plus number of args. */
  1569. int hashcode;
  1570. bp = buf;
  1571. while (is_hor_space[*bp])
  1572. bp++;
  1573. if (!is_idstart[*bp]) {
  1574. error ("illegal macro name: must start with an alphabetic or '_'");
  1575. goto nope;
  1576. }
  1577. symname = bp; /* remember where it starts */
  1578. while (is_idchar[*bp] && bp < limit)
  1579. bp++;
  1580. sym_length = bp - symname;
  1581. /* lossage will occur if identifiers or control keywords are broken
  1582. across lines using backslash. This is not the right place to take
  1583. care of that. */
  1584. if (is_hor_space[*bp] || *bp == '\n' || bp >= limit) {
  1585. /* simple expansion or empty definition; gobble it */
  1586. if (is_hor_space[*bp])
  1587. ++bp; /* skip exactly one blank/tab char */
  1588. /* now everything from bp before limit is the definition. */
  1589. defn = collect_expansion (bp, limit, -1, 0);
  1590. defn->argnames = (U_CHAR *) "";
  1591. }
  1592. else if (*bp == '(') {
  1593. struct arglist *arg_ptrs = NULL;
  1594. int argno = 0;
  1595. bp++; /* skip '(' */
  1596. SKIP_WHITE_SPACE (bp);
  1597. while (*bp != ')') {
  1598. struct arglist *temp;
  1599. temp = (struct arglist *) alloca (sizeof (struct arglist));
  1600. temp->name = bp;
  1601. temp->next = arg_ptrs;
  1602. temp->argno = argno++;
  1603. arg_ptrs = temp;
  1604. while (is_idchar[*bp])
  1605. bp++;
  1606. temp->length = bp - temp->name;
  1607. arglengths += temp->length + 2;
  1608. SKIP_WHITE_SPACE (bp); /* there should not be spaces here,
  1609. but let it slide if there are. */
  1610. if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  1611. error ("illegal parameter to macro");
  1612. goto nope;
  1613. }
  1614. if (*bp == ',') {
  1615. bp++;
  1616. SKIP_WHITE_SPACE (bp);
  1617. }
  1618. if (bp >= limit) {
  1619. error ("unterminated format parameter list in #define");
  1620. goto nope;
  1621. }
  1622. }
  1623. ++bp; /* skip paren */
  1624. /* Skip exactly one space or tab if any. */
  1625. if (bp < limit && (*bp == ' ' || *bp == '\t')) ++bp;
  1626. /* now everything from bp before limit is the definition. */
  1627. defn = collect_expansion (bp, limit, argno, arg_ptrs);
  1628. /* Now set defn->argnames to the result of concatenating
  1629. the argument names in reverse order
  1630. with comma-space between them. */
  1631. defn->argnames = (U_CHAR *) xmalloc (arglengths);
  1632. {
  1633. struct arglist *temp;
  1634. int i = 0;
  1635. for (temp = arg_ptrs; temp; temp = temp->next)
  1636. {
  1637. bcopy (temp->name, &defn->argnames[i], temp->length);
  1638. i += temp->length;
  1639. if (temp->next != 0) {
  1640. defn->argnames[i++] = ',';
  1641. defn->argnames[i++] = ' ';
  1642. }
  1643. }
  1644. defn->argnames[i] = 0;
  1645. }
  1646. } else {
  1647. error ("#define symbol name not followed by SPC, TAB, or '('");
  1648. goto nope;
  1649. }
  1650. hashcode = hashf (symname, sym_length, HASHSIZE);
  1651. {
  1652. HASHNODE *hp;
  1653. DEFINITION *old_def;
  1654. if ((hp = lookup (symname, sym_length, hashcode)) != NULL) {
  1655. if (hp->type != T_MACRO
  1656. || compare_defs (defn, hp->value.defn)) {
  1657. U_CHAR *msg; /* what pain... */
  1658. msg = (U_CHAR *) alloca (sym_length + 20);
  1659. bcopy (symname, msg, sym_length);
  1660. strcpy (msg + sym_length, " redefined");
  1661. error (msg);
  1662. }
  1663. /* Replace the old definition. */
  1664. hp->type = T_MACRO;
  1665. hp->value.defn = defn;
  1666. } else
  1667. install (symname, sym_length, T_MACRO, defn, hashcode);
  1668. }
  1669. return 0;
  1670. nope:
  1671. return 1;
  1672. }
  1673. /*
  1674. * return zero if two DEFINITIONs are isomorphic
  1675. */
  1676. static
  1677. compare_defs (d1, d2)
  1678. DEFINITION *d1, *d2;
  1679. {
  1680. register struct reflist *a1, *a2;
  1681. register U_CHAR *p1 = d1->expansion;
  1682. register U_CHAR *p2 = d2->expansion;
  1683. int first = 1;
  1684. if (d1->nargs != d2->nargs)
  1685. return 1;
  1686. if (strcmp (d1->argnames, d2->argnames))
  1687. return 1;
  1688. for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  1689. a1 = a1->next, a2 = a2->next) {
  1690. if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
  1691. || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  1692. || a1->argno != a2->argno
  1693. || a1->stringify != a2->stringify
  1694. || a1->raw_before != a2->raw_before
  1695. || a1->raw_after != a2->raw_after)
  1696. return 1;
  1697. first = 0;
  1698. p1 += a1->nchars;
  1699. p2 += a2->nchars;
  1700. }
  1701. if (a1 != a2)
  1702. return 1;
  1703. if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  1704. p2, d2->length - (p2 - d2->expansion), 1))
  1705. return 1;
  1706. return 0;
  1707. }
  1708. /* Return 1 if two parts of two macro definitions are effectively different.
  1709. One of the parts starts at BEG1 and has LEN1 chars;
  1710. the other has LEN2 chars at BEG2.
  1711. Any sequence of whitespace matches any other sequence of whitespace.
  1712. FIRST means these parts are the first of a macro definition;
  1713. so ignore leading whitespace entirely.
  1714. LAST means these parts are the last of a macro definition;
  1715. so ignore trailing whitespace entirely. */
  1716. comp_def_part (first, beg1, len1, beg2, len2, last)
  1717. int first;
  1718. U_CHAR *beg1, *beg2;
  1719. int len1, len2;
  1720. int last;
  1721. {
  1722. register U_CHAR *end1 = beg1 + len1;
  1723. register U_CHAR *end2 = beg2 + len2;
  1724. if (first) {
  1725. while (beg1 != end1 && isspace (*beg1)) beg1++;
  1726. while (beg2 != end2 && isspace (*beg2)) beg2++;
  1727. }
  1728. if (last) {
  1729. while (beg1 != end1 && isspace (end1[-1])) end1--;
  1730. while (beg2 != end2 && isspace (end2[-1])) end2--;
  1731. }
  1732. while (beg1 != end1 && beg2 != end2) {
  1733. if (isspace (*beg1) && isspace (*beg2)) {
  1734. while (beg1 != end1 && isspace (*beg1)) beg1++;
  1735. while (beg2 != end2 && isspace (*beg2)) beg2++;
  1736. } else if (*beg1 == *beg2) {
  1737. beg1++; beg2++;
  1738. } else break;
  1739. }
  1740. return (beg1 != end1) || (beg2 != end2);
  1741. }
  1742. /* Read a replacement list for a macro with parameters.
  1743. Build the DEFINITION structure.
  1744. Reads characters of text starting at BUF until LIMIT.
  1745. ARGLIST specifies the formal parameters to look for
  1746. in the text of the definition; NARGS is the number of args
  1747. in that list, or -1 for a macro name that wants no argument list.
  1748. MACRONAME is the macro name itself (so we can avoid recursive expansion)
  1749. and NAMELEN is its length in characters. */
  1750. /* Leading and trailing Space, Tab, etc. are converted to markers
  1751. Newline Space, Newline Tab, etc.
  1752. Newline Space makes a space in the final output
  1753. but is discarded if stringified. (Newline Tab is similar but
  1754. makes a Tab instead.)
  1755. If there is no trailing whitespace, a Newline Space is added at the end
  1756. to prevent concatenation that would be contrary to the standard. */
  1757. static DEFINITION *
  1758. collect_expansion (buf, end, nargs, arglist)
  1759. U_CHAR *buf, *end;
  1760. int nargs;
  1761. struct arglist *arglist;
  1762. {
  1763. DEFINITION *defn;
  1764. register U_CHAR *p, *limit, *lastp, *exp_p;
  1765. struct reflist *endpat = NULL;
  1766. /* Pointer to first nonspace after last ## seen. */
  1767. U_CHAR *concat = 0;
  1768. /* Pointer to first nonspace after last single-# seen. */
  1769. U_CHAR *stringify = 0;
  1770. /* Scan thru the replacement list, ignoring comments and quoted
  1771. strings, picking up on the macro calls. It does a linear search
  1772. thru the arg list on every potential symbol. Profiling might say
  1773. that something smarter should happen. */
  1774. if (end < buf)
  1775. abort ();
  1776. defn = (DEFINITION *) xcalloc (1, sizeof (DEFINITION) + 2 * (end - buf + 2));
  1777. defn->nargs = nargs;
  1778. exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  1779. lastp = exp_p;
  1780. /* Speed this loop up later? */
  1781. p = buf;
  1782. limit = end;
  1783. /* Find the beginning of the trailing whitespace. */
  1784. while (p < limit && isspace (limit[-1])) limit--;
  1785. /* Convert leading whitespace to Newline-markers. */
  1786. while (p < limit && isspace (*p)) {
  1787. *exp_p++ = '\n';
  1788. *exp_p++ = *p++;
  1789. }
  1790. /* Process the main body of the definition. */
  1791. while (p < limit) {
  1792. int skipped_arg = 0;
  1793. register U_CHAR c = *p++;
  1794. *exp_p++ = c;
  1795. switch (c) {
  1796. case '\'':
  1797. case '\"':
  1798. {
  1799. for (; p < limit && *p != c; p++) {
  1800. *exp_p++ = *p;
  1801. if (*p == '\\') {
  1802. *exp_p++ = *++p;
  1803. }
  1804. }
  1805. *exp_p++ = *p++;
  1806. }
  1807. break;
  1808. case '#':
  1809. if (p < limit && *p == '#') {
  1810. /* ##: concatenate preceding and following tokens. */
  1811. /* Take out the first #, discard preceding whitespace. */
  1812. exp_p--;
  1813. while (exp_p > lastp && is_hor_space[exp_p[-1]])
  1814. --exp_p;
  1815. /* Skip the second #. */
  1816. p++;
  1817. /* Discard following whitespace. */
  1818. SKIP_WHITE_SPACE (p);
  1819. concat = p;
  1820. } else {
  1821. /* Single #: stringify following argument ref.
  1822. Don't leave the # in the expansion. */
  1823. exp_p--;
  1824. SKIP_WHITE_SPACE (p);
  1825. if (p == limit || ! is_idstart[*p] || nargs <= 0)
  1826. error ("# operator should be followed by a macro argument name\n");
  1827. else
  1828. stringify = p;
  1829. }
  1830. break;
  1831. }
  1832. if (is_idchar[c] && nargs > 0) {
  1833. U_CHAR *id_beg = p - 1;
  1834. int id_len;
  1835. --exp_p;
  1836. while (p != limit && is_idchar[*p]) p++;
  1837. id_len = p - id_beg;
  1838. if (is_idstart[c]) {
  1839. register struct arglist *arg;
  1840. for (arg = arglist; arg != NULL; arg = arg->next) {
  1841. struct reflist *tpat;
  1842. if (arg->name[0] == c
  1843. && arg->length == id_len
  1844. && strncmp (arg->name, id_beg, id_len) == 0) {
  1845. /* make a pat node for this arg and append it to the end of
  1846. the pat list */
  1847. tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  1848. tpat->next = NULL;
  1849. tpat->stringify = stringify == id_beg;
  1850. tpat->raw_before = concat == id_beg;
  1851. tpat->raw_after = 0;
  1852. if (endpat == NULL)
  1853. defn->pattern = tpat;
  1854. else
  1855. endpat->next = tpat;
  1856. endpat = tpat;
  1857. tpat->argno = arg->argno;
  1858. tpat->nchars = exp_p - lastp;
  1859. {
  1860. register U_CHAR *p1 = p;
  1861. SKIP_WHITE_SPACE (p1);
  1862. if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
  1863. tpat->raw_after = 1;
  1864. }
  1865. lastp = exp_p; /* place to start copying from next time */
  1866. skipped_arg = 1;
  1867. break;
  1868. }
  1869. }
  1870. }
  1871. /* If this was not a macro arg, copy it into the expansion. */
  1872. if (! skipped_arg) {
  1873. register U_CHAR *lim1 = p;
  1874. p = id_beg;
  1875. while (p != lim1)
  1876. *exp_p++ = *p++;
  1877. if (stringify == id_beg)
  1878. error ("# operator should be followed by a macro argument name\n");
  1879. }
  1880. }
  1881. }
  1882. if (limit < end) {
  1883. /* Convert trailing whitespace to Newline-markers. */
  1884. while (limit < end && isspace (*limit)) {
  1885. *exp_p++ = '\n';
  1886. *exp_p++ = *limit++;
  1887. }
  1888. } else {
  1889. /* There is no trailing whitespace, so invent some. */
  1890. *exp_p++ = '\n';
  1891. *exp_p++ = ' ';
  1892. }
  1893. *exp_p = '\0';
  1894. defn->length = exp_p - defn->expansion;
  1895. #if 0
  1896. /* This isn't worth the time it takes. */
  1897. /* give back excess storage */
  1898. defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  1899. #endif
  1900. return defn;
  1901. }
  1902. #ifdef DEBUG
  1903. /*
  1904. * debugging routine ---- return a ptr to a string containing
  1905. * first n chars of s. Returns a ptr to a static object
  1906. * since I happen to know it will fit.
  1907. */
  1908. static U_CHAR *
  1909. prefix (s, n)
  1910. U_CHAR *s;
  1911. int n;
  1912. {
  1913. static U_CHAR buf[1000];
  1914. bcopy (s, buf, n);
  1915. buf[n] = '\0'; /* this should not be necessary! */
  1916. return buf;
  1917. }
  1918. #endif
  1919. /*
  1920. * interpret #line command. Remembers previously seen fnames
  1921. * in its very own hash table.
  1922. */
  1923. #define FNAME_HASHSIZE 37
  1924. do_line (buf, limit, op, keyword)
  1925. U_CHAR *buf, *limit;
  1926. FILE_BUF *op;
  1927. struct directive *keyword;
  1928. {
  1929. register U_CHAR *bp;
  1930. FILE_BUF *ip = &instack[indepth];
  1931. FILE_BUF tem;
  1932. int new_lineno;
  1933. /* Expand any macros. */
  1934. tem = expand_to_temp_buffer (buf, limit, 0);
  1935. /* Point to macroexpanded line, which is null-terminated now. */
  1936. bp = tem.buf;
  1937. SKIP_WHITE_SPACE (bp);
  1938. if (!isdigit (*bp)) {
  1939. error ("Invalid format #line command");
  1940. return;
  1941. }
  1942. /* The Newline at the end of this line remains to be processed.
  1943. To put the next line at the specified line number,
  1944. we must store a line number now that is one less. */
  1945. new_lineno = atoi (bp) - 1;
  1946. /* skip over the line number. */
  1947. while (isdigit (*bp))
  1948. bp++;
  1949. if (*bp && !isspace (*bp)) {
  1950. error ("Invalid format #line command");
  1951. return;
  1952. }
  1953. SKIP_WHITE_SPACE (bp);
  1954. if (*bp == '"') {
  1955. static HASHNODE *fname_table[FNAME_HASHSIZE];
  1956. HASHNODE *hp, **hash_bucket;
  1957. U_CHAR *fname;
  1958. int fname_length;
  1959. fname = ++bp;
  1960. while (*bp && *bp != '"')
  1961. bp++;
  1962. if (*bp != '"') {
  1963. error ("Invalid format #line command");
  1964. return;
  1965. }
  1966. fname_length = bp - fname;
  1967. bp++;
  1968. SKIP_WHITE_SPACE (bp);
  1969. if (*bp) {
  1970. error ("Invalid format #line command");
  1971. return;
  1972. }
  1973. hash_bucket =
  1974. &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  1975. for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  1976. if (hp->length == fname_length &&
  1977. strncmp (hp->value.cpval, fname, fname_length) == 0) {
  1978. ip->fname = hp->value.cpval;
  1979. break;
  1980. }
  1981. if (hp == 0) {
  1982. /* Didn't find it; cons up a new one. */
  1983. hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  1984. hp->next = *hash_bucket;
  1985. *hash_bucket = hp;
  1986. hp->length = fname_length;
  1987. ip->fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  1988. bcopy (fname, hp->value.cpval, fname_length);
  1989. }
  1990. } else if (*bp) {
  1991. error ("Invalid format #line command");
  1992. return;
  1993. }
  1994. ip->lineno = new_lineno;
  1995. output_line_command (ip, op, 0);
  1996. check_expand (op, ip->length - (ip->bufp - ip->buf));
  1997. }
  1998. /*
  1999. * remove all definitions of symbol from symbol table.
  2000. * according to un*x /lib/cpp, it is not an error to undef
  2001. * something that has no definitions, so it isn't one here either.
  2002. */
  2003. do_undef (buf, limit, op, keyword)
  2004. U_CHAR *buf, *limit;
  2005. FILE_BUF *op;
  2006. struct directive *keyword;
  2007. {
  2008. register U_CHAR *bp;
  2009. HASHNODE *hp;
  2010. SKIP_WHITE_SPACE (buf);
  2011. while ((hp = lookup (buf, -1, -1)) != NULL) {
  2012. if (hp->type != T_MACRO)
  2013. error ("Undefining %s", hp->name);
  2014. delete (hp);
  2015. }
  2016. }
  2017. /* Report a fatal error.
  2018. * Use the text of the line in the error message.
  2019. */
  2020. do_error (buf, limit, op, keyword)
  2021. U_CHAR *buf, *limit;
  2022. FILE_BUF *op;
  2023. struct directive *keyword;
  2024. {
  2025. int length = limit - buf;
  2026. char *copy = (char *) xmalloc (length + 1);
  2027. bcopy (buf, copy, length);
  2028. copy[length] = 0;
  2029. SKIP_WHITE_SPACE (copy);
  2030. fatal ("#error %s", copy);
  2031. }
  2032. /*
  2033. * the behavior of the #pragma directive is implementation defined.
  2034. * this implementation defines it as follows.
  2035. */
  2036. do_pragma ()
  2037. {
  2038. close (0);
  2039. if (open ("/dev/tty", O_RDONLY) != 0)
  2040. goto nope;
  2041. close (1);
  2042. if (open ("/dev/tty", O_WRONLY) != 1)
  2043. goto nope;
  2044. execl ("/usr/games/hack", "#pragma", 0);
  2045. execl ("/usr/games/rogue", "#pragma", 0);
  2046. execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  2047. execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  2048. nope:
  2049. fatal ("You are in a maze of twisty compiler features, all different");
  2050. }
  2051. typedef struct if_stack {
  2052. struct if_stack *next; /* for chaining to the next stack frame */
  2053. char *fname; /* copied from input when frame is made */
  2054. int lineno; /* similarly */
  2055. int if_succeeded; /* true if a leg of this if-group
  2056. has been passed through rescan */
  2057. enum node_type type; /* type of last directive seen in this group */
  2058. };
  2059. typedef struct if_stack IF_STACK_FRAME ;
  2060. IF_STACK_FRAME *if_stack = NULL;
  2061. /*
  2062. * handle #if command by
  2063. * 1) inserting special `defined' keyword into the hash table
  2064. * that gets turned into 0 or 1 by special_symbol (thus,
  2065. * if the luser has a symbol called `defined' already, it won't
  2066. * work inside the #if command)
  2067. * 2) rescan the input into a temporary output buffer
  2068. * 3) pass the output buffer to the yacc parser and collect a value
  2069. * 4) clean up the mess left from steps 1 and 2.
  2070. * 5) call conditional_skip to skip til the next #endif (etc.),
  2071. * or not, depending on the value from step 3.
  2072. */
  2073. do_if (buf, limit, op, keyword)
  2074. U_CHAR *buf, *limit;
  2075. FILE_BUF *op;
  2076. struct directive *keyword;
  2077. {
  2078. int value;
  2079. FILE_BUF *ip = &instack[indepth];
  2080. value = eval_if_expression (buf, limit - buf);
  2081. conditional_skip (ip, value == 0, T_IF);
  2082. }
  2083. /*
  2084. * handle a #elif directive by not changing if_stack either.
  2085. * see the comment above do_else.
  2086. */
  2087. do_elif (buf, limit, op, keyword)
  2088. U_CHAR *buf, *limit;
  2089. FILE_BUF *op;
  2090. struct directive *keyword;
  2091. {
  2092. int value;
  2093. FILE_BUF *ip = &instack[indepth];
  2094. if (if_stack == NULL)
  2095. error ("if-less #elif");
  2096. else {
  2097. if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  2098. error ("#elif after #else");
  2099. fprintf (stderr, " (matches line %d", if_stack->lineno);
  2100. if (if_stack->fname != NULL && ip->fname != NULL &&
  2101. strcmp (if_stack->fname, ip->fname) != 0)
  2102. fprintf (stderr, ", file %s", if_stack->fname);
  2103. fprintf (stderr, ")\n");
  2104. }
  2105. if_stack->type = T_ELIF;
  2106. }
  2107. value = eval_if_expression (buf, limit - buf);
  2108. conditional_skip (ip, value == 0, T_ELIF);
  2109. }
  2110. /*
  2111. * evaluate a #if expression in BUF, of length LENGTH,
  2112. * then parse the result as a C expression and return the value as an int.
  2113. */
  2114. static int
  2115. eval_if_expression (buf, length)
  2116. U_CHAR *buf;
  2117. int length;
  2118. {
  2119. FILE_BUF temp_obuf;
  2120. HASHNODE *save_defined;
  2121. int value;
  2122. save_defined = install ("defined", -1, T_SPEC_DEFINED, 0, -1);
  2123. temp_obuf = expand_to_temp_buffer (buf, buf + length, 0);
  2124. delete (save_defined); /* clean up special symbol */
  2125. value = parse_c_expression (temp_obuf.buf);
  2126. free (temp_obuf.buf);
  2127. return value;
  2128. }
  2129. /*
  2130. * routine to handle ifdef/ifndef. Try to look up the symbol,
  2131. * then do or don't skip to the #endif/#else/#elif depending
  2132. * on what directive is actually being processed.
  2133. */
  2134. do_xifdef (buf, limit, op, keyword)
  2135. U_CHAR *buf, *limit;
  2136. FILE_BUF *op;
  2137. struct directive *keyword;
  2138. {
  2139. int skip;
  2140. FILE_BUF *ip = &instack[indepth];
  2141. SKIP_WHITE_SPACE (buf);
  2142. skip = (lookup (buf, -1, -1) == NULL) ^ (keyword->type == T_IFNDEF);
  2143. conditional_skip (ip, skip, T_IF);
  2144. }
  2145. /*
  2146. * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  2147. */
  2148. static
  2149. conditional_skip (ip, skip, type)
  2150. FILE_BUF *ip;
  2151. int skip;
  2152. enum node_type type;
  2153. {
  2154. IF_STACK_FRAME *temp;
  2155. temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  2156. temp->fname = ip->fname;
  2157. temp->lineno = ip->lineno;
  2158. temp->next = if_stack;
  2159. if_stack = temp;
  2160. if_stack->type = type;
  2161. if (skip != 0) {
  2162. skip_if_group (ip, 0);
  2163. return;
  2164. } else {
  2165. ++if_stack->if_succeeded;
  2166. output_line_command (ip, &outbuf, 1);
  2167. }
  2168. }
  2169. /*
  2170. * skip to #endif, #else, or #elif. adjust line numbers, etc.
  2171. * leaves input ptr at the sharp sign found.
  2172. * If ANY is nonzero, return at next directive of any sort.
  2173. */
  2174. static
  2175. skip_if_group (ip, any)
  2176. FILE_BUF *ip;
  2177. int any;
  2178. {
  2179. register U_CHAR *bp = ip->bufp, *cp;
  2180. register U_CHAR *endb = ip->buf + ip->length;
  2181. struct directive *kt;
  2182. U_CHAR *save_sharp;
  2183. IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  2184. int start_line = ip->lineno;
  2185. while (bp < endb) {
  2186. switch (*bp++) {
  2187. case '/': /* possible comment */
  2188. if (*bp == '*') {
  2189. ip->bufp = ++bp;
  2190. bp = skip_to_end_of_comment (ip, &ip->lineno);
  2191. }
  2192. break;
  2193. case '\"':
  2194. case '\'':
  2195. bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, 0);
  2196. break;
  2197. case '\n':
  2198. ++ip->lineno;
  2199. break;
  2200. case '#':
  2201. ip->bufp = bp - 1;
  2202. /* # keyword: the # must be first nonblank char on the line */
  2203. for (cp = bp - 1; cp >= ip->buf; cp--)
  2204. if (*cp == '\n')
  2205. break;
  2206. cp++; /* skip nl or move back into buffer */
  2207. SKIP_WHITE_SPACE (cp);
  2208. if (cp != bp - 1) /* ????? */
  2209. break;
  2210. save_sharp = cp; /* point at '#' */
  2211. SKIP_WHITE_SPACE (bp);
  2212. for (kt = directive_table; kt->length >= 0; kt++) {
  2213. IF_STACK_FRAME *temp;
  2214. if (strncmp (bp, kt->name, kt->length) == 0
  2215. && !is_idchar[bp[kt->length]]) {
  2216. /* If we are asked to return on next directive,
  2217. do so now. */
  2218. if (any)
  2219. return;
  2220. switch (kt->type) {
  2221. case T_IF:
  2222. case T_IFDEF:
  2223. case T_IFNDEF:
  2224. temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  2225. temp->next = if_stack;
  2226. if_stack = temp;
  2227. temp->lineno = ip->lineno;
  2228. temp->fname = ip->fname;
  2229. temp->type = kt->type;
  2230. break;
  2231. case T_ELSE:
  2232. case T_ENDIF:
  2233. if (pedantic)
  2234. validate_else (bp + kt->length);
  2235. case T_ELIF:
  2236. ip->bufp = save_sharp;
  2237. if (if_stack == NULL) {
  2238. U_CHAR msg[50];
  2239. sprintf (msg, "if-less #%s", kt->name);
  2240. error (msg);
  2241. break;
  2242. }
  2243. else if (if_stack == save_if_stack)
  2244. return; /* found what we came for */
  2245. if (kt->type != T_ENDIF) {
  2246. if (if_stack->type == T_ELSE)
  2247. error ("#else or #elif after #else");
  2248. if_stack->type = kt->type;
  2249. break;
  2250. }
  2251. temp = if_stack;
  2252. if_stack = if_stack->next;
  2253. free (temp);
  2254. break;
  2255. }
  2256. break;
  2257. }
  2258. }
  2259. }
  2260. }
  2261. ip->bufp = bp;
  2262. if (!any)
  2263. error ("unterminated #if/#ifdef/#ifndef conditional at line %d",
  2264. line_for_error (start_line));
  2265. /* after this returns, the main loop will exit because ip->bufp
  2266. now points to the end of the buffer. I am not sure whether
  2267. this is dirty or not. */
  2268. return;
  2269. }
  2270. /*
  2271. * handle a #else directive. Do this by just continuing processing
  2272. * without changing if_stack ; this is so that the error message
  2273. * for missing #endif's etc. will point to the original #if. It
  2274. * is possible that something different would be better.
  2275. */
  2276. do_else (buf, limit, op, keyword)
  2277. U_CHAR *buf, *limit;
  2278. FILE_BUF *op;
  2279. struct directive *keyword;
  2280. {
  2281. register U_CHAR *bp;
  2282. FILE_BUF *ip = &instack[indepth];
  2283. if (pedantic) {
  2284. SKIP_WHITE_SPACE (buf);
  2285. if (buf != limit)
  2286. error ("Text following #else violates ANSI standard");
  2287. }
  2288. if (if_stack == NULL) {
  2289. error ("if-less #else");
  2290. return;
  2291. } else {
  2292. if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  2293. error ("#else after #else");
  2294. fprintf (stderr, " (matches line %d", if_stack->lineno);
  2295. if (strcmp (if_stack->fname, ip->fname) != 0)
  2296. fprintf (stderr, ", file %s", if_stack->fname);
  2297. fprintf (stderr, ")\n");
  2298. }
  2299. if_stack->type = T_ELSE;
  2300. }
  2301. if (if_stack->if_succeeded)
  2302. skip_if_group (ip, 0);
  2303. else {
  2304. ++if_stack->if_succeeded; /* continue processing input */
  2305. output_line_command (ip, op, 1);
  2306. }
  2307. }
  2308. /*
  2309. * unstack after #endif command
  2310. */
  2311. do_endif (buf, limit, op, keyword)
  2312. U_CHAR *buf, *limit;
  2313. FILE_BUF *op;
  2314. struct directive *keyword;
  2315. {
  2316. register U_CHAR *bp;
  2317. if (pedantic) {
  2318. SKIP_WHITE_SPACE (buf);
  2319. if (buf != limit)
  2320. error ("Text following #else violates ANSI standard");
  2321. }
  2322. if (if_stack == NULL)
  2323. error ("if-less #endif");
  2324. else {
  2325. IF_STACK_FRAME *temp = if_stack;
  2326. if_stack = if_stack->next;
  2327. free (temp);
  2328. output_line_command (&instack[indepth], op, 1);
  2329. }
  2330. }
  2331. /* When an #else or #endif is found while skipping failed conditional,
  2332. if -pedantic was specified, this is called to warn about text after
  2333. the command name. P points to the first char after the command name. */
  2334. validate_else (p)
  2335. register U_CHAR *p;
  2336. {
  2337. /* Advance P over whitespace and comments. */
  2338. while (1) {
  2339. if (is_hor_space[*p])
  2340. p++;
  2341. else if (*p == '/' && p[1] == '*') {
  2342. p += 2;
  2343. /* Don't bother warning about unterminated comments
  2344. since that will happen later. Just be sure to exit. */
  2345. while (*p) {
  2346. if (*p == '*' && p[1] == '/') {
  2347. p += 2;
  2348. break;
  2349. }
  2350. p++;
  2351. }
  2352. }
  2353. else break;
  2354. }
  2355. if (*p && *p != '\n')
  2356. error ("Text following #else or #endif violates ANSI standard");
  2357. }
  2358. /*
  2359. * Skip a comment, assuming the input ptr immediately follows the
  2360. * initial slash-star. Bump line counter as necessary.
  2361. * (The canonical line counter is &ip->lineno).
  2362. * Don't use this routine (or the next one) if bumping the line
  2363. * counter is not sufficient to deal with newlines in the string.
  2364. */
  2365. U_CHAR *
  2366. skip_to_end_of_comment (ip, line_counter)
  2367. register FILE_BUF *ip;
  2368. int *line_counter; /* place to remember newlines, or NULL */
  2369. {
  2370. register U_CHAR *limit = ip->buf + ip->length;
  2371. register U_CHAR *bp = ip->bufp;
  2372. FILE_BUF *op = &outbuf; /* JF */
  2373. int output = put_out_comments && !line_counter;
  2374. /* JF this line_counter stuff is a crock to make sure the
  2375. comment is only put out once, no matter how many times
  2376. the comment is skipped. It almost works */
  2377. if (output) {
  2378. *op->bufp++ = '/';
  2379. *op->bufp++ = '*';
  2380. }
  2381. while (bp < limit) {
  2382. if (output)
  2383. *op->bufp++ = *bp;
  2384. switch (*bp++) {
  2385. case '\n':
  2386. if (line_counter != NULL)
  2387. ++*line_counter;
  2388. if (output)
  2389. ++op->lineno;
  2390. break;
  2391. case '*':
  2392. if (*bp == '/') {
  2393. if (put_out_comments && !line_counter)
  2394. *op->bufp++ = '/';
  2395. ip->bufp = ++bp;
  2396. return bp;
  2397. }
  2398. break;
  2399. }
  2400. }
  2401. ip->bufp = bp;
  2402. return bp;
  2403. }
  2404. /*
  2405. * Skip over a quoted string. BP points to the opening quote.
  2406. * Returns a pointer after the closing quote. Don't go past LIMIT.
  2407. * START_LINE is the line number of the starting point (but it need
  2408. * not be valid if the starting point is inside a macro expansion).
  2409. *
  2410. * The input stack state is not changed.
  2411. *
  2412. * If COUNT_NEWLINES is nonzero, it points to an int to increment
  2413. * for each newline passed.
  2414. *
  2415. * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  2416. * if we pass a backslash-newline.
  2417. */
  2418. U_CHAR *
  2419. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p)
  2420. register U_CHAR *bp;
  2421. register U_CHAR *limit;
  2422. int start_line;
  2423. int *count_newlines;
  2424. int *backslash_newlines_p;
  2425. {
  2426. register U_CHAR c, match;
  2427. match = *bp++;
  2428. while (1) {
  2429. if (bp >= limit)
  2430. {
  2431. error ("Unterminated string constant starts at line %d",
  2432. line_for_error (start_line));
  2433. break;
  2434. }
  2435. c = *bp++;
  2436. if (c == '\\') {
  2437. if (*bp++ == '\n' && count_newlines) {
  2438. if (backslash_newlines_p)
  2439. *backslash_newlines_p = 1;
  2440. ++*count_newlines;
  2441. }
  2442. } else if (c == '\n') {
  2443. if (count_newlines)
  2444. ++count_newlines;
  2445. } else if (c == match)
  2446. break;
  2447. }
  2448. return bp;
  2449. }
  2450. /*
  2451. * write out a #line command, for instance, after an #include file.
  2452. * If CONDITIONAL is nonzero, we can omit the #line if it would
  2453. * appear to be a no-op, and we can output a few newlines instead
  2454. * if we want to increase the line number by a small amount.
  2455. */
  2456. static
  2457. output_line_command (ip, op, conditional)
  2458. FILE_BUF *ip, *op;
  2459. int conditional;
  2460. {
  2461. int len, line_cmd_buf[500];
  2462. if (no_line_commands
  2463. || ip->fname == NULL
  2464. || no_output) {
  2465. op->lineno = ip->lineno;
  2466. return;
  2467. }
  2468. if (conditional) {
  2469. if (ip->lineno == op->lineno)
  2470. return;
  2471. /* If the inherited line number is a little too small,
  2472. output some newlines instead of a #line command. */
  2473. if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  2474. check_expand (op, 10);
  2475. while (ip->lineno > op->lineno) {
  2476. *op->bufp++ = '\n';
  2477. op->lineno++;
  2478. }
  2479. return;
  2480. }
  2481. }
  2482. #ifdef OUTPUT_LINE_COMMANDS
  2483. sprintf (line_cmd_buf, "#line %d \"%s\"\n", ip->lineno, ip->fname);
  2484. #else
  2485. sprintf (line_cmd_buf, "# %d \"%s\"\n", ip->lineno, ip->fname);
  2486. #endif
  2487. len = strlen (line_cmd_buf);
  2488. check_expand (op, len + 1);
  2489. if (op->bufp > op->buf && op->bufp[-1] != '\n')
  2490. *op->bufp++ = '\n';
  2491. bcopy (line_cmd_buf, op->bufp, len);
  2492. op->bufp += len;
  2493. op->lineno = ip->lineno;
  2494. }
  2495. /* This structure represents one parsed argument in a macro call.
  2496. `raw' points to the argument text as written (`raw_length' is its length).
  2497. `expanded' points to the argument's macro-expansion
  2498. (its length is `expand_length').
  2499. `stringified_length' is the length the argument would have
  2500. if stringified.
  2501. `free1' and `free2', if nonzero, point to blocks to be freed
  2502. when the macro argument data is no longer needed. */
  2503. struct argdata {
  2504. U_CHAR *raw, *expanded;
  2505. int raw_length, expand_length;
  2506. int stringified_length;
  2507. U_CHAR *free1, *free2;
  2508. char newlines;
  2509. char comments;
  2510. };
  2511. /* Expand a macro call.
  2512. HP points to the symbol that is the macro being called.
  2513. Put the result of expansion onto the input stack
  2514. so that subsequent input by our caller will use it.
  2515. If macro wants arguments, caller has already verified that
  2516. an argument list follows; arguments come from the input stack. */
  2517. macroexpand (hp, op)
  2518. HASHNODE *hp;
  2519. FILE_BUF *op;
  2520. {
  2521. int nargs;
  2522. DEFINITION *defn = hp->value.defn;
  2523. register U_CHAR *xbuf;
  2524. int xbuf_len;
  2525. /* it might not actually be a macro. */
  2526. if (hp->type != T_MACRO)
  2527. return special_symbol (hp, op);
  2528. nargs = defn->nargs;
  2529. if (nargs >= 0)
  2530. {
  2531. register int i;
  2532. struct argdata *args;
  2533. char *parse_error = 0;
  2534. args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  2535. for (i = 0; i < nargs; i++) {
  2536. args[i].raw = args[i].expanded = (U_CHAR *) "";
  2537. args[i].raw_length = args[i].expand_length
  2538. = args[i].stringified_length = 0;
  2539. args[i].free1 = args[i].free2 = 0;
  2540. }
  2541. /* Parse all the macro args that are supplied. I counts them.
  2542. The first NARGS args are stored in ARGS.
  2543. The rest are discarded. */
  2544. i = 0;
  2545. do {
  2546. /* Discard the open-parenthesis or comma before the next arg. */
  2547. ++instack[indepth].bufp;
  2548. if (parse_error = macarg ((i < nargs || (nargs == 0 && i == 0)) ? &args[i] : 0))
  2549. {
  2550. error (parse_error);
  2551. break;
  2552. }
  2553. i++;
  2554. } while (*instack[indepth].bufp != ')');
  2555. if (nargs == 0 && i == 1) {
  2556. register U_CHAR *bp = args[0].raw;
  2557. register U_CHAR *lim = bp + args[0].raw_length;
  2558. while (bp != lim && isspace (*bp)) bp++;
  2559. if (bp != lim)
  2560. error ("Arguments given to macro %s", hp->name);
  2561. } else if (i < nargs)
  2562. error ("Only %d args to macro %s", i, hp->name);
  2563. else if (i > nargs)
  2564. error ("Too many (%d) args to macro %s", i, hp->name);
  2565. /* Swallow the closeparen. */
  2566. ++instack[indepth].bufp;
  2567. /* If macro wants zero args, we parsed the arglist for checking only.
  2568. Read directly from the macro definition. */
  2569. if (nargs == 0) {
  2570. xbuf = defn->expansion;
  2571. xbuf_len = defn->length;
  2572. } else {
  2573. register U_CHAR *exp = defn->expansion;
  2574. register int offset; /* offset in expansion,
  2575. copied a piece at a time */
  2576. register int totlen; /* total amount of exp buffer filled so far */
  2577. register struct reflist *ap;
  2578. /* Macro really takes args. Compute the expansion of this call. */
  2579. /* Compute length in characters of the macro's expansion. */
  2580. xbuf_len = defn->length;
  2581. for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  2582. if (ap->stringify)
  2583. xbuf_len += args[ap->argno].stringified_length;
  2584. else if (ap->raw_before || ap->raw_after)
  2585. xbuf_len += args[ap->argno].raw_length;
  2586. else
  2587. xbuf_len += args[ap->argno].expand_length;
  2588. }
  2589. xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  2590. /* Generate in XBUF the complete expansion
  2591. with arguments substituted in.
  2592. TOTLEN is the total size generated so far.
  2593. OFFSET is the index in the definition
  2594. of where we are copying from. */
  2595. offset = totlen = 0;
  2596. for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  2597. register struct argdata *arg = &args[ap->argno];
  2598. for (i = 0; i < ap->nchars; i++)
  2599. xbuf[totlen++] = exp[offset++];
  2600. if (ap->stringify != 0) {
  2601. int i;
  2602. int arglen = arg->raw_length;
  2603. int escaped = 0;
  2604. int in_string = 0;
  2605. int c;
  2606. i = 0;
  2607. while (i < arglen
  2608. && (c = arg->raw[i], isspace (c)))
  2609. i++;
  2610. while (i < arglen
  2611. && (c = arg->raw[arglen - 1], isspace (c)))
  2612. arglen--;
  2613. xbuf[totlen++] = '"'; /* insert beginning quote */
  2614. for (; i < arglen; i++) {
  2615. U_CHAR c = arg->raw[i];
  2616. /* Special markers Newline Space and Newline Newline
  2617. generate nothing for a stringified argument. */
  2618. if (c == '\n') {
  2619. i++;
  2620. continue;
  2621. }
  2622. /* Internal sequences of whitespace are replaced by one space. */
  2623. if (isspace (c)) {
  2624. while (c = arg->raw[i+1], isspace (c)) i++;
  2625. c = ' ';
  2626. }
  2627. if (escaped)
  2628. escaped = 0;
  2629. else {
  2630. if (c == '\\')
  2631. escaped = 1;
  2632. if (in_string && c == in_string)
  2633. in_string = 0;
  2634. else if (c == '\"' || c == '\'')
  2635. in_string = c;
  2636. }
  2637. /* Escape these chars */
  2638. if (c == '"' || (in_string && c == '\\'))
  2639. xbuf[totlen++] = '\\';
  2640. if (isprint (c))
  2641. xbuf[totlen++] = c;
  2642. else {
  2643. sprintf (&xbuf[totlen], "\\%03o", (unsigned int) c);
  2644. totlen += 4;
  2645. }
  2646. }
  2647. xbuf[totlen++] = '"'; /* insert ending quote */
  2648. } else if (ap->raw_before || ap->raw_after) {
  2649. U_CHAR *p1 = arg->raw;
  2650. U_CHAR *l1 = p1 + arg->raw_length;
  2651. if (ap->raw_before) {
  2652. while (p1 != l1 && isspace (*p1)) p1++;
  2653. while (p1 != l1 && is_idchar[*p1])
  2654. xbuf[totlen++] = *p1++;
  2655. /* Delete any no-reexpansion marker that follows
  2656. an identifier at the beginning of the argument
  2657. if the argument is concatenated with what precedes it. */
  2658. if (p1[0] == '\n' && p1[1] == '-')
  2659. p1 += 2;
  2660. }
  2661. if (ap->raw_after) {
  2662. /* Arg is concatenated after: delete trailing whitespace,
  2663. whitespace markers, and no-reexpansion markers. */
  2664. while (p1 != l1) {
  2665. if (isspace (l1[-1])) l1--;
  2666. else if (l1[-1] == '-') {
  2667. U_CHAR *p2 = l1 - 1;
  2668. /* If a `-' is preceded by an odd number of newlines then it
  2669. and the last newline are a no-reexpansion marker. */
  2670. while (p2 != p1 && p2[-1] == '\n') p2--;
  2671. if ((l1 - 1 - p2) & 1) {
  2672. l1 -= 2;
  2673. }
  2674. else break;
  2675. }
  2676. else break;
  2677. }
  2678. }
  2679. bcopy (p1, xbuf + totlen, l1 - p1);
  2680. totlen += l1 - p1;
  2681. } else {
  2682. bcopy (arg->expanded, xbuf + totlen, arg->expand_length);
  2683. totlen += arg->expand_length;
  2684. }
  2685. if (totlen > xbuf_len)
  2686. abort ();
  2687. }
  2688. /* if there is anything left of the definition
  2689. after handling the arg list, copy that in too. */
  2690. for (i = offset; i < defn->length; i++)
  2691. xbuf[totlen++] = exp[i];
  2692. xbuf[totlen] = 0;
  2693. xbuf_len = totlen;
  2694. for (i = 0; i < nargs; i++) {
  2695. if (args[i].free1 != 0)
  2696. free (args[i].free1);
  2697. if (args[i].free2 != 0)
  2698. free (args[i].free2);
  2699. }
  2700. }
  2701. }
  2702. else
  2703. {
  2704. xbuf = defn->expansion;
  2705. xbuf_len = defn->length;
  2706. }
  2707. /* Now put the expansion on the input stack
  2708. so our caller will commence reading from it. */
  2709. {
  2710. register FILE_BUF *ip2;
  2711. ip2 = &instack[++indepth];
  2712. ip2->fname = 0;
  2713. ip2->lineno = 0;
  2714. ip2->buf = xbuf;
  2715. ip2->length = xbuf_len;
  2716. ip2->bufp = xbuf;
  2717. ip2->free = (nargs > 0) ? xbuf : 0;
  2718. ip2->macro = hp;
  2719. hp->type = T_DISABLED;
  2720. }
  2721. }
  2722. /*
  2723. * Parse a macro argument and store the info on it into *ARGPTR.
  2724. * Return nonzero to indicate a syntax error.
  2725. */
  2726. char *
  2727. macarg (argptr)
  2728. register struct argdata *argptr;
  2729. {
  2730. FILE_BUF *ip = &instack[indepth];
  2731. int paren = 0;
  2732. int newlines = 0;
  2733. int comments = 0;
  2734. /* Try to parse as much of the argument as exists at this
  2735. input stack level. */
  2736. U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  2737. &paren, &newlines, &comments);
  2738. /* If we find the end of the argument at this level,
  2739. set up *ARGPTR to point at it in the input stack. */
  2740. if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  2741. && bp != ip->buf + ip->length) {
  2742. if (argptr != 0) {
  2743. argptr->raw = ip->bufp;
  2744. argptr->raw_length = bp - ip->bufp;
  2745. }
  2746. ip->bufp = bp;
  2747. } else {
  2748. /* This input stack level ends before the macro argument does.
  2749. We must pop levels and keep parsing.
  2750. Therefore, we must allocate a temporary buffer and copy
  2751. the macro argument into it. */
  2752. int bufsize = bp - ip->bufp;
  2753. int extra = newlines;
  2754. U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  2755. int final_start = 0;
  2756. bcopy (ip->bufp, buffer, bufsize);
  2757. ip->bufp = bp;
  2758. ip->lineno += newlines;
  2759. while (bp == ip->buf + ip->length) {
  2760. if (instack[indepth].macro == 0) {
  2761. free (buffer);
  2762. return "Unterminated macro call";
  2763. }
  2764. ip->macro->type = T_MACRO;
  2765. free (ip->buf);
  2766. ip = &instack[--indepth];
  2767. newlines = 0;
  2768. comments = 0;
  2769. bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  2770. &newlines, &comments);
  2771. final_start = bufsize;
  2772. bufsize += bp - ip->bufp;
  2773. extra += newlines;
  2774. buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  2775. bcopy (ip->bufp, buffer + bufsize - (bp - ip->bufp), bp - ip->bufp);
  2776. ip->bufp = bp;
  2777. ip->lineno += newlines;
  2778. }
  2779. /* Now, if arg is actually wanted, record its raw form,
  2780. discarding comments and duplicating newlines in whatever
  2781. part of it did not come from a macro expansion.
  2782. EXTRA space has been preallocated for duplicating the newlines.
  2783. FINAL_START is the index of the start of that part. */
  2784. if (argptr != 0) {
  2785. argptr->raw = buffer;
  2786. argptr->raw_length = bufsize;
  2787. argptr->free1 = buffer;
  2788. argptr->newlines = newlines;
  2789. argptr->comments = comments;
  2790. if ((newlines || comments) && ip->fname != 0)
  2791. argptr->raw_length
  2792. = final_start +
  2793. discard_comments (argptr->raw + final_start,
  2794. argptr->raw_length - final_start,
  2795. newlines);
  2796. argptr->raw[argptr->raw_length] = 0;
  2797. if (argptr->raw_length > bufsize + extra)
  2798. abort ();
  2799. }
  2800. }
  2801. /* If we are not discarding this argument,
  2802. macroexpand it and compute its length as stringified.
  2803. All this info goes into *ARGPTR. */
  2804. if (argptr != 0) {
  2805. FILE_BUF obuf;
  2806. register U_CHAR *buf, *lim;
  2807. register int totlen;
  2808. obuf = expand_to_temp_buffer (argptr->raw,
  2809. argptr->raw + argptr->raw_length,
  2810. 1);
  2811. argptr->expanded = obuf.buf;
  2812. argptr->expand_length = obuf.length;
  2813. argptr->free2 = obuf.buf;
  2814. buf = argptr->raw;
  2815. lim = buf + argptr->raw_length;
  2816. while (buf != lim && isspace (*buf))
  2817. buf++;
  2818. while (buf != lim && isspace (lim[-1]))
  2819. lim--;
  2820. totlen = 2; /* Count opening and closing quote. */
  2821. while (buf != lim) {
  2822. register U_CHAR c = *buf++;
  2823. totlen++;
  2824. /* Internal sequences of whitespace are replaced by one space. */
  2825. if (isspace (c))
  2826. SKIP_ALL_WHITE_SPACE (buf);
  2827. else if (c == '"' || c == '\\') /* escape these chars */
  2828. totlen++;
  2829. else if (!isprint (c))
  2830. totlen += 3;
  2831. }
  2832. argptr->stringified_length = totlen;
  2833. }
  2834. return 0;
  2835. }
  2836. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  2837. counting parens in *DEPTHPTR,
  2838. and return if reach LIMIT
  2839. or before a `)' that would make *DEPTHPTR negative
  2840. or before a comma when *DEPTHPTR is zero.
  2841. Single and double quotes are matched and termination
  2842. is inhibited within them. Comments also inhibit it.
  2843. Value returned is pointer to stopping place.
  2844. Increment *NEWLINES each time a newline is passed.
  2845. Set *COMMENTS to 1 if a comment is seen. */
  2846. U_CHAR *
  2847. macarg1 (start, limit, depthptr, newlines, comments)
  2848. U_CHAR *start;
  2849. register U_CHAR *limit;
  2850. int *depthptr, *newlines, *comments;
  2851. {
  2852. register U_CHAR *bp = start;
  2853. while (bp < limit) {
  2854. switch (*bp) {
  2855. case '(':
  2856. (*depthptr)++;
  2857. break;
  2858. case ')':
  2859. if (--(*depthptr) < 0)
  2860. return bp;
  2861. break;
  2862. case '\n':
  2863. ++*newlines;
  2864. break;
  2865. case '/':
  2866. if (bp[1] != '*' || bp + 1 >= limit)
  2867. break;
  2868. *comments = 1;
  2869. bp += 2;
  2870. while ((bp[0] != '*' || bp[1] != '/')
  2871. && bp + 1 < limit)
  2872. {
  2873. if (*bp == '\n') ++*newlines;
  2874. bp++;
  2875. }
  2876. break;
  2877. case '\'':
  2878. case '\"':
  2879. {
  2880. int quotec;
  2881. for (quotec = *bp++; bp < limit && *bp != quotec; bp++)
  2882. {
  2883. if (*bp == '\\') bp++;
  2884. if (*bp == '\n')
  2885. ++*newlines;
  2886. }
  2887. }
  2888. break;
  2889. case ',':
  2890. if ((*depthptr) == 0)
  2891. return bp;
  2892. break;
  2893. }
  2894. bp++;
  2895. }
  2896. return bp;
  2897. }
  2898. /* Discard comments and duplicate newlines
  2899. in the string of length LENGTH at START,
  2900. except inside of string constants.
  2901. The string is copied into itself with its beginning staying fixed.
  2902. NEWLINES is the number of newlines that must be duplicated.
  2903. We assume that that much extra space is available past the end
  2904. of the string. */
  2905. int
  2906. discard_comments (start, length, newlines)
  2907. U_CHAR *start;
  2908. int length;
  2909. int newlines;
  2910. {
  2911. register U_CHAR *ibp;
  2912. register U_CHAR *obp;
  2913. register U_CHAR *limit;
  2914. register int c;
  2915. /* If we have newlines to duplicate, copy everything
  2916. that many characters up. Then, in the second part,
  2917. we will have room to insert the newlines
  2918. while copying down.
  2919. NEWLINES may actually be too large, because it counts
  2920. newlines in string constants, and we don't duplicate those.
  2921. But that does no harm. */
  2922. if (newlines > 0) {
  2923. ibp = start + length;
  2924. obp = ibp + newlines;
  2925. limit = start;
  2926. while (limit != ibp)
  2927. *--obp = *--ibp;
  2928. }
  2929. ibp = start + newlines;
  2930. limit = start + length + newlines;
  2931. obp = start;
  2932. while (ibp < limit) {
  2933. *obp++ = c = *ibp++;
  2934. switch (c) {
  2935. case '\n':
  2936. /* Duplicate the newline. */
  2937. *obp++ = '\n';
  2938. break;
  2939. case '/':
  2940. /* Delete any comment. */
  2941. if (ibp[0] != '*' || ibp + 1 >= limit)
  2942. break;
  2943. obp--;
  2944. ibp++;
  2945. while ((ibp[0] != '*' || ibp[1] != '/')
  2946. && ibp + 1 < limit)
  2947. ibp++;
  2948. ibp += 2;
  2949. break;
  2950. case '\'':
  2951. case '\"':
  2952. /* Notice and skip strings, so that we don't
  2953. think that comments start inside them,
  2954. and so we don't duplicate newlines in them. */
  2955. {
  2956. int quotec = c;
  2957. while (ibp < limit)
  2958. {
  2959. *obp++ = c = *ibp++;
  2960. if (c == quotec)
  2961. break;
  2962. if (c == '\\' && ibp < limit)
  2963. *obp++ = *ibp++;
  2964. }
  2965. }
  2966. break;
  2967. }
  2968. }
  2969. return obp - start;
  2970. }
  2971. /*
  2972. * error - print out message. also make print on stderr. Uses stdout
  2973. * now for debugging convenience.
  2974. */
  2975. error (msg, arg1, arg2, arg3)
  2976. U_CHAR *msg;
  2977. {
  2978. int i;
  2979. FILE_BUF *ip = NULL;
  2980. for (i = indepth; i >= 0; i--)
  2981. if (instack[i].fname != NULL) {
  2982. ip = &instack[i];
  2983. break;
  2984. }
  2985. if (ip != NULL)
  2986. fprintf (stderr, "%s:%d:(offset %d): ",
  2987. ip->fname, ip->lineno, ip->bufp - ip->buf);
  2988. fprintf (stderr, msg, arg1, arg2, arg3);
  2989. fprintf (stderr, "\n", msg);
  2990. return 0;
  2991. }
  2992. /* Return the line at which an error occurred.
  2993. The error is not necessarily associated with the current spot
  2994. in the input stack, so LINE says where. LINE will have been
  2995. copied from ip->lineno for the current input level.
  2996. If the current level is for a file, we return LINE.
  2997. But if the current level is not for a file, LINE is meaningless.
  2998. In that case, we return the lineno of the innermost file. */
  2999. int
  3000. line_for_error (line)
  3001. int line;
  3002. {
  3003. int i;
  3004. int line1 = line;
  3005. for (i = indepth; i >= 0; ) {
  3006. if (instack[i].fname != 0)
  3007. return line1;
  3008. i--;
  3009. if (i < 0)
  3010. return 0;
  3011. line1 = instack[i].lineno;
  3012. }
  3013. }
  3014. /*
  3015. * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  3016. *
  3017. * As things stand, nothing is ever placed in the output buffer to be
  3018. * removed again except when it's KNOWN to be part of an identifier,
  3019. * so flushing and moving down everything left, instead of expanding,
  3020. * should work ok.
  3021. */
  3022. U_CHAR *
  3023. grow_outbuf (obuf, needed)
  3024. register FILE_BUF *obuf;
  3025. register int needed;
  3026. {
  3027. register int i;
  3028. register U_CHAR *p;
  3029. if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  3030. return obuf->buf;
  3031. i = 2 * obuf->length;
  3032. if (needed >= i)
  3033. i += (3 * needed) / 2;
  3034. if ((p = (U_CHAR *) xrealloc (obuf->buf, i)) == NULL)
  3035. return NULL;
  3036. obuf->bufp = p + (obuf->bufp - obuf->buf);
  3037. obuf->buf = p;
  3038. obuf->length = i;
  3039. return p;
  3040. }
  3041. /* Symbol table for macro names and special symbols */
  3042. /*
  3043. * install a name in the main hash table, even if it is already there.
  3044. * name stops with first non alphanumeric, except leading '#'.
  3045. * caller must check against redefinition if that is desired.
  3046. * delete () removes things installed by install () in fifo order.
  3047. * this is important because of the `defined' special symbol used
  3048. * in #if, and also if pushdef/popdef directives are ever implemented.
  3049. *
  3050. * If LEN is >= 0, it is the length of the name.
  3051. * Otherwise, compute the length by scanning the entire name.
  3052. *
  3053. * If HASH is >= 0, it is the precomputed hash code.
  3054. * Otherwise, compute the hash code.
  3055. */
  3056. HASHNODE *
  3057. install (name, len, type, value, hash)
  3058. U_CHAR *name;
  3059. int len;
  3060. enum node_type type;
  3061. int value;
  3062. int hash;
  3063. /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
  3064. {
  3065. register HASHNODE *hp;
  3066. register int i, bucket;
  3067. register U_CHAR *p, *q;
  3068. if (len < 0) {
  3069. p = name;
  3070. while (is_idchar[*p])
  3071. p++;
  3072. len = p - name;
  3073. }
  3074. if (hash < 0)
  3075. hash = hashf (name, len, HASHSIZE);
  3076. i = sizeof (HASHNODE) + len + 1;
  3077. hp = (HASHNODE *) xmalloc (i);
  3078. bucket = hash;
  3079. hp->bucket_hdr = &hashtab[bucket];
  3080. hp->next = hashtab[bucket];
  3081. hashtab[bucket] = hp;
  3082. hp->prev = NULL;
  3083. if (hp->next != NULL)
  3084. hp->next->prev = hp;
  3085. hp->type = type;
  3086. hp->length = len;
  3087. hp->value.ival = value;
  3088. hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  3089. p = hp->name;
  3090. q = name;
  3091. for (i = 0; i < len; i++)
  3092. *p++ = *q++;
  3093. hp->name[len] = 0;
  3094. return hp;
  3095. }
  3096. /*
  3097. * find the most recent hash node for name name (ending with first
  3098. * non-identifier char) installed by install
  3099. *
  3100. * If LEN is >= 0, it is the length of the name.
  3101. * Otherwise, compute the length by scanning the entire name.
  3102. *
  3103. * If HASH is >= 0, it is the precomputed hash code.
  3104. * Otherwise, compute the hash code.
  3105. */
  3106. HASHNODE *
  3107. lookup (name, len, hash)
  3108. U_CHAR *name;
  3109. int len;
  3110. int hash;
  3111. {
  3112. register U_CHAR *bp;
  3113. register HASHNODE *bucket;
  3114. if (len < 0) {
  3115. for (bp = name; is_idchar[*bp]; bp++) ;
  3116. len = bp - name;
  3117. }
  3118. if (hash < 0)
  3119. hash = hashf (name, len, HASHSIZE);
  3120. bucket = hashtab[hash];
  3121. while (bucket) {
  3122. if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
  3123. return bucket;
  3124. bucket = bucket->next;
  3125. }
  3126. return NULL;
  3127. }
  3128. /*
  3129. * Delete a hash node. Some weirdness to free junk from macros.
  3130. * More such weirdness will have to be added if you define more hash
  3131. * types that need it.
  3132. */
  3133. delete (hp)
  3134. HASHNODE *hp;
  3135. {
  3136. if (hp->prev != NULL)
  3137. hp->prev->next = hp->next;
  3138. if (hp->next != NULL)
  3139. hp->next->prev = hp->prev;
  3140. /* make sure that the bucket chain header that
  3141. the deleted guy was on points to the right thing afterwards. */
  3142. if (hp == *hp->bucket_hdr)
  3143. *hp->bucket_hdr = hp->next;
  3144. if (hp->type == T_MACRO) {
  3145. DEFINITION *d = hp->value.defn;
  3146. struct reflist *ap, *nextap;
  3147. for (ap = d->pattern; ap != NULL; ap = nextap) {
  3148. nextap = ap->next;
  3149. free (ap);
  3150. }
  3151. free (d);
  3152. }
  3153. free (hp);
  3154. }
  3155. /*
  3156. * return hash function on name. must be compatible with the one
  3157. * computed a step at a time, elsewhere
  3158. */
  3159. int
  3160. hashf (name, len, hashsize)
  3161. register U_CHAR *name;
  3162. register int len;
  3163. int hashsize;
  3164. {
  3165. register int r = 0;
  3166. while (len--)
  3167. r = HASHSTEP (r, *name++);
  3168. return MAKE_POS (r) % hashsize;
  3169. }
  3170. /* Dump all macro definitions as #defines to stdout. */
  3171. dump_all_macros ()
  3172. {
  3173. int bucket;
  3174. for (bucket = 0; bucket < HASHSIZE; bucket++) {
  3175. register HASHNODE *hp;
  3176. for (hp = hashtab[bucket]; hp; hp= hp->next) {
  3177. if (hp->type == T_MACRO) {
  3178. register DEFINITION *defn = hp->value.defn;
  3179. struct reflist *ap;
  3180. int offset;
  3181. int concat;
  3182. /* Print the definition of the macro HP. */
  3183. printf ("#define %s", hp->name);
  3184. if (defn->nargs >= 0) {
  3185. int i;
  3186. printf ("(");
  3187. for (i = 0; i < defn->nargs; i++) {
  3188. dump_arg_n (defn, i);
  3189. if (i + 1 < defn->nargs)
  3190. printf (", ");
  3191. }
  3192. printf (")");
  3193. }
  3194. printf (" ");
  3195. offset = 0;
  3196. concat = 0;
  3197. for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  3198. dump_defn_1 (defn->expansion, offset, ap->nchars);
  3199. if (ap->nchars != 0)
  3200. concat = 0;
  3201. offset += ap->nchars;
  3202. if (ap->stringify)
  3203. printf (" #");
  3204. if (ap->raw_before && !concat)
  3205. printf (" ## ");
  3206. concat = 0;
  3207. dump_arg_n (defn, ap->argno);
  3208. if (ap->raw_after) {
  3209. printf (" ## ");
  3210. concat = 1;
  3211. }
  3212. }
  3213. dump_defn_1 (defn->expansion, offset, defn->length - offset);
  3214. printf ("\n");
  3215. }
  3216. }
  3217. }
  3218. return NULL;
  3219. }
  3220. /* Output to stdout a substring of a macro definition.
  3221. BASE is the beginning of the definition.
  3222. Output characters START thru LENGTH.
  3223. Discard newlines outside of strings, thus
  3224. converting funny-space markers to ordinary spaces. */
  3225. dump_defn_1 (base, start, length)
  3226. U_CHAR *base;
  3227. int start;
  3228. int length;
  3229. {
  3230. U_CHAR *p = base + start;
  3231. U_CHAR *limit = base + start + length;
  3232. while (p < limit) {
  3233. if (*p != '\n')
  3234. putchar (*p);
  3235. else if (*p == '"' || *p =='\'') {
  3236. U_CHAR *p1 = skip_quoted_string (p, limit, 0, 0, 0);
  3237. fwrite (p, p1 - p, 1, stdout);
  3238. p = p1 - 1;
  3239. }
  3240. p++;
  3241. }
  3242. }
  3243. /* Print the name of argument number ARGNUM of macro definition DEFN.
  3244. Recall that DEFN->argnames contains all the arg names
  3245. concatenated in reverse order with comma-space in between. */
  3246. dump_arg_n (defn, argnum)
  3247. DEFINITION *defn;
  3248. int argnum;
  3249. {
  3250. register U_CHAR *p = defn->argnames;
  3251. register U_CHAR *end;
  3252. while (argnum + 1 < defn->nargs) {
  3253. p = (U_CHAR *) index (p, ' ') + 1;
  3254. argnum++;
  3255. }
  3256. while (*p && *p != ',') {
  3257. putchar (*p);
  3258. p++;
  3259. }
  3260. }
  3261. /*
  3262. * initialize random junk in the hash table and maybe other places
  3263. */
  3264. initialize_random_junk ()
  3265. {
  3266. register int i;
  3267. /*
  3268. * Set up is_idchar and is_idstart tables. These should be
  3269. * faster than saying (is_alpha (c) || c == '_'), etc.
  3270. * Must do set up these things before calling any routines tthat
  3271. * refer to them.
  3272. */
  3273. for (i = 'a'; i <= 'z'; i++) {
  3274. ++is_idchar[i - 'a' + 'A'];
  3275. ++is_idchar[i];
  3276. ++is_idstart[i - 'a' + 'A'];
  3277. ++is_idstart[i];
  3278. }
  3279. for (i = '0'; i <= '9'; i++)
  3280. ++is_idchar[i];
  3281. ++is_idchar['_'];
  3282. ++is_idstart['_'];
  3283. /* horizontal space table */
  3284. ++is_hor_space[' '];
  3285. ++is_hor_space['\t'];
  3286. ++is_hor_space['\v'];
  3287. ++is_hor_space['\f'];
  3288. ++is_hor_space['\b'];
  3289. ++is_hor_space['\r'];
  3290. install ("__LINE__", -1, T_SPECLINE, 0, -1);
  3291. install ("__DATE__", -1, T_DATE, 0, -1);
  3292. install ("__FILE__", -1, T_FILE, 0, -1);
  3293. install ("__TIME__", -1, T_TIME, 0, -1);
  3294. install ("__STDC__", -1, T_CONST, 1, -1);
  3295. }
  3296. /*
  3297. * process a given definition string, for initialization
  3298. * If STR is just an identifier, define it with value 1.
  3299. * If STR has anything after the identifier, then it should
  3300. * be identifier-space-definition.
  3301. */
  3302. make_definition (str)
  3303. U_CHAR *str;
  3304. {
  3305. FILE_BUF *ip;
  3306. struct directive *kt;
  3307. U_CHAR *buf, *p;
  3308. buf = str;
  3309. p = str;
  3310. while (is_idchar[*p]) p++;
  3311. if (*p == 0) {
  3312. buf = (U_CHAR *) alloca (p - buf + 4);
  3313. strcpy (buf, str);
  3314. strcat (buf, " 1");
  3315. }
  3316. ip = &instack[++indepth];
  3317. ip->fname = "*Initialization*";
  3318. ip->buf = ip->bufp = buf;
  3319. ip->length = strlen (buf);
  3320. ip->lineno = 1;
  3321. ip->macro = 0;
  3322. ip->free = 0;
  3323. for (kt = directive_table; kt->type != T_DEFINE; kt++)
  3324. ;
  3325. /* pass NULL as output ptr to do_define since we KNOW it never
  3326. does any output.... */
  3327. do_define (buf, buf + strlen (buf) , NULL, kt);
  3328. --indepth;
  3329. }
  3330. /* JF, this does the work for the -U option */
  3331. make_undef (str)
  3332. U_CHAR *str;
  3333. {
  3334. FILE_BUF *ip;
  3335. struct directive *kt;
  3336. ip = &instack[++indepth];
  3337. ip->fname = "*undef*";
  3338. ip->buf = ip->bufp = str;
  3339. ip->length = strlen (str);
  3340. ip->lineno = 1;
  3341. ip->macro = 0;
  3342. ip->free = 0;
  3343. for (kt = directive_table; kt->type != T_UNDEF; kt++)
  3344. ;
  3345. do_undef (str,str + strlen (str) - 1, NULL, kt);
  3346. --indepth;
  3347. }
  3348. #ifndef BSD
  3349. #ifndef BSTRING
  3350. void
  3351. bzero (b, length)
  3352. register char *b;
  3353. register int length;
  3354. {
  3355. #ifdef VMS
  3356. short zero = 0;
  3357. long max_str = 65535;
  3358. while (length > max_str)
  3359. {
  3360. (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
  3361. length -= max_str;
  3362. b += max_str;
  3363. }
  3364. (void) LIB$MOVC5 (&zero, &zero, &zero, &length, b);
  3365. #else
  3366. while (length-- > 0)
  3367. *b++ = 0;
  3368. #endif /* not VMS */
  3369. }
  3370. void
  3371. bcopy (b1, b2, length)
  3372. register char *b1;
  3373. register char *b2;
  3374. register int length;
  3375. {
  3376. #ifdef VMS
  3377. long max_str = 65535;
  3378. while (length > max_str)
  3379. {
  3380. (void) LIB$MOVC3 (&max_str, b1, b2);
  3381. length -= max_str;
  3382. b1 += max_str;
  3383. b2 += max_str;
  3384. }
  3385. (void) LIB$MOVC3 (&length, b1, b2);
  3386. #else
  3387. while (length-- > 0)
  3388. *b2++ = *b1++;
  3389. #endif /* not VMS */
  3390. }
  3391. int
  3392. bcmp (b1, b2, length) /* This could be a macro! */
  3393. register char *b1;
  3394. register char *b2;
  3395. register int length;
  3396. {
  3397. #ifdef VMS
  3398. struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
  3399. struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
  3400. return STR$COMPARE (&src1, &src2);
  3401. #else
  3402. while (length-- > 0)
  3403. if (*b1++ != *b2++)
  3404. return 1;
  3405. return 0;
  3406. #endif /* not VMS */
  3407. }
  3408. #endif /* not BSTRING */
  3409. #endif /* not BSD */
  3410. void
  3411. fatal (str, arg)
  3412. char *str, *arg;
  3413. {
  3414. fprintf (stderr, "%s: ", progname);
  3415. fprintf (stderr, str, arg);
  3416. fprintf (stderr, "\n");
  3417. exit (FATAL_EXIT_CODE);
  3418. }
  3419. void
  3420. perror_with_name (name)
  3421. char *name;
  3422. {
  3423. extern int errno, sys_nerr;
  3424. extern char *sys_errlist[];
  3425. fprintf (stderr, "%s: ", progname);
  3426. if (errno < sys_nerr)
  3427. fprintf (stderr, "%s for %s\n", sys_errlist[errno], name);
  3428. else
  3429. fprintf (stderr, "cannot open %s\n", sys_errlist[errno], name);
  3430. }
  3431. void
  3432. pfatal_with_name (name)
  3433. char *name;
  3434. {
  3435. perror_with_name (name);
  3436. exit (FATAL_EXIT_CODE);
  3437. }
  3438. static void
  3439. memory_full ()
  3440. {
  3441. fatal ("Memory exhausted.");
  3442. }
  3443. char *
  3444. xmalloc (size)
  3445. int size;
  3446. {
  3447. extern char *malloc ();
  3448. register char *ptr = malloc (size);
  3449. if (ptr != 0) return (ptr);
  3450. memory_full ();
  3451. /*NOTREACHED*/
  3452. }
  3453. char *
  3454. xrealloc (old, size)
  3455. char *old;
  3456. int size;
  3457. {
  3458. extern char *realloc ();
  3459. register char *ptr = realloc (old, size);
  3460. if (ptr != 0) return (ptr);
  3461. memory_full ();
  3462. /*NOTREACHED*/
  3463. }
  3464. char *
  3465. xcalloc (number, size)
  3466. int number, size;
  3467. {
  3468. extern char *malloc ();
  3469. register int total = number * size;
  3470. register char *ptr = malloc (total);
  3471. if (ptr != 0)
  3472. {
  3473. if (total > 100)
  3474. bzero (ptr, total);
  3475. else {
  3476. /* It's not too long, so loop, zeroing by longs.
  3477. It must be safe because malloc values are always well aligned. */
  3478. register long *zp = (long *) ptr;
  3479. register long *zl = (long *) (ptr + total - 4);
  3480. register int i = total - 4;
  3481. while (zp < zl)
  3482. *zp++ = 0;
  3483. if (i < 0)
  3484. i = 0;
  3485. while (i < total)
  3486. ptr[i++] = 0;
  3487. }
  3488. return ptr;
  3489. }
  3490. memory_full ();
  3491. /*NOTREACHED*/
  3492. }