2 * Copyright (C) 1994-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.21 1998-11-03 15:43:39 adam
8 * Fixed bug introduced by previous commit.
10 * Revision 1.20 1998/11/03 14:51:28 adam
11 * Changed code so that it creates as few data1 nodes as possible.
13 * Revision 1.19 1998/11/03 10:22:39 adam
14 * Fixed memory leak that could occur for when large data1 node were
15 * concatenated. Data-type data1_nodes may have multiple nodes.
17 * Revision 1.18 1998/10/15 13:11:47 adam
18 * Added support for option -record for "end element". When specified
19 * end element will mark end-of-record when at outer-level.
21 * Revision 1.17 1998/07/01 10:13:51 adam
24 * Revision 1.16 1998/06/30 15:15:09 adam
25 * Tags are trimmed: white space removed before- and after the tag.
27 * Revision 1.15 1998/06/30 12:55:45 adam
30 * Revision 1.14 1998/03/05 08:41:00 adam
31 * Implemented rule contexts.
33 * Revision 1.13 1997/12/12 06:33:58 adam
34 * Fixed bug that showed up when multiple filter where used.
35 * Made one routine thread-safe.
37 * Revision 1.12 1997/11/18 10:03:24 adam
38 * Member num_children removed from data1_node.
40 * Revision 1.11 1997/11/06 11:41:01 adam
41 * Implemented "begin variant" for the sgml.regx filter.
43 * Revision 1.10 1997/10/31 12:36:12 adam
44 * Minor change that avoids compiler warning.
46 * Revision 1.9 1997/09/29 09:02:49 adam
47 * Fixed small bug (introduced by previous commit).
49 * Revision 1.8 1997/09/17 12:19:22 adam
50 * Zebra version corresponds to YAZ version 1.4.
51 * Changed Zebra server so that it doesn't depend on global common_resource.
53 * Revision 1.7 1997/07/15 16:33:07 adam
54 * Check for zero length in execData.
56 * Revision 1.6 1997/02/24 10:41:51 adam
57 * Cleanup of code and commented out the "end element-end-record" code.
59 * Revision 1.5 1997/02/19 16:22:33 adam
60 * Fixed "end element" to terminate record in outer-most level.
62 * Revision 1.4 1997/02/12 20:42:58 adam
63 * Changed some log messages.
65 * Revision 1.3 1996/11/08 14:05:33 adam
66 * Bug fix: data1 node member u.tag.get_bytes weren't initialized.
68 * Revision 1.2 1996/10/29 14:02:09 adam
69 * Doesn't use the global data1_tabpath (from YAZ). Instead the function
70 * data1_get_tabpath is used.
72 * Revision 1.1 1996/10/11 10:57:30 adam
73 * New module recctrl. Used to manage records (extract/retrieval).
75 * Revision 1.24 1996/06/17 14:25:31 adam
76 * Removed LOG_DEBUG logs; can still be enabled by setting REGX_DEBUG.
78 * Revision 1.23 1996/06/04 10:19:00 adam
79 * Minor changes - removed include of ctype.h.
81 * Revision 1.22 1996/06/03 15:23:13 adam
82 * Bug fix: /../ BODY /../ - pattern didn't match EOF.
84 * Revision 1.21 1996/05/14 16:58:38 adam
87 * Revision 1.20 1996/05/01 13:46:36 adam
88 * First work on multiple records in one file.
89 * New option, -offset, to the "unread" command in the filter module.
91 * Revision 1.19 1996/02/12 16:18:20 adam
92 * Yet another bug fix in implementation of unread command.
94 * Revision 1.18 1996/02/12 16:07:54 adam
95 * Bug fix in new unread command.
97 * Revision 1.17 1996/02/12 15:56:11 adam
98 * New code command: unread.
100 * Revision 1.16 1996/01/17 14:57:51 adam
101 * Prototype changed for reader functions in extract/retrieve. File
102 * is identified by 'void *' instead of 'int.
104 * Revision 1.15 1996/01/08 19:15:47 adam
105 * New input filter that works!
107 * Revision 1.14 1996/01/08 09:10:38 adam
108 * Yet another complete rework on this module.
110 * Revision 1.13 1995/12/15 17:21:50 adam
111 * This version is able to set data.formatted_text in data1-nodes.
113 * Revision 1.12 1995/12/15 16:20:10 adam
114 * The filter files (*.flt) are read from the path given by data1_tabpath.
116 * Revision 1.11 1995/12/15 12:35:16 adam
119 * Revision 1.10 1995/12/15 10:35:36 adam
122 * Revision 1.9 1995/12/14 16:38:48 adam
123 * Completely new attempt to make regular expression parsing.
125 * Revision 1.8 1995/12/13 17:16:59 adam
128 * Revision 1.7 1995/12/13 16:51:58 adam
129 * Modified to set last_child in data1_nodes.
130 * Uses destroy handler to free up data text nodes.
132 * Revision 1.6 1995/12/13 13:45:37 quinn
133 * Changed data1 to use nmem.
135 * Revision 1.5 1995/12/11 09:12:52 adam
136 * The rec_get function returns NULL if record doesn't exist - will
137 * happen in the server if the result set records have been deleted since
138 * the creation of the set (i.e. the search).
139 * The server saves a result temporarily if it is 'volatile', i.e. the
140 * set is register dependent.
142 * Revision 1.4 1995/12/05 16:57:40 adam
143 * More work on regular patterns.
145 * Revision 1.3 1995/12/05 09:37:09 adam
146 * One malloc was renamed to xmalloc.
148 * Revision 1.2 1995/12/04 17:59:24 adam
149 * More work on regular expression conversion.
151 * Revision 1.1 1995/12/04 14:25:30 adam
152 * Started work on regular expression parsed input to structured records.
161 #include <zebrautl.h>
167 #define F_WIN_EOF 2000000000
171 #define REGX_PATTERN 1
176 #define REGX_CONTEXT 6
182 struct lexRuleAction {
186 struct DFA *dfa; /* REGX_PATTERN */
189 struct regxCode *code; /* REGX_CODE */
191 struct lexRuleAction *next;
196 struct lexRuleAction *actionList;
200 struct lexRuleInfo info;
201 struct lexRule *next;
207 struct lexRule *rules;
208 struct lexRuleInfo **fastRule;
211 struct lexRuleAction *beginActionList;
212 struct lexRuleAction *endActionList;
213 struct lexContext *next;
216 struct lexConcatBuf {
225 struct lexContext *context;
227 struct lexContext **context_stack;
228 int context_stack_size;
229 int context_stack_top;
235 void (*f_win_ef)(void *, off_t);
237 int f_win_start; /* first byte of buffer is this file offset */
238 int f_win_end; /* last byte of buffer is this offset - 1 */
239 int f_win_size; /* size of buffer */
240 char *f_win_buf; /* buffer itself */
241 int (*f_win_rf)(void *, char *, size_t);
242 off_t (*f_win_sf)(void *, off_t);
244 struct lexConcatBuf **concatBuf;
249 static char *f_win_get (struct lexSpec *spec, off_t start_pos, off_t end_pos,
252 int i, r, off = start_pos - spec->f_win_start;
254 if (off >= 0 && end_pos <= spec->f_win_end)
256 *size = end_pos - start_pos;
257 return spec->f_win_buf + off;
259 if (off < 0 || start_pos >= spec->f_win_end)
261 (*spec->f_win_sf)(spec->f_win_fh, start_pos);
262 spec->f_win_start = start_pos;
264 if (!spec->f_win_buf)
265 spec->f_win_buf = xmalloc (spec->f_win_size);
266 *size = (*spec->f_win_rf)(spec->f_win_fh, spec->f_win_buf,
268 spec->f_win_end = spec->f_win_start + *size;
270 if (*size > end_pos - start_pos)
271 *size = end_pos - start_pos;
272 return spec->f_win_buf;
274 for (i = 0; i<spec->f_win_end - start_pos; i++)
275 spec->f_win_buf[i] = spec->f_win_buf[i + off];
276 r = (*spec->f_win_rf)(spec->f_win_fh,
278 spec->f_win_size - i);
279 spec->f_win_start = start_pos;
280 spec->f_win_end += r;
282 if (*size > end_pos - start_pos)
283 *size = end_pos - start_pos;
284 return spec->f_win_buf;
287 static int f_win_advance (struct lexSpec *spec, int *pos)
292 if (*pos >= spec->f_win_start && *pos < spec->f_win_end)
293 return spec->f_win_buf[(*pos)++ - spec->f_win_start];
294 if (*pos == F_WIN_EOF)
296 buf = f_win_get (spec, *pos, *pos+1, &size);
306 static void regxCodeDel (struct regxCode **pp)
308 struct regxCode *p = *pp;
317 static void regxCodeMk (struct regxCode **pp, const char *buf, int len)
321 p = xmalloc (sizeof(*p));
322 p->str = xmalloc (len+1);
323 memcpy (p->str, buf, len);
328 static struct DFA *lexSpecDFA (void)
333 dfa_parse_cmap_del (dfa, ' ');
334 dfa_parse_cmap_del (dfa, '\t');
335 dfa_parse_cmap_add (dfa, '/', 0);
339 static void actionListDel (struct lexRuleAction **rap)
341 struct lexRuleAction *ra1, *ra;
343 for (ra = *rap; ra; ra = ra1)
349 dfa_delete (&ra->u.pattern.dfa);
352 regxCodeDel (&ra->u.code);
360 static struct lexContext *lexContextCreate (const char *name)
362 struct lexContext *p = xmalloc (sizeof(*p));
364 p->name = xstrdup (name);
366 p->dfa = lexSpecDFA ();
369 p->beginActionList = NULL;
370 p->endActionList = NULL;
375 static void lexContextDestroy (struct lexContext *p)
377 struct lexRule *rp, *rp1;
380 for (rp = p->rules; rp; rp = rp1)
383 actionListDel (&rp->info.actionList);
386 actionListDel (&p->beginActionList);
387 actionListDel (&p->endActionList);
392 static struct lexSpec *lexSpecCreate (const char *name)
397 p = xmalloc (sizeof(*p));
398 p->name = xmalloc (strlen(name)+1);
399 strcpy (p->name, name);
402 p->context_stack_size = 100;
403 p->context_stack = xmalloc (sizeof(*p->context_stack) *
404 p->context_stack_size);
408 p->concatBuf = xmalloc (sizeof(*p->concatBuf) * p->maxLevel);
409 for (i = 0; i < p->maxLevel; i++)
411 p->concatBuf[i] = xmalloc (sizeof(**p->concatBuf));
412 p->concatBuf[i]->len = p->concatBuf[i]->max = 0;
413 p->concatBuf[i]->buf = 0;
418 static void lexSpecDestroy (struct lexSpec **pp)
421 struct lexContext *lt;
429 for (i = 0; i < p->maxLevel; i++)
430 xfree (p->concatBuf[i]);
431 xfree (p->concatBuf);
436 struct lexContext *lt_next = lt->next;
437 lexContextDestroy (lt);
441 xfree (p->f_win_buf);
442 xfree (p->context_stack);
447 static int readParseToken (const char **cpp, int *len)
449 const char *cp = *cpp;
453 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
482 if (*cp >= 'a' && *cp <= 'z')
484 else if (*cp >= 'A' && *cp <= 'Z')
485 cmd[i] = *cp + 'a' - 'A';
488 if (i < sizeof(cmd)-2)
495 logf (LOG_WARN, "bad character %d %c", *cp, *cp);
497 while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
503 if (!strcmp (cmd, "begin"))
505 else if (!strcmp (cmd, "end"))
507 else if (!strcmp (cmd, "body"))
509 else if (!strcmp (cmd, "context"))
513 logf (LOG_WARN, "bad command %s", cmd);
519 static int actionListMk (struct lexSpec *spec, const char *s,
520 struct lexRuleAction **ap)
526 while ((tok = readParseToken (&s, &len)))
534 *ap = xmalloc (sizeof(**ap));
536 regxCodeMk (&(*ap)->u.code, s, len);
540 *ap = xmalloc (sizeof(**ap));
542 (*ap)->u.pattern.body = bodyMark;
544 (*ap)->u.pattern.dfa = lexSpecDFA ();
546 r = dfa_parse ((*ap)->u.pattern.dfa, &s);
551 logf (LOG_WARN, "regular expression error '%.*s'", s-s0, s0);
554 dfa_mkstate ((*ap)->u.pattern.dfa);
558 logf (LOG_WARN, "cannot use begin here");
561 *ap = xmalloc (sizeof(**ap));
571 int readOneSpec (struct lexSpec *spec, const char *s)
575 struct lexContext *lc;
577 tok = readParseToken (&s, &len);
578 if (tok == REGX_CONTEXT)
580 char context_name[32];
581 tok = readParseToken (&s, &len);
582 if (tok != REGX_CODE)
584 logf (LOG_WARN, "missing name after CONTEXT keyword");
589 memcpy (context_name, s, len);
590 context_name[len] = '\0';
591 lc = lexContextCreate (context_name);
592 lc->next = spec->context;
597 spec->context = lexContextCreate ("main");
602 actionListDel (&spec->context->beginActionList);
603 actionListMk (spec, s, &spec->context->beginActionList);
606 actionListDel (&spec->context->endActionList);
607 actionListMk (spec, s, &spec->context->endActionList);
611 logf (LOG_DEBUG, "rule %d %s", spec->context->ruleNo, s);
613 r = dfa_parse (spec->context->dfa, &s);
616 logf (LOG_WARN, "regular expression error. r=%d", r);
621 logf (LOG_WARN, "expects / at end of pattern. got %c", *s);
625 rp = xmalloc (sizeof(*rp));
626 rp->info.no = spec->context->ruleNo++;
627 rp->next = spec->context->rules;
628 spec->context->rules = rp;
629 actionListMk (spec, s, &rp->info.actionList);
634 int readFileSpec (struct lexSpec *spec)
636 struct lexContext *lc;
639 int c, i, errors = 0;
642 lineBuf = xmalloc (1+lineSize);
643 logf (LOG_LOG, "reading regx filter %s.flt", spec->name);
644 sprintf (lineBuf, "%s.flt", spec->name);
645 if (!(spec_inf = yaz_path_fopen (data1_get_tabpath(spec->dh),
648 logf (LOG_ERRNO|LOG_WARN, "cannot read spec file %s", spec->name);
657 if (c == '#' || c == '\n' || c == ' ' || c == '\t')
659 while (c != '\n' && c != EOF)
678 if (c != ' ' && c != '\t')
687 readOneSpec (spec, lineBuf);
688 spec->lineNo += addLine;
697 debug_dfa_followpos = 1;
700 for (lc = spec->context; lc; lc = lc->next)
703 lc->fastRule = xmalloc (sizeof(*lc->fastRule) * lc->ruleNo);
704 for (i = 0; i < lc->ruleNo; i++)
705 lc->fastRule[i] = NULL;
706 for (rp = lc->rules; rp; rp = rp->next)
707 lc->fastRule[rp->info.no] = &rp->info;
708 dfa_mkstate (lc->dfa);
715 static struct lexSpec *curLexSpec = NULL;
717 static void execData (struct lexSpec *spec,
718 data1_node **d1_stack, int *d1_level,
719 const char *ebuf, int elen, int formatted_text)
721 struct data1_node *res, *parent;
724 if (elen == 0) /* shouldn't happen, but it does! */
728 logf (LOG_DEBUG, "data (%d bytes) %.15s ... %.*s", elen,
729 ebuf, 15, ebuf + elen-15);
731 logf (LOG_DEBUG, "data (%d bytes) %.*s", elen, elen, ebuf);
733 logf (LOG_DEBUG, "data (%d bytes)", elen);
739 parent = d1_stack[*d1_level -1];
742 if ((res = d1_stack[*d1_level]) && res->which == DATA1N_data)
743 org_len = res->u.data.len;
748 res = data1_mk_node (spec->dh, spec->m);
749 res->parent = parent;
750 res->which = DATA1N_data;
751 res->u.data.what = DATA1I_text;
753 res->u.data.formatted_text = formatted_text;
755 if (elen > DATA1_LOCALDATA)
756 res->u.data.data = nmem_malloc (spec->m, elen);
758 res->u.data.data = res->lbuf;
759 memcpy (res->u.data.data, ebuf, elen);
761 res->u.data.data = 0;
763 res->root = parent->root;
765 parent->last_child = res;
766 if (d1_stack[*d1_level])
767 d1_stack[*d1_level]->next = res;
770 d1_stack[*d1_level] = res;
772 if (org_len + elen >= spec->concatBuf[*d1_level]->max)
774 char *old_buf, *new_buf;
776 spec->concatBuf[*d1_level]->max = org_len + elen + 256;
777 new_buf = xmalloc (spec->concatBuf[*d1_level]->max);
778 if ((old_buf = spec->concatBuf[*d1_level]->buf))
780 memcpy (new_buf, old_buf, org_len);
783 spec->concatBuf[*d1_level]->buf = new_buf;
785 assert (spec->concatBuf[*d1_level]);
786 memcpy (spec->concatBuf[*d1_level]->buf + org_len, ebuf, elen);
787 res->u.data.len += elen;
790 static void execDataP (struct lexSpec *spec,
791 data1_node **d1_stack, int *d1_level,
792 const char *ebuf, int elen, int formatted_text)
794 execData (spec, d1_stack, d1_level, ebuf, elen, formatted_text);
797 static void tagDataRelease (struct lexSpec *spec,
798 data1_node **d1_stack, int d1_level)
802 assert (d1_stack[d1_level]);
803 if ((res = d1_stack[d1_level]) &&
804 res->which == DATA1N_data &&
805 res->u.data.what == DATA1I_text)
807 assert (!res->u.data.data);
808 assert (res->u.data.len > 0);
809 if (res->u.data.len > DATA1_LOCALDATA)
810 res->u.data.data = nmem_malloc (spec->m, res->u.data.len);
812 res->u.data.data = res->lbuf;
813 memcpy (res->u.data.data, spec->concatBuf[d1_level]->buf,
818 static void variantBegin (struct lexSpec *spec,
819 data1_node **d1_stack, int *d1_level,
820 const char *class_str, int class_len,
821 const char *type_str, int type_len,
822 const char *value_str, int value_len)
824 struct data1_node *parent = d1_stack[*d1_level -1];
825 char tclass[DATA1_MAX_SYMBOL], ttype[DATA1_MAX_SYMBOL];
832 logf (LOG_WARN, "in variant begin. No record type defined");
835 if (class_len >= DATA1_MAX_SYMBOL)
836 class_len = DATA1_MAX_SYMBOL-1;
837 memcpy (tclass, class_str, class_len);
838 tclass[class_len] = '\0';
840 if (type_len >= DATA1_MAX_SYMBOL)
841 type_len = DATA1_MAX_SYMBOL-1;
842 memcpy (ttype, type_str, type_len);
843 ttype[type_len] = '\0';
846 logf (LOG_DEBUG, "variant begin %s %s (%d)", tclass, ttype, *d1_level);
850 data1_getvartypebyct(spec->dh, parent->root->u.root.absyn->varset,
854 if (parent->which != DATA1N_variant)
856 res = data1_mk_node (spec->dh, spec->m);
857 res->parent = parent;
858 res->which = DATA1N_variant;
859 res->u.variant.type = 0;
860 res->u.variant.value = 0;
861 res->root = parent->root;
863 parent->last_child = res;
864 if (d1_stack[*d1_level])
865 d1_stack[*d1_level]->next = res;
868 d1_stack[*d1_level] = res;
869 d1_stack[++(*d1_level)] = NULL;
871 for (i = *d1_level-1; d1_stack[i]->which == DATA1N_variant; i--)
872 if (d1_stack[i]->u.variant.type == tp)
879 logf (LOG_DEBUG, "variant node (%d)", *d1_level);
881 parent = d1_stack[*d1_level-1];
882 res = data1_mk_node (spec->dh, spec->m);
883 res->parent = parent;
884 res->which = DATA1N_variant;
885 res->root = parent->root;
886 res->u.variant.type = tp;
888 if (value_len >= DATA1_LOCALDATA)
889 value_len =DATA1_LOCALDATA-1;
890 memcpy (res->lbuf, value_str, value_len);
891 res->lbuf[value_len] = '\0';
893 res->u.variant.value = res->lbuf;
895 parent->last_child = res;
896 if (d1_stack[*d1_level])
898 tagDataRelease (spec, d1_stack, *d1_level);
899 d1_stack[*d1_level]->next = res;
903 d1_stack[*d1_level] = res;
904 d1_stack[++(*d1_level)] = NULL;
907 static void tagStrip (const char **tag, int *len)
911 for (i = *len; i > 0 && isspace((*tag)[i-1]); --i)
914 for (i = 0; i < *len && isspace((*tag)[i]); i++)
920 static void tagBegin (struct lexSpec *spec,
921 data1_node **d1_stack, int *d1_level,
922 const char *tag, int len)
924 struct data1_node *parent = d1_stack[*d1_level -1];
925 data1_element *elem = NULL;
926 data1_node *partag = get_parent_tag(spec->dh, parent);
928 data1_element *e = NULL;
933 logf (LOG_WARN, "in element begin. No record type defined");
936 tagStrip (&tag, &len);
938 res = data1_mk_node (spec->dh, spec->m);
939 res->parent = parent;
940 res->which = DATA1N_tag;
941 res->u.tag.get_bytes = -1;
943 if (len >= DATA1_LOCALDATA)
944 res->u.tag.tag = nmem_malloc (spec->m, len+1);
946 res->u.tag.tag = res->lbuf;
948 memcpy (res->u.tag.tag, tag, len);
949 res->u.tag.tag[len] = '\0';
952 logf (LOG_DEBUG, "begin tag %s (%d)", res->u.tag.tag, *d1_level);
954 if (parent->which == DATA1N_variant)
957 if (!(e = partag->u.tag.element))
960 elem = data1_getelementbytagname (spec->dh, d1_stack[0]->u.root.absyn,
962 res->u.tag.element = elem;
963 res->u.tag.node_selected = 0;
964 res->u.tag.make_variantlist = 0;
965 res->u.tag.no_data_requested = 0;
966 res->root = parent->root;
968 parent->last_child = res;
969 if (d1_stack[*d1_level])
971 tagDataRelease (spec, d1_stack, *d1_level);
972 d1_stack[*d1_level]->next = res;
976 d1_stack[*d1_level] = res;
977 d1_stack[++(*d1_level)] = NULL;
980 static void tagEnd (struct lexSpec *spec,
981 data1_node **d1_stack, int *d1_level, int min_level,
982 const char *tag, int len)
984 tagStrip (&tag, &len);
985 while (*d1_level > min_level)
987 tagDataRelease (spec, d1_stack, *d1_level);
991 if ((d1_stack[*d1_level]->which == DATA1N_tag) &&
993 (strlen(d1_stack[*d1_level]->u.tag.tag) == (size_t) len &&
994 !memcmp (d1_stack[*d1_level]->u.tag.tag, tag, len))))
998 logf (LOG_DEBUG, "end tag (%d)", *d1_level);
1003 static int tryMatch (struct lexSpec *spec, int *pptr, int *mptr,
1006 struct DFA_state *state = dfa->states[0];
1009 unsigned char c_prev = 0;
1010 int ptr = *pptr; /* current pointer */
1011 int start_ptr = *pptr; /* first char of match */
1012 int last_ptr = 0; /* last char of match */
1013 int last_rule = 0; /* rule number of current match */
1018 c = f_win_advance (spec, &ptr);
1019 if (ptr == F_WIN_EOF)
1036 *mptr = start_ptr; /* match starts here */
1037 *pptr = last_ptr; /* match end here (+1) */
1040 state = dfa->states[0];
1045 else if (c >= t->ch[0] && c <= t->ch[1])
1047 state = dfa->states[t->to];
1052 last_rule = state->rule_no;
1057 last_rule = state->rule_nno;
1069 static int execTok (struct lexSpec *spec, const char **src,
1070 int arg_no, int *arg_start, int *arg_end,
1071 const char **tokBuf, int *tokLen)
1073 const char *s = *src;
1075 while (*s == ' ' || *s == '\t')
1079 if (*s == '$' && s[1] >= '0' && s[1] <= '9')
1083 while (*s >= '0' && *s <= '9')
1084 n = n*10 + (*s++ -'0');
1094 *tokBuf = f_win_get (spec, arg_start[n], arg_end[n], tokLen);
1097 else if (*s == '\"')
1100 while (*s && *s != '\"')
1102 *tokLen = s - *tokBuf;
1107 else if (*s == '\n' || *s == ';')
1115 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
1117 *tokLen = s - *tokBuf;
1124 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
1126 *tokLen = s - *tokBuf;
1132 static char *regxStrz (const char *src, int len, char *str)
1136 memcpy (str, src, len);
1141 static int execCode (struct lexSpec *spec,
1142 int arg_no, int *arg_start, int *arg_end, int *pptr,
1143 struct regxCode *code,
1144 data1_node **d1_stack, int *d1_level)
1146 const char *s = code->str;
1149 const char *cmd_str;
1151 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str, &cmd_len);
1158 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1159 &cmd_str, &cmd_len);
1162 p = regxStrz (cmd_str, cmd_len, ptmp);
1163 if (!strcmp (p, "begin"))
1165 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1166 &cmd_str, &cmd_len);
1169 logf (LOG_WARN, "missing keyword after 'begin'");
1172 p = regxStrz (cmd_str, cmd_len, ptmp);
1173 if (!strcmp (p, "record"))
1175 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1176 &cmd_str, &cmd_len);
1181 static char absynName[64];
1186 memcpy (absynName, cmd_str, cmd_len);
1187 absynName[cmd_len] = '\0';
1190 logf (LOG_DEBUG, "begin record %s", absynName);
1192 if (!(absyn = data1_get_absyn (spec->dh, absynName)))
1193 logf (LOG_WARN, "Unknown tagset: %s", absynName);
1198 res = data1_mk_node (spec->dh, spec->m);
1199 res->which = DATA1N_root;
1200 res->u.root.type = absynName;
1201 res->u.root.absyn = absyn;
1204 d1_stack[*d1_level] = res;
1205 d1_stack[++(*d1_level)] = NULL;
1208 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1209 &cmd_str, &cmd_len);
1211 else if (!strcmp (p, "element"))
1213 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1214 &cmd_str, &cmd_len);
1217 tagBegin (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);
1221 else if (!strcmp (p, "variant"))
1224 const char *class_str = NULL;
1226 const char *type_str = NULL;
1228 const char *value_str = NULL;
1229 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1230 &cmd_str, &cmd_len);
1233 class_str = cmd_str;
1234 class_len = cmd_len;
1235 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1236 &cmd_str, &cmd_len);
1242 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1243 &cmd_str, &cmd_len);
1246 value_str = cmd_str;
1247 value_len = cmd_len;
1249 variantBegin (spec, d1_stack, d1_level, class_str, class_len,
1250 type_str, type_len, value_str, value_len);
1253 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1254 &cmd_str, &cmd_len);
1256 else if (!strcmp (p, "context"))
1260 struct lexContext *lc = spec->context;
1261 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1262 &cmd_str, &cmd_len);
1263 p = regxStrz (cmd_str, cmd_len, ptmp);
1265 logf (LOG_DEBUG, "begin context %s", p);
1267 while (lc && strcmp (p, lc->name))
1270 spec->context_stack[++(spec->context_stack_top)] = lc;
1272 logf (LOG_WARN, "unknown context %s", p);
1275 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1276 &cmd_str, &cmd_len);
1280 logf (LOG_WARN, "bad keyword '%s' after begin", p);
1283 else if (!strcmp (p, "end"))
1285 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1286 &cmd_str, &cmd_len);
1289 logf (LOG_WARN, "missing keyword after 'end'");
1292 p = regxStrz (cmd_str, cmd_len, ptmp);
1293 if (!strcmp (p, "record"))
1296 for (i = *d1_level; i; --i)
1297 tagDataRelease (spec, d1_stack, i);
1299 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1300 &cmd_str, &cmd_len);
1302 logf (LOG_DEBUG, "end record");
1306 else if (!strcmp (p, "element"))
1309 while ((r = execTok (spec, &s, arg_no, arg_start, arg_end,
1310 &cmd_str, &cmd_len)) == 3)
1312 if (cmd_len==7 && !memcmp ("-record", cmd_str, cmd_len))
1317 tagEnd (spec, d1_stack, d1_level, min_level,
1319 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1320 &cmd_str, &cmd_len);
1323 tagEnd (spec, d1_stack, d1_level, min_level, NULL, 0);
1327 logf (LOG_DEBUG, "end element end records");
1333 else if (!strcmp (p, "context"))
1336 logf (LOG_DEBUG, "end context");
1338 if (spec->context_stack_top)
1339 (spec->context_stack_top)--;
1340 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1341 &cmd_str, &cmd_len);
1344 logf (LOG_WARN, "bad keyword '%s' after end", p);
1346 else if (!strcmp (p, "data"))
1350 const char *element_str = NULL;
1352 while ((r = execTok (spec, &s, arg_no, arg_start, arg_end,
1353 &cmd_str, &cmd_len)) == 3)
1355 if (cmd_len==5 && !memcmp ("-text", cmd_str, cmd_len))
1357 else if (cmd_len==8 && !memcmp ("-element", cmd_str, cmd_len))
1359 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1360 &element_str, &element_len);
1365 logf (LOG_WARN, "bad data option: %.*s",
1370 logf (LOG_WARN, "missing data item after data");
1374 tagBegin (spec, d1_stack, d1_level, element_str, element_len);
1377 execData (spec, d1_stack, d1_level, cmd_str, cmd_len,
1379 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1380 &cmd_str, &cmd_len);
1383 tagEnd (spec, d1_stack, d1_level, 1, NULL, 0);
1385 else if (!strcmp (p, "unread"))
1388 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1389 &cmd_str, &cmd_len);
1390 if (r==3 && cmd_len == 7 && !memcmp ("-offset", cmd_str, cmd_len))
1392 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1393 &cmd_str, &cmd_len);
1396 logf (LOG_WARN, "missing number after -offset");
1399 p = regxStrz (cmd_str, cmd_len, ptmp);
1401 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1402 &cmd_str, &cmd_len);
1408 logf (LOG_WARN, "missing index after unread command");
1411 if (cmd_len != 1 || *cmd_str < '0' || *cmd_str > '9')
1413 logf (LOG_WARN, "bad index after unread command");
1418 no = *cmd_str - '0';
1421 *pptr = arg_start[no] + offset;
1423 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1424 &cmd_str, &cmd_len);
1426 else if (!strcmp (p, "context"))
1430 struct lexContext *lc = spec->context;
1431 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1432 &cmd_str, &cmd_len);
1433 p = regxStrz (cmd_str, cmd_len, ptmp);
1435 while (lc && strcmp (p, lc->name))
1438 spec->context_stack[spec->context_stack_top] = lc;
1440 logf (LOG_WARN, "unknown context %s", p);
1443 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1444 &cmd_str, &cmd_len);
1448 logf (LOG_WARN, "unknown code command '%.*s'", cmd_len, cmd_str);
1449 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1450 &cmd_str, &cmd_len);
1455 logf (LOG_WARN, "ignoring token %.*s", cmd_len, cmd_str);
1457 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str,
1467 * execAction: Execute action specified by 'ap'. Returns 0 if
1468 * the pattern(s) associated by rule and code could be executed
1469 * ok; returns 1 if code couldn't be executed.
1471 static int execAction (struct lexSpec *spec, struct lexRuleAction *ap,
1472 data1_node **d1_stack, int *d1_level,
1473 int start_ptr, int *pptr)
1480 arg_start[0] = start_ptr;
1488 if (ap->u.pattern.body)
1490 arg_start[arg_no] = *pptr;
1491 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1493 arg_end[arg_no] = F_WIN_EOF;
1495 arg_start[arg_no] = F_WIN_EOF;
1496 arg_end[arg_no] = F_WIN_EOF;
1501 arg_end[arg_no] = sptr;
1503 arg_start[arg_no] = sptr;
1504 arg_end[arg_no] = *pptr;
1509 arg_start[arg_no] = *pptr;
1510 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1512 if (sptr != arg_start[arg_no])
1514 arg_end[arg_no] = *pptr;
1519 if (!execCode (spec, arg_no, arg_start, arg_end, pptr,
1520 ap->u.code, d1_stack, d1_level))
1524 arg_start[arg_no] = *pptr;
1525 arg_end[arg_no] = F_WIN_EOF;
1534 static int execRule (struct lexSpec *spec, struct lexContext *context,
1535 data1_node **d1_stack, int *d1_level,
1536 int ruleNo, int start_ptr, int *pptr)
1539 logf (LOG_DEBUG, "exec rule %d", ruleNo);
1541 return execAction (spec, context->fastRule[ruleNo]->actionList,
1542 d1_stack, d1_level, start_ptr, pptr);
1545 data1_node *lexNode (struct lexSpec *spec,
1546 data1_node **d1_stack, int *d1_level, int *ptr)
1548 struct lexContext *context = spec->context_stack[spec->context_stack_top];
1549 struct DFA_state *state = context->dfa->states[0];
1552 unsigned char c_prev = '\n';
1554 int last_rule = 0; /* rule number of current match */
1555 int last_ptr = *ptr; /* last char of match */
1556 int start_ptr = *ptr; /* first char of match */
1557 int skip_ptr = *ptr; /* first char of run */
1561 c = f_win_advance (spec, ptr);
1562 if (*ptr == F_WIN_EOF)
1564 /* end of file met */
1567 /* there was a match */
1568 if (skip_ptr < start_ptr)
1570 /* deal with chars that didn't match */
1573 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1574 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1576 /* restore pointer */
1579 if (!execRule (spec, context, d1_stack, d1_level,
1580 last_rule, start_ptr, ptr))
1582 /* restore skip pointer */
1586 else if (skip_ptr < *ptr)
1588 /* deal with chars that didn't match */
1591 buf = f_win_get (spec, skip_ptr, *ptr, &size);
1592 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1594 if (*ptr == F_WIN_EOF)
1601 { /* no transition for character c ... */
1604 if (skip_ptr < start_ptr)
1606 /* deal with chars that didn't match */
1609 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1610 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1612 /* restore pointer */
1614 if (!execRule (spec, context, d1_stack, d1_level,
1615 last_rule, start_ptr, ptr))
1617 if (spec->f_win_ef && *ptr != F_WIN_EOF)
1620 logf (LOG_DEBUG, "regx: endf ptr=%d", *ptr);
1622 (*spec->f_win_ef)(spec->f_win_fh, *ptr);
1626 context = spec->context_stack[spec->context_stack_top];
1629 last_ptr = start_ptr = *ptr;
1633 c_prev = f_win_advance (spec, &start_ptr);
1638 c_prev = f_win_advance (spec, &start_ptr);
1641 state = context->dfa->states[0];
1644 else if (c >= t->ch[0] && c <= t->ch[1])
1645 { /* transition ... */
1646 state = context->dfa->states[t->to];
1651 last_rule = state->rule_no;
1654 else if (state->rule_nno)
1656 last_rule = state->rule_nno;
1668 static data1_node *lexRoot (struct lexSpec *spec, off_t offset,
1669 const char *context_name)
1671 struct lexContext *lt = spec->context;
1672 data1_node *d1_stack[128];
1674 int i, ptr = offset;
1676 spec->context_stack_top = 0;
1679 if (!strcmp (lt->name, context_name))
1685 logf (LOG_WARN, "cannot find context %s", context_name);
1688 spec->context_stack[spec->context_stack_top] = lt;
1689 d1_stack[d1_level] = NULL;
1690 if (lt->beginActionList)
1691 execAction (spec, lt->beginActionList, d1_stack, &d1_level, 0, &ptr);
1692 lexNode (spec, d1_stack, &d1_level, &ptr);
1693 for (i = d1_level; i; --i)
1694 tagDataRelease (spec, d1_stack, i);
1695 if (lt->endActionList)
1696 execAction (spec, lt->endActionList, d1_stack, &d1_level, ptr, &ptr);
1700 data1_node *grs_read_regx (struct grs_read_info *p)
1705 logf (LOG_DEBUG, "grs_read_regx");
1707 if (!curLexSpec || strcmp (curLexSpec->name, p->type))
1710 lexSpecDestroy (&curLexSpec);
1711 curLexSpec = lexSpecCreate (p->type);
1712 curLexSpec->dh = p->dh;
1713 res = readFileSpec (curLexSpec);
1716 lexSpecDestroy (&curLexSpec);
1720 curLexSpec->dh = p->dh;
1723 curLexSpec->f_win_start = 0;
1724 curLexSpec->f_win_end = 0;
1725 curLexSpec->f_win_rf = p->readf;
1726 curLexSpec->f_win_sf = p->seekf;
1727 curLexSpec->f_win_fh = p->fh;
1728 curLexSpec->f_win_ef = p->endf;
1729 curLexSpec->f_win_size = 500000;
1731 curLexSpec->m = p->mem;
1732 return lexRoot (curLexSpec, p->offset, "main");