/* BEGIN_ICS_COPYRIGHT7 **************************************** Copyright (c) 2015-2017, Intel Corporation Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ** END_ICS_COPYRIGHT7 ****************************************/ /* [ICS VERSION STRING: unknown] */ #include #include #include #include #include #include #include #include #define _GNU_SOURCE #include boolean g_trim = FALSE; boolean g_keep_newline = FALSE; boolean g_add_lineno = FALSE; boolean g_filter = FALSE; #define MAX_ELEMENTS 100 char *g_elements[MAX_ELEMENTS] = { 0 }; int g_numElements = 0; /* example of simple non-predefined parser */ /* for an example of a predefined parser, see opareport/topology.c */ static void *FieldXmlParserStart(IXmlParserState_t *input_state, void *parent, const char **attr); static void FieldXmlParserEnd(IXmlParserState_t *input_state, const IXML_FIELD *field, void *object, void *parent, XML_Char *content, unsigned len, boolean valid); static IXML_FIELD UntrimmedFields[] = { { tag:"*", format:'w', subfields:UntrimmedFields, start_func:FieldXmlParserStart, end_func:FieldXmlParserEnd }, // wildcard to traverse xml tree, keep all whitespace { NULL } }; #if 0 static IXML_FIELD TrimmedFields[] = { { tag:"*", format:'y', subfields:TrimmedFields, start_func:FieldXmlParserStart, end_func:FieldXmlParserEnd }, // wildcard to traverse xml tree, trim whitespace { NULL } }; #endif static void FieldXmlFormatAttr(IXmlOutputState_t *output_state, void *data) { const char **attr = (const char **)data; int i; for (i = 0; attr[i]; i += 2) { IXmlOutputPrint(output_state, " %s=\"", attr[i]); IXmlOutputPrintStr(output_state, attr[i+1]); IXmlOutputPrint(output_state, "\""); } } static boolean compareElement(const char* element) { int i; for (i=0; iparser)); else IXmlOutputEndTag(output_state, IXmlParserGetCurrentTag(input_state)); } } FSTATUS Xml2ParseInputFile(const char *input_file, void *context, IXML_FIELD *fields) { if (strcmp(input_file, "-") == 0) { fprintf(stderr, "Parsing stdin...\n"); if (FSUCCESS != IXmlParseFile(stdin, "stdin", IXML_PARSER_FLAG_NONE, fields, NULL, context, NULL, NULL, NULL, NULL)) { return FERROR; } } else { fprintf(stderr, "Parsing %s...\n", input_file); if (FSUCCESS != IXmlParseInputFile(input_file, IXML_PARSER_FLAG_NONE, fields, NULL, context, NULL, NULL, NULL, NULL)) { return FERROR; } } return FSUCCESS; } void Usage(int exitcode) { fprintf(stderr, "Usage: opaxmlfilter [-t|-k] [-l] [-i indent] [-s element] [input_file]\n"); fprintf(stderr, " or\n"); fprintf(stderr, " opaxmlfilter --help\n"); fprintf(stderr, " --help - produce full help text\n"); fprintf(stderr, " -t - trim leading and trailing whitespace in tag contents\n"); fprintf(stderr, " -k - in tags with purely whitespace which contain newlines,\n"); fprintf(stderr, " keep newlines as is (default is to format as an empty list)\n"); fprintf(stderr, " -l - add comments with line numbers after each end tag\n"); fprintf(stderr, " This can make comparison of resulting files easier\n"); fprintf(stderr, " since original line numbers will be available\n"); fprintf(stderr, " -i indent - set indentation to use per level (default 4)\n"); fprintf(stderr, " -s element - name of XML element to suppress\n"); fprintf(stderr, " can be used multiple times (max 100), order does not matter\n"); fprintf(stderr, " input_file - xml file to read. default is stdin\n"); exit(exitcode); } static void addElement(const char *element) { int len = 0; if (g_numElements >= MAX_ELEMENTS) { fprintf(stderr, "opaxmlfilter: Too many suppressed elements, limit of %d\n", MAX_ELEMENTS); exit(1); } if (! element || ! (len = strlen(element))) { fprintf(stderr, "opaxmlfilter: Missing element.\n"); Usage(2); } len += 3; // allow for \0 and possible wildcards g_elements[g_numElements] = malloc(len); if (! g_elements[g_numElements]) { fprintf(stderr, "opaxmlfilter: Unable to allocate memory\n"); exit(1); } snprintf(g_elements[g_numElements], len, "%s%s", strchr(element, '*') ? "" : "*.", element); // no issue with duplicates, so no need to check g_numElements++; } int main(int argc, char **argv) { IXmlOutputState_t output_state; int exit_code = 0; uint32 indent = 4; const char *opts="tkli:s:"; const struct option longopts[] = {{"help", 0, 0, '$'}, {0, 0, 0, 0}}; char *filename = "-"; // default to stdin IXML_FIELD *fields = UntrimmedFields; int c; while (-1 != (c = getopt_long(argc, argv, opts, longopts, NULL))) { switch (c) { case '$': Usage(0); case 't': // TrimmedFields treats empty list as tag with no content //fields = TrimmedFields; g_trim = TRUE; break; case 'i': if (FSUCCESS != StringToUint32(&indent, optarg, NULL, 0, TRUE)) { fprintf(stderr, "opaxmlfilter: Invalid indent: %s\n", optarg); Usage(2); } break; case 'k': g_keep_newline = TRUE; break; case 'l': g_add_lineno = TRUE; break; case 's': addElement(optarg); break; default: Usage(2); } } if (g_trim && g_keep_newline) { fprintf(stderr, "opaxmlfilter: Can't use -k and -t together\n"); Usage(2); } if (argc > optind){ filename = argv[optind++]; if (!filename) { fprintf(stderr, "opaxmlfilter: Error: null input filename\n"); exit(1); } } if (argc > optind) Usage(2); if (FSUCCESS != IXmlOutputInit(&output_state, stdout, indent, IXML_OUTPUT_FLAG_NONE, NULL)) exit(1); if (FSUCCESS != Xml2ParseInputFile(filename, &output_state, fields)) exit_code = 1; IXmlOutputDestroy(&output_state); exit(exit_code); }