diff --git a/crm/admin/xml_diff.c b/crm/admin/xml_diff.c index 93c35b8ab7..9b37f05dad 100644 --- a/crm/admin/xml_diff.c +++ b/crm/admin/xml_diff.c @@ -1,263 +1,263 @@ -/* $Id: xml_diff.c,v 1.7 2006/01/23 16:07:02 andrew Exp $ */ +/* $Id: xml_diff.c,v 1.8 2006/04/12 08:22:50 andrew Exp $ */ /* * Copyright (C) 2004 Andrew Beekhof * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef HAVE_GETOPT_H #include #endif #include /* someone complaining about _ha_msg_mod not being found */ #include const char *crm_system_name = "diff"; void usage(const char *cmd, int exit_status); #define OPTARGS "V?o:n:p:scfO:N:" int main(int argc, char **argv) { gboolean apply = FALSE; gboolean raw_1 = FALSE; gboolean raw_2 = FALSE; gboolean filter = FALSE; gboolean use_stdin = FALSE; gboolean as_cib = FALSE; int argerr = 0; int flag; crm_data_t *object_1 = NULL; crm_data_t *object_2 = NULL; crm_data_t *output = NULL; const char *xml_file_1 = NULL; const char *xml_file_2 = NULL; #ifdef HAVE_GETOPT_H int option_index = 0; static struct option long_options[] = { /* Top-level Options */ {"original", 1, 0, 'o'}, {"new", 1, 0, 'n'}, {"original-string", 1, 0, 'O'}, {"new-string", 1, 0, 'N'}, {"patch", 1, 0, 'p'}, {"stdin", 0, 0, 's'}, {"cib", 0, 0, 'c'}, {"verbose", 0, 0, 'V'}, {"help", 0, 0, '?'}, {0, 0, 0, 0} }; #endif cl_log_set_entity(crm_system_name); cl_log_set_facility(LOG_USER); set_crm_log_level(LOG_CRIT-1); if(argc < 2) { usage(crm_system_name, LSB_EXIT_EINVAL); } while (1) { #ifdef HAVE_GETOPT_H flag = getopt_long(argc, argv, OPTARGS, long_options, &option_index); #else flag = getopt(argc, argv, OPTARGS); #endif if (flag == -1) break; switch(flag) { case 'o': xml_file_1 = optarg; break; case 'O': xml_file_1 = optarg; raw_1 = TRUE; break; case 'n': xml_file_2 = optarg; break; case 'N': xml_file_2 = optarg; raw_2 = TRUE; break; case 'p': xml_file_2 = optarg; apply = TRUE; break; case 'f': filter = TRUE; break; case 's': use_stdin = TRUE; break; case 'c': as_cib = TRUE; break; case 'V': cl_log_enable_stderr(TRUE); alter_debug(DEBUG_INC); break; default: printf("Argument code 0%o (%c)" " is not (?yet?) supported\n", flag, flag); ++argerr; break; } } if (optind < argc) { printf("non-option ARGV-elements: "); while (optind < argc) printf("%s ", argv[optind++]); printf("\n"); } if (optind > argc) { ++argerr; } if (argerr) { usage(crm_system_name, LSB_EXIT_GENERIC); } crm_zero_mem_stats(NULL); if(raw_1) { object_1 = string2xml(xml_file_1); } else if(use_stdin) { fprintf(stderr, "Input first XML fragment:"); object_1 = stdin2xml(); } else if(xml_file_1 != NULL) { FILE *xml_strm = fopen(xml_file_1, "r"); if(xml_strm != NULL) { crm_debug("Reading: %s", xml_file_1); object_1 = file2xml(xml_strm); } else { cl_perror("File not found: %s", xml_file_1); } } if(raw_2) { object_2 = string2xml(xml_file_2); } else if(use_stdin) { fprintf(stderr, "Input second XML fragment:"); object_2 = stdin2xml(); - } else if(xml_file_1 != NULL) { + } else if(xml_file_2 != NULL) { FILE *xml_strm = fopen(xml_file_2, "r"); if(xml_strm != NULL) { crm_debug("Reading: %s", xml_file_2); object_2 = file2xml(xml_strm); } else { cl_perror("File not found: %s", xml_file_2); } } CRM_ASSERT(object_1 != NULL); CRM_ASSERT(object_2 != NULL); if(as_cib == FALSE) { if(apply) { apply_xml_diff(object_1, object_2, &output); } else { output = diff_xml_object(object_1, object_2, filter); } } else { if(apply) { apply_cib_diff(object_1, object_2, &output); } else { output = diff_cib_object(object_1, object_2, filter); } } if(output != NULL) { char *buffer = dump_xml_formatted(output); fprintf(stdout, "%s", crm_str(buffer)); crm_free(buffer); } free_xml(object_1); free_xml(object_2); free_xml(output); crm_mem_stats(NULL); if(apply == FALSE && output != NULL) { return 1; } return 0; } void usage(const char *cmd, int exit_status) { FILE *stream; stream = exit_status != 0 ? stderr : stdout; fprintf(stream, "usage: %s [-?V] [oO] [pnN]\n", cmd); fprintf(stream, "Options\n"); fprintf(stream, "\t--%s (-%c)\tthis help message\n", "help", '?'); fprintf(stream, "\t--%s (-%c) \t\n", "original", 'o'); fprintf(stream, "\t--%s (-%c) \t\n", "new", 'n'); fprintf(stream, "\t--%s (-%c) \t\n", "original-string", 'O'); fprintf(stream, "\t--%s (-%c) \t\n", "new-string", 'N'); fprintf(stream, "\t--%s (-%c) \tApply a patch to the original XML\n", "patch", 'p'); fprintf(stream, "\t--%s (-%c)\tCompare/patch the inputs as a CIB\n", "cib", 'c'); fprintf(stream, "\t--%s (-%c)\tRead the inputs from stdin\n", "stdin", 's'); fflush(stream); exit(exit_status); }