lib/XML/ED/parser.c

filter: Statementstmt 0.0% 0 / 465 Branchbran 0.0% 0 / 90 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 555 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« lib/XML/ED/Bare.xs ↑ Summary
1  
2 #include "parser.h"
3 #include<stdio.h>
4 #include "stdlib.h"
5 #ifdef NOSTRING
6 void memset(char *s, int c, int n)
7 {
8 char *se = s + n;
9 while (s < se)
10 *s++ = c;
11 }
12 #else
13 #include <string.h>
14 #endif
15  
16 0 int dh_memcmp(char *a, char *b, int n)
17 {
18 0 int c = 0;
19 0 ▶ while (c < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (c < n) { 0 0 0
20 0 ▶ if (*a != *b)
Branch: 1 branch
BranchTrueFalseTotal
if (*a != *b) 0 0 0
21 0 return c + 1;
22 0 a++;
23 0 b++;
24 0 c++;
25 }
26 0 return 0;
27 }
28  
29 0 struct nodec *new_nodecp(struct nodec *newparent)
30 {
31 static int pos = 0;
32 0 int size = sizeof(struct nodec);
33 0 struct nodec *self = (struct nodec *) malloc(size);
34 0 memset((char *) self, 0, size);
35 0 self->parent = newparent;
36 0 self->pos = ++pos;
37 0 return self;
38 }
39  
40 0 struct nodec *new_nodec()
41 {
42 0 struct nodec *node = (struct nodec *)calloc(sizeof(struct nodec), 1);
43  
44 0 return node;
45 }
46  
47 0 void del_nodec(struct nodec *node)
48 {
49 struct nodec *curnode;
50 struct attc *curatt;
51 struct nodec *next;
52 struct attc *nexta;
53 0 curnode = node->firstchild;
54 0 ▶ while (curnode) {
Branch: 1 branch
BranchTrueFalseTotal
while (curnode) { 0 0 0
55 0 next = curnode->next;
56 0 del_nodec(curnode);
57 0 ▶ if (!next)
Branch: 1 branch
BranchTrueFalseTotal
if (!next) 0 0 0
58 0 break;
59 0 curnode = next;
60 }
61 0 curatt = node->firstatt;
62 0 ▶ while (curatt) {
Branch: 1 branch
BranchTrueFalseTotal
while (curatt) { 0 0 0
63 0 nexta = curatt->next;
64 0 free(curatt);
65 0 curatt = nexta;
66 }
67 0 free(node);
68 0 }
69  
70 0 struct attc *new_attc(struct nodec *newparent)
71 {
72 0 int size = sizeof(struct attc);
73 0 struct attc *self = (struct attc *) malloc(size);
74 0 memset((char *) self, 0, size);
75 0 self->parent = newparent;
76 0 return self;
77 }
78  
79 //#define DEBUG
80  
81 0 struct nodec *parserc_parse(struct parserc *self, char *xmlin)
82 {
83 char *tagname, *attname, *attval, *val;
84 0 struct nodec *root = new_nodec();
85 0 int tagname_len = 0;
86 0 int tagname_type = 0;
87 0 int attname_len = 0;
88 0 int attval_len = 0;
89 0 struct nodec *curnode = root;
90 struct nodec *temp;
91 0 struct attc *curatt = NULL;
92 0 char *cpos = &xmlin[0];
93 0 int pos = 0;
94 0 int res = 0;
95 int dent;
96 register int let;
97  
98 #ifdef DEBUG
99 printf("Entry to C Parser\n");
100 #endif
101  
102 0 val_1:
103 #ifdef DEBUG
104 printf("val_1: %c\n", *cpos);
105 #endif
106 0 let = *cpos;
107 0 switch (let) {
108 0 case '\0':
109 0 goto done;
110 0 case '<':
111 0 goto val_x;
112 }
113  
114 {
115 static int x = 0;
116 char buffer[1023];
117 int len;
118 0 curnode = nodec_addchildr(curnode, "contents", 8, 5);
119 0 curnode->value = cpos;
120 0 curnode->vallen = 1;
121 0 cpos++;
122 }
123  
124 0 val_x:
125 #ifdef DEBUG
126 printf("val_x: %c\n", *cpos);
127 #endif
128 0 let = *cpos;
129 0 switch (let) {
130 0 case '\0':
131 0 ▶ if (curnode->type == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (curnode->type == 5) { 0 0 0
132 0 curnode->vallen = cpos - curnode->value;
133 0 curnode->z = cpos - xmlin;
134 0 curnode = curnode->parent; // jump up
135 }
136 0 goto done;
137 0 case '<':
138 0 ▶ if (curnode->type == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (curnode->type == 5) { 0 0 0
139 0 curnode->vallen = cpos - curnode->value;
140 0 curnode->z = cpos - xmlin;
141 0 curnode = curnode->parent; // jump up
142 }
143 0 tagname_type = 1;
144 0 switch (*(cpos + 1)) {
145 0 case '!':
146 0 curnode = nodec_addchildr(curnode, "comment", 7, 8);
147 0 curnode->value = cpos;
148 0 ▶ if (*(cpos + 2) == '-' && // <!--
Branch: 1 branch
BranchTrueFalseTotal
if (*(cpos + 2) == '-' && // <!-- 0 0 0
149 0 ▶ *(cpos + 3) == '-') {
Branch: 1 branch
BranchTrueFalseTotal
*(cpos + 3) == '-') { 0 0 0
150 0 cpos += 4;
151 0 curnode->type |= 4;
152 0 curnode->value = cpos;
153 0 goto comment;
154 } else {
155 0 cpos++;
156 0 curnode->type |= 2;
157 0 curnode->value = cpos;
158 0 goto bang;
159 }
160 0 case '?':
161 0 tagname_type = 2;
162 0 cpos++;
163 0 cpos++;
164 0 goto name_1;
165 }
166 0 tagname_len = 0; // for safety
167 0 cpos++;
168 0 goto name_1;
169 }
170 0 cpos++;
171 0 goto val_x;
172  
173 0 comment_1dash:
174 0 cpos++;
175 0 let = *cpos;
176 0 ▶ if (let == '-')
Branch: 1 branch
BranchTrueFalseTotal
if (let == '-') 0 0 0
177 0 goto comment_2dash;
178 0 goto comment_x;
179  
180 0 comment_2dash:
181 0 cpos++;
182 0 let = *cpos;
183 0 ▶ if (let == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (let == '>') { 0 0 0
184 0 curnode->vallen = cpos - curnode->value - 2;
185 0 cpos++;
186 0 curnode = curnode->parent;
187 0 goto val_1;
188 }
189 0 goto comment_x;
190  
191 0 comment:
192 0 let = *cpos;
193 0 switch (let) {
194 0 case '\0':
195 0 goto done;
196 0 case '-':
197 0 goto comment_1dash;
198 }
199 0 cpos++;
200  
201 0 comment_x:
202 0 let = *cpos;
203 0 switch (let) {
204 0 case 0:
205 0 goto done;
206 0 case '-':
207 0 goto comment_1dash;
208 }
209 0 cpos++;
210 0 goto comment_x;
211  
212 pi:
213 let = *cpos;
214 if (!let)
215 goto done;
216 if (let == '?' && *(cpos + 1) == '>') {
217 cpos += 2;
218 goto val_1;
219 }
220 cpos++;
221 goto pi;
222  
223 0 bang:
224 0 let = *cpos;
225 0 ▶ if (!let)
Branch: 1 branch
BranchTrueFalseTotal
if (!let) 0 0 0
226 0 goto done;
227 0 ▶ if (let == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (let == '>') { 0 0 0
228 0 curnode->vallen = cpos - curnode->value;
229 0 cpos++;
230 0 curnode = curnode->parent;
231  
232 0 goto val_1;
233 }
234 0 cpos++;
235 0 goto bang;
236  
237 cdata:
238 let = *cpos;
239 if (!let)
240 goto done;
241 if (let == ']' && *(cpos + 1) == ']' && *(cpos + 2) == '>') {
242 cpos += 3;
243 goto val_1;
244 }
245 cpos++;
246 goto cdata;
247  
248 // looking for name after <
249  
250 0 name_1:
251 #ifdef DEBUG
252 printf("name_1: %c\n", *cpos);
253 #endif
254 0 let = *cpos;
255 0 switch (let) {
256 0 case '\0':
257 0 goto done;
258 0 case ' ':
259 case '\r':
260 case '\n':
261 0 cpos++;
262 0 goto name_1;
263 0 case '/': // regular closing tag
264 0 tagname_len = 0; // needed to reset
265 0 cpos++;
266 0 goto ename_1;
267 }
268 0 tagname = cpos;
269 0 tagname_len = 1;
270 0 cpos++;
271 0 goto name_x;
272  
273 // have a name
274  
275 0 name_x:
276 #ifdef DEBUG
277 printf("name_x: %c\n", *cpos);
278 #endif
279 0 let = *cpos;
280 0 switch (let) {
281 0 case '\0':
282 0 goto done;
283 0 case '/': // self closing
284 case '?': // self closing
285  
286 case ' ':
287 case '\n':
288 case '\r':
289 case '>':
290 curnode =
291 0 nodec_addchildr(curnode, tagname, tagname_len, tagname_type);
292 0 attname_len = 0;
293 0 goto name_gap;
294 }
295  
296 0 tagname_len++;
297 0 cpos++;
298 0 goto name_x;
299  
300 // look for attributes
301  
302 0 name_gap:
303 0 let = *cpos;
304 0 switch (*cpos) {
305 0 case '\0':
306 0 goto done;
307 0 case ' ':
308 case '\n':
309 case '\r':
310 0 cpos++;
311 0 goto name_gap;
312 0 case '>':
313 0 cpos++;
314 0 goto val_1;
315 0 case '?': // self closing
316 case '/': // self closing
317 0 curnode->z = cpos + 1 - xmlin;
318 0 curnode = curnode->parent;
319 0 ▶ if (!curnode)
Branch: 1 branch
BranchTrueFalseTotal
if (!curnode) 0 0 0
320 0 goto done;
321 0 cpos += 2; // am assuming next char is >
322 0 goto val_1;
323 }
324  
325 0 att_name1:
326 #ifdef DEBUG
327 printf("attname1: %c\n", *cpos);
328 #endif
329 0 let = *cpos;
330 0 switch (*cpos) {
331 0 case 0:
332 0 goto done;
333 0 case 0x27:
334 0 cpos++;
335 0 attname = cpos;
336 0 attname_len = 0;
337 0 goto att_nameqs;
338 }
339 0 attname = cpos;
340 0 attname_len = 1;
341 0 cpos++;
342 0 goto att_name;
343  
344 0 att_space:
345 0 let = *cpos;
346 0 switch (let) {
347 0 case 0:
348 0 goto done;
349 0 case ' ':
350 case 0x0d:
351 case 0x0a:
352 0 cpos++;
353 0 goto att_space;
354 0 case '=':
355 0 cpos++;
356 0 goto att_eq1;
357 }
358 // we have another attribute name, so continue
359  
360 0 att_name:
361 #ifdef DEBUG
362 printf("attname: %c\n", *cpos);
363 #endif
364 0 let = *cpos;
365 0 switch (let) {
366 0 case 0:
367 0 goto done;
368 0 case '/': // self closing !! /> is assumed !!
369 0 curatt = nodec_addattr(curnode, attname, attname_len);
370 0 attname_len = 0;
371  
372 0 curnode->z = cpos + 1 - xmlin;
373 0 curnode = curnode->parent;
374 0 ▶ if (!curnode)
Branch: 1 branch
BranchTrueFalseTotal
if (!curnode) 0 0 0
375 0 goto done;
376 0 cpos += 2;
377 0 goto val_1;
378 0 case ' ':
379 0 ▶ if (*(cpos + 1) == '=') {
Branch: 1 branch
BranchTrueFalseTotal
if (*(cpos + 1) == '=') { 0 0 0
380 0 cpos++;
381 0 goto att_name;
382 }
383 0 curatt = nodec_addattr(curnode, attname, attname_len);
384 0 attname_len = 0;
385 0 cpos++;
386 0 goto att_space;
387 0 case '>':
388 0 curatt = nodec_addattr(curnode, attname, attname_len);
389 0 attname_len = 0;
390 0 cpos++;
391 0 goto val_1;
392 0 case '=':
393 0 attval_len = 0;
394 0 curatt = nodec_addattr(curnode, attname, attname_len);
395 0 attname_len = 0;
396 0 cpos++;
397 0 goto att_eq1;
398 }
399  
400 0 ▶ if (!attname_len)
Branch: 1 branch
BranchTrueFalseTotal
if (!attname_len) 0 0 0
401 0 attname = cpos;
402 0 attname_len++;
403 0 cpos++;
404 0 goto att_name;
405  
406 0 att_nameqs:
407 #ifdef DEBUG
408 printf("nameqs: %c\n", *cpos);
409 #endif
410 0 let = *cpos;
411 0 switch (let) {
412 0 case 0:
413 0 goto done;
414 0 case 0x27: // APOSTROPHE
415 0 cpos++;
416 0 goto att_nameqsdone;
417 }
418 0 attname_len++;
419 0 cpos++;
420 0 goto att_nameqs;
421  
422 0 att_nameqsdone:
423 #ifdef DEBUG
424 printf("nameqsdone: %c\n", *cpos);
425 #endif
426 0 let = *cpos;
427 0 switch (let) {
428 0 case 0:
429 0 goto done;
430 0 case '=':
431 0 attval_len = 0;
432 0 curatt = nodec_addattr(curnode, attname, attname_len);
433 0 attname_len = 0;
434 0 cpos++;
435 0 goto att_eq1;
436 }
437 0 goto att_nameqsdone;
438  
439 0 att_eq1:
440 0 let = *cpos;
441 0 switch (let) {
442 0 case 0:
443 0 goto done;
444 0 case '/': // self closing
445 0 ▶ if (*(cpos + 1) == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*(cpos + 1) == '>') { 0 0 0
446 0 curnode->z = cpos + 1 - xmlin;
447 0 curnode = curnode->parent;
448 0 ▶ if (!curnode)
Branch: 1 branch
BranchTrueFalseTotal
if (!curnode) 0 0 0
449 0 goto done;
450 0 cpos += 2;
451 0 goto att_eq1;
452 }
453 0 break;
454 0 case '"':
455 0 cpos++;
456 0 goto att_quot;
457 0 case 0x27: // '
458 0 cpos++;
459 0 goto att_quots;
460 0 case '`':
461 0 cpos++;
462 0 goto att_tick;
463 0 case '>':
464 0 cpos++;
465 0 goto val_1;
466 0 case ' ':
467 0 cpos++;
468 0 goto att_eq1;
469 }
470 0 ▶ if (!attval_len)
Branch: 1 branch
BranchTrueFalseTotal
if (!attval_len) 0 0 0
471 0 attval = cpos;
472 0 attval_len++;
473 0 cpos++;
474 0 goto att_eqx;
475  
476 0 att_eqx:
477 0 let = *cpos;
478 0 switch (let) {
479 0 case 0:
480 0 goto done;
481 0 case '?': // self closing
482 case '/': // self closing
483 0 ▶ if (*(cpos + 1) == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (*(cpos + 1) == '>') { 0 0 0
484 0 curnode->z = cpos + 1 - xmlin;
485 0 curnode = curnode->parent;
486 0 ▶ if (!curnode)
Branch: 1 branch
BranchTrueFalseTotal
if (!curnode) 0 0 0
487 0 goto done;
488 0 curatt->value = attval;
489 0 curatt->vallen = attval_len;
490 0 attval_len = 0;
491 0 cpos += 2;
492 0 goto val_1;
493 }
494 0 break;
495 0 case '>':
496 0 curatt->value = attval;
497 0 curatt->vallen = attval_len;
498 0 attval_len = 0;
499 0 cpos++;
500 0 goto val_1;
501 0 case ' ':
502 0 curatt->value = attval;
503 0 curatt->vallen = attval_len;
504 0 attval_len = 0;
505 0 cpos++;
506 0 goto name_gap;
507 }
508  
509 0 ▶ if (!attval_len)
Branch: 1 branch
BranchTrueFalseTotal
if (!attval_len) 0 0 0
510 0 attval = cpos;
511 0 attval_len++;
512 0 cpos++;
513 0 goto att_eqx;
514  
515 0 att_quot:
516 0 let = *cpos;
517 0 ▶ if (!let)
Branch: 1 branch
BranchTrueFalseTotal
if (!let) 0 0 0
518 0 goto done;
519 0 ▶ if (let == '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (let == '"') { 0 0 0
520 0 ▶ if (attval_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (attval_len) { 0 0 0
521 0 curatt->value = attval;
522 0 curatt->vallen = attval_len;
523 0 attval_len = 0;
524 }
525 0 cpos++;
526 0 goto name_gap;
527 } else {
528 0 ▶ if (!attval_len)
Branch: 1 branch
BranchTrueFalseTotal
if (!attval_len) 0 0 0
529 0 attval = cpos;
530 0 attval_len++;
531 0 cpos++;
532 0 goto att_quot;
533 }
534  
535 0 att_quots:
536 0 let = *cpos;
537 0 ▶ if (!let)
Branch: 1 branch
BranchTrueFalseTotal
if (!let) 0 0 0
538 0 goto done;
539 0 ▶ if (let == 0x27) {
Branch: 1 branch
BranchTrueFalseTotal
if (let == 0x27) { 0 0 0
540 0 ▶ if (attval_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (attval_len) { 0 0 0
541 0 curatt->value = attval;
542 0 curatt->vallen = attval_len;
543 0 attval_len = 0;
544 }
545 0 cpos++;
546 0 goto name_gap;
547 } else {
548 0 ▶ if (!attval_len)
Branch: 1 branch
BranchTrueFalseTotal
if (!attval_len) 0 0 0
549 0 attval = cpos;
550 0 attval_len++;
551 0 cpos++;
552 0 goto att_quots;
553 }
554  
555 0 att_tick:
556 0 let = *cpos;
557 0 ▶ if (!let)
Branch: 1 branch
BranchTrueFalseTotal
if (!let) 0 0 0
558 0 goto done;
559 0 ▶ if (let == '`') {
Branch: 1 branch
BranchTrueFalseTotal
if (let == '`') { 0 0 0
560 0 ▶ if (attval_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (attval_len) { 0 0 0
561 0 curatt->value = attval;
562 0 curatt->vallen = attval_len;
563 0 attval_len = 0;
564 }
565 0 cpos++;
566 0 goto name_gap;
567 }
568 0 ▶ if (!attval_len)
Branch: 1 branch
BranchTrueFalseTotal
if (!attval_len) 0 0 0
569 0 attval = cpos;
570 0 attval_len++;
571 0 cpos++;
572 0 goto att_tick;
573  
574 0 ename_1:
575 0 let = *cpos;
576 0 ▶ if (!let)
Branch: 1 branch
BranchTrueFalseTotal
if (!let) 0 0 0
577 0 goto done;
578  
579 0 ▶ if (let == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (let == '>') { 0 0 0
580 0 curnode->namelen = tagname_len;
581 0 curnode->z = cpos - xmlin;
582 0 curnode = curnode->parent; // jump up
583 0 ▶ if (!curnode)
Branch: 1 branch
BranchTrueFalseTotal
if (!curnode) 0 0 0
584 0 goto done;
585 0 tagname_len++;
586 0 cpos++;
587 0 root->err = -1;
588 0 goto error;
589 }
590 0 tagname = cpos;
591 0 tagname_len = 1;
592 0 cpos++;
593 // continue
594  
595 0 ename_x: // ending name
596 0 let = *cpos;
597 0 ▶ if (!let)
Branch: 1 branch
BranchTrueFalseTotal
if (!let) 0 0 0
598 0 goto done;
599 0 ▶ if (let == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (let == '>') { 0 0 0
600 0 ▶ if (curnode->namelen != tagname_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (curnode->namelen != tagname_len) { 0 0 0
601 0 goto error;
602 }
603 0 ▶ if (res = dh_memcmp(curnode->name, tagname, tagname_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (res = dh_memcmp(curnode->name, tagname, tagname_len)) { 0 0 0
604 0 cpos -= tagname_len;
605 0 cpos += res - 1;
606 0 goto error;
607 }
608 0 curnode->z = cpos - xmlin;
609 0 curnode = curnode->parent; // jump up
610 0 ▶ if (!curnode)
Branch: 1 branch
BranchTrueFalseTotal
if (!curnode) 0 0 0
611 0 goto done;
612 0 tagname_len++;
613 0 cpos++;
614  
615 0 goto val_1;
616 }
617 0 tagname_len++;
618 0 cpos++;
619 0 goto ename_x;
620 0 error:
621 0 root->err = -(int) (cpos - &xmlin[0]);
622 0 self->pcurnode = root;
623 //root->err = 1;
624 0 return root;
625 0 done:
626 #ifdef DEBUG
627 printf("done\n", *cpos);
628 #endif
629 0 self->pcurnode = root;
630 0 self->pcurnode->curchild = self->pcurnode->firstchild;
631 #ifdef DEBUG
632 printf("returning\n", *cpos);
633 #endif
634 0 return root;
635 }
636  
637 struct utfchar {
638 char high;
639 char low;
640 };
641  
642 0 struct nodec *nodec_addchildr(struct nodec *self, char *newname,
643 int newnamelen, int newtype)
644 {
645 0 struct nodec *newnode = new_nodecp(self);
646 0 newnode->name = newname;
647 0 newnode->namelen = newnamelen;
648 //newnode->value = newval;
649 //newnode->vallen = newvallen;
650 0 newnode->type = newtype;
651 0 ▶ if (self->numchildren == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->numchildren == 0) { 0 0 0
652 0 self->firstchild = newnode;
653 0 self->lastchild = newnode;
654 0 self->numchildren++;
655 0 return newnode;
656 } else {
657 0 self->lastchild->next = newnode;
658 0 self->lastchild = newnode;
659 0 self->numchildren++;
660 0 return newnode;
661 }
662 }
663  
664 0 struct attc *nodec_addattr(struct nodec *self, char *newname,
665 int newnamelen)
666 {
667 0 struct attc *newatt = new_attc(self);
668 0 newatt->name = newname;
669 0 newatt->namelen = newnamelen;
670 //newatt->value = newval;
671 //newatt->vallen = newvallen;
672  
673 0 ▶ if (!self->numatt) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self->numatt) { 0 0 0
674 0 self->firstatt = newatt;
675 0 self->lastatt = newatt;
676 0 self->numatt++;
677 0 return newatt;
678 } else {
679 0 self->lastatt->next = newatt;
680 0 self->lastatt = newatt;
681 0 self->numatt++;
682 0 return newatt;
683 }
684 }
685  
686 /* eof */
« lib/XML/ED/Bare.xs ↑ Summary