2 * FML interpreter. Europagate, 1995
5 * Revision 1.4 1995/02/27 09:01:21 adam
6 * Regular expression support. Argument passing by name option. New FML
9 * Revision 1.3 1995/02/23 08:32:06 adam
12 * Revision 1.1 1995/02/10 18:15:53 adam
13 * FML function 'strcmp' implemented. This function can be used to
14 * test for existence of MARC fields.
30 struct re_pattern_buffer buf;
32 struct reg_cache *next;
35 static int no_in_use = 0;
36 static struct reg_cache *reg_cache_list = NULL;
38 struct reg_cache *fml_reg_compile (const char *pattern)
40 struct reg_cache *list, *last = NULL;
41 for (list = reg_cache_list; list; list = list->next)
43 if (!strcmp (pattern, list->pattern))
49 for (list = reg_cache_list; list->next->next; list = list->next)
51 free (list->next->pattern);
52 regfree (&list->next->buf);
58 list = malloc (sizeof (*list));
60 list->next = reg_cache_list;
61 reg_cache_list = list;
62 list->pattern = malloc (strlen(pattern)+1);
63 assert (list->pattern);
64 strcpy (list->pattern, pattern);
66 re_syntax_options = RE_SYNTAX_GREP;
67 list->buf.translate = NULL;
68 list->buf.fastmap = NULL;
69 list->buf.buffer = NULL;
70 list->buf.allocated = 0;
71 re_compile_pattern (pattern, strlen(pattern), &list->buf);
75 static int fml_reg_match (struct reg_cache *reg_pat, const char *str)
77 int ret, len = strlen (str);
79 ret = re_match (®_pat->buf, str, len, 0, NULL);
87 static struct fml_node *fml_exec_match (Fml fml, struct fml_node **lp,
90 struct reg_cache *reg;
100 fml_cmd_lex (lp, tp);
104 fn = fml_expr_term (fml, lp, tp);
107 fml_node_delete (fml, fn);
110 fml_atom_str (fn->p[0], pattern);
111 fml_node_delete (fml, fn);
114 reg = fml_reg_compile (cp);
115 fn = fml_expr_term (fml, lp, tp);
118 fml_node_delete (fml, fn);
121 fml_atom_str (fn->p[0], sstring);
122 fml_node_delete (fml, fn);
123 if (fml_reg_match (reg, sstring))
124 return fml_mk_node_val (fml, 1);
128 static struct fml_node *fml_exec_strlen (Fml fml, struct fml_node **lp,
134 fml_cmd_lex (lp, tp);
135 fn = fml_expr_term (fml, lp, tp);
139 len += fml_atom_len (fn->p[0]);
144 fml_node_delete (fml, fn);
145 return fml_mk_node_val (fml, len);
148 static struct fml_node *fml_exec_strcmp (Fml fml, struct fml_node **lp,
152 struct fml_node *fn = NULL, *fn1, *fn2;
155 fml_cmd_lex (lp, tp);
157 fn1 = fml_expr_term (fml, lp, tp);
158 fn2 = fml_expr_term (fml, lp, tp);
159 if (!fn1->is_atom && !fn2->is_atom)
161 n = fml_atom_cmp (fml, fn1->p[0], fn2->p[0]);
168 fml_node_delete (fml, fn1);
169 fml_node_delete (fml, fn2);
170 fn = fml_node_alloc (fml);
172 fn->p[0] = fml_atom_alloc (fml, arg);
176 void fml_str_init (Fml fml)
178 struct fml_sym_info *sym_info;
180 sym_info = fml_sym_add (fml->sym_tab, "strcmp");
181 sym_info->kind = FML_CPREFIX;
182 sym_info->prefix = fml_exec_strcmp;
183 sym_info = fml_sym_add (fml->sym_tab, "strlen");
184 sym_info->kind = FML_CPREFIX;
185 sym_info->prefix = fml_exec_strlen;
187 sym_info = fml_sym_add (fml->sym_tab, "match");
188 sym_info->kind = FML_CPREFIX;
189 sym_info->prefix = fml_exec_match;