2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1997-11-06 11:41:01 adam
8 * Implemented "begin variant" for the sgml.regx filter.
10 * Revision 1.10 1997/10/31 12:36:12 adam
11 * Minor change that avoids compiler warning.
13 * Revision 1.9 1997/09/29 09:02:49 adam
14 * Fixed small bug (introduced by previous commit).
16 * Revision 1.8 1997/09/17 12:19:22 adam
17 * Zebra version corresponds to YAZ version 1.4.
18 * Changed Zebra server so that it doesn't depend on global common_resource.
20 * Revision 1.7 1997/07/15 16:33:07 adam
21 * Check for zero length in execData.
23 * Revision 1.6 1997/02/24 10:41:51 adam
24 * Cleanup of code and commented out the "end element-end-record" code.
26 * Revision 1.5 1997/02/19 16:22:33 adam
27 * Fixed "end element" to terminate record in outer-most level.
29 * Revision 1.4 1997/02/12 20:42:58 adam
30 * Changed some log messages.
32 * Revision 1.3 1996/11/08 14:05:33 adam
33 * Bug fix: data1 node member u.tag.get_bytes weren't initialized.
35 * Revision 1.2 1996/10/29 14:02:09 adam
36 * Doesn't use the global data1_tabpath (from YAZ). Instead the function
37 * data1_get_tabpath is used.
39 * Revision 1.1 1996/10/11 10:57:30 adam
40 * New module recctrl. Used to manage records (extract/retrieval).
42 * Revision 1.24 1996/06/17 14:25:31 adam
43 * Removed LOG_DEBUG logs; can still be enabled by setting REGX_DEBUG.
45 * Revision 1.23 1996/06/04 10:19:00 adam
46 * Minor changes - removed include of ctype.h.
48 * Revision 1.22 1996/06/03 15:23:13 adam
49 * Bug fix: /../ BODY /../ - pattern didn't match EOF.
51 * Revision 1.21 1996/05/14 16:58:38 adam
54 * Revision 1.20 1996/05/01 13:46:36 adam
55 * First work on multiple records in one file.
56 * New option, -offset, to the "unread" command in the filter module.
58 * Revision 1.19 1996/02/12 16:18:20 adam
59 * Yet another bug fix in implementation of unread command.
61 * Revision 1.18 1996/02/12 16:07:54 adam
62 * Bug fix in new unread command.
64 * Revision 1.17 1996/02/12 15:56:11 adam
65 * New code command: unread.
67 * Revision 1.16 1996/01/17 14:57:51 adam
68 * Prototype changed for reader functions in extract/retrieve. File
69 * is identified by 'void *' instead of 'int.
71 * Revision 1.15 1996/01/08 19:15:47 adam
72 * New input filter that works!
74 * Revision 1.14 1996/01/08 09:10:38 adam
75 * Yet another complete rework on this module.
77 * Revision 1.13 1995/12/15 17:21:50 adam
78 * This version is able to set data.formatted_text in data1-nodes.
80 * Revision 1.12 1995/12/15 16:20:10 adam
81 * The filter files (*.flt) are read from the path given by data1_tabpath.
83 * Revision 1.11 1995/12/15 12:35:16 adam
86 * Revision 1.10 1995/12/15 10:35:36 adam
89 * Revision 1.9 1995/12/14 16:38:48 adam
90 * Completely new attempt to make regular expression parsing.
92 * Revision 1.8 1995/12/13 17:16:59 adam
95 * Revision 1.7 1995/12/13 16:51:58 adam
96 * Modified to set last_child in data1_nodes.
97 * Uses destroy handler to free up data text nodes.
99 * Revision 1.6 1995/12/13 13:45:37 quinn
100 * Changed data1 to use nmem.
102 * Revision 1.5 1995/12/11 09:12:52 adam
103 * The rec_get function returns NULL if record doesn't exist - will
104 * happen in the server if the result set records have been deleted since
105 * the creation of the set (i.e. the search).
106 * The server saves a result temporarily if it is 'volatile', i.e. the
107 * set is register dependent.
109 * Revision 1.4 1995/12/05 16:57:40 adam
110 * More work on regular patterns.
112 * Revision 1.3 1995/12/05 09:37:09 adam
113 * One malloc was renamed to xmalloc.
115 * Revision 1.2 1995/12/04 17:59:24 adam
116 * More work on regular expression conversion.
118 * Revision 1.1 1995/12/04 14:25:30 adam
119 * Started work on regular expression parsed input to structured records.
127 #include <zebrautl.h>
133 #define F_WIN_EOF 2000000000
137 #define REGX_PATTERN 1
147 struct lexRuleAction {
151 struct DFA *dfa; /* REGX_PATTERN */
154 struct regxCode *code; /* REGX_CODE */
156 struct lexRuleAction *next;
161 struct lexRuleAction *actionList;
165 struct lexRuleInfo info;
166 struct lexRule *next;
171 struct lexRule *rules;
172 struct lexRuleInfo **fastRule;
178 struct lexTrans trans;
183 void (*f_win_ef)(void *, off_t);
189 int (*f_win_rf)(void *, char *, size_t);
190 off_t (*f_win_sf)(void *, off_t);
192 struct lexRuleAction *beginActionList;
193 struct lexRuleAction *endActionList;
197 static char *f_win_get (struct lexSpec *spec, off_t start_pos, off_t end_pos,
202 if (start_pos < spec->f_win_start || start_pos >= spec->f_win_end)
204 (*spec->f_win_sf)(spec->f_win_fh, start_pos);
205 spec->f_win_start = start_pos;
207 if (!spec->f_win_buf)
208 spec->f_win_buf = xmalloc (spec->f_win_size);
209 *size = (*spec->f_win_rf)(spec->f_win_fh, spec->f_win_buf,
211 spec->f_win_end = spec->f_win_start + *size;
213 if (*size > end_pos - start_pos)
214 *size = end_pos - start_pos;
215 return spec->f_win_buf;
217 if (end_pos <= spec->f_win_end)
219 *size = end_pos - start_pos;
220 return spec->f_win_buf + (start_pos - spec->f_win_start);
222 off = start_pos - spec->f_win_start;
223 for (i = 0; i<spec->f_win_end - start_pos; i++)
224 spec->f_win_buf[i] = spec->f_win_buf[i + off];
225 r = (*spec->f_win_rf)(spec->f_win_fh,
227 spec->f_win_size - i);
228 spec->f_win_start = start_pos;
229 spec->f_win_end += r;
231 if (*size > end_pos - start_pos)
232 *size = end_pos - start_pos;
233 return spec->f_win_buf;
236 static int f_win_advance (struct lexSpec *spec, int *pos)
241 if (*pos >= spec->f_win_start && *pos < spec->f_win_end)
242 return spec->f_win_buf[(*pos)++ - spec->f_win_start];
243 if (*pos == F_WIN_EOF)
245 buf = f_win_get (spec, *pos, *pos+1, &size);
255 static void regxCodeDel (struct regxCode **pp)
257 struct regxCode *p = *pp;
266 static void regxCodeMk (struct regxCode **pp, const char *buf, int len)
270 p = xmalloc (sizeof(*p));
271 p->str = xmalloc (len+1);
272 memcpy (p->str, buf, len);
277 static struct DFA *lexSpecDFA (void)
282 dfa_parse_cmap_del (dfa, ' ');
283 dfa_parse_cmap_del (dfa, '\t');
284 dfa_parse_cmap_add (dfa, '/', 0);
288 static struct lexSpec *lexSpecMk (const char *name)
292 p = xmalloc (sizeof(*p));
293 p->name = xmalloc (strlen(name)+1);
294 strcpy (p->name, name);
295 p->trans.dfa = lexSpecDFA ();
296 p->trans.rules = NULL;
297 p->trans.fastRule = NULL;
298 p->beginActionList = NULL;
299 p->endActionList = NULL;
304 static void actionListDel (struct lexRuleAction **rap)
306 struct lexRuleAction *ra1, *ra;
308 for (ra = *rap; ra; ra = ra1)
314 dfa_delete (&ra->u.pattern.dfa);
317 regxCodeDel (&ra->u.code);
325 static void lexSpecDel (struct lexSpec **pp)
328 struct lexRule *rp, *rp1;
334 dfa_delete (&p->trans.dfa);
336 xfree (p->trans.fastRule);
337 for (rp = p->trans.rules; rp; rp = rp1)
339 actionListDel (&rp->info.actionList);
342 actionListDel (&p->beginActionList);
343 actionListDel (&p->endActionList);
344 xfree (p->f_win_buf);
349 static int readParseToken (const char **cpp, int *len)
351 const char *cp = *cpp;
355 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
384 if (*cp >= 'a' && *cp <= 'z')
386 else if (*cp >= 'A' && *cp <= 'Z')
387 cmd[i] = *cp + 'a' - 'A';
390 if (i > sizeof(cmd)-2)
398 logf (LOG_WARN, "bad character %d %c", *cp, *cp);
400 while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
406 if (!strcmp (cmd, "begin"))
408 else if (!strcmp (cmd, "end"))
410 else if (!strcmp (cmd, "body"))
414 logf (LOG_WARN, "bad command %s", cmd);
420 static int actionListMk (struct lexSpec *spec, const char *s,
421 struct lexRuleAction **ap)
426 while ((tok = readParseToken (&s, &len)))
434 *ap = xmalloc (sizeof(**ap));
436 regxCodeMk (&(*ap)->u.code, s, len);
440 *ap = xmalloc (sizeof(**ap));
442 (*ap)->u.pattern.body = bodyMark;
444 (*ap)->u.pattern.dfa = lexSpecDFA ();
445 r = dfa_parse ((*ap)->u.pattern.dfa, &s);
450 logf (LOG_WARN, "regular expression error. r=%d", r);
453 dfa_mkstate ((*ap)->u.pattern.dfa);
457 logf (LOG_WARN, "cannot use begin here");
460 *ap = xmalloc (sizeof(**ap));
470 int readOneSpec (struct lexSpec *spec, const char *s)
474 tok = readParseToken (&s, &len);
475 if (tok == REGX_BEGIN)
477 actionListDel (&spec->beginActionList);
478 actionListMk (spec, s, &spec->beginActionList);
480 else if (tok == REGX_END)
482 actionListDel (&spec->endActionList);
483 actionListMk (spec, s, &spec->endActionList);
485 else if (tok == REGX_PATTERN)
489 r = dfa_parse (spec->trans.dfa, &s);
492 logf (LOG_WARN, "regular expression error. r=%d", r);
497 logf (LOG_WARN, "expects / at end of pattern. got %c", *s);
501 rp = xmalloc (sizeof(*rp));
502 rp->info.no = spec->trans.ruleNo++;
503 rp->next = spec->trans.rules;
504 spec->trans.rules = rp;
505 actionListMk (spec, s, &rp->info.actionList);
510 int readFileSpec (struct lexSpec *spec)
515 int c, i, errors = 0;
518 lineBuf = xmalloc (1+lineSize);
519 logf (LOG_LOG, "reading regx filter %s.flt", spec->name);
520 sprintf (lineBuf, "%s.flt", spec->name);
521 if (!(spec_inf = yaz_path_fopen (data1_get_tabpath(spec->dh),
524 logf (LOG_ERRNO|LOG_WARN, "cannot read spec file %s", spec->name);
529 spec->trans.ruleNo = 1;
534 if (c == '#' || c == '\n' || c == ' ' || c == '\t')
536 while (c != '\n' && c != EOF)
555 if (c != ' ' && c != '\t')
564 readOneSpec (spec, lineBuf);
565 spec->lineNo += addLine;
570 spec->trans.fastRule = xmalloc (sizeof(*spec->trans.fastRule) *
572 for (i = 0; i<spec->trans.ruleNo; i++)
573 spec->trans.fastRule[i] = NULL;
574 for (rp = spec->trans.rules; rp; rp = rp->next)
575 spec->trans.fastRule[rp->info.no] = &rp->info;
581 debug_dfa_followpos = 1;
584 dfa_mkstate (spec->trans.dfa);
588 static struct lexSpec *curLexSpec = NULL;
590 static void destroy_data (struct data1_node *n)
592 assert (n->which == DATA1N_data);
593 xfree (n->u.data.data);
596 static void execData (struct lexSpec *spec,
597 data1_node **d1_stack, int *d1_level,
598 const char *ebuf, int elen, int formatted_text)
600 struct data1_node *res, *parent;
602 if (elen == 0) /* shouldn't happen, but it does! */
606 logf (LOG_DEBUG, "execData %.15s ... %.*s", ebuf, 15, ebuf + elen-15);
608 logf (LOG_DEBUG, "execData %.*s", elen, ebuf);
610 logf (LOG_DEBUG, "execData len=%d", elen);
616 parent = d1_stack[*d1_level -1];
618 if ((res=d1_stack[*d1_level]) && res->which == DATA1N_data)
620 if (elen + res->u.data.len <= DATA1_LOCALDATA)
621 memcpy (res->u.data.data + res->u.data.len, ebuf, elen);
624 char *nb = xmalloc (elen + res->u.data.len);
625 memcpy (nb, res->u.data.data, res->u.data.len);
626 memcpy (nb + res->u.data.len, ebuf, elen);
627 if (res->u.data.len > DATA1_LOCALDATA)
628 xfree (res->u.data.data);
629 res->u.data.data = nb;
630 res->destroy = destroy_data;
632 res->u.data.len += elen;
636 res = data1_mk_node (spec->dh, spec->m);
637 res->parent = parent;
638 res->which = DATA1N_data;
639 res->u.data.what = DATA1I_text;
640 res->u.data.len = elen;
641 res->u.data.formatted_text = formatted_text;
642 if (elen > DATA1_LOCALDATA)
644 res->u.data.data = xmalloc (elen);
645 res->destroy = destroy_data;
648 res->u.data.data = res->lbuf;
649 memcpy (res->u.data.data, ebuf, elen);
650 res->root = parent->root;
652 parent->num_children++;
653 parent->last_child = res;
654 if (d1_stack[*d1_level])
655 d1_stack[*d1_level]->next = res;
658 d1_stack[*d1_level] = res;
662 static void execDataP (struct lexSpec *spec,
663 data1_node **d1_stack, int *d1_level,
664 const char *ebuf, int elen, int formatted_text)
666 execData (spec, d1_stack, d1_level, ebuf, elen, formatted_text);
669 static void variantBegin (struct lexSpec *spec,
670 data1_node **d1_stack, int *d1_level,
671 const char *class_str, int class_len,
672 const char *type_str, int type_len,
673 const char *value_str, int value_len)
675 struct data1_node *parent = d1_stack[*d1_level -1];
676 char tclass[DATA1_MAX_SYMBOL], ttype[DATA1_MAX_SYMBOL];
683 logf (LOG_WARN, "in variant begin. No record type defined");
686 if (class_len >= DATA1_MAX_SYMBOL)
687 class_len = DATA1_MAX_SYMBOL-1;
688 memcpy (tclass, class_str, class_len);
689 tclass[class_len] = '\0';
691 if (type_len >= DATA1_MAX_SYMBOL)
692 type_len = DATA1_MAX_SYMBOL-1;
693 memcpy (ttype, type_str, type_len);
694 ttype[type_len] = '\0';
697 logf (LOG_DEBUG, "variant begin %s %s (%d)", tclass, ttype, *d1_level);
701 data1_getvartypebyct(spec->dh, parent->root->u.root.absyn->varset,
705 if (parent->which != DATA1N_variant)
707 res = data1_mk_node (spec->dh, spec->m);
708 res->parent = parent;
709 res->which = DATA1N_variant;
710 res->u.variant.type = 0;
711 res->u.variant.value = 0;
712 res->root = parent->root;
714 parent->num_children++;
715 parent->last_child = res;
716 if (d1_stack[*d1_level])
717 d1_stack[*d1_level]->next = res;
720 d1_stack[*d1_level] = res;
721 d1_stack[++(*d1_level)] = NULL;
723 for (i = *d1_level-1; d1_stack[i]->which == DATA1N_variant; i--)
724 if (d1_stack[i]->u.variant.type == tp)
731 logf (LOG_DEBUG, "variant node (%d)", *d1_level);
733 parent = d1_stack[*d1_level-1];
734 res = data1_mk_node (spec->dh, spec->m);
735 res->parent = parent;
736 res->which = DATA1N_variant;
737 res->root = parent->root;
738 res->u.variant.type = tp;
740 if (value_len >= DATA1_LOCALDATA)
741 value_len =DATA1_LOCALDATA-1;
742 memcpy (res->lbuf, value_str, value_len);
743 res->lbuf[value_len] = '\0';
745 res->u.variant.value = res->lbuf;
747 parent->num_children++;
748 parent->last_child = res;
749 if (d1_stack[*d1_level])
750 d1_stack[*d1_level]->next = res;
753 d1_stack[*d1_level] = res;
754 d1_stack[++(*d1_level)] = NULL;
757 static void tagBegin (struct lexSpec *spec,
758 data1_node **d1_stack, int *d1_level,
759 const char *tag, int len)
761 struct data1_node *parent = d1_stack[*d1_level -1];
762 data1_element *elem = NULL;
763 data1_node *partag = get_parent_tag(spec->dh, parent);
765 data1_element *e = NULL;
770 logf (LOG_WARN, "in element begin. No record type defined");
774 res = data1_mk_node (spec->dh, spec->m);
775 res->parent = parent;
776 res->which = DATA1N_tag;
777 res->u.tag.tag = res->lbuf;
778 res->u.tag.get_bytes = -1;
780 if (len >= DATA1_LOCALDATA)
781 len = DATA1_LOCALDATA-1;
783 memcpy (res->u.tag.tag, tag, len);
784 res->u.tag.tag[len] = '\0';
787 logf (LOG_DEBUG, "tag begin %s (%d)", res->u.tag.tag, *d1_level);
789 if (parent->which == DATA1N_variant)
792 if (!(e = partag->u.tag.element))
795 elem = data1_getelementbytagname (spec->dh, d1_stack[0]->u.root.absyn,
797 res->u.tag.element = elem;
798 res->u.tag.node_selected = 0;
799 res->u.tag.make_variantlist = 0;
800 res->u.tag.no_data_requested = 0;
801 res->root = parent->root;
803 parent->num_children++;
804 parent->last_child = res;
805 if (d1_stack[*d1_level])
806 d1_stack[*d1_level]->next = res;
809 d1_stack[*d1_level] = res;
810 d1_stack[++(*d1_level)] = NULL;
813 static void tagEnd (struct lexSpec *spec,
814 data1_node **d1_stack, int *d1_level,
815 const char *tag, int len)
817 while (*d1_level > 1)
820 if ((d1_stack[*d1_level]->which == DATA1N_tag) &&
822 (strlen(d1_stack[*d1_level]->u.tag.tag) == (size_t) len &&
823 !memcmp (d1_stack[*d1_level]->u.tag.tag, tag, len))))
827 logf (LOG_DEBUG, "tag end (%d)", *d1_level);
832 static int tryMatch (struct lexSpec *spec, int *pptr, int *mptr,
835 struct DFA_state *state = dfa->states[0];
838 unsigned char c_prev = 0;
840 int start_ptr = *pptr;
847 c = f_win_advance (spec, &ptr);
848 if (ptr == F_WIN_EOF)
865 *mptr = start_ptr; /* match starts here */
866 *pptr = last_ptr; /* match end here (+1) */
869 state = dfa->states[0];
874 else if (c >= t->ch[0] && c <= t->ch[1])
876 state = dfa->states[t->to];
881 last_rule = state->rule_no;
886 last_rule = state->rule_nno;
898 static int execTok (struct lexSpec *spec, const char **src,
899 int arg_no, int *arg_start, int *arg_end,
900 const char **tokBuf, int *tokLen)
902 const char *s = *src;
904 while (*s == ' ' || *s == '\t')
908 if (*s == '$' && s[1] >= '0' && s[1] <= '9')
912 while (*s >= '0' && *s <= '9')
913 n = n*10 + (*s++ -'0');
923 *tokBuf = f_win_get (spec, arg_start[n], arg_end[n], tokLen);
929 while (*s && *s != '\"')
931 *tokLen = s - *tokBuf;
936 else if (*s == '\n' || *s == ';')
944 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
946 *tokLen = s - *tokBuf;
953 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
955 *tokLen = s - *tokBuf;
961 static char *regxStrz (const char *src, int len)
967 memcpy (str, src, len);
972 static int execCode (struct lexSpec *spec,
973 int arg_no, int *arg_start, int *arg_end, int *pptr,
974 struct regxCode *code,
975 data1_node **d1_stack, int *d1_level)
977 const char *s = code->str;
982 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str, &cmd_len);
989 r = execTok (spec, &s, arg_no, arg_start, arg_end,
993 p = regxStrz (cmd_str, cmd_len);
994 if (!strcmp (p, "begin"))
996 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1000 p = regxStrz (cmd_str, cmd_len);
1001 if (!strcmp (p, "record"))
1003 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1004 &cmd_str, &cmd_len);
1009 static char absynName[64];
1014 memcpy (absynName, cmd_str, cmd_len);
1015 absynName[cmd_len] = '\0';
1018 logf (LOG_DEBUG, "begin record %s", absynName);
1020 if (!(absyn = data1_get_absyn (spec->dh, absynName)))
1021 logf (LOG_WARN, "Unknown tagset: %s", absynName);
1026 res = data1_mk_node (spec->dh, spec->m);
1027 res->which = DATA1N_root;
1028 res->u.root.type = absynName;
1029 res->u.root.absyn = absyn;
1032 d1_stack[*d1_level] = res;
1033 d1_stack[++(*d1_level)] = NULL;
1036 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1037 &cmd_str, &cmd_len);
1039 else if (!strcmp (p, "element"))
1041 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1042 &cmd_str, &cmd_len);
1045 tagBegin (spec, d1_stack, d1_level, cmd_str, cmd_len);
1046 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1047 &cmd_str, &cmd_len);
1049 else if (!strcmp (p, "variant"))
1052 const char *class_str = NULL;
1054 const char *type_str = NULL;
1056 const char *value_str = NULL;
1057 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1058 &cmd_str, &cmd_len);
1061 class_str = cmd_str;
1062 class_len = cmd_len;
1063 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1064 &cmd_str, &cmd_len);
1070 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1071 &cmd_str, &cmd_len);
1074 value_str = cmd_str;
1075 value_len = cmd_len;
1077 variantBegin (spec, d1_stack, d1_level, class_str, class_len,
1078 type_str, type_len, value_str, value_len);
1081 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1082 &cmd_str, &cmd_len);
1085 else if (!strcmp (p, "end"))
1087 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1088 &cmd_str, &cmd_len);
1091 p = regxStrz (cmd_str, cmd_len);
1092 if (!strcmp (p, "record"))
1095 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1096 &cmd_str, &cmd_len);
1098 logf (LOG_DEBUG, "end record");
1102 else if (!strcmp (p, "element"))
1104 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1105 &cmd_str, &cmd_len);
1115 tagEnd (spec, d1_stack, d1_level, cmd_str, cmd_len);
1116 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1117 &cmd_str, &cmd_len);
1120 tagEnd (spec, d1_stack, d1_level, NULL, 0);
1123 logf (LOG_WARN, "missing record/element/variant");
1126 logf (LOG_WARN, "missing record/element/variant");
1128 else if (!strcmp (p, "data"))
1132 const char *element_str = NULL;
1134 while ((r = execTok (spec, &s, arg_no, arg_start, arg_end,
1135 &cmd_str, &cmd_len)) == 3)
1137 if (cmd_len==5 && !memcmp ("-text", cmd_str, cmd_len))
1139 else if (cmd_len==8 && !memcmp ("-element", cmd_str, cmd_len))
1141 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1142 &element_str, &element_len);
1147 logf (LOG_WARN, "bad data option: %.*s",
1152 logf (LOG_WARN, "missing data item after data");
1156 tagBegin (spec, d1_stack, d1_level, element_str, element_len);
1159 execData (spec, d1_stack, d1_level, cmd_str, cmd_len,
1161 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1162 &cmd_str, &cmd_len);
1165 tagEnd (spec, d1_stack, d1_level, NULL, 0);
1167 else if (!strcmp (p, "unread"))
1170 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1171 &cmd_str, &cmd_len);
1172 if (r==3 && cmd_len == 7 && !memcmp ("-offset", cmd_str, cmd_len))
1174 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1175 &cmd_str, &cmd_len);
1178 logf (LOG_WARN, "missing number after -offset");
1181 p = regxStrz (cmd_str, cmd_len);
1183 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1184 &cmd_str, &cmd_len);
1190 logf (LOG_WARN, "missing index after unread command");
1193 if (cmd_len != 1 || *cmd_str < '0' || *cmd_str > '9')
1195 logf (LOG_WARN, "bad index after unread command");
1200 no = *cmd_str - '0';
1203 *pptr = arg_start[no] + offset;
1205 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1206 &cmd_str, &cmd_len);
1210 logf (LOG_WARN, "unknown code command: %.*s", cmd_len, cmd_str);
1211 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1212 &cmd_str, &cmd_len);
1217 logf (LOG_WARN, "ignoring token %.*s", cmd_len, cmd_str);
1219 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str,
1228 static int execAction (struct lexSpec *spec, struct lexRuleAction *ap,
1229 data1_node **d1_stack, int *d1_level,
1230 int start_ptr, int *pptr)
1237 arg_start[0] = start_ptr;
1245 if (ap->u.pattern.body)
1247 arg_start[arg_no] = *pptr;
1248 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1250 arg_end[arg_no] = F_WIN_EOF;
1252 arg_start[arg_no] = F_WIN_EOF;
1253 arg_end[arg_no] = F_WIN_EOF;
1258 arg_end[arg_no] = sptr;
1260 arg_start[arg_no] = sptr;
1261 arg_end[arg_no] = *pptr;
1266 arg_start[arg_no] = *pptr;
1267 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1269 if (sptr != arg_start[arg_no])
1271 arg_end[arg_no] = *pptr;
1276 if (!execCode (spec, arg_no, arg_start, arg_end, pptr,
1277 ap->u.code, d1_stack, d1_level))
1281 arg_start[arg_no] = *pptr;
1282 arg_end[arg_no] = F_WIN_EOF;
1291 static int execRule (struct lexSpec *spec, struct lexTrans *trans,
1292 data1_node **d1_stack, int *d1_level,
1293 int ruleNo, int start_ptr, int *pptr)
1296 logf (LOG_DEBUG, "execRule %d", ruleNo);
1298 return execAction (spec, trans->fastRule[ruleNo]->actionList,
1299 d1_stack, d1_level, start_ptr, pptr);
1302 data1_node *lexNode (struct lexSpec *spec, struct lexTrans *trans,
1303 data1_node **d1_stack, int *d1_level,
1306 struct DFA_state *state = trans->dfa->states[0];
1309 unsigned char c_prev = '\n';
1312 int last_ptr = *ptr;
1313 int start_ptr = *ptr;
1314 int skip_ptr = *ptr;
1318 c = f_win_advance (spec, ptr);
1319 if (*ptr == F_WIN_EOF)
1323 if (skip_ptr < start_ptr)
1327 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1328 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1331 if (!execRule (spec, trans, d1_stack, d1_level, last_rule,
1337 else if (skip_ptr < *ptr)
1341 buf = f_win_get (spec, skip_ptr, *ptr, &size);
1342 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1344 if (*ptr == F_WIN_EOF)
1351 { /* no transition for character c ... */
1354 if (skip_ptr < start_ptr)
1358 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1359 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1362 if (!execRule (spec, trans, d1_stack, d1_level, last_rule,
1365 if (spec->f_win_ef && *ptr != F_WIN_EOF)
1368 logf (LOG_DEBUG, "regx: endf ptr=%d", *ptr);
1370 (*spec->f_win_ef)(spec->f_win_fh, *ptr);
1380 c_prev = f_win_advance (spec, &start_ptr);
1385 c_prev = f_win_advance (spec, &start_ptr);
1388 state = trans->dfa->states[0];
1391 else if (c >= t->ch[0] && c <= t->ch[1])
1392 { /* transition ... */
1393 state = trans->dfa->states[t->to];
1398 last_rule = state->rule_no;
1401 else if (state->rule_nno)
1403 last_rule = state->rule_nno;
1415 static data1_node *lexRoot (struct lexSpec *spec, off_t offset)
1417 data1_node *d1_stack[512];
1421 d1_stack[d1_level] = NULL;
1422 if (spec->beginActionList)
1423 execAction (spec, spec->beginActionList,
1424 d1_stack, &d1_level, 0, &ptr);
1425 lexNode (spec, &spec->trans, d1_stack, &d1_level, &ptr);
1426 if (spec->endActionList)
1427 execAction (spec, spec->endActionList,
1428 d1_stack, &d1_level, ptr, &ptr);
1432 data1_node *grs_read_regx (struct grs_read_info *p)
1438 logf (LOG_DEBUG, "grs_read_regx");
1440 if (!curLexSpec || strcmp (curLexSpec->name, p->type))
1443 lexSpecDel (&curLexSpec);
1444 curLexSpec = lexSpecMk (p->type);
1445 curLexSpec->dh = p->dh;
1446 res = readFileSpec (curLexSpec);
1449 lexSpecDel (&curLexSpec);
1455 curLexSpec->f_win_start = 0;
1456 curLexSpec->f_win_end = 0;
1457 curLexSpec->f_win_rf = p->readf;
1458 curLexSpec->f_win_sf = p->seekf;
1459 curLexSpec->f_win_fh = p->fh;
1460 curLexSpec->f_win_ef = p->endf;
1461 curLexSpec->f_win_size = 500000;
1463 curLexSpec->m = p->mem;
1464 n = lexRoot (curLexSpec, p->offset);