build.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <ctype.h>
  5. #include <html.h>
  6. #include "impl.h"
  7. // A stack for holding integer values
  8. enum {
  9. Nestmax = 40 // max nesting level of lists, font styles, etc.
  10. };
  11. struct Stack {
  12. int n; // next available slot (top of stack is stack[n-1])
  13. int slots[Nestmax]; // stack entries
  14. };
  15. // Parsing state
  16. struct Pstate
  17. {
  18. Pstate* next; // in stack of Pstates
  19. int skipping; // true when we shouldn't add items
  20. int skipwhite; // true when we should strip leading space
  21. int curfont; // font index for current font
  22. int curfg; // current foreground color
  23. Background curbg; // current background
  24. int curvoff; // current baseline offset
  25. uchar curul; // current underline/strike state
  26. uchar curjust; // current justify state
  27. int curanchor; // current (href) anchor id (if in one), or 0
  28. int curstate; // current value of item state
  29. int literal; // current literal state
  30. int inpar; // true when in a paragraph-like construct
  31. int adjsize; // current font size adjustment
  32. Item* items; // dummy head of item list we're building
  33. Item* lastit; // tail of item list we're building
  34. Item* prelastit; // item before lastit
  35. Stack fntstylestk; // style stack
  36. Stack fntsizestk; // size stack
  37. Stack fgstk; // text color stack
  38. Stack ulstk; // underline stack
  39. Stack voffstk; // vertical offset stack
  40. Stack listtypestk; // list type stack
  41. Stack listcntstk; // list counter stack
  42. Stack juststk; // justification stack
  43. Stack hangstk; // hanging stack
  44. };
  45. struct ItemSource
  46. {
  47. Docinfo* doc;
  48. Pstate* psstk;
  49. int nforms;
  50. int ntables;
  51. int nanchors;
  52. int nframes;
  53. Form* curform;
  54. Map* curmap;
  55. Table* tabstk;
  56. Kidinfo* kidstk;
  57. };
  58. // Some layout parameters
  59. enum {
  60. FRKIDMARGIN = 6, // default margin around kid frames
  61. IMGHSPACE = 0, // default hspace for images (0 matches IE, Netscape)
  62. IMGVSPACE = 0, // default vspace for images
  63. FLTIMGHSPACE = 2, // default hspace for float images
  64. TABSP = 5, // default cellspacing for tables
  65. TABPAD = 1, // default cell padding for tables
  66. LISTTAB = 1, // number of tabs to indent lists
  67. BQTAB = 1, // number of tabs to indent blockquotes
  68. HRSZ = 2, // thickness of horizontal rules
  69. SUBOFF = 4, // vertical offset for subscripts
  70. SUPOFF = 6, // vertical offset for superscripts
  71. NBSP = 160 // non-breaking space character
  72. };
  73. // These tables must be sorted
  74. static StringInt align_tab[] = {
  75. {L"baseline", ALbaseline},
  76. {L"bottom", ALbottom},
  77. {L"center", ALcenter},
  78. {L"char", ALchar},
  79. {L"justify", ALjustify},
  80. {L"left", ALleft},
  81. {L"middle", ALmiddle},
  82. {L"right", ALright},
  83. {L"top", ALtop}
  84. };
  85. #define NALIGNTAB (sizeof(align_tab)/sizeof(StringInt))
  86. static StringInt input_tab[] = {
  87. {L"button", Fbutton},
  88. {L"checkbox", Fcheckbox},
  89. {L"file", Ffile},
  90. {L"hidden", Fhidden},
  91. {L"image", Fimage},
  92. {L"password", Fpassword},
  93. {L"radio", Fradio},
  94. {L"reset", Freset},
  95. {L"submit", Fsubmit},
  96. {L"text", Ftext}
  97. };
  98. #define NINPUTTAB (sizeof(input_tab)/sizeof(StringInt))
  99. static StringInt clear_tab[] = {
  100. {L"all", IFcleft|IFcright},
  101. {L"left", IFcleft},
  102. {L"right", IFcright}
  103. };
  104. #define NCLEARTAB (sizeof(clear_tab)/sizeof(StringInt))
  105. static StringInt fscroll_tab[] = {
  106. {L"auto", FRhscrollauto|FRvscrollauto},
  107. {L"no", FRnoscroll},
  108. {L"yes", FRhscroll|FRvscroll},
  109. };
  110. #define NFSCROLLTAB (sizeof(fscroll_tab)/sizeof(StringInt))
  111. static StringInt shape_tab[] = {
  112. {L"circ", SHcircle},
  113. {L"circle", SHcircle},
  114. {L"poly", SHpoly},
  115. {L"polygon", SHpoly},
  116. {L"rect", SHrect},
  117. {L"rectangle", SHrect}
  118. };
  119. #define NSHAPETAB (sizeof(shape_tab)/sizeof(StringInt))
  120. static StringInt method_tab[] = {
  121. {L"get", HGet},
  122. {L"post", HPost}
  123. };
  124. #define NMETHODTAB (sizeof(method_tab)/sizeof(StringInt))
  125. static Rune* roman[15]= {
  126. L"I", L"II", L"III", L"IV", L"V", L"VI", L"VII", L"VIII", L"IX", L"X",
  127. L"XI", L"XII", L"XIII", L"XIV", L"XV"
  128. };
  129. #define NROMAN 15
  130. // List number types
  131. enum {
  132. LTdisc, LTsquare, LTcircle, LT1, LTa, LTA, LTi, LTI
  133. };
  134. enum {
  135. SPBefore = 2,
  136. SPAfter = 4,
  137. BL = 1,
  138. BLBA = (BL|SPBefore|SPAfter)
  139. };
  140. // blockbrk[tag] is break info for a block level element, or one
  141. // of a few others that get the same treatment re ending open paragraphs
  142. // and requiring a line break / vertical space before them.
  143. // If we want a line of space before the given element, SPBefore is OR'd in.
  144. // If we want a line of space after the given element, SPAfter is OR'd in.
  145. static uchar blockbrk[Numtags]= {
  146. [Taddress] BLBA, [Tblockquote] BLBA, [Tcenter] BL,
  147. [Tdir] BLBA, [Tdiv] BL, [Tdd] BL, [Tdl] BLBA,
  148. [Tdt] BL, [Tform] BLBA,
  149. // headings and tables get breaks added manually
  150. [Th1] BL, [Th2] BL, [Th3] BL,
  151. [Th4] BL, [Th5] BL, [Th6] BL,
  152. [Thr] BL, [Tisindex] BLBA, [Tli] BL, [Tmenu] BLBA,
  153. [Tol] BLBA, [Tp] BLBA, [Tpre] BLBA,
  154. [Tul] BLBA
  155. };
  156. enum {
  157. AGEN = 1
  158. };
  159. // attrinfo is information about attributes.
  160. // The AGEN value means that the attribute is generic (applies to almost all elements)
  161. static uchar attrinfo[Numattrs]= {
  162. [Aid] AGEN, [Aclass] AGEN, [Astyle] AGEN, [Atitle] AGEN,
  163. [Aonblur] AGEN, [Aonchange] AGEN, [Aonclick] AGEN,
  164. [Aondblclick] AGEN, [Aonfocus] AGEN, [Aonkeypress] AGEN,
  165. [Aonkeyup] AGEN, [Aonload] AGEN, [Aonmousedown] AGEN,
  166. [Aonmousemove] AGEN, [Aonmouseout] AGEN, [Aonmouseover] AGEN,
  167. [Aonmouseup] AGEN, [Aonreset] AGEN, [Aonselect] AGEN,
  168. [Aonsubmit] AGEN, [Aonunload] AGEN
  169. };
  170. static uchar scriptev[Numattrs]= {
  171. [Aonblur] SEonblur, [Aonchange] SEonchange, [Aonclick] SEonclick,
  172. [Aondblclick] SEondblclick, [Aonfocus] SEonfocus, [Aonkeypress] SEonkeypress,
  173. [Aonkeyup] SEonkeyup, [Aonload] SEonload, [Aonmousedown] SEonmousedown,
  174. [Aonmousemove] SEonmousemove, [Aonmouseout] SEonmouseout, [Aonmouseover] SEonmouseover,
  175. [Aonmouseup] SEonmouseup, [Aonreset] SEonreset, [Aonselect] SEonselect,
  176. [Aonsubmit] SEonsubmit, [Aonunload] SEonunload
  177. };
  178. // Color lookup table
  179. static StringInt color_tab[] = {
  180. {L"aqua", 0x00FFFF},
  181. {L"black", 0x000000},
  182. {L"blue", 0x0000CC},
  183. {L"fuchsia", 0xFF00FF},
  184. {L"gray", 0x808080},
  185. {L"green", 0x008000},
  186. {L"lime", 0x00FF00},
  187. {L"maroon", 0x800000},
  188. {L"navy", 0x000080,},
  189. {L"olive", 0x808000},
  190. {L"purple", 0x800080},
  191. {L"red", 0xFF0000},
  192. {L"silver", 0xC0C0C0},
  193. {L"teal", 0x008080},
  194. {L"white", 0xFFFFFF},
  195. {L"yellow", 0xFFFF00}
  196. };
  197. #define NCOLORS (sizeof(color_tab)/sizeof(StringInt))
  198. static StringInt *targetmap;
  199. static int targetmapsize;
  200. static int ntargets;
  201. static int buildinited = 0;
  202. #define SMALLBUFSIZE 240
  203. #define BIGBUFSIZE 2000
  204. int dbgbuild = 0;
  205. int warn = 0;
  206. static Align aalign(Token* tok);
  207. static int acolorval(Token* tok, int attid, int dflt);
  208. static void addbrk(Pstate* ps, int sp, int clr);
  209. static void additem(Pstate* ps, Item* it, Token* tok);
  210. static void addlinebrk(Pstate* ps, int clr);
  211. static void addnbsp(Pstate* ps);
  212. static void addtext(Pstate* ps, Rune* s);
  213. static Dimen adimen(Token* tok, int attid);
  214. static int aflagval(Token* tok, int attid);
  215. static int aintval(Token* tok, int attid, int dflt);
  216. static Rune* astrval(Token* tok, int attid, Rune* dflt);
  217. static int atabval(Token* tok, int attid, StringInt* tab, int ntab, int dflt);
  218. static int atargval(Token* tok, int dflt);
  219. static int auintval(Token* tok, int attid, int dflt);
  220. static Rune* aurlval(Token* tok, int attid, Rune* dflt, Rune* base);
  221. static Rune* aval(Token* tok, int attid);
  222. static void buildinit(void);
  223. static Pstate* cell_pstate(Pstate* oldps, int ishead);
  224. static void changehang(Pstate* ps, int delta);
  225. static void changeindent(Pstate* ps, int delta);
  226. static int color(Rune* s, int dflt);
  227. static void copystack(Stack* tostk, Stack* fromstk);
  228. static int dimprint(char* buf, int nbuf, Dimen d);
  229. static Pstate* finishcell(Table* curtab, Pstate* psstk);
  230. static void finish_table(Table* t);
  231. static void freeanchor(Anchor* a);
  232. static void freedestanchor(DestAnchor* da);
  233. static void freeform(Form* f);
  234. static void freeformfield(Formfield* ff);
  235. static void freeitem(Item* it);
  236. static void freepstate(Pstate* p);
  237. static void freepstatestack(Pstate* pshead);
  238. static void freescriptevents(SEvent* ehead);
  239. static void freetable(Table* t);
  240. static Map* getmap(Docinfo* di, Rune* name);
  241. static Rune* getpcdata(Token* toks, int tokslen, int* ptoki);
  242. static Pstate* lastps(Pstate* psl);
  243. static Rune* listmark(uchar ty, int n);
  244. static int listtyval(Token* tok, int dflt);
  245. static Align makealign(int halign, int valign);
  246. static Background makebackground(Rune* imgurl, int color);
  247. static Dimen makedimen(int kind, int spec);
  248. static Anchor* newanchor(int index, Rune* name, Rune* href, int target, Anchor* link);
  249. static Area* newarea(int shape, Rune* href, int target, Area* link);
  250. static DestAnchor* newdestanchor(int index, Rune* name, Item* item, DestAnchor* link);
  251. static Docinfo* newdocinfo(void);
  252. static Genattr* newgenattr(Rune* id, Rune* class, Rune* style, Rune* title, Attr* events);
  253. static Form* newform(int formid, Rune* name, Rune* action,
  254. int target, int method, Form* link);
  255. static Formfield* newformfield(int ftype, int fieldid, Form* form, Rune* name,
  256. Rune* value, int size, int maxlength, Formfield* link);
  257. static Item* newifloat(Item* it, int side);
  258. static Item* newiformfield(Formfield* ff);
  259. static Item* newiimage(Rune* src, Rune* altrep, int align, int width, int height,
  260. int hspace, int vspace, int border, int ismap, Map* map);
  261. static Item* newirule(int align, int size, int noshade, Dimen wspec);
  262. static Item* newispacer(int spkind);
  263. static Item* newitable(Table* t);
  264. static ItemSource* newitemsource(Docinfo* di);
  265. static Item* newitext(Rune* s, int fnt, int fg, int voff, int ul);
  266. static Kidinfo* newkidinfo(int isframeset, Kidinfo* link);
  267. static Option* newoption(int selected, Rune* value, Rune* display, Option* link);
  268. static Pstate* newpstate(Pstate* link);
  269. static SEvent* newscriptevent(int type, Rune* script, SEvent* link);
  270. static Table* newtable(int tableid, Align align, Dimen width, int border,
  271. int cellspacing, int cellpadding, Background bg, Token* tok, Table* link);
  272. static Tablecell* newtablecell(int cellid, int rowspan, int colspan, Align align, Dimen wspec,
  273. int hspec, Background bg, int flags, Tablecell* link);
  274. static Tablerow* newtablerow(Align align, Background bg, int flags, Tablerow* link);
  275. static Dimen parsedim(Rune* s, int ns);
  276. static void pop(Stack* stk);
  277. static void popfontsize(Pstate* ps);
  278. static void popfontstyle(Pstate* ps);
  279. static void popjust(Pstate* ps);
  280. static int popretnewtop(Stack* stk, int dflt);
  281. static int push(Stack* stk, int val);
  282. static void pushfontsize(Pstate* ps, int sz);
  283. static void pushfontstyle(Pstate* ps, int sty);
  284. static void pushjust(Pstate* ps, int j);
  285. static Item* textit(Pstate* ps, Rune* s);
  286. static Rune* removeallwhite(Rune* s);
  287. static void resetdocinfo(Docinfo* d);
  288. static void setcurfont(Pstate* ps);
  289. static void setcurjust(Pstate* ps);
  290. static void setdimarray(Token* tok, int attid, Dimen** pans, int* panslen);
  291. static Rune* stringalign(int a);
  292. static void targetmapinit(void);
  293. static int toint(Rune* s);
  294. static int top(Stack* stk, int dflt);
  295. static void trim_cell(Tablecell* c);
  296. static int validalign(Align a);
  297. static int validdimen(Dimen d);
  298. static int validformfield(Formfield* f);
  299. static int validhalign(int a);
  300. static int validptr(void* p);
  301. static int validStr(Rune* s);
  302. static int validtable(Table* t);
  303. static int validtablerow(Tablerow* r);
  304. static int validtablecol(Tablecol* c);
  305. static int validtablecell(Tablecell* c);
  306. static int validvalign(int a);
  307. static int Iconv(Fmt *f);
  308. static void
  309. buildinit(void)
  310. {
  311. fmtinstall('I', Iconv);
  312. targetmapinit();
  313. buildinited = 1;
  314. }
  315. static ItemSource*
  316. newitemsource(Docinfo* di)
  317. {
  318. ItemSource* is;
  319. Pstate* ps;
  320. ps = newpstate(nil);
  321. if(di->mediatype != TextHtml) {
  322. ps->curstate &= ~IFwrap;
  323. ps->literal = 1;
  324. pushfontstyle(ps, FntT);
  325. }
  326. is = (ItemSource*)emalloc(sizeof(ItemSource));
  327. is->doc = di;
  328. is->psstk = ps;
  329. is->nforms = 0;
  330. is->ntables = 0;
  331. is->nanchors = 0;
  332. is->nframes = 0;
  333. is->curform = nil;
  334. is->curmap = nil;
  335. is->tabstk = nil;
  336. is->kidstk = nil;
  337. return is;
  338. }
  339. static Item *getitems(ItemSource* is, uchar* data, int datalen);
  340. // Parse an html document and create a list of layout items.
  341. // Allocate and return document info in *pdi.
  342. // When caller is done with the items, it should call
  343. // freeitems on the returned result, and then
  344. // freedocinfo(*pdi).
  345. Item*
  346. parsehtml(uchar* data, int datalen, Rune* pagesrc, int mtype, int chset, Docinfo** pdi)
  347. {
  348. Item *it;
  349. Docinfo* di;
  350. ItemSource* is;
  351. di = newdocinfo();
  352. di->src = _Strdup(pagesrc);
  353. di->base = _Strdup(pagesrc);
  354. di->mediatype = mtype;
  355. di->chset = chset;
  356. *pdi = di;
  357. is = newitemsource(di);
  358. it = getitems(is, data, datalen);
  359. freepstatestack(is->psstk);
  360. free(is);
  361. return it;
  362. }
  363. // Get a group of tokens for lexer, parse them, and create
  364. // a list of layout items.
  365. // When caller is done with the items, it should call
  366. // freeitems on the returned result.
  367. static Item*
  368. getitems(ItemSource* is, uchar* data, int datalen)
  369. {
  370. int i;
  371. int j;
  372. int nt;
  373. int pt;
  374. int doscripts;
  375. int tokslen;
  376. int toki;
  377. int h;
  378. int sz;
  379. int method;
  380. int n;
  381. int nblank;
  382. int norsz;
  383. int bramt;
  384. int sty;
  385. int nosh;
  386. int oldcuranchor;
  387. int dfltbd;
  388. int v;
  389. int hang;
  390. int isempty;
  391. int tag;
  392. int brksp;
  393. int target;
  394. uchar brk;
  395. uchar flags;
  396. uchar align;
  397. uchar al;
  398. uchar ty;
  399. uchar ty2;
  400. Pstate* ps;
  401. Pstate* nextps;
  402. Pstate* outerps;
  403. Table* curtab;
  404. Token* tok;
  405. Token* toks;
  406. Docinfo* di;
  407. Item* ans;
  408. Item* img;
  409. Item* ffit;
  410. Item* tabitem;
  411. Rune* s;
  412. Rune* t;
  413. Rune* name;
  414. Rune* enctype;
  415. Rune* usemap;
  416. Rune* prompt;
  417. Rune* equiv;
  418. Rune* val;
  419. Rune* nsz;
  420. Rune* script;
  421. Map* map;
  422. Form* frm;
  423. Iimage* ii;
  424. Kidinfo* kd;
  425. Kidinfo* ks;
  426. Kidinfo* pks;
  427. Dimen wd;
  428. Option* option;
  429. Table* tab;
  430. Tablecell* c;
  431. Tablerow* tr;
  432. Formfield* field;
  433. Formfield* ff;
  434. Rune* href;
  435. Rune* src;
  436. Rune* scriptsrc;
  437. Rune* bgurl;
  438. Rune* action;
  439. Background bg;
  440. if(!buildinited)
  441. buildinit();
  442. doscripts = 0; // for now
  443. ps = is->psstk;
  444. curtab = is->tabstk;
  445. di = is->doc;
  446. toks = _gettoks(data, datalen, di->chset, di->mediatype, &tokslen);
  447. toki = 0;
  448. for(; toki < tokslen; toki++) {
  449. tok = &toks[toki];
  450. if(dbgbuild > 1)
  451. fprint(2, "build: curstate %ux, token %T\n", ps->curstate, tok);
  452. tag = tok->tag;
  453. brk = 0;
  454. brksp = 0;
  455. if(tag < Numtags) {
  456. brk = blockbrk[tag];
  457. if(brk&SPBefore)
  458. brksp = 1;
  459. }
  460. else if(tag < Numtags + RBRA) {
  461. brk = blockbrk[tag - RBRA];
  462. if(brk&SPAfter)
  463. brksp = 1;
  464. }
  465. if(brk) {
  466. addbrk(ps, brksp, 0);
  467. if(ps->inpar) {
  468. popjust(ps);
  469. ps->inpar = 0;
  470. }
  471. }
  472. // check common case first (Data), then switch statement on tag
  473. if(tag == Data) {
  474. // Lexing didn't pay attention to SGML record boundary rules:
  475. // \n after start tag or before end tag to be discarded.
  476. // (Lex has already discarded all \r's).
  477. // Some pages assume this doesn't happen in <PRE> text,
  478. // so we won't do it if literal is true.
  479. // BUG: won't discard \n before a start tag that begins
  480. // the next bufferful of tokens.
  481. s = tok->text;
  482. n = _Strlen(s);
  483. if(!ps->literal) {
  484. i = 0;
  485. j = n;
  486. if(toki > 0) {
  487. pt = toks[toki - 1].tag;
  488. // IE and Netscape both ignore this rule (contrary to spec)
  489. // if previous tag was img
  490. if(pt < Numtags && pt != Timg && j > 0 && s[0] == '\n')
  491. i++;
  492. }
  493. if(toki < tokslen - 1) {
  494. nt = toks[toki + 1].tag;
  495. if(nt >= RBRA && nt < Numtags + RBRA && j > i && s[j - 1] == '\n')
  496. j--;
  497. }
  498. if(i > 0 || j < n) {
  499. t = s;
  500. s = _Strsubstr(s, i, j);
  501. free(t);
  502. n = j-i;
  503. }
  504. }
  505. if(ps->skipwhite) {
  506. _trimwhite(s, n, &t, &nt);
  507. if(t == nil) {
  508. free(s);
  509. s = nil;
  510. }
  511. else if(t != s) {
  512. t = _Strndup(t, nt);
  513. free(s);
  514. s = t;
  515. }
  516. if(s != nil)
  517. ps->skipwhite = 0;
  518. }
  519. tok->text = nil; // token doesn't own string anymore
  520. if(s != nil)
  521. addtext(ps, s);
  522. }
  523. else
  524. switch(tag) {
  525. // Some abbrevs used in following DTD comments
  526. // %text = #PCDATA
  527. // | TT | I | B | U | STRIKE | BIG | SMALL | SUB | SUP
  528. // | EM | STRONG | DFN | CODE | SAMP | KBD | VAR | CITE
  529. // | A | IMG | APPLET | FONT | BASEFONT | BR | SCRIPT | MAP
  530. // | INPUT | SELECT | TEXTAREA
  531. // %block = P | UL | OL | DIR | MENU | DL | PRE | DL | DIV | CENTER
  532. // | BLOCKQUOTE | FORM | ISINDEX | HR | TABLE
  533. // %flow = (%text | %block)*
  534. // %body.content = (%heading | %text | %block | ADDRESS)*
  535. // <!ELEMENT A - - (%text) -(A)>
  536. // Anchors are not supposed to be nested, but you sometimes see
  537. // href anchors inside destination anchors.
  538. case Ta:
  539. if(ps->curanchor != 0) {
  540. if(warn)
  541. fprint(2, "warning: nested <A> or missing </A>\n");
  542. ps->curanchor = 0;
  543. }
  544. name = aval(tok, Aname);
  545. href = aurlval(tok, Ahref, nil, di->base);
  546. // ignore rel, rev, and title attrs
  547. if(href != nil) {
  548. target = atargval(tok, di->target);
  549. di->anchors = newanchor(++is->nanchors, name, href, target, di->anchors);
  550. if(name != nil)
  551. name = _Strdup(name); // for DestAnchor construction, below
  552. ps->curanchor = is->nanchors;
  553. ps->curfg = push(&ps->fgstk, di->link);
  554. ps->curul = push(&ps->ulstk, ULunder);
  555. }
  556. if(name != nil) {
  557. // add a null item to be destination
  558. additem(ps, newispacer(ISPnull), tok);
  559. di->dests = newdestanchor(++is->nanchors, name, ps->lastit, di->dests);
  560. }
  561. break;
  562. case Ta+RBRA :
  563. if(ps->curanchor != 0) {
  564. ps->curfg = popretnewtop(&ps->fgstk, di->text);
  565. ps->curul = popretnewtop(&ps->ulstk, ULnone);
  566. ps->curanchor = 0;
  567. }
  568. break;
  569. // <!ELEMENT APPLET - - (PARAM | %text)* >
  570. // We can't do applets, so ignore PARAMS, and let
  571. // the %text contents appear for the alternative rep
  572. case Tapplet:
  573. case Tapplet+RBRA:
  574. if(warn && tag == Tapplet)
  575. fprint(2, "warning: <APPLET> ignored\n");
  576. break;
  577. // <!ELEMENT AREA - O EMPTY>
  578. case Tarea:
  579. map = di->maps;
  580. if(map == nil) {
  581. if(warn)
  582. fprint(2, "warning: <AREA> not inside <MAP>\n");
  583. continue;
  584. }
  585. map->areas = newarea(atabval(tok, Ashape, shape_tab, NSHAPETAB, SHrect),
  586. aurlval(tok, Ahref, nil, di->base),
  587. atargval(tok, di->target),
  588. map->areas);
  589. setdimarray(tok, Acoords, &map->areas->coords, &map->areas->ncoords);
  590. break;
  591. // <!ELEMENT (B|STRONG) - - (%text)*>
  592. case Tb:
  593. case Tstrong:
  594. pushfontstyle(ps, FntB);
  595. break;
  596. case Tb+RBRA:
  597. case Tcite+RBRA:
  598. case Tcode+RBRA:
  599. case Tdfn+RBRA:
  600. case Tem+RBRA:
  601. case Tkbd+RBRA:
  602. case Ti+RBRA:
  603. case Tsamp+RBRA:
  604. case Tstrong+RBRA:
  605. case Ttt+RBRA:
  606. case Tvar+RBRA :
  607. case Taddress+RBRA:
  608. popfontstyle(ps);
  609. break;
  610. // <!ELEMENT BASE - O EMPTY>
  611. case Tbase:
  612. t = di->base;
  613. di->base = aurlval(tok, Ahref, di->base, di->base);
  614. if(t != nil)
  615. free(t);
  616. di->target = atargval(tok, di->target);
  617. break;
  618. // <!ELEMENT BASEFONT - O EMPTY>
  619. case Tbasefont:
  620. ps->adjsize = aintval(tok, Asize, 3) - 3;
  621. break;
  622. // <!ELEMENT (BIG|SMALL) - - (%text)*>
  623. case Tbig:
  624. case Tsmall:
  625. sz = ps->adjsize;
  626. if(tag == Tbig)
  627. sz += Large;
  628. else
  629. sz += Small;
  630. pushfontsize(ps, sz);
  631. break;
  632. case Tbig+RBRA:
  633. case Tsmall+RBRA:
  634. popfontsize(ps);
  635. break;
  636. // <!ELEMENT BLOCKQUOTE - - %body.content>
  637. case Tblockquote:
  638. changeindent(ps, BQTAB);
  639. break;
  640. case Tblockquote+RBRA:
  641. changeindent(ps, -BQTAB);
  642. break;
  643. // <!ELEMENT BODY O O %body.content>
  644. case Tbody:
  645. ps->skipping = 0;
  646. bg = makebackground(nil, acolorval(tok, Abgcolor, di->background.color));
  647. bgurl = aurlval(tok, Abackground, nil, di->base);
  648. if(bgurl != nil) {
  649. if(di->backgrounditem != nil)
  650. freeitem((Item*)di->backgrounditem);
  651. // really should remove old item from di->images list,
  652. // but there should only be one BODY element ...
  653. di->backgrounditem = (Iimage*)newiimage(bgurl, nil, ALnone, 0, 0, 0, 0, 0, 0, nil);
  654. di->backgrounditem->nextimage = di->images;
  655. di->images = di->backgrounditem;
  656. }
  657. ps->curbg = bg;
  658. di->background = bg;
  659. di->text = acolorval(tok, Atext, di->text);
  660. di->link = acolorval(tok, Alink, di->link);
  661. di->vlink = acolorval(tok, Avlink, di->vlink);
  662. di->alink = acolorval(tok, Aalink, di->alink);
  663. if(di->text != ps->curfg) {
  664. ps->curfg = di->text;
  665. ps->fgstk.n = 0;
  666. }
  667. break;
  668. case Tbody+RBRA:
  669. // HTML spec says ignore things after </body>,
  670. // but IE and Netscape don't
  671. // ps.skipping = 1;
  672. break;
  673. // <!ELEMENT BR - O EMPTY>
  674. case Tbr:
  675. addlinebrk(ps, atabval(tok, Aclear, clear_tab, NCLEARTAB, 0));
  676. break;
  677. // <!ELEMENT CAPTION - - (%text;)*>
  678. case Tcaption:
  679. if(curtab == nil) {
  680. if(warn)
  681. fprint(2, "warning: <CAPTION> outside <TABLE>\n");
  682. continue;
  683. }
  684. if(curtab->caption != nil) {
  685. if(warn)
  686. fprint(2, "warning: more than one <CAPTION> in <TABLE>\n");
  687. continue;
  688. }
  689. ps = newpstate(ps);
  690. curtab->caption_place = atabval(tok, Aalign, align_tab, NALIGNTAB, ALtop);
  691. break;
  692. case Tcaption+RBRA:
  693. nextps = ps->next;
  694. if(curtab == nil || nextps == nil) {
  695. if(warn)
  696. fprint(2, "warning: unexpected </CAPTION>\n");
  697. continue;
  698. }
  699. curtab->caption = ps->items->next;
  700. free(ps);
  701. ps = nextps;
  702. break;
  703. case Tcenter:
  704. case Tdiv:
  705. if(tag == Tcenter)
  706. al = ALcenter;
  707. else
  708. al = atabval(tok, Aalign, align_tab, NALIGNTAB, ps->curjust);
  709. pushjust(ps, al);
  710. break;
  711. case Tcenter+RBRA:
  712. case Tdiv+RBRA:
  713. popjust(ps);
  714. break;
  715. // <!ELEMENT DD - O %flow >
  716. case Tdd:
  717. if(ps->hangstk.n == 0) {
  718. if(warn)
  719. fprint(2, "warning: <DD> not inside <DL\n");
  720. continue;
  721. }
  722. h = top(&ps->hangstk, 0);
  723. if(h != 0)
  724. changehang(ps, -10*LISTTAB);
  725. else
  726. addbrk(ps, 0, 0);
  727. push(&ps->hangstk, 0);
  728. break;
  729. //<!ELEMENT (DIR|MENU) - - (LI)+ -(%block) >
  730. //<!ELEMENT (OL|UL) - - (LI)+>
  731. case Tdir:
  732. case Tmenu:
  733. case Tol:
  734. case Tul:
  735. changeindent(ps, LISTTAB);
  736. push(&ps->listtypestk, listtyval(tok, (tag==Tol)? LT1 : LTdisc));
  737. push(&ps->listcntstk, aintval(tok, Astart, 1));
  738. break;
  739. case Tdir+RBRA:
  740. case Tmenu+RBRA:
  741. case Tol+RBRA:
  742. case Tul+RBRA:
  743. if(ps->listtypestk.n == 0) {
  744. if(warn)
  745. fprint(2, "warning: %T ended no list\n", tok);
  746. continue;
  747. }
  748. addbrk(ps, 0, 0);
  749. pop(&ps->listtypestk);
  750. pop(&ps->listcntstk);
  751. changeindent(ps, -LISTTAB);
  752. break;
  753. // <!ELEMENT DL - - (DT|DD)+ >
  754. case Tdl:
  755. changeindent(ps, LISTTAB);
  756. push(&ps->hangstk, 0);
  757. break;
  758. case Tdl+RBRA:
  759. if(ps->hangstk.n == 0) {
  760. if(warn)
  761. fprint(2, "warning: unexpected </DL>\n");
  762. continue;
  763. }
  764. changeindent(ps, -LISTTAB);
  765. if(top(&ps->hangstk, 0) != 0)
  766. changehang(ps, -10*LISTTAB);
  767. pop(&ps->hangstk);
  768. break;
  769. // <!ELEMENT DT - O (%text)* >
  770. case Tdt:
  771. if(ps->hangstk.n == 0) {
  772. if(warn)
  773. fprint(2, "warning: <DT> not inside <DL>\n");
  774. continue;
  775. }
  776. h = top(&ps->hangstk, 0);
  777. pop(&ps->hangstk);
  778. if(h != 0)
  779. changehang(ps, -10*LISTTAB);
  780. changehang(ps, 10*LISTTAB);
  781. push(&ps->hangstk, 1);
  782. break;
  783. // <!ELEMENT FONT - - (%text)*>
  784. case Tfont:
  785. sz = top(&ps->fntsizestk, Normal);
  786. if(_tokaval(tok, Asize, &nsz, 0)) {
  787. if(_prefix(L"+", nsz))
  788. sz = Normal + _Strtol(nsz+1, nil, 10) + ps->adjsize;
  789. else if(_prefix(L"-", nsz))
  790. sz = Normal - _Strtol(nsz+1, nil, 10) + ps->adjsize;
  791. else if(nsz != nil)
  792. sz = Normal + (_Strtol(nsz, nil, 10) - 3);
  793. }
  794. ps->curfg = push(&ps->fgstk, acolorval(tok, Acolor, ps->curfg));
  795. pushfontsize(ps, sz);
  796. break;
  797. case Tfont+RBRA:
  798. if(ps->fgstk.n == 0) {
  799. if(warn)
  800. fprint(2, "warning: unexpected </FONT>\n");
  801. continue;
  802. }
  803. ps->curfg = popretnewtop(&ps->fgstk, di->text);
  804. popfontsize(ps);
  805. break;
  806. // <!ELEMENT FORM - - %body.content -(FORM) >
  807. case Tform:
  808. if(is->curform != nil) {
  809. if(warn)
  810. fprint(2, "warning: <FORM> nested inside another\n");
  811. continue;
  812. }
  813. action = aurlval(tok, Aaction, di->base, di->base);
  814. s = aval(tok, Aid);
  815. name = astrval(tok, Aname, s);
  816. if(s)
  817. free(s);
  818. target = atargval(tok, di->target);
  819. method = atabval(tok, Amethod, method_tab, NMETHODTAB, HGet);
  820. if(warn && _tokaval(tok, Aenctype, &enctype, 0) &&
  821. _Strcmp(enctype, L"application/x-www-form-urlencoded"))
  822. fprint(2, "form enctype %S not handled\n", enctype);
  823. frm = newform(++is->nforms, name, action, target, method, di->forms);
  824. di->forms = frm;
  825. is->curform = frm;
  826. break;
  827. case Tform+RBRA:
  828. if(is->curform == nil) {
  829. if(warn)
  830. fprint(2, "warning: unexpected </FORM>\n");
  831. continue;
  832. }
  833. // put fields back in input order
  834. is->curform->fields = (Formfield*)_revlist((List*)is->curform->fields);
  835. is->curform = nil;
  836. break;
  837. // <!ELEMENT FRAME - O EMPTY>
  838. case Tframe:
  839. ks = is->kidstk;
  840. if(ks == nil) {
  841. if(warn)
  842. fprint(2, "warning: <FRAME> not in <FRAMESET>\n");
  843. continue;
  844. }
  845. ks->kidinfos = kd = newkidinfo(0, ks->kidinfos);
  846. kd->src = aurlval(tok, Asrc, nil, di->base);
  847. kd->name = aval(tok, Aname);
  848. if(kd->name == nil) {
  849. s = _ltoStr(++is->nframes);
  850. kd->name = _Strdup2(L"_fr", s);
  851. free(s);
  852. }
  853. kd->marginw = auintval(tok, Amarginwidth, 0);
  854. kd->marginh = auintval(tok, Amarginheight, 0);
  855. kd->framebd = auintval(tok, Aframeborder, 1);
  856. kd->flags = atabval(tok, Ascrolling, fscroll_tab, NFSCROLLTAB, kd->flags);
  857. norsz = aflagval(tok, Anoresize);
  858. if(norsz)
  859. kd->flags |= FRnoresize;
  860. break;
  861. // <!ELEMENT FRAMESET - - (FRAME|FRAMESET)+>
  862. case Tframeset:
  863. ks = newkidinfo(1, nil);
  864. pks = is->kidstk;
  865. if(pks == nil)
  866. di->kidinfo = ks;
  867. else {
  868. ks->next = pks->kidinfos;
  869. pks->kidinfos = ks;
  870. }
  871. ks->nextframeset = pks;
  872. is->kidstk = ks;
  873. setdimarray(tok, Arows, &ks->rows, &ks->nrows);
  874. if(ks->nrows == 0) {
  875. ks->rows = (Dimen*)emalloc(sizeof(Dimen));
  876. ks->nrows = 1;
  877. ks->rows[0] = makedimen(Dpercent, 100);
  878. }
  879. setdimarray(tok, Acols, &ks->cols, &ks->ncols);
  880. if(ks->ncols == 0) {
  881. ks->cols = (Dimen*)emalloc(sizeof(Dimen));
  882. ks->ncols = 1;
  883. ks->cols[0] = makedimen(Dpercent, 100);
  884. }
  885. break;
  886. case Tframeset+RBRA:
  887. if(is->kidstk == nil) {
  888. if(warn)
  889. fprint(2, "warning: unexpected </FRAMESET>\n");
  890. continue;
  891. }
  892. ks = is->kidstk;
  893. // put kids back in original order
  894. // and add blank frames to fill out cells
  895. n = ks->nrows*ks->ncols;
  896. nblank = n - _listlen((List*)ks->kidinfos);
  897. while(nblank-- > 0)
  898. ks->kidinfos = newkidinfo(0, ks->kidinfos);
  899. ks->kidinfos = (Kidinfo*)_revlist((List*)ks->kidinfos);
  900. is->kidstk = is->kidstk->nextframeset;
  901. if(is->kidstk == nil) {
  902. // end input
  903. ans = nil;
  904. goto return_ans;
  905. }
  906. break;
  907. // <!ELEMENT H1 - - (%text;)*>, etc.
  908. case Th1:
  909. case Th2:
  910. case Th3:
  911. case Th4:
  912. case Th5:
  913. case Th6:
  914. bramt = 1;
  915. if(ps->items == ps->lastit)
  916. bramt = 0;
  917. addbrk(ps, bramt, IFcleft|IFcright);
  918. sz = Verylarge - (tag - Th1);
  919. if(sz < Tiny)
  920. sz = Tiny;
  921. pushfontsize(ps, sz);
  922. sty = top(&ps->fntstylestk, FntR);
  923. if(tag == Th1)
  924. sty = FntB;
  925. pushfontstyle(ps, sty);
  926. pushjust(ps, atabval(tok, Aalign, align_tab, NALIGNTAB, ps->curjust));
  927. ps->skipwhite = 1;
  928. break;
  929. case Th1+RBRA:
  930. case Th2+RBRA:
  931. case Th3+RBRA:
  932. case Th4+RBRA:
  933. case Th5+RBRA:
  934. case Th6+RBRA:
  935. addbrk(ps, 1, IFcleft|IFcright);
  936. popfontsize(ps);
  937. popfontstyle(ps);
  938. popjust(ps);
  939. break;
  940. case Thead:
  941. // HTML spec says ignore regular markup in head,
  942. // but Netscape and IE don't
  943. // ps.skipping = 1;
  944. break;
  945. case Thead+RBRA:
  946. ps->skipping = 0;
  947. break;
  948. // <!ELEMENT HR - O EMPTY>
  949. case Thr:
  950. al = atabval(tok, Aalign, align_tab, NALIGNTAB, ALcenter);
  951. sz = auintval(tok, Asize, HRSZ);
  952. wd = adimen(tok, Awidth);
  953. if(dimenkind(wd) == Dnone)
  954. wd = makedimen(Dpercent, 100);
  955. nosh = aflagval(tok, Anoshade);
  956. additem(ps, newirule(al, sz, nosh, wd), tok);
  957. addbrk(ps, 0, 0);
  958. break;
  959. case Ti:
  960. case Tcite:
  961. case Tdfn:
  962. case Tem:
  963. case Tvar:
  964. case Taddress:
  965. pushfontstyle(ps, FntI);
  966. break;
  967. // <!ELEMENT IMG - O EMPTY>
  968. case Timg:
  969. map = nil;
  970. oldcuranchor = ps->curanchor;
  971. if(_tokaval(tok, Ausemap, &usemap, 0)) {
  972. if(!_prefix(L"#", usemap)) {
  973. if(warn)
  974. fprint(2, "warning: can't handle non-local map %S\n", usemap);
  975. }
  976. else {
  977. map = getmap(di, usemap+1);
  978. if(ps->curanchor == 0) {
  979. di->anchors = newanchor(++is->nanchors, nil, nil, di->target, di->anchors);
  980. ps->curanchor = is->nanchors;
  981. }
  982. }
  983. }
  984. align = atabval(tok, Aalign, align_tab, NALIGNTAB, ALbottom);
  985. dfltbd = 0;
  986. if(ps->curanchor != 0)
  987. dfltbd = 2;
  988. src = aurlval(tok, Asrc, nil, di->base);
  989. if(src == nil) {
  990. if(warn)
  991. fprint(2, "warning: <img> has no src attribute\n");
  992. ps->curanchor = oldcuranchor;
  993. continue;
  994. }
  995. img = newiimage(src,
  996. aval(tok, Aalt),
  997. align,
  998. auintval(tok, Awidth, 0),
  999. auintval(tok, Aheight, 0),
  1000. auintval(tok, Ahspace, IMGHSPACE),
  1001. auintval(tok, Avspace, IMGVSPACE),
  1002. auintval(tok, Aborder, dfltbd),
  1003. aflagval(tok, Aismap),
  1004. map);
  1005. if(align == ALleft || align == ALright) {
  1006. additem(ps, newifloat(img, align), tok);
  1007. // if no hspace specified, use FLTIMGHSPACE
  1008. if(!_tokaval(tok, Ahspace, &val, 0))
  1009. ((Iimage*)img)->hspace = FLTIMGHSPACE;
  1010. }
  1011. else {
  1012. ps->skipwhite = 0;
  1013. additem(ps, img, tok);
  1014. }
  1015. if(!ps->skipping) {
  1016. ((Iimage*)img)->nextimage = di->images;
  1017. di->images = (Iimage*)img;
  1018. }
  1019. ps->curanchor = oldcuranchor;
  1020. break;
  1021. // <!ELEMENT INPUT - O EMPTY>
  1022. case Tinput:
  1023. ps->skipwhite = 0;
  1024. if(is->curform == nil) {
  1025. if(warn)
  1026. fprint(2, "<INPUT> not inside <FORM>\n");
  1027. continue;
  1028. }
  1029. is->curform->fields = field = newformfield(
  1030. atabval(tok, Atype, input_tab, NINPUTTAB, Ftext),
  1031. ++is->curform->nfields,
  1032. is->curform,
  1033. aval(tok, Aname),
  1034. aval(tok, Avalue),
  1035. auintval(tok, Asize, 0),
  1036. auintval(tok, Amaxlength, 1000),
  1037. is->curform->fields);
  1038. if(aflagval(tok, Achecked))
  1039. field->flags = FFchecked;
  1040. switch(field->ftype) {
  1041. case Ftext:
  1042. case Fpassword:
  1043. case Ffile:
  1044. if(field->size == 0)
  1045. field->size = 20;
  1046. break;
  1047. case Fcheckbox:
  1048. if(field->name == nil) {
  1049. if(warn)
  1050. fprint(2, "warning: checkbox form field missing name\n");
  1051. continue;
  1052. }
  1053. if(field->value == nil)
  1054. field->value = _Strdup(L"1");
  1055. break;
  1056. case Fradio:
  1057. if(field->name == nil || field->value == nil) {
  1058. if(warn)
  1059. fprint(2, "warning: radio form field missing name or value\n");
  1060. continue;
  1061. }
  1062. break;
  1063. case Fsubmit:
  1064. if(field->value == nil)
  1065. field->value = _Strdup(L"Submit");
  1066. if(field->name == nil)
  1067. field->name = _Strdup(L"_no_name_submit_");
  1068. break;
  1069. case Fimage:
  1070. src = aurlval(tok, Asrc, nil, di->base);
  1071. if(src == nil) {
  1072. if(warn)
  1073. fprint(2, "warning: image form field missing src\n");
  1074. continue;
  1075. }
  1076. // width and height attrs aren't specified in HTML 3.2,
  1077. // but some people provide them and they help avoid
  1078. // a relayout
  1079. field->image = newiimage(src,
  1080. astrval(tok, Aalt, L"Submit"),
  1081. atabval(tok, Aalign, align_tab, NALIGNTAB, ALbottom),
  1082. auintval(tok, Awidth, 0), auintval(tok, Aheight, 0),
  1083. 0, 0, 0, 0, nil);
  1084. ii = (Iimage*)field->image;
  1085. ii->nextimage = di->images;
  1086. di->images = ii;
  1087. break;
  1088. case Freset:
  1089. if(field->value == nil)
  1090. field->value = _Strdup(L"Reset");
  1091. break;
  1092. case Fbutton:
  1093. if(field->value == nil)
  1094. field->value = _Strdup(L" ");
  1095. break;
  1096. }
  1097. ffit = newiformfield(field);
  1098. additem(ps, ffit, tok);
  1099. if(ffit->genattr != nil)
  1100. field->events = ffit->genattr->events;
  1101. break;
  1102. // <!ENTITY ISINDEX - O EMPTY>
  1103. case Tisindex:
  1104. ps->skipwhite = 0;
  1105. prompt = astrval(tok, Aprompt, L"Index search terms:");
  1106. target = atargval(tok, di->target);
  1107. additem(ps, textit(ps, prompt), tok);
  1108. frm = newform(++is->nforms,
  1109. nil,
  1110. di->base,
  1111. target,
  1112. HGet,
  1113. di->forms);
  1114. di->forms = frm;
  1115. ff = newformfield(Ftext,
  1116. 1,
  1117. frm,
  1118. _Strdup(L"_ISINDEX_"),
  1119. nil,
  1120. 50,
  1121. 1000,
  1122. nil);
  1123. frm->fields = ff;
  1124. frm->nfields = 1;
  1125. additem(ps, newiformfield(ff), tok);
  1126. addbrk(ps, 1, 0);
  1127. break;
  1128. // <!ELEMENT LI - O %flow>
  1129. case Tli:
  1130. if(ps->listtypestk.n == 0) {
  1131. if(warn)
  1132. fprint(2, "<LI> not in list\n");
  1133. continue;
  1134. }
  1135. ty = top(&ps->listtypestk, 0);
  1136. ty2 = listtyval(tok, ty);
  1137. if(ty != ty2) {
  1138. ty = ty2;
  1139. push(&ps->listtypestk, ty2);
  1140. }
  1141. v = aintval(tok, Avalue, top(&ps->listcntstk, 1));
  1142. if(ty == LTdisc || ty == LTsquare || ty == LTcircle)
  1143. hang = 10*LISTTAB - 3;
  1144. else
  1145. hang = 10*LISTTAB - 1;
  1146. changehang(ps, hang);
  1147. addtext(ps, listmark(ty, v));
  1148. push(&ps->listcntstk, v + 1);
  1149. changehang(ps, -hang);
  1150. ps->skipwhite = 1;
  1151. break;
  1152. // <!ELEMENT MAP - - (AREA)+>
  1153. case Tmap:
  1154. if(_tokaval(tok, Aname, &name, 0))
  1155. is->curmap = getmap(di, name);
  1156. break;
  1157. case Tmap+RBRA:
  1158. map = is->curmap;
  1159. if(map == nil) {
  1160. if(warn)
  1161. fprint(2, "warning: unexpected </MAP>\n");
  1162. continue;
  1163. }
  1164. map->areas = (Area*)_revlist((List*)map->areas);
  1165. break;
  1166. case Tmeta:
  1167. if(ps->skipping)
  1168. continue;
  1169. if(_tokaval(tok, Ahttp_equiv, &equiv, 0)) {
  1170. val = aval(tok, Acontent);
  1171. n = _Strlen(equiv);
  1172. if(!_Strncmpci(equiv, n, L"refresh"))
  1173. di->refresh = val;
  1174. else if(!_Strncmpci(equiv, n, L"content-script-type")) {
  1175. n = _Strlen(val);
  1176. if(!_Strncmpci(val, n, L"javascript")
  1177. || !_Strncmpci(val, n, L"jscript1.1")
  1178. || !_Strncmpci(val, n, L"jscript"))
  1179. di->scripttype = TextJavascript;
  1180. else {
  1181. if(warn)
  1182. fprint(2, "unimplemented script type %S\n", val);
  1183. di->scripttype = UnknownType;
  1184. }
  1185. }
  1186. }
  1187. break;
  1188. // Nobr is NOT in HMTL 4.0, but it is ubiquitous on the web
  1189. case Tnobr:
  1190. ps->skipwhite = 0;
  1191. ps->curstate &= ~IFwrap;
  1192. break;
  1193. case Tnobr+RBRA:
  1194. ps->curstate |= IFwrap;
  1195. break;
  1196. // We do frames, so skip stuff in noframes
  1197. case Tnoframes:
  1198. ps->skipping = 1;
  1199. break;
  1200. case Tnoframes+RBRA:
  1201. ps->skipping = 0;
  1202. break;
  1203. // We do scripts (if enabled), so skip stuff in noscripts
  1204. case Tnoscript:
  1205. if(doscripts)
  1206. ps->skipping = 1;
  1207. break;
  1208. case Tnoscript+RBRA:
  1209. if(doscripts)
  1210. ps->skipping = 0;
  1211. break;
  1212. // <!ELEMENT OPTION - O ( //PCDATA)>
  1213. case Toption:
  1214. if(is->curform == nil || is->curform->fields == nil) {
  1215. if(warn)
  1216. fprint(2, "warning: <OPTION> not in <SELECT>\n");
  1217. continue;
  1218. }
  1219. field = is->curform->fields;
  1220. if(field->ftype != Fselect) {
  1221. if(warn)
  1222. fprint(2, "warning: <OPTION> not in <SELECT>\n");
  1223. continue;
  1224. }
  1225. val = aval(tok, Avalue);
  1226. option = newoption(aflagval(tok, Aselected), val, nil, field->options);
  1227. field->options = option;
  1228. option->display = getpcdata(toks, tokslen, &toki);
  1229. if(val == nil)
  1230. option->value = _Strdup(option->display);
  1231. break;
  1232. // <!ELEMENT P - O (%text)* >
  1233. case Tp:
  1234. pushjust(ps, atabval(tok, Aalign, align_tab, NALIGNTAB, ps->curjust));
  1235. ps->inpar = 1;
  1236. ps->skipwhite = 1;
  1237. break;
  1238. case Tp+RBRA:
  1239. break;
  1240. // <!ELEMENT PARAM - O EMPTY>
  1241. // Do something when we do applets...
  1242. case Tparam:
  1243. break;
  1244. // <!ELEMENT PRE - - (%text)* -(IMG|BIG|SMALL|SUB|SUP|FONT) >
  1245. case Tpre:
  1246. ps->curstate &= ~IFwrap;
  1247. ps->literal = 1;
  1248. ps->skipwhite = 0;
  1249. pushfontstyle(ps, FntT);
  1250. break;
  1251. case Tpre+RBRA:
  1252. ps->curstate |= IFwrap;
  1253. if(ps->literal) {
  1254. popfontstyle(ps);
  1255. ps->literal = 0;
  1256. }
  1257. break;
  1258. // <!ELEMENT SCRIPT - - CDATA>
  1259. case Tscript:
  1260. if(doscripts) {
  1261. if(!di->hasscripts) {
  1262. if(di->scripttype == TextJavascript) {
  1263. // TODO: initialize script if nec.
  1264. // initjscript(di);
  1265. di->hasscripts = 1;
  1266. }
  1267. }
  1268. }
  1269. if(!di->hasscripts) {
  1270. if(warn)
  1271. fprint(2, "warning: <SCRIPT> ignored\n");
  1272. ps->skipping = 1;
  1273. }
  1274. else {
  1275. scriptsrc = aurlval(tok, Asrc, nil, di->base);
  1276. script = nil;
  1277. if(scriptsrc != nil) {
  1278. if(warn)
  1279. fprint(2, "warning: non-local <SCRIPT> ignored\n");
  1280. free(scriptsrc);
  1281. }
  1282. else {
  1283. script = getpcdata(toks, tokslen, &toki);
  1284. }
  1285. if(script != nil) {
  1286. if(warn)
  1287. fprint(2, "script ignored\n");
  1288. free(script);
  1289. }
  1290. }
  1291. break;
  1292. case Tscript+RBRA:
  1293. ps->skipping = 0;
  1294. break;
  1295. // <!ELEMENT SELECT - - (OPTION+)>
  1296. case Tselect:
  1297. if(is->curform == nil) {
  1298. if(warn)
  1299. fprint(2, "<SELECT> not inside <FORM>\n");
  1300. continue;
  1301. }
  1302. field = newformfield(Fselect,
  1303. ++is->curform->nfields,
  1304. is->curform,
  1305. aval(tok, Aname),
  1306. nil,
  1307. auintval(tok, Asize, 0),
  1308. 0,
  1309. is->curform->fields);
  1310. is->curform->fields = field;
  1311. if(aflagval(tok, Amultiple))
  1312. field->flags = FFmultiple;
  1313. ffit = newiformfield(field);
  1314. additem(ps, ffit, tok);
  1315. if(ffit->genattr != nil)
  1316. field->events = ffit->genattr->events;
  1317. // throw away stuff until next tag (should be <OPTION>)
  1318. s = getpcdata(toks, tokslen, &toki);
  1319. if(s != nil)
  1320. free(s);
  1321. break;
  1322. case Tselect+RBRA:
  1323. if(is->curform == nil || is->curform->fields == nil) {
  1324. if(warn)
  1325. fprint(2, "warning: unexpected </SELECT>\n");
  1326. continue;
  1327. }
  1328. field = is->curform->fields;
  1329. if(field->ftype != Fselect)
  1330. continue;
  1331. // put options back in input order
  1332. field->options = (Option*)_revlist((List*)field->options);
  1333. break;
  1334. // <!ELEMENT (STRIKE|U) - - (%text)*>
  1335. case Tstrike:
  1336. case Tu:
  1337. ps->curul = push(&ps->ulstk, (tag==Tstrike)? ULmid : ULunder);
  1338. break;
  1339. case Tstrike+RBRA:
  1340. case Tu+RBRA:
  1341. if(ps->ulstk.n == 0) {
  1342. if(warn)
  1343. fprint(2, "warning: unexpected %T\n", tok);
  1344. continue;
  1345. }
  1346. ps->curul = popretnewtop(&ps->ulstk, ULnone);
  1347. break;
  1348. // <!ELEMENT STYLE - - CDATA>
  1349. case Tstyle:
  1350. if(warn)
  1351. fprint(2, "warning: unimplemented <STYLE>\n");
  1352. ps->skipping = 1;
  1353. break;
  1354. case Tstyle+RBRA:
  1355. ps->skipping = 0;
  1356. break;
  1357. // <!ELEMENT (SUB|SUP) - - (%text)*>
  1358. case Tsub:
  1359. case Tsup:
  1360. if(tag == Tsub)
  1361. ps->curvoff += SUBOFF;
  1362. else
  1363. ps->curvoff -= SUPOFF;
  1364. push(&ps->voffstk, ps->curvoff);
  1365. sz = top(&ps->fntsizestk, Normal);
  1366. pushfontsize(ps, sz - 1);
  1367. break;
  1368. case Tsub+RBRA:
  1369. case Tsup+RBRA:
  1370. if(ps->voffstk.n == 0) {
  1371. if(warn)
  1372. fprint(2, "warning: unexpected %T\n", tok);
  1373. continue;
  1374. }
  1375. ps->curvoff = popretnewtop(&ps->voffstk, 0);
  1376. popfontsize(ps);
  1377. break;
  1378. // <!ELEMENT TABLE - - (CAPTION?, TR+)>
  1379. case Ttable:
  1380. ps->skipwhite = 0;
  1381. tab = newtable(++is->ntables,
  1382. aalign(tok),
  1383. adimen(tok, Awidth),
  1384. aflagval(tok, Aborder),
  1385. auintval(tok, Acellspacing, TABSP),
  1386. auintval(tok, Acellpadding, TABPAD),
  1387. makebackground(nil, acolorval(tok, Abgcolor, ps->curbg.color)),
  1388. tok,
  1389. is->tabstk);
  1390. is->tabstk = tab;
  1391. curtab = tab;
  1392. break;
  1393. case Ttable+RBRA:
  1394. if(curtab == nil) {
  1395. if(warn)
  1396. fprint(2, "warning: unexpected </TABLE>\n");
  1397. continue;
  1398. }
  1399. isempty = (curtab->cells == nil);
  1400. if(isempty) {
  1401. if(warn)
  1402. fprint(2, "warning: <TABLE> has no cells\n");
  1403. }
  1404. else {
  1405. ps = finishcell(curtab, ps);
  1406. if(curtab->rows != nil)
  1407. curtab->rows->flags = 0;
  1408. finish_table(curtab);
  1409. }
  1410. ps->skipping = 0;
  1411. if(!isempty) {
  1412. tabitem = newitable(curtab);
  1413. al = curtab->align.halign;
  1414. switch(al) {
  1415. case ALleft:
  1416. case ALright:
  1417. additem(ps, newifloat(tabitem, al), tok);
  1418. break;
  1419. default:
  1420. if(al == ALcenter)
  1421. pushjust(ps, ALcenter);
  1422. addbrk(ps, 0, 0);
  1423. if(ps->inpar) {
  1424. popjust(ps);
  1425. ps->inpar = 0;
  1426. }
  1427. additem(ps, tabitem, curtab->tabletok);
  1428. if(al == ALcenter)
  1429. popjust(ps);
  1430. break;
  1431. }
  1432. }
  1433. if(is->tabstk == nil) {
  1434. if(warn)
  1435. fprint(2, "warning: table stack is wrong\n");
  1436. }
  1437. else
  1438. is->tabstk = is->tabstk->next;
  1439. curtab->next = di->tables;
  1440. di->tables = curtab;
  1441. curtab = is->tabstk;
  1442. if(!isempty)
  1443. addbrk(ps, 0, 0);
  1444. break;
  1445. // <!ELEMENT (TH|TD) - O %body.content>
  1446. // Cells for a row are accumulated in reverse order.
  1447. // We push ps on a stack, and use a new one to accumulate
  1448. // the contents of the cell.
  1449. case Ttd:
  1450. case Tth:
  1451. if(curtab == nil) {
  1452. if(warn)
  1453. fprint(2, "%T outside <TABLE>\n", tok);
  1454. continue;
  1455. }
  1456. if(ps->inpar) {
  1457. popjust(ps);
  1458. ps->inpar = 0;
  1459. }
  1460. ps = finishcell(curtab, ps);
  1461. tr = nil;
  1462. if(curtab->rows != nil)
  1463. tr = curtab->rows;
  1464. if(tr == nil || !tr->flags) {
  1465. if(warn)
  1466. fprint(2, "%T outside row\n", tok);
  1467. tr = newtablerow(makealign(ALnone, ALnone),
  1468. makebackground(nil, curtab->background.color),
  1469. TFparsing,
  1470. curtab->rows);
  1471. curtab->rows = tr;
  1472. }
  1473. ps = cell_pstate(ps, tag == Tth);
  1474. flags = TFparsing;
  1475. if(aflagval(tok, Anowrap)) {
  1476. flags |= TFnowrap;
  1477. ps->curstate &= ~IFwrap;
  1478. }
  1479. if(tag == Tth)
  1480. flags |= TFisth;
  1481. c = newtablecell(curtab->cells==nil? 1 : curtab->cells->cellid+1,
  1482. auintval(tok, Arowspan, 1),
  1483. auintval(tok, Acolspan, 1),
  1484. aalign(tok),
  1485. adimen(tok, Awidth),
  1486. auintval(tok, Aheight, 0),
  1487. makebackground(nil, acolorval(tok, Abgcolor, tr->background.color)),
  1488. flags,
  1489. curtab->cells);
  1490. curtab->cells = c;
  1491. ps->curbg = c->background;
  1492. if(c->align.halign == ALnone) {
  1493. if(tr->align.halign != ALnone)
  1494. c->align.halign = tr->align.halign;
  1495. else if(tag == Tth)
  1496. c->align.halign = ALcenter;
  1497. else
  1498. c->align.halign = ALleft;
  1499. }
  1500. if(c->align.valign == ALnone) {
  1501. if(tr->align.valign != ALnone)
  1502. c->align.valign = tr->align.valign;
  1503. else
  1504. c->align.valign = ALmiddle;
  1505. }
  1506. c->nextinrow = tr->cells;
  1507. tr->cells = c;
  1508. break;
  1509. case Ttd+RBRA:
  1510. case Tth+RBRA:
  1511. if(curtab == nil || curtab->cells == nil) {
  1512. if(warn)
  1513. fprint(2, "unexpected %T\n", tok);
  1514. continue;
  1515. }
  1516. ps = finishcell(curtab, ps);
  1517. break;
  1518. // <!ELEMENT TEXTAREA - - ( //PCDATA)>
  1519. case Ttextarea:
  1520. if(is->curform == nil) {
  1521. if(warn)
  1522. fprint(2, "<TEXTAREA> not inside <FORM>\n");
  1523. continue;
  1524. }
  1525. field = newformfield(Ftextarea,
  1526. ++is->curform->nfields,
  1527. is->curform,
  1528. aval(tok, Aname),
  1529. nil,
  1530. 0,
  1531. 0,
  1532. is->curform->fields);
  1533. is->curform->fields = field;
  1534. field->rows = auintval(tok, Arows, 3);
  1535. field->cols = auintval(tok, Acols, 50);
  1536. field->value = getpcdata(toks, tokslen, &toki);
  1537. if(warn && toki < tokslen - 1 && toks[toki + 1].tag != Ttextarea + RBRA)
  1538. fprint(2, "warning: <TEXTAREA> data ended by %T\n", &toks[toki + 1]);
  1539. ffit = newiformfield(field);
  1540. additem(ps, ffit, tok);
  1541. if(ffit->genattr != nil)
  1542. field->events = ffit->genattr->events;
  1543. break;
  1544. // <!ELEMENT TITLE - - ( //PCDATA)* -(%head.misc)>
  1545. case Ttitle:
  1546. di->doctitle = getpcdata(toks, tokslen, &toki);
  1547. if(warn && toki < tokslen - 1 && toks[toki + 1].tag != Ttitle + RBRA)
  1548. fprint(2, "warning: <TITLE> data ended by %T\n", &toks[toki + 1]);
  1549. break;
  1550. // <!ELEMENT TR - O (TH|TD)+>
  1551. // rows are accumulated in reverse order in curtab->rows
  1552. case Ttr:
  1553. if(curtab == nil) {
  1554. if(warn)
  1555. fprint(2, "warning: <TR> outside <TABLE>\n");
  1556. continue;
  1557. }
  1558. if(ps->inpar) {
  1559. popjust(ps);
  1560. ps->inpar = 0;
  1561. }
  1562. ps = finishcell(curtab, ps);
  1563. if(curtab->rows != nil)
  1564. curtab->rows->flags = 0;
  1565. curtab->rows = newtablerow(aalign(tok),
  1566. makebackground(nil, acolorval(tok, Abgcolor, curtab->background.color)),
  1567. TFparsing,
  1568. curtab->rows);
  1569. break;
  1570. case Ttr+RBRA:
  1571. if(curtab == nil || curtab->rows == nil) {
  1572. if(warn)
  1573. fprint(2, "warning: unexpected </TR>\n");
  1574. continue;
  1575. }
  1576. ps = finishcell(curtab, ps);
  1577. tr = curtab->rows;
  1578. if(tr->cells == nil) {
  1579. if(warn)
  1580. fprint(2, "warning: empty row\n");
  1581. curtab->rows = tr->next;
  1582. tr->next = nil;
  1583. }
  1584. else
  1585. tr->flags = 0;
  1586. break;
  1587. // <!ELEMENT (TT|CODE|KBD|SAMP) - - (%text)*>
  1588. case Ttt:
  1589. case Tcode:
  1590. case Tkbd:
  1591. case Tsamp:
  1592. pushfontstyle(ps, FntT);
  1593. break;
  1594. // Tags that have empty action
  1595. case Tabbr:
  1596. case Tabbr+RBRA:
  1597. case Tacronym:
  1598. case Tacronym+RBRA:
  1599. case Tarea+RBRA:
  1600. case Tbase+RBRA:
  1601. case Tbasefont+RBRA:
  1602. case Tbr+RBRA:
  1603. case Tdd+RBRA:
  1604. case Tdt+RBRA:
  1605. case Tframe+RBRA:
  1606. case Thr+RBRA:
  1607. case Thtml:
  1608. case Thtml+RBRA:
  1609. case Timg+RBRA:
  1610. case Tinput+RBRA:
  1611. case Tisindex+RBRA:
  1612. case Tli+RBRA:
  1613. case Tlink:
  1614. case Tlink+RBRA:
  1615. case Tmeta+RBRA:
  1616. case Toption+RBRA:
  1617. case Tparam+RBRA:
  1618. case Ttextarea+RBRA:
  1619. case Ttitle+RBRA:
  1620. break;
  1621. // Tags not implemented
  1622. case Tbdo:
  1623. case Tbdo+RBRA:
  1624. case Tbutton:
  1625. case Tbutton+RBRA:
  1626. case Tdel:
  1627. case Tdel+RBRA:
  1628. case Tfieldset:
  1629. case Tfieldset+RBRA:
  1630. case Tiframe:
  1631. case Tiframe+RBRA:
  1632. case Tins:
  1633. case Tins+RBRA:
  1634. case Tlabel:
  1635. case Tlabel+RBRA:
  1636. case Tlegend:
  1637. case Tlegend+RBRA:
  1638. case Tobject:
  1639. case Tobject+RBRA:
  1640. case Toptgroup:
  1641. case Toptgroup+RBRA:
  1642. case Tspan:
  1643. case Tspan+RBRA:
  1644. if(warn) {
  1645. if(tag > RBRA)
  1646. tag -= RBRA;
  1647. fprint(2, "warning: unimplemented HTML tag: %S\n", tagnames[tag]);
  1648. }
  1649. break;
  1650. default:
  1651. if(warn)
  1652. fprint(2, "warning: unknown HTML tag: %S\n", tok->text);
  1653. break;
  1654. }
  1655. }
  1656. // some pages omit trailing </table>
  1657. while(curtab != nil) {
  1658. if(warn)
  1659. fprint(2, "warning: <TABLE> not closed\n");
  1660. if(curtab->cells != nil) {
  1661. ps = finishcell(curtab, ps);
  1662. if(curtab->cells == nil) {
  1663. if(warn)
  1664. fprint(2, "warning: empty table\n");
  1665. }
  1666. else {
  1667. if(curtab->rows != nil)
  1668. curtab->rows->flags = 0;
  1669. finish_table(curtab);
  1670. ps->skipping = 0;
  1671. additem(ps, newitable(curtab), curtab->tabletok);
  1672. addbrk(ps, 0, 0);
  1673. }
  1674. }
  1675. if(is->tabstk != nil)
  1676. is->tabstk = is->tabstk->next;
  1677. curtab->next = di->tables;
  1678. di->tables = curtab;
  1679. curtab = is->tabstk;
  1680. }
  1681. outerps = lastps(ps);
  1682. ans = outerps->items->next;
  1683. // note: ans may be nil and di->kids not nil, if there's a frameset!
  1684. outerps->items = newispacer(ISPnull);
  1685. outerps->lastit = outerps->items;
  1686. is->psstk = ps;
  1687. if(ans != nil && di->hasscripts) {
  1688. // TODO evalscript(nil);
  1689. ;
  1690. }
  1691. return_ans:
  1692. if(dbgbuild) {
  1693. assert(validitems(ans));
  1694. if(ans == nil)
  1695. fprint(2, "getitems returning nil\n");
  1696. else
  1697. printitems(ans, "getitems returning:");
  1698. }
  1699. return ans;
  1700. }
  1701. // Concatenate together maximal set of Data tokens, starting at toks[toki+1].
  1702. // Lexer has ensured that there will either be a following non-data token or
  1703. // we will be at eof.
  1704. // Return emallocd trimmed concatenation, and update *ptoki to last used toki
  1705. static Rune*
  1706. getpcdata(Token* toks, int tokslen, int* ptoki)
  1707. {
  1708. Rune* ans;
  1709. Rune* p;
  1710. Rune* trimans;
  1711. int anslen;
  1712. int trimanslen;
  1713. int toki;
  1714. Token* tok;
  1715. ans = nil;
  1716. anslen = 0;
  1717. // first find length of answer
  1718. toki = (*ptoki) + 1;
  1719. while(toki < tokslen) {
  1720. tok = &toks[toki];
  1721. if(tok->tag == Data) {
  1722. toki++;
  1723. anslen += _Strlen(tok->text);
  1724. }
  1725. else
  1726. break;
  1727. }
  1728. // now make up the initial answer
  1729. if(anslen > 0) {
  1730. ans = _newstr(anslen);
  1731. p = ans;
  1732. toki = (*ptoki) + 1;
  1733. while(toki < tokslen) {
  1734. tok = &toks[toki];
  1735. if(tok->tag == Data) {
  1736. toki++;
  1737. p = _Stradd(p, tok->text, _Strlen(tok->text));
  1738. }
  1739. else
  1740. break;
  1741. }
  1742. *p = 0;
  1743. _trimwhite(ans, anslen, &trimans, &trimanslen);
  1744. if(trimanslen != anslen) {
  1745. p = ans;
  1746. ans = _Strndup(trimans, trimanslen);
  1747. free(p);
  1748. }
  1749. }
  1750. *ptoki = toki-1;
  1751. return ans;
  1752. }
  1753. // If still parsing head of curtab->cells list, finish it off
  1754. // by transferring the items on the head of psstk to the cell.
  1755. // Then pop the psstk and return the new psstk.
  1756. static Pstate*
  1757. finishcell(Table* curtab, Pstate* psstk)
  1758. {
  1759. Tablecell* c;
  1760. Pstate* psstknext;
  1761. c = curtab->cells;
  1762. if(c != nil) {
  1763. if((c->flags&TFparsing)) {
  1764. psstknext = psstk->next;
  1765. if(psstknext == nil) {
  1766. if(warn)
  1767. fprint(2, "warning: parse state stack is wrong\n");
  1768. }
  1769. else {
  1770. c->content = psstk->items->next;
  1771. c->flags &= ~TFparsing;
  1772. freepstate(psstk);
  1773. psstk = psstknext;
  1774. }
  1775. }
  1776. }
  1777. return psstk;
  1778. }
  1779. // Make a new Pstate for a cell, based on the old pstate, oldps.
  1780. // Also, put the new ps on the head of the oldps stack.
  1781. static Pstate*
  1782. cell_pstate(Pstate* oldps, int ishead)
  1783. {
  1784. Pstate* ps;
  1785. int sty;
  1786. ps = newpstate(oldps);
  1787. ps->skipwhite = 1;
  1788. ps->curanchor = oldps->curanchor;
  1789. copystack(&ps->fntstylestk, &oldps->fntstylestk);
  1790. copystack(&ps->fntsizestk, &oldps->fntsizestk);
  1791. ps->curfont = oldps->curfont;
  1792. ps->curfg = oldps->curfg;
  1793. ps->curbg = oldps->curbg;
  1794. copystack(&ps->fgstk, &oldps->fgstk);
  1795. ps->adjsize = oldps->adjsize;
  1796. if(ishead) {
  1797. sty = ps->curfont%NumSize;
  1798. ps->curfont = FntB*NumSize + sty;
  1799. }
  1800. return ps;
  1801. }
  1802. // Return a new Pstate with default starting state.
  1803. // Use link to add it to head of a list, if any.
  1804. static Pstate*
  1805. newpstate(Pstate* link)
  1806. {
  1807. Pstate* ps;
  1808. ps = (Pstate*)emalloc(sizeof(Pstate));
  1809. ps->curfont = DefFnt;
  1810. ps->curfg = Black;
  1811. ps->curbg.image = nil;
  1812. ps->curbg.color = White;
  1813. ps->curul = ULnone;
  1814. ps->curjust = ALleft;
  1815. ps->curstate = IFwrap;
  1816. ps->items = newispacer(ISPnull);
  1817. ps->lastit = ps->items;
  1818. ps->prelastit = nil;
  1819. ps->next = link;
  1820. return ps;
  1821. }
  1822. // Return last Pstate on psl list
  1823. static Pstate*
  1824. lastps(Pstate* psl)
  1825. {
  1826. assert(psl != nil);
  1827. while(psl->next != nil)
  1828. psl = psl->next;
  1829. return psl;
  1830. }
  1831. // Add it to end of ps item chain, adding in current state from ps.
  1832. // Also, if tok is not nil, scan it for generic attributes and assign
  1833. // the genattr field of the item accordingly.
  1834. static void
  1835. additem(Pstate* ps, Item* it, Token* tok)
  1836. {
  1837. int aid;
  1838. int any;
  1839. Rune* i;
  1840. Rune* c;
  1841. Rune* s;
  1842. Rune* t;
  1843. Attr* a;
  1844. SEvent* e;
  1845. if(ps->skipping) {
  1846. if(warn)
  1847. fprint(2, "warning: skipping item: %I\n", it);
  1848. return;
  1849. }
  1850. it->anchorid = ps->curanchor;
  1851. it->state |= ps->curstate;
  1852. if(tok != nil) {
  1853. any = 0;
  1854. i = nil;
  1855. c = nil;
  1856. s = nil;
  1857. t = nil;
  1858. e = nil;
  1859. for(a = tok->attr; a != nil; a = a->next) {
  1860. aid = a->attid;
  1861. if(!attrinfo[aid])
  1862. continue;
  1863. switch(aid) {
  1864. case Aid:
  1865. i = a->value;
  1866. break;
  1867. case Aclass:
  1868. c = a->value;
  1869. break;
  1870. case Astyle:
  1871. s = a->value;
  1872. break;
  1873. case Atitle:
  1874. t = a->value;
  1875. break;
  1876. default:
  1877. assert(aid >= Aonblur && aid <= Aonunload);
  1878. e = newscriptevent(scriptev[a->attid], a->value, e);
  1879. break;
  1880. }
  1881. a->value = nil;
  1882. any = 1;
  1883. }
  1884. if(any)
  1885. it->genattr = newgenattr(i, c, s, t, e);
  1886. }
  1887. ps->curstate &= ~(IFbrk|IFbrksp|IFnobrk|IFcleft|IFcright);
  1888. ps->prelastit = ps->lastit;
  1889. ps->lastit->next = it;
  1890. ps->lastit = it;
  1891. }
  1892. // Make a text item out of s,
  1893. // using current font, foreground, vertical offset and underline state.
  1894. static Item*
  1895. textit(Pstate* ps, Rune* s)
  1896. {
  1897. assert(s != nil);
  1898. return newitext(s, ps->curfont, ps->curfg, ps->curvoff + Voffbias, ps->curul);
  1899. }
  1900. // Add text item or items for s, paying attention to
  1901. // current font, foreground, baseline offset, underline state,
  1902. // and literal mode. Unless we're in literal mode, compress
  1903. // whitespace to single blank, and, if curstate has a break,
  1904. // trim any leading whitespace. Whether in literal mode or not,
  1905. // turn nonbreaking spaces into spacer items with IFnobrk set.
  1906. //
  1907. // In literal mode, break up s at newlines and add breaks instead.
  1908. // Also replace tabs appropriate number of spaces.
  1909. // In nonliteral mode, break up the items every 100 or so characters
  1910. // just to make the layout algorithm not go quadratic.
  1911. //
  1912. // addtext assumes ownership of s.
  1913. static void
  1914. addtext(Pstate* ps, Rune* s)
  1915. {
  1916. int n;
  1917. int i;
  1918. int j;
  1919. int k;
  1920. int col;
  1921. int c;
  1922. int nsp;
  1923. Item* it;
  1924. Rune* ss;
  1925. Rune* p;
  1926. Rune buf[SMALLBUFSIZE];
  1927. assert(s != nil);
  1928. n = runestrlen(s);
  1929. i = 0;
  1930. j = 0;
  1931. if(ps->literal) {
  1932. col = 0;
  1933. while(i < n) {
  1934. if(s[i] == '\n') {
  1935. if(i > j) {
  1936. // trim trailing blanks from line
  1937. for(k = i; k > j; k--)
  1938. if(s[k - 1] != ' ')
  1939. break;
  1940. if(k > j)
  1941. additem(ps, textit(ps, _Strndup(s+j, k-j)), nil);
  1942. }
  1943. addlinebrk(ps, 0);
  1944. j = i + 1;
  1945. col = 0;
  1946. }
  1947. else {
  1948. if(s[i] == '\t') {
  1949. col += i - j;
  1950. nsp = 8 - (col%8);
  1951. // make ss = s[j:i] + nsp spaces
  1952. ss = _newstr(i-j+nsp);
  1953. p = _Stradd(ss, s+j, i-j);
  1954. p = _Stradd(p, L" ", nsp);
  1955. *p = 0;
  1956. additem(ps, textit(ps, ss), nil);
  1957. col += nsp;
  1958. j = i + 1;
  1959. }
  1960. else if(s[i] == NBSP) {
  1961. if(i > j)
  1962. additem(ps, textit(ps, _Strndup(s+j, i-j)), nil);
  1963. addnbsp(ps);
  1964. col += (i - j) + 1;
  1965. j = i + 1;
  1966. }
  1967. }
  1968. i++;
  1969. }
  1970. if(i > j) {
  1971. if(j == 0 && i == n) {
  1972. // just transfer s over
  1973. additem(ps, textit(ps, s), nil);
  1974. }
  1975. else {
  1976. additem(ps, textit(ps, _Strndup(s+j, i-j)), nil);
  1977. free(s);
  1978. }
  1979. }
  1980. }
  1981. else { // not literal mode
  1982. if((ps->curstate&IFbrk) || ps->lastit == ps->items)
  1983. while(i < n) {
  1984. c = s[i];
  1985. if(c >= 256 || !isspace(c))
  1986. break;
  1987. i++;
  1988. }
  1989. p = buf;
  1990. for(j = i; i < n; i++) {
  1991. assert(p+i-j < buf+SMALLBUFSIZE-1);
  1992. c = s[i];
  1993. if(c == NBSP) {
  1994. if(i > j)
  1995. p = _Stradd(p, s+j, i-j);
  1996. if(p > buf)
  1997. additem(ps, textit(ps, _Strndup(buf, p-buf)), nil);
  1998. p = buf;
  1999. addnbsp(ps);
  2000. j = i + 1;
  2001. continue;
  2002. }
  2003. if(c < 256 && isspace(c)) {
  2004. if(i > j)
  2005. p = _Stradd(p, s+j, i-j);
  2006. *p++ = ' ';
  2007. while(i < n - 1) {
  2008. c = s[i + 1];
  2009. if(c >= 256 || !isspace(c))
  2010. break;
  2011. i++;
  2012. }
  2013. j = i + 1;
  2014. }
  2015. if(i - j >= 100) {
  2016. p = _Stradd(p, s+j, i+1-j);
  2017. j = i + 1;
  2018. }
  2019. if(p-buf >= 100) {
  2020. additem(ps, textit(ps, _Strndup(buf, p-buf)), nil);
  2021. p = buf;
  2022. }
  2023. }
  2024. if(i > j && j < n) {
  2025. assert(p+i-j < buf+SMALLBUFSIZE-1);
  2026. p = _Stradd(p, s+j, i-j);
  2027. }
  2028. // don't add a space if previous item ended in a space
  2029. if(p-buf == 1 && buf[0] == ' ' && ps->lastit != nil) {
  2030. it = ps->lastit;
  2031. if(it->tag == Itexttag) {
  2032. ss = ((Itext*)it)->s;
  2033. k = _Strlen(ss);
  2034. if(k > 0 && ss[k] == ' ')
  2035. p = buf;
  2036. }
  2037. }
  2038. if(p > buf)
  2039. additem(ps, textit(ps, _Strndup(buf, p-buf)), nil);
  2040. free(s);
  2041. }
  2042. }
  2043. // Add a break to ps->curstate, with extra space if sp is true.
  2044. // If there was a previous break, combine this one's parameters
  2045. // with that to make the amt be the max of the two and the clr
  2046. // be the most general. (amt will be 0 or 1)
  2047. // Also, if the immediately preceding item was a text item,
  2048. // trim any whitespace from the end of it, if not in literal mode.
  2049. // Finally, if this is at the very beginning of the item list
  2050. // (the only thing there is a null spacer), then don't add the space.
  2051. static void
  2052. addbrk(Pstate* ps, int sp, int clr)
  2053. {
  2054. int state;
  2055. Rune* l;
  2056. int nl;
  2057. Rune* r;
  2058. int nr;
  2059. Itext* t;
  2060. Rune* s;
  2061. state = ps->curstate;
  2062. clr = clr|(state&(IFcleft|IFcright));
  2063. if(sp && !(ps->lastit == ps->items))
  2064. sp = IFbrksp;
  2065. else
  2066. sp = 0;
  2067. ps->curstate = IFbrk|sp|(state&~(IFcleft|IFcright))|clr;
  2068. if(ps->lastit != ps->items) {
  2069. if(!ps->literal && ps->lastit->tag == Itexttag) {
  2070. t = (Itext*)ps->lastit;
  2071. _splitr(t->s, _Strlen(t->s), notwhitespace, &l, &nl, &r, &nr);
  2072. // try to avoid making empty items
  2073. // but not crucial f the occasional one gets through
  2074. if(nl == 0 && ps->prelastit != nil) {
  2075. ps->lastit = ps->prelastit;
  2076. ps->lastit->next = nil;
  2077. ps->prelastit = nil;
  2078. }
  2079. else {
  2080. s = t->s;
  2081. if(nl == 0) {
  2082. // need a non-nil pointer to empty string
  2083. // (_Strdup(L"") returns nil)
  2084. t->s = emalloc(sizeof(Rune));
  2085. t->s[0] = 0;
  2086. }
  2087. else
  2088. t->s = _Strndup(l, nl);
  2089. if(s)
  2090. free(s);
  2091. }
  2092. }
  2093. }
  2094. }
  2095. // Add break due to a <br> or a newline within a preformatted section.
  2096. // We add a null item first, with current font's height and ascent, to make
  2097. // sure that the current line takes up at least that amount of vertical space.
  2098. // This ensures that <br>s on empty lines cause blank lines, and that
  2099. // multiple <br>s in a row give multiple blank lines.
  2100. // However don't add the spacer if the previous item was something that
  2101. // takes up space itself.
  2102. static void
  2103. addlinebrk(Pstate* ps, int clr)
  2104. {
  2105. int obrkstate;
  2106. int b;
  2107. int addit;
  2108. // don't want break before our null item unless the previous item
  2109. // was also a null item for the purposes of line breaking
  2110. obrkstate = ps->curstate&(IFbrk|IFbrksp);
  2111. b = IFnobrk;
  2112. addit = 0;
  2113. if(ps->lastit != nil) {
  2114. if(ps->lastit->tag == Ispacertag) {
  2115. if(((Ispacer*)ps->lastit)->spkind == ISPvline)
  2116. b = IFbrk;
  2117. addit = 1;
  2118. }
  2119. else if(ps->lastit->tag == Ifloattag)
  2120. addit = 1;
  2121. }
  2122. if(addit) {
  2123. ps->curstate = (ps->curstate&~(IFbrk|IFbrksp))|b;
  2124. additem(ps, newispacer(ISPvline), nil);
  2125. ps->curstate = (ps->curstate&~(IFbrk|IFbrksp))|obrkstate;
  2126. }
  2127. addbrk(ps, 0, clr);
  2128. }
  2129. // Add a nonbreakable space
  2130. static void
  2131. addnbsp(Pstate* ps)
  2132. {
  2133. // if nbsp comes right where a break was specified,
  2134. // do the break anyway (nbsp is being used to generate undiscardable
  2135. // space rather than to prevent a break)
  2136. if((ps->curstate&IFbrk) == 0)
  2137. ps->curstate |= IFnobrk;
  2138. additem(ps, newispacer(ISPhspace), nil);
  2139. // but definitely no break on next item
  2140. ps->curstate |= IFnobrk;
  2141. }
  2142. // Change hang in ps.curstate by delta.
  2143. // The amount is in 1/10ths of tabs, and is the amount that
  2144. // the current contiguous set of items with a hang value set
  2145. // is to be shifted left from its normal (indented) place.
  2146. static void
  2147. changehang(Pstate* ps, int delta)
  2148. {
  2149. int amt;
  2150. amt = (ps->curstate&IFhangmask) + delta;
  2151. if(amt < 0) {
  2152. if(warn)
  2153. fprint(2, "warning: hang went negative\n");
  2154. amt = 0;
  2155. }
  2156. ps->curstate = (ps->curstate&~IFhangmask)|amt;
  2157. }
  2158. // Change indent in ps.curstate by delta.
  2159. static void
  2160. changeindent(Pstate* ps, int delta)
  2161. {
  2162. int amt;
  2163. amt = ((ps->curstate&IFindentmask) >> IFindentshift) + delta;
  2164. if(amt < 0) {
  2165. if(warn)
  2166. fprint(2, "warning: indent went negative\n");
  2167. amt = 0;
  2168. }
  2169. ps->curstate = (ps->curstate&~IFindentmask)|(amt << IFindentshift);
  2170. }
  2171. // Push val on top of stack, and also return value pushed
  2172. static int
  2173. push(Stack* stk, int val)
  2174. {
  2175. if(stk->n == Nestmax) {
  2176. if(warn)
  2177. fprint(2, "warning: build stack overflow\n");
  2178. }
  2179. else
  2180. stk->slots[stk->n++] = val;
  2181. return val;
  2182. }
  2183. // Pop top of stack
  2184. static void
  2185. pop(Stack* stk)
  2186. {
  2187. if(stk->n > 0)
  2188. --stk->n;
  2189. }
  2190. //Return top of stack, using dflt if stack is empty
  2191. static int
  2192. top(Stack* stk, int dflt)
  2193. {
  2194. if(stk->n == 0)
  2195. return dflt;
  2196. return stk->slots[stk->n-1];
  2197. }
  2198. // pop, then return new top, with dflt if empty
  2199. static int
  2200. popretnewtop(Stack* stk, int dflt)
  2201. {
  2202. if(stk->n == 0)
  2203. return dflt;
  2204. stk->n--;
  2205. if(stk->n == 0)
  2206. return dflt;
  2207. return stk->slots[stk->n-1];
  2208. }
  2209. // Copy fromstk entries into tostk
  2210. static void
  2211. copystack(Stack* tostk, Stack* fromstk)
  2212. {
  2213. int n;
  2214. n = fromstk->n;
  2215. tostk->n = n;
  2216. memmove(tostk->slots, fromstk->slots, n*sizeof(int));
  2217. }
  2218. static void
  2219. popfontstyle(Pstate* ps)
  2220. {
  2221. pop(&ps->fntstylestk);
  2222. setcurfont(ps);
  2223. }
  2224. static void
  2225. pushfontstyle(Pstate* ps, int sty)
  2226. {
  2227. push(&ps->fntstylestk, sty);
  2228. setcurfont(ps);
  2229. }
  2230. static void
  2231. popfontsize(Pstate* ps)
  2232. {
  2233. pop(&ps->fntsizestk);
  2234. setcurfont(ps);
  2235. }
  2236. static void
  2237. pushfontsize(Pstate* ps, int sz)
  2238. {
  2239. push(&ps->fntsizestk, sz);
  2240. setcurfont(ps);
  2241. }
  2242. static void
  2243. setcurfont(Pstate* ps)
  2244. {
  2245. int sty;
  2246. int sz;
  2247. sty = top(&ps->fntstylestk, FntR);
  2248. sz = top(&ps->fntsizestk, Normal);
  2249. if(sz < Tiny)
  2250. sz = Tiny;
  2251. if(sz > Verylarge)
  2252. sz = Verylarge;
  2253. ps->curfont = sty*NumSize + sz;
  2254. }
  2255. static void
  2256. popjust(Pstate* ps)
  2257. {
  2258. pop(&ps->juststk);
  2259. setcurjust(ps);
  2260. }
  2261. static void
  2262. pushjust(Pstate* ps, int j)
  2263. {
  2264. push(&ps->juststk, j);
  2265. setcurjust(ps);
  2266. }
  2267. static void
  2268. setcurjust(Pstate* ps)
  2269. {
  2270. int j;
  2271. int state;
  2272. j = top(&ps->juststk, ALleft);
  2273. if(j != ps->curjust) {
  2274. ps->curjust = j;
  2275. state = ps->curstate;
  2276. state &= ~(IFrjust|IFcjust);
  2277. if(j == ALcenter)
  2278. state |= IFcjust;
  2279. else if(j == ALright)
  2280. state |= IFrjust;
  2281. ps->curstate = state;
  2282. }
  2283. }
  2284. // Do final rearrangement after table parsing is finished
  2285. // and assign cells to grid points
  2286. static void
  2287. finish_table(Table* t)
  2288. {
  2289. int ncol;
  2290. int nrow;
  2291. int r;
  2292. Tablerow* rl;
  2293. Tablecell* cl;
  2294. int* rowspancnt;
  2295. Tablecell** rowspancell;
  2296. int ri;
  2297. int ci;
  2298. Tablecell* c;
  2299. Tablecell* cnext;
  2300. Tablerow* row;
  2301. Tablerow* rownext;
  2302. int rcols;
  2303. int newncol;
  2304. int k;
  2305. int j;
  2306. int cspan;
  2307. int rspan;
  2308. int i;
  2309. rl = t->rows;
  2310. t->nrow = nrow = _listlen((List*)rl);
  2311. t->rows = (Tablerow*)emalloc(nrow * sizeof(Tablerow));
  2312. ncol = 0;
  2313. r = nrow - 1;
  2314. for(row = rl; row != nil; row = rownext) {
  2315. // copy the data from the allocated Tablerow into the array slot
  2316. t->rows[r] = *row;
  2317. rownext = row->next;
  2318. row = &t->rows[r];
  2319. r--;
  2320. rcols = 0;
  2321. c = row->cells;
  2322. // If rowspan is > 1 but this is the last row,
  2323. // reset the rowspan
  2324. if(c != nil && c->rowspan > 1 && r == nrow-2)
  2325. c->rowspan = 1;
  2326. // reverse row->cells list (along nextinrow pointers)
  2327. row->cells = nil;
  2328. while(c != nil) {
  2329. cnext = c->nextinrow;
  2330. c->nextinrow = row->cells;
  2331. row->cells = c;
  2332. rcols += c->colspan;
  2333. c = cnext;
  2334. }
  2335. if(rcols > ncol)
  2336. ncol = rcols;
  2337. }
  2338. t->ncol = ncol;
  2339. t->cols = (Tablecol*)emalloc(ncol * sizeof(Tablecol));
  2340. // Reverse cells just so they are drawn in source order.
  2341. // Also, trim their contents so they don't end in whitespace.
  2342. t->cells = (Tablecell*)_revlist((List*)t->cells);
  2343. for(c = t->cells; c != nil; c= c->next)
  2344. trim_cell(c);
  2345. t->grid = (Tablecell***)emalloc(nrow * sizeof(Tablecell**));
  2346. for(i = 0; i < nrow; i++)
  2347. t->grid[i] = (Tablecell**)emalloc(ncol * sizeof(Tablecell*));
  2348. // The following arrays keep track of cells that are spanning
  2349. // multiple rows; rowspancnt[i] is the number of rows left
  2350. // to be spanned in column i.
  2351. // When done, cell's (row,col) is upper left grid point.
  2352. rowspancnt = (int*)emalloc(ncol * sizeof(int));
  2353. rowspancell = (Tablecell**)emalloc(ncol * sizeof(Tablecell*));
  2354. for(ri = 0; ri < nrow; ri++) {
  2355. row = &t->rows[ri];
  2356. cl = row->cells;
  2357. ci = 0;
  2358. while(ci < ncol || cl != nil) {
  2359. if(ci < ncol && rowspancnt[ci] > 0) {
  2360. t->grid[ri][ci] = rowspancell[ci];
  2361. rowspancnt[ci]--;
  2362. ci++;
  2363. }
  2364. else {
  2365. if(cl == nil) {
  2366. ci++;
  2367. continue;
  2368. }
  2369. c = cl;
  2370. cl = cl->nextinrow;
  2371. cspan = c->colspan;
  2372. rspan = c->rowspan;
  2373. if(ci + cspan > ncol) {
  2374. // because of row spanning, we calculated
  2375. // ncol incorrectly; adjust it
  2376. newncol = ci + cspan;
  2377. t->cols = (Tablecol*)erealloc(t->cols, newncol * sizeof(Tablecol));
  2378. rowspancnt = (int*)erealloc(rowspancnt, newncol * sizeof(int));
  2379. rowspancell = (Tablecell**)erealloc(rowspancell, newncol * sizeof(Tablecell*));
  2380. k = newncol-ncol;
  2381. memset(t->cols+ncol, 0, k*sizeof(Tablecol));
  2382. memset(rowspancnt+ncol, 0, k*sizeof(int));
  2383. memset(rowspancell+ncol, 0, k*sizeof(Tablecell*));
  2384. for(j = 0; j < nrow; j++) {
  2385. t->grid[j] = (Tablecell**)erealloc(t->grid[j], newncol * sizeof(Tablecell*));
  2386. memset(t->grid[j], 0, k*sizeof(Tablecell*));
  2387. }
  2388. t->ncol = ncol = newncol;
  2389. }
  2390. c->row = ri;
  2391. c->col = ci;
  2392. for(i = 0; i < cspan; i++) {
  2393. t->grid[ri][ci] = c;
  2394. if(rspan > 1) {
  2395. rowspancnt[ci] = rspan - 1;
  2396. rowspancell[ci] = c;
  2397. }
  2398. ci++;
  2399. }
  2400. }
  2401. }
  2402. }
  2403. }
  2404. // Remove tail of cell content until it isn't whitespace.
  2405. static void
  2406. trim_cell(Tablecell* c)
  2407. {
  2408. int dropping;
  2409. Rune* s;
  2410. Rune* x;
  2411. Rune* y;
  2412. int nx;
  2413. int ny;
  2414. Item* p;
  2415. Itext* q;
  2416. Item* pprev;
  2417. dropping = 1;
  2418. while(c->content != nil && dropping) {
  2419. p = c->content;
  2420. pprev = nil;
  2421. while(p->next != nil) {
  2422. pprev = p;
  2423. p = p->next;
  2424. }
  2425. dropping = 0;
  2426. if(!(p->state&IFnobrk)) {
  2427. if(p->tag == Itexttag) {
  2428. q = (Itext*)p;
  2429. s = q->s;
  2430. _splitr(s, _Strlen(s), notwhitespace, &x, &nx, &y, &ny);
  2431. if(nx != 0 && ny != 0) {
  2432. q->s = _Strndup(x, nx);
  2433. free(s);
  2434. }
  2435. break;
  2436. }
  2437. }
  2438. if(dropping) {
  2439. if(pprev == nil)
  2440. c->content = nil;
  2441. else
  2442. pprev->next = nil;
  2443. freeitem(p);
  2444. }
  2445. }
  2446. }
  2447. // Caller must free answer (eventually).
  2448. static Rune*
  2449. listmark(uchar ty, int n)
  2450. {
  2451. Rune* s;
  2452. Rune* t;
  2453. int n2;
  2454. int i;
  2455. s = nil;
  2456. switch(ty) {
  2457. case LTdisc:
  2458. case LTsquare:
  2459. case LTcircle:
  2460. s = _newstr(1);
  2461. s[0] = (ty == LTdisc)? 0x2022 // bullet
  2462. : ((ty == LTsquare)? 0x220e // filled square
  2463. : 0x2218); // degree
  2464. s[1] = 0;
  2465. break;
  2466. case LT1:
  2467. t = _ltoStr(n);
  2468. n2 = _Strlen(t);
  2469. s = _newstr(n2+1);
  2470. t = _Stradd(s, t, n2);
  2471. *t++ = '.';
  2472. *t = 0;
  2473. break;
  2474. case LTa:
  2475. case LTA:
  2476. n--;
  2477. i = 0;
  2478. if(n < 0)
  2479. n = 0;
  2480. s = _newstr((n <= 25)? 2 : 3);
  2481. if(n > 25) {
  2482. n2 = n%26;
  2483. n /= 26;
  2484. if(n2 > 25)
  2485. n2 = 25;
  2486. s[i++] = n2 + (ty == LTa)? 'a' : 'A';
  2487. }
  2488. s[i++] = n + (ty == LTa)? 'a' : 'A';
  2489. s[i++] = '.';
  2490. s[i] = 0;
  2491. break;
  2492. case LTi:
  2493. case LTI:
  2494. if(n >= NROMAN) {
  2495. if(warn)
  2496. fprint(2, "warning: unimplemented roman number > %d\n", NROMAN);
  2497. n = NROMAN;
  2498. }
  2499. t = roman[n - 1];
  2500. n2 = _Strlen(t);
  2501. s = _newstr(n2+1);
  2502. for(i = 0; i < n2; i++)
  2503. s[i] = (ty == LTi)? tolower(t[i]) : t[i];
  2504. s[i++] = '.';
  2505. s[i] = 0;
  2506. break;
  2507. }
  2508. return s;
  2509. }
  2510. // Find map with given name in di.maps.
  2511. // If not there, add one, copying name.
  2512. // Ownership of map remains with di->maps list.
  2513. static Map*
  2514. getmap(Docinfo* di, Rune* name)
  2515. {
  2516. Map* m;
  2517. for(m = di->maps; m != nil; m = m->next) {
  2518. if(!_Strcmp(name, m->name))
  2519. return m;
  2520. }
  2521. m = (Map*)emalloc(sizeof(Map));
  2522. m->name = _Strdup(name);
  2523. m->areas = nil;
  2524. m->next = di->maps;
  2525. di->maps = m;
  2526. return m;
  2527. }
  2528. // Transfers ownership of href to Area
  2529. static Area*
  2530. newarea(int shape, Rune* href, int target, Area* link)
  2531. {
  2532. Area* a;
  2533. a = (Area*)emalloc(sizeof(Area));
  2534. a->shape = shape;
  2535. a->href = href;
  2536. a->target = target;
  2537. a->next = link;
  2538. return a;
  2539. }
  2540. // Return string value associated with attid in tok, nil if none.
  2541. // Caller must free the result (eventually).
  2542. static Rune*
  2543. aval(Token* tok, int attid)
  2544. {
  2545. Rune* ans;
  2546. _tokaval(tok, attid, &ans, 1); // transfers string ownership from token to ans
  2547. return ans;
  2548. }
  2549. // Like aval, but use dflt if there was no such attribute in tok.
  2550. // Caller must free the result (eventually).
  2551. static Rune*
  2552. astrval(Token* tok, int attid, Rune* dflt)
  2553. {
  2554. Rune* ans;
  2555. if(_tokaval(tok, attid, &ans, 1))
  2556. return ans; // transfers string ownership from token to ans
  2557. else
  2558. return _Strdup(dflt);
  2559. }
  2560. // Here we're supposed to convert to an int,
  2561. // and have a default when not found
  2562. static int
  2563. aintval(Token* tok, int attid, int dflt)
  2564. {
  2565. Rune* ans;
  2566. if(!_tokaval(tok, attid, &ans, 0) || ans == nil)
  2567. return dflt;
  2568. else
  2569. return toint(ans);
  2570. }
  2571. // Like aintval, but result should be >= 0
  2572. static int
  2573. auintval(Token* tok, int attid, int dflt)
  2574. {
  2575. Rune* ans;
  2576. int v;
  2577. if(!_tokaval(tok, attid, &ans, 0) || ans == nil)
  2578. return dflt;
  2579. else {
  2580. v = toint(ans);
  2581. return v >= 0? v : 0;
  2582. }
  2583. }
  2584. // int conversion, but with possible error check (if warning)
  2585. static int
  2586. toint(Rune* s)
  2587. {
  2588. int ans;
  2589. Rune* eptr;
  2590. ans = _Strtol(s, &eptr, 10);
  2591. if(warn) {
  2592. if(*eptr != 0) {
  2593. eptr = _Strclass(eptr, notwhitespace);
  2594. if(eptr != nil)
  2595. fprint(2, "warning: expected integer, got %S\n", s);
  2596. }
  2597. }
  2598. return ans;
  2599. }
  2600. // Attribute value when need a table to convert strings to ints
  2601. static int
  2602. atabval(Token* tok, int attid, StringInt* tab, int ntab, int dflt)
  2603. {
  2604. Rune* aval;
  2605. int ans;
  2606. ans = dflt;
  2607. if(_tokaval(tok, attid, &aval, 0)) {
  2608. if(!_lookup(tab, ntab, aval, _Strlen(aval), &ans)) {
  2609. ans = dflt;
  2610. if(warn)
  2611. fprint(2, "warning: name not found in table lookup: %S\n", aval);
  2612. }
  2613. }
  2614. return ans;
  2615. }
  2616. // Attribute value when supposed to be a color
  2617. static int
  2618. acolorval(Token* tok, int attid, int dflt)
  2619. {
  2620. Rune* aval;
  2621. int ans;
  2622. ans = dflt;
  2623. if(_tokaval(tok, attid, &aval, 0))
  2624. ans = color(aval, dflt);
  2625. return ans;
  2626. }
  2627. // Attribute value when supposed to be a target frame name
  2628. static int
  2629. atargval(Token* tok, int dflt)
  2630. {
  2631. int ans;
  2632. Rune* aval;
  2633. ans = dflt;
  2634. if(_tokaval(tok, Atarget, &aval, 0)){
  2635. ans = targetid(aval);
  2636. }
  2637. return ans;
  2638. }
  2639. // special for list types, where "i" and "I" are different,
  2640. // but "square" and "SQUARE" are the same
  2641. static int
  2642. listtyval(Token* tok, int dflt)
  2643. {
  2644. Rune* aval;
  2645. int ans;
  2646. int n;
  2647. ans = dflt;
  2648. if(_tokaval(tok, Atype, &aval, 0)) {
  2649. n = _Strlen(aval);
  2650. if(n == 1) {
  2651. switch(aval[0]) {
  2652. case '1':
  2653. ans = LT1;
  2654. break;
  2655. case 'A':
  2656. ans = LTA;
  2657. break;
  2658. case 'I':
  2659. ans = LTI;
  2660. break;
  2661. case 'a':
  2662. ans = LTa;
  2663. break;
  2664. case 'i':
  2665. ans = LTi;
  2666. default:
  2667. if(warn)
  2668. fprint(2, "warning: unknown list element type %c\n", aval[0]);
  2669. }
  2670. }
  2671. else {
  2672. if(!_Strncmpci(aval, n, L"circle"))
  2673. ans = LTcircle;
  2674. else if(!_Strncmpci(aval, n, L"disc"))
  2675. ans = LTdisc;
  2676. else if(!_Strncmpci(aval, n, L"square"))
  2677. ans = LTsquare;
  2678. else {
  2679. if(warn)
  2680. fprint(2, "warning: unknown list element type %S\n", aval);
  2681. }
  2682. }
  2683. }
  2684. return ans;
  2685. }
  2686. // Attribute value when value is a URL, possibly relative to base.
  2687. // FOR NOW: leave the url relative.
  2688. // Caller must free the result (eventually).
  2689. static Rune*
  2690. aurlval(Token* tok, int attid, Rune* dflt, Rune* base)
  2691. {
  2692. Rune* ans;
  2693. Rune* url;
  2694. USED(base);
  2695. ans = nil;
  2696. if(_tokaval(tok, attid, &url, 0) && url != nil)
  2697. ans = removeallwhite(url);
  2698. if(ans == nil)
  2699. ans = _Strdup(dflt);
  2700. return ans;
  2701. }
  2702. // Return copy of s but with all whitespace (even internal) removed.
  2703. // This fixes some buggy URL specification strings.
  2704. static Rune*
  2705. removeallwhite(Rune* s)
  2706. {
  2707. int j;
  2708. int n;
  2709. int i;
  2710. int c;
  2711. Rune* ans;
  2712. j = 0;
  2713. n = _Strlen(s);
  2714. for(i = 0; i < n; i++) {
  2715. c = s[i];
  2716. if(c >= 256 || !isspace(c))
  2717. j++;
  2718. }
  2719. if(j < n) {
  2720. ans = _newstr(j);
  2721. j = 0;
  2722. for(i = 0; i < n; i++) {
  2723. c = s[i];
  2724. if(c >= 256 || !isspace(c))
  2725. ans[j++] = c;
  2726. }
  2727. ans[j] = 0;
  2728. }
  2729. else
  2730. ans = _Strdup(s);
  2731. return ans;
  2732. }
  2733. // Attribute value when mere presence of attr implies value of 1,
  2734. // but if there is an integer there, return it as the value.
  2735. static int
  2736. aflagval(Token* tok, int attid)
  2737. {
  2738. int val;
  2739. Rune* sval;
  2740. val = 0;
  2741. if(_tokaval(tok, attid, &sval, 0)) {
  2742. val = 1;
  2743. if(sval != nil)
  2744. val = toint(sval);
  2745. }
  2746. return val;
  2747. }
  2748. static Align
  2749. makealign(int halign, int valign)
  2750. {
  2751. Align al;
  2752. al.halign = halign;
  2753. al.valign = valign;
  2754. return al;
  2755. }
  2756. // Make an Align (two alignments, horizontal and vertical)
  2757. static Align
  2758. aalign(Token* tok)
  2759. {
  2760. return makealign(
  2761. atabval(tok, Aalign, align_tab, NALIGNTAB, ALnone),
  2762. atabval(tok, Avalign, align_tab, NALIGNTAB, ALnone));
  2763. }
  2764. // Make a Dimen, based on value of attid attr
  2765. static Dimen
  2766. adimen(Token* tok, int attid)
  2767. {
  2768. Rune* wd;
  2769. if(_tokaval(tok, attid, &wd, 0))
  2770. return parsedim(wd, _Strlen(wd));
  2771. else
  2772. return makedimen(Dnone, 0);
  2773. }
  2774. // Parse s[0:n] as num[.[num]][unit][%|*]
  2775. static Dimen
  2776. parsedim(Rune* s, int ns)
  2777. {
  2778. int kind;
  2779. int spec;
  2780. Rune* l;
  2781. int nl;
  2782. Rune* r;
  2783. int nr;
  2784. int mul;
  2785. int i;
  2786. Rune* f;
  2787. int nf;
  2788. int Tkdpi;
  2789. Rune* units;
  2790. kind = Dnone;
  2791. spec = 0;
  2792. _splitl(s, ns, L"^0-9", &l, &nl, &r, &nr);
  2793. if(nl != 0) {
  2794. spec = 1000*_Strtol(l, nil, 10);
  2795. if(nr > 0 && r[0] == '.') {
  2796. _splitl(r+1, nr-1, L"^0-9", &f, &nf, &r, &nr);
  2797. if(nf != 0) {
  2798. mul = 100;
  2799. for(i = 0; i < nf; i++) {
  2800. spec = spec + mul*(f[i]-'0');
  2801. mul = mul/10;
  2802. }
  2803. }
  2804. }
  2805. kind = Dpixels;
  2806. if(nr != 0) {
  2807. if(nr >= 2) {
  2808. Tkdpi = 100;
  2809. units = r;
  2810. r = r+2;
  2811. nr -= 2;
  2812. if(!_Strncmpci(units, 2, L"pt"))
  2813. spec = (spec*Tkdpi)/72;
  2814. else if(!_Strncmpci(units, 2, L"pi"))
  2815. spec = (spec*12*Tkdpi)/72;
  2816. else if(!_Strncmpci(units, 2, L"in"))
  2817. spec = spec*Tkdpi;
  2818. else if(!_Strncmpci(units, 2, L"cm"))
  2819. spec = (spec*100*Tkdpi)/254;
  2820. else if(!_Strncmpci(units, 2, L"mm"))
  2821. spec = (spec*10*Tkdpi)/254;
  2822. else if(!_Strncmpci(units, 2, L"em"))
  2823. spec = spec*15;
  2824. else {
  2825. if(warn)
  2826. fprint(2, "warning: unknown units %C%Cs\n", units[0], units[1]);
  2827. }
  2828. }
  2829. if(nr >= 1) {
  2830. if(r[0] == '%')
  2831. kind = Dpercent;
  2832. else if(r[0] == '*')
  2833. kind = Drelative;
  2834. }
  2835. }
  2836. spec = spec/1000;
  2837. }
  2838. else if(nr == 1 && r[0] == '*') {
  2839. spec = 1;
  2840. kind = Drelative;
  2841. }
  2842. return makedimen(kind, spec);
  2843. }
  2844. static void
  2845. setdimarray(Token* tok, int attid, Dimen** pans, int* panslen)
  2846. {
  2847. Rune* s;
  2848. Dimen* d;
  2849. int k;
  2850. int nc;
  2851. Rune* a[SMALLBUFSIZE];
  2852. int an[SMALLBUFSIZE];
  2853. if(_tokaval(tok, attid, &s, 0)) {
  2854. nc = _splitall(s, _Strlen(s), L", ", a, an, SMALLBUFSIZE);
  2855. if(nc > 0) {
  2856. d = (Dimen*)emalloc(nc * sizeof(Dimen));
  2857. for(k = 0; k < nc; k++) {
  2858. d[k] = parsedim(a[k], an[k]);
  2859. }
  2860. *pans = d;
  2861. *panslen = nc;
  2862. return;
  2863. }
  2864. }
  2865. *pans = nil;
  2866. *panslen = 0;
  2867. }
  2868. static Background
  2869. makebackground(Rune* imageurl, int color)
  2870. {
  2871. Background bg;
  2872. bg.image = imageurl;
  2873. bg.color = color;
  2874. return bg;
  2875. }
  2876. static Item*
  2877. newitext(Rune* s, int fnt, int fg, int voff, int ul)
  2878. {
  2879. Itext* t;
  2880. assert(s != nil);
  2881. t = (Itext*)emalloc(sizeof(Itext));
  2882. t->tag = Itexttag;
  2883. t->s = s;
  2884. t->fnt = fnt;
  2885. t->fg = fg;
  2886. t->voff = voff;
  2887. t->ul = ul;
  2888. return (Item*)t;
  2889. }
  2890. static Item*
  2891. newirule(int align, int size, int noshade, Dimen wspec)
  2892. {
  2893. Irule* r;
  2894. r = (Irule*)emalloc(sizeof(Irule));
  2895. r->tag = Iruletag;
  2896. r->align = align;
  2897. r->size = size;
  2898. r->noshade = noshade;
  2899. r->wspec = wspec;
  2900. return (Item*)r;
  2901. }
  2902. // Map is owned elsewhere.
  2903. static Item*
  2904. newiimage(Rune* src, Rune* altrep, int align, int width, int height,
  2905. int hspace, int vspace, int border, int ismap, Map* map)
  2906. {
  2907. Iimage* i;
  2908. int state;
  2909. state = 0;
  2910. if(ismap)
  2911. state = IFsmap;
  2912. i = (Iimage*)emalloc(sizeof(Iimage));
  2913. i->tag = Iimagetag;
  2914. i->state = state;
  2915. i->imsrc = src;
  2916. i->altrep = altrep;
  2917. i->align = align;
  2918. i->imwidth = width;
  2919. i->imheight = height;
  2920. i->hspace = hspace;
  2921. i->vspace = vspace;
  2922. i->border = border;
  2923. i->map = map;
  2924. i->ctlid = -1;
  2925. return (Item*)i;
  2926. }
  2927. static Item*
  2928. newiformfield(Formfield* ff)
  2929. {
  2930. Iformfield* f;
  2931. f = (Iformfield*)emalloc(sizeof(Iformfield));
  2932. f->tag = Iformfieldtag;
  2933. f->formfield = ff;
  2934. return (Item*)f;
  2935. }
  2936. static Item*
  2937. newitable(Table* tab)
  2938. {
  2939. Itable* t;
  2940. t = (Itable*)emalloc(sizeof(Itable));
  2941. t->tag = Itabletag;
  2942. t->table = tab;
  2943. return (Item*)t;
  2944. }
  2945. static Item*
  2946. newifloat(Item* it, int side)
  2947. {
  2948. Ifloat* f;
  2949. f = (Ifloat*)emalloc(sizeof(Ifloat));
  2950. f->tag = Ifloattag;
  2951. f->state = IFwrap;
  2952. f->item = it;
  2953. f->side = side;
  2954. return (Item*)f;
  2955. }
  2956. static Item*
  2957. newispacer(int spkind)
  2958. {
  2959. Ispacer* s;
  2960. s = (Ispacer*)emalloc(sizeof(Ispacer));
  2961. s->tag = Ispacertag;
  2962. s->spkind = spkind;
  2963. return (Item*)s;
  2964. }
  2965. // Free one item (caller must deal with next pointer)
  2966. static void
  2967. freeitem(Item* it)
  2968. {
  2969. Iimage* ii;
  2970. Genattr* ga;
  2971. if(it == nil)
  2972. return;
  2973. switch(it->tag) {
  2974. case Itexttag:
  2975. free(((Itext*)it)->s);
  2976. break;
  2977. case Iimagetag:
  2978. ii = (Iimage*)it;
  2979. free(ii->imsrc);
  2980. free(ii->altrep);
  2981. break;
  2982. case Iformfieldtag:
  2983. freeformfield(((Iformfield*)it)->formfield);
  2984. break;
  2985. case Itabletag:
  2986. freetable(((Itable*)it)->table);
  2987. break;
  2988. case Ifloattag:
  2989. freeitem(((Ifloat*)it)->item);
  2990. break;
  2991. }
  2992. ga = it->genattr;
  2993. if(ga != nil) {
  2994. free(ga->id);
  2995. free(ga->class);
  2996. free(ga->style);
  2997. free(ga->title);
  2998. freescriptevents(ga->events);
  2999. }
  3000. free(it);
  3001. }
  3002. // Free list of items chained through next pointer
  3003. void
  3004. freeitems(Item* ithead)
  3005. {
  3006. Item* it;
  3007. Item* itnext;
  3008. it = ithead;
  3009. while(it != nil) {
  3010. itnext = it->next;
  3011. freeitem(it);
  3012. it = itnext;
  3013. }
  3014. }
  3015. static void
  3016. freeformfield(Formfield* ff)
  3017. {
  3018. Option* o;
  3019. Option* onext;
  3020. if(ff == nil)
  3021. return;
  3022. free(ff->name);
  3023. free(ff->value);
  3024. for(o = ff->options; o != nil; o = onext) {
  3025. onext = o->next;
  3026. free(o->value);
  3027. free(o->display);
  3028. }
  3029. free(ff);
  3030. }
  3031. static void
  3032. freetable(Table* t)
  3033. {
  3034. int i;
  3035. Tablecell* c;
  3036. Tablecell* cnext;
  3037. if(t == nil)
  3038. return;
  3039. // We'll find all the unique cells via t->cells and next pointers.
  3040. // (Other pointers to cells in the table are duplicates of these)
  3041. for(c = t->cells; c != nil; c = cnext) {
  3042. cnext = c->next;
  3043. freeitems(c->content);
  3044. }
  3045. if(t->grid != nil) {
  3046. for(i = 0; i < t->nrow; i++)
  3047. free(t->grid[i]);
  3048. free(t->grid);
  3049. }
  3050. free(t->rows);
  3051. free(t->cols);
  3052. freeitems(t->caption);
  3053. free(t);
  3054. }
  3055. static void
  3056. freeform(Form* f)
  3057. {
  3058. if(f == nil)
  3059. return;
  3060. free(f->name);
  3061. free(f->action);
  3062. // Form doesn't own its fields (Iformfield items do)
  3063. free(f);
  3064. }
  3065. static void
  3066. freeforms(Form* fhead)
  3067. {
  3068. Form* f;
  3069. Form* fnext;
  3070. for(f = fhead; f != nil; f = fnext) {
  3071. fnext = f->next;
  3072. freeform(f);
  3073. }
  3074. }
  3075. static void
  3076. freeanchor(Anchor* a)
  3077. {
  3078. if(a == nil)
  3079. return;
  3080. free(a->name);
  3081. free(a->href);
  3082. free(a);
  3083. }
  3084. static void
  3085. freeanchors(Anchor* ahead)
  3086. {
  3087. Anchor* a;
  3088. Anchor* anext;
  3089. for(a = ahead; a != nil; a = anext) {
  3090. anext = a->next;
  3091. freeanchor(a);
  3092. }
  3093. }
  3094. static void
  3095. freedestanchor(DestAnchor* da)
  3096. {
  3097. if(da == nil)
  3098. return;
  3099. free(da->name);
  3100. free(da);
  3101. }
  3102. static void
  3103. freedestanchors(DestAnchor* dahead)
  3104. {
  3105. DestAnchor* da;
  3106. DestAnchor* danext;
  3107. for(da = dahead; da != nil; da = danext) {
  3108. danext = da->next;
  3109. freedestanchor(da);
  3110. }
  3111. }
  3112. static void
  3113. freearea(Area* a)
  3114. {
  3115. if(a == nil)
  3116. return;
  3117. free(a->href);
  3118. free(a->coords);
  3119. }
  3120. static void freekidinfos(Kidinfo* khead);
  3121. static void
  3122. freekidinfo(Kidinfo* k)
  3123. {
  3124. if(k->isframeset) {
  3125. free(k->rows);
  3126. free(k->cols);
  3127. freekidinfos(k->kidinfos);
  3128. }
  3129. else {
  3130. free(k->src);
  3131. free(k->name);
  3132. }
  3133. free(k);
  3134. }
  3135. static void
  3136. freekidinfos(Kidinfo* khead)
  3137. {
  3138. Kidinfo* k;
  3139. Kidinfo* knext;
  3140. for(k = khead; k != nil; k = knext) {
  3141. knext = k->next;
  3142. freekidinfo(k);
  3143. }
  3144. }
  3145. static void
  3146. freemap(Map* m)
  3147. {
  3148. Area* a;
  3149. Area* anext;
  3150. if(m == nil)
  3151. return;
  3152. free(m->name);
  3153. for(a = m->areas; a != nil; a = anext) {
  3154. anext = a->next;
  3155. freearea(a);
  3156. }
  3157. free(m);
  3158. }
  3159. static void
  3160. freemaps(Map* mhead)
  3161. {
  3162. Map* m;
  3163. Map* mnext;
  3164. for(m = mhead; m != nil; m = mnext) {
  3165. mnext = m->next;
  3166. freemap(m);
  3167. }
  3168. }
  3169. void
  3170. freedocinfo(Docinfo* d)
  3171. {
  3172. if(d == nil)
  3173. return;
  3174. free(d->src);
  3175. free(d->base);
  3176. freeitem((Item*)d->backgrounditem);
  3177. free(d->refresh);
  3178. freekidinfos(d->kidinfo);
  3179. freeanchors(d->anchors);
  3180. freedestanchors(d->dests);
  3181. freeforms(d->forms);
  3182. freemaps(d->maps);
  3183. // tables, images, and formfields are freed when
  3184. // the items pointing at them are freed
  3185. free(d);
  3186. }
  3187. // Currently, someone else owns all the memory
  3188. // pointed to by things in a Pstate.
  3189. static void
  3190. freepstate(Pstate* p)
  3191. {
  3192. free(p);
  3193. }
  3194. static void
  3195. freepstatestack(Pstate* pshead)
  3196. {
  3197. Pstate* p;
  3198. Pstate* pnext;
  3199. for(p = pshead; p != nil; p = pnext) {
  3200. pnext = p->next;
  3201. free(p);
  3202. }
  3203. }
  3204. static int
  3205. Iconv(Fmt *f)
  3206. {
  3207. Item* it;
  3208. Itext* t;
  3209. Irule* r;
  3210. Iimage* i;
  3211. Ifloat* fl;
  3212. int state;
  3213. Formfield* ff;
  3214. Rune* ty;
  3215. Tablecell* c;
  3216. Table* tab;
  3217. char* p;
  3218. int cl;
  3219. int hang;
  3220. int indent;
  3221. int bi;
  3222. int nbuf;
  3223. char buf[BIGBUFSIZE];
  3224. it = va_arg(f->args, Item*);
  3225. bi = 0;
  3226. nbuf = sizeof(buf);
  3227. state = it->state;
  3228. nbuf = nbuf-1;
  3229. if(state&IFbrk) {
  3230. cl = state&(IFcleft|IFcright);
  3231. p = "";
  3232. if(cl) {
  3233. if(cl == (IFcleft|IFcright))
  3234. p = " both";
  3235. else if(cl == IFcleft)
  3236. p = " left";
  3237. else
  3238. p = " right";
  3239. }
  3240. bi = snprint(buf, nbuf, "brk(%d%s)", (state&IFbrksp)? 1 : 0, p);
  3241. }
  3242. if(state&IFnobrk)
  3243. bi += snprint(buf+bi, nbuf-bi, " nobrk");
  3244. if(!(state&IFwrap))
  3245. bi += snprint(buf+bi, nbuf-bi, " nowrap");
  3246. if(state&IFrjust)
  3247. bi += snprint(buf+bi, nbuf-bi, " rjust");
  3248. if(state&IFcjust)
  3249. bi += snprint(buf+bi, nbuf-bi, " cjust");
  3250. if(state&IFsmap)
  3251. bi += snprint(buf+bi, nbuf-bi, " smap");
  3252. indent = (state&IFindentmask) >> IFindentshift;
  3253. if(indent > 0)
  3254. bi += snprint(buf+bi, nbuf-bi, " indent=%d", indent);
  3255. hang = state&IFhangmask;
  3256. if(hang > 0)
  3257. bi += snprint(buf+bi, nbuf-bi, " hang=%d", hang);
  3258. switch(it->tag) {
  3259. case Itexttag:
  3260. t = (Itext*)it;
  3261. bi += snprint(buf+bi, nbuf-bi, " Text '%S', fnt=%d, fg=%x", t->s, t->fnt, t->fg);
  3262. break;
  3263. case Iruletag:
  3264. r = (Irule*)it;
  3265. bi += snprint(buf+bi, nbuf-bi, "Rule size=%d, al=%S, wspec=", r->size, stringalign(r->align));
  3266. bi += dimprint(buf+bi, nbuf-bi, r->wspec);
  3267. break;
  3268. case Iimagetag:
  3269. i = (Iimage*)it;
  3270. bi += snprint(buf+bi, nbuf-bi,
  3271. "Image src=%S, alt=%S, al=%S, w=%d, h=%d hsp=%d, vsp=%d, bd=%d, map=%S",
  3272. i->imsrc, i->altrep? i->altrep : L"", stringalign(i->align), i->imwidth, i->imheight,
  3273. i->hspace, i->vspace, i->border, i->map? i->map->name : L"");
  3274. break;
  3275. case Iformfieldtag:
  3276. ff = ((Iformfield*)it)->formfield;
  3277. if(ff->ftype == Ftextarea)
  3278. ty = L"textarea";
  3279. else if(ff->ftype == Fselect)
  3280. ty = L"select";
  3281. else {
  3282. ty = _revlookup(input_tab, NINPUTTAB, ff->ftype);
  3283. if(ty == nil)
  3284. ty = L"none";
  3285. }
  3286. bi += snprint(buf+bi, nbuf-bi, "Formfield %S, fieldid=%d, formid=%d, name=%S, value=%S",
  3287. ty, ff->fieldid, ff->form->formid, ff->name? ff->name : L"",
  3288. ff->value? ff->value : L"");
  3289. break;
  3290. case Itabletag:
  3291. tab = ((Itable*)it)->table;
  3292. bi += snprint(buf+bi, nbuf-bi, "Table tableid=%d, width=", tab->tableid);
  3293. bi += dimprint(buf+bi, nbuf-bi, tab->width);
  3294. bi += snprint(buf+bi, nbuf-bi, ", nrow=%d, ncol=%d, ncell=%d, totw=%d, toth=%d\n",
  3295. tab->nrow, tab->ncol, tab->ncell, tab->totw, tab->toth);
  3296. for(c = tab->cells; c != nil; c = c->next)
  3297. bi += snprint(buf+bi, nbuf-bi, "Cell %d.%d, at (%d,%d) ",
  3298. tab->tableid, c->cellid, c->row, c->col);
  3299. bi += snprint(buf+bi, nbuf-bi, "End of Table %d", tab->tableid);
  3300. break;
  3301. case Ifloattag:
  3302. fl = (Ifloat*)it;
  3303. bi += snprint(buf+bi, nbuf-bi, "Float, x=%d y=%d, side=%S, it=%I",
  3304. fl->x, fl->y, stringalign(fl->side), fl->item);
  3305. bi += snprint(buf+bi, nbuf-bi, "\n\t");
  3306. break;
  3307. case Ispacertag:
  3308. p = "";
  3309. switch(((Ispacer*)it)->spkind) {
  3310. case ISPnull:
  3311. p = "null";
  3312. break;
  3313. case ISPvline:
  3314. p = "vline";
  3315. break;
  3316. case ISPhspace:
  3317. p = "hspace";
  3318. break;
  3319. }
  3320. bi += snprint(buf+bi, nbuf-bi, "Spacer %s ", p);
  3321. break;
  3322. }
  3323. bi += snprint(buf+bi, nbuf-bi, " w=%d, h=%d, a=%d, anchor=%d\n",
  3324. it->width, it->height, it->ascent, it->anchorid);
  3325. buf[bi] = 0;
  3326. return fmtstrcpy(f, buf);
  3327. }
  3328. // String version of alignment 'a'
  3329. static Rune*
  3330. stringalign(int a)
  3331. {
  3332. Rune* s;
  3333. s = _revlookup(align_tab, NALIGNTAB, a);
  3334. if(s == nil)
  3335. s = L"none";
  3336. return s;
  3337. }
  3338. // Put at most nbuf chars of representation of d into buf,
  3339. // and return number of characters put
  3340. static int
  3341. dimprint(char* buf, int nbuf, Dimen d)
  3342. {
  3343. int n;
  3344. int k;
  3345. n = 0;
  3346. n += snprint(buf, nbuf, "%d", dimenspec(d));
  3347. k = dimenkind(d);
  3348. if(k == Dpercent)
  3349. buf[n++] = '%';
  3350. if(k == Drelative)
  3351. buf[n++] = '*';
  3352. return n;
  3353. }
  3354. void
  3355. printitems(Item* items, char* msg)
  3356. {
  3357. Item* il;
  3358. fprint(2, "%s\n", msg);
  3359. il = items;
  3360. while(il != nil) {
  3361. fprint(2, "%I", il);
  3362. il = il->next;
  3363. }
  3364. }
  3365. static Genattr*
  3366. newgenattr(Rune* id, Rune* class, Rune* style, Rune* title, SEvent* events)
  3367. {
  3368. Genattr* g;
  3369. g = (Genattr*)emalloc(sizeof(Genattr));
  3370. g->id = id;
  3371. g->class = class;
  3372. g->style = style;
  3373. g->title = title;
  3374. g->events = events;
  3375. return g;
  3376. }
  3377. static Formfield*
  3378. newformfield(int ftype, int fieldid, Form* form, Rune* name,
  3379. Rune* value, int size, int maxlength, Formfield* link)
  3380. {
  3381. Formfield* ff;
  3382. ff = (Formfield*)emalloc(sizeof(Formfield));
  3383. ff->ftype = ftype;
  3384. ff->fieldid = fieldid;
  3385. ff->form = form;
  3386. ff->name = name;
  3387. ff->value = value;
  3388. ff->size = size;
  3389. ff->maxlength = maxlength;
  3390. ff->ctlid = -1;
  3391. ff->next = link;
  3392. return ff;
  3393. }
  3394. // Transfers ownership of value and display to Option.
  3395. static Option*
  3396. newoption(int selected, Rune* value, Rune* display, Option* link)
  3397. {
  3398. Option *o;
  3399. o = (Option*)emalloc(sizeof(Option));
  3400. o->selected = selected;
  3401. o->value = value;
  3402. o->display = display;
  3403. o->next = link;
  3404. return o;
  3405. }
  3406. static Form*
  3407. newform(int formid, Rune* name, Rune* action, int target, int method, Form* link)
  3408. {
  3409. Form* f;
  3410. f = (Form*)emalloc(sizeof(Form));
  3411. f->formid = formid;
  3412. f->name = name;
  3413. f->action = action;
  3414. f->target = target;
  3415. f->method = method;
  3416. f->nfields = 0;
  3417. f->fields = nil;
  3418. f->next = link;
  3419. return f;
  3420. }
  3421. static Table*
  3422. newtable(int tableid, Align align, Dimen width, int border,
  3423. int cellspacing, int cellpadding, Background bg, Token* tok, Table* link)
  3424. {
  3425. Table* t;
  3426. t = (Table*)emalloc(sizeof(Table));
  3427. t->tableid = tableid;
  3428. t->align = align;
  3429. t->width = width;
  3430. t->border = border;
  3431. t->cellspacing = cellspacing;
  3432. t->cellpadding = cellpadding;
  3433. t->background = bg;
  3434. t->caption_place = ALbottom;
  3435. t->caption_lay = nil;
  3436. t->tabletok = tok;
  3437. t->tabletok = nil;
  3438. t->next = link;
  3439. return t;
  3440. }
  3441. static Tablerow*
  3442. newtablerow(Align align, Background bg, int flags, Tablerow* link)
  3443. {
  3444. Tablerow* tr;
  3445. tr = (Tablerow*)emalloc(sizeof(Tablerow));
  3446. tr->align = align;
  3447. tr->background = bg;
  3448. tr->flags = flags;
  3449. tr->next = link;
  3450. return tr;
  3451. }
  3452. static Tablecell*
  3453. newtablecell(int cellid, int rowspan, int colspan, Align align, Dimen wspec, int hspec,
  3454. Background bg, int flags, Tablecell* link)
  3455. {
  3456. Tablecell* c;
  3457. c = (Tablecell*)emalloc(sizeof(Tablecell));
  3458. c->cellid = cellid;
  3459. c->lay = nil;
  3460. c->rowspan = rowspan;
  3461. c->colspan = colspan;
  3462. c->align = align;
  3463. c->flags = flags;
  3464. c->wspec = wspec;
  3465. c->hspec = hspec;
  3466. c->background = bg;
  3467. c->next = link;
  3468. return c;
  3469. }
  3470. static Anchor*
  3471. newanchor(int index, Rune* name, Rune* href, int target, Anchor* link)
  3472. {
  3473. Anchor* a;
  3474. a = (Anchor*)emalloc(sizeof(Anchor));
  3475. a->index = index;
  3476. a->name = name;
  3477. a->href = href;
  3478. a->target = target;
  3479. a->next = link;
  3480. return a;
  3481. }
  3482. static DestAnchor*
  3483. newdestanchor(int index, Rune* name, Item* item, DestAnchor* link)
  3484. {
  3485. DestAnchor* d;
  3486. d = (DestAnchor*)emalloc(sizeof(DestAnchor));
  3487. d->index = index;
  3488. d->name = name;
  3489. d->item = item;
  3490. d->next = link;
  3491. return d;
  3492. }
  3493. static SEvent*
  3494. newscriptevent(int type, Rune* script, SEvent* link)
  3495. {
  3496. SEvent* ans;
  3497. ans = (SEvent*)emalloc(sizeof(SEvent));
  3498. ans->type = type;
  3499. ans->script = script;
  3500. ans->next = link;
  3501. return ans;
  3502. }
  3503. static void
  3504. freescriptevents(SEvent* ehead)
  3505. {
  3506. SEvent* e;
  3507. SEvent* nexte;
  3508. e = ehead;
  3509. while(e != nil) {
  3510. nexte = e->next;
  3511. free(e->script);
  3512. free(e);
  3513. e = nexte;
  3514. }
  3515. }
  3516. static Dimen
  3517. makedimen(int kind, int spec)
  3518. {
  3519. Dimen d;
  3520. if(spec&Dkindmask) {
  3521. if(warn)
  3522. fprint(2, "warning: dimension spec too big: %d\n", spec);
  3523. spec = 0;
  3524. }
  3525. d.kindspec = kind|spec;
  3526. return d;
  3527. }
  3528. int
  3529. dimenkind(Dimen d)
  3530. {
  3531. return (d.kindspec&Dkindmask);
  3532. }
  3533. int
  3534. dimenspec(Dimen d)
  3535. {
  3536. return (d.kindspec&Dspecmask);
  3537. }
  3538. static Kidinfo*
  3539. newkidinfo(int isframeset, Kidinfo* link)
  3540. {
  3541. Kidinfo* ki;
  3542. ki = (Kidinfo*)emalloc(sizeof(Kidinfo));
  3543. ki->isframeset = isframeset;
  3544. if(!isframeset) {
  3545. ki->flags = FRhscrollauto|FRvscrollauto;
  3546. ki->marginw = FRKIDMARGIN;
  3547. ki->marginh = FRKIDMARGIN;
  3548. ki->framebd = 1;
  3549. }
  3550. ki->next = link;
  3551. return ki;
  3552. }
  3553. static Docinfo*
  3554. newdocinfo(void)
  3555. {
  3556. Docinfo* d;
  3557. d = (Docinfo*)emalloc(sizeof(Docinfo));
  3558. resetdocinfo(d);
  3559. return d;
  3560. }
  3561. static void
  3562. resetdocinfo(Docinfo* d)
  3563. {
  3564. memset(d, 0, sizeof(Docinfo));
  3565. d->background = makebackground(nil, White);
  3566. d->text = Black;
  3567. d->link = Blue;
  3568. d->vlink = Blue;
  3569. d->alink = Blue;
  3570. d->target = FTself;
  3571. d->chset = ISO_8859_1;
  3572. d->scripttype = TextJavascript;
  3573. d->frameid = -1;
  3574. }
  3575. // Use targetmap array to keep track of name <-> targetid mapping.
  3576. // Use real malloc(), and never free
  3577. static void
  3578. targetmapinit(void)
  3579. {
  3580. targetmapsize = 10;
  3581. targetmap = (StringInt*)emalloc(targetmapsize*sizeof(StringInt));
  3582. memset(targetmap, 0, targetmapsize*sizeof(StringInt));
  3583. targetmap[0].key = _Strdup(L"_top");
  3584. targetmap[0].val = FTtop;
  3585. targetmap[1].key = _Strdup(L"_self");
  3586. targetmap[1].val = FTself;
  3587. targetmap[2].key = _Strdup(L"_parent");
  3588. targetmap[2].val = FTparent;
  3589. targetmap[3].key = _Strdup(L"_blank");
  3590. targetmap[3].val = FTblank;
  3591. ntargets = 4;
  3592. }
  3593. int
  3594. targetid(Rune* s)
  3595. {
  3596. int i;
  3597. int n;
  3598. n = _Strlen(s);
  3599. if(n == 0)
  3600. return FTself;
  3601. for(i = 0; i < ntargets; i++)
  3602. if(_Strcmp(s, targetmap[i].key) == 0)
  3603. return targetmap[i].val;
  3604. if(i >= targetmapsize) {
  3605. targetmapsize += 10;
  3606. targetmap = (StringInt*)erealloc(targetmap, targetmapsize*sizeof(StringInt));
  3607. }
  3608. targetmap[i].key = (Rune*)emalloc((n+1)*sizeof(Rune));
  3609. memmove(targetmap[i].key, s, (n+1)*sizeof(Rune));
  3610. targetmap[i].val = i;
  3611. ntargets++;
  3612. return i;
  3613. }
  3614. Rune*
  3615. targetname(int targid)
  3616. {
  3617. int i;
  3618. for(i = 0; i < ntargets; i++)
  3619. if(targetmap[i].val == targid)
  3620. return targetmap[i].key;
  3621. return L"?";
  3622. }
  3623. // Convert HTML color spec to RGB value, returning dflt if can't.
  3624. // Argument is supposed to be a valid HTML color, or "".
  3625. // Return the RGB value of the color, using dflt if s
  3626. // is nil or an invalid color.
  3627. static int
  3628. color(Rune* s, int dflt)
  3629. {
  3630. int v;
  3631. Rune* rest;
  3632. if(s == nil)
  3633. return dflt;
  3634. if(_lookup(color_tab, NCOLORS, s, _Strlen(s), &v))
  3635. return v;
  3636. if(s[0] == '#')
  3637. s++;
  3638. v = _Strtol(s, &rest, 16);
  3639. if(*rest == 0)
  3640. return v;
  3641. return dflt;
  3642. }
  3643. // Debugging
  3644. #define HUGEPIX 10000
  3645. // A "shallow" validitem, that doesn't follow next links
  3646. // or descend into tables.
  3647. static int
  3648. validitem(Item* i)
  3649. {
  3650. int ok;
  3651. Itext* ti;
  3652. Irule* ri;
  3653. Iimage* ii;
  3654. Ifloat* fi;
  3655. int a;
  3656. ok = (i->tag >= Itexttag && i->tag <= Ispacertag) &&
  3657. (i->next == nil || validptr(i->next)) &&
  3658. (i->width >= 0 && i->width < HUGEPIX) &&
  3659. (i->height >= 0 && i->height < HUGEPIX) &&
  3660. (i->ascent > -HUGEPIX && i->ascent < HUGEPIX) &&
  3661. (i->anchorid >= 0) &&
  3662. (i->genattr == nil || validptr(i->genattr));
  3663. // also, could check state for ridiculous combinations
  3664. // also, could check anchorid for within-doc-range
  3665. if(ok)
  3666. switch(i->tag) {
  3667. case Itexttag:
  3668. ti = (Itext*)i;
  3669. ok = validStr(ti->s) &&
  3670. (ti->fnt >= 0 && ti->fnt < NumStyle*NumSize) &&
  3671. (ti->ul == ULnone || ti->ul == ULunder || ti->ul == ULmid);
  3672. break;
  3673. case Iruletag:
  3674. ri = (Irule*)i;
  3675. ok = (validvalign(ri->align) || validhalign(ri->align)) &&
  3676. (ri->size >=0 && ri->size < HUGEPIX);
  3677. break;
  3678. case Iimagetag:
  3679. ii = (Iimage*)i;
  3680. ok = (ii->imsrc == nil || validptr(ii->imsrc)) &&
  3681. (ii->width >= 0 && ii->width < HUGEPIX) &&
  3682. (ii->height >= 0 && ii->height < HUGEPIX) &&
  3683. (ii->imwidth >= 0 && ii->imwidth < HUGEPIX) &&
  3684. (ii->imheight >= 0 && ii->imheight < HUGEPIX) &&
  3685. (ii->altrep == nil || validStr(ii->altrep)) &&
  3686. (ii->map == nil || validptr(ii->map)) &&
  3687. (validvalign(ii->align) || validhalign(ii->align)) &&
  3688. (ii->nextimage == nil || validptr(ii->nextimage));
  3689. break;
  3690. case Iformfieldtag:
  3691. ok = validformfield(((Iformfield*)i)->formfield);
  3692. break;
  3693. case Itabletag:
  3694. ok = validptr((Itable*)i);
  3695. break;
  3696. case Ifloattag:
  3697. fi = (Ifloat*)i;
  3698. ok = (fi->side == ALleft || fi->side == ALright) &&
  3699. validitem(fi->item) &&
  3700. (fi->item->tag == Iimagetag || fi->item->tag == Itabletag);
  3701. break;
  3702. case Ispacertag:
  3703. a = ((Ispacer*)i)->spkind;
  3704. ok = a==ISPnull || a==ISPvline || a==ISPhspace || a==ISPgeneral;
  3705. break;
  3706. default:
  3707. ok = 0;
  3708. }
  3709. return ok;
  3710. }
  3711. // "deep" validation, that checks whole list of items,
  3712. // and descends into tables and floated tables.
  3713. // nil is ok for argument.
  3714. int
  3715. validitems(Item* i)
  3716. {
  3717. int ok;
  3718. Item* ii;
  3719. ok = 1;
  3720. while(i != nil && ok) {
  3721. ok = validitem(i);
  3722. if(ok) {
  3723. if(i->tag == Itabletag) {
  3724. ok = validtable(((Itable*)i)->table);
  3725. }
  3726. else if(i->tag == Ifloattag) {
  3727. ii = ((Ifloat*)i)->item;
  3728. if(ii->tag == Itabletag)
  3729. ok = validtable(((Itable*)ii)->table);
  3730. }
  3731. }
  3732. if(!ok) {
  3733. fprint(2, "invalid item: %I\n", i);
  3734. }
  3735. i = i->next;
  3736. }
  3737. return ok;
  3738. }
  3739. static int
  3740. validformfield(Formfield* f)
  3741. {
  3742. int ok;
  3743. ok = (f->next == nil || validptr(f->next)) &&
  3744. (f->ftype >= 0 && f->ftype <= Ftextarea) &&
  3745. f->fieldid >= 0 &&
  3746. (f->form == nil || validptr(f->form)) &&
  3747. (f->name == nil || validStr(f->name)) &&
  3748. (f->value == nil || validStr(f->value)) &&
  3749. (f->options == nil || validptr(f->options)) &&
  3750. (f->image == nil || validitem(f->image)) &&
  3751. (f->events == nil || validptr(f->events));
  3752. // when all built, should have f->fieldid < f->form->nfields,
  3753. // but this may be called during build...
  3754. return ok;
  3755. }
  3756. // "deep" validation -- checks cell contents too
  3757. static int
  3758. validtable(Table* t)
  3759. {
  3760. int ok;
  3761. int i, j;
  3762. Tablecell* c;
  3763. ok = (t->next == nil || validptr(t->next)) &&
  3764. t->nrow >= 0 &&
  3765. t->ncol >= 0 &&
  3766. t->ncell >= 0 &&
  3767. validalign(t->align) &&
  3768. validdimen(t->width) &&
  3769. (t->border >= 0 && t->border < HUGEPIX) &&
  3770. (t->cellspacing >= 0 && t->cellspacing < HUGEPIX) &&
  3771. (t->cellpadding >= 0 && t->cellpadding < HUGEPIX) &&
  3772. validitems(t->caption) &&
  3773. (t->caption_place == ALtop || t->caption_place == ALbottom) &&
  3774. (t->totw >= 0 && t->totw < HUGEPIX) &&
  3775. (t->toth >= 0 && t->toth < HUGEPIX) &&
  3776. (t->tabletok == nil || validptr(t->tabletok));
  3777. // during parsing, t->rows has list;
  3778. // only when parsing is done is t->nrow set > 0
  3779. if(ok && t->nrow > 0 && t->ncol > 0) {
  3780. // table is "finished"
  3781. for(i = 0; i < t->nrow && ok; i++)
  3782. ok = validtablerow(t->rows+i);
  3783. for(j = 0; j < t->ncol && ok; j++)
  3784. ok = validtablecol(t->cols+j);
  3785. for(c = t->cells; c != nil && ok; c = c->next)
  3786. ok = validtablecell(c);
  3787. for(i = 0; i < t->nrow && ok; i++)
  3788. for(j = 0; j < t->ncol && ok; j++)
  3789. ok = validptr(t->grid[i][j]);
  3790. }
  3791. return ok;
  3792. }
  3793. static int
  3794. validvalign(int a)
  3795. {
  3796. return a == ALnone || a == ALmiddle || a == ALbottom || a == ALtop || a == ALbaseline;
  3797. }
  3798. static int
  3799. validhalign(int a)
  3800. {
  3801. return a == ALnone || a == ALleft || a == ALcenter || a == ALright ||
  3802. a == ALjustify || a == ALchar;
  3803. }
  3804. static int
  3805. validalign(Align a)
  3806. {
  3807. return validhalign(a.halign) && validvalign(a.valign);
  3808. }
  3809. static int
  3810. validdimen(Dimen d)
  3811. {
  3812. int ok;
  3813. int s;
  3814. ok = 0;
  3815. s = d.kindspec&Dspecmask;
  3816. switch(d.kindspec&Dkindmask) {
  3817. case Dnone:
  3818. ok = s==0;
  3819. break;
  3820. case Dpixels:
  3821. ok = s < HUGEPIX;
  3822. break;
  3823. case Dpercent:
  3824. case Drelative:
  3825. ok = 1;
  3826. break;
  3827. }
  3828. return ok;
  3829. }
  3830. static int
  3831. validtablerow(Tablerow* r)
  3832. {
  3833. return (r->cells == nil || validptr(r->cells)) &&
  3834. (r->height >= 0 && r->height < HUGEPIX) &&
  3835. (r->ascent > -HUGEPIX && r->ascent < HUGEPIX) &&
  3836. validalign(r->align);
  3837. }
  3838. static int
  3839. validtablecol(Tablecol* c)
  3840. {
  3841. return c->width >= 0 && c->width < HUGEPIX
  3842. && validalign(c->align);
  3843. }
  3844. static int
  3845. validtablecell(Tablecell* c)
  3846. {
  3847. int ok;
  3848. ok = (c->next == nil || validptr(c->next)) &&
  3849. (c->nextinrow == nil || validptr(c->nextinrow)) &&
  3850. (c->content == nil || validptr(c->content)) &&
  3851. (c->lay == nil || validptr(c->lay)) &&
  3852. c->rowspan >= 0 &&
  3853. c->colspan >= 0 &&
  3854. validalign(c->align) &&
  3855. validdimen(c->wspec) &&
  3856. c->row >= 0 &&
  3857. c->col >= 0;
  3858. if(ok) {
  3859. if(c->content != nil)
  3860. ok = validitems(c->content);
  3861. }
  3862. return ok;
  3863. }
  3864. static int
  3865. validptr(void* p)
  3866. {
  3867. // TODO: a better job of this.
  3868. // For now, just dereference, which cause a bomb
  3869. // if not valid
  3870. static char c;
  3871. c = *((char*)p);
  3872. return 1;
  3873. }
  3874. static int
  3875. validStr(Rune* s)
  3876. {
  3877. return s != nil && validptr(s);
  3878. }