2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1998-03-05 08:41:00 adam
8 * Implemented rule contexts.
10 * Revision 1.13 1997/12/12 06:33:58 adam
11 * Fixed bug that showed up when multiple filter where used.
12 * Made one routine thread-safe.
14 * Revision 1.12 1997/11/18 10:03:24 adam
15 * Member num_children removed from data1_node.
17 * Revision 1.11 1997/11/06 11:41:01 adam
18 * Implemented "begin variant" for the sgml.regx filter.
20 * Revision 1.10 1997/10/31 12:36:12 adam
21 * Minor change that avoids compiler warning.
23 * Revision 1.9 1997/09/29 09:02:49 adam
24 * Fixed small bug (introduced by previous commit).
26 * Revision 1.8 1997/09/17 12:19:22 adam
27 * Zebra version corresponds to YAZ version 1.4.
28 * Changed Zebra server so that it doesn't depend on global common_resource.
30 * Revision 1.7 1997/07/15 16:33:07 adam
31 * Check for zero length in execData.
33 * Revision 1.6 1997/02/24 10:41:51 adam
34 * Cleanup of code and commented out the "end element-end-record" code.
36 * Revision 1.5 1997/02/19 16:22:33 adam
37 * Fixed "end element" to terminate record in outer-most level.
39 * Revision 1.4 1997/02/12 20:42:58 adam
40 * Changed some log messages.
42 * Revision 1.3 1996/11/08 14:05:33 adam
43 * Bug fix: data1 node member u.tag.get_bytes weren't initialized.
45 * Revision 1.2 1996/10/29 14:02:09 adam
46 * Doesn't use the global data1_tabpath (from YAZ). Instead the function
47 * data1_get_tabpath is used.
49 * Revision 1.1 1996/10/11 10:57:30 adam
50 * New module recctrl. Used to manage records (extract/retrieval).
52 * Revision 1.24 1996/06/17 14:25:31 adam
53 * Removed LOG_DEBUG logs; can still be enabled by setting REGX_DEBUG.
55 * Revision 1.23 1996/06/04 10:19:00 adam
56 * Minor changes - removed include of ctype.h.
58 * Revision 1.22 1996/06/03 15:23:13 adam
59 * Bug fix: /../ BODY /../ - pattern didn't match EOF.
61 * Revision 1.21 1996/05/14 16:58:38 adam
64 * Revision 1.20 1996/05/01 13:46:36 adam
65 * First work on multiple records in one file.
66 * New option, -offset, to the "unread" command in the filter module.
68 * Revision 1.19 1996/02/12 16:18:20 adam
69 * Yet another bug fix in implementation of unread command.
71 * Revision 1.18 1996/02/12 16:07:54 adam
72 * Bug fix in new unread command.
74 * Revision 1.17 1996/02/12 15:56:11 adam
75 * New code command: unread.
77 * Revision 1.16 1996/01/17 14:57:51 adam
78 * Prototype changed for reader functions in extract/retrieve. File
79 * is identified by 'void *' instead of 'int.
81 * Revision 1.15 1996/01/08 19:15:47 adam
82 * New input filter that works!
84 * Revision 1.14 1996/01/08 09:10:38 adam
85 * Yet another complete rework on this module.
87 * Revision 1.13 1995/12/15 17:21:50 adam
88 * This version is able to set data.formatted_text in data1-nodes.
90 * Revision 1.12 1995/12/15 16:20:10 adam
91 * The filter files (*.flt) are read from the path given by data1_tabpath.
93 * Revision 1.11 1995/12/15 12:35:16 adam
96 * Revision 1.10 1995/12/15 10:35:36 adam
99 * Revision 1.9 1995/12/14 16:38:48 adam
100 * Completely new attempt to make regular expression parsing.
102 * Revision 1.8 1995/12/13 17:16:59 adam
105 * Revision 1.7 1995/12/13 16:51:58 adam
106 * Modified to set last_child in data1_nodes.
107 * Uses destroy handler to free up data text nodes.
109 * Revision 1.6 1995/12/13 13:45:37 quinn
110 * Changed data1 to use nmem.
112 * Revision 1.5 1995/12/11 09:12:52 adam
113 * The rec_get function returns NULL if record doesn't exist - will
114 * happen in the server if the result set records have been deleted since
115 * the creation of the set (i.e. the search).
116 * The server saves a result temporarily if it is 'volatile', i.e. the
117 * set is register dependent.
119 * Revision 1.4 1995/12/05 16:57:40 adam
120 * More work on regular patterns.
122 * Revision 1.3 1995/12/05 09:37:09 adam
123 * One malloc was renamed to xmalloc.
125 * Revision 1.2 1995/12/04 17:59:24 adam
126 * More work on regular expression conversion.
128 * Revision 1.1 1995/12/04 14:25:30 adam
129 * Started work on regular expression parsed input to structured records.
137 #include <zebrautl.h>
143 #define F_WIN_EOF 2000000000
147 #define REGX_PATTERN 1
152 #define REGX_CONTEXT 6
158 struct lexRuleAction {
162 struct DFA *dfa; /* REGX_PATTERN */
165 struct regxCode *code; /* REGX_CODE */
167 struct lexRuleAction *next;
172 struct lexRuleAction *actionList;
176 struct lexRuleInfo info;
177 struct lexRule *next;
183 struct lexRule *rules;
184 struct lexRuleInfo **fastRule;
187 struct lexRuleAction *beginActionList;
188 struct lexRuleAction *endActionList;
189 struct lexContext *next;
194 struct lexContext *context;
196 struct lexContext **context_stack;
197 int context_stack_size;
198 int context_stack_top;
204 void (*f_win_ef)(void *, off_t);
206 int f_win_start; /* first byte of buffer is this file offset */
207 int f_win_end; /* last byte of buffer is this offset - 1 */
208 int f_win_size; /* size of buffer */
209 char *f_win_buf; /* buffer itself */
210 int (*f_win_rf)(void *, char *, size_t);
211 off_t (*f_win_sf)(void *, off_t);
216 static char *f_win_get (struct lexSpec *spec, off_t start_pos, off_t end_pos,
219 int i, r, off = start_pos - spec->f_win_start;
221 if (off >= 0 && end_pos <= spec->f_win_end)
223 *size = end_pos - start_pos;
224 return spec->f_win_buf + off;
226 if (off < 0 || start_pos >= spec->f_win_end)
228 (*spec->f_win_sf)(spec->f_win_fh, start_pos);
229 spec->f_win_start = start_pos;
231 if (!spec->f_win_buf)
232 spec->f_win_buf = xmalloc (spec->f_win_size);
233 *size = (*spec->f_win_rf)(spec->f_win_fh, spec->f_win_buf,
235 spec->f_win_end = spec->f_win_start + *size;
237 if (*size > end_pos - start_pos)
238 *size = end_pos - start_pos;
239 return spec->f_win_buf;
241 for (i = 0; i<spec->f_win_end - start_pos; i++)
242 spec->f_win_buf[i] = spec->f_win_buf[i + off];
243 r = (*spec->f_win_rf)(spec->f_win_fh,
245 spec->f_win_size - i);
246 spec->f_win_start = start_pos;
247 spec->f_win_end += r;
249 if (*size > end_pos - start_pos)
250 *size = end_pos - start_pos;
251 return spec->f_win_buf;
254 static int f_win_advance (struct lexSpec *spec, int *pos)
259 if (*pos >= spec->f_win_start && *pos < spec->f_win_end)
260 return spec->f_win_buf[(*pos)++ - spec->f_win_start];
261 if (*pos == F_WIN_EOF)
263 buf = f_win_get (spec, *pos, *pos+1, &size);
273 static void regxCodeDel (struct regxCode **pp)
275 struct regxCode *p = *pp;
284 static void regxCodeMk (struct regxCode **pp, const char *buf, int len)
288 p = xmalloc (sizeof(*p));
289 p->str = xmalloc (len+1);
290 memcpy (p->str, buf, len);
295 static struct DFA *lexSpecDFA (void)
300 dfa_parse_cmap_del (dfa, ' ');
301 dfa_parse_cmap_del (dfa, '\t');
302 dfa_parse_cmap_add (dfa, '/', 0);
306 static void actionListDel (struct lexRuleAction **rap)
308 struct lexRuleAction *ra1, *ra;
310 for (ra = *rap; ra; ra = ra1)
316 dfa_delete (&ra->u.pattern.dfa);
319 regxCodeDel (&ra->u.code);
327 static struct lexContext *lexContextCreate (const char *name)
329 struct lexContext *p = xmalloc (sizeof(*p));
331 p->name = xstrdup (name);
333 p->dfa = lexSpecDFA ();
336 p->beginActionList = NULL;
337 p->endActionList = NULL;
342 static void lexContextDestroy (struct lexContext *p)
344 struct lexRule *rp, *rp1;
347 for (rp = p->rules; rp; rp = rp1)
350 actionListDel (&rp->info.actionList);
353 actionListDel (&p->beginActionList);
354 actionListDel (&p->endActionList);
359 static struct lexSpec *lexSpecCreate (const char *name)
363 p = xmalloc (sizeof(*p));
364 p->name = xmalloc (strlen(name)+1);
365 strcpy (p->name, name);
368 p->context_stack_size = 100;
369 p->context_stack = xmalloc (sizeof(*p->context_stack) *
370 p->context_stack_size);
375 static void lexSpecDestroy (struct lexSpec **pp)
378 struct lexContext *lt;
387 struct lexContext *lt_next = lt->next;
388 lexContextDestroy (lt);
392 xfree (p->f_win_buf);
393 xfree (p->context_stack);
398 static int readParseToken (const char **cpp, int *len)
400 const char *cp = *cpp;
404 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
433 if (*cp >= 'a' && *cp <= 'z')
435 else if (*cp >= 'A' && *cp <= 'Z')
436 cmd[i] = *cp + 'a' - 'A';
439 if (i < sizeof(cmd)-2)
446 logf (LOG_WARN, "bad character %d %c", *cp, *cp);
448 while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
454 if (!strcmp (cmd, "begin"))
456 else if (!strcmp (cmd, "end"))
458 else if (!strcmp (cmd, "body"))
460 else if (!strcmp (cmd, "context"))
464 logf (LOG_WARN, "bad command %s", cmd);
470 static int actionListMk (struct lexSpec *spec, const char *s,
471 struct lexRuleAction **ap)
477 while ((tok = readParseToken (&s, &len)))
485 *ap = xmalloc (sizeof(**ap));
487 regxCodeMk (&(*ap)->u.code, s, len);
491 *ap = xmalloc (sizeof(**ap));
493 (*ap)->u.pattern.body = bodyMark;
495 (*ap)->u.pattern.dfa = lexSpecDFA ();
497 r = dfa_parse ((*ap)->u.pattern.dfa, &s);
502 logf (LOG_WARN, "regular expression error '%.*s'", s-s0, s0);
505 dfa_mkstate ((*ap)->u.pattern.dfa);
509 logf (LOG_WARN, "cannot use begin here");
512 *ap = xmalloc (sizeof(**ap));
522 int readOneSpec (struct lexSpec *spec, const char *s)
526 struct lexContext *lc;
528 tok = readParseToken (&s, &len);
529 if (tok == REGX_CONTEXT)
531 char context_name[32];
532 tok = readParseToken (&s, &len);
533 if (tok != REGX_CODE)
535 logf (LOG_WARN, "missing name after CONTEXT keyword");
540 memcpy (context_name, s, len);
541 context_name[len] = '\0';
542 lc = lexContextCreate (context_name);
543 lc->next = spec->context;
548 spec->context = lexContextCreate ("main");
553 actionListDel (&spec->context->beginActionList);
554 actionListMk (spec, s, &spec->context->beginActionList);
557 actionListDel (&spec->context->endActionList);
558 actionListMk (spec, s, &spec->context->endActionList);
562 logf (LOG_DEBUG, "rule %d %s", spec->context->ruleNo, s);
564 r = dfa_parse (spec->context->dfa, &s);
567 logf (LOG_WARN, "regular expression error. r=%d", r);
572 logf (LOG_WARN, "expects / at end of pattern. got %c", *s);
576 rp = xmalloc (sizeof(*rp));
577 rp->info.no = spec->context->ruleNo++;
578 rp->next = spec->context->rules;
579 spec->context->rules = rp;
580 actionListMk (spec, s, &rp->info.actionList);
585 int readFileSpec (struct lexSpec *spec)
587 struct lexContext *lc;
590 int c, i, errors = 0;
593 lineBuf = xmalloc (1+lineSize);
594 logf (LOG_LOG, "reading regx filter %s.flt", spec->name);
595 sprintf (lineBuf, "%s.flt", spec->name);
596 if (!(spec_inf = yaz_path_fopen (data1_get_tabpath(spec->dh),
599 logf (LOG_ERRNO|LOG_WARN, "cannot read spec file %s", spec->name);
608 if (c == '#' || c == '\n' || c == ' ' || c == '\t')
610 while (c != '\n' && c != EOF)
629 if (c != ' ' && c != '\t')
638 readOneSpec (spec, lineBuf);
639 spec->lineNo += addLine;
648 debug_dfa_followpos = 1;
651 for (lc = spec->context; lc; lc = lc->next)
654 lc->fastRule = xmalloc (sizeof(*lc->fastRule) * lc->ruleNo);
655 for (i = 0; i < lc->ruleNo; i++)
656 lc->fastRule[i] = NULL;
657 for (rp = lc->rules; rp; rp = rp->next)
658 lc->fastRule[rp->info.no] = &rp->info;
659 dfa_mkstate (lc->dfa);
666 static struct lexSpec *curLexSpec = NULL;
668 static void destroy_data (struct data1_node *n)
670 assert (n->which == DATA1N_data);
671 xfree (n->u.data.data);
674 static void execData (struct lexSpec *spec,
675 data1_node **d1_stack, int *d1_level,
676 const char *ebuf, int elen, int formatted_text)
678 struct data1_node *res, *parent;
680 if (elen == 0) /* shouldn't happen, but it does! */
684 logf (LOG_DEBUG, "data (%d bytes) %.15s ... %.*s", elen,
685 ebuf, 15, ebuf + elen-15);
687 logf (LOG_DEBUG, "data (%d bytes) %.*s", elen, elen, ebuf);
689 logf (LOG_DEBUG, "data (%d bytes)", elen);
695 parent = d1_stack[*d1_level -1];
697 if ((res=d1_stack[*d1_level]) && res->which == DATA1N_data)
699 if (elen + res->u.data.len <= DATA1_LOCALDATA)
700 memcpy (res->u.data.data + res->u.data.len, ebuf, elen);
703 char *nb = xmalloc (elen + res->u.data.len);
704 memcpy (nb, res->u.data.data, res->u.data.len);
705 memcpy (nb + res->u.data.len, ebuf, elen);
706 if (res->u.data.len > DATA1_LOCALDATA)
707 xfree (res->u.data.data);
708 res->u.data.data = nb;
709 res->destroy = destroy_data;
711 res->u.data.len += elen;
715 res = data1_mk_node (spec->dh, spec->m);
716 res->parent = parent;
717 res->which = DATA1N_data;
718 res->u.data.what = DATA1I_text;
719 res->u.data.len = elen;
720 res->u.data.formatted_text = formatted_text;
721 if (elen > DATA1_LOCALDATA)
722 res->u.data.data = nmem_malloc (spec->m, elen);
724 res->u.data.data = res->lbuf;
725 memcpy (res->u.data.data, ebuf, elen);
726 res->root = parent->root;
728 parent->last_child = res;
729 if (d1_stack[*d1_level])
730 d1_stack[*d1_level]->next = res;
733 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 tagBegin (struct lexSpec *spec,
831 data1_node **d1_stack, int *d1_level,
832 const char *tag, int len)
834 struct data1_node *parent = d1_stack[*d1_level -1];
835 data1_element *elem = NULL;
836 data1_node *partag = get_parent_tag(spec->dh, parent);
838 data1_element *e = NULL;
843 logf (LOG_WARN, "in element begin. No record type defined");
847 res = data1_mk_node (spec->dh, spec->m);
848 res->parent = parent;
849 res->which = DATA1N_tag;
850 res->u.tag.get_bytes = -1;
852 if (len >= DATA1_LOCALDATA)
853 len = DATA1_LOCALDATA-1;
854 memcpy (res->lbuf, tag, len);
855 res->lbuf[len] = '\0';
856 res->u.tag.tag = res->lbuf;
859 logf (LOG_DEBUG, "begin tag %s (%d)", res->u.tag.tag, *d1_level);
861 if (parent->which == DATA1N_variant)
864 if (!(e = partag->u.tag.element))
867 elem = data1_getelementbytagname (spec->dh, d1_stack[0]->u.root.absyn,
869 res->u.tag.element = elem;
870 res->u.tag.node_selected = 0;
871 res->u.tag.make_variantlist = 0;
872 res->u.tag.no_data_requested = 0;
873 res->root = parent->root;
875 parent->last_child = res;
876 if (d1_stack[*d1_level])
877 d1_stack[*d1_level]->next = res;
880 d1_stack[*d1_level] = res;
881 d1_stack[++(*d1_level)] = NULL;
884 static void tagEnd (struct lexSpec *spec,
885 data1_node **d1_stack, int *d1_level,
886 const char *tag, int len)
888 while (*d1_level > 1)
891 if ((d1_stack[*d1_level]->which == DATA1N_tag) &&
893 (strlen(d1_stack[*d1_level]->u.tag.tag) == (size_t) len &&
894 !memcmp (d1_stack[*d1_level]->u.tag.tag, tag, len))))
898 logf (LOG_DEBUG, "end tag (%d)", *d1_level);
903 static int tryMatch (struct lexSpec *spec, int *pptr, int *mptr,
906 struct DFA_state *state = dfa->states[0];
909 unsigned char c_prev = 0;
910 int ptr = *pptr; /* current pointer */
911 int start_ptr = *pptr; /* first char of match */
912 int last_ptr = 0; /* last char of match */
913 int last_rule = 0; /* rule number of current match */
918 c = f_win_advance (spec, &ptr);
919 if (ptr == F_WIN_EOF)
936 *mptr = start_ptr; /* match starts here */
937 *pptr = last_ptr; /* match end here (+1) */
940 state = dfa->states[0];
945 else if (c >= t->ch[0] && c <= t->ch[1])
947 state = dfa->states[t->to];
952 last_rule = state->rule_no;
957 last_rule = state->rule_nno;
969 static int execTok (struct lexSpec *spec, const char **src,
970 int arg_no, int *arg_start, int *arg_end,
971 const char **tokBuf, int *tokLen)
973 const char *s = *src;
975 while (*s == ' ' || *s == '\t')
979 if (*s == '$' && s[1] >= '0' && s[1] <= '9')
983 while (*s >= '0' && *s <= '9')
984 n = n*10 + (*s++ -'0');
994 *tokBuf = f_win_get (spec, arg_start[n], arg_end[n], tokLen);
1000 while (*s && *s != '\"')
1002 *tokLen = s - *tokBuf;
1007 else if (*s == '\n' || *s == ';')
1015 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
1017 *tokLen = s - *tokBuf;
1024 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
1026 *tokLen = s - *tokBuf;
1032 static char *regxStrz (const char *src, int len, char *str)
1036 memcpy (str, src, len);
1041 static int execCode (struct lexSpec *spec,
1042 int arg_no, int *arg_start, int *arg_end, int *pptr,
1043 struct regxCode *code,
1044 data1_node **d1_stack, int *d1_level)
1046 const char *s = code->str;
1049 const char *cmd_str;
1051 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str, &cmd_len);
1058 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1059 &cmd_str, &cmd_len);
1062 p = regxStrz (cmd_str, cmd_len, ptmp);
1063 if (!strcmp (p, "begin"))
1065 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1066 &cmd_str, &cmd_len);
1069 logf (LOG_WARN, "missing keyword after 'begin'");
1072 p = regxStrz (cmd_str, cmd_len, ptmp);
1073 if (!strcmp (p, "record"))
1075 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1076 &cmd_str, &cmd_len);
1081 static char absynName[64];
1086 memcpy (absynName, cmd_str, cmd_len);
1087 absynName[cmd_len] = '\0';
1090 logf (LOG_DEBUG, "begin record %s", absynName);
1092 if (!(absyn = data1_get_absyn (spec->dh, absynName)))
1093 logf (LOG_WARN, "Unknown tagset: %s", absynName);
1098 res = data1_mk_node (spec->dh, spec->m);
1099 res->which = DATA1N_root;
1100 res->u.root.type = absynName;
1101 res->u.root.absyn = absyn;
1104 d1_stack[*d1_level] = res;
1105 d1_stack[++(*d1_level)] = NULL;
1108 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1109 &cmd_str, &cmd_len);
1111 else if (!strcmp (p, "element"))
1113 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1114 &cmd_str, &cmd_len);
1117 tagBegin (spec, d1_stack, d1_level, cmd_str, cmd_len);
1118 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1119 &cmd_str, &cmd_len);
1121 else if (!strcmp (p, "variant"))
1124 const char *class_str = NULL;
1126 const char *type_str = NULL;
1128 const char *value_str = NULL;
1129 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1130 &cmd_str, &cmd_len);
1133 class_str = cmd_str;
1134 class_len = cmd_len;
1135 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1136 &cmd_str, &cmd_len);
1142 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1143 &cmd_str, &cmd_len);
1146 value_str = cmd_str;
1147 value_len = cmd_len;
1149 variantBegin (spec, d1_stack, d1_level, class_str, class_len,
1150 type_str, type_len, value_str, value_len);
1153 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1154 &cmd_str, &cmd_len);
1156 else if (!strcmp (p, "context"))
1160 struct lexContext *lc = spec->context;
1161 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1162 &cmd_str, &cmd_len);
1163 p = regxStrz (cmd_str, cmd_len, ptmp);
1165 logf (LOG_DEBUG, "begin context %s", p);
1167 while (lc && strcmp (p, lc->name))
1170 spec->context_stack[++(spec->context_stack_top)] = lc;
1172 logf (LOG_WARN, "unknown context %s", p);
1175 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1176 &cmd_str, &cmd_len);
1180 logf (LOG_WARN, "bad keyword '%s' after begin", p);
1183 else if (!strcmp (p, "end"))
1185 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1186 &cmd_str, &cmd_len);
1189 logf (LOG_WARN, "missing keyword after 'end'");
1192 p = regxStrz (cmd_str, cmd_len, ptmp);
1193 if (!strcmp (p, "record"))
1196 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1197 &cmd_str, &cmd_len);
1199 logf (LOG_DEBUG, "end record");
1203 else if (!strcmp (p, "element"))
1205 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1206 &cmd_str, &cmd_len);
1216 tagEnd (spec, d1_stack, d1_level, cmd_str, cmd_len);
1217 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1218 &cmd_str, &cmd_len);
1221 tagEnd (spec, d1_stack, d1_level, NULL, 0);
1223 else if (!strcmp (p, "context"))
1226 logf (LOG_DEBUG, "end context");
1228 if (spec->context_stack_top)
1229 (spec->context_stack_top)--;
1230 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1231 &cmd_str, &cmd_len);
1234 logf (LOG_WARN, "bad keyword '%s' after end", p);
1236 else if (!strcmp (p, "data"))
1240 const char *element_str = NULL;
1242 while ((r = execTok (spec, &s, arg_no, arg_start, arg_end,
1243 &cmd_str, &cmd_len)) == 3)
1245 if (cmd_len==5 && !memcmp ("-text", cmd_str, cmd_len))
1247 else if (cmd_len==8 && !memcmp ("-element", cmd_str, cmd_len))
1249 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1250 &element_str, &element_len);
1255 logf (LOG_WARN, "bad data option: %.*s",
1260 logf (LOG_WARN, "missing data item after data");
1264 tagBegin (spec, d1_stack, d1_level, element_str, element_len);
1267 execData (spec, d1_stack, d1_level, cmd_str, cmd_len,
1269 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1270 &cmd_str, &cmd_len);
1273 tagEnd (spec, d1_stack, d1_level, NULL, 0);
1275 else if (!strcmp (p, "unread"))
1278 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1279 &cmd_str, &cmd_len);
1280 if (r==3 && cmd_len == 7 && !memcmp ("-offset", cmd_str, cmd_len))
1282 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1283 &cmd_str, &cmd_len);
1286 logf (LOG_WARN, "missing number after -offset");
1289 p = regxStrz (cmd_str, cmd_len, ptmp);
1291 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1292 &cmd_str, &cmd_len);
1298 logf (LOG_WARN, "missing index after unread command");
1301 if (cmd_len != 1 || *cmd_str < '0' || *cmd_str > '9')
1303 logf (LOG_WARN, "bad index after unread command");
1308 no = *cmd_str - '0';
1311 *pptr = arg_start[no] + offset;
1313 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1314 &cmd_str, &cmd_len);
1316 else if (!strcmp (p, "context"))
1320 struct lexContext *lc = spec->context;
1321 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1322 &cmd_str, &cmd_len);
1323 p = regxStrz (cmd_str, cmd_len, ptmp);
1325 while (lc && strcmp (p, lc->name))
1328 spec->context_stack[spec->context_stack_top] = lc;
1330 logf (LOG_WARN, "unknown context %s", p);
1333 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1334 &cmd_str, &cmd_len);
1338 logf (LOG_WARN, "unknown code command '%.*s'", cmd_len, cmd_str);
1339 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1340 &cmd_str, &cmd_len);
1345 logf (LOG_WARN, "ignoring token %.*s", cmd_len, cmd_str);
1347 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str,
1357 * execAction: Execute action specified by 'ap'. Returns 0 if
1358 * the pattern(s) associated by rule and code could be executed
1359 * ok; returns 1 if code couldn't be executed.
1361 static int execAction (struct lexSpec *spec, struct lexRuleAction *ap,
1362 data1_node **d1_stack, int *d1_level,
1363 int start_ptr, int *pptr)
1370 arg_start[0] = start_ptr;
1378 if (ap->u.pattern.body)
1380 arg_start[arg_no] = *pptr;
1381 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1383 arg_end[arg_no] = F_WIN_EOF;
1385 arg_start[arg_no] = F_WIN_EOF;
1386 arg_end[arg_no] = F_WIN_EOF;
1391 arg_end[arg_no] = sptr;
1393 arg_start[arg_no] = sptr;
1394 arg_end[arg_no] = *pptr;
1399 arg_start[arg_no] = *pptr;
1400 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1402 if (sptr != arg_start[arg_no])
1404 arg_end[arg_no] = *pptr;
1409 if (!execCode (spec, arg_no, arg_start, arg_end, pptr,
1410 ap->u.code, d1_stack, d1_level))
1414 arg_start[arg_no] = *pptr;
1415 arg_end[arg_no] = F_WIN_EOF;
1424 static int execRule (struct lexSpec *spec, struct lexContext *context,
1425 data1_node **d1_stack, int *d1_level,
1426 int ruleNo, int start_ptr, int *pptr)
1429 logf (LOG_DEBUG, "exec rule %d", ruleNo);
1431 return execAction (spec, context->fastRule[ruleNo]->actionList,
1432 d1_stack, d1_level, start_ptr, pptr);
1435 data1_node *lexNode (struct lexSpec *spec,
1436 data1_node **d1_stack, int *d1_level, int *ptr)
1438 struct lexContext *context = spec->context_stack[spec->context_stack_top];
1439 struct DFA_state *state = context->dfa->states[0];
1442 unsigned char c_prev = '\n';
1444 int last_rule = 0; /* rule number of current match */
1445 int last_ptr = *ptr; /* last char of match */
1446 int start_ptr = *ptr; /* first char of match */
1447 int skip_ptr = *ptr; /* first char of run */
1451 c = f_win_advance (spec, ptr);
1452 if (*ptr == F_WIN_EOF)
1454 /* end of file met */
1457 /* there was a match */
1458 if (skip_ptr < start_ptr)
1460 /* deal with chars that didn't match */
1463 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1464 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1466 /* restore pointer */
1469 if (!execRule (spec, context, d1_stack, d1_level,
1470 last_rule, start_ptr, ptr))
1472 /* restore skip pointer */
1476 else if (skip_ptr < *ptr)
1478 /* deal with chars that didn't match */
1481 buf = f_win_get (spec, skip_ptr, *ptr, &size);
1482 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1484 if (*ptr == F_WIN_EOF)
1491 { /* no transition for character c ... */
1494 if (skip_ptr < start_ptr)
1496 /* deal with chars that didn't match */
1499 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1500 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1502 /* restore pointer */
1504 if (!execRule (spec, context, d1_stack, d1_level,
1505 last_rule, start_ptr, ptr))
1507 if (spec->f_win_ef && *ptr != F_WIN_EOF)
1510 logf (LOG_DEBUG, "regx: endf ptr=%d", *ptr);
1512 (*spec->f_win_ef)(spec->f_win_fh, *ptr);
1516 context = spec->context_stack[spec->context_stack_top];
1519 last_ptr = start_ptr = *ptr;
1523 c_prev = f_win_advance (spec, &start_ptr);
1528 c_prev = f_win_advance (spec, &start_ptr);
1531 state = context->dfa->states[0];
1534 else if (c >= t->ch[0] && c <= t->ch[1])
1535 { /* transition ... */
1536 state = context->dfa->states[t->to];
1541 last_rule = state->rule_no;
1544 else if (state->rule_nno)
1546 last_rule = state->rule_nno;
1558 static data1_node *lexRoot (struct lexSpec *spec, off_t offset,
1559 const char *context_name)
1561 struct lexContext *lt = spec->context;
1562 data1_node *d1_stack[512];
1566 spec->context_stack_top = 0;
1569 if (!strcmp (lt->name, context_name))
1575 logf (LOG_WARN, "cannot find context %s", context_name);
1578 spec->context_stack[spec->context_stack_top] = lt;
1579 d1_stack[d1_level] = NULL;
1580 if (lt->beginActionList)
1581 execAction (spec, lt->beginActionList, d1_stack, &d1_level, 0, &ptr);
1582 lexNode (spec, d1_stack, &d1_level, &ptr);
1583 if (lt->endActionList)
1584 execAction (spec, lt->endActionList, d1_stack, &d1_level, ptr, &ptr);
1588 data1_node *grs_read_regx (struct grs_read_info *p)
1593 logf (LOG_DEBUG, "grs_read_regx");
1595 if (!curLexSpec || strcmp (curLexSpec->name, p->type))
1598 lexSpecDestroy (&curLexSpec);
1599 curLexSpec = lexSpecCreate (p->type);
1600 curLexSpec->dh = p->dh;
1601 res = readFileSpec (curLexSpec);
1604 lexSpecDestroy (&curLexSpec);
1610 curLexSpec->f_win_start = 0;
1611 curLexSpec->f_win_end = 0;
1612 curLexSpec->f_win_rf = p->readf;
1613 curLexSpec->f_win_sf = p->seekf;
1614 curLexSpec->f_win_fh = p->fh;
1615 curLexSpec->f_win_ef = p->endf;
1616 curLexSpec->f_win_size = 500000;
1618 curLexSpec->m = p->mem;
1619 return lexRoot (curLexSpec, p->offset, "main");