static void output (char **roots) { xmlOutputBufferPtr ob = xmlOutputBufferCreateFd (1, NULL); if (ob == NULL) { fprintf (stderr, _("%s: xmlOutputBufferCreateFd: failed to open stdout\n"), guestfs_int_program_name); exit (EXIT_FAILURE); } /* 'ob' is freed when 'xo' is freed.. */ CLEANUP_XMLFREETEXTWRITER xmlTextWriterPtr xo = xmlNewTextWriter (ob); if (xo == NULL) { fprintf (stderr, _("%s: xmlNewTextWriter: failed to create libxml2 writer\n"), guestfs_int_program_name); exit (EXIT_FAILURE); } /* Pretty-print the output. */ XMLERROR (-1, xmlTextWriterSetIndent (xo, 1)); XMLERROR (-1, xmlTextWriterSetIndentString (xo, BAD_CAST " ")); XMLERROR (-1, xmlTextWriterStartDocument (xo, NULL, NULL, NULL)); output_roots (xo, roots); XMLERROR (-1, xmlTextWriterEndDocument (xo)); }
/** * Write the xml document out to a file descriptor. * @param xml The xml document * @param fd The xml output descriptor * @return @c TRUE if successful, @c FALSE if an error occurs. * @ingroup EXML_Write_Group */ int exml_fd_write(EXML *xml, int fd) { xmlTextWriterPtr writer; xmlOutputBufferPtr out; out = xmlOutputBufferCreateFd(fd, NULL); if (out == NULL) { xmlGenericError(xmlGenericErrorContext, "xmlNewTextWriterFd : out of memory!\n"); return FALSE; } CHECK_PARAM_POINTER_RETURN("xml", xml, FALSE); writer = xmlNewTextWriter( out ); return _exml_write(xml, writer); }
static void output (char **roots) { xmlOutputBufferPtr ob = xmlOutputBufferCreateFd (1, NULL); if (ob == NULL) error (EXIT_FAILURE, 0, _("xmlOutputBufferCreateFd: failed to open stdout")); /* 'ob' is freed when 'xo' is freed.. */ CLEANUP_XMLFREETEXTWRITER xmlTextWriterPtr xo = xmlNewTextWriter (ob); if (xo == NULL) error (EXIT_FAILURE, 0, _("xmlNewTextWriter: failed to create libxml2 writer")); /* Pretty-print the output. */ XMLERROR (-1, xmlTextWriterSetIndent (xo, 1)); XMLERROR (-1, xmlTextWriterSetIndentString (xo, BAD_CAST " ")); XMLERROR (-1, xmlTextWriterStartDocument (xo, NULL, NULL, NULL)); output_roots (xo, roots); XMLERROR (-1, xmlTextWriterEndDocument (xo)); }
int oscap_xml_save_filename(const char *filename, xmlDocPtr doc) { xmlOutputBufferPtr buff; int xmlCode; if (strcmp(filename, "-") == 0) { xmlCode = xmlSaveFormatFileEnc(filename, doc, "UTF-8", 1); } else { int fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); if (fd < 0) { oscap_seterr(OSCAP_EFAMILY_GLIBC, "%s '%s'", strerror(errno), filename); xmlFreeDoc(doc); return -1; } buff = xmlOutputBufferCreateFd(fd, NULL); if (buff == NULL) { close(fd); oscap_setxmlerr(xmlGetLastError()); oscap_dlprintf(DBG_W, "xmlOutputBufferCreateFile() failed.\n"); xmlFreeDoc(doc); return -1; } xmlCode = xmlSaveFormatFileTo(buff, doc, "UTF-8", 1); close(fd); } if (xmlCode <= 0) { oscap_setxmlerr(xmlGetLastError()); oscap_dlprintf(DBG_W, "No bytes exported: xmlCode: %d.\n", xmlCode); } return (xmlCode >= 1) ? 1 : -1; }
int main(int argc, char **argv) { int ch; char *encoding = "utf8"; char *filename; int quiet = 0; int exitcode = EXIT_OK; xmlDocPtr doc = NULL; xmlDtdPtr dtd = NULL; xmlCharEncoding enc; xmlCharEncodingHandlerPtr enchandler; xmlOutputBufferPtr out; /* LibXML version checking */ LIBXML_TEST_VERSION; /* Who am I? */ if ((progname = rindex(argv[0], '/'))) progname++; else progname = argv[0]; /* Parsing CLI */ while ((ch = getopt(argc, argv, "hqe:")) != -1) switch (ch) { case 'q': quiet = 1; break; case 'e': encoding = optarg; break; case 'h': /* FALL THROUGH */ case '?': usage(EXIT_OK); break; default: usage(EXIT_USAGE); break; } argc -= optind; argv += optind; switch (argc) { case 1: filename = argv[0]; break; case 0: filename = "-"; break; default: usage(EXIT_USAGE); } /* Get user encoding */ if ((enc = xmlParseCharEncoding(encoding)) == XML_CHAR_ENCODING_ERROR) { fprintf(stderr, "Unknown encoding: %s\n", encoding); usage(EXIT_USAGE); } enchandler = xmlGetCharEncodingHandler(enc); /* Shut up */ xmlSetGenericErrorFunc(NULL, &noerror_handler); xmlGetWarningsDefaultValue = 0; xmlPedanticParserDefault(0); /* File exists? (race condition below) */ if (strcmp(filename, "-") && (open(filename, O_RDONLY) < 0)) { if (!quiet) fprintf(stderr, "ERROR: unable to open %s\n", filename); exitcode = EXIT_NOFILE; goto exit; } /* Parse document */ if ((doc = xmlParseFile(filename)) == NULL) { if (!quiet) fprintf(stderr, "ERROR: badly formed document\n"); exitcode = EXIT_BADXML; goto exit; } /* Extract DTD (if any) */ dtd = xmlGetIntSubset(doc); /* Create output buffer */ if ((out = xmlOutputBufferCreateFd(1, enchandler)) == NULL) { if (!quiet) fprintf(stderr, "ERROR: unable to open output channel\n"); exitcode = EXIT_INTERNAL; goto exit; } /* Dump information */ if (doc->version) { xmlOutputBufferWriteString(out, "Version : "); xmlOutputBufferWriteString(out, doc->version); xmlOutputBufferWriteString(out, "\n"); } if (doc->encoding) { xmlOutputBufferWriteString(out, "Encoding : "); xmlOutputBufferWriteString(out, doc->encoding); xmlOutputBufferWriteString(out, "\n"); } if (dtd && dtd->name) { xmlOutputBufferWriteString(out, "Name : "); xmlOutputBufferWriteString(out, dtd->name); xmlOutputBufferWriteString(out, "\n"); } if (dtd && dtd->ExternalID) { xmlOutputBufferWriteString(out, "Identifier: "); xmlOutputBufferWriteString(out, dtd->ExternalID); xmlOutputBufferWriteString(out, "\n"); } if (dtd && dtd->SystemID) { xmlOutputBufferWriteString(out, "URI : "); xmlOutputBufferWriteString(out, dtd->SystemID); xmlOutputBufferWriteString(out, "\n"); xmlOutputBufferFlush(out); } /* Free resources */ if (doc) xmlFreeDoc(doc); xmlCleanupParser(); /* OK */ exit: exit(exitcode); }