Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <ctype.h>
5 #include <html.h>
6 #include "impl.h"
8 /* A stack for holding integer values */
9 enum {
10 Nestmax = 40 /* max nesting level of lists, font styles, etc. */
11 };
13 struct Stack {
14 int n; /* next available slot (top of stack is stack[n-1]) */
15 int slots[Nestmax]; /* stack entries */
16 };
18 /* Parsing state */
19 struct Pstate
20 {
21 Pstate* next; /* in stack of Pstates */
22 int skipping; /* true when we shouldn't add items */
23 int skipwhite; /* true when we should strip leading space */
24 int curfont; /* font index for current font */
25 int curfg; /* current foreground color */
26 Background curbg; /* current background */
27 int curvoff; /* current baseline offset */
28 uchar curul; /* current underline/strike state */
29 uchar curjust; /* current justify state */
30 int curanchor; /* current (href) anchor id (if in one), or 0 */
31 int curstate; /* current value of item state */
32 int literal; /* current literal state */
33 int inpar; /* true when in a paragraph-like construct */
34 int adjsize; /* current font size adjustment */
35 Item* items; /* dummy head of item list we're building */
36 Item* lastit; /* tail of item list we're building */
37 Item* prelastit; /* item before lastit */
38 Stack fntstylestk; /* style stack */
39 Stack fntsizestk; /* size stack */
40 Stack fgstk; /* text color stack */
41 Stack ulstk; /* underline stack */
42 Stack voffstk; /* vertical offset stack */
43 Stack listtypestk; /* list type stack */
44 Stack listcntstk; /* list counter stack */
45 Stack juststk; /* justification stack */
46 Stack hangstk; /* hanging stack */
47 };
49 struct ItemSource
50 {
51 Docinfo* doc;
52 Pstate* psstk;
53 int nforms;
54 int ntables;
55 int nanchors;
56 int nframes;
57 Form* curform;
58 Map* curmap;
59 Table* tabstk;
60 Kidinfo* kidstk;
61 };
63 /* Some layout parameters */
64 enum {
65 FRKIDMARGIN = 6, /* default margin around kid frames */
66 IMGHSPACE = 0, /* default hspace for images (0 matches IE, Netscape) */
67 IMGVSPACE = 0, /* default vspace for images */
68 FLTIMGHSPACE = 2, /* default hspace for float images */
69 TABSP = 5, /* default cellspacing for tables */
70 TABPAD = 1, /* default cell padding for tables */
71 LISTTAB = 1, /* number of tabs to indent lists */
72 BQTAB = 1, /* number of tabs to indent blockquotes */
73 HRSZ = 2, /* thickness of horizontal rules */
74 SUBOFF = 4, /* vertical offset for subscripts */
75 SUPOFF = 6, /* vertical offset for superscripts */
76 NBSP = 160 /* non-breaking space character */
77 };
79 /* These tables must be sorted */
80 static StringInt *align_tab;
81 static AsciiInt _align_tab[] = {
82 {"baseline", ALbaseline},
83 {"bottom", ALbottom},
84 {"center", ALcenter},
85 {"char", ALchar},
86 {"justify", ALjustify},
87 {"left", ALleft},
88 {"middle", ALmiddle},
89 {"right", ALright},
90 {"top", ALtop}
91 };
92 #define NALIGNTAB (sizeof(_align_tab)/sizeof(StringInt))
94 static StringInt *input_tab;
95 static AsciiInt _input_tab[] = {
96 {"button", Fbutton},
97 {"checkbox", Fcheckbox},
98 {"file", Ffile},
99 {"hidden", Fhidden},
100 {"image", Fimage},
101 {"password", Fpassword},
102 {"radio", Fradio},
103 {"reset", Freset},
104 {"submit", Fsubmit},
105 {"text", Ftext}
106 };
107 #define NINPUTTAB (sizeof(_input_tab)/sizeof(StringInt))
109 static StringInt *clear_tab;
110 static AsciiInt _clear_tab[] = {
111 {"all", IFcleft|IFcright},
112 {"left", IFcleft},
113 {"right", IFcright}
114 };
115 #define NCLEARTAB (sizeof(_clear_tab)/sizeof(StringInt))
117 static StringInt *fscroll_tab;
118 static AsciiInt _fscroll_tab[] = {
119 {"auto", FRhscrollauto|FRvscrollauto},
120 {"no", FRnoscroll},
121 {"yes", FRhscroll|FRvscroll},
122 };
123 #define NFSCROLLTAB (sizeof(_fscroll_tab)/sizeof(StringInt))
125 static StringInt *shape_tab;
126 static AsciiInt _shape_tab[] = {
127 {"circ", SHcircle},
128 {"circle", SHcircle},
129 {"poly", SHpoly},
130 {"polygon", SHpoly},
131 {"rect", SHrect},
132 {"rectangle", SHrect}
133 };
134 #define NSHAPETAB (sizeof(_shape_tab)/sizeof(StringInt))
136 static StringInt *method_tab;
137 static AsciiInt _method_tab[] = {
138 {"get", HGet},
139 {"post", HPost}
140 };
141 #define NMETHODTAB (sizeof(_method_tab)/sizeof(StringInt))
143 static Rune** roman;
144 static char* _roman[15]= {
145 "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X",
146 "XI", "XII", "XIII", "XIV", "XV"
147 };
148 #define NROMAN 15
150 /* List number types */
151 enum {
152 LTdisc, LTsquare, LTcircle, LT1, LTa, LTA, LTi, LTI
153 };
155 enum {
156 SPBefore = 2,
157 SPAfter = 4,
158 BL = 1,
159 BLBA = (BL|SPBefore|SPAfter)
160 };
162 /* blockbrk[tag] is break info for a block level element, or one */
163 /* of a few others that get the same treatment re ending open paragraphs */
164 /* and requiring a line break / vertical space before them. */
165 /* If we want a line of space before the given element, SPBefore is OR'd in. */
166 /* If we want a line of space after the given element, SPAfter is OR'd in. */
168 static uchar blockbrk[Numtags]= {
169 /*Notfound*/ 0,
170 /*Comment*/ 0,
171 /*Ta*/ 0,
172 /*Tabbr*/ 0,
173 /*Tacronym*/ 0,
174 /*Taddress*/ BLBA,
175 /*Tapplet*/ 0,
176 /*Tarea*/ 0,
177 /*Tb*/ 0,
178 /*Tbase*/ 0,
179 /*Tbasefont*/ 0,
180 /*Tbdo*/ 0,
181 /*Tbig*/ 0,
182 /*Tblink*/ 0,
183 /*Tblockquote*/ BLBA,
184 /*Tbody*/ 0,
185 /*Tbq*/ 0,
186 /*Tbr*/ 0,
187 /*Tbutton*/ 0,
188 /*Tcaption*/ 0,
189 /*Tcenter*/ BL,
190 /*Tcite*/ 0,
191 /*Tcode*/ 0,
192 /*Tcol*/ 0,
193 /*Tcolgroup*/ 0,
194 /*Tdd*/ BL,
195 /*Tdel*/ 0,
196 /*Tdfn*/ 0,
197 /*Tdir*/ BLBA,
198 /*Tdiv*/ BL,
199 /*Tdl*/ BLBA,
200 /*Tdt*/ BL,
201 /*Tem*/ 0,
202 /*Tfieldset*/ 0,
203 /*Tfont*/ 0,
204 /*Tform*/ BLBA,
205 /*Tframe*/ 0,
206 /*Tframeset*/ 0,
207 /*Th1*/ BL,
208 /*Th2*/ BL,
209 /*Th3*/ BL,
210 /*Th4*/ BL,
211 /*Th5*/ BL,
212 /*Th6*/ BL,
213 /*Thead*/ 0,
214 /*Thr*/ BL,
215 /*Thtml*/ 0,
216 /*Ti*/ 0,
217 /*Tiframe*/ 0,
218 /*Timg*/ 0,
219 /*Tinput*/ 0,
220 /*Tins*/ 0,
221 /*Tisindex*/ BLBA,
222 /*Tkbd*/ 0,
223 /*Tlabel*/ 0,
224 /*Tlegend*/ 0,
225 /*Tli*/ BL,
226 /*Tlink*/ 0,
227 /*Tmap*/ 0,
228 /*Tmenu*/ BLBA,
229 /*Tmeta*/ 0,
230 /*Tnobr*/ 0,
231 /*Tnoframes*/ 0,
232 /*Tnoscript*/ 0,
233 /*Tobject*/ 0,
234 /*Tol*/ BLBA,
235 /*Toptgroup*/ 0,
236 /*Toption*/ 0,
237 /*Tp*/ BLBA,
238 /*Tparam*/ 0,
239 /*Tpre*/ BLBA,
240 /*Tq*/ 0,
241 /*Ts*/ 0,
242 /*Tsamp*/ 0,
243 /*Tscript*/ 0,
244 /*Tselect*/ 0,
245 /*Tsmall*/ 0,
246 /*Tspan*/ 0,
247 /*Tstrike*/ 0,
248 /*Tstrong*/ 0,
249 /*Tstyle*/ 0,
250 /*Tsub*/ 0,
251 /*Tsup*/ 0,
252 /*Ttable*/ 0,
253 /*Ttbody*/ 0,
254 /*Ttd*/ 0,
255 /*Ttextarea*/ 0,
256 /*Ttfoot*/ 0,
257 /*Tth*/ 0,
258 /*Tthead*/ 0,
259 /*Ttitle*/ 0,
260 /*Ttr*/ 0,
261 /*Ttt*/ 0,
262 /*Tu*/ 0,
263 /*Tul*/ BLBA,
264 /*Tvar*/ 0,
265 };
267 enum {
268 AGEN = 1
269 };
271 /* attrinfo is information about attributes. */
272 /* The AGEN value means that the attribute is generic (applies to almost all elements) */
273 static uchar attrinfo[Numattrs]= {
274 /*Aabbr*/ 0,
275 /*Aaccept_charset*/ 0,
276 /*Aaccess_key*/ 0,
277 /*Aaction*/ 0,
278 /*Aalign*/ 0,
279 /*Aalink*/ 0,
280 /*Aalt*/ 0,
281 /*Aarchive*/ 0,
282 /*Aaxis*/ 0,
283 /*Abackground*/ 0,
284 /*Abgcolor*/ 0,
285 /*Aborder*/ 0,
286 /*Acellpadding*/ 0,
287 /*Acellspacing*/ 0,
288 /*Achar*/ 0,
289 /*Acharoff*/ 0,
290 /*Acharset*/ 0,
291 /*Achecked*/ 0,
292 /*Acite*/ 0,
293 /*Aclass*/ AGEN,
294 /*Aclassid*/ 0,
295 /*Aclear*/ 0,
296 /*Acode*/ 0,
297 /*Acodebase*/ 0,
298 /*Acodetype*/ 0,
299 /*Acolor*/ 0,
300 /*Acols*/ 0,
301 /*Acolspan*/ 0,
302 /*Acompact*/ 0,
303 /*Acontent*/ 0,
304 /*Acoords*/ 0,
305 /*Adata*/ 0,
306 /*Adatetime*/ 0,
307 /*Adeclare*/ 0,
308 /*Adefer*/ 0,
309 /*Adir*/ 0,
310 /*Adisabled*/ 0,
311 /*Aenctype*/ 0,
312 /*Aface*/ 0,
313 /*Afor*/ 0,
314 /*Aframe*/ 0,
315 /*Aframeborder*/ 0,
316 /*Aheaders*/ 0,
317 /*Aheight*/ 0,
318 /*Ahref*/ 0,
319 /*Ahreflang*/ 0,
320 /*Ahspace*/ 0,
321 /*Ahttp_equiv*/ 0,
322 /*Aid*/ AGEN,
323 /*Aismap*/ 0,
324 /*Alabel*/ 0,
325 /*Alang*/ 0,
326 /*Alink*/ 0,
327 /*Alongdesc*/ 0,
328 /*Amarginheight*/ 0,
329 /*Amarginwidth*/ 0,
330 /*Amaxlength*/ 0,
331 /*Amedia*/ 0,
332 /*Amethod*/ 0,
333 /*Amultiple*/ 0,
334 /*Aname*/ 0,
335 /*Anohref*/ 0,
336 /*Anoresize*/ 0,
337 /*Anoshade*/ 0,
338 /*Anowrap*/ 0,
339 /*Aobject*/ 0,
340 /*Aonblur*/ AGEN,
341 /*Aonchange*/ AGEN,
342 /*Aonclick*/ AGEN,
343 /*Aondblclick*/ AGEN,
344 /*Aonfocus*/ AGEN,
345 /*Aonkeypress*/ AGEN,
346 /*Aonkeyup*/ AGEN,
347 /*Aonload*/ AGEN,
348 /*Aonmousedown*/ AGEN,
349 /*Aonmousemove*/ AGEN,
350 /*Aonmouseout*/ AGEN,
351 /*Aonmouseover*/ AGEN,
352 /*Aonmouseup*/ AGEN,
353 /*Aonreset*/ AGEN,
354 /*Aonselect*/ AGEN,
355 /*Aonsubmit*/ AGEN,
356 /*Aonunload*/ AGEN,
357 /*Aprofile*/ 0,
358 /*Aprompt*/ 0,
359 /*Areadonly*/ 0,
360 /*Arel*/ 0,
361 /*Arev*/ 0,
362 /*Arows*/ 0,
363 /*Arowspan*/ 0,
364 /*Arules*/ 0,
365 /*Ascheme*/ 0,
366 /*Ascope*/ 0,
367 /*Ascrolling*/ 0,
368 /*Aselected*/ 0,
369 /*Ashape*/ 0,
370 /*Asize*/ 0,
371 /*Aspan*/ 0,
372 /*Asrc*/ 0,
373 /*Astandby*/ 0,
374 /*Astart*/ 0,
375 /*Astyle*/ AGEN,
376 /*Asummary*/ 0,
377 /*Atabindex*/ 0,
378 /*Atarget*/ 0,
379 /*Atext*/ 0,
380 /*Atitle*/ AGEN,
381 /*Atype*/ 0,
382 /*Ausemap*/ 0,
383 /*Avalign*/ 0,
384 /*Avalue*/ 0,
385 /*Avaluetype*/ 0,
386 /*Aversion*/ 0,
387 /*Avlink*/ 0,
388 /*Avspace*/ 0,
389 /*Awidth*/ 0,
390 };
392 static uchar scriptev[Numattrs]= {
393 /*Aabbr*/ 0,
394 /*Aaccept_charset*/ 0,
395 /*Aaccess_key*/ 0,
396 /*Aaction*/ 0,
397 /*Aalign*/ 0,
398 /*Aalink*/ 0,
399 /*Aalt*/ 0,
400 /*Aarchive*/ 0,
401 /*Aaxis*/ 0,
402 /*Abackground*/ 0,
403 /*Abgcolor*/ 0,
404 /*Aborder*/ 0,
405 /*Acellpadding*/ 0,
406 /*Acellspacing*/ 0,
407 /*Achar*/ 0,
408 /*Acharoff*/ 0,
409 /*Acharset*/ 0,
410 /*Achecked*/ 0,
411 /*Acite*/ 0,
412 /*Aclass*/ 0,
413 /*Aclassid*/ 0,
414 /*Aclear*/ 0,
415 /*Acode*/ 0,
416 /*Acodebase*/ 0,
417 /*Acodetype*/ 0,
418 /*Acolor*/ 0,
419 /*Acols*/ 0,
420 /*Acolspan*/ 0,
421 /*Acompact*/ 0,
422 /*Acontent*/ 0,
423 /*Acoords*/ 0,
424 /*Adata*/ 0,
425 /*Adatetime*/ 0,
426 /*Adeclare*/ 0,
427 /*Adefer*/ 0,
428 /*Adir*/ 0,
429 /*Adisabled*/ 0,
430 /*Aenctype*/ 0,
431 /*Aface*/ 0,
432 /*Afor*/ 0,
433 /*Aframe*/ 0,
434 /*Aframeborder*/ 0,
435 /*Aheaders*/ 0,
436 /*Aheight*/ 0,
437 /*Ahref*/ 0,
438 /*Ahreflang*/ 0,
439 /*Ahspace*/ 0,
440 /*Ahttp_equiv*/ 0,
441 /*Aid*/ 0,
442 /*Aismap*/ 0,
443 /*Alabel*/ 0,
444 /*Alang*/ 0,
445 /*Alink*/ 0,
446 /*Alongdesc*/ 0,
447 /*Amarginheight*/ 0,
448 /*Amarginwidth*/ 0,
449 /*Amaxlength*/ 0,
450 /*Amedia*/ 0,
451 /*Amethod*/ 0,
452 /*Amultiple*/ 0,
453 /*Aname*/ 0,
454 /*Anohref*/ 0,
455 /*Anoresize*/ 0,
456 /*Anoshade*/ 0,
457 /*Anowrap*/ 0,
458 /*Aobject*/ 0,
459 /*Aonblur*/ SEonblur,
460 /*Aonchange*/ SEonchange,
461 /*Aonclick*/ SEonclick,
462 /*Aondblclick*/ SEondblclick,
463 /*Aonfocus*/ SEonfocus,
464 /*Aonkeypress*/ SEonkeypress,
465 /*Aonkeyup*/ SEonkeyup,
466 /*Aonload*/ SEonload,
467 /*Aonmousedown*/ SEonmousedown,
468 /*Aonmousemove*/ SEonmousemove,
469 /*Aonmouseout*/ SEonmouseout,
470 /*Aonmouseover*/ SEonmouseover,
471 /*Aonmouseup*/ SEonmouseup,
472 /*Aonreset*/ SEonreset,
473 /*Aonselect*/ SEonselect,
474 /*Aonsubmit*/ SEonsubmit,
475 /*Aonunload*/ SEonunload,
476 /*Aprofile*/ 0,
477 /*Aprompt*/ 0,
478 /*Areadonly*/ 0,
479 /*Arel*/ 0,
480 /*Arev*/ 0,
481 /*Arows*/ 0,
482 /*Arowspan*/ 0,
483 /*Arules*/ 0,
484 /*Ascheme*/ 0,
485 /*Ascope*/ 0,
486 /*Ascrolling*/ 0,
487 /*Aselected*/ 0,
488 /*Ashape*/ 0,
489 /*Asize*/ 0,
490 /*Aspan*/ 0,
491 /*Asrc*/ 0,
492 /*Astandby*/ 0,
493 /*Astart*/ 0,
494 /*Astyle*/ 0,
495 /*Asummary*/ 0,
496 /*Atabindex*/ 0,
497 /*Atarget*/ 0,
498 /*Atext*/ 0,
499 /*Atitle*/ 0,
500 /*Atype*/ 0,
501 /*Ausemap*/ 0,
502 /*Avalign*/ 0,
503 /*Avalue*/ 0,
504 /*Avaluetype*/ 0,
505 /*Aversion*/ 0,
506 /*Avlink*/ 0,
507 /*Avspace*/ 0,
508 /*Awidth*/ 0,
509 };
511 /* Color lookup table */
512 static StringInt *color_tab;
513 static AsciiInt _color_tab[] = {
514 {"aqua", 0x00FFFF},
515 {"black", 0x000000},
516 {"blue", 0x0000CC},
517 {"fuchsia", 0xFF00FF},
518 {"gray", 0x808080},
519 {"green", 0x008000},
520 {"lime", 0x00FF00},
521 {"maroon", 0x800000},
522 {"navy", 0x000080,},
523 {"olive", 0x808000},
524 {"purple", 0x800080},
525 {"red", 0xFF0000},
526 {"silver", 0xC0C0C0},
527 {"teal", 0x008080},
528 {"white", 0xFFFFFF},
529 {"yellow", 0xFFFF00}
530 };
531 #define NCOLORS (sizeof(_color_tab)/sizeof(StringInt))
533 static StringInt *targetmap;
534 static int targetmapsize;
535 static int ntargets;
537 static int buildinited = 0;
539 #define SMALLBUFSIZE 240
540 #define BIGBUFSIZE 2000
542 int dbgbuild = 0;
543 int warn = 0;
545 static Align aalign(Token* tok);
546 static int acolorval(Token* tok, int attid, int dflt);
547 static void addbrk(Pstate* ps, int sp, int clr);
548 static void additem(Pstate* ps, Item* it, Token* tok);
549 static void addlinebrk(Pstate* ps, int clr);
550 static void addnbsp(Pstate* ps);
551 static void addtext(Pstate* ps, Rune* s);
552 static Dimen adimen(Token* tok, int attid);
553 static int aflagval(Token* tok, int attid);
554 static int aintval(Token* tok, int attid, int dflt);
555 static Rune* astrval(Token* tok, int attid, Rune* dflt);
556 static int atabval(Token* tok, int attid, StringInt* tab, int ntab, int dflt);
557 static int atargval(Token* tok, int dflt);
558 static int auintval(Token* tok, int attid, int dflt);
559 static Rune* aurlval(Token* tok, int attid, Rune* dflt, Rune* base);
560 static Rune* aval(Token* tok, int attid);
561 static void buildinit(void);
562 static Pstate* cell_pstate(Pstate* oldps, int ishead);
563 static void changehang(Pstate* ps, int delta);
564 static void changeindent(Pstate* ps, int delta);
565 static int color(Rune* s, int dflt);
566 static void copystack(Stack* tostk, Stack* fromstk);
567 static int dimprint(char* buf, int nbuf, Dimen d);
568 static Pstate* finishcell(Table* curtab, Pstate* psstk);
569 static void finish_table(Table* t);
570 static void freeanchor(Anchor* a);
571 static void freedestanchor(DestAnchor* da);
572 static void freeform(Form* f);
573 static void freeformfield(Formfield* ff);
574 static void freeitem(Item* it);
575 static void freepstate(Pstate* p);
576 static void freepstatestack(Pstate* pshead);
577 static void freescriptevents(SEvent* ehead);
578 static void freetable(Table* t);
579 static Map* getmap(Docinfo* di, Rune* name);
580 static Rune* getpcdata(Token* toks, int tokslen, int* ptoki);
581 static Pstate* lastps(Pstate* psl);
582 static Rune* listmark(uchar ty, int n);
583 static int listtyval(Token* tok, int dflt);
584 static Align makealign(int halign, int valign);
585 static Background makebackground(Rune* imgurl, int color);
586 static Dimen makedimen(int kind, int spec);
587 static Anchor* newanchor(int index, Rune* name, Rune* href, int target, Anchor* link);
588 static Area* newarea(int shape, Rune* href, int target, Area* link);
589 static DestAnchor* newdestanchor(int index, Rune* name, Item* item, DestAnchor* link);
590 static Docinfo* newdocinfo(void);
591 static Genattr* newgenattr(Rune* id, Rune* class, Rune* style, Rune* title, SEvent* events);
592 static Form* newform(int formid, Rune* name, Rune* action,
593 int target, int method, Form* link);
594 static Formfield* newformfield(int ftype, int fieldid, Form* form, Rune* name,
595 Rune* value, int size, int maxlength, Formfield* link);
596 static Item* newifloat(Item* it, int side);
597 static Item* newiformfield(Formfield* ff);
598 static Item* newiimage(Rune* src, Rune* altrep, int align, int width, int height,
599 int hspace, int vspace, int border, int ismap, Map* map);
600 static Item* newirule(int align, int size, int noshade, Dimen wspec);
601 static Item* newispacer(int spkind);
602 static Item* newitable(Table* t);
603 static ItemSource* newitemsource(Docinfo* di);
604 static Item* newitext(Rune* s, int fnt, int fg, int voff, int ul);
605 static Kidinfo* newkidinfo(int isframeset, Kidinfo* link);
606 static Option* newoption(int selected, Rune* value, Rune* display, Option* link);
607 static Pstate* newpstate(Pstate* link);
608 static SEvent* newscriptevent(int type, Rune* script, SEvent* link);
609 static Table* newtable(int tableid, Align align, Dimen width, int border,
610 int cellspacing, int cellpadding, Background bg, Token* tok, Table* link);
611 static Tablecell* newtablecell(int cellid, int rowspan, int colspan, Align align, Dimen wspec,
612 int hspec, Background bg, int flags, Tablecell* link);
613 static Tablerow* newtablerow(Align align, Background bg, int flags, Tablerow* link);
614 static Dimen parsedim(Rune* s, int ns);
615 static void pop(Stack* stk);
616 static void popfontsize(Pstate* ps);
617 static void popfontstyle(Pstate* ps);
618 static void popjust(Pstate* ps);
619 static int popretnewtop(Stack* stk, int dflt);
620 static int push(Stack* stk, int val);
621 static void pushfontsize(Pstate* ps, int sz);
622 static void pushfontstyle(Pstate* ps, int sty);
623 static void pushjust(Pstate* ps, int j);
624 static Item* textit(Pstate* ps, Rune* s);
625 static Rune* removeallwhite(Rune* s);
626 static void resetdocinfo(Docinfo* d);
627 static void setcurfont(Pstate* ps);
628 static void setcurjust(Pstate* ps);
629 static void setdimarray(Token* tok, int attid, Dimen** pans, int* panslen);
630 static Rune* stringalign(int a);
631 static void targetmapinit(void);
632 static int toint(Rune* s);
633 static int top(Stack* stk, int dflt);
634 static void trim_cell(Tablecell* c);
635 static int validalign(Align a);
636 static int validdimen(Dimen d);
637 static int validformfield(Formfield* f);
638 static int validhalign(int a);
639 static int validptr(void* p);
640 static int validStr(Rune* s);
641 static int validtable(Table* t);
642 static int validtablerow(Tablerow* r);
643 static int validtablecol(Tablecol* c);
644 static int validtablecell(Tablecell* c);
645 static int validvalign(int a);
646 static int Iconv(Fmt *f);
648 static void
649 buildinit(void)
651 _runetabinit();
652 roman = _cvtstringtab(_roman, nelem(_roman));
653 color_tab = _cvtstringinttab(_color_tab, nelem(_color_tab));
654 method_tab = _cvtstringinttab(_method_tab, nelem(_method_tab));
655 shape_tab = _cvtstringinttab(_shape_tab, nelem(_shape_tab));
656 fscroll_tab = _cvtstringinttab(_fscroll_tab, nelem(_fscroll_tab));
657 clear_tab = _cvtstringinttab(_clear_tab, nelem(_clear_tab));
658 input_tab = _cvtstringinttab(_input_tab, nelem(_input_tab));
659 align_tab = _cvtstringinttab(_align_tab, nelem(_align_tab));
661 fmtinstall('I', Iconv);
662 targetmapinit();
663 buildinited = 1;
666 static ItemSource*
667 newitemsource(Docinfo* di)
669 ItemSource* is;
670 Pstate* ps;
672 ps = newpstate(nil);
673 if(di->mediatype != TextHtml) {
674 ps->curstate &= ~IFwrap;
675 ps->literal = 1;
676 pushfontstyle(ps, FntT);
678 is = (ItemSource*)emalloc(sizeof(ItemSource));
679 is->doc = di;
680 is->psstk = ps;
681 is->nforms = 0;
682 is->ntables = 0;
683 is->nanchors = 0;
684 is->nframes = 0;
685 is->curform = nil;
686 is->curmap = nil;
687 is->tabstk = nil;
688 is->kidstk = nil;
689 return is;
692 static Item *getitems(ItemSource* is, uchar* data, int datalen);
694 /* Parse an html document and create a list of layout items. */
695 /* Allocate and return document info in *pdi. */
696 /* When caller is done with the items, it should call */
697 /* freeitems on the returned result, and then */
698 /* freedocinfo(*pdi). */
699 Item*
700 parsehtml(uchar* data, int datalen, Rune* pagesrc, int mtype, int chset, Docinfo** pdi)
702 Item *it;
703 Docinfo* di;
704 ItemSource* is;
706 di = newdocinfo();
707 di->src = _Strdup(pagesrc);
708 di->base = _Strdup(pagesrc);
709 di->mediatype = mtype;
710 di->chset = chset;
711 *pdi = di;
712 is = newitemsource(di);
713 it = getitems(is, data, datalen);
714 freepstatestack(is->psstk);
715 free(is);
716 return it;
719 /* Get a group of tokens for lexer, parse them, and create */
720 /* a list of layout items. */
721 /* When caller is done with the items, it should call */
722 /* freeitems on the returned result. */
723 static Item*
724 getitems(ItemSource* is, uchar* data, int datalen)
726 int i;
727 int j;
728 int nt;
729 int pt;
730 int doscripts;
731 int tokslen;
732 int toki;
733 int h;
734 int sz;
735 int method;
736 int n;
737 int nblank;
738 int norsz;
739 int bramt;
740 int sty;
741 int nosh;
742 int oldcuranchor;
743 int dfltbd;
744 int v;
745 int hang;
746 int isempty;
747 int tag;
748 int brksp;
749 int target;
750 uchar brk;
751 uchar flags;
752 uchar align;
753 uchar al;
754 uchar ty;
755 uchar ty2;
756 Pstate* ps;
757 Pstate* nextps;
758 Pstate* outerps;
759 Table* curtab;
760 Token* tok;
761 Token* toks;
762 Docinfo* di;
763 Item* ans;
764 Item* img;
765 Item* ffit;
766 Item* tabitem;
767 Rune* s;
768 Rune* t;
769 Rune* name;
770 Rune* enctype;
771 Rune* usemap;
772 Rune* prompt;
773 Rune* equiv;
774 Rune* val;
775 Rune* nsz;
776 Rune* script;
777 Map* map;
778 Form* frm;
779 Iimage* ii;
780 Kidinfo* kd;
781 Kidinfo* ks;
782 Kidinfo* pks;
783 Dimen wd;
784 Option* option;
785 Table* tab;
786 Tablecell* c;
787 Tablerow* tr;
788 Formfield* field;
789 Formfield* ff;
790 Rune* href;
791 Rune* src;
792 Rune* scriptsrc;
793 Rune* bgurl;
794 Rune* action;
795 Background bg;
797 if(!buildinited)
798 buildinit();
799 doscripts = 0; /* for now */
800 ps = is->psstk;
801 curtab = is->tabstk;
802 di = is->doc;
803 toks = _gettoks(data, datalen, di->chset, di->mediatype, &tokslen);
804 toki = 0;
805 for(; toki < tokslen; toki++) {
806 tok = &toks[toki];
807 if(dbgbuild > 1)
808 fprint(2, "build: curstate %ux, token %T\n", ps->curstate, tok);
809 tag = tok->tag;
810 brk = 0;
811 brksp = 0;
812 if(tag < Numtags) {
813 brk = blockbrk[tag];
814 if(brk&SPBefore)
815 brksp = 1;
817 else if(tag < Numtags + RBRA) {
818 brk = blockbrk[tag - RBRA];
819 if(brk&SPAfter)
820 brksp = 1;
822 if(brk) {
823 addbrk(ps, brksp, 0);
824 if(ps->inpar) {
825 popjust(ps);
826 ps->inpar = 0;
829 /* check common case first (Data), then switch statement on tag */
830 if(tag == Data) {
831 /* Lexing didn't pay attention to SGML record boundary rules: */
832 /* \n after start tag or before end tag to be discarded. */
833 /* (Lex has already discarded all \r's). */
834 /* Some pages assume this doesn't happen in <PRE> text, */
835 /* so we won't do it if literal is true. */
836 /* BUG: won't discard \n before a start tag that begins */
837 /* the next bufferful of tokens. */
838 s = tok->text;
839 n = _Strlen(s);
840 if(!ps->literal) {
841 i = 0;
842 j = n;
843 if(toki > 0) {
844 pt = toks[toki - 1].tag;
845 /* IE and Netscape both ignore this rule (contrary to spec) */
846 /* if previous tag was img */
847 if(pt < Numtags && pt != Timg && j > 0 && s[0] == '\n')
848 i++;
850 if(toki < tokslen - 1) {
851 nt = toks[toki + 1].tag;
852 if(nt >= RBRA && nt < Numtags + RBRA && j > i && s[j - 1] == '\n')
853 j--;
855 if(i > 0 || j < n) {
856 t = s;
857 s = _Strsubstr(s, i, j);
858 free(t);
859 n = j-i;
862 if(ps->skipwhite) {
863 _trimwhite(s, n, &t, &nt);
864 if(t == nil) {
865 free(s);
866 s = nil;
868 else if(t != s) {
869 t = _Strndup(t, nt);
870 free(s);
871 s = t;
873 if(s != nil)
874 ps->skipwhite = 0;
876 tok->text = nil; /* token doesn't own string anymore */
877 if(s != nil){
878 addtext(ps, s);
879 s = nil;
882 else
883 switch(tag) {
884 /* Some abbrevs used in following DTD comments */
885 /* %text = #PCDATA */
886 /* | TT | I | B | U | STRIKE | BIG | SMALL | SUB | SUP */
887 /* | EM | STRONG | DFN | CODE | SAMP | KBD | VAR | CITE */
888 /* | A | IMG | APPLET | FONT | BASEFONT | BR | SCRIPT | MAP */
889 /* | INPUT | SELECT | TEXTAREA */
890 /* %block = P | UL | OL | DIR | MENU | DL | PRE | DL | DIV | CENTER */
891 /* | BLOCKQUOTE | FORM | ISINDEX | HR | TABLE */
892 /* %flow = (%text | %block)* */
893 /* %body.content = (%heading | %text | %block | ADDRESS)* */
895 /* <!ELEMENT A - - (%text) -(A)> */
896 /* Anchors are not supposed to be nested, but you sometimes see */
897 /* href anchors inside destination anchors. */
898 case Ta:
899 if(ps->curanchor != 0) {
900 if(warn)
901 fprint(2, "warning: nested <A> or missing </A>\n");
902 ps->curanchor = 0;
904 name = aval(tok, Aname);
905 href = aurlval(tok, Ahref, nil, di->base);
906 /* ignore rel, rev, and title attrs */
907 if(href != nil) {
908 target = atargval(tok, di->target);
909 di->anchors = newanchor(++is->nanchors, name, href, target, di->anchors);
910 if(name != nil)
911 name = _Strdup(name); /* for DestAnchor construction, below */
912 ps->curanchor = is->nanchors;
913 ps->curfg = push(&ps->fgstk, di->link);
914 ps->curul = push(&ps->ulstk, ULunder);
916 if(name != nil) {
917 /* add a null item to be destination */
918 additem(ps, newispacer(ISPnull), tok);
919 di->dests = newdestanchor(++is->nanchors, name, ps->lastit, di->dests);
921 break;
923 case Ta+RBRA :
924 if(ps->curanchor != 0) {
925 ps->curfg = popretnewtop(&ps->fgstk, di->text);
926 ps->curul = popretnewtop(&ps->ulstk, ULnone);
927 ps->curanchor = 0;
929 break;
931 /* <!ELEMENT APPLET - - (PARAM | %text)* > */
932 /* We can't do applets, so ignore PARAMS, and let */
933 /* the %text contents appear for the alternative rep */
934 case Tapplet:
935 case Tapplet+RBRA:
936 if(warn && tag == Tapplet)
937 fprint(2, "warning: <APPLET> ignored\n");
938 break;
940 /* <!ELEMENT AREA - O EMPTY> */
941 case Tarea:
942 map = di->maps;
943 if(map == nil) {
944 if(warn)
945 fprint(2, "warning: <AREA> not inside <MAP>\n");
946 continue;
948 map->areas = newarea(atabval(tok, Ashape, shape_tab, NSHAPETAB, SHrect),
949 aurlval(tok, Ahref, nil, di->base),
950 atargval(tok, di->target),
951 map->areas);
952 setdimarray(tok, Acoords, &map->areas->coords, &map->areas->ncoords);
953 break;
955 /* <!ELEMENT (B|STRONG) - - (%text)*> */
956 case Tb:
957 case Tstrong:
958 pushfontstyle(ps, FntB);
959 break;
961 case Tb+RBRA:
962 case Tcite+RBRA:
963 case Tcode+RBRA:
964 case Tdfn+RBRA:
965 case Tem+RBRA:
966 case Tkbd+RBRA:
967 case Ti+RBRA:
968 case Tsamp+RBRA:
969 case Tstrong+RBRA:
970 case Ttt+RBRA:
971 case Tvar+RBRA :
972 case Taddress+RBRA:
973 popfontstyle(ps);
974 break;
976 /* <!ELEMENT BASE - O EMPTY> */
977 case Tbase:
978 t = di->base;
979 di->base = aurlval(tok, Ahref, di->base, di->base);
980 if(t != nil)
981 free(t);
982 di->target = atargval(tok, di->target);
983 break;
985 /* <!ELEMENT BASEFONT - O EMPTY> */
986 case Tbasefont:
987 ps->adjsize = aintval(tok, Asize, 3) - 3;
988 break;
990 /* <!ELEMENT (BIG|SMALL) - - (%text)*> */
991 case Tbig:
992 case Tsmall:
993 sz = ps->adjsize;
994 if(tag == Tbig)
995 sz += Large;
996 else
997 sz += Small;
998 pushfontsize(ps, sz);
999 break;
1001 case Tbig+RBRA:
1002 case Tsmall+RBRA:
1003 popfontsize(ps);
1004 break;
1006 /* <!ELEMENT BLOCKQUOTE - - %body.content> */
1007 case Tblockquote:
1008 changeindent(ps, BQTAB);
1009 break;
1011 case Tblockquote+RBRA:
1012 changeindent(ps, -BQTAB);
1013 break;
1015 /* <!ELEMENT BODY O O %body.content> */
1016 case Tbody:
1017 ps->skipping = 0;
1018 bg = makebackground(nil, acolorval(tok, Abgcolor, di->background.color));
1019 bgurl = aurlval(tok, Abackground, nil, di->base);
1020 if(bgurl != nil) {
1021 if(di->backgrounditem != nil)
1022 freeitem((Item*)di->backgrounditem);
1023 /* really should remove old item from di->images list, */
1024 /* but there should only be one BODY element ... */
1025 di->backgrounditem = (Iimage*)newiimage(bgurl, nil, ALnone, 0, 0, 0, 0, 0, 0, nil);
1026 di->backgrounditem->nextimage = di->images;
1027 di->images = di->backgrounditem;
1029 ps->curbg = bg;
1030 di->background = bg;
1031 di->text = acolorval(tok, Atext, di->text);
1032 di->link = acolorval(tok, Alink, di->link);
1033 di->vlink = acolorval(tok, Avlink, di->vlink);
1034 di->alink = acolorval(tok, Aalink, di->alink);
1035 if(di->text != ps->curfg) {
1036 ps->curfg = di->text;
1037 ps->fgstk.n = 0;
1039 break;
1041 case Tbody+RBRA:
1042 /* HTML spec says ignore things after </body>, */
1043 /* but IE and Netscape don't */
1044 /* ps.skipping = 1; */
1045 break;
1047 /* <!ELEMENT BR - O EMPTY> */
1048 case Tbr:
1049 addlinebrk(ps, atabval(tok, Aclear, clear_tab, NCLEARTAB, 0));
1050 break;
1052 /* <!ELEMENT CAPTION - - (%text;)*> */
1053 case Tcaption:
1054 if(curtab == nil) {
1055 if(warn)
1056 fprint(2, "warning: <CAPTION> outside <TABLE>\n");
1057 continue;
1059 if(curtab->caption != nil) {
1060 if(warn)
1061 fprint(2, "warning: more than one <CAPTION> in <TABLE>\n");
1062 continue;
1064 ps = newpstate(ps);
1065 curtab->caption_place = atabval(tok, Aalign, align_tab, NALIGNTAB, ALtop);
1066 break;
1068 case Tcaption+RBRA:
1069 nextps = ps->next;
1070 if(curtab == nil || nextps == nil) {
1071 if(warn)
1072 fprint(2, "warning: unexpected </CAPTION>\n");
1073 continue;
1075 curtab->caption = ps->items->next;
1076 free(ps);
1077 ps = nextps;
1078 break;
1080 case Tcenter:
1081 case Tdiv:
1082 if(tag == Tcenter)
1083 al = ALcenter;
1084 else
1085 al = atabval(tok, Aalign, align_tab, NALIGNTAB, ps->curjust);
1086 pushjust(ps, al);
1087 break;
1089 case Tcenter+RBRA:
1090 case Tdiv+RBRA:
1091 popjust(ps);
1092 break;
1094 /* <!ELEMENT DD - O %flow > */
1095 case Tdd:
1096 if(ps->hangstk.n == 0) {
1097 if(warn)
1098 fprint(2, "warning: <DD> not inside <DL\n");
1099 continue;
1101 h = top(&ps->hangstk, 0);
1102 if(h != 0)
1103 changehang(ps, -10*LISTTAB);
1104 else
1105 addbrk(ps, 0, 0);
1106 push(&ps->hangstk, 0);
1107 break;
1109 /*<!ELEMENT (DIR|MENU) - - (LI)+ -(%block) > */
1110 /*<!ELEMENT (OL|UL) - - (LI)+> */
1111 case Tdir:
1112 case Tmenu:
1113 case Tol:
1114 case Tul:
1115 changeindent(ps, LISTTAB);
1116 push(&ps->listtypestk, listtyval(tok, (tag==Tol)? LT1 : LTdisc));
1117 push(&ps->listcntstk, aintval(tok, Astart, 1));
1118 break;
1120 case Tdir+RBRA:
1121 case Tmenu+RBRA:
1122 case Tol+RBRA:
1123 case Tul+RBRA:
1124 if(ps->listtypestk.n == 0) {
1125 if(warn)
1126 fprint(2, "warning: %T ended no list\n", tok);
1127 continue;
1129 addbrk(ps, 0, 0);
1130 pop(&ps->listtypestk);
1131 pop(&ps->listcntstk);
1132 changeindent(ps, -LISTTAB);
1133 break;
1135 /* <!ELEMENT DL - - (DT|DD)+ > */
1136 case Tdl:
1137 changeindent(ps, LISTTAB);
1138 push(&ps->hangstk, 0);
1139 break;
1141 case Tdl+RBRA:
1142 if(ps->hangstk.n == 0) {
1143 if(warn)
1144 fprint(2, "warning: unexpected </DL>\n");
1145 continue;
1147 changeindent(ps, -LISTTAB);
1148 if(top(&ps->hangstk, 0) != 0)
1149 changehang(ps, -10*LISTTAB);
1150 pop(&ps->hangstk);
1151 break;
1153 /* <!ELEMENT DT - O (%text)* > */
1154 case Tdt:
1155 if(ps->hangstk.n == 0) {
1156 if(warn)
1157 fprint(2, "warning: <DT> not inside <DL>\n");
1158 continue;
1160 h = top(&ps->hangstk, 0);
1161 pop(&ps->hangstk);
1162 if(h != 0)
1163 changehang(ps, -10*LISTTAB);
1164 changehang(ps, 10*LISTTAB);
1165 push(&ps->hangstk, 1);
1166 break;
1168 /* <!ELEMENT FONT - - (%text)*> */
1169 case Tfont:
1170 sz = top(&ps->fntsizestk, Normal);
1171 if(_tokaval(tok, Asize, &nsz, 0)) {
1172 if(_prefix(L(Lplus), nsz))
1173 sz = Normal + _Strtol(nsz+1, nil, 10) + ps->adjsize;
1174 else if(_prefix(L(Lminus), nsz))
1175 sz = Normal - _Strtol(nsz+1, nil, 10) + ps->adjsize;
1176 else if(nsz != nil)
1177 sz = Normal + (_Strtol(nsz, nil, 10) - 3);
1179 ps->curfg = push(&ps->fgstk, acolorval(tok, Acolor, ps->curfg));
1180 pushfontsize(ps, sz);
1181 break;
1183 case Tfont+RBRA:
1184 if(ps->fgstk.n == 0) {
1185 if(warn)
1186 fprint(2, "warning: unexpected </FONT>\n");
1187 continue;
1189 ps->curfg = popretnewtop(&ps->fgstk, di->text);
1190 popfontsize(ps);
1191 break;
1193 /* <!ELEMENT FORM - - %body.content -(FORM) > */
1194 case Tform:
1195 if(is->curform != nil) {
1196 if(warn)
1197 fprint(2, "warning: <FORM> nested inside another\n");
1198 continue;
1200 action = aurlval(tok, Aaction, di->base, di->base);
1201 s = aval(tok, Aid);
1202 name = astrval(tok, Aname, s);
1203 if(s)
1204 free(s);
1205 target = atargval(tok, di->target);
1206 method = atabval(tok, Amethod, method_tab, NMETHODTAB, HGet);
1207 if(warn && _tokaval(tok, Aenctype, &enctype, 0) &&
1208 _Strcmp(enctype, L(Lappl_form)))
1209 fprint(2, "form enctype %S not handled\n", enctype);
1210 frm = newform(++is->nforms, name, action, target, method, di->forms);
1211 di->forms = frm;
1212 is->curform = frm;
1213 break;
1215 case Tform+RBRA:
1216 if(is->curform == nil) {
1217 if(warn)
1218 fprint(2, "warning: unexpected </FORM>\n");
1219 continue;
1221 /* put fields back in input order */
1222 is->curform->fields = (Formfield*)_revlist((List*)is->curform->fields);
1223 is->curform = nil;
1224 break;
1226 /* <!ELEMENT FRAME - O EMPTY> */
1227 case Tframe:
1228 ks = is->kidstk;
1229 if(ks == nil) {
1230 if(warn)
1231 fprint(2, "warning: <FRAME> not in <FRAMESET>\n");
1232 continue;
1234 ks->kidinfos = kd = newkidinfo(0, ks->kidinfos);
1235 kd->src = aurlval(tok, Asrc, nil, di->base);
1236 kd->name = aval(tok, Aname);
1237 if(kd->name == nil) {
1238 s = _ltoStr(++is->nframes);
1239 kd->name = _Strdup2(L(Lfr), s);
1240 free(s);
1242 kd->marginw = auintval(tok, Amarginwidth, 0);
1243 kd->marginh = auintval(tok, Amarginheight, 0);
1244 kd->framebd = auintval(tok, Aframeborder, 1);
1245 kd->flags = atabval(tok, Ascrolling, fscroll_tab, NFSCROLLTAB, kd->flags);
1246 norsz = aflagval(tok, Anoresize);
1247 if(norsz)
1248 kd->flags |= FRnoresize;
1249 break;
1251 /* <!ELEMENT FRAMESET - - (FRAME|FRAMESET)+> */
1252 case Tframeset:
1253 ks = newkidinfo(1, nil);
1254 pks = is->kidstk;
1255 if(pks == nil)
1256 di->kidinfo = ks;
1257 else {
1258 ks->next = pks->kidinfos;
1259 pks->kidinfos = ks;
1261 ks->nextframeset = pks;
1262 is->kidstk = ks;
1263 setdimarray(tok, Arows, &ks->rows, &ks->nrows);
1264 if(ks->nrows == 0) {
1265 ks->rows = (Dimen*)emalloc(sizeof(Dimen));
1266 ks->nrows = 1;
1267 ks->rows[0] = makedimen(Dpercent, 100);
1269 setdimarray(tok, Acols, &ks->cols, &ks->ncols);
1270 if(ks->ncols == 0) {
1271 ks->cols = (Dimen*)emalloc(sizeof(Dimen));
1272 ks->ncols = 1;
1273 ks->cols[0] = makedimen(Dpercent, 100);
1275 break;
1277 case Tframeset+RBRA:
1278 if(is->kidstk == nil) {
1279 if(warn)
1280 fprint(2, "warning: unexpected </FRAMESET>\n");
1281 continue;
1283 ks = is->kidstk;
1284 /* put kids back in original order */
1285 /* and add blank frames to fill out cells */
1286 n = ks->nrows*ks->ncols;
1287 nblank = n - _listlen((List*)ks->kidinfos);
1288 while(nblank-- > 0)
1289 ks->kidinfos = newkidinfo(0, ks->kidinfos);
1290 ks->kidinfos = (Kidinfo*)_revlist((List*)ks->kidinfos);
1291 is->kidstk = is->kidstk->nextframeset;
1292 if(is->kidstk == nil) {
1293 /* end input */
1294 ans = nil;
1295 goto return_ans;
1297 break;
1299 /* <!ELEMENT H1 - - (%text;)*>, etc. */
1300 case Th1:
1301 case Th2:
1302 case Th3:
1303 case Th4:
1304 case Th5:
1305 case Th6:
1306 bramt = 1;
1307 if(ps->items == ps->lastit)
1308 bramt = 0;
1309 addbrk(ps, bramt, IFcleft|IFcright);
1310 sz = Verylarge - (tag - Th1);
1311 if(sz < Tiny)
1312 sz = Tiny;
1313 pushfontsize(ps, sz);
1314 sty = top(&ps->fntstylestk, FntR);
1315 if(tag == Th1)
1316 sty = FntB;
1317 pushfontstyle(ps, sty);
1318 pushjust(ps, atabval(tok, Aalign, align_tab, NALIGNTAB, ps->curjust));
1319 ps->skipwhite = 1;
1320 break;
1322 case Th1+RBRA:
1323 case Th2+RBRA:
1324 case Th3+RBRA:
1325 case Th4+RBRA:
1326 case Th5+RBRA:
1327 case Th6+RBRA:
1328 addbrk(ps, 1, IFcleft|IFcright);
1329 popfontsize(ps);
1330 popfontstyle(ps);
1331 popjust(ps);
1332 break;
1334 case Thead:
1335 /* HTML spec says ignore regular markup in head, */
1336 /* but Netscape and IE don't */
1337 /* ps.skipping = 1; */
1338 break;
1340 case Thead+RBRA:
1341 ps->skipping = 0;
1342 break;
1344 /* <!ELEMENT HR - O EMPTY> */
1345 case Thr:
1346 al = atabval(tok, Aalign, align_tab, NALIGNTAB, ALcenter);
1347 sz = auintval(tok, Asize, HRSZ);
1348 wd = adimen(tok, Awidth);
1349 if(dimenkind(wd) == Dnone)
1350 wd = makedimen(Dpercent, 100);
1351 nosh = aflagval(tok, Anoshade);
1352 additem(ps, newirule(al, sz, nosh, wd), tok);
1353 addbrk(ps, 0, 0);
1354 break;
1356 case Ti:
1357 case Tcite:
1358 case Tdfn:
1359 case Tem:
1360 case Tvar:
1361 case Taddress:
1362 pushfontstyle(ps, FntI);
1363 break;
1365 /* <!ELEMENT IMG - O EMPTY> */
1366 case Timg:
1367 map = nil;
1368 oldcuranchor = ps->curanchor;
1369 if(_tokaval(tok, Ausemap, &usemap, 0)) {
1370 if(!_prefix(L(Lhash), usemap)) {
1371 if(warn)
1372 fprint(2, "warning: can't handle non-local map %S\n", usemap);
1374 else {
1375 map = getmap(di, usemap+1);
1376 if(ps->curanchor == 0) {
1377 di->anchors = newanchor(++is->nanchors, nil, nil, di->target, di->anchors);
1378 ps->curanchor = is->nanchors;
1382 align = atabval(tok, Aalign, align_tab, NALIGNTAB, ALbottom);
1383 dfltbd = 0;
1384 if(ps->curanchor != 0)
1385 dfltbd = 2;
1386 src = aurlval(tok, Asrc, nil, di->base);
1387 if(src == nil) {
1388 if(warn)
1389 fprint(2, "warning: <img> has no src attribute\n");
1390 ps->curanchor = oldcuranchor;
1391 continue;
1393 img = newiimage(src,
1394 aval(tok, Aalt),
1395 align,
1396 auintval(tok, Awidth, 0),
1397 auintval(tok, Aheight, 0),
1398 auintval(tok, Ahspace, IMGHSPACE),
1399 auintval(tok, Avspace, IMGVSPACE),
1400 auintval(tok, Aborder, dfltbd),
1401 aflagval(tok, Aismap),
1402 map);
1403 if(align == ALleft || align == ALright) {
1404 additem(ps, newifloat(img, align), tok);
1405 /* if no hspace specified, use FLTIMGHSPACE */
1406 if(!_tokaval(tok, Ahspace, &val, 0))
1407 ((Iimage*)img)->hspace = FLTIMGHSPACE;
1409 else {
1410 ps->skipwhite = 0;
1411 additem(ps, img, tok);
1413 if(!ps->skipping) {
1414 ((Iimage*)img)->nextimage = di->images;
1415 di->images = (Iimage*)img;
1417 ps->curanchor = oldcuranchor;
1418 break;
1420 /* <!ELEMENT INPUT - O EMPTY> */
1421 case Tinput:
1422 ps->skipwhite = 0;
1423 if(is->curform == nil) {
1424 if(warn)
1425 fprint(2, "<INPUT> not inside <FORM>\n");
1426 continue;
1428 is->curform->fields = field = newformfield(
1429 atabval(tok, Atype, input_tab, NINPUTTAB, Ftext),
1430 ++is->curform->nfields,
1431 is->curform,
1432 aval(tok, Aname),
1433 aval(tok, Avalue),
1434 auintval(tok, Asize, 0),
1435 auintval(tok, Amaxlength, 1000),
1436 is->curform->fields);
1437 if(aflagval(tok, Achecked))
1438 field->flags = FFchecked;
1440 switch(field->ftype) {
1441 case Ftext:
1442 case Fpassword:
1443 case Ffile:
1444 if(field->size == 0)
1445 field->size = 20;
1446 break;
1448 case Fcheckbox:
1449 if(field->name == nil) {
1450 if(warn)
1451 fprint(2, "warning: checkbox form field missing name\n");
1452 continue;
1454 if(field->value == nil)
1455 field->value = _Strdup(L(Lone));
1456 break;
1458 case Fradio:
1459 if(field->name == nil || field->value == nil) {
1460 if(warn)
1461 fprint(2, "warning: radio form field missing name or value\n");
1462 continue;
1464 break;
1466 case Fsubmit:
1467 if(field->value == nil)
1468 field->value = _Strdup(L(Lsubmit));
1469 if(field->name == nil)
1470 field->name = _Strdup(L(Lnoname));
1471 break;
1473 case Fimage:
1474 src = aurlval(tok, Asrc, nil, di->base);
1475 if(src == nil) {
1476 if(warn)
1477 fprint(2, "warning: image form field missing src\n");
1478 continue;
1480 /* width and height attrs aren't specified in HTML 3.2, */
1481 /* but some people provide them and they help avoid */
1482 /* a relayout */
1483 field->image = newiimage(src,
1484 astrval(tok, Aalt, L(Lsubmit)),
1485 atabval(tok, Aalign, align_tab, NALIGNTAB, ALbottom),
1486 auintval(tok, Awidth, 0), auintval(tok, Aheight, 0),
1487 0, 0, 0, 0, nil);
1488 ii = (Iimage*)field->image;
1489 ii->nextimage = di->images;
1490 di->images = ii;
1491 break;
1493 case Freset:
1494 if(field->value == nil)
1495 field->value = _Strdup(L(Lreset));
1496 break;
1498 case Fbutton:
1499 if(field->value == nil)
1500 field->value = _Strdup(L(Lspace));
1501 break;
1503 ffit = newiformfield(field);
1504 additem(ps, ffit, tok);
1505 if(ffit->genattr != nil)
1506 field->events = ffit->genattr->events;
1507 break;
1509 /* <!ENTITY ISINDEX - O EMPTY> */
1510 case Tisindex:
1511 ps->skipwhite = 0;
1512 prompt = astrval(tok, Aprompt, L(Lindex));
1513 target = atargval(tok, di->target);
1514 additem(ps, textit(ps, prompt), tok);
1515 frm = newform(++is->nforms,
1516 nil,
1517 di->base,
1518 target,
1519 HGet,
1520 di->forms);
1521 di->forms = frm;
1522 ff = newformfield(Ftext,
1524 frm,
1525 _Strdup(L(Lisindex)),
1526 nil,
1527 50,
1528 1000,
1529 nil);
1530 frm->fields = ff;
1531 frm->nfields = 1;
1532 additem(ps, newiformfield(ff), tok);
1533 addbrk(ps, 1, 0);
1534 break;
1536 /* <!ELEMENT LI - O %flow> */
1537 case Tli:
1538 if(ps->listtypestk.n == 0) {
1539 if(warn)
1540 fprint(2, "<LI> not in list\n");
1541 continue;
1543 ty = top(&ps->listtypestk, 0);
1544 ty2 = listtyval(tok, ty);
1545 if(ty != ty2) {
1546 ty = ty2;
1547 push(&ps->listtypestk, ty2);
1549 v = aintval(tok, Avalue, top(&ps->listcntstk, 1));
1550 if(ty == LTdisc || ty == LTsquare || ty == LTcircle)
1551 hang = 10*LISTTAB - 3;
1552 else
1553 hang = 10*LISTTAB - 1;
1554 changehang(ps, hang);
1555 addtext(ps, listmark(ty, v));
1556 push(&ps->listcntstk, v + 1);
1557 changehang(ps, -hang);
1558 ps->skipwhite = 1;
1559 break;
1561 /* <!ELEMENT MAP - - (AREA)+> */
1562 case Tmap:
1563 if(_tokaval(tok, Aname, &name, 0))
1564 is->curmap = getmap(di, name);
1565 break;
1567 case Tmap+RBRA:
1568 map = is->curmap;
1569 if(map == nil) {
1570 if(warn)
1571 fprint(2, "warning: unexpected </MAP>\n");
1572 continue;
1574 map->areas = (Area*)_revlist((List*)map->areas);
1575 break;
1577 case Tmeta:
1578 if(ps->skipping)
1579 continue;
1580 if(_tokaval(tok, Ahttp_equiv, &equiv, 0)) {
1581 val = aval(tok, Acontent);
1582 n = _Strlen(equiv);
1583 if(!_Strncmpci(equiv, n, L(Lrefresh)))
1584 di->refresh = val;
1585 else if(!_Strncmpci(equiv, n, L(Lcontent))) {
1586 n = _Strlen(val);
1587 if(!_Strncmpci(val, n, L(Ljavascript))
1588 || !_Strncmpci(val, n, L(Ljscript1))
1589 || !_Strncmpci(val, n, L(Ljscript)))
1590 di->scripttype = TextJavascript;
1591 else {
1592 if(warn)
1593 fprint(2, "unimplemented script type %S\n", val);
1594 di->scripttype = UnknownType;
1598 break;
1600 /* Nobr is NOT in HMTL 4.0, but it is ubiquitous on the web */
1601 case Tnobr:
1602 ps->skipwhite = 0;
1603 ps->curstate &= ~IFwrap;
1604 break;
1606 case Tnobr+RBRA:
1607 ps->curstate |= IFwrap;
1608 break;
1610 /* We do frames, so skip stuff in noframes */
1611 case Tnoframes:
1612 ps->skipping = 1;
1613 break;
1615 case Tnoframes+RBRA:
1616 ps->skipping = 0;
1617 break;
1619 /* We do scripts (if enabled), so skip stuff in noscripts */
1620 case Tnoscript:
1621 if(doscripts)
1622 ps->skipping = 1;
1623 break;
1625 case Tnoscript+RBRA:
1626 if(doscripts)
1627 ps->skipping = 0;
1628 break;
1630 /* <!ELEMENT OPTION - O ( //PCDATA)> */
1631 case Toption:
1632 if(is->curform == nil || is->curform->fields == nil) {
1633 if(warn)
1634 fprint(2, "warning: <OPTION> not in <SELECT>\n");
1635 continue;
1637 field = is->curform->fields;
1638 if(field->ftype != Fselect) {
1639 if(warn)
1640 fprint(2, "warning: <OPTION> not in <SELECT>\n");
1641 continue;
1643 val = aval(tok, Avalue);
1644 option = newoption(aflagval(tok, Aselected), val, nil, field->options);
1645 field->options = option;
1646 option->display = getpcdata(toks, tokslen, &toki);
1647 if(val == nil)
1648 option->value = _Strdup(option->display);
1649 break;
1651 /* <!ELEMENT P - O (%text)* > */
1652 case Tp:
1653 pushjust(ps, atabval(tok, Aalign, align_tab, NALIGNTAB, ps->curjust));
1654 ps->inpar = 1;
1655 ps->skipwhite = 1;
1656 break;
1658 case Tp+RBRA:
1659 break;
1661 /* <!ELEMENT PARAM - O EMPTY> */
1662 /* Do something when we do applets... */
1663 case Tparam:
1664 break;
1666 /* <!ELEMENT PRE - - (%text)* -(IMG|BIG|SMALL|SUB|SUP|FONT) > */
1667 case Tpre:
1668 ps->curstate &= ~IFwrap;
1669 ps->literal = 1;
1670 ps->skipwhite = 0;
1671 pushfontstyle(ps, FntT);
1672 break;
1674 case Tpre+RBRA:
1675 ps->curstate |= IFwrap;
1676 if(ps->literal) {
1677 popfontstyle(ps);
1678 ps->literal = 0;
1680 break;
1682 /* <!ELEMENT SCRIPT - - CDATA> */
1683 case Tscript:
1684 if(doscripts) {
1685 if(!di->hasscripts) {
1686 if(di->scripttype == TextJavascript) {
1687 /* TODO: initialize script if nec. */
1688 /* initjscript(di); */
1689 di->hasscripts = 1;
1693 if(!di->hasscripts) {
1694 if(warn)
1695 fprint(2, "warning: <SCRIPT> ignored\n");
1696 ps->skipping = 1;
1698 else {
1699 scriptsrc = aurlval(tok, Asrc, nil, di->base);
1700 script = nil;
1701 if(scriptsrc != nil) {
1702 if(warn)
1703 fprint(2, "warning: non-local <SCRIPT> ignored\n");
1704 free(scriptsrc);
1706 else {
1707 script = getpcdata(toks, tokslen, &toki);
1709 if(script != nil) {
1710 if(warn)
1711 fprint(2, "script ignored\n");
1712 free(script);
1715 break;
1717 case Tscript+RBRA:
1718 ps->skipping = 0;
1719 break;
1721 /* <!ELEMENT SELECT - - (OPTION+)> */
1722 case Tselect:
1723 if(is->curform == nil) {
1724 if(warn)
1725 fprint(2, "<SELECT> not inside <FORM>\n");
1726 continue;
1728 field = newformfield(Fselect,
1729 ++is->curform->nfields,
1730 is->curform,
1731 aval(tok, Aname),
1732 nil,
1733 auintval(tok, Asize, 0),
1735 is->curform->fields);
1736 is->curform->fields = field;
1737 if(aflagval(tok, Amultiple))
1738 field->flags = FFmultiple;
1739 ffit = newiformfield(field);
1740 additem(ps, ffit, tok);
1741 if(ffit->genattr != nil)
1742 field->events = ffit->genattr->events;
1743 /* throw away stuff until next tag (should be <OPTION>) */
1744 s = getpcdata(toks, tokslen, &toki);
1745 if(s != nil)
1746 free(s);
1747 break;
1749 case Tselect+RBRA:
1750 if(is->curform == nil || is->curform->fields == nil) {
1751 if(warn)
1752 fprint(2, "warning: unexpected </SELECT>\n");
1753 continue;
1755 field = is->curform->fields;
1756 if(field->ftype != Fselect)
1757 continue;
1758 /* put options back in input order */
1759 field->options = (Option*)_revlist((List*)field->options);
1760 break;
1762 /* <!ELEMENT (STRIKE|U) - - (%text)*> */
1763 case Tstrike:
1764 case Tu:
1765 ps->curul = push(&ps->ulstk, (tag==Tstrike)? ULmid : ULunder);
1766 break;
1768 case Tstrike+RBRA:
1769 case Tu+RBRA:
1770 if(ps->ulstk.n == 0) {
1771 if(warn)
1772 fprint(2, "warning: unexpected %T\n", tok);
1773 continue;
1775 ps->curul = popretnewtop(&ps->ulstk, ULnone);
1776 break;
1778 /* <!ELEMENT STYLE - - CDATA> */
1779 case Tstyle:
1780 if(warn)
1781 fprint(2, "warning: unimplemented <STYLE>\n");
1782 ps->skipping = 1;
1783 break;
1785 case Tstyle+RBRA:
1786 ps->skipping = 0;
1787 break;
1789 /* <!ELEMENT (SUB|SUP) - - (%text)*> */
1790 case Tsub:
1791 case Tsup:
1792 if(tag == Tsub)
1793 ps->curvoff += SUBOFF;
1794 else
1795 ps->curvoff -= SUPOFF;
1796 push(&ps->voffstk, ps->curvoff);
1797 sz = top(&ps->fntsizestk, Normal);
1798 pushfontsize(ps, sz - 1);
1799 break;
1801 case Tsub+RBRA:
1802 case Tsup+RBRA:
1803 if(ps->voffstk.n == 0) {
1804 if(warn)
1805 fprint(2, "warning: unexpected %T\n", tok);
1806 continue;
1808 ps->curvoff = popretnewtop(&ps->voffstk, 0);
1809 popfontsize(ps);
1810 break;
1812 /* <!ELEMENT TABLE - - (CAPTION?, TR+)> */
1813 case Ttable:
1814 ps->skipwhite = 0;
1815 tab = newtable(++is->ntables,
1816 aalign(tok),
1817 adimen(tok, Awidth),
1818 aflagval(tok, Aborder),
1819 auintval(tok, Acellspacing, TABSP),
1820 auintval(tok, Acellpadding, TABPAD),
1821 makebackground(nil, acolorval(tok, Abgcolor, ps->curbg.color)),
1822 tok,
1823 is->tabstk);
1824 is->tabstk = tab;
1825 curtab = tab;
1826 break;
1828 case Ttable+RBRA:
1829 if(curtab == nil) {
1830 if(warn)
1831 fprint(2, "warning: unexpected </TABLE>\n");
1832 continue;
1834 isempty = (curtab->cells == nil);
1835 if(isempty) {
1836 if(warn)
1837 fprint(2, "warning: <TABLE> has no cells\n");
1839 else {
1840 ps = finishcell(curtab, ps);
1841 if(curtab->rows != nil)
1842 curtab->rows->flags = 0;
1843 finish_table(curtab);
1845 ps->skipping = 0;
1846 if(!isempty) {
1847 tabitem = newitable(curtab);
1848 al = curtab->align.halign;
1849 switch(al) {
1850 case ALleft:
1851 case ALright:
1852 additem(ps, newifloat(tabitem, al), tok);
1853 break;
1854 default:
1855 if(al == ALcenter)
1856 pushjust(ps, ALcenter);
1857 addbrk(ps, 0, 0);
1858 if(ps->inpar) {
1859 popjust(ps);
1860 ps->inpar = 0;
1862 additem(ps, tabitem, curtab->tabletok);
1863 if(al == ALcenter)
1864 popjust(ps);
1865 break;
1868 if(is->tabstk == nil) {
1869 if(warn)
1870 fprint(2, "warning: table stack is wrong\n");
1872 else
1873 is->tabstk = is->tabstk->next;
1874 curtab->next = di->tables;
1875 di->tables = curtab;
1876 curtab = is->tabstk;
1877 if(!isempty)
1878 addbrk(ps, 0, 0);
1879 break;
1881 /* <!ELEMENT (TH|TD) - O %body.content> */
1882 /* Cells for a row are accumulated in reverse order. */
1883 /* We push ps on a stack, and use a new one to accumulate */
1884 /* the contents of the cell. */
1885 case Ttd:
1886 case Tth:
1887 if(curtab == nil) {
1888 if(warn)
1889 fprint(2, "%T outside <TABLE>\n", tok);
1890 continue;
1892 if(ps->inpar) {
1893 popjust(ps);
1894 ps->inpar = 0;
1896 ps = finishcell(curtab, ps);
1897 tr = nil;
1898 if(curtab->rows != nil)
1899 tr = curtab->rows;
1900 if(tr == nil || !tr->flags) {
1901 if(warn)
1902 fprint(2, "%T outside row\n", tok);
1903 tr = newtablerow(makealign(ALnone, ALnone),
1904 makebackground(nil, curtab->background.color),
1905 TFparsing,
1906 curtab->rows);
1907 curtab->rows = tr;
1909 ps = cell_pstate(ps, tag == Tth);
1910 flags = TFparsing;
1911 if(aflagval(tok, Anowrap)) {
1912 flags |= TFnowrap;
1913 ps->curstate &= ~IFwrap;
1915 if(tag == Tth)
1916 flags |= TFisth;
1917 c = newtablecell(curtab->cells==nil? 1 : curtab->cells->cellid+1,
1918 auintval(tok, Arowspan, 1),
1919 auintval(tok, Acolspan, 1),
1920 aalign(tok),
1921 adimen(tok, Awidth),
1922 auintval(tok, Aheight, 0),
1923 makebackground(nil, acolorval(tok, Abgcolor, tr->background.color)),
1924 flags,
1925 curtab->cells);
1926 curtab->cells = c;
1927 ps->curbg = c->background;
1928 if(c->align.halign == ALnone) {
1929 if(tr->align.halign != ALnone)
1930 c->align.halign = tr->align.halign;
1931 else if(tag == Tth)
1932 c->align.halign = ALcenter;
1933 else
1934 c->align.halign = ALleft;
1936 if(c->align.valign == ALnone) {
1937 if(tr->align.valign != ALnone)
1938 c->align.valign = tr->align.valign;
1939 else
1940 c->align.valign = ALmiddle;
1942 c->nextinrow = tr->cells;
1943 tr->cells = c;
1944 break;
1946 case Ttd+RBRA:
1947 case Tth+RBRA:
1948 if(curtab == nil || curtab->cells == nil) {
1949 if(warn)
1950 fprint(2, "unexpected %T\n", tok);
1951 continue;
1953 ps = finishcell(curtab, ps);
1954 break;
1956 /* <!ELEMENT TEXTAREA - - ( //PCDATA)> */
1957 case Ttextarea:
1958 if(is->curform == nil) {
1959 if(warn)
1960 fprint(2, "<TEXTAREA> not inside <FORM>\n");
1961 continue;
1963 field = newformfield(Ftextarea,
1964 ++is->curform->nfields,
1965 is->curform,
1966 aval(tok, Aname),
1967 nil,
1970 is->curform->fields);
1971 is->curform->fields = field;
1972 field->rows = auintval(tok, Arows, 3);
1973 field->cols = auintval(tok, Acols, 50);
1974 field->value = getpcdata(toks, tokslen, &toki);
1975 if(warn && toki < tokslen - 1 && toks[toki + 1].tag != Ttextarea + RBRA)
1976 fprint(2, "warning: <TEXTAREA> data ended by %T\n", &toks[toki + 1]);
1977 ffit = newiformfield(field);
1978 additem(ps, ffit, tok);
1979 if(ffit->genattr != nil)
1980 field->events = ffit->genattr->events;
1981 break;
1983 /* <!ELEMENT TITLE - - ( //PCDATA)* -(%head.misc)> */
1984 case Ttitle:
1985 di->doctitle = getpcdata(toks, tokslen, &toki);
1986 if(warn && toki < tokslen - 1 && toks[toki + 1].tag != Ttitle + RBRA)
1987 fprint(2, "warning: <TITLE> data ended by %T\n", &toks[toki + 1]);
1988 break;
1990 /* <!ELEMENT TR - O (TH|TD)+> */
1991 /* rows are accumulated in reverse order in curtab->rows */
1992 case Ttr:
1993 if(curtab == nil) {
1994 if(warn)
1995 fprint(2, "warning: <TR> outside <TABLE>\n");
1996 continue;
1998 if(ps->inpar) {
1999 popjust(ps);
2000 ps->inpar = 0;
2002 ps = finishcell(curtab, ps);
2003 if(curtab->rows != nil)
2004 curtab->rows->flags = 0;
2005 curtab->rows = newtablerow(aalign(tok),
2006 makebackground(nil, acolorval(tok, Abgcolor, curtab->background.color)),
2007 TFparsing,
2008 curtab->rows);
2009 break;
2011 case Ttr+RBRA:
2012 if(curtab == nil || curtab->rows == nil) {
2013 if(warn)
2014 fprint(2, "warning: unexpected </TR>\n");
2015 continue;
2017 ps = finishcell(curtab, ps);
2018 tr = curtab->rows;
2019 if(tr->cells == nil) {
2020 if(warn)
2021 fprint(2, "warning: empty row\n");
2022 curtab->rows = tr->next;
2023 tr->next = nil;
2025 else
2026 tr->flags = 0;
2027 break;
2029 /* <!ELEMENT (TT|CODE|KBD|SAMP) - - (%text)*> */
2030 case Ttt:
2031 case Tcode:
2032 case Tkbd:
2033 case Tsamp:
2034 pushfontstyle(ps, FntT);
2035 break;
2037 /* Tags that have empty action */
2038 case Tabbr:
2039 case Tabbr+RBRA:
2040 case Tacronym:
2041 case Tacronym+RBRA:
2042 case Tarea+RBRA:
2043 case Tbase+RBRA:
2044 case Tbasefont+RBRA:
2045 case Tbr+RBRA:
2046 case Tdd+RBRA:
2047 case Tdt+RBRA:
2048 case Tframe+RBRA:
2049 case Thr+RBRA:
2050 case Thtml:
2051 case Thtml+RBRA:
2052 case Timg+RBRA:
2053 case Tinput+RBRA:
2054 case Tisindex+RBRA:
2055 case Tli+RBRA:
2056 case Tlink:
2057 case Tlink+RBRA:
2058 case Tmeta+RBRA:
2059 case Toption+RBRA:
2060 case Tparam+RBRA:
2061 case Ttextarea+RBRA:
2062 case Ttitle+RBRA:
2063 break;
2066 /* Tags not implemented */
2067 case Tbdo:
2068 case Tbdo+RBRA:
2069 case Tbutton:
2070 case Tbutton+RBRA:
2071 case Tdel:
2072 case Tdel+RBRA:
2073 case Tfieldset:
2074 case Tfieldset+RBRA:
2075 case Tiframe:
2076 case Tiframe+RBRA:
2077 case Tins:
2078 case Tins+RBRA:
2079 case Tlabel:
2080 case Tlabel+RBRA:
2081 case Tlegend:
2082 case Tlegend+RBRA:
2083 case Tobject:
2084 case Tobject+RBRA:
2085 case Toptgroup:
2086 case Toptgroup+RBRA:
2087 case Tspan:
2088 case Tspan+RBRA:
2089 if(warn) {
2090 if(tag > RBRA)
2091 tag -= RBRA;
2092 fprint(2, "warning: unimplemented HTML tag: %S\n", tagnames[tag]);
2094 break;
2096 default:
2097 if(warn)
2098 fprint(2, "warning: unknown HTML tag: %S\n", tok->text);
2099 break;
2102 /* some pages omit trailing </table> */
2103 while(curtab != nil) {
2104 if(warn)
2105 fprint(2, "warning: <TABLE> not closed\n");
2106 if(curtab->cells != nil) {
2107 ps = finishcell(curtab, ps);
2108 if(curtab->cells == nil) {
2109 if(warn)
2110 fprint(2, "warning: empty table\n");
2112 else {
2113 if(curtab->rows != nil)
2114 curtab->rows->flags = 0;
2115 finish_table(curtab);
2116 ps->skipping = 0;
2117 additem(ps, newitable(curtab), curtab->tabletok);
2118 addbrk(ps, 0, 0);
2121 if(is->tabstk != nil)
2122 is->tabstk = is->tabstk->next;
2123 curtab->next = di->tables;
2124 di->tables = curtab;
2125 curtab = is->tabstk;
2127 outerps = lastps(ps);
2128 ans = outerps->items->next;
2129 /* note: ans may be nil and di->kids not nil, if there's a frameset! */
2130 outerps->items = newispacer(ISPnull);
2131 outerps->lastit = outerps->items;
2132 is->psstk = ps;
2133 if(ans != nil && di->hasscripts) {
2134 /* TODO evalscript(nil); */
2138 return_ans:
2139 if(dbgbuild) {
2140 assert(validitems(ans));
2141 if(ans == nil)
2142 fprint(2, "getitems returning nil\n");
2143 else
2144 printitems(ans, "getitems returning:");
2146 return ans;
2149 /* Concatenate together maximal set of Data tokens, starting at toks[toki+1]. */
2150 /* Lexer has ensured that there will either be a following non-data token or */
2151 /* we will be at eof. */
2152 /* Return emallocd trimmed concatenation, and update *ptoki to last used toki */
2153 static Rune*
2154 getpcdata(Token* toks, int tokslen, int* ptoki)
2156 Rune* ans;
2157 Rune* p;
2158 Rune* trimans;
2159 int anslen;
2160 int trimanslen;
2161 int toki;
2162 Token* tok;
2164 ans = nil;
2165 anslen = 0;
2166 /* first find length of answer */
2167 toki = (*ptoki) + 1;
2168 while(toki < tokslen) {
2169 tok = &toks[toki];
2170 if(tok->tag == Data) {
2171 toki++;
2172 anslen += _Strlen(tok->text);
2174 else
2175 break;
2177 /* now make up the initial answer */
2178 if(anslen > 0) {
2179 ans = _newstr(anslen);
2180 p = ans;
2181 toki = (*ptoki) + 1;
2182 while(toki < tokslen) {
2183 tok = &toks[toki];
2184 if(tok->tag == Data) {
2185 toki++;
2186 p = _Stradd(p, tok->text, _Strlen(tok->text));
2188 else
2189 break;
2191 *p = 0;
2192 _trimwhite(ans, anslen, &trimans, &trimanslen);
2193 if(trimanslen != anslen) {
2194 p = ans;
2195 ans = _Strndup(trimans, trimanslen);
2196 free(p);
2199 *ptoki = toki-1;
2200 return ans;
2203 /* If still parsing head of curtab->cells list, finish it off */
2204 /* by transferring the items on the head of psstk to the cell. */
2205 /* Then pop the psstk and return the new psstk. */
2206 static Pstate*
2207 finishcell(Table* curtab, Pstate* psstk)
2209 Tablecell* c;
2210 Pstate* psstknext;
2212 c = curtab->cells;
2213 if(c != nil) {
2214 if((c->flags&TFparsing)) {
2215 psstknext = psstk->next;
2216 if(psstknext == nil) {
2217 if(warn)
2218 fprint(2, "warning: parse state stack is wrong\n");
2220 else {
2221 c->content = psstk->items->next;
2222 c->flags &= ~TFparsing;
2223 freepstate(psstk);
2224 psstk = psstknext;
2228 return psstk;
2231 /* Make a new Pstate for a cell, based on the old pstate, oldps. */
2232 /* Also, put the new ps on the head of the oldps stack. */
2233 static Pstate*
2234 cell_pstate(Pstate* oldps, int ishead)
2236 Pstate* ps;
2237 int sty;
2239 ps = newpstate(oldps);
2240 ps->skipwhite = 1;
2241 ps->curanchor = oldps->curanchor;
2242 copystack(&ps->fntstylestk, &oldps->fntstylestk);
2243 copystack(&ps->fntsizestk, &oldps->fntsizestk);
2244 ps->curfont = oldps->curfont;
2245 ps->curfg = oldps->curfg;
2246 ps->curbg = oldps->curbg;
2247 copystack(&ps->fgstk, &oldps->fgstk);
2248 ps->adjsize = oldps->adjsize;
2249 if(ishead) {
2250 sty = ps->curfont%NumSize;
2251 ps->curfont = FntB*NumSize + sty;
2253 return ps;
2256 /* Return a new Pstate with default starting state. */
2257 /* Use link to add it to head of a list, if any. */
2258 static Pstate*
2259 newpstate(Pstate* link)
2261 Pstate* ps;
2263 ps = (Pstate*)emalloc(sizeof(Pstate));
2264 ps->curfont = DefFnt;
2265 ps->curfg = Black;
2266 ps->curbg.image = nil;
2267 ps->curbg.color = White;
2268 ps->curul = ULnone;
2269 ps->curjust = ALleft;
2270 ps->curstate = IFwrap;
2271 ps->items = newispacer(ISPnull);
2272 ps->lastit = ps->items;
2273 ps->prelastit = nil;
2274 ps->next = link;
2275 return ps;
2278 /* Return last Pstate on psl list */
2279 static Pstate*
2280 lastps(Pstate* psl)
2282 assert(psl != nil);
2283 while(psl->next != nil)
2284 psl = psl->next;
2285 return psl;
2288 /* Add it to end of ps item chain, adding in current state from ps. */
2289 /* Also, if tok is not nil, scan it for generic attributes and assign */
2290 /* the genattr field of the item accordingly. */
2291 static void
2292 additem(Pstate* ps, Item* it, Token* tok)
2294 int aid;
2295 int any;
2296 Rune* i;
2297 Rune* c;
2298 Rune* s;
2299 Rune* t;
2300 Attr* a;
2301 SEvent* e;
2303 if(ps->skipping) {
2304 if(warn)
2305 fprint(2, "warning: skipping item: %I\n", it);
2306 return;
2308 it->anchorid = ps->curanchor;
2309 it->state |= ps->curstate;
2310 if(tok != nil) {
2311 any = 0;
2312 i = nil;
2313 c = nil;
2314 s = nil;
2315 t = nil;
2316 e = nil;
2317 for(a = tok->attr; a != nil; a = a->next) {
2318 aid = a->attid;
2319 if(!attrinfo[aid])
2320 continue;
2321 switch(aid) {
2322 case Aid:
2323 i = a->value;
2324 break;
2326 case Aclass:
2327 c = a->value;
2328 break;
2330 case Astyle:
2331 s = a->value;
2332 break;
2334 case Atitle:
2335 t = a->value;
2336 break;
2338 default:
2339 assert(aid >= Aonblur && aid <= Aonunload);
2340 e = newscriptevent(scriptev[a->attid], a->value, e);
2341 break;
2343 a->value = nil;
2344 any = 1;
2346 if(any)
2347 it->genattr = newgenattr(i, c, s, t, e);
2349 ps->curstate &= ~(IFbrk|IFbrksp|IFnobrk|IFcleft|IFcright);
2350 ps->prelastit = ps->lastit;
2351 ps->lastit->next = it;
2352 ps->lastit = it;
2355 /* Make a text item out of s, */
2356 /* using current font, foreground, vertical offset and underline state. */
2357 static Item*
2358 textit(Pstate* ps, Rune* s)
2360 assert(s != nil);
2361 return newitext(s, ps->curfont, ps->curfg, ps->curvoff + Voffbias, ps->curul);
2364 /* Add text item or items for s, paying attention to */
2365 /* current font, foreground, baseline offset, underline state, */
2366 /* and literal mode. Unless we're in literal mode, compress */
2367 /* whitespace to single blank, and, if curstate has a break, */
2368 /* trim any leading whitespace. Whether in literal mode or not, */
2369 /* turn nonbreaking spaces into spacer items with IFnobrk set. */
2370 /* */
2371 /* In literal mode, break up s at newlines and add breaks instead. */
2372 /* Also replace tabs appropriate number of spaces. */
2373 /* In nonliteral mode, break up the items every 100 or so characters */
2374 /* just to make the layout algorithm not go quadratic. */
2375 /* */
2376 /* addtext assumes ownership of s. */
2377 static void
2378 addtext(Pstate* ps, Rune* s)
2380 int n;
2381 int i;
2382 int j;
2383 int k;
2384 int col;
2385 int c;
2386 int nsp;
2387 Item* it;
2388 Rune* ss;
2389 Rune* p;
2390 Rune buf[SMALLBUFSIZE];
2392 assert(s != nil);
2393 n = runestrlen(s);
2394 i = 0;
2395 j = 0;
2396 if(ps->literal) {
2397 col = 0;
2398 while(i < n) {
2399 if(s[i] == '\n') {
2400 if(i > j) {
2401 /* trim trailing blanks from line */
2402 for(k = i; k > j; k--)
2403 if(s[k - 1] != ' ')
2404 break;
2405 if(k > j)
2406 additem(ps, textit(ps, _Strndup(s+j, k-j)), nil);
2408 addlinebrk(ps, 0);
2409 j = i + 1;
2410 col = 0;
2412 else {
2413 if(s[i] == '\t') {
2414 col += i - j;
2415 nsp = 8 - (col%8);
2416 /* make ss = s[j:i] + nsp spaces */
2417 ss = _newstr(i-j+nsp);
2418 p = _Stradd(ss, s+j, i-j);
2419 p = _Stradd(p, L(Ltab2space), nsp);
2420 *p = 0;
2421 additem(ps, textit(ps, ss), nil);
2422 col += nsp;
2423 j = i + 1;
2425 else if(s[i] == NBSP) {
2426 if(i > j)
2427 additem(ps, textit(ps, _Strndup(s+j, i-j)), nil);
2428 addnbsp(ps);
2429 col += (i - j) + 1;
2430 j = i + 1;
2433 i++;
2435 if(i > j) {
2436 if(j == 0 && i == n) {
2437 /* just transfer s over */
2438 additem(ps, textit(ps, s), nil);
2440 else {
2441 additem(ps, textit(ps, _Strndup(s+j, i-j)), nil);
2442 free(s);
2446 else { /* not literal mode */
2447 if((ps->curstate&IFbrk) || ps->lastit == ps->items)
2448 while(i < n) {
2449 c = s[i];
2450 if(c >= 256 || !isspace(c))
2451 break;
2452 i++;
2454 p = buf;
2455 for(j = i; i < n; i++) {
2456 assert(p+i-j < buf+SMALLBUFSIZE-1);
2457 c = s[i];
2458 if(c == NBSP) {
2459 if(i > j)
2460 p = _Stradd(p, s+j, i-j);
2461 if(p > buf)
2462 additem(ps, textit(ps, _Strndup(buf, p-buf)), nil);
2463 p = buf;
2464 addnbsp(ps);
2465 j = i + 1;
2466 continue;
2468 if(c < 256 && isspace(c)) {
2469 if(i > j)
2470 p = _Stradd(p, s+j, i-j);
2471 *p++ = ' ';
2472 while(i < n - 1) {
2473 c = s[i + 1];
2474 if(c >= 256 || !isspace(c))
2475 break;
2476 i++;
2478 j = i + 1;
2480 if(i - j >= 100) {
2481 p = _Stradd(p, s+j, i+1-j);
2482 j = i + 1;
2484 if(p-buf >= 100) {
2485 additem(ps, textit(ps, _Strndup(buf, p-buf)), nil);
2486 p = buf;
2489 if(i > j && j < n) {
2490 assert(p+i-j < buf+SMALLBUFSIZE-1);
2491 p = _Stradd(p, s+j, i-j);
2493 /* don't add a space if previous item ended in a space */
2494 if(p-buf == 1 && buf[0] == ' ' && ps->lastit != nil) {
2495 it = ps->lastit;
2496 if(it->tag == Itexttag) {
2497 ss = ((Itext*)it)->s;
2498 k = _Strlen(ss);
2499 if(k > 0 && ss[k] == ' ')
2500 p = buf;
2503 if(p > buf)
2504 additem(ps, textit(ps, _Strndup(buf, p-buf)), nil);
2505 free(s);
2509 /* Add a break to ps->curstate, with extra space if sp is true. */
2510 /* If there was a previous break, combine this one's parameters */
2511 /* with that to make the amt be the max of the two and the clr */
2512 /* be the most general. (amt will be 0 or 1) */
2513 /* Also, if the immediately preceding item was a text item, */
2514 /* trim any whitespace from the end of it, if not in literal mode. */
2515 /* Finally, if this is at the very beginning of the item list */
2516 /* (the only thing there is a null spacer), then don't add the space. */
2517 static void
2518 addbrk(Pstate* ps, int sp, int clr)
2520 int state;
2521 Rune* l;
2522 int nl;
2523 Rune* r;
2524 int nr;
2525 Itext* t;
2526 Rune* s;
2528 state = ps->curstate;
2529 clr = clr|(state&(IFcleft|IFcright));
2530 if(sp && !(ps->lastit == ps->items))
2531 sp = IFbrksp;
2532 else
2533 sp = 0;
2534 ps->curstate = IFbrk|sp|(state&~(IFcleft|IFcright))|clr;
2535 if(ps->lastit != ps->items) {
2536 if(!ps->literal && ps->lastit->tag == Itexttag) {
2537 t = (Itext*)ps->lastit;
2538 _splitr(t->s, _Strlen(t->s), notwhitespace, &l, &nl, &r, &nr);
2539 /* try to avoid making empty items */
2540 /* but not crucial f the occasional one gets through */
2541 if(nl == 0 && ps->prelastit != nil) {
2542 ps->lastit = ps->prelastit;
2543 ps->lastit->next = nil;
2544 ps->prelastit = nil;
2546 else {
2547 s = t->s;
2548 if(nl == 0) {
2549 /* need a non-nil pointer to empty string */
2550 /* (_Strdup(L(Lempty)) returns nil) */
2551 t->s = emalloc(sizeof(Rune));
2552 t->s[0] = 0;
2554 else
2555 t->s = _Strndup(l, nl);
2556 if(s)
2557 free(s);
2563 /* Add break due to a <br> or a newline within a preformatted section. */
2564 /* We add a null item first, with current font's height and ascent, to make */
2565 /* sure that the current line takes up at least that amount of vertical space. */
2566 /* This ensures that <br>s on empty lines cause blank lines, and that */
2567 /* multiple <br>s in a row give multiple blank lines. */
2568 /* However don't add the spacer if the previous item was something that */
2569 /* takes up space itself. */
2570 static void
2571 addlinebrk(Pstate* ps, int clr)
2573 int obrkstate;
2574 int b;
2575 int addit;
2577 /* don't want break before our null item unless the previous item */
2578 /* was also a null item for the purposes of line breaking */
2579 obrkstate = ps->curstate&(IFbrk|IFbrksp);
2580 b = IFnobrk;
2581 addit = 0;
2582 if(ps->lastit != nil) {
2583 if(ps->lastit->tag == Ispacertag) {
2584 if(((Ispacer*)ps->lastit)->spkind == ISPvline)
2585 b = IFbrk;
2586 addit = 1;
2588 else if(ps->lastit->tag == Ifloattag)
2589 addit = 1;
2591 if(addit) {
2592 ps->curstate = (ps->curstate&~(IFbrk|IFbrksp))|b;
2593 additem(ps, newispacer(ISPvline), nil);
2594 ps->curstate = (ps->curstate&~(IFbrk|IFbrksp))|obrkstate;
2596 addbrk(ps, 0, clr);
2599 /* Add a nonbreakable space */
2600 static void
2601 addnbsp(Pstate* ps)
2603 /* if nbsp comes right where a break was specified, */
2604 /* do the break anyway (nbsp is being used to generate undiscardable */
2605 /* space rather than to prevent a break) */
2606 if((ps->curstate&IFbrk) == 0)
2607 ps->curstate |= IFnobrk;
2608 additem(ps, newispacer(ISPhspace), nil);
2609 /* but definitely no break on next item */
2610 ps->curstate |= IFnobrk;
2613 /* Change hang in ps.curstate by delta. */
2614 /* The amount is in 1/10ths of tabs, and is the amount that */
2615 /* the current contiguous set of items with a hang value set */
2616 /* is to be shifted left from its normal (indented) place. */
2617 static void
2618 changehang(Pstate* ps, int delta)
2620 int amt;
2622 amt = (ps->curstate&IFhangmask) + delta;
2623 if(amt < 0) {
2624 if(warn)
2625 fprint(2, "warning: hang went negative\n");
2626 amt = 0;
2628 ps->curstate = (ps->curstate&~IFhangmask)|amt;
2631 /* Change indent in ps.curstate by delta. */
2632 static void
2633 changeindent(Pstate* ps, int delta)
2635 int amt;
2637 amt = ((ps->curstate&IFindentmask) >> IFindentshift) + delta;
2638 if(amt < 0) {
2639 if(warn)
2640 fprint(2, "warning: indent went negative\n");
2641 amt = 0;
2643 ps->curstate = (ps->curstate&~IFindentmask)|(amt << IFindentshift);
2646 /* Push val on top of stack, and also return value pushed */
2647 static int
2648 push(Stack* stk, int val)
2650 if(stk->n == Nestmax) {
2651 if(warn)
2652 fprint(2, "warning: build stack overflow\n");
2654 else
2655 stk->slots[stk->n++] = val;
2656 return val;
2659 /* Pop top of stack */
2660 static void
2661 pop(Stack* stk)
2663 if(stk->n > 0)
2664 --stk->n;
2667 /*Return top of stack, using dflt if stack is empty */
2668 static int
2669 top(Stack* stk, int dflt)
2671 if(stk->n == 0)
2672 return dflt;
2673 return stk->slots[stk->n-1];
2676 /* pop, then return new top, with dflt if empty */
2677 static int
2678 popretnewtop(Stack* stk, int dflt)
2680 if(stk->n == 0)
2681 return dflt;
2682 stk->n--;
2683 if(stk->n == 0)
2684 return dflt;
2685 return stk->slots[stk->n-1];
2688 /* Copy fromstk entries into tostk */
2689 static void
2690 copystack(Stack* tostk, Stack* fromstk)
2692 int n;
2694 n = fromstk->n;
2695 tostk->n = n;
2696 memmove(tostk->slots, fromstk->slots, n*sizeof(int));
2699 static void
2700 popfontstyle(Pstate* ps)
2702 pop(&ps->fntstylestk);
2703 setcurfont(ps);
2706 static void
2707 pushfontstyle(Pstate* ps, int sty)
2709 push(&ps->fntstylestk, sty);
2710 setcurfont(ps);
2713 static void
2714 popfontsize(Pstate* ps)
2716 pop(&ps->fntsizestk);
2717 setcurfont(ps);
2720 static void
2721 pushfontsize(Pstate* ps, int sz)
2723 push(&ps->fntsizestk, sz);
2724 setcurfont(ps);
2727 static void
2728 setcurfont(Pstate* ps)
2730 int sty;
2731 int sz;
2733 sty = top(&ps->fntstylestk, FntR);
2734 sz = top(&ps->fntsizestk, Normal);
2735 if(sz < Tiny)
2736 sz = Tiny;
2737 if(sz > Verylarge)
2738 sz = Verylarge;
2739 ps->curfont = sty*NumSize + sz;
2742 static void
2743 popjust(Pstate* ps)
2745 pop(&ps->juststk);
2746 setcurjust(ps);
2749 static void
2750 pushjust(Pstate* ps, int j)
2752 push(&ps->juststk, j);
2753 setcurjust(ps);
2756 static void
2757 setcurjust(Pstate* ps)
2759 int j;
2760 int state;
2762 j = top(&ps->juststk, ALleft);
2763 if(j != ps->curjust) {
2764 ps->curjust = j;
2765 state = ps->curstate;
2766 state &= ~(IFrjust|IFcjust);
2767 if(j == ALcenter)
2768 state |= IFcjust;
2769 else if(j == ALright)
2770 state |= IFrjust;
2771 ps->curstate = state;
2775 /* Do final rearrangement after table parsing is finished */
2776 /* and assign cells to grid points */
2777 static void
2778 finish_table(Table* t)
2780 int ncol;
2781 int nrow;
2782 int r;
2783 Tablerow* rl;
2784 Tablecell* cl;
2785 int* rowspancnt;
2786 Tablecell** rowspancell;
2787 int ri;
2788 int ci;
2789 Tablecell* c;
2790 Tablecell* cnext;
2791 Tablerow* row;
2792 Tablerow* rownext;
2793 int rcols;
2794 int newncol;
2795 int k;
2796 int j;
2797 int cspan;
2798 int rspan;
2799 int i;
2801 rl = t->rows;
2802 t->nrow = nrow = _listlen((List*)rl);
2803 t->rows = (Tablerow*)emalloc(nrow * sizeof(Tablerow));
2804 ncol = 0;
2805 r = nrow - 1;
2806 for(row = rl; row != nil; row = rownext) {
2807 /* copy the data from the allocated Tablerow into the array slot */
2808 t->rows[r] = *row;
2809 rownext = row->next;
2810 row = &t->rows[r];
2811 r--;
2812 rcols = 0;
2813 c = row->cells;
2815 /* If rowspan is > 1 but this is the last row, */
2816 /* reset the rowspan */
2817 if(c != nil && c->rowspan > 1 && r == nrow-2)
2818 c->rowspan = 1;
2820 /* reverse row->cells list (along nextinrow pointers) */
2821 row->cells = nil;
2822 while(c != nil) {
2823 cnext = c->nextinrow;
2824 c->nextinrow = row->cells;
2825 row->cells = c;
2826 rcols += c->colspan;
2827 c = cnext;
2829 if(rcols > ncol)
2830 ncol = rcols;
2832 t->ncol = ncol;
2833 t->cols = (Tablecol*)emalloc(ncol * sizeof(Tablecol));
2835 /* Reverse cells just so they are drawn in source order. */
2836 /* Also, trim their contents so they don't end in whitespace. */
2837 t->cells = (Tablecell*)_revlist((List*)t->cells);
2838 for(c = t->cells; c != nil; c= c->next)
2839 trim_cell(c);
2840 t->grid = (Tablecell***)emalloc(nrow * sizeof(Tablecell**));
2841 for(i = 0; i < nrow; i++)
2842 t->grid[i] = (Tablecell**)emalloc(ncol * sizeof(Tablecell*));
2844 /* The following arrays keep track of cells that are spanning */
2845 /* multiple rows; rowspancnt[i] is the number of rows left */
2846 /* to be spanned in column i. */
2847 /* When done, cell's (row,col) is upper left grid point. */
2848 rowspancnt = (int*)emalloc(ncol * sizeof(int));
2849 rowspancell = (Tablecell**)emalloc(ncol * sizeof(Tablecell*));
2850 for(ri = 0; ri < nrow; ri++) {
2851 row = &t->rows[ri];
2852 cl = row->cells;
2853 ci = 0;
2854 while(ci < ncol || cl != nil) {
2855 if(ci < ncol && rowspancnt[ci] > 0) {
2856 t->grid[ri][ci] = rowspancell[ci];
2857 rowspancnt[ci]--;
2858 ci++;
2860 else {
2861 if(cl == nil) {
2862 ci++;
2863 continue;
2865 c = cl;
2866 cl = cl->nextinrow;
2867 cspan = c->colspan;
2868 rspan = c->rowspan;
2869 if(ci + cspan > ncol) {
2870 /* because of row spanning, we calculated */
2871 /* ncol incorrectly; adjust it */
2872 newncol = ci + cspan;
2873 t->cols = (Tablecol*)erealloc(t->cols, newncol * sizeof(Tablecol));
2874 rowspancnt = (int*)erealloc(rowspancnt, newncol * sizeof(int));
2875 rowspancell = (Tablecell**)erealloc(rowspancell, newncol * sizeof(Tablecell*));
2876 k = newncol-ncol;
2877 memset(t->cols+ncol, 0, k*sizeof(Tablecol));
2878 memset(rowspancnt+ncol, 0, k*sizeof(int));
2879 memset(rowspancell+ncol, 0, k*sizeof(Tablecell*));
2880 for(j = 0; j < nrow; j++) {
2881 t->grid[j] = (Tablecell**)erealloc(t->grid[j], newncol * sizeof(Tablecell*));
2882 memset(t->grid[j], 0, k*sizeof(Tablecell*));
2884 t->ncol = ncol = newncol;
2886 c->row = ri;
2887 c->col = ci;
2888 for(i = 0; i < cspan; i++) {
2889 t->grid[ri][ci] = c;
2890 if(rspan > 1) {
2891 rowspancnt[ci] = rspan - 1;
2892 rowspancell[ci] = c;
2894 ci++;
2901 /* Remove tail of cell content until it isn't whitespace. */
2902 static void
2903 trim_cell(Tablecell* c)
2905 int dropping;
2906 Rune* s;
2907 Rune* x;
2908 Rune* y;
2909 int nx;
2910 int ny;
2911 Item* p;
2912 Itext* q;
2913 Item* pprev;
2915 dropping = 1;
2916 while(c->content != nil && dropping) {
2917 p = c->content;
2918 pprev = nil;
2919 while(p->next != nil) {
2920 pprev = p;
2921 p = p->next;
2923 dropping = 0;
2924 if(!(p->state&IFnobrk)) {
2925 if(p->tag == Itexttag) {
2926 q = (Itext*)p;
2927 s = q->s;
2928 _splitr(s, _Strlen(s), notwhitespace, &x, &nx, &y, &ny);
2929 if(nx != 0 && ny != 0) {
2930 q->s = _Strndup(x, nx);
2931 free(s);
2933 break;
2936 if(dropping) {
2937 if(pprev == nil)
2938 c->content = nil;
2939 else
2940 pprev->next = nil;
2941 freeitem(p);
2946 /* Caller must free answer (eventually). */
2947 static Rune*
2948 listmark(uchar ty, int n)
2950 Rune* s;
2951 Rune* t;
2952 int n2;
2953 int i;
2955 s = nil;
2956 switch(ty) {
2957 case LTdisc:
2958 case LTsquare:
2959 case LTcircle:
2960 s = _newstr(1);
2961 s[0] = (ty == LTdisc)? 0x2022 /* bullet */
2962 : ((ty == LTsquare)? 0x220e /* filled square */
2963 : 0x2218); /* degree */
2964 s[1] = 0;
2965 break;
2967 case LT1:
2968 t = _ltoStr(n);
2969 n2 = _Strlen(t);
2970 s = _newstr(n2+1);
2971 t = _Stradd(s, t, n2);
2972 *t++ = '.';
2973 *t = 0;
2974 break;
2976 case LTa:
2977 case LTA:
2978 n--;
2979 i = 0;
2980 if(n < 0)
2981 n = 0;
2982 s = _newstr((n <= 25)? 2 : 3);
2983 if(n > 25) {
2984 n2 = n%26;
2985 n /= 26;
2986 if(n2 > 25)
2987 n2 = 25;
2988 s[i++] = n2 + (ty == LTa)? 'a' : 'A';
2990 s[i++] = n + (ty == LTa)? 'a' : 'A';
2991 s[i++] = '.';
2992 s[i] = 0;
2993 break;
2995 case LTi:
2996 case LTI:
2997 if(n >= NROMAN) {
2998 if(warn)
2999 fprint(2, "warning: unimplemented roman number > %d\n", NROMAN);
3000 n = NROMAN;
3002 t = roman[n - 1];
3003 n2 = _Strlen(t);
3004 s = _newstr(n2+1);
3005 for(i = 0; i < n2; i++)
3006 s[i] = (ty == LTi)? tolower(t[i]) : t[i];
3007 s[i++] = '.';
3008 s[i] = 0;
3009 break;
3011 return s;
3014 /* Find map with given name in di.maps. */
3015 /* If not there, add one, copying name. */
3016 /* Ownership of map remains with di->maps list. */
3017 static Map*
3018 getmap(Docinfo* di, Rune* name)
3020 Map* m;
3022 for(m = di->maps; m != nil; m = m->next) {
3023 if(!_Strcmp(name, m->name))
3024 return m;
3026 m = (Map*)emalloc(sizeof(Map));
3027 m->name = _Strdup(name);
3028 m->areas = nil;
3029 m->next = di->maps;
3030 di->maps = m;
3031 return m;
3034 /* Transfers ownership of href to Area */
3035 static Area*
3036 newarea(int shape, Rune* href, int target, Area* link)
3038 Area* a;
3040 a = (Area*)emalloc(sizeof(Area));
3041 a->shape = shape;
3042 a->href = href;
3043 a->target = target;
3044 a->next = link;
3045 return a;
3048 /* Return string value associated with attid in tok, nil if none. */
3049 /* Caller must free the result (eventually). */
3050 static Rune*
3051 aval(Token* tok, int attid)
3053 Rune* ans;
3055 _tokaval(tok, attid, &ans, 1); /* transfers string ownership from token to ans */
3056 return ans;
3059 /* Like aval, but use dflt if there was no such attribute in tok. */
3060 /* Caller must free the result (eventually). */
3061 static Rune*
3062 astrval(Token* tok, int attid, Rune* dflt)
3064 Rune* ans;
3066 if(_tokaval(tok, attid, &ans, 1))
3067 return ans; /* transfers string ownership from token to ans */
3068 else
3069 return _Strdup(dflt);
3072 /* Here we're supposed to convert to an int, */
3073 /* and have a default when not found */
3074 static int
3075 aintval(Token* tok, int attid, int dflt)
3077 Rune* ans;
3079 if(!_tokaval(tok, attid, &ans, 0) || ans == nil)
3080 return dflt;
3081 else
3082 return toint(ans);
3085 /* Like aintval, but result should be >= 0 */
3086 static int
3087 auintval(Token* tok, int attid, int dflt)
3089 Rune* ans;
3090 int v;
3092 if(!_tokaval(tok, attid, &ans, 0) || ans == nil)
3093 return dflt;
3094 else {
3095 v = toint(ans);
3096 return v >= 0? v : 0;
3100 /* int conversion, but with possible error check (if warning) */
3101 static int
3102 toint(Rune* s)
3104 int ans;
3105 Rune* eptr;
3107 ans = _Strtol(s, &eptr, 10);
3108 if(warn) {
3109 if(*eptr != 0) {
3110 eptr = _Strclass(eptr, notwhitespace);
3111 if(eptr != nil)
3112 fprint(2, "warning: expected integer, got %S\n", s);
3115 return ans;
3118 /* Attribute value when need a table to convert strings to ints */
3119 static int
3120 atabval(Token* tok, int attid, StringInt* tab, int ntab, int dflt)
3122 Rune* aval;
3123 int ans;
3125 ans = dflt;
3126 if(_tokaval(tok, attid, &aval, 0)) {
3127 if(!_lookup(tab, ntab, aval, _Strlen(aval), &ans)) {
3128 ans = dflt;
3129 if(warn)
3130 fprint(2, "warning: name not found in table lookup: %S\n", aval);
3133 return ans;
3136 /* Attribute value when supposed to be a color */
3137 static int
3138 acolorval(Token* tok, int attid, int dflt)
3140 Rune* aval;
3141 int ans;
3143 ans = dflt;
3144 if(_tokaval(tok, attid, &aval, 0))
3145 ans = color(aval, dflt);
3146 return ans;
3149 /* Attribute value when supposed to be a target frame name */
3150 static int
3151 atargval(Token* tok, int dflt)
3153 int ans;
3154 Rune* aval;
3156 ans = dflt;
3157 if(_tokaval(tok, Atarget, &aval, 0)){
3158 ans = targetid(aval);
3160 return ans;
3163 /* special for list types, where "i" and "I" are different, */
3164 /* but "square" and "SQUARE" are the same */
3165 static int
3166 listtyval(Token* tok, int dflt)
3168 Rune* aval;
3169 int ans;
3170 int n;
3172 ans = dflt;
3173 if(_tokaval(tok, Atype, &aval, 0)) {
3174 n = _Strlen(aval);
3175 if(n == 1) {
3176 switch(aval[0]) {
3177 case '1':
3178 ans = LT1;
3179 break;
3180 case 'A':
3181 ans = LTA;
3182 break;
3183 case 'I':
3184 ans = LTI;
3185 break;
3186 case 'a':
3187 ans = LTa;
3188 break;
3189 case 'i':
3190 ans = LTi;
3191 default:
3192 if(warn)
3193 fprint(2, "warning: unknown list element type %c\n", aval[0]);
3196 else {
3197 if(!_Strncmpci(aval, n, L(Lcircle)))
3198 ans = LTcircle;
3199 else if(!_Strncmpci(aval, n, L(Ldisc)))
3200 ans = LTdisc;
3201 else if(!_Strncmpci(aval, n, L(Lsquare)))
3202 ans = LTsquare;
3203 else {
3204 if(warn)
3205 fprint(2, "warning: unknown list element type %S\n", aval);
3209 return ans;
3212 /* Attribute value when value is a URL, possibly relative to base. */
3213 /* FOR NOW: leave the url relative. */
3214 /* Caller must free the result (eventually). */
3215 static Rune*
3216 aurlval(Token* tok, int attid, Rune* dflt, Rune* base)
3218 Rune* ans;
3219 Rune* url;
3221 USED(base);
3222 ans = nil;
3223 if(_tokaval(tok, attid, &url, 0) && url != nil)
3224 ans = removeallwhite(url);
3225 if(ans == nil)
3226 ans = _Strdup(dflt);
3227 return ans;
3230 /* Return copy of s but with all whitespace (even internal) removed. */
3231 /* This fixes some buggy URL specification strings. */
3232 static Rune*
3233 removeallwhite(Rune* s)
3235 int j;
3236 int n;
3237 int i;
3238 int c;
3239 Rune* ans;
3241 j = 0;
3242 n = _Strlen(s);
3243 for(i = 0; i < n; i++) {
3244 c = s[i];
3245 if(c >= 256 || !isspace(c))
3246 j++;
3248 if(j < n) {
3249 ans = _newstr(j);
3250 j = 0;
3251 for(i = 0; i < n; i++) {
3252 c = s[i];
3253 if(c >= 256 || !isspace(c))
3254 ans[j++] = c;
3256 ans[j] = 0;
3258 else
3259 ans = _Strdup(s);
3260 return ans;
3263 /* Attribute value when mere presence of attr implies value of 1, */
3264 /* but if there is an integer there, return it as the value. */
3265 static int
3266 aflagval(Token* tok, int attid)
3268 int val;
3269 Rune* sval;
3271 val = 0;
3272 if(_tokaval(tok, attid, &sval, 0)) {
3273 val = 1;
3274 if(sval != nil)
3275 val = toint(sval);
3277 return val;
3280 static Align
3281 makealign(int halign, int valign)
3283 Align al;
3285 al.halign = halign;
3286 al.valign = valign;
3287 return al;
3290 /* Make an Align (two alignments, horizontal and vertical) */
3291 static Align
3292 aalign(Token* tok)
3294 return makealign(
3295 atabval(tok, Aalign, align_tab, NALIGNTAB, ALnone),
3296 atabval(tok, Avalign, align_tab, NALIGNTAB, ALnone));
3299 /* Make a Dimen, based on value of attid attr */
3300 static Dimen
3301 adimen(Token* tok, int attid)
3303 Rune* wd;
3305 if(_tokaval(tok, attid, &wd, 0))
3306 return parsedim(wd, _Strlen(wd));
3307 else
3308 return makedimen(Dnone, 0);
3311 /* Parse s[0:n] as num[.[num]][unit][%|*] */
3312 static Dimen
3313 parsedim(Rune* s, int ns)
3315 int kind;
3316 int spec;
3317 Rune* l;
3318 int nl;
3319 Rune* r;
3320 int nr;
3321 int mul;
3322 int i;
3323 Rune* f;
3324 int nf;
3325 int Tkdpi;
3326 Rune* units;
3328 kind = Dnone;
3329 spec = 0;
3330 _splitl(s, ns, L(Lnot0to9), &l, &nl, &r, &nr);
3331 if(nl != 0) {
3332 spec = 1000*_Strtol(l, nil, 10);
3333 if(nr > 0 && r[0] == '.') {
3334 _splitl(r+1, nr-1, L(Lnot0to9), &f, &nf, &r, &nr);
3335 if(nf != 0) {
3336 mul = 100;
3337 for(i = 0; i < nf; i++) {
3338 spec = spec + mul*(f[i]-'0');
3339 mul = mul/10;
3343 kind = Dpixels;
3344 if(nr != 0) {
3345 if(nr >= 2) {
3346 Tkdpi = 100;
3347 units = r;
3348 r = r+2;
3349 nr -= 2;
3350 if(!_Strncmpci(units, 2, L(Lpt)))
3351 spec = (spec*Tkdpi)/72;
3352 else if(!_Strncmpci(units, 2, L(Lpi)))
3353 spec = (spec*12*Tkdpi)/72;
3354 else if(!_Strncmpci(units, 2, L(Lin)))
3355 spec = spec*Tkdpi;
3356 else if(!_Strncmpci(units, 2, L(Lcm)))
3357 spec = (spec*100*Tkdpi)/254;
3358 else if(!_Strncmpci(units, 2, L(Lmm)))
3359 spec = (spec*10*Tkdpi)/254;
3360 else if(!_Strncmpci(units, 2, L(Lem)))
3361 spec = spec*15;
3362 else {
3363 if(warn)
3364 fprint(2, "warning: unknown units %C%Cs\n", units[0], units[1]);
3367 if(nr >= 1) {
3368 if(r[0] == '%')
3369 kind = Dpercent;
3370 else if(r[0] == '*')
3371 kind = Drelative;
3374 spec = spec/1000;
3376 else if(nr == 1 && r[0] == '*') {
3377 spec = 1;
3378 kind = Drelative;
3380 return makedimen(kind, spec);
3383 static void
3384 setdimarray(Token* tok, int attid, Dimen** pans, int* panslen)
3386 Rune* s;
3387 Dimen* d;
3388 int k;
3389 int nc;
3390 Rune* a[SMALLBUFSIZE];
3391 int an[SMALLBUFSIZE];
3393 if(_tokaval(tok, attid, &s, 0)) {
3394 nc = _splitall(s, _Strlen(s), L(Lcommaspace), a, an, SMALLBUFSIZE);
3395 if(nc > 0) {
3396 d = (Dimen*)emalloc(nc * sizeof(Dimen));
3397 for(k = 0; k < nc; k++) {
3398 d[k] = parsedim(a[k], an[k]);
3400 *pans = d;
3401 *panslen = nc;
3402 return;
3405 *pans = nil;
3406 *panslen = 0;
3409 static Background
3410 makebackground(Rune* imageurl, int color)
3412 Background bg;
3414 bg.image = imageurl;
3415 bg.color = color;
3416 return bg;
3419 static Item*
3420 newitext(Rune* s, int fnt, int fg, int voff, int ul)
3422 Itext* t;
3424 assert(s != nil);
3425 t = (Itext*)emalloc(sizeof(Itext));
3426 t->item.tag = Itexttag;
3427 t->s = s;
3428 t->fnt = fnt;
3429 t->fg = fg;
3430 t->voff = voff;
3431 t->ul = ul;
3432 return (Item*)t;
3435 static Item*
3436 newirule(int align, int size, int noshade, Dimen wspec)
3438 Irule* r;
3440 r = (Irule*)emalloc(sizeof(Irule));
3441 r->item.tag = Iruletag;
3442 r->align = align;
3443 r->size = size;
3444 r->noshade = noshade;
3445 r->wspec = wspec;
3446 return (Item*)r;
3449 /* Map is owned elsewhere. */
3450 static Item*
3451 newiimage(Rune* src, Rune* altrep, int align, int width, int height,
3452 int hspace, int vspace, int border, int ismap, Map* map)
3454 Iimage* i;
3455 int state;
3457 state = 0;
3458 if(ismap)
3459 state = IFsmap;
3460 i = (Iimage*)emalloc(sizeof(Iimage));
3461 i->item.tag = Iimagetag;
3462 i->item.state = state;
3463 i->imsrc = src;
3464 i->altrep = altrep;
3465 i->align = align;
3466 i->imwidth = width;
3467 i->imheight = height;
3468 i->hspace = hspace;
3469 i->vspace = vspace;
3470 i->border = border;
3471 i->map = map;
3472 i->ctlid = -1;
3473 return (Item*)i;
3476 static Item*
3477 newiformfield(Formfield* ff)
3479 Iformfield* f;
3481 f = (Iformfield*)emalloc(sizeof(Iformfield));
3482 f->item.tag = Iformfieldtag;
3483 f->formfield = ff;
3484 return (Item*)f;
3487 static Item*
3488 newitable(Table* tab)
3490 Itable* t;
3492 t = (Itable*)emalloc(sizeof(Itable));
3493 t->item.tag = Itabletag;
3494 t->table = tab;
3495 return (Item*)t;
3498 static Item*
3499 newifloat(Item* it, int side)
3501 Ifloat* f;
3503 f = (Ifloat*)emalloc(sizeof(Ifloat));
3504 f->_item.tag = Ifloattag;
3505 f->_item.state = IFwrap;
3506 f->item = it;
3507 f->side = side;
3508 return (Item*)f;
3511 static Item*
3512 newispacer(int spkind)
3514 Ispacer* s;
3516 s = (Ispacer*)emalloc(sizeof(Ispacer));
3517 s->item.tag = Ispacertag;
3518 s->spkind = spkind;
3519 return (Item*)s;
3522 /* Free one item (caller must deal with next pointer) */
3523 static void
3524 freeitem(Item* it)
3526 Iimage* ii;
3527 Genattr* ga;
3529 if(it == nil)
3530 return;
3532 switch(it->tag) {
3533 case Itexttag:
3534 free(((Itext*)it)->s);
3535 break;
3536 case Iimagetag:
3537 ii = (Iimage*)it;
3538 free(ii->imsrc);
3539 free(ii->altrep);
3540 break;
3541 case Iformfieldtag:
3542 freeformfield(((Iformfield*)it)->formfield);
3543 break;
3544 case Itabletag:
3545 freetable(((Itable*)it)->table);
3546 break;
3547 case Ifloattag:
3548 freeitem(((Ifloat*)it)->item);
3549 break;
3551 ga = it->genattr;
3552 if(ga != nil) {
3553 free(ga->id);
3554 free(ga->class);
3555 free(ga->style);
3556 free(ga->title);
3557 freescriptevents(ga->events);
3559 free(it);
3562 /* Free list of items chained through next pointer */
3563 void
3564 freeitems(Item* ithead)
3566 Item* it;
3567 Item* itnext;
3569 it = ithead;
3570 while(it != nil) {
3571 itnext = it->next;
3572 freeitem(it);
3573 it = itnext;
3577 static void
3578 freeformfield(Formfield* ff)
3580 Option* o;
3581 Option* onext;
3583 if(ff == nil)
3584 return;
3586 free(ff->name);
3587 free(ff->value);
3588 for(o = ff->options; o != nil; o = onext) {
3589 onext = o->next;
3590 free(o->value);
3591 free(o->display);
3593 free(ff);
3596 static void
3597 freetable(Table* t)
3599 int i;
3600 Tablecell* c;
3601 Tablecell* cnext;
3603 if(t == nil)
3604 return;
3606 /* We'll find all the unique cells via t->cells and next pointers. */
3607 /* (Other pointers to cells in the table are duplicates of these) */
3608 for(c = t->cells; c != nil; c = cnext) {
3609 cnext = c->next;
3610 freeitems(c->content);
3612 if(t->grid != nil) {
3613 for(i = 0; i < t->nrow; i++)
3614 free(t->grid[i]);
3615 free(t->grid);
3617 free(t->rows);
3618 free(t->cols);
3619 freeitems(t->caption);
3620 free(t);
3623 static void
3624 freeform(Form* f)
3626 if(f == nil)
3627 return;
3629 free(f->name);
3630 free(f->action);
3631 /* Form doesn't own its fields (Iformfield items do) */
3632 free(f);
3635 static void
3636 freeforms(Form* fhead)
3638 Form* f;
3639 Form* fnext;
3641 for(f = fhead; f != nil; f = fnext) {
3642 fnext = f->next;
3643 freeform(f);
3647 static void
3648 freeanchor(Anchor* a)
3650 if(a == nil)
3651 return;
3653 free(a->name);
3654 free(a->href);
3655 free(a);
3658 static void
3659 freeanchors(Anchor* ahead)
3661 Anchor* a;
3662 Anchor* anext;
3664 for(a = ahead; a != nil; a = anext) {
3665 anext = a->next;
3666 freeanchor(a);
3670 static void
3671 freedestanchor(DestAnchor* da)
3673 if(da == nil)
3674 return;
3676 free(da->name);
3677 free(da);
3680 static void
3681 freedestanchors(DestAnchor* dahead)
3683 DestAnchor* da;
3684 DestAnchor* danext;
3686 for(da = dahead; da != nil; da = danext) {
3687 danext = da->next;
3688 freedestanchor(da);
3692 static void
3693 freearea(Area* a)
3695 if(a == nil)
3696 return;
3697 free(a->href);
3698 free(a->coords);
3701 static void freekidinfos(Kidinfo* khead);
3703 static void
3704 freekidinfo(Kidinfo* k)
3706 if(k->isframeset) {
3707 free(k->rows);
3708 free(k->cols);
3709 freekidinfos(k->kidinfos);
3711 else {
3712 free(k->src);
3713 free(k->name);
3715 free(k);
3718 static void
3719 freekidinfos(Kidinfo* khead)
3721 Kidinfo* k;
3722 Kidinfo* knext;
3724 for(k = khead; k != nil; k = knext) {
3725 knext = k->next;
3726 freekidinfo(k);
3730 static void
3731 freemap(Map* m)
3733 Area* a;
3734 Area* anext;
3736 if(m == nil)
3737 return;
3739 free(m->name);
3740 for(a = m->areas; a != nil; a = anext) {
3741 anext = a->next;
3742 freearea(a);
3744 free(m);
3747 static void
3748 freemaps(Map* mhead)
3750 Map* m;
3751 Map* mnext;
3753 for(m = mhead; m != nil; m = mnext) {
3754 mnext = m->next;
3755 freemap(m);
3759 void
3760 freedocinfo(Docinfo* d)
3762 if(d == nil)
3763 return;
3764 free(d->src);
3765 free(d->base);
3766 freeitem((Item*)d->backgrounditem);
3767 free(d->refresh);
3768 freekidinfos(d->kidinfo);
3769 freeanchors(d->anchors);
3770 freedestanchors(d->dests);
3771 freeforms(d->forms);
3772 freemaps(d->maps);
3773 /* tables, images, and formfields are freed when */
3774 /* the items pointing at them are freed */
3775 free(d);
3778 /* Currently, someone else owns all the memory */
3779 /* pointed to by things in a Pstate. */
3780 static void
3781 freepstate(Pstate* p)
3783 free(p);
3786 static void
3787 freepstatestack(Pstate* pshead)
3789 Pstate* p;
3790 Pstate* pnext;
3792 for(p = pshead; p != nil; p = pnext) {
3793 pnext = p->next;
3794 free(p);
3798 static int
3799 Iconv(Fmt *f)
3801 Item* it;
3802 Itext* t;
3803 Irule* r;
3804 Iimage* i;
3805 Ifloat* fl;
3806 int state;
3807 Formfield* ff;
3808 Rune* ty;
3809 Tablecell* c;
3810 Table* tab;
3811 char* p;
3812 int cl;
3813 int hang;
3814 int indent;
3815 int bi;
3816 int nbuf;
3817 char buf[BIGBUFSIZE];
3819 it = va_arg(f->args, Item*);
3820 bi = 0;
3821 nbuf = sizeof(buf);
3822 state = it->state;
3823 nbuf = nbuf-1;
3824 if(state&IFbrk) {
3825 cl = state&(IFcleft|IFcright);
3826 p = "";
3827 if(cl) {
3828 if(cl == (IFcleft|IFcright))
3829 p = " both";
3830 else if(cl == IFcleft)
3831 p = " left";
3832 else
3833 p = " right";
3835 bi = snprint(buf, nbuf, "brk(%d%s)", (state&IFbrksp)? 1 : 0, p);
3837 if(state&IFnobrk)
3838 bi += snprint(buf+bi, nbuf-bi, " nobrk");
3839 if(!(state&IFwrap))
3840 bi += snprint(buf+bi, nbuf-bi, " nowrap");
3841 if(state&IFrjust)
3842 bi += snprint(buf+bi, nbuf-bi, " rjust");
3843 if(state&IFcjust)
3844 bi += snprint(buf+bi, nbuf-bi, " cjust");
3845 if(state&IFsmap)
3846 bi += snprint(buf+bi, nbuf-bi, " smap");
3847 indent = (state&IFindentmask) >> IFindentshift;
3848 if(indent > 0)
3849 bi += snprint(buf+bi, nbuf-bi, " indent=%d", indent);
3850 hang = state&IFhangmask;
3851 if(hang > 0)
3852 bi += snprint(buf+bi, nbuf-bi, " hang=%d", hang);
3854 switch(it->tag) {
3855 case Itexttag:
3856 t = (Itext*)it;
3857 bi += snprint(buf+bi, nbuf-bi, " Text '%S', fnt=%d, fg=%x", t->s, t->fnt, t->fg);
3858 break;
3860 case Iruletag:
3861 r = (Irule*)it;
3862 bi += snprint(buf+bi, nbuf-bi, "Rule size=%d, al=%S, wspec=", r->size, stringalign(r->align));
3863 bi += dimprint(buf+bi, nbuf-bi, r->wspec);
3864 break;
3866 case Iimagetag:
3867 i = (Iimage*)it;
3868 bi += snprint(buf+bi, nbuf-bi,
3869 "Image src=%S, alt=%S, al=%S, w=%d, h=%d hsp=%d, vsp=%d, bd=%d, map=%S",
3870 i->imsrc, i->altrep? i->altrep : L(Lempty), stringalign(i->align), i->imwidth, i->imheight,
3871 i->hspace, i->vspace, i->border, i->map?i->map->name : L(Lempty));
3872 break;
3874 case Iformfieldtag:
3875 ff = ((Iformfield*)it)->formfield;
3876 if(ff->ftype == Ftextarea)
3877 ty = L(Ltextarea);
3878 else if(ff->ftype == Fselect)
3879 ty = L(Lselect);
3880 else {
3881 ty = _revlookup(input_tab, NINPUTTAB, ff->ftype);
3882 if(ty == nil)
3883 ty = L(Lnone);
3885 bi += snprint(buf+bi, nbuf-bi, "Formfield %S, fieldid=%d, formid=%d, name=%S, value=%S",
3886 ty, ff->fieldid, ff->form->formid, ff->name? ff->name : L(Lempty),
3887 ff->value? ff->value : L(Lempty));
3888 break;
3890 case Itabletag:
3891 tab = ((Itable*)it)->table;
3892 bi += snprint(buf+bi, nbuf-bi, "Table tableid=%d, width=", tab->tableid);
3893 bi += dimprint(buf+bi, nbuf-bi, tab->width);
3894 bi += snprint(buf+bi, nbuf-bi, ", nrow=%d, ncol=%d, ncell=%d, totw=%d, toth=%d\n",
3895 tab->nrow, tab->ncol, tab->ncell, tab->totw, tab->toth);
3896 for(c = tab->cells; c != nil; c = c->next)
3897 bi += snprint(buf+bi, nbuf-bi, "Cell %d.%d, at (%d,%d) ",
3898 tab->tableid, c->cellid, c->row, c->col);
3899 bi += snprint(buf+bi, nbuf-bi, "End of Table %d", tab->tableid);
3900 break;
3902 case Ifloattag:
3903 fl = (Ifloat*)it;
3904 bi += snprint(buf+bi, nbuf-bi, "Float, x=%d y=%d, side=%S, it=%I",
3905 fl->x, fl->y, stringalign(fl->side), fl->item);
3906 bi += snprint(buf+bi, nbuf-bi, "\n\t");
3907 break;
3909 case Ispacertag:
3910 p = "";
3911 switch(((Ispacer*)it)->spkind) {
3912 case ISPnull:
3913 p = "null";
3914 break;
3915 case ISPvline:
3916 p = "vline";
3917 break;
3918 case ISPhspace:
3919 p = "hspace";
3920 break;
3922 bi += snprint(buf+bi, nbuf-bi, "Spacer %s ", p);
3923 break;
3925 bi += snprint(buf+bi, nbuf-bi, " w=%d, h=%d, a=%d, anchor=%d\n",
3926 it->width, it->height, it->ascent, it->anchorid);
3927 buf[bi] = 0;
3928 return fmtstrcpy(f, buf);
3931 /* String version of alignment 'a' */
3932 static Rune*
3933 stringalign(int a)
3935 Rune* s;
3937 s = _revlookup(align_tab, NALIGNTAB, a);
3938 if(s == nil)
3939 s = L(Lnone);
3940 return s;
3943 /* Put at most nbuf chars of representation of d into buf, */
3944 /* and return number of characters put */
3945 static int
3946 dimprint(char* buf, int nbuf, Dimen d)
3948 int n;
3949 int k;
3951 n = 0;
3952 n += snprint(buf, nbuf, "%d", dimenspec(d));
3953 k = dimenkind(d);
3954 if(k == Dpercent)
3955 buf[n++] = '%';
3956 if(k == Drelative)
3957 buf[n++] = '*';
3958 return n;
3961 void
3962 printitems(Item* items, char* msg)
3964 Item* il;
3966 fprint(2, "%s\n", msg);
3967 il = items;
3968 while(il != nil) {
3969 fprint(2, "%I", il);
3970 il = il->next;
3974 static Genattr*
3975 newgenattr(Rune* id, Rune* class, Rune* style, Rune* title, SEvent* events)
3977 Genattr* g;
3979 g = (Genattr*)emalloc(sizeof(Genattr));
3980 g->id = id;
3981 g->class = class;
3982 g->style = style;
3983 g->title = title;
3984 g->events = events;
3985 return g;
3988 static Formfield*
3989 newformfield(int ftype, int fieldid, Form* form, Rune* name,
3990 Rune* value, int size, int maxlength, Formfield* link)
3992 Formfield* ff;
3994 ff = (Formfield*)emalloc(sizeof(Formfield));
3995 ff->ftype = ftype;
3996 ff->fieldid = fieldid;
3997 ff->form = form;
3998 ff->name = name;
3999 ff->value = value;
4000 ff->size = size;
4001 ff->maxlength = maxlength;
4002 ff->ctlid = -1;
4003 ff->next = link;
4004 return ff;
4007 /* Transfers ownership of value and display to Option. */
4008 static Option*
4009 newoption(int selected, Rune* value, Rune* display, Option* link)
4011 Option *o;
4013 o = (Option*)emalloc(sizeof(Option));
4014 o->selected = selected;
4015 o->value = value;
4016 o->display = display;
4017 o->next = link;
4018 return o;
4021 static Form*
4022 newform(int formid, Rune* name, Rune* action, int target, int method, Form* link)
4024 Form* f;
4026 f = (Form*)emalloc(sizeof(Form));
4027 f->formid = formid;
4028 f->name = name;
4029 f->action = action;
4030 f->target = target;
4031 f->method = method;
4032 f->nfields = 0;
4033 f->fields = nil;
4034 f->next = link;
4035 return f;
4038 static Table*
4039 newtable(int tableid, Align align, Dimen width, int border,
4040 int cellspacing, int cellpadding, Background bg, Token* tok, Table* link)
4042 Table* t;
4044 t = (Table*)emalloc(sizeof(Table));
4045 t->tableid = tableid;
4046 t->align = align;
4047 t->width = width;
4048 t->border = border;
4049 t->cellspacing = cellspacing;
4050 t->cellpadding = cellpadding;
4051 t->background = bg;
4052 t->caption_place = ALbottom;
4053 t->caption_lay = nil;
4054 t->tabletok = tok;
4055 t->tabletok = nil;
4056 t->next = link;
4057 return t;
4060 static Tablerow*
4061 newtablerow(Align align, Background bg, int flags, Tablerow* link)
4063 Tablerow* tr;
4065 tr = (Tablerow*)emalloc(sizeof(Tablerow));
4066 tr->align = align;
4067 tr->background = bg;
4068 tr->flags = flags;
4069 tr->next = link;
4070 return tr;
4073 static Tablecell*
4074 newtablecell(int cellid, int rowspan, int colspan, Align align, Dimen wspec, int hspec,
4075 Background bg, int flags, Tablecell* link)
4077 Tablecell* c;
4079 c = (Tablecell*)emalloc(sizeof(Tablecell));
4080 c->cellid = cellid;
4081 c->lay = nil;
4082 c->rowspan = rowspan;
4083 c->colspan = colspan;
4084 c->align = align;
4085 c->flags = flags;
4086 c->wspec = wspec;
4087 c->hspec = hspec;
4088 c->background = bg;
4089 c->next = link;
4090 return c;
4093 static Anchor*
4094 newanchor(int index, Rune* name, Rune* href, int target, Anchor* link)
4096 Anchor* a;
4098 a = (Anchor*)emalloc(sizeof(Anchor));
4099 a->index = index;
4100 a->name = name;
4101 a->href = href;
4102 a->target = target;
4103 a->next = link;
4104 return a;
4107 static DestAnchor*
4108 newdestanchor(int index, Rune* name, Item* item, DestAnchor* link)
4110 DestAnchor* d;
4112 d = (DestAnchor*)emalloc(sizeof(DestAnchor));
4113 d->index = index;
4114 d->name = name;
4115 d->item = item;
4116 d->next = link;
4117 return d;
4120 static SEvent*
4121 newscriptevent(int type, Rune* script, SEvent* link)
4123 SEvent* ans;
4125 ans = (SEvent*)emalloc(sizeof(SEvent));
4126 ans->type = type;
4127 ans->script = script;
4128 ans->next = link;
4129 return ans;
4132 static void
4133 freescriptevents(SEvent* ehead)
4135 SEvent* e;
4136 SEvent* nexte;
4138 e = ehead;
4139 while(e != nil) {
4140 nexte = e->next;
4141 free(e->script);
4142 free(e);
4143 e = nexte;
4147 static Dimen
4148 makedimen(int kind, int spec)
4150 Dimen d;
4152 if(spec&Dkindmask) {
4153 if(warn)
4154 fprint(2, "warning: dimension spec too big: %d\n", spec);
4155 spec = 0;
4157 d.kindspec = kind|spec;
4158 return d;
4161 int
4162 dimenkind(Dimen d)
4164 return (d.kindspec&Dkindmask);
4167 int
4168 dimenspec(Dimen d)
4170 return (d.kindspec&Dspecmask);
4173 static Kidinfo*
4174 newkidinfo(int isframeset, Kidinfo* link)
4176 Kidinfo* ki;
4178 ki = (Kidinfo*)emalloc(sizeof(Kidinfo));
4179 ki->isframeset = isframeset;
4180 if(!isframeset) {
4181 ki->flags = FRhscrollauto|FRvscrollauto;
4182 ki->marginw = FRKIDMARGIN;
4183 ki->marginh = FRKIDMARGIN;
4184 ki->framebd = 1;
4186 ki->next = link;
4187 return ki;
4190 static Docinfo*
4191 newdocinfo(void)
4193 Docinfo* d;
4195 d = (Docinfo*)emalloc(sizeof(Docinfo));
4196 resetdocinfo(d);
4197 return d;
4200 static void
4201 resetdocinfo(Docinfo* d)
4203 memset(d, 0, sizeof(Docinfo));
4204 d->background = makebackground(nil, White);
4205 d->text = Black;
4206 d->link = Blue;
4207 d->vlink = Blue;
4208 d->alink = Blue;
4209 d->target = FTself;
4210 d->chset = ISO_8859_1;
4211 d->scripttype = TextJavascript;
4212 d->frameid = -1;
4215 /* Use targetmap array to keep track of name <-> targetid mapping. */
4216 /* Use real malloc(), and never free */
4217 static void
4218 targetmapinit(void)
4220 targetmapsize = 10;
4221 targetmap = (StringInt*)emalloc(targetmapsize*sizeof(StringInt));
4222 memset(targetmap, 0, targetmapsize*sizeof(StringInt));
4223 targetmap[0].key = _Strdup(L(L_top));
4224 targetmap[0].val = FTtop;
4225 targetmap[1].key = _Strdup(L(L_self));
4226 targetmap[1].val = FTself;
4227 targetmap[2].key = _Strdup(L(L_parent));
4228 targetmap[2].val = FTparent;
4229 targetmap[3].key = _Strdup(L(L_blank));
4230 targetmap[3].val = FTblank;
4231 ntargets = 4;
4234 int
4235 targetid(Rune* s)
4237 int i;
4238 int n;
4240 n = _Strlen(s);
4241 if(n == 0)
4242 return FTself;
4243 for(i = 0; i < ntargets; i++)
4244 if(_Strcmp(s, targetmap[i].key) == 0)
4245 return targetmap[i].val;
4246 if(i >= targetmapsize) {
4247 targetmapsize += 10;
4248 targetmap = (StringInt*)erealloc(targetmap, targetmapsize*sizeof(StringInt));
4250 targetmap[i].key = (Rune*)emalloc((n+1)*sizeof(Rune));
4251 memmove(targetmap[i].key, s, (n+1)*sizeof(Rune));
4252 targetmap[i].val = i;
4253 ntargets++;
4254 return i;
4257 Rune*
4258 targetname(int targid)
4260 int i;
4262 for(i = 0; i < ntargets; i++)
4263 if(targetmap[i].val == targid)
4264 return targetmap[i].key;
4265 return L(Lquestion);
4268 /* Convert HTML color spec to RGB value, returning dflt if can't. */
4269 /* Argument is supposed to be a valid HTML color, or "". */
4270 /* Return the RGB value of the color, using dflt if s */
4271 /* is nil or an invalid color. */
4272 static int
4273 color(Rune* s, int dflt)
4275 int v;
4276 Rune* rest;
4278 if(s == nil)
4279 return dflt;
4280 if(_lookup(color_tab, NCOLORS, s, _Strlen(s), &v))
4281 return v;
4282 if(s[0] == '#')
4283 s++;
4284 v = _Strtol(s, &rest, 16);
4285 if(*rest == 0)
4286 return v;
4287 return dflt;
4290 /* Debugging */
4292 #define HUGEPIX 10000
4294 /* A "shallow" validitem, that doesn't follow next links */
4295 /* or descend into tables. */
4296 static int
4297 validitem(Item* i)
4299 int ok;
4300 Itext* ti;
4301 Irule* ri;
4302 Iimage* ii;
4303 Ifloat* fi;
4304 int a;
4306 ok = (i->tag >= Itexttag && i->tag <= Ispacertag) &&
4307 (i->next == nil || validptr(i->next)) &&
4308 (i->width >= 0 && i->width < HUGEPIX) &&
4309 (i->height >= 0 && i->height < HUGEPIX) &&
4310 (i->ascent > -HUGEPIX && i->ascent < HUGEPIX) &&
4311 (i->anchorid >= 0) &&
4312 (i->genattr == nil || validptr(i->genattr));
4313 /* also, could check state for ridiculous combinations */
4314 /* also, could check anchorid for within-doc-range */
4315 if(ok)
4316 switch(i->tag) {
4317 case Itexttag:
4318 ti = (Itext*)i;
4319 ok = validStr(ti->s) &&
4320 (ti->fnt >= 0 && ti->fnt < NumStyle*NumSize) &&
4321 (ti->ul == ULnone || ti->ul == ULunder || ti->ul == ULmid);
4322 break;
4323 case Iruletag:
4324 ri = (Irule*)i;
4325 ok = (validvalign(ri->align) || validhalign(ri->align)) &&
4326 (ri->size >=0 && ri->size < HUGEPIX);
4327 break;
4328 case Iimagetag:
4329 ii = (Iimage*)i;
4330 ok = (ii->imsrc == nil || validptr(ii->imsrc)) &&
4331 (ii->item.width >= 0 && ii->item.width < HUGEPIX) &&
4332 (ii->item.height >= 0 && ii->item.height < HUGEPIX) &&
4333 (ii->imwidth >= 0 && ii->imwidth < HUGEPIX) &&
4334 (ii->imheight >= 0 && ii->imheight < HUGEPIX) &&
4335 (ii->altrep == nil || validStr(ii->altrep)) &&
4336 (ii->map == nil || validptr(ii->map)) &&
4337 (validvalign(ii->align) || validhalign(ii->align)) &&
4338 (ii->nextimage == nil || validptr(ii->nextimage));
4339 break;
4340 case Iformfieldtag:
4341 ok = validformfield(((Iformfield*)i)->formfield);
4342 break;
4343 case Itabletag:
4344 ok = validptr((Itable*)i);
4345 break;
4346 case Ifloattag:
4347 fi = (Ifloat*)i;
4348 ok = (fi->side == ALleft || fi->side == ALright) &&
4349 validitem(fi->item) &&
4350 (fi->item->tag == Iimagetag || fi->item->tag == Itabletag);
4351 break;
4352 case Ispacertag:
4353 a = ((Ispacer*)i)->spkind;
4354 ok = a==ISPnull || a==ISPvline || a==ISPhspace || a==ISPgeneral;
4355 break;
4356 default:
4357 ok = 0;
4359 return ok;
4362 /* "deep" validation, that checks whole list of items, */
4363 /* and descends into tables and floated tables. */
4364 /* nil is ok for argument. */
4365 int
4366 validitems(Item* i)
4368 int ok;
4369 Item* ii;
4371 ok = 1;
4372 while(i != nil && ok) {
4373 ok = validitem(i);
4374 if(ok) {
4375 if(i->tag == Itabletag) {
4376 ok = validtable(((Itable*)i)->table);
4378 else if(i->tag == Ifloattag) {
4379 ii = ((Ifloat*)i)->item;
4380 if(ii->tag == Itabletag)
4381 ok = validtable(((Itable*)ii)->table);
4384 if(!ok) {
4385 fprint(2, "invalid item: %I\n", i);
4387 i = i->next;
4389 return ok;
4392 static int
4393 validformfield(Formfield* f)
4395 int ok;
4397 ok = (f->next == nil || validptr(f->next)) &&
4398 (f->ftype >= 0 && f->ftype <= Ftextarea) &&
4399 f->fieldid >= 0 &&
4400 (f->form == nil || validptr(f->form)) &&
4401 (f->name == nil || validStr(f->name)) &&
4402 (f->value == nil || validStr(f->value)) &&
4403 (f->options == nil || validptr(f->options)) &&
4404 (f->image == nil || validitem(f->image)) &&
4405 (f->events == nil || validptr(f->events));
4406 /* when all built, should have f->fieldid < f->form->nfields, */
4407 /* but this may be called during build... */
4408 return ok;
4411 /* "deep" validation -- checks cell contents too */
4412 static int
4413 validtable(Table* t)
4415 int ok;
4416 int i, j;
4417 Tablecell* c;
4419 ok = (t->next == nil || validptr(t->next)) &&
4420 t->nrow >= 0 &&
4421 t->ncol >= 0 &&
4422 t->ncell >= 0 &&
4423 validalign(t->align) &&
4424 validdimen(t->width) &&
4425 (t->border >= 0 && t->border < HUGEPIX) &&
4426 (t->cellspacing >= 0 && t->cellspacing < HUGEPIX) &&
4427 (t->cellpadding >= 0 && t->cellpadding < HUGEPIX) &&
4428 validitems(t->caption) &&
4429 (t->caption_place == ALtop || t->caption_place == ALbottom) &&
4430 (t->totw >= 0 && t->totw < HUGEPIX) &&
4431 (t->toth >= 0 && t->toth < HUGEPIX) &&
4432 (t->tabletok == nil || validptr(t->tabletok));
4433 /* during parsing, t->rows has list; */
4434 /* only when parsing is done is t->nrow set > 0 */
4435 if(ok && t->nrow > 0 && t->ncol > 0) {
4436 /* table is "finished" */
4437 for(i = 0; i < t->nrow && ok; i++)
4438 ok = validtablerow(t->rows+i);
4439 for(j = 0; j < t->ncol && ok; j++)
4440 ok = validtablecol(t->cols+j);
4441 for(c = t->cells; c != nil && ok; c = c->next)
4442 ok = validtablecell(c);
4443 for(i = 0; i < t->nrow && ok; i++)
4444 for(j = 0; j < t->ncol && ok; j++)
4445 ok = validptr(t->grid[i][j]);
4447 return ok;
4450 static int
4451 validvalign(int a)
4453 return a == ALnone || a == ALmiddle || a == ALbottom || a == ALtop || a == ALbaseline;
4456 static int
4457 validhalign(int a)
4459 return a == ALnone || a == ALleft || a == ALcenter || a == ALright ||
4460 a == ALjustify || a == ALchar;
4463 static int
4464 validalign(Align a)
4466 return validhalign(a.halign) && validvalign(a.valign);
4469 static int
4470 validdimen(Dimen d)
4472 int ok;
4473 int s;
4475 ok = 0;
4476 s = d.kindspec&Dspecmask;
4477 switch(d.kindspec&Dkindmask) {
4478 case Dnone:
4479 ok = s==0;
4480 break;
4481 case Dpixels:
4482 ok = s < HUGEPIX;
4483 break;
4484 case Dpercent:
4485 case Drelative:
4486 ok = 1;
4487 break;
4489 return ok;
4492 static int
4493 validtablerow(Tablerow* r)
4495 return (r->cells == nil || validptr(r->cells)) &&
4496 (r->height >= 0 && r->height < HUGEPIX) &&
4497 (r->ascent > -HUGEPIX && r->ascent < HUGEPIX) &&
4498 validalign(r->align);
4501 static int
4502 validtablecol(Tablecol* c)
4504 return c->width >= 0 && c->width < HUGEPIX
4505 && validalign(c->align);
4508 static int
4509 validtablecell(Tablecell* c)
4511 int ok;
4513 ok = (c->next == nil || validptr(c->next)) &&
4514 (c->nextinrow == nil || validptr(c->nextinrow)) &&
4515 (c->content == nil || validptr(c->content)) &&
4516 (c->lay == nil || validptr(c->lay)) &&
4517 c->rowspan >= 0 &&
4518 c->colspan >= 0 &&
4519 validalign(c->align) &&
4520 validdimen(c->wspec) &&
4521 c->row >= 0 &&
4522 c->col >= 0;
4523 if(ok) {
4524 if(c->content != nil)
4525 ok = validitems(c->content);
4527 return ok;
4530 static int
4531 validptr(void* p)
4533 /* TODO: a better job of this. */
4534 /* For now, just dereference, which cause a bomb */
4535 /* if not valid */
4536 static char c;
4538 c = *((char*)p);
4539 return 1;
4542 static int
4543 validStr(Rune* s)
4545 return s != nil && validptr(s);