Run latex
[egate.git] / util / iso2709dump.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  * Iso2709 record management
46  *
47  * Europagate, 1994-1995.
48  *
49  * $Log: iso2709dump.c,v $
50  * Revision 1.11  1995/05/16 09:40:54  adam
51  * LICENSE.
52  *
53  * Revision 1.10  1995/03/31  09:43:24  adam
54  * Removed test display.
55  *
56  * Revision 1.9  1995/03/30  14:22:18  adam
57  * More work on new MARC anchor functions.
58  *
59  * Revision 1.8  1995/03/30  07:33:37  adam
60  * New 2709 function: iso2709_mk.
61  * First implementation of iso2709_a_insert.
62  *
63  * Revision 1.7  1995/03/28  16:07:07  adam
64  * New function: iso2709_out. This function is the reverse of iso2709_cvt.
65  *
66  * Revision 1.6  1995/03/27  12:52:18  adam
67  * A little more verbose in marc dump.
68  *
69  * Revision 1.5  1995/02/22  21:32:36  adam
70  * Changed header.
71  *
72  * Revision 1.3  1995/02/10  17:05:18  adam
73  * New function iso2709_display to display MARC records in a
74  * line-by-line format. The iso2709_cvt function no longer
75  * prints the record to stderr.
76  *
77  * Revision 1.2  1995/02/10  16:50:33  adam
78  * Indicator field moved to 'struct iso2709_dir' from 'struct
79  * iso2709_field'.
80  * Function iso2709_rm implemented - to delete a MARC record.
81  *
82  * Revision 1.1.1.1  1995/02/09  17:27:11  adam
83  * Initial version of email gateway under CVS control.
84  *
85  */
86
87 #include <stdio.h>
88 #include <stdlib.h>
89 #include <errno.h>
90
91 #include <iso2709.h>
92
93 static char *prog;
94
95 static Iso2709Rec copy_rec (Iso2709Rec rec_in)
96 {
97     Iso2709Rec rec_out;
98     Iso2709Anchor a_in, a_out;
99     char *tag, *indicator, *identifier, *data;
100
101     rec_out = iso2709_mk();
102
103     a_in = iso2709_a_mk (rec_in);
104     a_out = iso2709_a_mk (rec_out);
105
106     do {
107         if (!iso2709_a_info_field (a_in, &tag, &indicator, &identifier, &data))
108             break;
109         iso2709_a_insert (a_out, tag, indicator, identifier, data);
110     } while (iso2709_a_next (a_in));
111     iso2709_a_rm (a_in);
112     iso2709_a_rm (a_out);
113     return rec_out;
114 }
115
116 int main (int argc, char **argv)
117 {
118     char *buf;
119     int no = 0;
120     int start_pos = 0;
121     int end_pos = 99999;
122     char *output_file = NULL;
123     char *input_file = NULL;
124     char *filter = NULL;
125     int verbose = 0;
126     int quiet = 0;
127     FILE *outf = NULL;
128     FILE *inf = stdin;
129
130     prog = *argv;
131     while (--argc > 0)
132     {
133         if (**++argv == '-')
134         {
135             switch (argv[0][1])
136             {
137             case 'h':
138             case 'H':
139                 fprintf (stderr, "iso2709dump [options] [file]\n");
140                 fprintf (stderr, "If no file is specified stdin is used\n");
141                 fprintf (stderr, "Options:\n");
142                 fprintf (stderr, " -p no        Starting position\n");
143                 fprintf (stderr, " -l end       Ending position\n");
144                 fprintf (stderr, " -o file      Raw MARC output file\n");
145                 fprintf (stderr, " -v           Verbose\n");
146                 fprintf (stderr, " -q           Quiet mode\n");
147                 exit (1);
148             case 'p':
149                 if (argv[0][2])
150                     start_pos = atoi (argv[0]+2);
151                 else if (argc > 0)
152                 {
153                     --argc;
154                     start_pos = atoi (*++argv);
155                 }
156                 else
157                 {
158                     fprintf (stderr, "%s: missing start pos\n", prog);
159                     exit (1);
160                 }
161                 break;
162             case 'l':
163                 if (argv[0][2])
164                     end_pos = atoi (argv[0]+2);
165                 else if (argc > 0)
166                 {
167                     --argc;
168                     end_pos = atoi (*++argv);
169                 }
170                 else
171                 {
172                     fprintf (stderr, "%s: missing end pos\n", prog);
173                     exit (1);
174                 }
175                 break;
176             case 'o':
177                 if (argv[0][2])
178                     output_file = argv[0]+2;
179                 else if (argc > 0)
180                 {
181                     --argc;
182                     output_file = *++argv;
183                 }
184                 else
185                 {
186                     fprintf (stderr, "%s: missing output file\n", prog);
187                     exit (1);
188                 }
189                 break;
190             case 'v':
191                 verbose = 1;
192                 break;
193             case 'q':
194                 quiet = 1;
195                 break;
196             case 'f':
197                 if (argv[0][2])
198                     filter = argv[0]+2;
199                 else if (argc > 0)
200                 {
201                     --argc;
202                     filter = *++argv;
203                 }
204                 else
205                 {
206                     fprintf (stderr, "%s: missing filter\n", prog);
207                     exit (1);
208                 }
209                 break;
210             default:
211                 fprintf (stderr, "%s: unknown option %s; use -h for help\n",
212                          prog, *argv);
213                 exit (1);
214             }
215         }
216         else
217             input_file = *argv;
218     }
219     if (input_file)
220     {
221         inf = fopen (input_file, "r");
222         if (!inf)
223         {
224             fprintf (stderr, "%s: cannot open %s: %s\n",
225                      prog, input_file, strerror (errno));
226             exit (1);
227         }
228     }
229     if (output_file)
230     {
231         outf = fopen (output_file, "w");
232         if (!outf)
233         {
234             fprintf (stderr, "%s: cannot open %s: %s\n",
235                      prog, output_file, strerror (errno));
236             exit (1);
237         }
238     }
239     while ((buf = iso2709_read (inf)))
240     {
241         if (no >= start_pos && no <= end_pos)
242         {
243             char *obuf;
244             int olen;
245             Iso2709Rec rec_input;
246             Iso2709Rec rec_output;
247             
248             rec_input = iso2709_cvt (buf);
249             if (!quiet)
250                 printf ("------- %d --------\n", no);
251             if (!rec_input)
252             {
253                 if (!quiet)
254                     printf ("Bad record\n");
255                 fprintf (stderr, "%s: bad record at position %d\n", prog, no);
256                 break;
257             }
258             olen = iso2709_out (rec_input, &obuf, 0);
259             rec_output = copy_rec (rec_input);
260             if (!quiet)
261                 iso2709_display (rec_input, stdout);
262             if (outf && fwrite (obuf, 1, olen, outf) != olen)
263             {
264                 fprintf (stderr, "%s: write fail of %s: %s\n",
265                          prog, output_file, strerror (errno));
266                 exit (1);
267             }
268             free (obuf);
269             iso2709_rm (rec_input);
270             iso2709_rm (rec_output);
271         }
272         free (buf);
273         no++;
274     }
275     if (outf)
276         fclose (outf);
277     return 0;
278 }