Debian Codegraph @

parsetagx.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* $Id: parsetagx.c,v 1.18 2006/06/07 03:52:03 inu Exp $ */
2 #include "fm.h"
3 #include "myctype.h"
4 #include "indep.h"
5 #include "Str.h"
6 #include "parsetagx.h"
7 #include "hash.h"
8
9 #include "html.c"
10
11 /* parse HTML tag */
12
13 static int noConv(char *, char **);
14 static int toNumber(char *, int *);
15 static int toLength(char *, int *);
16 static int toAlign(char *, int *);
17 static int toVAlign(char *, int *);
18
19 /* *INDENT-OFF* */
20 static int (*toValFunc[]) () = {
21 noConv, /* VTYPE_NONE */
22 noConv, /* VTYPE_STR */
23 toNumber, /* VTYPE_NUMBER */
24 toLength, /* VTYPE_LENGTH */
25 toAlign, /* VTYPE_ALIGN */
26 toVAlign, /* VTYPE_VALIGN */
27 noConv, /* VTYPE_ACTION */
28 noConv, /* VTYPE_ENCTYPE */
29 noConv, /* VTYPE_METHOD */
30 noConv, /* VTYPE_MLENGTH */
31 noConv, /* VTYPE_TYPE */
32 };
33 /* *INDENT-ON* */
34
35 static int
36 noConv(char *oval, char **str)
37 {
38 *str = oval;
39 return 1;
40 }
41
42 static int
43 toNumber(char *oval, int *num)
44 {
45 char *ep;
46 int x;
47
48 x = strtol(oval, &ep, 10);
49
50 if (ep > oval) {
51 *num = x;
52 return 1;
53 }
54 else
55 return 0;
56 }
57
58 static int
59 toLength(char *oval, int *len)
60 {
61 int w;
62 if (!IS_DIGIT(oval[0]))
63 return 0;
64 w = atoi(oval);
65 if (w < 0)
66 return 0;
67 if (w == 0)
68 w = 1;
69 if (oval[strlen(oval) - 1] == '%')
70 *len = -w;
71 else
72 *len = w;
73 return 1;
74 }
75
76 static int
77 toAlign(char *oval, int *align)
78 {
79 if (strcasecmp(oval, "left") == 0)
80 *align = ALIGN_LEFT;
81 else if (strcasecmp(oval, "right") == 0)
82 *align = ALIGN_RIGHT;
83 else if (strcasecmp(oval, "center") == 0)
84 *align = ALIGN_CENTER;
85 else if (strcasecmp(oval, "top") == 0)
86 *align = ALIGN_TOP;
87 else if (strcasecmp(oval, "bottom") == 0)
88 *align = ALIGN_BOTTOM;
89 else if (strcasecmp(oval, "middle") == 0)
90 *align = ALIGN_MIDDLE;
91 else
92 return 0;
93 return 1;
94 }
95
96 static int
97 toVAlign(char *oval, int *valign)
98 {
99 if (strcasecmp(oval, "top") == 0 || strcasecmp(oval, "baseline") == 0)
100 *valign = VALIGN_TOP;
101 else if (strcasecmp(oval, "bottom") == 0)
102 *valign = VALIGN_BOTTOM;
103 else if (strcasecmp(oval, "middle") == 0)
104 *valign = VALIGN_MIDDLE;
105 else
106 return 0;
107 return 1;
108 }
109
110 extern Hash_si tagtable;
111 #define MAX_TAG_LEN 64
112
113 struct parsed_tag *
114 parse_tag(char **s, int internal)
115 {
116 struct parsed_tag *tag = NULL;
117 int tag_id;
118 char tagname[MAX_TAG_LEN], attrname[MAX_TAG_LEN];
119 char *p, *q;
120 int i, attr_id = 0, nattr;
121
122 /* Parse tag name */
123 tagname[0] = '\0';
124 q = (*s) + 1;
125 p = tagname;
126 if (*q == '/') {
127 *(p++) = *(q++);
128 SKIP_BLANKS(q);
129 }
130 while (*q && !IS_SPACE(*q) && !(tagname[0] != '/' && *q == '/') &&
131 *q != '>' && p - tagname < MAX_TAG_LEN - 1) {
132 *(p++) = TOLOWER(*q);
133 q++;
134 }
135 *p = '\0';
136 while (*q && !IS_SPACE(*q) && !(tagname[0] != '/' && *q == '/') &&
137 *q != '>')
138 q++;
139
140 tag_id = getHash_si(&tagtable, tagname, HTML_UNKNOWN);
141
142 if (tag_id == HTML_UNKNOWN ||
143 (!internal && TagMAP[tag_id].flag & TFLG_INT))
144 goto skip_parse_tagarg;
145
146 tag = New(struct parsed_tag);
147 bzero(tag, sizeof(struct parsed_tag));
148 tag->tagid = tag_id;
149
150 if ((nattr = TagMAP[tag_id].max_attribute) > 0) {
151 tag->attrid = NewAtom_N(unsigned char, nattr);
152 tag->value = New_N(char *, nattr);
153 tag->map = NewAtom_N(unsigned char, MAX_TAGATTR);
154 memset(tag->map, MAX_TAGATTR, MAX_TAGATTR);
155 memset(tag->attrid, ATTR_UNKNOWN, nattr);
156 for (i = 0; i < nattr; i++)
157 tag->map[TagMAP[tag_id].accept_attribute[i]] = i;
158 }
159
160 /* Parse tag arguments */
161 SKIP_BLANKS(q);
162 while (1) {
163 Str value = NULL, value_tmp = NULL;
164 if (*q == '>' || *q == '\0')
165 goto done_parse_tag;
166 p = attrname;
167 while (*q && *q != '=' && !IS_SPACE(*q) &&
168 *q != '>' && p - attrname < MAX_TAG_LEN - 1) {
169 *(p++) = TOLOWER(*q);
170 q++;
171 }
172 *p = '\0';
173 while (*q && *q != '=' && !IS_SPACE(*q) && *q != '>')
174 q++;
175 SKIP_BLANKS(q);
176 if (*q == '=') {
177 /* get value */
178 value_tmp = Strnew();
179 q++;
180 SKIP_BLANKS(q);
181 if (*q == '"') {
182 q++;
183 while (*q && *q != '"') {
184 Strcat_char(value_tmp, *q);
185 if (!tag->need_reconstruct && is_html_quote(*q))
186 tag->need_reconstruct = TRUE;
187 q++;
188 }
189 if (*q == '"')
190 q++;
191 }
192 else if (*q == '\'') {
193 q++;
194 while (*q && *q != '\'') {
195 Strcat_char(value_tmp, *q);
196 if (!tag->need_reconstruct && is_html_quote(*q))
197 tag->need_reconstruct = TRUE;
198 q++;
199 }
200 if (*q == '\'')
201 q++;
202 }
203 else if (*q) {
204 while (*q && !IS_SPACE(*q) && *q != '>') {
205 Strcat_char(value_tmp, *q);
206 if (!tag->need_reconstruct && is_html_quote(*q))
207 tag->need_reconstruct = TRUE;
208 q++;
209 }
210 }
211 }
212 for (i = 0; i < nattr; i++) {
213 if ((tag)->attrid[i] == ATTR_UNKNOWN &&
214 strcmp(AttrMAP[TagMAP[tag_id].accept_attribute[i]].name,
215 attrname) == 0) {
216 attr_id = TagMAP[tag_id].accept_attribute[i];
217 break;
218 }
219 }
220
221 if (value_tmp) {
222 int j, hidden=FALSE;
223 for (j=0; j<i; j++) {
224 if (tag->attrid[j] == ATTR_TYPE &&
225 tag->value[j] &&
226 strcmp("hidden",tag->value[j]) == 0) {
227 hidden=TRUE;
228 break;
229 }
230 }
231 if ((tag_id == HTML_INPUT || tag_id == HTML_INPUT_ALT) &&
232 attr_id == ATTR_VALUE && hidden) {
233 value = value_tmp;
234 } else {
235 char *x;
236 value = Strnew();
237 for (x = value_tmp->ptr; *x; x++) {
238 if (*x != '\n')
239 Strcat_char(value, *x);
240 }
241 }
242 }
243
244 if (i != nattr) {
245 if (!internal &&
246 ((AttrMAP[attr_id].flag & AFLG_INT) ||
247 (value && AttrMAP[attr_id].vtype == VTYPE_METHOD &&
248 !strcasecmp(value->ptr, "internal")))) {
249 tag->need_reconstruct = TRUE;
250 continue;
251 }
252 tag->attrid[i] = attr_id;
253 if (value)
254 tag->value[i] = html_unquote(value->ptr);
255 else
256 tag->value[i] = NULL;
257 }
258 else {
259 tag->need_reconstruct = TRUE;
260 }
261 }
262
263 skip_parse_tagarg:
264 while (*q != '>' && *q)
265 q++;
266 done_parse_tag:
267 if (*q == '>')
268 q++;
269 *s = q;
270 return tag;
271 }
272
273 int
274 parsedtag_set_value(struct parsed_tag *tag, int id, char *value)
275 {
276 int i;
277
278 if (!parsedtag_accepts(tag, id))
279 return 0;
280
281 i = tag->map[id];
282 tag->attrid[i] = id;
283 if (value)
284 tag->value[i] = allocStr(value, -1);
285 else
286 tag->value[i] = NULL;
287 tag->need_reconstruct = TRUE;
288 return 1;
289 }
290
291 int
292 parsedtag_get_value(struct parsed_tag *tag, int id, void *value)
293 {
294 int i;
295 if (!parsedtag_exists(tag, id) || !tag->value[i = tag->map[id]])
296 return 0;
297 return toValFunc[AttrMAP[id].vtype] (tag->value[i], value);
298 }
299
300 Str
301 parsedtag2str(struct parsed_tag *tag)
302 {
303 int i;
304 int tag_id = tag->tagid;
305 int nattr = TagMAP[tag_id].max_attribute;
306 Str tagstr = Strnew();
307 Strcat_char(tagstr, '<');
308 Strcat_charp(tagstr, TagMAP[tag_id].name);
309 for (i = 0; i < nattr; i++) {
310 if (tag->attrid[i] != ATTR_UNKNOWN) {
311 Strcat_char(tagstr, ' ');
312 Strcat_charp(tagstr, AttrMAP[tag->attrid[i]].name);
313 if (tag->value[i])
314 Strcat(tagstr, Sprintf("=\"%s\"", html_quote(tag->value[i])));
315 }
316 }
317 Strcat_char(tagstr, '>');
318 return tagstr;
319 }