2 * Copyright (C) 1994-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.19 1998-11-03 10:22:39 adam
8 * Fixed memory leak that could occur for when large data1 node were
9 * concatenated. Data-type data1_nodes may have multiple nodes.
11 * Revision 1.18 1998/10/15 13:11:47 adam
12 * Added support for option -record for "end element". When specified
13 * end element will mark end-of-record when at outer-level.
15 * Revision 1.17 1998/07/01 10:13:51 adam
18 * Revision 1.16 1998/06/30 15:15:09 adam
19 * Tags are trimmed: white space removed before- and after the tag.
21 * Revision 1.15 1998/06/30 12:55:45 adam
24 * Revision 1.14 1998/03/05 08:41:00 adam
25 * Implemented rule contexts.
27 * Revision 1.13 1997/12/12 06:33:58 adam
28 * Fixed bug that showed up when multiple filter where used.
29 * Made one routine thread-safe.
31 * Revision 1.12 1997/11/18 10:03:24 adam
32 * Member num_children removed from data1_node.
34 * Revision 1.11 1997/11/06 11:41:01 adam
35 * Implemented "begin variant" for the sgml.regx filter.
37 * Revision 1.10 1997/10/31 12:36:12 adam
38 * Minor change that avoids compiler warning.
40 * Revision 1.9 1997/09/29 09:02:49 adam
41 * Fixed small bug (introduced by previous commit).
43 * Revision 1.8 1997/09/17 12:19:22 adam
44 * Zebra version corresponds to YAZ version 1.4.
45 * Changed Zebra server so that it doesn't depend on global common_resource.
47 * Revision 1.7 1997/07/15 16:33:07 adam
48 * Check for zero length in execData.
50 * Revision 1.6 1997/02/24 10:41:51 adam
51 * Cleanup of code and commented out the "end element-end-record" code.
53 * Revision 1.5 1997/02/19 16:22:33 adam
54 * Fixed "end element" to terminate record in outer-most level.
56 * Revision 1.4 1997/02/12 20:42:58 adam
57 * Changed some log messages.
59 * Revision 1.3 1996/11/08 14:05:33 adam
60 * Bug fix: data1 node member u.tag.get_bytes weren't initialized.
62 * Revision 1.2 1996/10/29 14:02:09 adam
63 * Doesn't use the global data1_tabpath (from YAZ). Instead the function
64 * data1_get_tabpath is used.
66 * Revision 1.1 1996/10/11 10:57:30 adam
67 * New module recctrl. Used to manage records (extract/retrieval).
69 * Revision 1.24 1996/06/17 14:25:31 adam
70 * Removed LOG_DEBUG logs; can still be enabled by setting REGX_DEBUG.
72 * Revision 1.23 1996/06/04 10:19:00 adam
73 * Minor changes - removed include of ctype.h.
75 * Revision 1.22 1996/06/03 15:23:13 adam
76 * Bug fix: /../ BODY /../ - pattern didn't match EOF.
78 * Revision 1.21 1996/05/14 16:58:38 adam
81 * Revision 1.20 1996/05/01 13:46:36 adam
82 * First work on multiple records in one file.
83 * New option, -offset, to the "unread" command in the filter module.
85 * Revision 1.19 1996/02/12 16:18:20 adam
86 * Yet another bug fix in implementation of unread command.
88 * Revision 1.18 1996/02/12 16:07:54 adam
89 * Bug fix in new unread command.
91 * Revision 1.17 1996/02/12 15:56:11 adam
92 * New code command: unread.
94 * Revision 1.16 1996/01/17 14:57:51 adam
95 * Prototype changed for reader functions in extract/retrieve. File
96 * is identified by 'void *' instead of 'int.
98 * Revision 1.15 1996/01/08 19:15:47 adam
99 * New input filter that works!
101 * Revision 1.14 1996/01/08 09:10:38 adam
102 * Yet another complete rework on this module.
104 * Revision 1.13 1995/12/15 17:21:50 adam
105 * This version is able to set data.formatted_text in data1-nodes.
107 * Revision 1.12 1995/12/15 16:20:10 adam
108 * The filter files (*.flt) are read from the path given by data1_tabpath.
110 * Revision 1.11 1995/12/15 12:35:16 adam
113 * Revision 1.10 1995/12/15 10:35:36 adam
116 * Revision 1.9 1995/12/14 16:38:48 adam
117 * Completely new attempt to make regular expression parsing.
119 * Revision 1.8 1995/12/13 17:16:59 adam
122 * Revision 1.7 1995/12/13 16:51:58 adam
123 * Modified to set last_child in data1_nodes.
124 * Uses destroy handler to free up data text nodes.
126 * Revision 1.6 1995/12/13 13:45:37 quinn
127 * Changed data1 to use nmem.
129 * Revision 1.5 1995/12/11 09:12:52 adam
130 * The rec_get function returns NULL if record doesn't exist - will
131 * happen in the server if the result set records have been deleted since
132 * the creation of the set (i.e. the search).
133 * The server saves a result temporarily if it is 'volatile', i.e. the
134 * set is register dependent.
136 * Revision 1.4 1995/12/05 16:57:40 adam
137 * More work on regular patterns.
139 * Revision 1.3 1995/12/05 09:37:09 adam
140 * One malloc was renamed to xmalloc.
142 * Revision 1.2 1995/12/04 17:59:24 adam
143 * More work on regular expression conversion.
145 * Revision 1.1 1995/12/04 14:25:30 adam
146 * Started work on regular expression parsed input to structured records.
155 #include <zebrautl.h>
161 #define F_WIN_EOF 2000000000
165 #define REGX_PATTERN 1
170 #define REGX_CONTEXT 6
176 struct lexRuleAction {
180 struct DFA *dfa; /* REGX_PATTERN */
183 struct regxCode *code; /* REGX_CODE */
185 struct lexRuleAction *next;
190 struct lexRuleAction *actionList;
194 struct lexRuleInfo info;
195 struct lexRule *next;
201 struct lexRule *rules;
202 struct lexRuleInfo **fastRule;
205 struct lexRuleAction *beginActionList;
206 struct lexRuleAction *endActionList;
207 struct lexContext *next;
212 struct lexContext *context;
214 struct lexContext **context_stack;
215 int context_stack_size;
216 int context_stack_top;
222 void (*f_win_ef)(void *, off_t);
224 int f_win_start; /* first byte of buffer is this file offset */
225 int f_win_end; /* last byte of buffer is this offset - 1 */
226 int f_win_size; /* size of buffer */
227 char *f_win_buf; /* buffer itself */
228 int (*f_win_rf)(void *, char *, size_t);
229 off_t (*f_win_sf)(void *, off_t);
234 static char *f_win_get (struct lexSpec *spec, off_t start_pos, off_t end_pos,
237 int i, r, off = start_pos - spec->f_win_start;
239 if (off >= 0 && end_pos <= spec->f_win_end)
241 *size = end_pos - start_pos;
242 return spec->f_win_buf + off;
244 if (off < 0 || start_pos >= spec->f_win_end)
246 (*spec->f_win_sf)(spec->f_win_fh, start_pos);
247 spec->f_win_start = start_pos;
249 if (!spec->f_win_buf)
250 spec->f_win_buf = xmalloc (spec->f_win_size);
251 *size = (*spec->f_win_rf)(spec->f_win_fh, spec->f_win_buf,
253 spec->f_win_end = spec->f_win_start + *size;
255 if (*size > end_pos - start_pos)
256 *size = end_pos - start_pos;
257 return spec->f_win_buf;
259 for (i = 0; i<spec->f_win_end - start_pos; i++)
260 spec->f_win_buf[i] = spec->f_win_buf[i + off];
261 r = (*spec->f_win_rf)(spec->f_win_fh,
263 spec->f_win_size - i);
264 spec->f_win_start = start_pos;
265 spec->f_win_end += r;
267 if (*size > end_pos - start_pos)
268 *size = end_pos - start_pos;
269 return spec->f_win_buf;
272 static int f_win_advance (struct lexSpec *spec, int *pos)
277 if (*pos >= spec->f_win_start && *pos < spec->f_win_end)
278 return spec->f_win_buf[(*pos)++ - spec->f_win_start];
279 if (*pos == F_WIN_EOF)
281 buf = f_win_get (spec, *pos, *pos+1, &size);
291 static void regxCodeDel (struct regxCode **pp)
293 struct regxCode *p = *pp;
302 static void regxCodeMk (struct regxCode **pp, const char *buf, int len)
306 p = xmalloc (sizeof(*p));
307 p->str = xmalloc (len+1);
308 memcpy (p->str, buf, len);
313 static struct DFA *lexSpecDFA (void)
318 dfa_parse_cmap_del (dfa, ' ');
319 dfa_parse_cmap_del (dfa, '\t');
320 dfa_parse_cmap_add (dfa, '/', 0);
324 static void actionListDel (struct lexRuleAction **rap)
326 struct lexRuleAction *ra1, *ra;
328 for (ra = *rap; ra; ra = ra1)
334 dfa_delete (&ra->u.pattern.dfa);
337 regxCodeDel (&ra->u.code);
345 static struct lexContext *lexContextCreate (const char *name)
347 struct lexContext *p = xmalloc (sizeof(*p));
349 p->name = xstrdup (name);
351 p->dfa = lexSpecDFA ();
354 p->beginActionList = NULL;
355 p->endActionList = NULL;
360 static void lexContextDestroy (struct lexContext *p)
362 struct lexRule *rp, *rp1;
365 for (rp = p->rules; rp; rp = rp1)
368 actionListDel (&rp->info.actionList);
371 actionListDel (&p->beginActionList);
372 actionListDel (&p->endActionList);
377 static struct lexSpec *lexSpecCreate (const char *name)
381 p = xmalloc (sizeof(*p));
382 p->name = xmalloc (strlen(name)+1);
383 strcpy (p->name, name);
386 p->context_stack_size = 100;
387 p->context_stack = xmalloc (sizeof(*p->context_stack) *
388 p->context_stack_size);
393 static void lexSpecDestroy (struct lexSpec **pp)
396 struct lexContext *lt;
405 struct lexContext *lt_next = lt->next;
406 lexContextDestroy (lt);
410 xfree (p->f_win_buf);
411 xfree (p->context_stack);
416 static int readParseToken (const char **cpp, int *len)
418 const char *cp = *cpp;
422 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
451 if (*cp >= 'a' && *cp <= 'z')
453 else if (*cp >= 'A' && *cp <= 'Z')
454 cmd[i] = *cp + 'a' - 'A';
457 if (i < sizeof(cmd)-2)
464 logf (LOG_WARN, "bad character %d %c", *cp, *cp);
466 while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
472 if (!strcmp (cmd, "begin"))
474 else if (!strcmp (cmd, "end"))
476 else if (!strcmp (cmd, "body"))
478 else if (!strcmp (cmd, "context"))
482 logf (LOG_WARN, "bad command %s", cmd);
488 static int actionListMk (struct lexSpec *spec, const char *s,
489 struct lexRuleAction **ap)
495 while ((tok = readParseToken (&s, &len)))
503 *ap = xmalloc (sizeof(**ap));
505 regxCodeMk (&(*ap)->u.code, s, len);
509 *ap = xmalloc (sizeof(**ap));
511 (*ap)->u.pattern.body = bodyMark;
513 (*ap)->u.pattern.dfa = lexSpecDFA ();
515 r = dfa_parse ((*ap)->u.pattern.dfa, &s);
520 logf (LOG_WARN, "regular expression error '%.*s'", s-s0, s0);
523 dfa_mkstate ((*ap)->u.pattern.dfa);
527 logf (LOG_WARN, "cannot use begin here");
530 *ap = xmalloc (sizeof(**ap));
540 int readOneSpec (struct lexSpec *spec, const char *s)
544 struct lexContext *lc;
546 tok = readParseToken (&s, &len);
547 if (tok == REGX_CONTEXT)
549 char context_name[32];
550 tok = readParseToken (&s, &len);
551 if (tok != REGX_CODE)
553 logf (LOG_WARN, "missing name after CONTEXT keyword");
558 memcpy (context_name, s, len);
559 context_name[len] = '\0';
560 lc = lexContextCreate (context_name);
561 lc->next = spec->context;
566 spec->context = lexContextCreate ("main");
571 actionListDel (&spec->context->beginActionList);
572 actionListMk (spec, s, &spec->context->beginActionList);
575 actionListDel (&spec->context->endActionList);
576 actionListMk (spec, s, &spec->context->endActionList);
580 logf (LOG_DEBUG, "rule %d %s", spec->context->ruleNo, s);
582 r = dfa_parse (spec->context->dfa, &s);
585 logf (LOG_WARN, "regular expression error. r=%d", r);
590 logf (LOG_WARN, "expects / at end of pattern. got %c", *s);
594 rp = xmalloc (sizeof(*rp));
595 rp->info.no = spec->context->ruleNo++;
596 rp->next = spec->context->rules;
597 spec->context->rules = rp;
598 actionListMk (spec, s, &rp->info.actionList);
603 int readFileSpec (struct lexSpec *spec)
605 struct lexContext *lc;
608 int c, i, errors = 0;
611 lineBuf = xmalloc (1+lineSize);
612 logf (LOG_LOG, "reading regx filter %s.flt", spec->name);
613 sprintf (lineBuf, "%s.flt", spec->name);
614 if (!(spec_inf = yaz_path_fopen (data1_get_tabpath(spec->dh),
617 logf (LOG_ERRNO|LOG_WARN, "cannot read spec file %s", spec->name);
626 if (c == '#' || c == '\n' || c == ' ' || c == '\t')
628 while (c != '\n' && c != EOF)
647 if (c != ' ' && c != '\t')
656 readOneSpec (spec, lineBuf);
657 spec->lineNo += addLine;
666 debug_dfa_followpos = 1;
669 for (lc = spec->context; lc; lc = lc->next)
672 lc->fastRule = xmalloc (sizeof(*lc->fastRule) * lc->ruleNo);
673 for (i = 0; i < lc->ruleNo; i++)
674 lc->fastRule[i] = NULL;
675 for (rp = lc->rules; rp; rp = rp->next)
676 lc->fastRule[rp->info.no] = &rp->info;
677 dfa_mkstate (lc->dfa);
684 static struct lexSpec *curLexSpec = NULL;
686 static void destroy_data (struct data1_node *n)
688 assert (n->which == DATA1N_data);
689 xfree (n->u.data.data);
692 static void execData (struct lexSpec *spec,
693 data1_node **d1_stack, int *d1_level,
694 const char *ebuf, int elen, int formatted_text)
696 struct data1_node *res, *parent;
698 if (elen == 0) /* shouldn't happen, but it does! */
702 logf (LOG_DEBUG, "data (%d bytes) %.15s ... %.*s", elen,
703 ebuf, 15, ebuf + elen-15);
705 logf (LOG_DEBUG, "data (%d bytes) %.*s", elen, elen, ebuf);
707 logf (LOG_DEBUG, "data (%d bytes)", elen);
713 parent = d1_stack[*d1_level -1];
716 res = data1_mk_node (spec->dh, spec->m);
717 res->parent = parent;
718 res->which = DATA1N_data;
719 res->u.data.what = DATA1I_text;
720 res->u.data.len = elen;
721 res->u.data.formatted_text = formatted_text;
722 if (elen > DATA1_LOCALDATA)
723 res->u.data.data = nmem_malloc (spec->m, elen);
725 res->u.data.data = res->lbuf;
726 memcpy (res->u.data.data, ebuf, elen);
727 res->root = parent->root;
729 parent->last_child = res;
730 if (d1_stack[*d1_level])
731 d1_stack[*d1_level]->next = res;
734 d1_stack[*d1_level] = res;
737 static void execDataP (struct lexSpec *spec,
738 data1_node **d1_stack, int *d1_level,
739 const char *ebuf, int elen, int formatted_text)
741 execData (spec, d1_stack, d1_level, ebuf, elen, formatted_text);
744 static void variantBegin (struct lexSpec *spec,
745 data1_node **d1_stack, int *d1_level,
746 const char *class_str, int class_len,
747 const char *type_str, int type_len,
748 const char *value_str, int value_len)
750 struct data1_node *parent = d1_stack[*d1_level -1];
751 char tclass[DATA1_MAX_SYMBOL], ttype[DATA1_MAX_SYMBOL];
758 logf (LOG_WARN, "in variant begin. No record type defined");
761 if (class_len >= DATA1_MAX_SYMBOL)
762 class_len = DATA1_MAX_SYMBOL-1;
763 memcpy (tclass, class_str, class_len);
764 tclass[class_len] = '\0';
766 if (type_len >= DATA1_MAX_SYMBOL)
767 type_len = DATA1_MAX_SYMBOL-1;
768 memcpy (ttype, type_str, type_len);
769 ttype[type_len] = '\0';
772 logf (LOG_DEBUG, "variant begin %s %s (%d)", tclass, ttype, *d1_level);
776 data1_getvartypebyct(spec->dh, parent->root->u.root.absyn->varset,
780 if (parent->which != DATA1N_variant)
782 res = data1_mk_node (spec->dh, spec->m);
783 res->parent = parent;
784 res->which = DATA1N_variant;
785 res->u.variant.type = 0;
786 res->u.variant.value = 0;
787 res->root = parent->root;
789 parent->last_child = res;
790 if (d1_stack[*d1_level])
791 d1_stack[*d1_level]->next = res;
794 d1_stack[*d1_level] = res;
795 d1_stack[++(*d1_level)] = NULL;
797 for (i = *d1_level-1; d1_stack[i]->which == DATA1N_variant; i--)
798 if (d1_stack[i]->u.variant.type == tp)
805 logf (LOG_DEBUG, "variant node (%d)", *d1_level);
807 parent = d1_stack[*d1_level-1];
808 res = data1_mk_node (spec->dh, spec->m);
809 res->parent = parent;
810 res->which = DATA1N_variant;
811 res->root = parent->root;
812 res->u.variant.type = tp;
814 if (value_len >= DATA1_LOCALDATA)
815 value_len =DATA1_LOCALDATA-1;
816 memcpy (res->lbuf, value_str, value_len);
817 res->lbuf[value_len] = '\0';
819 res->u.variant.value = res->lbuf;
821 parent->last_child = res;
822 if (d1_stack[*d1_level])
823 d1_stack[*d1_level]->next = res;
826 d1_stack[*d1_level] = res;
827 d1_stack[++(*d1_level)] = NULL;
830 static void tagStrip (const char **tag, int *len)
834 for (i = *len; i > 0 && isspace((*tag)[i-1]); --i)
837 for (i = 0; i < *len && isspace((*tag)[i]); i++)
843 static void tagBegin (struct lexSpec *spec,
844 data1_node **d1_stack, int *d1_level,
845 const char *tag, int len)
847 struct data1_node *parent = d1_stack[*d1_level -1];
848 data1_element *elem = NULL;
849 data1_node *partag = get_parent_tag(spec->dh, parent);
851 data1_element *e = NULL;
856 logf (LOG_WARN, "in element begin. No record type defined");
859 tagStrip (&tag, &len);
861 res = data1_mk_node (spec->dh, spec->m);
862 res->parent = parent;
863 res->which = DATA1N_tag;
864 res->u.tag.get_bytes = -1;
866 if (len >= DATA1_LOCALDATA)
867 res->u.tag.tag = nmem_malloc (spec->m, len+1);
869 res->u.tag.tag = res->lbuf;
871 memcpy (res->u.tag.tag, tag, len);
872 res->u.tag.tag[len] = '\0';
875 logf (LOG_DEBUG, "begin tag %s (%d)", res->u.tag.tag, *d1_level);
877 if (parent->which == DATA1N_variant)
880 if (!(e = partag->u.tag.element))
883 elem = data1_getelementbytagname (spec->dh, d1_stack[0]->u.root.absyn,
885 res->u.tag.element = elem;
886 res->u.tag.node_selected = 0;
887 res->u.tag.make_variantlist = 0;
888 res->u.tag.no_data_requested = 0;
889 res->root = parent->root;
891 parent->last_child = res;
892 if (d1_stack[*d1_level])
893 d1_stack[*d1_level]->next = res;
896 d1_stack[*d1_level] = res;
897 d1_stack[++(*d1_level)] = NULL;
900 static void tagEnd (struct lexSpec *spec,
901 data1_node **d1_stack, int *d1_level, int min_level,
902 const char *tag, int len)
904 tagStrip (&tag, &len);
905 while (*d1_level > min_level)
910 if ((d1_stack[*d1_level]->which == DATA1N_tag) &&
912 (strlen(d1_stack[*d1_level]->u.tag.tag) == (size_t) len &&
913 !memcmp (d1_stack[*d1_level]->u.tag.tag, tag, len))))
917 logf (LOG_DEBUG, "end tag (%d)", *d1_level);
922 static int tryMatch (struct lexSpec *spec, int *pptr, int *mptr,
925 struct DFA_state *state = dfa->states[0];
928 unsigned char c_prev = 0;
929 int ptr = *pptr; /* current pointer */
930 int start_ptr = *pptr; /* first char of match */
931 int last_ptr = 0; /* last char of match */
932 int last_rule = 0; /* rule number of current match */
937 c = f_win_advance (spec, &ptr);
938 if (ptr == F_WIN_EOF)
955 *mptr = start_ptr; /* match starts here */
956 *pptr = last_ptr; /* match end here (+1) */
959 state = dfa->states[0];
964 else if (c >= t->ch[0] && c <= t->ch[1])
966 state = dfa->states[t->to];
971 last_rule = state->rule_no;
976 last_rule = state->rule_nno;
988 static int execTok (struct lexSpec *spec, const char **src,
989 int arg_no, int *arg_start, int *arg_end,
990 const char **tokBuf, int *tokLen)
992 const char *s = *src;
994 while (*s == ' ' || *s == '\t')
998 if (*s == '$' && s[1] >= '0' && s[1] <= '9')
1002 while (*s >= '0' && *s <= '9')
1003 n = n*10 + (*s++ -'0');
1013 *tokBuf = f_win_get (spec, arg_start[n], arg_end[n], tokLen);
1016 else if (*s == '\"')
1019 while (*s && *s != '\"')
1021 *tokLen = s - *tokBuf;
1026 else if (*s == '\n' || *s == ';')
1034 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
1036 *tokLen = s - *tokBuf;
1043 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
1045 *tokLen = s - *tokBuf;
1051 static char *regxStrz (const char *src, int len, char *str)
1055 memcpy (str, src, len);
1060 static int execCode (struct lexSpec *spec,
1061 int arg_no, int *arg_start, int *arg_end, int *pptr,
1062 struct regxCode *code,
1063 data1_node **d1_stack, int *d1_level)
1065 const char *s = code->str;
1068 const char *cmd_str;
1070 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str, &cmd_len);
1077 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1078 &cmd_str, &cmd_len);
1081 p = regxStrz (cmd_str, cmd_len, ptmp);
1082 if (!strcmp (p, "begin"))
1084 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1085 &cmd_str, &cmd_len);
1088 logf (LOG_WARN, "missing keyword after 'begin'");
1091 p = regxStrz (cmd_str, cmd_len, ptmp);
1092 if (!strcmp (p, "record"))
1094 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1095 &cmd_str, &cmd_len);
1100 static char absynName[64];
1105 memcpy (absynName, cmd_str, cmd_len);
1106 absynName[cmd_len] = '\0';
1109 logf (LOG_DEBUG, "begin record %s", absynName);
1111 if (!(absyn = data1_get_absyn (spec->dh, absynName)))
1112 logf (LOG_WARN, "Unknown tagset: %s", absynName);
1117 res = data1_mk_node (spec->dh, spec->m);
1118 res->which = DATA1N_root;
1119 res->u.root.type = absynName;
1120 res->u.root.absyn = absyn;
1123 d1_stack[*d1_level] = res;
1124 d1_stack[++(*d1_level)] = NULL;
1127 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1128 &cmd_str, &cmd_len);
1130 else if (!strcmp (p, "element"))
1132 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1133 &cmd_str, &cmd_len);
1136 tagBegin (spec, d1_stack, d1_level, cmd_str, cmd_len);
1137 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1138 &cmd_str, &cmd_len);
1140 else if (!strcmp (p, "variant"))
1143 const char *class_str = NULL;
1145 const char *type_str = NULL;
1147 const char *value_str = NULL;
1148 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1149 &cmd_str, &cmd_len);
1152 class_str = cmd_str;
1153 class_len = cmd_len;
1154 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1155 &cmd_str, &cmd_len);
1161 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1162 &cmd_str, &cmd_len);
1165 value_str = cmd_str;
1166 value_len = cmd_len;
1168 variantBegin (spec, d1_stack, d1_level, class_str, class_len,
1169 type_str, type_len, value_str, value_len);
1172 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1173 &cmd_str, &cmd_len);
1175 else if (!strcmp (p, "context"))
1179 struct lexContext *lc = spec->context;
1180 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1181 &cmd_str, &cmd_len);
1182 p = regxStrz (cmd_str, cmd_len, ptmp);
1184 logf (LOG_DEBUG, "begin context %s", p);
1186 while (lc && strcmp (p, lc->name))
1189 spec->context_stack[++(spec->context_stack_top)] = lc;
1191 logf (LOG_WARN, "unknown context %s", p);
1194 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1195 &cmd_str, &cmd_len);
1199 logf (LOG_WARN, "bad keyword '%s' after begin", p);
1202 else if (!strcmp (p, "end"))
1204 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1205 &cmd_str, &cmd_len);
1208 logf (LOG_WARN, "missing keyword after 'end'");
1211 p = regxStrz (cmd_str, cmd_len, ptmp);
1212 if (!strcmp (p, "record"))
1215 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1216 &cmd_str, &cmd_len);
1218 logf (LOG_DEBUG, "end record");
1222 else if (!strcmp (p, "element"))
1225 while ((r = execTok (spec, &s, arg_no, arg_start, arg_end,
1226 &cmd_str, &cmd_len)) == 3)
1228 if (cmd_len==7 && !memcmp ("-record", cmd_str, cmd_len))
1233 tagEnd (spec, d1_stack, d1_level, min_level,
1235 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1236 &cmd_str, &cmd_len);
1239 tagEnd (spec, d1_stack, d1_level, min_level, NULL, 0);
1243 logf (LOG_DEBUG, "end element end records");
1249 else if (!strcmp (p, "context"))
1252 logf (LOG_DEBUG, "end context");
1254 if (spec->context_stack_top)
1255 (spec->context_stack_top)--;
1256 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1257 &cmd_str, &cmd_len);
1260 logf (LOG_WARN, "bad keyword '%s' after end", p);
1262 else if (!strcmp (p, "data"))
1266 const char *element_str = NULL;
1268 while ((r = execTok (spec, &s, arg_no, arg_start, arg_end,
1269 &cmd_str, &cmd_len)) == 3)
1271 if (cmd_len==5 && !memcmp ("-text", cmd_str, cmd_len))
1273 else if (cmd_len==8 && !memcmp ("-element", cmd_str, cmd_len))
1275 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1276 &element_str, &element_len);
1281 logf (LOG_WARN, "bad data option: %.*s",
1286 logf (LOG_WARN, "missing data item after data");
1290 tagBegin (spec, d1_stack, d1_level, element_str, element_len);
1293 execData (spec, d1_stack, d1_level, cmd_str, cmd_len,
1295 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1296 &cmd_str, &cmd_len);
1299 tagEnd (spec, d1_stack, d1_level, 1, NULL, 0);
1301 else if (!strcmp (p, "unread"))
1304 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1305 &cmd_str, &cmd_len);
1306 if (r==3 && cmd_len == 7 && !memcmp ("-offset", cmd_str, cmd_len))
1308 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1309 &cmd_str, &cmd_len);
1312 logf (LOG_WARN, "missing number after -offset");
1315 p = regxStrz (cmd_str, cmd_len, ptmp);
1317 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1318 &cmd_str, &cmd_len);
1324 logf (LOG_WARN, "missing index after unread command");
1327 if (cmd_len != 1 || *cmd_str < '0' || *cmd_str > '9')
1329 logf (LOG_WARN, "bad index after unread command");
1334 no = *cmd_str - '0';
1337 *pptr = arg_start[no] + offset;
1339 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1340 &cmd_str, &cmd_len);
1342 else if (!strcmp (p, "context"))
1346 struct lexContext *lc = spec->context;
1347 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1348 &cmd_str, &cmd_len);
1349 p = regxStrz (cmd_str, cmd_len, ptmp);
1351 while (lc && strcmp (p, lc->name))
1354 spec->context_stack[spec->context_stack_top] = lc;
1356 logf (LOG_WARN, "unknown context %s", p);
1359 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1360 &cmd_str, &cmd_len);
1364 logf (LOG_WARN, "unknown code command '%.*s'", cmd_len, cmd_str);
1365 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1366 &cmd_str, &cmd_len);
1371 logf (LOG_WARN, "ignoring token %.*s", cmd_len, cmd_str);
1373 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str,
1383 * execAction: Execute action specified by 'ap'. Returns 0 if
1384 * the pattern(s) associated by rule and code could be executed
1385 * ok; returns 1 if code couldn't be executed.
1387 static int execAction (struct lexSpec *spec, struct lexRuleAction *ap,
1388 data1_node **d1_stack, int *d1_level,
1389 int start_ptr, int *pptr)
1396 arg_start[0] = start_ptr;
1404 if (ap->u.pattern.body)
1406 arg_start[arg_no] = *pptr;
1407 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1409 arg_end[arg_no] = F_WIN_EOF;
1411 arg_start[arg_no] = F_WIN_EOF;
1412 arg_end[arg_no] = F_WIN_EOF;
1417 arg_end[arg_no] = sptr;
1419 arg_start[arg_no] = sptr;
1420 arg_end[arg_no] = *pptr;
1425 arg_start[arg_no] = *pptr;
1426 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1428 if (sptr != arg_start[arg_no])
1430 arg_end[arg_no] = *pptr;
1435 if (!execCode (spec, arg_no, arg_start, arg_end, pptr,
1436 ap->u.code, d1_stack, d1_level))
1440 arg_start[arg_no] = *pptr;
1441 arg_end[arg_no] = F_WIN_EOF;
1450 static int execRule (struct lexSpec *spec, struct lexContext *context,
1451 data1_node **d1_stack, int *d1_level,
1452 int ruleNo, int start_ptr, int *pptr)
1455 logf (LOG_DEBUG, "exec rule %d", ruleNo);
1457 return execAction (spec, context->fastRule[ruleNo]->actionList,
1458 d1_stack, d1_level, start_ptr, pptr);
1461 data1_node *lexNode (struct lexSpec *spec,
1462 data1_node **d1_stack, int *d1_level, int *ptr)
1464 struct lexContext *context = spec->context_stack[spec->context_stack_top];
1465 struct DFA_state *state = context->dfa->states[0];
1468 unsigned char c_prev = '\n';
1470 int last_rule = 0; /* rule number of current match */
1471 int last_ptr = *ptr; /* last char of match */
1472 int start_ptr = *ptr; /* first char of match */
1473 int skip_ptr = *ptr; /* first char of run */
1477 c = f_win_advance (spec, ptr);
1478 if (*ptr == F_WIN_EOF)
1480 /* end of file met */
1483 /* there was a match */
1484 if (skip_ptr < start_ptr)
1486 /* deal with chars that didn't match */
1489 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1490 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1492 /* restore pointer */
1495 if (!execRule (spec, context, d1_stack, d1_level,
1496 last_rule, start_ptr, ptr))
1498 /* restore skip pointer */
1502 else if (skip_ptr < *ptr)
1504 /* deal with chars that didn't match */
1507 buf = f_win_get (spec, skip_ptr, *ptr, &size);
1508 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1510 if (*ptr == F_WIN_EOF)
1517 { /* no transition for character c ... */
1520 if (skip_ptr < start_ptr)
1522 /* deal with chars that didn't match */
1525 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1526 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1528 /* restore pointer */
1530 if (!execRule (spec, context, d1_stack, d1_level,
1531 last_rule, start_ptr, ptr))
1533 if (spec->f_win_ef && *ptr != F_WIN_EOF)
1536 logf (LOG_DEBUG, "regx: endf ptr=%d", *ptr);
1538 (*spec->f_win_ef)(spec->f_win_fh, *ptr);
1542 context = spec->context_stack[spec->context_stack_top];
1545 last_ptr = start_ptr = *ptr;
1549 c_prev = f_win_advance (spec, &start_ptr);
1554 c_prev = f_win_advance (spec, &start_ptr);
1557 state = context->dfa->states[0];
1560 else if (c >= t->ch[0] && c <= t->ch[1])
1561 { /* transition ... */
1562 state = context->dfa->states[t->to];
1567 last_rule = state->rule_no;
1570 else if (state->rule_nno)
1572 last_rule = state->rule_nno;
1584 static data1_node *lexRoot (struct lexSpec *spec, off_t offset,
1585 const char *context_name)
1587 struct lexContext *lt = spec->context;
1588 data1_node *d1_stack[512];
1592 spec->context_stack_top = 0;
1595 if (!strcmp (lt->name, context_name))
1601 logf (LOG_WARN, "cannot find context %s", context_name);
1604 spec->context_stack[spec->context_stack_top] = lt;
1605 d1_stack[d1_level] = NULL;
1606 if (lt->beginActionList)
1607 execAction (spec, lt->beginActionList, d1_stack, &d1_level, 0, &ptr);
1608 lexNode (spec, d1_stack, &d1_level, &ptr);
1609 if (lt->endActionList)
1610 execAction (spec, lt->endActionList, d1_stack, &d1_level, ptr, &ptr);
1614 data1_node *grs_read_regx (struct grs_read_info *p)
1619 logf (LOG_DEBUG, "grs_read_regx");
1621 if (!curLexSpec || strcmp (curLexSpec->name, p->type))
1624 lexSpecDestroy (&curLexSpec);
1625 curLexSpec = lexSpecCreate (p->type);
1626 curLexSpec->dh = p->dh;
1627 res = readFileSpec (curLexSpec);
1630 lexSpecDestroy (&curLexSpec);
1634 curLexSpec->dh = p->dh;
1637 curLexSpec->f_win_start = 0;
1638 curLexSpec->f_win_end = 0;
1639 curLexSpec->f_win_rf = p->readf;
1640 curLexSpec->f_win_sf = p->seekf;
1641 curLexSpec->f_win_fh = p->fh;
1642 curLexSpec->f_win_ef = p->endf;
1643 curLexSpec->f_win_size = 500000;
1645 curLexSpec->m = p->mem;
1646 return lexRoot (curLexSpec, p->offset, "main");