OSDN Git Service

* Added test code of the <plaintext> tag for au HDML converter.
[modchxj/mod_chxj.git] / src / qs_parse_string.c
1 /*
2  * Copyright (C) 2005-2008 Atsushi Konno All rights reserved.
3  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <stdio.h>
18 #include <strings.h>
19 #include <stdlib.h>
20
21 #include "chxj_apache.h"
22 #include "qs_parse_string.h"
23 #include "qs_parse_tag.h"
24 #include "qs_log.h"
25
26 #include "mod_chxj.h"
27 #include <iconv.h>
28
29
30 #define NL_COUNT_MAX  (10)
31
32 typedef struct node_stack_element {
33   Node *node;
34   struct node_stack_element *next;
35   struct node_stack_element **ref;
36 } *NodeStackElement;
37
38 typedef struct node_stack {
39   NodeStackElement head;
40   NodeStackElement tail;
41 } *NodeStack;
42
43 static int s_cut_tag (const char* s, int len);
44 static int s_cut_text(const char* s, int len, int script);
45 static void qs_dump_node(Doc* doc, Node* node, int indent);
46 static void qs_push_node(Doc* doc, Node *node, NodeStack stack);
47 static Node *qs_pop_node(Doc* doc, NodeStack stack);
48 #ifdef DUMP_NODE_STACK
49 static void qs_dump_node_stack(Doc *doc, NodeStack stack);
50 #endif
51 static void qs_free_node_stack(Doc *doc, NodeStack stack);
52 static void s_error_check(Doc *doc, Node *node, NodeStack node_stack, NodeStack err_stack);
53
54
55 Node *
56 qs_parse_string(Doc *doc, const char *src, int srclen) 
57 {
58   int     ii;
59   int     nl_cnt = 0;
60   char    encoding[256];
61   char    *osrc;
62   char    *ibuf;
63   size_t  olen;
64   size_t  ilen;
65   int     script_flag = 0;
66   iconv_t cd;
67
68   osrc = NULL;
69   ibuf = NULL;
70   NodeStack node_stack;
71   NodeStack err_stack;
72
73   memset(encoding, 0, 256);
74
75   doc->now_parent_node = qs_init_root_node(doc);
76   if (! src || srclen <= 0) {
77     return doc->root_node;
78   }
79   if (doc->r != NULL) {
80     node_stack = apr_palloc(doc->r->pool, sizeof(struct node_stack));
81     memset(node_stack, 0, sizeof(struct node_stack));
82     err_stack = apr_palloc(doc->r->pool, sizeof(struct node_stack));
83     memset(err_stack, 0, sizeof(struct node_stack));
84   }
85   else {
86     node_stack = calloc(sizeof(struct node_stack), 1);
87     err_stack  = calloc(sizeof(struct node_stack), 1);
88   }
89
90   /* 
91    * It is the pre reading. 
92    * Because I want to specify encoding.
93    */
94   for (ii=0; ii<srclen; ii++) {
95     if (src[ii] == '\n')  nl_cnt++;
96     if (nl_cnt >= NL_COUNT_MAX) break; /* not found <?xml ...> */
97
98     if (is_white_space(src[ii]))
99       continue;
100
101     if ((unsigned char)'<' == src[ii]) {
102       int endpoint = s_cut_tag(&src[ii], srclen - ii);
103       Node* node   = NULL;
104       node = qs_parse_tag(doc, &src[ii], endpoint);
105       ii += endpoint;
106
107       if (node->name[0] != '?') break; 
108
109       if (strcasecmp(node->name, "?xml") == 0) {
110         Attr* parse_attr;
111         for(parse_attr = node->attr;
112             parse_attr && *encoding == '\0'; 
113             parse_attr = parse_attr->next) {
114           if ((*parse_attr->name == 'e' || *parse_attr->name == 'E')
115           &&   strcasecmp(parse_attr->name, "encoding") == 0) {
116             switch (*parse_attr->value) {
117             case 'X':
118             case 'x':
119               if (strcasecmp(parse_attr->value, "x-sjis"   ) == 0) {
120                 strcpy((char*)encoding, (char*)"NONE");
121               }
122               break;
123
124             case 'S':
125             case 's':
126               if ((strcasecmp(parse_attr->value, "Shift_JIS") == 0)
127               ||  (strcasecmp(parse_attr->value, "SJIS"     ) == 0)
128               ||  (strcasecmp(parse_attr->value, "Shift-JIS") == 0)) {
129                 strcpy((char*)encoding, (char*)"NONE");
130               }
131               break;
132
133             case 'e':
134             case 'E':
135               if ((strcasecmp(parse_attr->value, "EUC_JP") == 0)
136               ||  (strcasecmp(parse_attr->value, "EUC-JP") == 0)
137               ||  (strcasecmp(parse_attr->value, "EUCJP" ) == 0)) {
138                 strcpy((char*)encoding, "EUC-JP");
139               }
140               break;
141
142             case 'u':
143             case 'U':
144               if ((strcasecmp(parse_attr->value, "UTF-8") == 0)
145               ||  (strcasecmp(parse_attr->value, "UTF8") == 0)) {
146                 strcpy((char*)encoding, "UTF-8");
147               }
148               break;
149
150             default:
151               strcpy((char*)encoding, "NONE");
152               break;
153             }
154           }
155         }
156         break;
157       }
158       break;
159     }
160   }
161
162   if (strcasecmp(encoding, "NONE") != 0 && strlen(encoding) != 0) {
163     char* sv_osrc;
164     olen = srclen * 4 + 1;
165     sv_osrc = osrc =(char*)apr_palloc(doc->pool, olen);
166     memset((char*)osrc, 0, olen);
167     if ((cd = iconv_open("CP932", encoding)) != (iconv_t) -1) {
168       ilen = srclen;
169       ibuf = apr_palloc(doc->pool, ilen+1);
170       memset(ibuf, 0, ilen+1);
171       memcpy(ibuf, src, ilen);
172       while (ilen > 0) {
173         size_t result = iconv(cd, &ibuf, &ilen, &osrc, &olen);
174         if (result == (size_t)(-1)) {
175           break;
176         }
177       }
178       srclen = olen;
179       src = sv_osrc;
180       iconv_close(cd);
181     }
182   }
183
184   /*
185    * Now, true parsing is done here. 
186    */
187   nl_cnt = 1;
188   for (ii=0; ii<srclen; ii++) {
189     if (src[ii] == '\n') nl_cnt++;
190     if (doc->parse_mode != PARSE_MODE_NO_PARSE 
191         && is_white_space(src[ii])
192         && (doc->now_parent_node == NULL || !STRCASEEQ('p','P',"pre",doc->now_parent_node->name))) {
193       continue;
194     }
195     if ((unsigned char)'<' == src[ii]) {
196       int endpoint = s_cut_tag(&src[ii], srclen - ii);
197       Node* node   = NULL;
198       node = qs_parse_tag(doc, &src[ii], endpoint);
199       node->line = nl_cnt;
200
201       ii += endpoint;
202       if (node->name[0] == '/' ) {
203         if (doc->parse_mode == PARSE_MODE_CHTML) {
204           if (has_child(&(node->name[1]))) {
205             if (doc->now_parent_node->parent != NULL) {
206               doc->now_parent_node = doc->now_parent_node->parent;
207               doc->parse_mode = PARSE_MODE_CHTML;
208             }
209             if (STRCASEEQ('s','S',"script",&node->name[1])) {
210               script_flag = 0;
211             }
212             s_error_check(doc, node, node_stack, err_stack);
213           }
214           else {
215             continue;
216           }
217         }
218         else
219         if (doc->parse_mode == PARSE_MODE_NO_PARSE) {
220           if (STRCASEEQ('c','C',"chxj:if",&node->name[1]) || STRCASEEQ('p','P',"plaintext",&node->name[1])) {
221             if (doc->now_parent_node->parent != NULL) {
222               doc->now_parent_node = doc->now_parent_node->parent;
223               doc->parse_mode = PARSE_MODE_CHTML;
224               s_error_check(doc, node, node_stack, err_stack);
225             }
226           }
227         }
228
229         if (doc->parse_mode != PARSE_MODE_NO_PARSE)
230           continue;
231
232       }
233       if (*node->name == '!' && strncmp(node->name, "!--", 3) == 0) {
234         /* comment tag */
235         continue;
236       }
237       qs_add_child_node(doc,node);
238       if ((has_child(node->name) && doc->parse_mode != PARSE_MODE_NO_PARSE) || STRCASEEQ('p','P',"plaintext",node->name)) {
239         qs_push_node(doc, node, node_stack);
240       }
241
242       if (doc->parse_mode == PARSE_MODE_NO_PARSE) {
243         if (node->name[0] == '/')
244           continue;
245       }
246
247       if (doc->parse_mode == PARSE_MODE_CHTML && STRCASEEQ('c','C',"chxj:if", node->name)) {
248         Attr* parse_attr;
249         doc->parse_mode = PARSE_MODE_NO_PARSE;
250         doc->now_parent_node = node;
251         for(parse_attr = node->attr;
252             parse_attr; 
253             parse_attr = parse_attr->next) {
254           if (STRCASEEQ('p','P',"parse",parse_attr->name)) {
255             if (STRCASEEQ('t','T',"true",parse_attr->value)) {
256               doc->parse_mode = PARSE_MODE_CHTML;
257             }
258           }
259         }
260       }
261       else if (doc->parse_mode == PARSE_MODE_CHTML && STRCASEEQ('p','P',"plaintext",node->name)) {
262         doc->parse_mode = PARSE_MODE_NO_PARSE;
263         doc->now_parent_node = node;
264       }
265
266       if (doc->parse_mode == PARSE_MODE_CHTML && has_child(node->name)) {
267         doc->now_parent_node = node;
268       }
269       if (STRCASEEQ('s','S',"script", node->name)) {
270         script_flag = 1;
271       }
272     }
273     else {
274       /* TEXT */
275       int endpoint = s_cut_text(&src[ii], srclen - ii, script_flag);
276       Node* node = qs_new_tag(doc);
277       node->value = (char*)apr_palloc(doc->pool,endpoint+1);
278       node->name  = (char*)apr_palloc(doc->pool,4+1);
279       node->otext = (char*)apr_palloc(doc->pool,endpoint+1);
280       node->size  = endpoint;
281       node->line  = nl_cnt;
282       memset(node->value, 0, endpoint+1);
283       memset(node->otext, 0, endpoint+1);
284       memset(node->name,  0, 4+1       );
285       memcpy(node->value, &src[ii], endpoint);
286       memcpy(node->name,  "text",   4);
287       memcpy(node->otext,node->value, endpoint);
288
289       qs_add_child_node(doc,node);
290       ii += (endpoint - 1);
291     }
292   }
293 #ifdef DEBUG
294   QX_LOGGER_DEBUG("parse_string end");
295 #endif
296 #ifdef DEBUG
297   if (doc->r != NULL) {
298     qs_dump_node(doc, doc->root_node, 0);
299   }
300 #endif
301 #ifdef DUMP_NODE_STACK
302   qs_dump_node_stack(doc, node_stack);
303 #endif
304   {
305     Node* prevNode;
306     for (prevNode = qs_pop_node(doc,node_stack);
307          prevNode;
308          prevNode = qs_pop_node(doc, node_stack)) {
309       if (has_child(prevNode->name)) {
310         if (doc->r)
311           ERR(doc->r, "tag parse error (perhaps, not close). tag_name:[%s] line:[%d]", prevNode->name, prevNode->line);
312         else
313           fprintf(stderr, "error :tag parse error (perhaps, not close). tag_name:[%s] line:[%d]\n", prevNode->name, prevNode->line);
314       }
315     }
316   }
317   qs_free_node_stack(doc, node_stack); node_stack = NULL;
318   qs_free_node_stack(doc, err_stack);  err_stack = NULL;
319   return doc->root_node;
320 }
321
322
323 static void
324 s_error_check(Doc *doc, Node *node, NodeStack node_stack, NodeStack err_stack) 
325 {
326   Node *prevNode;
327   int err = 0;
328   for (prevNode = qs_pop_node(doc,node_stack);
329        prevNode;
330        prevNode = qs_pop_node(doc, node_stack)) {
331     if (prevNode && strcasecmp(prevNode->name, &node->name[1]) != 0) {
332       qs_push_node(doc, prevNode, err_stack);
333       err++;
334       continue;
335     }
336     break;
337   }
338   if (err) {
339     Node *tmpNode = qs_pop_node(doc,node_stack);
340     if (tmpNode == NULL && err != 1) {
341       if (doc->r) 
342         ERR(doc->r, "tag parse error (perhaps, miss spell). tag_name:[%s] line:[%d]", &node->name[1], node->line);
343       else
344         fprintf(stderr, "error :tag parse error (perhaps, miss spell). tag_name:[%s] line:[%d]\n", &node->name[1], node->line);
345       for (prevNode = qs_pop_node(doc,err_stack);
346            prevNode;
347            prevNode = qs_pop_node(doc, err_stack)) {
348         qs_push_node(doc, prevNode, node_stack);
349       }
350     }
351     else {
352       for (prevNode = qs_pop_node(doc,err_stack);
353            prevNode;
354            prevNode = qs_pop_node(doc, err_stack)) {
355         if (doc->r)
356           ERR(doc->r, "tag parse error (perhaps, not close). tag_name:[%s] line:[%d]", prevNode->name, prevNode->line);
357         else
358           fprintf(stderr, "error :tag parse error (perhaps, not close). tag_name:[%s] line:[%d]\n", prevNode->name, prevNode->line);
359       }
360       qs_push_node(doc, tmpNode, node_stack);
361     }
362     err = 0;
363   }
364 }
365
366
367 static void
368 qs_dump_node(Doc* doc, Node* node, int indent) 
369 {
370   Node* p = (Node*)qs_get_child_node(doc,node);
371
372   for (;p;p = (Node*)qs_get_next_node(doc,p)) {
373     Attr* attr;
374     if ((char*)qs_get_node_value(doc,p) != NULL) {
375       DBG(doc->r,"%*.*sNode:[%s][%s]\n", indent,indent," ",
376                       (char*)qs_get_node_name(doc,p),
377                       (char*)qs_get_node_value(doc,p));
378     }
379     else {
380       DBG(doc->r,"%*.*sNode:[%s]\n", indent,indent," ", qs_get_node_name(doc,p));
381     }
382     for (attr = (Attr*)qs_get_attr(doc,p); attr; attr = (Attr*)qs_get_next_attr(doc,attr)) {
383       DBG(doc->r,"%*.*s  ATTR:[%s]\n", indent,indent," ", (char *)qs_get_attr_name(doc,attr));
384       DBG(doc->r,"%*.*s  VAL :[%s]\n", indent,indent," ", (char *)qs_get_attr_value(doc,attr));
385     }
386     qs_dump_node(doc,p, indent+4);
387   }
388 }
389
390
391
392 static int
393 s_cut_tag(const char* s, int len) 
394 {
395   int lv = 0;
396   int ii;
397   int comment = 0;
398   int cdata = 0;
399
400   if (strncmp("<!--", s, 4) == 0) {
401     comment = 1;
402   }
403   else if (strncasecmp("<![CDATA[", s, sizeof("<![CDATA[")-1) == 0) {
404     cdata = 1;
405   }
406
407   for (ii=0;ii<len; ii++) {
408     if (is_sjis_kanji(s[ii])) {
409       ii++;
410       continue;
411     }
412     if (is_sjis_kana(s[ii])) 
413       continue;
414
415     if (is_white_space(s[ii])) 
416       continue;
417
418     if (s[ii] == '<') {
419       lv++;
420       continue;
421     }
422     if (comment && s[ii] == '-') {
423       if (strncmp(&s[ii], "-->", 3) == 0) {
424         ii += 2;
425         break;
426       }
427     }
428     if (cdata && s[ii] == ']') {
429       if (strncmp(&s[ii], "]]>", 3) == 0) {
430         ii += 2;
431         break;
432       }
433     }
434
435     if (!cdata && !comment && s[ii] == '>') {
436       lv--;
437       if (lv == 0) 
438         break;
439       continue;
440     }
441   }
442   return ii;
443 }
444
445 static int
446 s_cut_text(const char* s, int len, int script)
447 {
448   register int ii;
449   register int sq = 0;
450   register int dq = 0;
451
452   for (ii=0;ii<len; ii++) {
453     if (is_sjis_kanji(s[ii])) {
454       ii++;
455       continue;
456     }
457     if (is_sjis_kana(s[ii])) 
458       continue;
459
460     if (is_white_space(s[ii])) 
461       continue;
462
463     if (script) {
464       if (s[ii] == '\\') {
465         ii++;
466         continue;
467       }
468       /* single quote */
469       if (s[ii] == '\'' && !dq) {
470         if (sq) sq--;
471         else    sq++;
472       }
473       /* double quote */
474       if (s[ii] == '"' && !sq) {
475         if (dq) dq--;
476         else    dq++;
477       }
478     }
479
480     if (!sq && !dq && s[ii] == '<') 
481       break;
482
483   }
484
485   return ii;
486 }
487
488
489 Node*
490 qs_init_root_node(Doc* doc) 
491 {
492   doc->root_node = (Node*)apr_palloc(doc->pool,sizeof(struct Node));
493   if (doc->root_node == NULL) 
494     QX_LOGGER_FATAL("Out Of Memory");
495
496   doc->root_node->next   = NULL;
497   doc->root_node->parent = NULL;
498   doc->root_node->child  = NULL;
499   doc->root_node->attr   = NULL;
500
501   doc->root_node->name   = (char*)apr_palloc(doc->pool,5);
502   if (doc->root_node->name == NULL) {
503     QX_LOGGER_FATAL("Out Of Memory");
504   }
505   memset(doc->root_node->name, 0, 5);
506   strcpy(doc->root_node->name, "ROOT");
507
508   return doc->root_node;
509 }
510
511
512
513 void
514 qs_add_child_node(Doc *doc,Node *node) 
515 {
516   node->next       = NULL;
517   node->child      = NULL;
518   node->child_tail = NULL;
519   node->parent = doc->now_parent_node;
520   if (doc->now_parent_node->child == NULL) {
521     doc->now_parent_node->child      = node;
522     doc->now_parent_node->child_tail = node;
523   }
524   else {
525 #ifdef DEBUG
526     QX_LOGGER_DEBUG("search child free node");
527 #endif
528     doc->now_parent_node->child_tail->next = node;
529     doc->now_parent_node->child_tail       = node;
530   }
531 }
532
533
534
535
536 Node *
537 qs_get_root(Doc *doc) {
538   return doc->root_node;
539 }
540
541
542
543
544 char * 
545 qs_get_node_value(Doc *UNUSED(doc), Node *node) {
546   return node->value;
547 }
548
549
550
551
552 char *
553 qs_get_node_name(Doc *UNUSED(doc), Node *node) {
554   return node->name;
555 }
556
557
558
559 Node *
560 qs_get_child_node(Doc *UNUSED(doc), Node *node) {
561   return node->child;
562 }
563
564
565
566
567 Node *
568 qs_get_next_node(Doc* UNUSED(doc), Node *node) {
569   return node->next;
570 }
571
572
573
574 Attr *
575 qs_get_attr(Doc *UNUSED(doc), Node *node) {
576   return node->attr;
577 }
578
579
580
581
582 Attr *
583 qs_get_next_attr(Doc *UNUSED(doc), Attr *attr) {
584   return attr->next;
585 }
586
587
588
589 char *
590 qs_get_attr_name(Doc *UNUSED(doc), Attr *attr) {
591   return attr->name;
592 }
593
594
595
596 char *
597 qs_get_attr_value(Doc *UNUSED(doc), Attr *attr) {
598   return attr->value;
599 }
600
601 int 
602 qs_get_node_size(Doc *UNUSED(doc), Node *node) {
603   return node->size;
604 }
605
606
607 #define list_insert(node, point) do {           \
608     node->ref = point->ref;                     \
609     *node->ref = node;                          \
610     node->next = point;                         \
611     point->ref = &node->next;                   \
612 } while (0)
613
614 #define list_remove(node) do {                  \
615     *node->ref = node->next;                    \
616     node->next->ref = node->ref;                \
617 } while (0)
618
619
620 static void 
621 qs_push_node(Doc* doc, Node *node, NodeStack stack)
622 {
623   NodeStackElement elem;
624   if (doc->r != NULL) {
625     elem = apr_palloc(doc->r->pool, sizeof(struct node_stack_element));
626     memset(elem, 0, sizeof(struct node_stack_element));
627   }
628   else {
629     elem = malloc(sizeof(struct node_stack_element));
630     memset(elem, 0, sizeof(struct node_stack_element));
631   }
632   elem->node = node;
633   if (stack->head == NULL) {
634     /* add dummy head */
635     if (doc->r != NULL) {
636       stack->head = apr_palloc(doc->r->pool, sizeof(struct node_stack_element));
637       memset(stack->head, 0, sizeof(struct node_stack_element));
638     }
639     else {
640       stack->head = malloc(sizeof(struct node_stack_element));
641       memset(stack->head, 0, sizeof(struct node_stack_element));
642     }
643     stack->head->next = stack->head;
644     stack->head->ref = &stack->head->next;
645   }
646   list_insert(elem, stack->head);
647   stack->tail = elem;
648 }
649
650 #include "apr_ring.h"
651
652 static Node *
653 qs_pop_node(Doc *doc, NodeStack stack)
654 {
655   NodeStackElement tail = stack->tail;
656   Node *result;
657
658   if (tail == NULL) return NULL;
659   if (tail == stack->head) return NULL;
660   result = tail->node;
661
662
663   list_remove(tail);
664   stack->tail = (NodeStackElement)((apr_size_t)stack->head->ref - (apr_size_t)APR_OFFSETOF(struct node_stack_element, next));
665   if (doc->r == NULL)
666     free(tail);
667
668   return result;
669 }
670
671 #ifdef DUMP_NODE_STACK
672 static void
673 qs_dump_node_stack(Doc *doc, NodeStack stack)
674 {
675   NodeStackElement elm;
676   for (elm = stack->head->next;elm != stack->head; elm = elm->next) {
677     if (doc->r) DBG(doc->r, "name:[%s]", elm->node->name);
678      else       fprintf(stderr, "[%x] name:[%s] next:[%x]\n", (apr_size_t)elm, elm->node->name, (apr_size_t)elm->next);
679   }
680 }
681 #endif
682
683 static void
684 qs_free_node_stack(Doc *doc, NodeStack stack)
685 {
686   if (doc->r == NULL && stack != NULL) {
687     Node* elm;
688     for (elm = qs_pop_node(doc, stack);elm; elm = qs_pop_node(doc,stack))
689       ;
690     if (stack->head) 
691       free(stack->head);
692     free(stack);
693   }
694 }
695 /*
696  * vim:ts=2 et
697  */