MARC decode fix. Attribute set fix for scan in server. Prox logging.
[yaz-moved-to-github.git] / util / marcdisp.c
1 /*
2  * Copyright (c) 1995-2001, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Log: marcdisp.c,v $
6  * Revision 1.16  2002-01-22 10:54:46  adam
7  * MARC decode fix. Attribute set fix for scan in server. Prox logging.
8  *
9  * Revision 1.15  2001/10/29 09:17:19  adam
10  * New function marc_display_exl - used by YAZ client. Server returns
11  * bad record on position 98 (for testing).
12  *
13  * Revision 1.14  2001/10/23 21:00:20  adam
14  * Old Z39.50 codecs gone. Added ZOOM. WRBUF MARC display util.
15  *
16  * Revision 1.13  2001/10/15 19:36:48  adam
17  * New function marc_display_wrbuf.
18  *
19  * Revision 1.12  2000/10/02 11:07:44  adam
20  * Added peer_name member for bend_init handler. Changed the YAZ
21  * client so that tcp: can be avoided in target spec.
22  *
23  * Revision 1.11  2000/02/29 13:44:55  adam
24  * Check for config.h (currently not generated).
25  *
26  * Revision 1.10  2000/02/05 10:47:19  adam
27  * Identifier-length and indicator-lenght no longer set to 2 (forced).
28  *
29  * Revision 1.9  1999/12/21 16:24:48  adam
30  * More robust ISO2709 handling (in case of real bad formats).
31  *
32  * Revision 1.8  1999/11/30 13:47:12  adam
33  * Improved installation. Moved header files to include/yaz.
34  *
35  * Revision 1.7  1997/09/24 13:29:40  adam
36  * Added verbose option -v to marcdump utility.
37  *
38  * Revision 1.6  1997/09/04 07:52:27  adam
39  * Moved atoi_n function to separate source file.
40  *
41  * Revision 1.5  1997/05/01 15:08:15  adam
42  * Added log_mask_str_x routine.
43  *
44  * Revision 1.4  1995/09/29 17:12:34  quinn
45  * Smallish
46  *
47  * Revision 1.3  1995/09/27  15:03:03  quinn
48  * Modified function heads & prototypes.
49  *
50  * Revision 1.2  1995/05/16  08:51:12  quinn
51  * License, documentation, and memory fixes
52  *
53  * Revision 1.1  1995/04/10  10:28:46  quinn
54  * Added copy of CCL and MARC display
55  *
56  */
57
58 #if HAVE_CONFIG_H
59 #include <config.h>
60 #endif
61
62 #include <stdio.h>
63 #include <string.h>
64 #include <ctype.h>
65 #include <yaz/marcdisp.h>
66 #include <yaz/wrbuf.h>
67 #include <yaz/yaz-util.h>
68
69 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
70                         int bsize)
71 {
72     int entry_p;
73     int record_length;
74     int indicator_length;
75     int identifier_length;
76     int base_address;
77     int length_data_entry;
78     int length_starting;
79     int length_implementation;
80
81     record_length = atoi_n (buf, 5);
82     if (record_length < 25)
83     {
84         if (debug)
85         {
86             char str[40];
87             
88             sprintf (str, "Record length %d - aborting\n", record_length);
89             wrbuf_puts (wr, str);
90         }
91         return -1;
92     }
93     /* ballout if bsize is known and record_length is than that */
94     if (bsize != -1 && record_length > bsize)
95         return -1;
96     if (isdigit(buf[10]))
97         indicator_length = atoi_n (buf+10, 1);
98     else
99         indicator_length = 2;
100     if (isdigit(buf[11]))
101         identifier_length = atoi_n (buf+11, 1);
102     else
103         identifier_length = 2;
104     base_address = atoi_n (buf+12, 5);
105
106     length_data_entry = atoi_n (buf+20, 1);
107     length_starting = atoi_n (buf+21, 1);
108     length_implementation = atoi_n (buf+22, 1);
109
110     if (debug)
111     {
112         char str[40];
113         sprintf (str, "Record length         %5d\n", record_length);
114         wrbuf_puts (wr, str);
115         sprintf (str, "Indicator length      %5d\n", indicator_length);
116         wrbuf_puts (wr, str);
117         sprintf (str, "Identifier length     %5d\n", identifier_length);
118         wrbuf_puts (wr, str);
119         sprintf (str, "Base address          %5d\n", base_address);
120         wrbuf_puts (wr, str);
121         sprintf (str, "Length data entry     %5d\n", length_data_entry);
122         wrbuf_puts (wr, str);
123         sprintf (str, "Length starting       %5d\n", length_starting);
124         wrbuf_puts (wr, str);
125         sprintf (str, "Length implementation %5d\n", length_implementation);
126         wrbuf_puts (wr, str);
127     }
128     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
129     {
130         entry_p += 3+length_data_entry+length_starting;
131         if (entry_p >= record_length)
132             return -1;
133     }
134     base_address = entry_p+1;
135     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
136     {
137         int data_length;
138         int data_offset;
139         int end_offset;
140         int i, j;
141         char tag[4];
142
143         memcpy (tag, buf+entry_p, 3);
144         entry_p += 3;
145         tag[3] = '\0';
146         if (debug)
147             wrbuf_puts (wr, "Tag: ");
148         wrbuf_puts (wr, tag);
149         wrbuf_puts (wr, " ");
150         data_length = atoi_n (buf+entry_p, length_data_entry);
151         entry_p += length_data_entry;
152         data_offset = atoi_n (buf+entry_p, length_starting);
153         entry_p += length_starting;
154         i = data_offset + base_address;
155         end_offset = i+data_length-1;
156         if (debug)
157             wrbuf_puts (wr, " Ind: ");
158         if (memcmp (tag, "00", 2) && indicator_length)
159         {
160             for (j = 0; j<indicator_length; j++, i++)
161                 wrbuf_putc (wr, buf[i]);
162         }
163         if (debug)
164             wrbuf_puts (wr, " Fields: ");
165         while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
166         {
167             if (memcmp (tag, "00", 2) && identifier_length)
168             {
169                 i++;
170                 wrbuf_puts (wr, " $"); 
171                 for (j = 1; j<identifier_length; j++, i++)
172                     wrbuf_putc (wr, buf[i]);
173                 wrbuf_putc (wr, ' ');
174                 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
175                        buf[i] != ISO2709_FS && i < end_offset)
176                 {
177                     wrbuf_putc (wr, buf[i]);
178                     i++;
179                 }
180             }
181             else
182             {
183                 wrbuf_putc (wr, buf[i]);
184                 i++;
185             }
186         }
187         wrbuf_putc (wr, '\n');
188         if (i < end_offset)
189             wrbuf_puts (wr, "-- separator but not at end of field\n");
190         if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
191             wrbuf_puts (wr, "-- no separator at end of field\n");
192     }
193     wrbuf_puts (wr, "");
194     return record_length;
195 }
196
197 int marc_display_exl (const char *buf, FILE *outf, int debug, int length)
198 {
199     int record_length;
200
201     WRBUF wrbuf = wrbuf_alloc ();
202     record_length = marc_display_wrbuf (buf, wrbuf, debug, length);
203     if (!outf)
204         outf = stdout;
205     if (record_length > 0)
206         fwrite (wrbuf_buf(wrbuf), 1, wrbuf_len(wrbuf), outf);
207     wrbuf_free (wrbuf, 1);
208     return record_length;
209 }
210
211
212 int marc_display_ex (const char *buf, FILE *outf, int debug)
213 {
214     return marc_display_exl (buf, outf, debug, -1);
215 }
216
217 int marc_display (const char *buf, FILE *outf)
218 {
219     return marc_display_ex (buf, outf, 0);
220 }
221
222