<libroxml  version="3.0.2" />
contact: tristan.lelong@libroxml.net
roxml_shell.c
Go to the documentation of this file.
1 
12 #include "roxml.h"
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 void print_help(void)
20 {
21  fprintf(stderr, "LGPL command line XML parser\n<tristan.lelong@libroxml.net>\n");
22 }
23 
24 void print_usage(const char *progname)
25 {
26  fprintf(stderr, "\nusage: %s [-q|-h] <filename> [/]<node1>/<node2>/<node3>/.../<nodeN>\n", progname);
27  fprintf(stderr, "-q|--quiet: activate quiet mode\n");
28  fprintf(stderr, "-c|--content: show content of node instead of name\n");
29  fprintf(stderr, "-h|--help: display this message\n");
30 }
31 
32 int main(int argc, char **argv)
33 {
34  int optind;
35  int quiet = 0;
36  int content = 0;
37  int j, max;
38  node_t *root;
39  node_t **ans;
40 
41  for (optind = 1; optind < argc; optind++) {
42  int option = 0;
43  if (argv[optind][0] == '-') {
44  if (strcmp(argv[optind], "--help") == 0) {
45  option = 'h';
46  } else if (strcmp(argv[optind], "--content") == 0) {
47  option = 'c';
48  } else if (strcmp(argv[optind], "--quiet") == 0) {
49  option = 'q';
50  } else if (strcmp(argv[optind], "-h") == 0) {
51  option = 'h';
52  } else if (strcmp(argv[optind], "-c") == 0) {
53  option = 'c';
54  } else if (strcmp(argv[optind], "-q") == 0) {
55  option = 'q';
56  }
57  } else {
58  break;
59  }
60  switch (option) {
61  case 'q':
62  quiet = 1;
63  break;
64  case 'c':
65  content = 1;
66  break;
67  case 'h':
68  print_help();
69  print_usage(argv[0]);
70  return EXIT_FAILURE;
71  break;
72  default:
73  print_usage(argv[0]);
74  return EXIT_FAILURE;
75  break;
76  }
77  }
78 
79  root = roxml_load_doc(argv[optind]);
80  if (root == NULL) {
81  if (!quiet)
82  perror("error parsing xml file");
83  goto error;
84  }
85 
86  if (argc < optind + 2)
87  ans = roxml_xpath(root, "/", &max);
88  else
89  ans = roxml_xpath(root, argv[optind + 1], &max);
90 
91  for (j = 0; j < max; j++) {
92  char *c = NULL;
93 
94  // Dump the XML subtree.
95  if (content) {
96  roxml_commit_fd(ans[j], 1, 1);
97  fprintf(stdout, "\n");
98  fflush(stdout);
99  continue;
100  }
101 
102  // Or display:
103  // - the node content for leaf nodes.
104  // - list of children otherwise.
105  c = roxml_get_content(ans[j], NULL, 0, NULL);
106  if (c && *c) {
107  if (!quiet)
108  fprintf(stdout, "ans[%d]: ", j);
109  fprintf(stdout, "%s\n", c);
110  } else if (c) {
111  char *s = NULL;
112  int nb_chld = roxml_get_chld_nb(ans[j]);
113  int i = 0;
114 
115  for (i = 0; i < nb_chld; i++) {
116  node_t *child = roxml_get_chld(ans[j], NULL, i);
117  node_t *ns = roxml_get_ns(child);
118  c = roxml_get_name(child, NULL, 0);
119  if (!quiet)
120  fprintf(stdout, "ans[%d]: ", j);
121  if (ns) {
122  s = roxml_get_name(ns, NULL, 0);
123  if (strlen(s))
124  fprintf(stdout, "%s:", s);
125  }
126  fprintf(stdout, "%s\n", c);
127  }
128  }
129  }
131 
132  roxml_close(root);
133 
134 error:
135  return 0;
136 }
ROXML_API node_t * roxml_get_ns(node_t *n)
namespace getter function
node_t structure
Definition: roxml_types.h:133
ROXML_API int roxml_get_chld_nb(node_t *n)
chlds number getter function
ROXML_API void roxml_release(void *data)
memory cleanning function
Definition: roxml_mem.c:109
ROXML_API node_t ** roxml_xpath(node_t *n, char *path, int *nb_ans)
exec path function
Definition: roxml_stub.c:167
ROXML_API void roxml_close(node_t *n)
unload function
Definition: roxml_core.c:25
ROXML_API node_t * roxml_load_doc(char *filename)
load function for files
Definition: roxml_file.c:90
header for libroxml.so
ROXML_API char * roxml_get_content(node_t *n, char *buffer, int bufsize, int *size)
content getter function
ROXML_API int roxml_commit_fd(node_t *n, int fd, int human)
sync to file descriptor function
Definition: roxml_commit.c:317
ROXML_API node_t * roxml_get_chld(node_t *n, char *name, int nth)
chld getter function
ROXML_API char * roxml_get_name(node_t *n, char *buffer, int size)
name getter function
#define RELEASE_ALL
Definition: roxml.h:198