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