1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
6 #define _FILE_OFFSET_BITS 64
13 #include <libxml/parser.h>
14 #include <libxml/tree.h>
15 #include <libxml/xpath.h>
16 #include <libxml/xpathInternals.h>
18 /* Libxml2 version < 2.6.15. xmlreader not reliable/present */
19 #if LIBXML_VERSION < 20615
20 #define USE_XMLREADER 0
22 #define USE_XMLREADER 1
26 #include <libxml/xmlreader.h>
44 #include <yaz/marcdisp.h>
46 #include <yaz/yaz-util.h>
47 #include <yaz/xmalloc.h>
48 #include <yaz/options.h>
60 static int no_errors = 0;
62 static void usage(const char *prog)
64 fprintf(stderr, "Usage: %s [-i format] [-o format] [-f from] [-t to] "
65 "[-l pos=value] [-c cfile] [-s prefix] [-C size] [-n] "
66 "[-p] [-v] [-V] file...\n",
70 static void show_version(void)
72 char vstr[20], sha1_str[41];
74 yaz_version(vstr, sha1_str);
75 printf("YAZ version: %s %s\n", YAZ_VERSION, YAZ_VERSION_SHA1);
76 if (strcmp(sha1_str, YAZ_VERSION_SHA1))
77 printf("YAZ DLL/SO: %s %s\n", vstr, sha1_str);
81 static int getbyte_stream(void *client_data)
83 FILE *f = (FILE*) client_data;
91 static void ungetbyte_stream(int c, void *client_data)
93 FILE *f = (FILE*) client_data;
100 static void marcdump_read_line(yaz_marc_t mt, const char *fname)
102 FILE *inf = fopen(fname, "rb");
105 fprintf(stderr, "%s: cannot open %s:%s\n",
106 prog, fname, strerror(errno));
110 while (yaz_marc_read_line(mt, getbyte_stream,
111 ungetbyte_stream, inf) == 0)
113 WRBUF wrbuf = wrbuf_alloc();
114 yaz_marc_write_mode(mt, wrbuf);
115 fputs(wrbuf_cstr(wrbuf), stdout);
116 wrbuf_destroy(wrbuf);
121 static void marcdump_read_json(yaz_marc_t mt, const char *fname)
123 FILE *inf = fopen(fname, "rb");
126 fprintf(stderr, "%s: cannot open %s:%s\n",
127 prog, fname, strerror(errno));
134 WRBUF w = wrbuf_alloc();
138 while ((c = getc(inf)) != EOF)
140 n = json_parse2(wrbuf_cstr(w), &errmsg, &errpos);
143 int r = yaz_marc_read_json_node(mt, n);
147 yaz_marc_write_mode(mt, w);
148 fputs(wrbuf_cstr(w), stdout);
153 fprintf(stderr, "%s: JSON MARC parsing failed ret=%d\n", fname,
159 fprintf(stderr, "%s: JSON parse error: %s . pos=%ld\n", fname,
160 errmsg, (long) errpos);
169 static void marcdump_read_xml(yaz_marc_t mt, const char *fname)
171 WRBUF wrbuf = wrbuf_alloc();
173 xmlTextReaderPtr reader = xmlReaderForFile(fname, 0 /* encoding */,
179 while ((ret = xmlTextReaderRead(reader)) == 1)
181 int type = xmlTextReaderNodeType(reader);
182 if (type == XML_READER_TYPE_ELEMENT)
184 char *name = (char *) xmlTextReaderLocalName(reader);
185 if (!strcmp(name, "record") || !strcmp(name, "r"))
187 xmlNodePtr ptr = xmlTextReaderExpand(reader);
189 int r = yaz_marc_read_xml(mt, ptr);
193 fprintf(stderr, "yaz_marc_read_xml failed\n");
197 int write_rc = yaz_marc_write_mode(mt, wrbuf);
200 yaz_log(YLOG_WARN, "yaz_marc_write_mode: "
201 "write error: %d", write_rc);
204 fputs(wrbuf_cstr(wrbuf), stdout);
211 xmlFreeTextReader(reader);
214 xmlDocPtr doc = xmlParseFile(fname);
217 xmlNodePtr ptr = xmlDocGetRootElement(doc);
218 for (; ptr; ptr = ptr->next)
220 if (ptr->type == XML_ELEMENT_NODE)
222 if (!strcmp((const char *) ptr->name, "collection"))
227 if (!strcmp((const char *) ptr->name, "record") ||
228 !strcmp((const char *) ptr->name, "r"))
230 int r = yaz_marc_read_xml(mt, ptr);
234 fprintf(stderr, "yaz_marc_read_xml failed\n");
238 yaz_marc_write_mode(mt, wrbuf);
240 fputs(wrbuf_cstr(wrbuf), stdout);
249 fputs(wrbuf_cstr(wrbuf), stdout);
250 wrbuf_destroy(wrbuf);
254 static void dump(const char *fname, const char *from, const char *to,
255 int input_format, int output_format,
256 int write_using_libxml2,
257 int print_offset, const char *split_fname, int split_chunk,
258 int verbose, FILE *cfile, const char *leader_spec)
260 yaz_marc_t mt = yaz_marc_create();
263 if (yaz_marc_leader_spec(mt, leader_spec))
265 fprintf(stderr, "bad leader spec: %s\n", leader_spec);
266 yaz_marc_destroy(mt);
271 cd = yaz_iconv_open(to, from);
274 fprintf(stderr, "conversion from %s to %s "
275 "unsupported\n", from, to);
276 yaz_marc_destroy(mt);
279 yaz_marc_iconv(mt, cd);
281 yaz_marc_enable_collection(mt);
282 yaz_marc_xml(mt, output_format);
283 yaz_marc_write_using_libxml2(mt, write_using_libxml2);
284 yaz_marc_debug(mt, verbose);
286 if (input_format == YAZ_MARC_MARCXML || input_format == YAZ_MARC_TURBOMARC || input_format == YAZ_MARC_XCHANGE)
289 marcdump_read_xml(mt, fname);
292 else if (input_format == YAZ_MARC_LINE)
294 marcdump_read_line(mt, fname);
296 else if (input_format == YAZ_MARC_JSON)
298 marcdump_read_json(mt, fname);
300 else if (input_format == YAZ_MARC_ISO2709)
302 FILE *inf = fopen(fname, "rb");
305 int split_file_no = -1;
308 fprintf(stderr, "%s: cannot open %s:%s\n",
309 prog, fname, strerror(errno));
313 fprintf(cfile, "char *marc_records[] = {\n");
316 const char *result = 0;
323 r = fread(buf, 1, 5, inf);
326 if (r == 0) /* normal EOF, all good */
328 if (print_offset && verbose)
330 printf("<!-- Extra %ld bytes at end of file -->\n",
335 while (*buf < '0' || *buf > '9')
338 long off = ftell(inf) - 5;
339 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
341 *buf & 0xff, *buf & 0xff,
343 for (i = 0; i<4; i++)
345 r = fread(buf+4, 1, 1, inf);
352 if (verbose || print_offset)
353 printf("<!-- End of file with data -->\n");
358 long off = ftell(inf) - 5;
359 printf("<!-- Record %d offset %ld (0x%lx) -->\n",
362 len = atoi_n(buf, 5);
363 if (len < 25 || len > 100000)
365 long off = ftell(inf) - 5;
366 printf("<!-- Bad Length %ld read at offset %ld (%lx) -->\n",
367 (long)len, (long) off, (long) off);
372 r = fread(buf + 5, 1, rlen, inf);
375 long off = ftell(inf);
376 printf("<!-- Premature EOF at offset %ld (%lx) -->\n",
377 (long) off, (long) off);
381 while (buf[len-1] != ISO2709_RS)
383 if (len > sizeof(buf)-2)
388 r = fread(buf + len, 1, 1, inf);
395 printf("<!-- EOF while searching for RS -->\n");
402 const char *mode = 0;
404 if ((marc_no % split_chunk) == 0)
411 sprintf(fname, "%.200s%07d", split_fname, split_file_no);
412 sf = fopen(fname, mode);
415 fprintf(stderr, "Could not open %s\n", fname);
420 if (fwrite(buf, 1, len, sf) != len)
422 fprintf(stderr, "Could write content to %s\n",
431 r = yaz_marc_decode_buf(mt, buf, -1, &result, &len_result);
434 if (r > 0 && result && len_result)
436 if (fwrite(result, len_result, 1, stdout) != 1)
438 fprintf(stderr, "Write to stdout failed\n");
449 fprintf(cfile, "\n");
450 for (i = 0; i < r; i++)
453 fprintf(cfile, " \"");
454 if (p[i] < 32 || p[i] > 126)
455 fprintf(cfile, "\" \"\\x%02X\" \"", p[i] & 255);
459 if (i < r - 1 && (i & 15) == 15)
460 fprintf(cfile, "\"\n");
463 fprintf(cfile, "\"\n");
470 fprintf(cfile, "};\n");
474 WRBUF wrbuf = wrbuf_alloc();
475 yaz_marc_write_trailer(mt, wrbuf);
476 fputs(wrbuf_cstr(wrbuf), stdout);
477 wrbuf_destroy(wrbuf);
481 yaz_marc_destroy(mt);
484 int main (int argc, char **argv)
487 int print_offset = 0;
491 int output_format = YAZ_MARC_LINE;
493 char *from = 0, *to = 0;
494 int input_format = YAZ_MARC_ISO2709;
496 const char *split_fname = 0;
497 const char *leader_spec = 0;
498 int write_using_libxml2 = 0;
501 setlocale(LC_CTYPE, "");
505 to = nl_langinfo(CODESET);
510 while ((r = options("i:o:C:npc:xOeXIf:t:s:l:Vv", argv, argc, &arg)) != -2)
516 input_format = yaz_marc_decode_formatstr(arg);
517 if (input_format == -1)
519 fprintf(stderr, "%s: bad input format: %s\n", prog, arg);
524 if (input_format == YAZ_MARC_MARCXML
525 || input_format == YAZ_MARC_XCHANGE)
527 fprintf(stderr, "%s: Libxml2 support not enabled\n", prog);
533 /* dirty hack so we can make Libxml2 do the writing ..
535 if (strlen(arg) > 4 && strncmp(arg, "xml,", 4) == 0)
537 /* Only supported for Libxml2 2.6.0 or later */
538 #if LIBXML_VERSION >= 20600
540 write_using_libxml2 = 1;
542 fprintf(stderr, "%s: output using Libxml2 unsupported\n", prog);
546 output_format = yaz_marc_decode_formatstr(arg);
547 if (output_format == -1)
549 fprintf(stderr, "%s: bad output format: %s\n", prog, arg);
565 cfile = fopen(arg, "w");
568 fprintf(stderr, "%s: -x no longer supported. "
569 "Use -i marcxml instead\n", prog);
573 fprintf(stderr, "%s: OAI MARC no longer supported."
574 " Use MARCXML instead.\n", prog);
578 fprintf(stderr, "%s: -e no longer supported. "
579 "Use -o marcxchange instead\n", prog);
583 fprintf(stderr, "%s: -X no longer supported. "
584 "Use -o marcxml instead\n", prog);
588 fprintf(stderr, "%s: -I no longer supported. "
589 "Use -o marc instead\n", prog);
593 output_format = YAZ_MARC_CHECK;
602 split_chunk = atoi(arg);
605 dump(arg, from, to, input_format, output_format,
607 print_offset, split_fname, split_chunk,
608 verbose, cfile, leader_spec);
635 * c-file-style: "Stroustrup"
636 * indent-tabs-mode: nil
638 * vim: shiftwidth=4 tabstop=8 expandtab