build.c 93 KB

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