2 * Copyright (C) 1994-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.20 1998-11-03 14:51:28 adam
8 * Changed code so that it creates as few data1 nodes as possible.
10 * Revision 1.19 1998/11/03 10:22:39 adam
11 * Fixed memory leak that could occur for when large data1 node were
12 * concatenated. Data-type data1_nodes may have multiple nodes.
14 * Revision 1.18 1998/10/15 13:11:47 adam
15 * Added support for option -record for "end element". When specified
16 * end element will mark end-of-record when at outer-level.
18 * Revision 1.17 1998/07/01 10:13:51 adam
21 * Revision 1.16 1998/06/30 15:15:09 adam
22 * Tags are trimmed: white space removed before- and after the tag.
24 * Revision 1.15 1998/06/30 12:55:45 adam
27 * Revision 1.14 1998/03/05 08:41:00 adam
28 * Implemented rule contexts.
30 * Revision 1.13 1997/12/12 06:33:58 adam
31 * Fixed bug that showed up when multiple filter where used.
32 * Made one routine thread-safe.
34 * Revision 1.12 1997/11/18 10:03:24 adam
35 * Member num_children removed from data1_node.
37 * Revision 1.11 1997/11/06 11:41:01 adam
38 * Implemented "begin variant" for the sgml.regx filter.
40 * Revision 1.10 1997/10/31 12:36:12 adam
41 * Minor change that avoids compiler warning.
43 * Revision 1.9 1997/09/29 09:02:49 adam
44 * Fixed small bug (introduced by previous commit).
46 * Revision 1.8 1997/09/17 12:19:22 adam
47 * Zebra version corresponds to YAZ version 1.4.
48 * Changed Zebra server so that it doesn't depend on global common_resource.
50 * Revision 1.7 1997/07/15 16:33:07 adam
51 * Check for zero length in execData.
53 * Revision 1.6 1997/02/24 10:41:51 adam
54 * Cleanup of code and commented out the "end element-end-record" code.
56 * Revision 1.5 1997/02/19 16:22:33 adam
57 * Fixed "end element" to terminate record in outer-most level.
59 * Revision 1.4 1997/02/12 20:42:58 adam
60 * Changed some log messages.
62 * Revision 1.3 1996/11/08 14:05:33 adam
63 * Bug fix: data1 node member u.tag.get_bytes weren't initialized.
65 * Revision 1.2 1996/10/29 14:02:09 adam
66 * Doesn't use the global data1_tabpath (from YAZ). Instead the function
67 * data1_get_tabpath is used.
69 * Revision 1.1 1996/10/11 10:57:30 adam
70 * New module recctrl. Used to manage records (extract/retrieval).
72 * Revision 1.24 1996/06/17 14:25:31 adam
73 * Removed LOG_DEBUG logs; can still be enabled by setting REGX_DEBUG.
75 * Revision 1.23 1996/06/04 10:19:00 adam
76 * Minor changes - removed include of ctype.h.
78 * Revision 1.22 1996/06/03 15:23:13 adam
79 * Bug fix: /../ BODY /../ - pattern didn't match EOF.
81 * Revision 1.21 1996/05/14 16:58:38 adam
84 * Revision 1.20 1996/05/01 13:46:36 adam
85 * First work on multiple records in one file.
86 * New option, -offset, to the "unread" command in the filter module.
88 * Revision 1.19 1996/02/12 16:18:20 adam
89 * Yet another bug fix in implementation of unread command.
91 * Revision 1.18 1996/02/12 16:07:54 adam
92 * Bug fix in new unread command.
94 * Revision 1.17 1996/02/12 15:56:11 adam
95 * New code command: unread.
97 * Revision 1.16 1996/01/17 14:57:51 adam
98 * Prototype changed for reader functions in extract/retrieve. File
99 * is identified by 'void *' instead of 'int.
101 * Revision 1.15 1996/01/08 19:15:47 adam
102 * New input filter that works!
104 * Revision 1.14 1996/01/08 09:10:38 adam
105 * Yet another complete rework on this module.
107 * Revision 1.13 1995/12/15 17:21:50 adam
108 * This version is able to set data.formatted_text in data1-nodes.
110 * Revision 1.12 1995/12/15 16:20:10 adam
111 * The filter files (*.flt) are read from the path given by data1_tabpath.
113 * Revision 1.11 1995/12/15 12:35:16 adam
116 * Revision 1.10 1995/12/15 10:35:36 adam
119 * Revision 1.9 1995/12/14 16:38:48 adam
120 * Completely new attempt to make regular expression parsing.
122 * Revision 1.8 1995/12/13 17:16:59 adam
125 * Revision 1.7 1995/12/13 16:51:58 adam
126 * Modified to set last_child in data1_nodes.
127 * Uses destroy handler to free up data text nodes.
129 * Revision 1.6 1995/12/13 13:45:37 quinn
130 * Changed data1 to use nmem.
132 * Revision 1.5 1995/12/11 09:12:52 adam
133 * The rec_get function returns NULL if record doesn't exist - will
134 * happen in the server if the result set records have been deleted since
135 * the creation of the set (i.e. the search).
136 * The server saves a result temporarily if it is 'volatile', i.e. the
137 * set is register dependent.
139 * Revision 1.4 1995/12/05 16:57:40 adam
140 * More work on regular patterns.
142 * Revision 1.3 1995/12/05 09:37:09 adam
143 * One malloc was renamed to xmalloc.
145 * Revision 1.2 1995/12/04 17:59:24 adam
146 * More work on regular expression conversion.
148 * Revision 1.1 1995/12/04 14:25:30 adam
149 * Started work on regular expression parsed input to structured records.
158 #include <zebrautl.h>
164 #define F_WIN_EOF 2000000000
168 #define REGX_PATTERN 1
173 #define REGX_CONTEXT 6
179 struct lexRuleAction {
183 struct DFA *dfa; /* REGX_PATTERN */
186 struct regxCode *code; /* REGX_CODE */
188 struct lexRuleAction *next;
193 struct lexRuleAction *actionList;
197 struct lexRuleInfo info;
198 struct lexRule *next;
204 struct lexRule *rules;
205 struct lexRuleInfo **fastRule;
208 struct lexRuleAction *beginActionList;
209 struct lexRuleAction *endActionList;
210 struct lexContext *next;
213 struct lexConcatBuf {
222 struct lexContext *context;
224 struct lexContext **context_stack;
225 int context_stack_size;
226 int context_stack_top;
232 void (*f_win_ef)(void *, off_t);
234 int f_win_start; /* first byte of buffer is this file offset */
235 int f_win_end; /* last byte of buffer is this offset - 1 */
236 int f_win_size; /* size of buffer */
237 char *f_win_buf; /* buffer itself */
238 int (*f_win_rf)(void *, char *, size_t);
239 off_t (*f_win_sf)(void *, off_t);
241 struct lexConcatBuf **concatBuf;
246 static char *f_win_get (struct lexSpec *spec, off_t start_pos, off_t end_pos,
249 int i, r, off = start_pos - spec->f_win_start;
251 if (off >= 0 && end_pos <= spec->f_win_end)
253 *size = end_pos - start_pos;
254 return spec->f_win_buf + off;
256 if (off < 0 || start_pos >= spec->f_win_end)
258 (*spec->f_win_sf)(spec->f_win_fh, start_pos);
259 spec->f_win_start = start_pos;
261 if (!spec->f_win_buf)
262 spec->f_win_buf = xmalloc (spec->f_win_size);
263 *size = (*spec->f_win_rf)(spec->f_win_fh, spec->f_win_buf,
265 spec->f_win_end = spec->f_win_start + *size;
267 if (*size > end_pos - start_pos)
268 *size = end_pos - start_pos;
269 return spec->f_win_buf;
271 for (i = 0; i<spec->f_win_end - start_pos; i++)
272 spec->f_win_buf[i] = spec->f_win_buf[i + off];
273 r = (*spec->f_win_rf)(spec->f_win_fh,
275 spec->f_win_size - i);
276 spec->f_win_start = start_pos;
277 spec->f_win_end += r;
279 if (*size > end_pos - start_pos)
280 *size = end_pos - start_pos;
281 return spec->f_win_buf;
284 static int f_win_advance (struct lexSpec *spec, int *pos)
289 if (*pos >= spec->f_win_start && *pos < spec->f_win_end)
290 return spec->f_win_buf[(*pos)++ - spec->f_win_start];
291 if (*pos == F_WIN_EOF)
293 buf = f_win_get (spec, *pos, *pos+1, &size);
303 static void regxCodeDel (struct regxCode **pp)
305 struct regxCode *p = *pp;
314 static void regxCodeMk (struct regxCode **pp, const char *buf, int len)
318 p = xmalloc (sizeof(*p));
319 p->str = xmalloc (len+1);
320 memcpy (p->str, buf, len);
325 static struct DFA *lexSpecDFA (void)
330 dfa_parse_cmap_del (dfa, ' ');
331 dfa_parse_cmap_del (dfa, '\t');
332 dfa_parse_cmap_add (dfa, '/', 0);
336 static void actionListDel (struct lexRuleAction **rap)
338 struct lexRuleAction *ra1, *ra;
340 for (ra = *rap; ra; ra = ra1)
346 dfa_delete (&ra->u.pattern.dfa);
349 regxCodeDel (&ra->u.code);
357 static struct lexContext *lexContextCreate (const char *name)
359 struct lexContext *p = xmalloc (sizeof(*p));
361 p->name = xstrdup (name);
363 p->dfa = lexSpecDFA ();
366 p->beginActionList = NULL;
367 p->endActionList = NULL;
372 static void lexContextDestroy (struct lexContext *p)
374 struct lexRule *rp, *rp1;
377 for (rp = p->rules; rp; rp = rp1)
380 actionListDel (&rp->info.actionList);
383 actionListDel (&p->beginActionList);
384 actionListDel (&p->endActionList);
389 static struct lexSpec *lexSpecCreate (const char *name)
394 p = xmalloc (sizeof(*p));
395 p->name = xmalloc (strlen(name)+1);
396 strcpy (p->name, name);
399 p->context_stack_size = 100;
400 p->context_stack = xmalloc (sizeof(*p->context_stack) *
401 p->context_stack_size);
405 p->concatBuf = xmalloc (sizeof(*p->concatBuf) * p->maxLevel);
406 for (i = 0; i < p->maxLevel; i++)
408 p->concatBuf[i] = xmalloc (sizeof(**p->concatBuf));
409 p->concatBuf[i]->len = p->concatBuf[i]->max = 0;
410 p->concatBuf[i]->buf = 0;
415 static void lexSpecDestroy (struct lexSpec **pp)
418 struct lexContext *lt;
426 for (i = 0; i < p->maxLevel; i++)
427 xfree (p->concatBuf[i]);
428 xfree (p->concatBuf);
433 struct lexContext *lt_next = lt->next;
434 lexContextDestroy (lt);
438 xfree (p->f_win_buf);
439 xfree (p->context_stack);
444 static int readParseToken (const char **cpp, int *len)
446 const char *cp = *cpp;
450 while (*cp == ' ' || *cp == '\t' || *cp == '\n')
479 if (*cp >= 'a' && *cp <= 'z')
481 else if (*cp >= 'A' && *cp <= 'Z')
482 cmd[i] = *cp + 'a' - 'A';
485 if (i < sizeof(cmd)-2)
492 logf (LOG_WARN, "bad character %d %c", *cp, *cp);
494 while (*cp && *cp != ' ' && *cp != '\t' && *cp != '\n')
500 if (!strcmp (cmd, "begin"))
502 else if (!strcmp (cmd, "end"))
504 else if (!strcmp (cmd, "body"))
506 else if (!strcmp (cmd, "context"))
510 logf (LOG_WARN, "bad command %s", cmd);
516 static int actionListMk (struct lexSpec *spec, const char *s,
517 struct lexRuleAction **ap)
523 while ((tok = readParseToken (&s, &len)))
531 *ap = xmalloc (sizeof(**ap));
533 regxCodeMk (&(*ap)->u.code, s, len);
537 *ap = xmalloc (sizeof(**ap));
539 (*ap)->u.pattern.body = bodyMark;
541 (*ap)->u.pattern.dfa = lexSpecDFA ();
543 r = dfa_parse ((*ap)->u.pattern.dfa, &s);
548 logf (LOG_WARN, "regular expression error '%.*s'", s-s0, s0);
551 dfa_mkstate ((*ap)->u.pattern.dfa);
555 logf (LOG_WARN, "cannot use begin here");
558 *ap = xmalloc (sizeof(**ap));
568 int readOneSpec (struct lexSpec *spec, const char *s)
572 struct lexContext *lc;
574 tok = readParseToken (&s, &len);
575 if (tok == REGX_CONTEXT)
577 char context_name[32];
578 tok = readParseToken (&s, &len);
579 if (tok != REGX_CODE)
581 logf (LOG_WARN, "missing name after CONTEXT keyword");
586 memcpy (context_name, s, len);
587 context_name[len] = '\0';
588 lc = lexContextCreate (context_name);
589 lc->next = spec->context;
594 spec->context = lexContextCreate ("main");
599 actionListDel (&spec->context->beginActionList);
600 actionListMk (spec, s, &spec->context->beginActionList);
603 actionListDel (&spec->context->endActionList);
604 actionListMk (spec, s, &spec->context->endActionList);
608 logf (LOG_DEBUG, "rule %d %s", spec->context->ruleNo, s);
610 r = dfa_parse (spec->context->dfa, &s);
613 logf (LOG_WARN, "regular expression error. r=%d", r);
618 logf (LOG_WARN, "expects / at end of pattern. got %c", *s);
622 rp = xmalloc (sizeof(*rp));
623 rp->info.no = spec->context->ruleNo++;
624 rp->next = spec->context->rules;
625 spec->context->rules = rp;
626 actionListMk (spec, s, &rp->info.actionList);
631 int readFileSpec (struct lexSpec *spec)
633 struct lexContext *lc;
636 int c, i, errors = 0;
639 lineBuf = xmalloc (1+lineSize);
640 logf (LOG_LOG, "reading regx filter %s.flt", spec->name);
641 sprintf (lineBuf, "%s.flt", spec->name);
642 if (!(spec_inf = yaz_path_fopen (data1_get_tabpath(spec->dh),
645 logf (LOG_ERRNO|LOG_WARN, "cannot read spec file %s", spec->name);
654 if (c == '#' || c == '\n' || c == ' ' || c == '\t')
656 while (c != '\n' && c != EOF)
675 if (c != ' ' && c != '\t')
684 readOneSpec (spec, lineBuf);
685 spec->lineNo += addLine;
694 debug_dfa_followpos = 1;
697 for (lc = spec->context; lc; lc = lc->next)
700 lc->fastRule = xmalloc (sizeof(*lc->fastRule) * lc->ruleNo);
701 for (i = 0; i < lc->ruleNo; i++)
702 lc->fastRule[i] = NULL;
703 for (rp = lc->rules; rp; rp = rp->next)
704 lc->fastRule[rp->info.no] = &rp->info;
705 dfa_mkstate (lc->dfa);
712 static struct lexSpec *curLexSpec = NULL;
714 static void execData (struct lexSpec *spec,
715 data1_node **d1_stack, int *d1_level,
716 const char *ebuf, int elen, int formatted_text)
718 struct data1_node *res, *parent;
721 if (elen == 0) /* shouldn't happen, but it does! */
725 logf (LOG_DEBUG, "data (%d bytes) %.15s ... %.*s", elen,
726 ebuf, 15, ebuf + elen-15);
728 logf (LOG_DEBUG, "data (%d bytes) %.*s", elen, elen, ebuf);
730 logf (LOG_DEBUG, "data (%d bytes)", elen);
736 parent = d1_stack[*d1_level -1];
739 if ((res = d1_stack[*d1_level]) && res->which == DATA1N_data)
740 org_len = res->u.data.len;
745 res = data1_mk_node (spec->dh, spec->m);
746 res->parent = parent;
747 res->which = DATA1N_data;
748 res->u.data.what = DATA1I_text;
750 res->u.data.formatted_text = formatted_text;
752 if (elen > DATA1_LOCALDATA)
753 res->u.data.data = nmem_malloc (spec->m, elen);
755 res->u.data.data = res->lbuf;
756 memcpy (res->u.data.data, ebuf, elen);
758 res->u.data.data = 0;
760 res->root = parent->root;
762 parent->last_child = res;
763 if (d1_stack[*d1_level])
764 d1_stack[*d1_level]->next = res;
767 d1_stack[*d1_level] = res;
769 if (org_len + elen >= spec->concatBuf[*d1_level]->max)
771 char *old_buf, *new_buf;
773 spec->concatBuf[*d1_level]->max = org_len + elen + 256;
774 new_buf = xmalloc (spec->concatBuf[*d1_level]->max);
775 if ((old_buf = spec->concatBuf[*d1_level]->buf))
777 memcpy (new_buf, old_buf, org_len);
780 spec->concatBuf[*d1_level]->buf = new_buf;
782 assert (spec->concatBuf[*d1_level]);
783 memcpy (spec->concatBuf[*d1_level]->buf + org_len, ebuf, elen);
784 res->u.data.len += elen;
787 static void execDataP (struct lexSpec *spec,
788 data1_node **d1_stack, int *d1_level,
789 const char *ebuf, int elen, int formatted_text)
791 execData (spec, d1_stack, d1_level, ebuf, elen, formatted_text);
794 static void tagDataRelease (struct lexSpec *spec,
795 data1_node **d1_stack, int d1_level)
799 assert (d1_stack[d1_level]);
800 if ((res = d1_stack[d1_level]) &&
801 res->which == DATA1N_data &&
802 res->u.data.what == DATA1I_text)
804 assert (!res->u.data.data);
805 assert (res->u.data.len > 0);
806 if (res->u.data.len > DATA1_LOCALDATA)
807 res->u.data.data = nmem_malloc (spec->m, res->u.data.len);
809 res->u.data.data = res->lbuf;
810 memcpy (res->u.data.data, spec->concatBuf[d1_level]->buf,
815 static void variantBegin (struct lexSpec *spec,
816 data1_node **d1_stack, int *d1_level,
817 const char *class_str, int class_len,
818 const char *type_str, int type_len,
819 const char *value_str, int value_len)
821 struct data1_node *parent = d1_stack[*d1_level -1];
822 char tclass[DATA1_MAX_SYMBOL], ttype[DATA1_MAX_SYMBOL];
829 logf (LOG_WARN, "in variant begin. No record type defined");
832 if (class_len >= DATA1_MAX_SYMBOL)
833 class_len = DATA1_MAX_SYMBOL-1;
834 memcpy (tclass, class_str, class_len);
835 tclass[class_len] = '\0';
837 if (type_len >= DATA1_MAX_SYMBOL)
838 type_len = DATA1_MAX_SYMBOL-1;
839 memcpy (ttype, type_str, type_len);
840 ttype[type_len] = '\0';
843 logf (LOG_DEBUG, "variant begin %s %s (%d)", tclass, ttype, *d1_level);
847 data1_getvartypebyct(spec->dh, parent->root->u.root.absyn->varset,
851 if (parent->which != DATA1N_variant)
853 res = data1_mk_node (spec->dh, spec->m);
854 res->parent = parent;
855 res->which = DATA1N_variant;
856 res->u.variant.type = 0;
857 res->u.variant.value = 0;
858 res->root = parent->root;
860 parent->last_child = res;
861 if (d1_stack[*d1_level])
862 d1_stack[*d1_level]->next = res;
865 d1_stack[*d1_level] = res;
866 d1_stack[++(*d1_level)] = NULL;
868 for (i = *d1_level-1; d1_stack[i]->which == DATA1N_variant; i--)
869 if (d1_stack[i]->u.variant.type == tp)
876 logf (LOG_DEBUG, "variant node (%d)", *d1_level);
878 parent = d1_stack[*d1_level-1];
879 res = data1_mk_node (spec->dh, spec->m);
880 res->parent = parent;
881 res->which = DATA1N_variant;
882 res->root = parent->root;
883 res->u.variant.type = tp;
885 if (value_len >= DATA1_LOCALDATA)
886 value_len =DATA1_LOCALDATA-1;
887 memcpy (res->lbuf, value_str, value_len);
888 res->lbuf[value_len] = '\0';
890 res->u.variant.value = res->lbuf;
892 parent->last_child = res;
893 if (d1_stack[*d1_level])
895 tagDataRelease (spec, d1_stack, *d1_level);
896 d1_stack[*d1_level]->next = res;
900 d1_stack[*d1_level] = res;
901 d1_stack[++(*d1_level)] = NULL;
904 static void tagStrip (const char **tag, int *len)
908 for (i = *len; i > 0 && isspace((*tag)[i-1]); --i)
911 for (i = 0; i < *len && isspace((*tag)[i]); i++)
917 static void tagBegin (struct lexSpec *spec,
918 data1_node **d1_stack, int *d1_level,
919 const char *tag, int len)
921 struct data1_node *parent = d1_stack[*d1_level -1];
922 data1_element *elem = NULL;
923 data1_node *partag = get_parent_tag(spec->dh, parent);
925 data1_element *e = NULL;
930 logf (LOG_WARN, "in element begin. No record type defined");
933 tagStrip (&tag, &len);
935 res = data1_mk_node (spec->dh, spec->m);
936 res->parent = parent;
937 res->which = DATA1N_tag;
938 res->u.tag.get_bytes = -1;
940 if (len >= DATA1_LOCALDATA)
941 res->u.tag.tag = nmem_malloc (spec->m, len+1);
943 res->u.tag.tag = res->lbuf;
945 memcpy (res->u.tag.tag, tag, len);
946 res->u.tag.tag[len] = '\0';
949 logf (LOG_DEBUG, "begin tag %s (%d)", res->u.tag.tag, *d1_level);
951 if (parent->which == DATA1N_variant)
954 if (!(e = partag->u.tag.element))
957 elem = data1_getelementbytagname (spec->dh, d1_stack[0]->u.root.absyn,
959 res->u.tag.element = elem;
960 res->u.tag.node_selected = 0;
961 res->u.tag.make_variantlist = 0;
962 res->u.tag.no_data_requested = 0;
963 res->root = parent->root;
965 parent->last_child = res;
966 if (d1_stack[*d1_level])
968 tagDataRelease (spec, d1_stack, *d1_level);
969 d1_stack[*d1_level]->next = res;
973 d1_stack[*d1_level] = res;
974 d1_stack[++(*d1_level)] = NULL;
977 static void tagEnd (struct lexSpec *spec,
978 data1_node **d1_stack, int *d1_level, int min_level,
979 const char *tag, int len)
981 tagStrip (&tag, &len);
982 while (*d1_level > min_level)
984 tagDataRelease (spec, d1_stack, *d1_level);
988 if ((d1_stack[*d1_level]->which == DATA1N_tag) &&
990 (strlen(d1_stack[*d1_level]->u.tag.tag) == (size_t) len &&
991 !memcmp (d1_stack[*d1_level]->u.tag.tag, tag, len))))
995 logf (LOG_DEBUG, "end tag (%d)", *d1_level);
1000 static int tryMatch (struct lexSpec *spec, int *pptr, int *mptr,
1003 struct DFA_state *state = dfa->states[0];
1006 unsigned char c_prev = 0;
1007 int ptr = *pptr; /* current pointer */
1008 int start_ptr = *pptr; /* first char of match */
1009 int last_ptr = 0; /* last char of match */
1010 int last_rule = 0; /* rule number of current match */
1015 c = f_win_advance (spec, &ptr);
1016 if (ptr == F_WIN_EOF)
1033 *mptr = start_ptr; /* match starts here */
1034 *pptr = last_ptr; /* match end here (+1) */
1037 state = dfa->states[0];
1042 else if (c >= t->ch[0] && c <= t->ch[1])
1044 state = dfa->states[t->to];
1049 last_rule = state->rule_no;
1054 last_rule = state->rule_nno;
1066 static int execTok (struct lexSpec *spec, const char **src,
1067 int arg_no, int *arg_start, int *arg_end,
1068 const char **tokBuf, int *tokLen)
1070 const char *s = *src;
1072 while (*s == ' ' || *s == '\t')
1076 if (*s == '$' && s[1] >= '0' && s[1] <= '9')
1080 while (*s >= '0' && *s <= '9')
1081 n = n*10 + (*s++ -'0');
1091 *tokBuf = f_win_get (spec, arg_start[n], arg_end[n], tokLen);
1094 else if (*s == '\"')
1097 while (*s && *s != '\"')
1099 *tokLen = s - *tokBuf;
1104 else if (*s == '\n' || *s == ';')
1112 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
1114 *tokLen = s - *tokBuf;
1121 while (*s && *s != ' ' && *s != '\t' && *s != '\n' && *s != ';')
1123 *tokLen = s - *tokBuf;
1129 static char *regxStrz (const char *src, int len, char *str)
1133 memcpy (str, src, len);
1138 static int execCode (struct lexSpec *spec,
1139 int arg_no, int *arg_start, int *arg_end, int *pptr,
1140 struct regxCode *code,
1141 data1_node **d1_stack, int *d1_level)
1143 const char *s = code->str;
1146 const char *cmd_str;
1148 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str, &cmd_len);
1155 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1156 &cmd_str, &cmd_len);
1159 p = regxStrz (cmd_str, cmd_len, ptmp);
1160 if (!strcmp (p, "begin"))
1162 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1163 &cmd_str, &cmd_len);
1166 logf (LOG_WARN, "missing keyword after 'begin'");
1169 p = regxStrz (cmd_str, cmd_len, ptmp);
1170 if (!strcmp (p, "record"))
1172 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1173 &cmd_str, &cmd_len);
1178 static char absynName[64];
1183 memcpy (absynName, cmd_str, cmd_len);
1184 absynName[cmd_len] = '\0';
1187 logf (LOG_DEBUG, "begin record %s", absynName);
1189 if (!(absyn = data1_get_absyn (spec->dh, absynName)))
1190 logf (LOG_WARN, "Unknown tagset: %s", absynName);
1195 res = data1_mk_node (spec->dh, spec->m);
1196 res->which = DATA1N_root;
1197 res->u.root.type = absynName;
1198 res->u.root.absyn = absyn;
1201 d1_stack[*d1_level] = res;
1202 d1_stack[++(*d1_level)] = NULL;
1205 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1206 &cmd_str, &cmd_len);
1208 else if (!strcmp (p, "element"))
1210 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1211 &cmd_str, &cmd_len);
1214 tagBegin (spec, d1_stack, d1_level, cmd_str, cmd_len);
1215 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1216 &cmd_str, &cmd_len);
1218 else if (!strcmp (p, "variant"))
1221 const char *class_str = NULL;
1223 const char *type_str = NULL;
1225 const char *value_str = NULL;
1226 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1227 &cmd_str, &cmd_len);
1230 class_str = cmd_str;
1231 class_len = cmd_len;
1232 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1233 &cmd_str, &cmd_len);
1239 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1240 &cmd_str, &cmd_len);
1243 value_str = cmd_str;
1244 value_len = cmd_len;
1246 variantBegin (spec, d1_stack, d1_level, class_str, class_len,
1247 type_str, type_len, value_str, value_len);
1250 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1251 &cmd_str, &cmd_len);
1253 else if (!strcmp (p, "context"))
1257 struct lexContext *lc = spec->context;
1258 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1259 &cmd_str, &cmd_len);
1260 p = regxStrz (cmd_str, cmd_len, ptmp);
1262 logf (LOG_DEBUG, "begin context %s", p);
1264 while (lc && strcmp (p, lc->name))
1267 spec->context_stack[++(spec->context_stack_top)] = lc;
1269 logf (LOG_WARN, "unknown context %s", p);
1272 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1273 &cmd_str, &cmd_len);
1277 logf (LOG_WARN, "bad keyword '%s' after begin", p);
1280 else if (!strcmp (p, "end"))
1282 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1283 &cmd_str, &cmd_len);
1286 logf (LOG_WARN, "missing keyword after 'end'");
1289 p = regxStrz (cmd_str, cmd_len, ptmp);
1290 if (!strcmp (p, "record"))
1293 for (i = *d1_level; i; --i)
1294 tagDataRelease (spec, d1_stack, i);
1296 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1297 &cmd_str, &cmd_len);
1299 logf (LOG_DEBUG, "end record");
1303 else if (!strcmp (p, "element"))
1306 while ((r = execTok (spec, &s, arg_no, arg_start, arg_end,
1307 &cmd_str, &cmd_len)) == 3)
1309 if (cmd_len==7 && !memcmp ("-record", cmd_str, cmd_len))
1314 tagEnd (spec, d1_stack, d1_level, min_level,
1316 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1317 &cmd_str, &cmd_len);
1320 tagEnd (spec, d1_stack, d1_level, min_level, NULL, 0);
1324 logf (LOG_DEBUG, "end element end records");
1330 else if (!strcmp (p, "context"))
1333 logf (LOG_DEBUG, "end context");
1335 if (spec->context_stack_top)
1336 (spec->context_stack_top)--;
1337 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1338 &cmd_str, &cmd_len);
1341 logf (LOG_WARN, "bad keyword '%s' after end", p);
1343 else if (!strcmp (p, "data"))
1347 const char *element_str = NULL;
1349 while ((r = execTok (spec, &s, arg_no, arg_start, arg_end,
1350 &cmd_str, &cmd_len)) == 3)
1352 if (cmd_len==5 && !memcmp ("-text", cmd_str, cmd_len))
1354 else if (cmd_len==8 && !memcmp ("-element", cmd_str, cmd_len))
1356 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1357 &element_str, &element_len);
1362 logf (LOG_WARN, "bad data option: %.*s",
1367 logf (LOG_WARN, "missing data item after data");
1371 tagBegin (spec, d1_stack, d1_level, element_str, element_len);
1374 execData (spec, d1_stack, d1_level, cmd_str, cmd_len,
1376 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1377 &cmd_str, &cmd_len);
1380 tagEnd (spec, d1_stack, d1_level, 1, NULL, 0);
1382 else if (!strcmp (p, "unread"))
1385 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1386 &cmd_str, &cmd_len);
1387 if (r==3 && cmd_len == 7 && !memcmp ("-offset", cmd_str, cmd_len))
1389 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1390 &cmd_str, &cmd_len);
1393 logf (LOG_WARN, "missing number after -offset");
1396 p = regxStrz (cmd_str, cmd_len, ptmp);
1398 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1399 &cmd_str, &cmd_len);
1405 logf (LOG_WARN, "missing index after unread command");
1408 if (cmd_len != 1 || *cmd_str < '0' || *cmd_str > '9')
1410 logf (LOG_WARN, "bad index after unread command");
1415 no = *cmd_str - '0';
1418 *pptr = arg_start[no] + offset;
1420 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1421 &cmd_str, &cmd_len);
1423 else if (!strcmp (p, "context"))
1427 struct lexContext *lc = spec->context;
1428 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1429 &cmd_str, &cmd_len);
1430 p = regxStrz (cmd_str, cmd_len, ptmp);
1432 while (lc && strcmp (p, lc->name))
1435 spec->context_stack[spec->context_stack_top] = lc;
1437 logf (LOG_WARN, "unknown context %s", p);
1440 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1441 &cmd_str, &cmd_len);
1445 logf (LOG_WARN, "unknown code command '%.*s'", cmd_len, cmd_str);
1446 r = execTok (spec, &s, arg_no, arg_start, arg_end,
1447 &cmd_str, &cmd_len);
1452 logf (LOG_WARN, "ignoring token %.*s", cmd_len, cmd_str);
1454 r = execTok (spec, &s, arg_no, arg_start, arg_end, &cmd_str,
1464 * execAction: Execute action specified by 'ap'. Returns 0 if
1465 * the pattern(s) associated by rule and code could be executed
1466 * ok; returns 1 if code couldn't be executed.
1468 static int execAction (struct lexSpec *spec, struct lexRuleAction *ap,
1469 data1_node **d1_stack, int *d1_level,
1470 int start_ptr, int *pptr)
1477 arg_start[0] = start_ptr;
1485 if (ap->u.pattern.body)
1487 arg_start[arg_no] = *pptr;
1488 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1490 arg_end[arg_no] = F_WIN_EOF;
1492 arg_start[arg_no] = F_WIN_EOF;
1493 arg_end[arg_no] = F_WIN_EOF;
1498 arg_end[arg_no] = sptr;
1500 arg_start[arg_no] = sptr;
1501 arg_end[arg_no] = *pptr;
1506 arg_start[arg_no] = *pptr;
1507 if (!tryMatch (spec, pptr, &sptr, ap->u.pattern.dfa))
1509 if (sptr != arg_start[arg_no])
1511 arg_end[arg_no] = *pptr;
1516 if (!execCode (spec, arg_no, arg_start, arg_end, pptr,
1517 ap->u.code, d1_stack, d1_level))
1521 arg_start[arg_no] = *pptr;
1522 arg_end[arg_no] = F_WIN_EOF;
1531 static int execRule (struct lexSpec *spec, struct lexContext *context,
1532 data1_node **d1_stack, int *d1_level,
1533 int ruleNo, int start_ptr, int *pptr)
1536 logf (LOG_DEBUG, "exec rule %d", ruleNo);
1538 return execAction (spec, context->fastRule[ruleNo]->actionList,
1539 d1_stack, d1_level, start_ptr, pptr);
1542 data1_node *lexNode (struct lexSpec *spec,
1543 data1_node **d1_stack, int *d1_level, int *ptr)
1545 struct lexContext *context = spec->context_stack[spec->context_stack_top];
1546 struct DFA_state *state = context->dfa->states[0];
1549 unsigned char c_prev = '\n';
1551 int last_rule = 0; /* rule number of current match */
1552 int last_ptr = *ptr; /* last char of match */
1553 int start_ptr = *ptr; /* first char of match */
1554 int skip_ptr = *ptr; /* first char of run */
1558 c = f_win_advance (spec, ptr);
1559 if (*ptr == F_WIN_EOF)
1561 /* end of file met */
1564 /* there was a match */
1565 if (skip_ptr < start_ptr)
1567 /* deal with chars that didn't match */
1570 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1571 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1573 /* restore pointer */
1576 if (!execRule (spec, context, d1_stack, d1_level,
1577 last_rule, start_ptr, ptr))
1579 /* restore skip pointer */
1583 else if (skip_ptr < *ptr)
1585 /* deal with chars that didn't match */
1588 buf = f_win_get (spec, skip_ptr, *ptr, &size);
1589 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1591 if (*ptr == F_WIN_EOF)
1598 { /* no transition for character c ... */
1601 if (skip_ptr < start_ptr)
1603 /* deal with chars that didn't match */
1606 buf = f_win_get (spec, skip_ptr, start_ptr, &size);
1607 execDataP (spec, d1_stack, d1_level, buf, size, 0);
1609 /* restore pointer */
1611 if (!execRule (spec, context, d1_stack, d1_level,
1612 last_rule, start_ptr, ptr))
1614 if (spec->f_win_ef && *ptr != F_WIN_EOF)
1617 logf (LOG_DEBUG, "regx: endf ptr=%d", *ptr);
1619 (*spec->f_win_ef)(spec->f_win_fh, *ptr);
1623 context = spec->context_stack[spec->context_stack_top];
1626 last_ptr = start_ptr = *ptr;
1630 c_prev = f_win_advance (spec, &start_ptr);
1635 c_prev = f_win_advance (spec, &start_ptr);
1638 state = context->dfa->states[0];
1641 else if (c >= t->ch[0] && c <= t->ch[1])
1642 { /* transition ... */
1643 state = context->dfa->states[t->to];
1648 last_rule = state->rule_no;
1651 else if (state->rule_nno)
1653 last_rule = state->rule_nno;
1665 static data1_node *lexRoot (struct lexSpec *spec, off_t offset,
1666 const char *context_name)
1668 struct lexContext *lt = spec->context;
1669 data1_node *d1_stack[128];
1673 spec->context_stack_top = 0;
1676 if (!strcmp (lt->name, context_name))
1682 logf (LOG_WARN, "cannot find context %s", context_name);
1685 spec->context_stack[spec->context_stack_top] = lt;
1686 d1_stack[d1_level] = NULL;
1687 if (lt->beginActionList)
1688 execAction (spec, lt->beginActionList, d1_stack, &d1_level, 0, &ptr);
1689 lexNode (spec, d1_stack, &d1_level, &ptr);
1690 if (lt->endActionList)
1691 execAction (spec, lt->endActionList, d1_stack, &d1_level, ptr, &ptr);
1695 data1_node *grs_read_regx (struct grs_read_info *p)
1700 logf (LOG_DEBUG, "grs_read_regx");
1702 if (!curLexSpec || strcmp (curLexSpec->name, p->type))
1705 lexSpecDestroy (&curLexSpec);
1706 curLexSpec = lexSpecCreate (p->type);
1707 curLexSpec->dh = p->dh;
1708 res = readFileSpec (curLexSpec);
1711 lexSpecDestroy (&curLexSpec);
1715 curLexSpec->dh = p->dh;
1718 curLexSpec->f_win_start = 0;
1719 curLexSpec->f_win_end = 0;
1720 curLexSpec->f_win_rf = p->readf;
1721 curLexSpec->f_win_sf = p->seekf;
1722 curLexSpec->f_win_fh = p->fh;
1723 curLexSpec->f_win_ef = p->endf;
1724 curLexSpec->f_win_size = 500000;
1726 curLexSpec->m = p->mem;
1727 return lexRoot (curLexSpec, p->offset, "main");