2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1998-06-30 15:15:09 adam
8 * Tags are trimmed: white space removed before- and after the tag.
10 * Revision 1.15 1998/06/30 12:55:45 adam
13 * Revision 1.14 1998/03/05 08:41:00 adam
14 * Implemented rule contexts.
16 * Revision 1.13 1997/12/12 06:33:58 adam
17 * Fixed bug that showed up when multiple filter where used.
18 * Made one routine thread-safe.
20 * Revision 1.12 1997/11/18 10:03:24 adam
21 * Member num_children removed from data1_node.
23 * Revision 1.11 1997/11/06 11:41:01 adam
24 * Implemented "begin variant" for the sgml.regx filter.
26 * Revision 1.10 1997/10/31 12:36:12 adam
27 * Minor change that avoids compiler warning.
29 * Revision 1.9 1997/09/29 09:02:49 adam
30 * Fixed small bug (introduced by previous commit).
32 * Revision 1.8 1997/09/17 12:19:22 adam
33 * Zebra version corresponds to YAZ version 1.4.
34 * Changed Zebra server so that it doesn't depend on global common_resource.
36 * Revision 1.7 1997/07/15 16:33:07 adam
37 * Check for zero length in execData.
39 * Revision 1.6 1997/02/24 10:41:51 adam
40 * Cleanup of code and commented out the "end element-end-record" code.
42 * Revision 1.5 1997/02/19 16:22:33 adam
43 * Fixed "end element" to terminate record in outer-most level.
45 * Revision 1.4 1997/02/12 20:42:58 adam
46 * Changed some log messages.
48 * Revision 1.3 1996/11/08 14:05:33 adam
49 * Bug fix: data1 node member u.tag.get_bytes weren't initialized.
51 * Revision 1.2 1996/10/29 14:02:09 adam
52 * Doesn't use the global data1_tabpath (from YAZ). Instead the function
53 * data1_get_tabpath is used.
55 * Revision 1.1 1996/10/11 10:57:30 adam
56 * New module recctrl. Used to manage records (extract/retrieval).
58 * Revision 1.24 1996/06/17 14:25:31 adam
59 * Removed LOG_DEBUG logs; can still be enabled by setting REGX_DEBUG.
61 * Revision 1.23 1996/06/04 10:19:00 adam
62 * Minor changes - removed include of ctype.h.
64 * Revision 1.22 1996/06/03 15:23:13 adam
65 * Bug fix: /../ BODY /../ - pattern didn't match EOF.
67 * Revision 1.21 1996/05/14 16:58:38 adam
70 * Revision 1.20 1996/05/01 13:46:36 adam
71 * First work on multiple records in one file.
72 * New option, -offset, to the "unread" command in the filter module.
74 * Revision 1.19 1996/02/12 16:18:20 adam
75 * Yet another bug fix in implementation of unread command.
77 * Revision 1.18 1996/02/12 16:07:54 adam
78 * Bug fix in new unread command.
80 * Revision 1.17 1996/02/12 15:56:11 adam
81 * New code command: unread.
83 * Revision 1.16 1996/01/17 14:57:51 adam
84 * Prototype changed for reader functions in extract/retrieve. File
85 * is identified by 'void *' instead of 'int.
87 * Revision 1.15 1996/01/08 19:15:47 adam
88 * New input filter that works!
90 * Revision 1.14 1996/01/08 09:10:38 adam
91 * Yet another complete rework on this module.
93 * Revision 1.13 1995/12/15 17:21:50 adam
94 * This version is able to set data.formatted_text in data1-nodes.
96 * Revision 1.12 1995/12/15 16:20:10 adam
97 * The filter files (*.flt) are read from the path given by data1_tabpath.
99 * Revision 1.11 1995/12/15 12:35:16 adam
102 * Revision 1.10 1995/12/15 10:35:36 adam
105 * Revision 1.9 1995/12/14 16:38:48 adam
106 * Completely new attempt to make regular expression parsing.
108 * Revision 1.8 1995/12/13 17:16:59 adam
111 * Revision 1.7 1995/12/13 16:51:58 adam
112 * Modified to set last_child in data1_nodes.
113 * Uses destroy handler to free up data text nodes.
115 * Revision 1.6 1995/12/13 13:45:37 quinn
116 * Changed data1 to use nmem.
118 * Revision 1.5 1995/12/11 09:12:52 adam
119 * The rec_get function returns NULL if record doesn't exist - will
120 * happen in the server if the result set records have been deleted since
121 * the creation of the set (i.e. the search).
122 * The server saves a result temporarily if it is 'volatile', i.e. the
123 * set is register dependent.
125 * Revision 1.4 1995/12/05 16:57:40 adam
126 * More work on regular patterns.
128 * Revision 1.3 1995/12/05 09:37:09 adam
129 * One malloc was renamed to xmalloc.
131 * Revision 1.2 1995/12/04 17:59:24 adam
132 * More work on regular expression conversion.
134 * Revision 1.1 1995/12/04 14:25:30 adam
135 * Started work on regular expression parsed input to structured records.
144 #include <zebrautl.h>
150 #define F_WIN_EOF 2000000000
154 #define REGX_PATTERN 1
159 #define REGX_CONTEXT 6
165 struct lexRuleAction {
169 struct DFA *dfa; /* REGX_PATTERN */
172 struct regxCode *code; /* REGX_CODE */
174 struct lexRuleAction *next;
179 struct lexRuleAction *actionList;
183 struct lexRuleInfo info;
184 struct lexRule *next;
190 struct lexRule *rules;
191 struct lexRuleInfo **fastRule;
194 struct lexRuleAction *beginActionList;
195 struct lexRuleAction *endActionList;
196 struct lexContext *next;
201 struct lexContext *context;
203 struct lexContext **context_stack;
204 int context_stack_size;
205 int context_stack_top;
211 void (*f_win_ef)(void *, off_t);
213 int f_win_start; /* first byte of buffer is this file offset */
214 int f_win_end; /* last byte of buffer is this offset - 1 */
215 int f_win_size; /* size of buffer */
216 char *f_win_buf; /* buffer itself */
217 int (*f_win_rf)(void *, char *, size_t);
218 off_t (*f_win_sf)(void *, off_t);
223 static char *f_win_get (struct lexSpec *spec, off_t start_pos, off_t end_pos,
226 int i, r, off = start_pos - spec->f_win_start;
228 if (off >= 0 && end_pos <= spec->f_win_end)
230 *size = end_pos - start_pos;
231 return spec->f_win_buf + off;
233 if (off < 0 || start_pos >= spec->f_win_end)
235 (*spec->f_win_sf)(spec->f_win_fh, start_pos);
236 spec->f_win_start = start_pos;
238 if (!spec->f_win_buf)
239 spec->f_win_buf = xmalloc (spec->f_win_size);
240 *size = (*spec->f_win_rf)(spec->f_win_fh, spec->f_win_buf,
242 spec->f_win_end = spec->f_win_start + *size;
244 if (*size > end_pos - start_pos)
245 *size = end_pos - start_pos;
246 return spec->f_win_buf;
248 for (i = 0; i<spec->f_win_end - start_pos; i++)
249 spec->f_win_buf[i] = spec->f_win_buf[i + off];
250 r = (*spec->f_win_rf)(spec->f_win_fh,
252 spec->f_win_size - i);
253 spec->f_win_start = start_pos;
254 spec->f_win_end += r;
256 if (*size > end_pos - start_pos)
257 *size = end_pos - start_pos;
258 return spec->f_win_buf;
261 static int f_win_advance (struct lexSpec *spec, int *pos)
266 if (*pos >= spec->f_win_start && *pos < spec->f_win_end)
267 return spec->f_win_buf[(*pos)++ - spec->f_win_start];
268 if (*pos == F_WIN_EOF)
270 buf = f_win_get (spec, *pos, *pos+1, &size);
280 static void regxCodeDel (struct regxCode **pp)
282 struct regxCode *p = *pp;
291 static void regxCodeMk (struct regxCode **pp, const char *buf, int len)
295 p = xmalloc (sizeof(*p));
296 p->str = xmalloc (len+1);
297 memcpy (p->str, buf, len);
302 static struct DFA *lexSpecDFA (void)
307 dfa_parse_cmap_del (dfa, ' ');
308 dfa_parse_cmap_del (dfa, '\t');
309 dfa_parse_cmap_add (dfa, '/', 0);
313 static void actionListDel (struct lexRuleAction **rap)
315 struct lexRuleAction *ra1, *ra;
317 for (ra = *rap; ra; ra = ra1)
323 dfa_delete (&ra->u.pattern.dfa);
326 regxCodeDel (&ra->u.code);
334 static struct lexContext *lexContextCreate (const char *name)
336 struct lexContext *p = xmalloc (sizeof(*p));
338 p->name = xstrdup (name);
340 p->dfa = lexSpecDFA ();
343 p->beginActionList = NULL;
344 p->endActionList = NULL;
349 static void lexContextDestroy (struct lexContext *p)
351 struct lexRule *rp, *rp1;
354 for (rp = p->rules; rp; rp = rp1)
357 actionListDel (&rp->info.actionList);
360 actionListDel (&p->beginActionList);
361 actionListDel (&p->endActionList);
366 static struct lexSpec *lexSpecCreate (const char *name)
370 p = xmalloc (sizeof(*p));
371 p->name = xmalloc (strlen(name)+1);
372 strcpy (p->name, name);
375 p->context_stack_size = 100;
376 p->context_stack = xmalloc (sizeof(*p->context_stack) *
377 p->context_stack_size);
382 static void lexSpecDestroy (struct lexSpec **pp)
385 struct lexContext *lt;
394 struct lexContext *lt_next = lt->next;
395 lexContextDestroy (lt);
399 xfree (p->f_win_buf);
400 xfree (p->context_stack);
405 static int readParseToken (const char **cpp, int *len)
407 const char *cp = *cpp;
411 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
440 if (*cp >= 'a' && *cp <= 'z')
442 else if (*cp >= 'A' && *cp <= 'Z')
443 cmd[i] = *cp + 'a' - 'A';
446 if (i < sizeof(cmd)-2)
453 logf (LOG_WARN, "bad character %d %c", *cp, *cp);
455 while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
461 if (!strcmp (cmd, "begin"))
463 else if (!strcmp (cmd, "end"))
465 else if (!strcmp (cmd, "body"))
467 else if (!strcmp (cmd, "context"))
471 logf (LOG_WARN, "bad command %s", cmd);
477 static int actionListMk (struct lexSpec *spec, const char *s,
478 struct lexRuleAction **ap)
484 while ((tok = readParseToken (&s, &len)))
492 *ap = xmalloc (sizeof(**ap));
494 regxCodeMk (&(*ap)->u.code, s, len);
498 *ap = xmalloc (sizeof(**ap));
500 (*ap)->u.pattern.body = bodyMark;
502 (*ap)->u.pattern.dfa = lexSpecDFA ();
504 r = dfa_parse ((*ap)->u.pattern.dfa, &s);
509 logf (LOG_WARN, "regular expression error '%.*s'", s-s0, s0);
512 dfa_mkstate ((*ap)->u.pattern.dfa);
516 logf (LOG_WARN, "cannot use begin here");
519 *ap = xmalloc (sizeof(**ap));
529 int readOneSpec (struct lexSpec *spec, const char *s)
533 struct lexContext *lc;
535 tok = readParseToken (&s, &len);
536 if (tok == REGX_CONTEXT)
538 char context_name[32];
539 tok = readParseToken (&s, &len);
540 if (tok != REGX_CODE)
542 logf (LOG_WARN, "missing name after CONTEXT keyword");
547 memcpy (context_name, s, len);
548 context_name[len] = '\0';
549 lc = lexContextCreate (context_name);
550 lc->next = spec->context;
555 spec->context = lexContextCreate ("main");
560 actionListDel (&spec->context->beginActionList);
561 actionListMk (spec, s, &spec->context->beginActionList);
564 actionListDel (&spec->context->endActionList);
565 actionListMk (spec, s, &spec->context->endActionList);
569 logf (LOG_DEBUG, "rule %d %s", spec->context->ruleNo, s);
571 r = dfa_parse (spec->context->dfa, &s);
574 logf (LOG_WARN, "regular expression error. r=%d", r);
579 logf (LOG_WARN, "expects / at end of pattern. got %c", *s);
583 rp = xmalloc (sizeof(*rp));
584 rp->info.no = spec->context->ruleNo++;
585 rp->next = spec->context->rules;
586 spec->context->rules = rp;
587 actionListMk (spec, s, &rp->info.actionList);
592 int readFileSpec (struct lexSpec *spec)
594 struct lexContext *lc;
597 int c, i, errors = 0;
600 lineBuf = xmalloc (1+lineSize);
601 logf (LOG_LOG, "reading regx filter %s.flt", spec->name);
602 sprintf (lineBuf, "%s.flt", spec->name);
603 if (!(spec_inf = yaz_path_fopen (data1_get_tabpath(spec->dh),
606 logf (LOG_ERRNO|LOG_WARN, "cannot read spec file %s", spec->name);
615 if (c == '#' || c == '\n' || c == ' ' || c == '\t')
617 while (c != '\n' && c != EOF)
636 if (c != ' ' && c != '\t')
645 readOneSpec (spec, lineBuf);
646 spec->lineNo += addLine;
655 debug_dfa_followpos = 1;
658 for (lc = spec->context; lc; lc = lc->next)
661 lc->fastRule = xmalloc (sizeof(*lc->fastRule) * lc->ruleNo);
662 for (i = 0; i < lc->ruleNo; i++)
663 lc->fastRule[i] = NULL;
664 for (rp = lc->rules; rp; rp = rp->next)
665 lc->fastRule[rp->info.no] = &rp->info;
666 dfa_mkstate (lc->dfa);
673 static struct lexSpec *curLexSpec = NULL;
675 static void destroy_data (struct data1_node *n)
677 assert (n->which == DATA1N_data);
678 xfree (n->u.data.data);
681 static void execData (struct lexSpec *spec,
682 data1_node **d1_stack, int *d1_level,
683 const char *ebuf, int elen, int formatted_text)
685 struct data1_node *res, *parent;
687 if (elen == 0) /* shouldn't happen, but it does! */
691 logf (LOG_DEBUG, "data (%d bytes) %.15s ... %.*s", elen,
692 ebuf, 15, ebuf + elen-15);
694 logf (LOG_DEBUG, "data (%d bytes) %.*s", elen, elen, ebuf);
696 logf (LOG_DEBUG, "data (%d bytes)", elen);
702 parent = d1_stack[*d1_level -1];
704 if ((res=d1_stack[*d1_level]) && res->which == DATA1N_data)
706 if (elen + res->u.data.len <= DATA1_LOCALDATA)
707 memcpy (res->u.data.data + res->u.data.len, ebuf, elen);
710 char *nb = xmalloc (elen + res->u.data.len);
711 memcpy (nb, res->u.data.data, res->u.data.len);
712 memcpy (nb + res->u.data.len, ebuf, elen);
713 res->u.data.data = nb;
714 res->destroy = destroy_data;
716 res->u.data.len += elen;
720 res = data1_mk_node (spec->dh, spec->m);
721 res->parent = parent;
722 res->which = DATA1N_data;
723 res->u.data.what = DATA1I_text;
724 res->u.data.len = elen;
725 res->u.data.formatted_text = formatted_text;
726 if (elen > DATA1_LOCALDATA)
727 res->u.data.data = nmem_malloc (spec->m, elen);
729 res->u.data.data = res->lbuf;
730 memcpy (res->u.data.data, ebuf, elen);
731 res->root = parent->root;
733 parent->last_child = res;
734 if (d1_stack[*d1_level])
735 d1_stack[*d1_level]->next = res;
738 d1_stack[*d1_level] = res;
742 static void execDataP (struct lexSpec *spec,
743 data1_node **d1_stack, int *d1_level,
744 const char *ebuf, int elen, int formatted_text)
746 execData (spec, d1_stack, d1_level, ebuf, elen, formatted_text);
749 static void variantBegin (struct lexSpec *spec,
750 data1_node **d1_stack, int *d1_level,
751 const char *class_str, int class_len,
752 const char *type_str, int type_len,
753 const char *value_str, int value_len)
755 struct data1_node *parent = d1_stack[*d1_level -1];
756 char tclass[DATA1_MAX_SYMBOL], ttype[DATA1_MAX_SYMBOL];
763 logf (LOG_WARN, "in variant begin. No record type defined");
766 if (class_len >= DATA1_MAX_SYMBOL)
767 class_len = DATA1_MAX_SYMBOL-1;
768 memcpy (tclass, class_str, class_len);
769 tclass[class_len] = '\0';
771 if (type_len >= DATA1_MAX_SYMBOL)
772 type_len = DATA1_MAX_SYMBOL-1;
773 memcpy (ttype, type_str, type_len);
774 ttype[type_len] = '\0';
777 logf (LOG_DEBUG, "variant begin %s %s (%d)", tclass, ttype, *d1_level);
781 data1_getvartypebyct(spec->dh, parent->root->u.root.absyn->varset,
785 if (parent->which != DATA1N_variant)
787 res = data1_mk_node (spec->dh, spec->m);
788 res->parent = parent;
789 res->which = DATA1N_variant;
790 res->u.variant.type = 0;
791 res->u.variant.value = 0;
792 res->root = parent->root;
794 parent->last_child = res;
795 if (d1_stack[*d1_level])
796 d1_stack[*d1_level]->next = res;
799 d1_stack[*d1_level] = res;
800 d1_stack[++(*d1_level)] = NULL;
802 for (i = *d1_level-1; d1_stack[i]->which == DATA1N_variant; i--)
803 if (d1_stack[i]->u.variant.type == tp)
810 logf (LOG_DEBUG, "variant node (%d)", *d1_level);
812 parent = d1_stack[*d1_level-1];
813 res = data1_mk_node (spec->dh, spec->m);
814 res->parent = parent;
815 res->which = DATA1N_variant;
816 res->root = parent->root;
817 res->u.variant.type = tp;
819 if (value_len >= DATA1_LOCALDATA)
820 value_len =DATA1_LOCALDATA-1;
821 memcpy (res->lbuf, value_str, value_len);
822 res->lbuf[value_len] = '\0';
824 res->u.variant.value = res->lbuf;
826 parent->last_child = res;
827 if (d1_stack[*d1_level])
828 d1_stack[*d1_level]->next = res;
831 d1_stack[*d1_level] = res;
832 d1_stack[++(*d1_level)] = NULL;
835 static void tagStrip (const char **tag, int *len)
839 for (i = *len; i > 0 && isspace((*tag)[i-1]); --i)
842 for (i = 0; i < *len && isspace((*tag)[i]); i++)
848 static void tagBegin (struct lexSpec *spec,
849 data1_node **d1_stack, int *d1_level,
850 const char *tag, int len)
852 struct data1_node *parent = d1_stack[*d1_level -1];
853 data1_element *elem = NULL;
854 data1_node *partag = get_parent_tag(spec->dh, parent);
856 data1_element *e = NULL;
861 logf (LOG_WARN, "in element begin. No record type defined");
864 tagStrip (&tag, &len);
866 res = data1_mk_node (spec->dh, spec->m);
867 res->parent = parent;
868 res->which = DATA1N_tag;
869 res->u.tag.get_bytes = -1;
871 if (len >= DATA1_LOCALDATA)
872 len = DATA1_LOCALDATA-1;
873 memcpy (res->lbuf, tag, len);
874 res->lbuf[len] = '\0';
875 res->u.tag.tag = res->lbuf;
878 logf (LOG_DEBUG, "begin tag %s (%d)", res->u.tag.tag, *d1_level);
880 if (parent->which == DATA1N_variant)
883 if (!(e = partag->u.tag.element))
886 elem = data1_getelementbytagname (spec->dh, d1_stack[0]->u.root.absyn,
888 res->u.tag.element = elem;
889 res->u.tag.node_selected = 0;
890 res->u.tag.make_variantlist = 0;
891 res->u.tag.no_data_requested = 0;
892 res->root = parent->root;
894 parent->last_child = res;
895 if (d1_stack[*d1_level])
896 d1_stack[*d1_level]->next = res;
899 d1_stack[*d1_level] = res;
900 d1_stack[++(*d1_level)] = NULL;
903 static void tagEnd (struct lexSpec *spec,
904 data1_node **d1_stack, int *d1_level,
905 const char *tag, int len)
907 tagStrip (&tag, &len);
908 while (*d1_level > 1)
911 if ((d1_stack[*d1_level]->which == DATA1N_tag) &&
913 (strlen(d1_stack[*d1_level]->u.tag.tag) == (size_t) len &&
914 !memcmp (d1_stack[*d1_level]->u.tag.tag, tag, len))))
918 logf (LOG_DEBUG, "end tag (%d)", *d1_level);
923 static int tryMatch (struct lexSpec *spec, int *pptr, int *mptr,
926 struct DFA_state *state = dfa->states[0];
929 unsigned char c_prev = 0;
930 int ptr = *pptr; /* current pointer */
931 int start_ptr = *pptr; /* first char of match */
932 int last_ptr = 0; /* last char of match */
933 int last_rule = 0; /* rule number of current match */
938 c = f_win_advance (spec, &ptr);
939 if (ptr == F_WIN_EOF)
956 *mptr = start_ptr; /* match starts here */
957 *pptr = last_ptr; /* match end here (+1) */
960 state = dfa->states[0];
965 else if (c >= t->ch[0] && c <= t->ch[1])
967 state = dfa->states[t->to];
972 last_rule = state->rule_no;
977 last_rule = state->rule_nno;
989 static int execTok (struct lexSpec *spec, const char **src,
990 int arg_no, int *arg_start, int *arg_end,
991 const char **tokBuf, int *tokLen)
993 const char *s = *src;
995 while (*s == ' ' || *s == '\t')
999 if (*s == '$' && s[1] >= '0' && s[1] <= '9')
1003 while (*s >= '0' && *s <= '9')
1004 n = n*10 + (*s++ -'0');
1014 *tokBuf = f_win_get (spec, arg_start[n], arg_end[n], tokLen);
1017 else if (*s == '\"')
1020 while (*s && *s != '\"')
1022 *tokLen = s - *tokBuf;
1027 else if (*s == '\n' || *s == ';')
1035 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
1037 *tokLen = s - *tokBuf;
1044 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
1046 *tokLen = s - *tokBuf;
1052 static char *regxStrz (const char *src, int len, char *str)
1056 memcpy (str, src, len);
1061 static int execCode (struct lexSpec *spec,
1062 int arg_no, int *arg_start, int *arg_end, int *pptr,
1063 struct regxCode *code,
1064 data1_node **d1_stack, int *d1_level)
1066 const char *s = code->str;
1069 const char *cmd_str;
1071 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str, &cmd_len);
1078 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1079 &cmd_str, &cmd_len);
1082 p = regxStrz (cmd_str, cmd_len, ptmp);
1083 if (!strcmp (p, "begin"))
1085 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1086 &cmd_str, &cmd_len);
1089 logf (LOG_WARN, "missing keyword after 'begin'");
1092 p = regxStrz (cmd_str, cmd_len, ptmp);
1093 if (!strcmp (p, "record"))
1095 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1096 &cmd_str, &cmd_len);
1101 static char absynName[64];
1106 memcpy (absynName, cmd_str, cmd_len);
1107 absynName[cmd_len] = '\0';
1110 logf (LOG_DEBUG, "begin record %s", absynName);
1112 if (!(absyn = data1_get_absyn (spec->dh, absynName)))
1113 logf (LOG_WARN, "Unknown tagset: %s", absynName);
1118 res = data1_mk_node (spec->dh, spec->m);
1119 res->which = DATA1N_root;
1120 res->u.root.type = absynName;
1121 res->u.root.absyn = absyn;
1124 d1_stack[*d1_level] = res;
1125 d1_stack[++(*d1_level)] = NULL;
1128 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1129 &cmd_str, &cmd_len);
1131 else if (!strcmp (p, "element"))
1133 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1134 &cmd_str, &cmd_len);
1137 tagBegin (spec, d1_stack, d1_level, cmd_str, cmd_len);
1138 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1139 &cmd_str, &cmd_len);
1141 else if (!strcmp (p, "variant"))
1144 const char *class_str = NULL;
1146 const char *type_str = NULL;
1148 const char *value_str = NULL;
1149 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1150 &cmd_str, &cmd_len);
1153 class_str = cmd_str;
1154 class_len = cmd_len;
1155 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1156 &cmd_str, &cmd_len);
1162 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1163 &cmd_str, &cmd_len);
1166 value_str = cmd_str;
1167 value_len = cmd_len;
1169 variantBegin (spec, d1_stack, d1_level, class_str, class_len,
1170 type_str, type_len, value_str, value_len);
1173 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1174 &cmd_str, &cmd_len);
1176 else if (!strcmp (p, "context"))
1180 struct lexContext *lc = spec->context;
1181 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1182 &cmd_str, &cmd_len);
1183 p = regxStrz (cmd_str, cmd_len, ptmp);
1185 logf (LOG_DEBUG, "begin context %s", p);
1187 while (lc && strcmp (p, lc->name))
1190 spec->context_stack[++(spec->context_stack_top)] = lc;
1192 logf (LOG_WARN, "unknown context %s", p);
1195 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1196 &cmd_str, &cmd_len);
1200 logf (LOG_WARN, "bad keyword '%s' after begin", p);
1203 else if (!strcmp (p, "end"))
1205 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1206 &cmd_str, &cmd_len);
1209 logf (LOG_WARN, "missing keyword after 'end'");
1212 p = regxStrz (cmd_str, cmd_len, ptmp);
1213 if (!strcmp (p, "record"))
1216 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1217 &cmd_str, &cmd_len);
1219 logf (LOG_DEBUG, "end record");
1223 else if (!strcmp (p, "element"))
1225 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1226 &cmd_str, &cmd_len);
1236 tagEnd (spec, d1_stack, d1_level, cmd_str, cmd_len);
1237 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1238 &cmd_str, &cmd_len);
1241 tagEnd (spec, d1_stack, d1_level, NULL, 0);
1243 else if (!strcmp (p, "context"))
1246 logf (LOG_DEBUG, "end context");
1248 if (spec->context_stack_top)
1249 (spec->context_stack_top)--;
1250 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1251 &cmd_str, &cmd_len);
1254 logf (LOG_WARN, "bad keyword '%s' after end", p);
1256 else if (!strcmp (p, "data"))
1260 const char *element_str = NULL;
1262 while ((r = execTok (spec, &s, arg_no, arg_start, arg_end,
1263 &cmd_str, &cmd_len)) == 3)
1265 if (cmd_len==5 && !memcmp ("-text", cmd_str, cmd_len))
1267 else if (cmd_len==8 && !memcmp ("-element", cmd_str, cmd_len))
1269 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1270 &element_str, &element_len);
1275 logf (LOG_WARN, "bad data option: %.*s",
1280 logf (LOG_WARN, "missing data item after data");
1284 tagBegin (spec, d1_stack, d1_level, element_str, element_len);
1287 execData (spec, d1_stack, d1_level, cmd_str, cmd_len,
1289 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1290 &cmd_str, &cmd_len);
1293 tagEnd (spec, d1_stack, d1_level, NULL, 0);
1295 else if (!strcmp (p, "unread"))
1298 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1299 &cmd_str, &cmd_len);
1300 if (r==3 && cmd_len == 7 && !memcmp ("-offset", cmd_str, cmd_len))
1302 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1303 &cmd_str, &cmd_len);
1306 logf (LOG_WARN, "missing number after -offset");
1309 p = regxStrz (cmd_str, cmd_len, ptmp);
1311 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1312 &cmd_str, &cmd_len);
1318 logf (LOG_WARN, "missing index after unread command");
1321 if (cmd_len != 1 || *cmd_str < '0' || *cmd_str > '9')
1323 logf (LOG_WARN, "bad index after unread command");
1328 no = *cmd_str - '0';
1331 *pptr = arg_start[no] + offset;
1333 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1334 &cmd_str, &cmd_len);
1336 else if (!strcmp (p, "context"))
1340 struct lexContext *lc = spec->context;
1341 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1342 &cmd_str, &cmd_len);
1343 p = regxStrz (cmd_str, cmd_len, ptmp);
1345 while (lc && strcmp (p, lc->name))
1348 spec->context_stack[spec->context_stack_top] = lc;
1350 logf (LOG_WARN, "unknown context %s", p);
1353 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1354 &cmd_str, &cmd_len);
1358 logf (LOG_WARN, "unknown code command '%.*s'", cmd_len, cmd_str);
1359 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1360 &cmd_str, &cmd_len);
1365 logf (LOG_WARN, "ignoring token %.*s", cmd_len, cmd_str);
1367 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str,
1377 * execAction: Execute action specified by 'ap'. Returns 0 if
1378 * the pattern(s) associated by rule and code could be executed
1379 * ok; returns 1 if code couldn't be executed.
1381 static int execAction (struct lexSpec *spec, struct lexRuleAction *ap,
1382 data1_node **d1_stack, int *d1_level,
1383 int start_ptr, int *pptr)
1390 arg_start[0] = start_ptr;
1398 if (ap->u.pattern.body)
1400 arg_start[arg_no] = *pptr;
1401 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1403 arg_end[arg_no] = F_WIN_EOF;
1405 arg_start[arg_no] = F_WIN_EOF;
1406 arg_end[arg_no] = F_WIN_EOF;
1411 arg_end[arg_no] = sptr;
1413 arg_start[arg_no] = sptr;
1414 arg_end[arg_no] = *pptr;
1419 arg_start[arg_no] = *pptr;
1420 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1422 if (sptr != arg_start[arg_no])
1424 arg_end[arg_no] = *pptr;
1429 if (!execCode (spec, arg_no, arg_start, arg_end, pptr,
1430 ap->u.code, d1_stack, d1_level))
1434 arg_start[arg_no] = *pptr;
1435 arg_end[arg_no] = F_WIN_EOF;
1444 static int execRule (struct lexSpec *spec, struct lexContext *context,
1445 data1_node **d1_stack, int *d1_level,
1446 int ruleNo, int start_ptr, int *pptr)
1449 logf (LOG_DEBUG, "exec rule %d", ruleNo);
1451 return execAction (spec, context->fastRule[ruleNo]->actionList,
1452 d1_stack, d1_level, start_ptr, pptr);
1455 data1_node *lexNode (struct lexSpec *spec,
1456 data1_node **d1_stack, int *d1_level, int *ptr)
1458 struct lexContext *context = spec->context_stack[spec->context_stack_top];
1459 struct DFA_state *state = context->dfa->states[0];
1462 unsigned char c_prev = '\n';
1464 int last_rule = 0; /* rule number of current match */
1465 int last_ptr = *ptr; /* last char of match */
1466 int start_ptr = *ptr; /* first char of match */
1467 int skip_ptr = *ptr; /* first char of run */
1471 c = f_win_advance (spec, ptr);
1472 if (*ptr == F_WIN_EOF)
1474 /* end of file met */
1477 /* there was a match */
1478 if (skip_ptr < start_ptr)
1480 /* deal with chars that didn't match */
1483 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1484 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1486 /* restore pointer */
1489 if (!execRule (spec, context, d1_stack, d1_level,
1490 last_rule, start_ptr, ptr))
1492 /* restore skip pointer */
1496 else if (skip_ptr < *ptr)
1498 /* deal with chars that didn't match */
1501 buf = f_win_get (spec, skip_ptr, *ptr, &size);
1502 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1504 if (*ptr == F_WIN_EOF)
1511 { /* no transition for character c ... */
1514 if (skip_ptr < start_ptr)
1516 /* deal with chars that didn't match */
1519 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1520 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1522 /* restore pointer */
1524 if (!execRule (spec, context, d1_stack, d1_level,
1525 last_rule, start_ptr, ptr))
1527 if (spec->f_win_ef && *ptr != F_WIN_EOF)
1530 logf (LOG_DEBUG, "regx: endf ptr=%d", *ptr);
1532 (*spec->f_win_ef)(spec->f_win_fh, *ptr);
1536 context = spec->context_stack[spec->context_stack_top];
1539 last_ptr = start_ptr = *ptr;
1543 c_prev = f_win_advance (spec, &start_ptr);
1548 c_prev = f_win_advance (spec, &start_ptr);
1551 state = context->dfa->states[0];
1554 else if (c >= t->ch[0] && c <= t->ch[1])
1555 { /* transition ... */
1556 state = context->dfa->states[t->to];
1561 last_rule = state->rule_no;
1564 else if (state->rule_nno)
1566 last_rule = state->rule_nno;
1578 static data1_node *lexRoot (struct lexSpec *spec, off_t offset,
1579 const char *context_name)
1581 struct lexContext *lt = spec->context;
1582 data1_node *d1_stack[512];
1586 spec->context_stack_top = 0;
1589 if (!strcmp (lt->name, context_name))
1595 logf (LOG_WARN, "cannot find context %s", context_name);
1598 spec->context_stack[spec->context_stack_top] = lt;
1599 d1_stack[d1_level] = NULL;
1600 if (lt->beginActionList)
1601 execAction (spec, lt->beginActionList, d1_stack, &d1_level, 0, &ptr);
1602 lexNode (spec, d1_stack, &d1_level, &ptr);
1603 if (lt->endActionList)
1604 execAction (spec, lt->endActionList, d1_stack, &d1_level, ptr, &ptr);
1608 data1_node *grs_read_regx (struct grs_read_info *p)
1613 logf (LOG_DEBUG, "grs_read_regx");
1615 if (!curLexSpec || strcmp (curLexSpec->name, p->type))
1618 lexSpecDestroy (&curLexSpec);
1619 curLexSpec = lexSpecCreate (p->type);
1620 curLexSpec->dh = p->dh;
1621 res = readFileSpec (curLexSpec);
1624 lexSpecDestroy (&curLexSpec);
1630 curLexSpec->f_win_start = 0;
1631 curLexSpec->f_win_end = 0;
1632 curLexSpec->f_win_rf = p->readf;
1633 curLexSpec->f_win_sf = p->seekf;
1634 curLexSpec->f_win_fh = p->fh;
1635 curLexSpec->f_win_ef = p->endf;
1636 curLexSpec->f_win_size = 500000;
1638 curLexSpec->m = p->mem;
1639 return lexRoot (curLexSpec, p->offset, "main");