Minor changes.
[egate.git] / fml / fmltest.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  */
44 /*
45  * FML interpreter. Europagate, 1995
46  *
47  * $Log: fmltest.c,v $
48  * Revision 1.9  1995/05/16 09:39:35  adam
49  * LICENSE.
50  *
51  * Revision 1.8  1995/03/02  08:06:10  adam
52  * Fml function strsub implemented. New test files marc[45].fml.
53  * New test options in fmltest.
54  *
55  * Revision 1.7  1995/02/23  08:32:06  adam
56  * Changed header.
57  *
58  * Revision 1.5  1995/02/10  15:50:56  adam
59  * MARC interface implemented. Minor bugs fixed. fmltest can
60  * be used to format single MARC records. New function '\list'
61  * implemented.
62  *
63  * Revision 1.4  1995/02/09  16:06:08  adam
64  * FML can be called from the outside multiple times by the functions:
65  * fml_exec_call and fml_exec_call_str.
66  * An interactive parameter (-i) to fmltest starts a shell-like
67  * interface to FML by using the fml_exec_call_str function.
68  *
69  * Revision 1.3  1995/02/09  13:07:15  adam
70  * Nodes are freed now. Many bugs fixed.
71  *
72  * Revision 1.2  1995/02/07  16:09:24  adam
73  * The \ character is no longer INCLUDED when terminating a token.
74  * Major changes in tokenization routines. Bug fixes in expressions
75  * with lists (fml_sub0).
76  *
77  * Revision 1.1.1.1  1995/02/06  13:48:10  adam
78  * First version of the FML interpreter. It's slow and memory isn't
79  * freed properly. In particular, the FML nodes aren't released yet.
80  *
81  */
82
83 #include <stdio.h>
84 #include <string.h>
85 #include <stdlib.h>
86
87 #include <fmlmarc.h>
88
89 char *prog;
90 static FILE *inf;
91
92 static int inf_read (void)
93 {
94     return getc (inf);
95 }
96
97 int main (int argc, char **argv)
98 {
99     Fml fml;
100     int nfiles = 0;
101     int marc_show = 0;
102     int interactive = 0;
103     const char *iso2709_fname = NULL;
104     const char *format_func = NULL;
105     int number_of_records = 10000;
106
107     prog = *argv;
108     fml = fml_open ();
109     while (-- argc > 0)
110     {
111         ++argv;
112         if (**argv == '-')
113         {
114             if (argv[0][1] == 'd')
115                 fml->debug |= 1;
116             else if (argv[0][1] == 'm')
117                 fml->debug |= 2;
118             else if (argv[0][1] == 'i')
119                 interactive = 1;
120             else if (argv[0][1] == 's')
121                 marc_show = 1;
122             else if (argv[0][1] == 'n')
123             {
124                 if (argc > 1)
125                 {
126                     number_of_records = atoi (*++argv);
127                     --argc;
128                 }
129             }
130             else if (argv[0][1] == '2')
131             {
132                 if (argc > 2)
133                 {
134                     iso2709_fname = *++argv;
135                     --argc;
136                     format_func = *++argv;
137                     --argc;
138                 }
139                 else
140                 {
141                     fprintf (stderr, "missing marcfile and format\n");
142                     exit (1);
143                 }
144             }
145             else
146             {
147                 fprintf (stderr, "unknown option `%s'\n", *argv);
148                 exit (1);
149             }
150         }
151         else
152         {
153             nfiles++;
154             inf = fopen (*argv, "r");
155             if (!inf)
156             {
157                 fprintf (stderr, "cannot open FML file `%s'\n", *argv);
158                 exit (1);
159             }
160             fml->read_func = inf_read;
161             fml_preprocess (fml);
162             fml_exec (fml);
163             fclose (inf);
164         }
165     }
166     if (!nfiles)
167     {
168         fml_preprocess (fml);
169         fml_exec (fml);
170     }
171     else if (iso2709_fname)
172     {
173         FILE *inf;
174         char *buf;
175         const char *nargv[5];
176         Iso2709Rec rec;
177         int no = 0;
178
179         inf = fopen (iso2709_fname, "r");
180         if (!inf)
181         {
182             fprintf (stderr, "cannot open %s\n", iso2709_fname);
183             exit (1);
184         }
185         while (no < number_of_records && (buf = iso2709_read (inf)))
186         {
187             rec = iso2709_cvt (buf);
188             free (buf);
189             nargv[0] = "\\";
190             nargv[1] = format_func;
191             nargv[2] = " \\list";
192             nargv[4] = NULL;
193             nargv[3]= marc_to_str (fml, rec);
194             if (marc_show)
195                 printf ("\n[%s]\n", nargv[3]);
196             iso2709_rm (rec);
197             fml_exec_call_argv (fml, nargv);
198             no++;
199         }
200         fclose (inf);
201     }
202     else
203     {
204         if (interactive)
205         {
206             char arg[128];
207
208             while (1)
209             {
210                 char *cp;
211                 const char *nargv[4];
212
213                 printf ("\nFML>");
214                 fflush (stdout);
215
216                 if (!fgets (arg, 127, stdin))
217                     break;
218                 if ((cp = strchr (arg, '\n')))
219                     *cp = '\0';
220                 nargv[0] = arg;
221                 nargv[1] = NULL;
222                 fml_exec_call_argv (fml, nargv);
223             }
224         }
225     }
226     return 0;
227 }