build.c 93 KB

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