2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1998-06-30 12:55:45 adam
10 * Revision 1.14 1998/03/05 08:41:00 adam
11 * Implemented rule contexts.
13 * Revision 1.13 1997/12/12 06:33:58 adam
14 * Fixed bug that showed up when multiple filter where used.
15 * Made one routine thread-safe.
17 * Revision 1.12 1997/11/18 10:03:24 adam
18 * Member num_children removed from data1_node.
20 * Revision 1.11 1997/11/06 11:41:01 adam
21 * Implemented "begin variant" for the sgml.regx filter.
23 * Revision 1.10 1997/10/31 12:36:12 adam
24 * Minor change that avoids compiler warning.
26 * Revision 1.9 1997/09/29 09:02:49 adam
27 * Fixed small bug (introduced by previous commit).
29 * Revision 1.8 1997/09/17 12:19:22 adam
30 * Zebra version corresponds to YAZ version 1.4.
31 * Changed Zebra server so that it doesn't depend on global common_resource.
33 * Revision 1.7 1997/07/15 16:33:07 adam
34 * Check for zero length in execData.
36 * Revision 1.6 1997/02/24 10:41:51 adam
37 * Cleanup of code and commented out the "end element-end-record" code.
39 * Revision 1.5 1997/02/19 16:22:33 adam
40 * Fixed "end element" to terminate record in outer-most level.
42 * Revision 1.4 1997/02/12 20:42:58 adam
43 * Changed some log messages.
45 * Revision 1.3 1996/11/08 14:05:33 adam
46 * Bug fix: data1 node member u.tag.get_bytes weren't initialized.
48 * Revision 1.2 1996/10/29 14:02:09 adam
49 * Doesn't use the global data1_tabpath (from YAZ). Instead the function
50 * data1_get_tabpath is used.
52 * Revision 1.1 1996/10/11 10:57:30 adam
53 * New module recctrl. Used to manage records (extract/retrieval).
55 * Revision 1.24 1996/06/17 14:25:31 adam
56 * Removed LOG_DEBUG logs; can still be enabled by setting REGX_DEBUG.
58 * Revision 1.23 1996/06/04 10:19:00 adam
59 * Minor changes - removed include of ctype.h.
61 * Revision 1.22 1996/06/03 15:23:13 adam
62 * Bug fix: /../ BODY /../ - pattern didn't match EOF.
64 * Revision 1.21 1996/05/14 16:58:38 adam
67 * Revision 1.20 1996/05/01 13:46:36 adam
68 * First work on multiple records in one file.
69 * New option, -offset, to the "unread" command in the filter module.
71 * Revision 1.19 1996/02/12 16:18:20 adam
72 * Yet another bug fix in implementation of unread command.
74 * Revision 1.18 1996/02/12 16:07:54 adam
75 * Bug fix in new unread command.
77 * Revision 1.17 1996/02/12 15:56:11 adam
78 * New code command: unread.
80 * Revision 1.16 1996/01/17 14:57:51 adam
81 * Prototype changed for reader functions in extract/retrieve. File
82 * is identified by 'void *' instead of 'int.
84 * Revision 1.15 1996/01/08 19:15:47 adam
85 * New input filter that works!
87 * Revision 1.14 1996/01/08 09:10:38 adam
88 * Yet another complete rework on this module.
90 * Revision 1.13 1995/12/15 17:21:50 adam
91 * This version is able to set data.formatted_text in data1-nodes.
93 * Revision 1.12 1995/12/15 16:20:10 adam
94 * The filter files (*.flt) are read from the path given by data1_tabpath.
96 * Revision 1.11 1995/12/15 12:35:16 adam
99 * Revision 1.10 1995/12/15 10:35:36 adam
102 * Revision 1.9 1995/12/14 16:38:48 adam
103 * Completely new attempt to make regular expression parsing.
105 * Revision 1.8 1995/12/13 17:16:59 adam
108 * Revision 1.7 1995/12/13 16:51:58 adam
109 * Modified to set last_child in data1_nodes.
110 * Uses destroy handler to free up data text nodes.
112 * Revision 1.6 1995/12/13 13:45:37 quinn
113 * Changed data1 to use nmem.
115 * Revision 1.5 1995/12/11 09:12:52 adam
116 * The rec_get function returns NULL if record doesn't exist - will
117 * happen in the server if the result set records have been deleted since
118 * the creation of the set (i.e. the search).
119 * The server saves a result temporarily if it is 'volatile', i.e. the
120 * set is register dependent.
122 * Revision 1.4 1995/12/05 16:57:40 adam
123 * More work on regular patterns.
125 * Revision 1.3 1995/12/05 09:37:09 adam
126 * One malloc was renamed to xmalloc.
128 * Revision 1.2 1995/12/04 17:59:24 adam
129 * More work on regular expression conversion.
131 * Revision 1.1 1995/12/04 14:25:30 adam
132 * Started work on regular expression parsed input to structured records.
140 #include <zebrautl.h>
146 #define F_WIN_EOF 2000000000
150 #define REGX_PATTERN 1
155 #define REGX_CONTEXT 6
161 struct lexRuleAction {
165 struct DFA *dfa; /* REGX_PATTERN */
168 struct regxCode *code; /* REGX_CODE */
170 struct lexRuleAction *next;
175 struct lexRuleAction *actionList;
179 struct lexRuleInfo info;
180 struct lexRule *next;
186 struct lexRule *rules;
187 struct lexRuleInfo **fastRule;
190 struct lexRuleAction *beginActionList;
191 struct lexRuleAction *endActionList;
192 struct lexContext *next;
197 struct lexContext *context;
199 struct lexContext **context_stack;
200 int context_stack_size;
201 int context_stack_top;
207 void (*f_win_ef)(void *, off_t);
209 int f_win_start; /* first byte of buffer is this file offset */
210 int f_win_end; /* last byte of buffer is this offset - 1 */
211 int f_win_size; /* size of buffer */
212 char *f_win_buf; /* buffer itself */
213 int (*f_win_rf)(void *, char *, size_t);
214 off_t (*f_win_sf)(void *, off_t);
219 static char *f_win_get (struct lexSpec *spec, off_t start_pos, off_t end_pos,
222 int i, r, off = start_pos - spec->f_win_start;
224 if (off >= 0 && end_pos <= spec->f_win_end)
226 *size = end_pos - start_pos;
227 return spec->f_win_buf + off;
229 if (off < 0 || start_pos >= spec->f_win_end)
231 (*spec->f_win_sf)(spec->f_win_fh, start_pos);
232 spec->f_win_start = start_pos;
234 if (!spec->f_win_buf)
235 spec->f_win_buf = xmalloc (spec->f_win_size);
236 *size = (*spec->f_win_rf)(spec->f_win_fh, spec->f_win_buf,
238 spec->f_win_end = spec->f_win_start + *size;
240 if (*size > end_pos - start_pos)
241 *size = end_pos - start_pos;
242 return spec->f_win_buf;
244 for (i = 0; i<spec->f_win_end - start_pos; i++)
245 spec->f_win_buf[i] = spec->f_win_buf[i + off];
246 r = (*spec->f_win_rf)(spec->f_win_fh,
248 spec->f_win_size - i);
249 spec->f_win_start = start_pos;
250 spec->f_win_end += r;
252 if (*size > end_pos - start_pos)
253 *size = end_pos - start_pos;
254 return spec->f_win_buf;
257 static int f_win_advance (struct lexSpec *spec, int *pos)
262 if (*pos >= spec->f_win_start && *pos < spec->f_win_end)
263 return spec->f_win_buf[(*pos)++ - spec->f_win_start];
264 if (*pos == F_WIN_EOF)
266 buf = f_win_get (spec, *pos, *pos+1, &size);
276 static void regxCodeDel (struct regxCode **pp)
278 struct regxCode *p = *pp;
287 static void regxCodeMk (struct regxCode **pp, const char *buf, int len)
291 p = xmalloc (sizeof(*p));
292 p->str = xmalloc (len+1);
293 memcpy (p->str, buf, len);
298 static struct DFA *lexSpecDFA (void)
303 dfa_parse_cmap_del (dfa, ' ');
304 dfa_parse_cmap_del (dfa, '\t');
305 dfa_parse_cmap_add (dfa, '/', 0);
309 static void actionListDel (struct lexRuleAction **rap)
311 struct lexRuleAction *ra1, *ra;
313 for (ra = *rap; ra; ra = ra1)
319 dfa_delete (&ra->u.pattern.dfa);
322 regxCodeDel (&ra->u.code);
330 static struct lexContext *lexContextCreate (const char *name)
332 struct lexContext *p = xmalloc (sizeof(*p));
334 p->name = xstrdup (name);
336 p->dfa = lexSpecDFA ();
339 p->beginActionList = NULL;
340 p->endActionList = NULL;
345 static void lexContextDestroy (struct lexContext *p)
347 struct lexRule *rp, *rp1;
350 for (rp = p->rules; rp; rp = rp1)
353 actionListDel (&rp->info.actionList);
356 actionListDel (&p->beginActionList);
357 actionListDel (&p->endActionList);
362 static struct lexSpec *lexSpecCreate (const char *name)
366 p = xmalloc (sizeof(*p));
367 p->name = xmalloc (strlen(name)+1);
368 strcpy (p->name, name);
371 p->context_stack_size = 100;
372 p->context_stack = xmalloc (sizeof(*p->context_stack) *
373 p->context_stack_size);
378 static void lexSpecDestroy (struct lexSpec **pp)
381 struct lexContext *lt;
390 struct lexContext *lt_next = lt->next;
391 lexContextDestroy (lt);
395 xfree (p->f_win_buf);
396 xfree (p->context_stack);
401 static int readParseToken (const char **cpp, int *len)
403 const char *cp = *cpp;
407 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
436 if (*cp >= 'a' && *cp <= 'z')
438 else if (*cp >= 'A' && *cp <= 'Z')
439 cmd[i] = *cp + 'a' - 'A';
442 if (i < sizeof(cmd)-2)
449 logf (LOG_WARN, "bad character %d %c", *cp, *cp);
451 while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
457 if (!strcmp (cmd, "begin"))
459 else if (!strcmp (cmd, "end"))
461 else if (!strcmp (cmd, "body"))
463 else if (!strcmp (cmd, "context"))
467 logf (LOG_WARN, "bad command %s", cmd);
473 static int actionListMk (struct lexSpec *spec, const char *s,
474 struct lexRuleAction **ap)
480 while ((tok = readParseToken (&s, &len)))
488 *ap = xmalloc (sizeof(**ap));
490 regxCodeMk (&(*ap)->u.code, s, len);
494 *ap = xmalloc (sizeof(**ap));
496 (*ap)->u.pattern.body = bodyMark;
498 (*ap)->u.pattern.dfa = lexSpecDFA ();
500 r = dfa_parse ((*ap)->u.pattern.dfa, &s);
505 logf (LOG_WARN, "regular expression error '%.*s'", s-s0, s0);
508 dfa_mkstate ((*ap)->u.pattern.dfa);
512 logf (LOG_WARN, "cannot use begin here");
515 *ap = xmalloc (sizeof(**ap));
525 int readOneSpec (struct lexSpec *spec, const char *s)
529 struct lexContext *lc;
531 tok = readParseToken (&s, &len);
532 if (tok == REGX_CONTEXT)
534 char context_name[32];
535 tok = readParseToken (&s, &len);
536 if (tok != REGX_CODE)
538 logf (LOG_WARN, "missing name after CONTEXT keyword");
543 memcpy (context_name, s, len);
544 context_name[len] = '\0';
545 lc = lexContextCreate (context_name);
546 lc->next = spec->context;
551 spec->context = lexContextCreate ("main");
556 actionListDel (&spec->context->beginActionList);
557 actionListMk (spec, s, &spec->context->beginActionList);
560 actionListDel (&spec->context->endActionList);
561 actionListMk (spec, s, &spec->context->endActionList);
565 logf (LOG_DEBUG, "rule %d %s", spec->context->ruleNo, s);
567 r = dfa_parse (spec->context->dfa, &s);
570 logf (LOG_WARN, "regular expression error. r=%d", r);
575 logf (LOG_WARN, "expects / at end of pattern. got %c", *s);
579 rp = xmalloc (sizeof(*rp));
580 rp->info.no = spec->context->ruleNo++;
581 rp->next = spec->context->rules;
582 spec->context->rules = rp;
583 actionListMk (spec, s, &rp->info.actionList);
588 int readFileSpec (struct lexSpec *spec)
590 struct lexContext *lc;
593 int c, i, errors = 0;
596 lineBuf = xmalloc (1+lineSize);
597 logf (LOG_LOG, "reading regx filter %s.flt", spec->name);
598 sprintf (lineBuf, "%s.flt", spec->name);
599 if (!(spec_inf = yaz_path_fopen (data1_get_tabpath(spec->dh),
602 logf (LOG_ERRNO|LOG_WARN, "cannot read spec file %s", spec->name);
611 if (c == '#' || c == '\n' || c == ' ' || c == '\t')
613 while (c != '\n' && c != EOF)
632 if (c != ' ' && c != '\t')
641 readOneSpec (spec, lineBuf);
642 spec->lineNo += addLine;
651 debug_dfa_followpos = 1;
654 for (lc = spec->context; lc; lc = lc->next)
657 lc->fastRule = xmalloc (sizeof(*lc->fastRule) * lc->ruleNo);
658 for (i = 0; i < lc->ruleNo; i++)
659 lc->fastRule[i] = NULL;
660 for (rp = lc->rules; rp; rp = rp->next)
661 lc->fastRule[rp->info.no] = &rp->info;
662 dfa_mkstate (lc->dfa);
669 static struct lexSpec *curLexSpec = NULL;
671 static void destroy_data (struct data1_node *n)
673 assert (n->which == DATA1N_data);
674 xfree (n->u.data.data);
677 static void execData (struct lexSpec *spec,
678 data1_node **d1_stack, int *d1_level,
679 const char *ebuf, int elen, int formatted_text)
681 struct data1_node *res, *parent;
683 if (elen == 0) /* shouldn't happen, but it does! */
687 logf (LOG_DEBUG, "data (%d bytes) %.15s ... %.*s", elen,
688 ebuf, 15, ebuf + elen-15);
690 logf (LOG_DEBUG, "data (%d bytes) %.*s", elen, elen, ebuf);
692 logf (LOG_DEBUG, "data (%d bytes)", elen);
698 parent = d1_stack[*d1_level -1];
700 if ((res=d1_stack[*d1_level]) && res->which == DATA1N_data)
702 if (elen + res->u.data.len <= DATA1_LOCALDATA)
703 memcpy (res->u.data.data + res->u.data.len, ebuf, elen);
706 char *nb = xmalloc (elen + res->u.data.len);
707 memcpy (nb, res->u.data.data, res->u.data.len);
708 memcpy (nb + res->u.data.len, ebuf, elen);
709 res->u.data.data = nb;
710 res->destroy = destroy_data;
712 res->u.data.len += elen;
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;
738 static void execDataP (struct lexSpec *spec,
739 data1_node **d1_stack, int *d1_level,
740 const char *ebuf, int elen, int formatted_text)
742 execData (spec, d1_stack, d1_level, ebuf, elen, formatted_text);
745 static void variantBegin (struct lexSpec *spec,
746 data1_node **d1_stack, int *d1_level,
747 const char *class_str, int class_len,
748 const char *type_str, int type_len,
749 const char *value_str, int value_len)
751 struct data1_node *parent = d1_stack[*d1_level -1];
752 char tclass[DATA1_MAX_SYMBOL], ttype[DATA1_MAX_SYMBOL];
759 logf (LOG_WARN, "in variant begin. No record type defined");
762 if (class_len >= DATA1_MAX_SYMBOL)
763 class_len = DATA1_MAX_SYMBOL-1;
764 memcpy (tclass, class_str, class_len);
765 tclass[class_len] = '\0';
767 if (type_len >= DATA1_MAX_SYMBOL)
768 type_len = DATA1_MAX_SYMBOL-1;
769 memcpy (ttype, type_str, type_len);
770 ttype[type_len] = '\0';
773 logf (LOG_DEBUG, "variant begin %s %s (%d)", tclass, ttype, *d1_level);
777 data1_getvartypebyct(spec->dh, parent->root->u.root.absyn->varset,
781 if (parent->which != DATA1N_variant)
783 res = data1_mk_node (spec->dh, spec->m);
784 res->parent = parent;
785 res->which = DATA1N_variant;
786 res->u.variant.type = 0;
787 res->u.variant.value = 0;
788 res->root = parent->root;
790 parent->last_child = res;
791 if (d1_stack[*d1_level])
792 d1_stack[*d1_level]->next = res;
795 d1_stack[*d1_level] = res;
796 d1_stack[++(*d1_level)] = NULL;
798 for (i = *d1_level-1; d1_stack[i]->which == DATA1N_variant; i--)
799 if (d1_stack[i]->u.variant.type == tp)
806 logf (LOG_DEBUG, "variant node (%d)", *d1_level);
808 parent = d1_stack[*d1_level-1];
809 res = data1_mk_node (spec->dh, spec->m);
810 res->parent = parent;
811 res->which = DATA1N_variant;
812 res->root = parent->root;
813 res->u.variant.type = tp;
815 if (value_len >= DATA1_LOCALDATA)
816 value_len =DATA1_LOCALDATA-1;
817 memcpy (res->lbuf, value_str, value_len);
818 res->lbuf[value_len] = '\0';
820 res->u.variant.value = res->lbuf;
822 parent->last_child = res;
823 if (d1_stack[*d1_level])
824 d1_stack[*d1_level]->next = res;
827 d1_stack[*d1_level] = res;
828 d1_stack[++(*d1_level)] = NULL;
831 static void tagBegin (struct lexSpec *spec,
832 data1_node **d1_stack, int *d1_level,
833 const char *tag, int len)
835 struct data1_node *parent = d1_stack[*d1_level -1];
836 data1_element *elem = NULL;
837 data1_node *partag = get_parent_tag(spec->dh, parent);
839 data1_element *e = NULL;
844 logf (LOG_WARN, "in element begin. No record type defined");
848 res = data1_mk_node (spec->dh, spec->m);
849 res->parent = parent;
850 res->which = DATA1N_tag;
851 res->u.tag.get_bytes = -1;
853 if (len >= DATA1_LOCALDATA)
854 len = DATA1_LOCALDATA-1;
855 memcpy (res->lbuf, tag, len);
856 res->lbuf[len] = '\0';
857 res->u.tag.tag = res->lbuf;
860 logf (LOG_DEBUG, "begin tag %s (%d)", res->u.tag.tag, *d1_level);
862 if (parent->which == DATA1N_variant)
865 if (!(e = partag->u.tag.element))
868 elem = data1_getelementbytagname (spec->dh, d1_stack[0]->u.root.absyn,
870 res->u.tag.element = elem;
871 res->u.tag.node_selected = 0;
872 res->u.tag.make_variantlist = 0;
873 res->u.tag.no_data_requested = 0;
874 res->root = parent->root;
876 parent->last_child = res;
877 if (d1_stack[*d1_level])
878 d1_stack[*d1_level]->next = res;
881 d1_stack[*d1_level] = res;
882 d1_stack[++(*d1_level)] = NULL;
885 static void tagEnd (struct lexSpec *spec,
886 data1_node **d1_stack, int *d1_level,
887 const char *tag, int len)
889 while (*d1_level > 1)
892 if ((d1_stack[*d1_level]->which == DATA1N_tag) &&
894 (strlen(d1_stack[*d1_level]->u.tag.tag) == (size_t) len &&
895 !memcmp (d1_stack[*d1_level]->u.tag.tag, tag, len))))
899 logf (LOG_DEBUG, "end tag (%d)", *d1_level);
904 static int tryMatch (struct lexSpec *spec, int *pptr, int *mptr,
907 struct DFA_state *state = dfa->states[0];
910 unsigned char c_prev = 0;
911 int ptr = *pptr; /* current pointer */
912 int start_ptr = *pptr; /* first char of match */
913 int last_ptr = 0; /* last char of match */
914 int last_rule = 0; /* rule number of current match */
919 c = f_win_advance (spec, &ptr);
920 if (ptr == F_WIN_EOF)
937 *mptr = start_ptr; /* match starts here */
938 *pptr = last_ptr; /* match end here (+1) */
941 state = dfa->states[0];
946 else if (c >= t->ch[0] && c <= t->ch[1])
948 state = dfa->states[t->to];
953 last_rule = state->rule_no;
958 last_rule = state->rule_nno;
970 static int execTok (struct lexSpec *spec, const char **src,
971 int arg_no, int *arg_start, int *arg_end,
972 const char **tokBuf, int *tokLen)
974 const char *s = *src;
976 while (*s == ' ' || *s == '\t')
980 if (*s == '$' && s[1] >= '0' && s[1] <= '9')
984 while (*s >= '0' && *s <= '9')
985 n = n*10 + (*s++ -'0');
995 *tokBuf = f_win_get (spec, arg_start[n], arg_end[n], tokLen);
1001 while (*s && *s != '\"')
1003 *tokLen = s - *tokBuf;
1008 else if (*s == '\n' || *s == ';')
1016 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
1018 *tokLen = s - *tokBuf;
1025 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
1027 *tokLen = s - *tokBuf;
1033 static char *regxStrz (const char *src, int len, char *str)
1037 memcpy (str, src, len);
1042 static int execCode (struct lexSpec *spec,
1043 int arg_no, int *arg_start, int *arg_end, int *pptr,
1044 struct regxCode *code,
1045 data1_node **d1_stack, int *d1_level)
1047 const char *s = code->str;
1050 const char *cmd_str;
1052 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str, &cmd_len);
1059 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1060 &cmd_str, &cmd_len);
1063 p = regxStrz (cmd_str, cmd_len, ptmp);
1064 if (!strcmp (p, "begin"))
1066 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1067 &cmd_str, &cmd_len);
1070 logf (LOG_WARN, "missing keyword after 'begin'");
1073 p = regxStrz (cmd_str, cmd_len, ptmp);
1074 if (!strcmp (p, "record"))
1076 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1077 &cmd_str, &cmd_len);
1082 static char absynName[64];
1087 memcpy (absynName, cmd_str, cmd_len);
1088 absynName[cmd_len] = '\0';
1091 logf (LOG_DEBUG, "begin record %s", absynName);
1093 if (!(absyn = data1_get_absyn (spec->dh, absynName)))
1094 logf (LOG_WARN, "Unknown tagset: %s", absynName);
1099 res = data1_mk_node (spec->dh, spec->m);
1100 res->which = DATA1N_root;
1101 res->u.root.type = absynName;
1102 res->u.root.absyn = absyn;
1105 d1_stack[*d1_level] = res;
1106 d1_stack[++(*d1_level)] = NULL;
1109 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1110 &cmd_str, &cmd_len);
1112 else if (!strcmp (p, "element"))
1114 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1115 &cmd_str, &cmd_len);
1118 tagBegin (spec, d1_stack, d1_level, cmd_str, cmd_len);
1119 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1120 &cmd_str, &cmd_len);
1122 else if (!strcmp (p, "variant"))
1125 const char *class_str = NULL;
1127 const char *type_str = NULL;
1129 const char *value_str = NULL;
1130 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1131 &cmd_str, &cmd_len);
1134 class_str = cmd_str;
1135 class_len = cmd_len;
1136 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1137 &cmd_str, &cmd_len);
1143 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1144 &cmd_str, &cmd_len);
1147 value_str = cmd_str;
1148 value_len = cmd_len;
1150 variantBegin (spec, d1_stack, d1_level, class_str, class_len,
1151 type_str, type_len, value_str, value_len);
1154 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1155 &cmd_str, &cmd_len);
1157 else if (!strcmp (p, "context"))
1161 struct lexContext *lc = spec->context;
1162 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1163 &cmd_str, &cmd_len);
1164 p = regxStrz (cmd_str, cmd_len, ptmp);
1166 logf (LOG_DEBUG, "begin context %s", p);
1168 while (lc && strcmp (p, lc->name))
1171 spec->context_stack[++(spec->context_stack_top)] = lc;
1173 logf (LOG_WARN, "unknown context %s", p);
1176 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1177 &cmd_str, &cmd_len);
1181 logf (LOG_WARN, "bad keyword '%s' after begin", p);
1184 else if (!strcmp (p, "end"))
1186 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1187 &cmd_str, &cmd_len);
1190 logf (LOG_WARN, "missing keyword after 'end'");
1193 p = regxStrz (cmd_str, cmd_len, ptmp);
1194 if (!strcmp (p, "record"))
1197 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1198 &cmd_str, &cmd_len);
1200 logf (LOG_DEBUG, "end record");
1204 else if (!strcmp (p, "element"))
1206 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1207 &cmd_str, &cmd_len);
1217 tagEnd (spec, d1_stack, d1_level, cmd_str, cmd_len);
1218 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1219 &cmd_str, &cmd_len);
1222 tagEnd (spec, d1_stack, d1_level, NULL, 0);
1224 else if (!strcmp (p, "context"))
1227 logf (LOG_DEBUG, "end context");
1229 if (spec->context_stack_top)
1230 (spec->context_stack_top)--;
1231 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1232 &cmd_str, &cmd_len);
1235 logf (LOG_WARN, "bad keyword '%s' after end", p);
1237 else if (!strcmp (p, "data"))
1241 const char *element_str = NULL;
1243 while ((r = execTok (spec, &s, arg_no, arg_start, arg_end,
1244 &cmd_str, &cmd_len)) == 3)
1246 if (cmd_len==5 && !memcmp ("-text", cmd_str, cmd_len))
1248 else if (cmd_len==8 && !memcmp ("-element", cmd_str, cmd_len))
1250 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1251 &element_str, &element_len);
1256 logf (LOG_WARN, "bad data option: %.*s",
1261 logf (LOG_WARN, "missing data item after data");
1265 tagBegin (spec, d1_stack, d1_level, element_str, element_len);
1268 execData (spec, d1_stack, d1_level, cmd_str, cmd_len,
1270 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1271 &cmd_str, &cmd_len);
1274 tagEnd (spec, d1_stack, d1_level, NULL, 0);
1276 else if (!strcmp (p, "unread"))
1279 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1280 &cmd_str, &cmd_len);
1281 if (r==3 && cmd_len == 7 && !memcmp ("-offset", cmd_str, cmd_len))
1283 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1284 &cmd_str, &cmd_len);
1287 logf (LOG_WARN, "missing number after -offset");
1290 p = regxStrz (cmd_str, cmd_len, ptmp);
1292 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1293 &cmd_str, &cmd_len);
1299 logf (LOG_WARN, "missing index after unread command");
1302 if (cmd_len != 1 || *cmd_str < '0' || *cmd_str > '9')
1304 logf (LOG_WARN, "bad index after unread command");
1309 no = *cmd_str - '0';
1312 *pptr = arg_start[no] + offset;
1314 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1315 &cmd_str, &cmd_len);
1317 else if (!strcmp (p, "context"))
1321 struct lexContext *lc = spec->context;
1322 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1323 &cmd_str, &cmd_len);
1324 p = regxStrz (cmd_str, cmd_len, ptmp);
1326 while (lc && strcmp (p, lc->name))
1329 spec->context_stack[spec->context_stack_top] = lc;
1331 logf (LOG_WARN, "unknown context %s", p);
1334 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1335 &cmd_str, &cmd_len);
1339 logf (LOG_WARN, "unknown code command '%.*s'", cmd_len, cmd_str);
1340 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1341 &cmd_str, &cmd_len);
1346 logf (LOG_WARN, "ignoring token %.*s", cmd_len, cmd_str);
1348 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str,
1358 * execAction: Execute action specified by 'ap'. Returns 0 if
1359 * the pattern(s) associated by rule and code could be executed
1360 * ok; returns 1 if code couldn't be executed.
1362 static int execAction (struct lexSpec *spec, struct lexRuleAction *ap,
1363 data1_node **d1_stack, int *d1_level,
1364 int start_ptr, int *pptr)
1371 arg_start[0] = start_ptr;
1379 if (ap->u.pattern.body)
1381 arg_start[arg_no] = *pptr;
1382 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1384 arg_end[arg_no] = F_WIN_EOF;
1386 arg_start[arg_no] = F_WIN_EOF;
1387 arg_end[arg_no] = F_WIN_EOF;
1392 arg_end[arg_no] = sptr;
1394 arg_start[arg_no] = sptr;
1395 arg_end[arg_no] = *pptr;
1400 arg_start[arg_no] = *pptr;
1401 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1403 if (sptr != arg_start[arg_no])
1405 arg_end[arg_no] = *pptr;
1410 if (!execCode (spec, arg_no, arg_start, arg_end, pptr,
1411 ap->u.code, d1_stack, d1_level))
1415 arg_start[arg_no] = *pptr;
1416 arg_end[arg_no] = F_WIN_EOF;
1425 static int execRule (struct lexSpec *spec, struct lexContext *context,
1426 data1_node **d1_stack, int *d1_level,
1427 int ruleNo, int start_ptr, int *pptr)
1430 logf (LOG_DEBUG, "exec rule %d", ruleNo);
1432 return execAction (spec, context->fastRule[ruleNo]->actionList,
1433 d1_stack, d1_level, start_ptr, pptr);
1436 data1_node *lexNode (struct lexSpec *spec,
1437 data1_node **d1_stack, int *d1_level, int *ptr)
1439 struct lexContext *context = spec->context_stack[spec->context_stack_top];
1440 struct DFA_state *state = context->dfa->states[0];
1443 unsigned char c_prev = '\n';
1445 int last_rule = 0; /* rule number of current match */
1446 int last_ptr = *ptr; /* last char of match */
1447 int start_ptr = *ptr; /* first char of match */
1448 int skip_ptr = *ptr; /* first char of run */
1452 c = f_win_advance (spec, ptr);
1453 if (*ptr == F_WIN_EOF)
1455 /* end of file met */
1458 /* there was a match */
1459 if (skip_ptr < start_ptr)
1461 /* deal with chars that didn't match */
1464 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1465 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1467 /* restore pointer */
1470 if (!execRule (spec, context, d1_stack, d1_level,
1471 last_rule, start_ptr, ptr))
1473 /* restore skip pointer */
1477 else if (skip_ptr < *ptr)
1479 /* deal with chars that didn't match */
1482 buf = f_win_get (spec, skip_ptr, *ptr, &size);
1483 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1485 if (*ptr == F_WIN_EOF)
1492 { /* no transition for character c ... */
1495 if (skip_ptr < start_ptr)
1497 /* deal with chars that didn't match */
1500 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1501 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1503 /* restore pointer */
1505 if (!execRule (spec, context, d1_stack, d1_level,
1506 last_rule, start_ptr, ptr))
1508 if (spec->f_win_ef && *ptr != F_WIN_EOF)
1511 logf (LOG_DEBUG, "regx: endf ptr=%d", *ptr);
1513 (*spec->f_win_ef)(spec->f_win_fh, *ptr);
1517 context = spec->context_stack[spec->context_stack_top];
1520 last_ptr = start_ptr = *ptr;
1524 c_prev = f_win_advance (spec, &start_ptr);
1529 c_prev = f_win_advance (spec, &start_ptr);
1532 state = context->dfa->states[0];
1535 else if (c >= t->ch[0] && c <= t->ch[1])
1536 { /* transition ... */
1537 state = context->dfa->states[t->to];
1542 last_rule = state->rule_no;
1545 else if (state->rule_nno)
1547 last_rule = state->rule_nno;
1559 static data1_node *lexRoot (struct lexSpec *spec, off_t offset,
1560 const char *context_name)
1562 struct lexContext *lt = spec->context;
1563 data1_node *d1_stack[512];
1567 spec->context_stack_top = 0;
1570 if (!strcmp (lt->name, context_name))
1576 logf (LOG_WARN, "cannot find context %s", context_name);
1579 spec->context_stack[spec->context_stack_top] = lt;
1580 d1_stack[d1_level] = NULL;
1581 if (lt->beginActionList)
1582 execAction (spec, lt->beginActionList, d1_stack, &d1_level, 0, &ptr);
1583 lexNode (spec, d1_stack, &d1_level, &ptr);
1584 if (lt->endActionList)
1585 execAction (spec, lt->endActionList, d1_stack, &d1_level, ptr, &ptr);
1589 data1_node *grs_read_regx (struct grs_read_info *p)
1594 logf (LOG_DEBUG, "grs_read_regx");
1596 if (!curLexSpec || strcmp (curLexSpec->name, p->type))
1599 lexSpecDestroy (&curLexSpec);
1600 curLexSpec = lexSpecCreate (p->type);
1601 curLexSpec->dh = p->dh;
1602 res = readFileSpec (curLexSpec);
1605 lexSpecDestroy (&curLexSpec);
1611 curLexSpec->f_win_start = 0;
1612 curLexSpec->f_win_end = 0;
1613 curLexSpec->f_win_rf = p->readf;
1614 curLexSpec->f_win_sf = p->seekf;
1615 curLexSpec->f_win_fh = p->fh;
1616 curLexSpec->f_win_ef = p->endf;
1617 curLexSpec->f_win_size = 500000;
1619 curLexSpec->m = p->mem;
1620 return lexRoot (curLexSpec, p->offset, "main");