7664473769c057c1b883c9413b6b9e15f33b2df1
[egate.git] / fml / fmltest.c
1 /*
2  * FML interpreter. Europagate, 1995
3  *
4  * $Log: fmltest.c,v $
5  * Revision 1.8  1995/03/02 08:06:10  adam
6  * Fml function strsub implemented. New test files marc[45].fml.
7  * New test options in fmltest.
8  *
9  * Revision 1.7  1995/02/23  08:32:06  adam
10  * Changed header.
11  *
12  * Revision 1.5  1995/02/10  15:50:56  adam
13  * MARC interface implemented. Minor bugs fixed. fmltest can
14  * be used to format single MARC records. New function '\list'
15  * implemented.
16  *
17  * Revision 1.4  1995/02/09  16:06:08  adam
18  * FML can be called from the outside multiple times by the functions:
19  * fml_exec_call and fml_exec_call_str.
20  * An interactive parameter (-i) to fmltest starts a shell-like
21  * interface to FML by using the fml_exec_call_str function.
22  *
23  * Revision 1.3  1995/02/09  13:07:15  adam
24  * Nodes are freed now. Many bugs fixed.
25  *
26  * Revision 1.2  1995/02/07  16:09:24  adam
27  * The \ character is no longer INCLUDED when terminating a token.
28  * Major changes in tokenization routines. Bug fixes in expressions
29  * with lists (fml_sub0).
30  *
31  * Revision 1.1.1.1  1995/02/06  13:48:10  adam
32  * First version of the FML interpreter. It's slow and memory isn't
33  * freed properly. In particular, the FML nodes aren't released yet.
34  *
35  */
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40
41 #include <fmlmarc.h>
42
43 char *prog;
44 static FILE *inf;
45
46 static int inf_read (void)
47 {
48     return getc (inf);
49 }
50
51 int main (int argc, char **argv)
52 {
53     Fml fml;
54     int nfiles = 0;
55     int marc_show = 0;
56     int interactive = 0;
57     const char *iso2709_fname = NULL;
58     const char *format_func = NULL;
59     int number_of_records = 10000;
60
61     prog = *argv;
62     fml = fml_open ();
63     while (-- argc > 0)
64     {
65         ++argv;
66         if (**argv == '-')
67         {
68             if (argv[0][1] == 'd')
69                 fml->debug |= 1;
70             else if (argv[0][1] == 'm')
71                 fml->debug |= 2;
72             else if (argv[0][1] == 'i')
73                 interactive = 1;
74             else if (argv[0][1] == 's')
75                 marc_show = 1;
76             else if (argv[0][1] == 'n')
77             {
78                 if (argc > 1)
79                 {
80                     number_of_records = atoi (*++argv);
81                     --argc;
82                 }
83             }
84             else if (argv[0][1] == '2')
85             {
86                 if (argc > 2)
87                 {
88                     iso2709_fname = *++argv;
89                     --argc;
90                     format_func = *++argv;
91                     --argc;
92                 }
93                 else
94                 {
95                     fprintf (stderr, "missing marcfile and format\n");
96                     exit (1);
97                 }
98             }
99             else
100             {
101                 fprintf (stderr, "unknown option `%s'\n", *argv);
102                 exit (1);
103             }
104         }
105         else
106         {
107             nfiles++;
108             inf = fopen (*argv, "r");
109             if (!inf)
110             {
111                 fprintf (stderr, "cannot open FML file `%s'\n", *argv);
112                 exit (1);
113             }
114             fml->read_func = inf_read;
115             fml_preprocess (fml);
116             fml_exec (fml);
117             fclose (inf);
118         }
119     }
120     if (!nfiles)
121     {
122         fml_preprocess (fml);
123         fml_exec (fml);
124     }
125     else if (iso2709_fname)
126     {
127         FILE *inf;
128         char *buf;
129         const char *nargv[5];
130         Iso2709Rec rec;
131         int no = 0;
132
133         inf = fopen (iso2709_fname, "r");
134         if (!inf)
135         {
136             fprintf (stderr, "cannot open %s\n", iso2709_fname);
137             exit (1);
138         }
139         while (no < number_of_records && (buf = iso2709_read (inf)))
140         {
141             rec = iso2709_cvt (buf);
142             free (buf);
143             nargv[0] = "\\";
144             nargv[1] = format_func;
145             nargv[2] = " \\list";
146             nargv[4] = NULL;
147             nargv[3]= marc_to_str (fml, rec);
148             if (marc_show)
149                 printf ("\n[%s]\n", nargv[3]);
150             iso2709_rm (rec);
151             fml_exec_call_argv (fml, nargv);
152             no++;
153         }
154         fclose (inf);
155     }
156     else
157     {
158         if (interactive)
159         {
160             char arg[128];
161
162             while (1)
163             {
164                 char *cp;
165                 const char *nargv[4];
166
167                 printf ("\nFML>");
168                 fflush (stdout);
169
170                 if (!fgets (arg, 127, stdin))
171                     break;
172                 if ((cp = strchr (arg, '\n')))
173                     *cp = '\0';
174                 nargv[0] = arg;
175                 nargv[1] = NULL;
176                 fml_exec_call_argv (fml, nargv);
177             }
178         }
179     }
180     return 0;
181 }