# HG changeset patch
# User Dan Smith <danms(a)us.ibm.com>
# Date 1199911546 28800
# Node ID d981b576039570b106ecdb9a329b67d433ed4812
# Parent 1fe9e5492885e9af358f392c01ff6a88b613ab96
Signed-off-by: Dan Smith <danms(a)us.ibm.com>
diff -r 1fe9e5492885 -r d981b5760395 libxkutil/xml_parse_test.c
--- a/libxkutil/xml_parse_test.c Wed Jan 09 12:45:46 2008 -0800
+++ b/libxkutil/xml_parse_test.c Wed Jan 09 12:45:46 2008 -0800
@@ -1,5 +1,7 @@
#include <stdio.h>
#include <inttypes.h>
+
+#include <getopt.h>
#include <libvirt/libvirt.h>
@@ -110,6 +112,7 @@ static void print_devices(struct domain
{
int i;
+
fprintf(d, "\n-- Memory (%i) --\n", dominfo->dev_mem_ct);
for (i = 0; i < dominfo->dev_mem_ct; i++)
print_dev_mem(&dominfo->dev_mem[i], d);
@@ -149,40 +152,164 @@ static void print_domxml(struct domain *
printf("%s\n", xml);
}
-int main(int argc, char **argv)
-{
- virConnectPtr conn;
- virDomainPtr dom;
- struct domain *dominfo;
-
- if (argc < 2) {
- printf("Usage: %s domain [URI] [xml]\n", argv[0]);
- return 1;
- }
-
- if (argc > 2)
- conn = virConnectOpen(argv[2]);
- else
- conn = virConnectOpen("xen:///");
+static char *read_from_file(FILE *file)
+{
+ char *xml = NULL;
+ char buf[256];
+ int size = 0;
+
+ while (fgets(buf, sizeof(buf) - 1, file) != NULL) {
+ xml = realloc(xml, size + strlen(buf) + 1);
+ if (xml == NULL) {
+ printf("Out of memory\n");
+ return NULL;
+ }
+
+ strcat(xml, buf);
+ size += strlen(buf);
+ }
+
+ return xml;
+}
+
+static int dominfo_from_dom(const char *uri,
+ const char *domain,
+ struct domain **d)
+{
+ virConnectPtr conn = NULL;
+ virDomainPtr dom = NULL;
+ int ret = 0;
+
+ conn = virConnectOpen(uri);
if (conn == NULL) {
printf("Unable to connect to libvirt\n");
+ goto out;
+ }
+
+ dom = virDomainLookupByName(conn, domain);
+ if (dom == NULL) {
+ printf("Unable to find domain `%s'\n", domain);
+ goto out;
+ }
+
+ ret = get_dominfo(dom, d);
+
+ out:
+ virDomainFree(dom);
+ virConnectClose(conn);
+
+ return ret;
+}
+
+static int dominfo_from_file(const char *fname, struct domain **d)
+{
+ char *xml;
+ FILE *file;
+ int ret;
+
+ if (fname[0] == '-')
+ file = stdin;
+ else
+ file = fopen(fname, "r");
+
+ if (file == NULL) {
+ printf("Unable to open `%s'\n", fname);
+ return 0;
+ }
+
+ xml = read_from_file(file);
+ if (xml == NULL) {
+ printf("Unable to read from `%s'\n", fname);
+ return 0;
+ }
+
+ ret = get_dominfo_from_xml(xml, d);
+
+ free(xml);
+ fclose(file);
+
+ printf("XML:\n%s", xml);
+
+ return ret;
+}
+
+static void usage(void)
+{
+ printf("xml_parse_test -f [FILE | -] [--xml]\n"
+ "xml_parse_test -d domain [--uri URI] [--xml]\n"
+ "\n"
+ "-f,--file FILE Parse domain XML from file (or stdin if
-)\n"
+ "-d,--domain DOM Display dominfo for a domain from libvirt\n"
+ "-u,--uri URI Connect to libvirt with URI\n"
+ "-x,--xml Dump generated XML instead of summary\n"
+ "-h,--help Display this help message\n");
+}
+
+int main(int argc, char **argv)
+{
+ int c;
+ char *domain = NULL;
+ char *uri = "xen";
+ char *file = NULL;
+ bool xml = false;
+ struct domain *dominfo = NULL;
+ int ret;
+
+ static struct option lopts[] = {
+ {"domain", 1, 0, 'd'},
+ {"uri", 1, 0, 'u'},
+ {"xml", 0, 0, 'x'},
+ {"file", 1, 0, 'f'},
+ {"help", 0, 0, 'h'},
+ {0, 0, 0, 0}};
+
+ while (1) {
+ int optidx = 0;
+
+ c = getopt_long(argc, argv, "d:u:f:xh", lopts, &optidx);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 'd':
+ domain = optarg;
+ break;
+
+ case 'u':
+ uri = optarg;
+ break;
+
+ case 'f':
+ file = optarg;
+ break;
+
+ case 'x':
+ xml = true;
+ break;
+
+ case '?':
+ case 'h':
+ usage();
+ return c == '?';
+
+ };
+ }
+
+ if (file != NULL)
+ ret = dominfo_from_file(file, &dominfo);
+ else if (domain != NULL)
+ ret = dominfo_from_dom(uri, domain, &dominfo);
+ else {
+ printf("Need a data source (--domain or --file)\n");
+ return 1;
+ }
+
+ if (ret == 0) {
+ printf("Unable to get dominfo\n");
return 2;
}
- dom = virDomainLookupByName(conn, argv[1]);
- if (dom == NULL) {
- printf("Unable to lookup domain `%s'\n", argv[1]);
- return 3;
- }
-
- if (get_dominfo(dom, &dominfo) == 0) {
- printf("Failed to parse domain info\n");
- return 4;
- }
-
- printf("Parsed domain info\n");
-
- if ((argc > 3) && (argv[3][0] == 'x'))
+ if (xml)
print_domxml(dominfo, stdout);
else {
print_dominfo(dominfo, stdout);