Permalink
Browse files

Add "whereis" command to xmllint shell

When playing with xpath in the xmllint shell, it's really handy to be
able to ask where the returned nodes live in the tree, in the same
way "pwd" asks where the current node lives.

The feature is actually quite easy to implement by combining the
functionality of the existing dir/ls and pwd commands (see proposed patch).

Example usage:

/ > whereis //last_name
/clinical_study/overall_official/last_name
/clinical_study/location/contact/last_name
/clinical_study/location/investigator/last_name
  • Loading branch information...
1 parent 9964492 commit 0cd29a3a256ef122920263cf0ef793eafb5f3faa Ryan committed with veillard May 7, 2012
Showing with 78 additions and 0 deletions.
  1. +78 −0 debugXML.c
View
@@ -2920,6 +2920,7 @@ xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n");
#endif /* LIBXML_XPATH_ENABLED */
fprintf(ctxt->output, "\tpwd display current working directory\n");
+ fprintf(ctxt->output, "\twhereis display absolute path of [path] or current working directory\n");
fprintf(ctxt->output, "\tquit leave shell\n");
#ifdef LIBXML_OUTPUT_ENABLED
fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n");
@@ -3159,6 +3160,83 @@ xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
}
ctxt->pctxt->node = NULL;
}
+ } else if (!strcmp(command, "whereis")) {
+ char dir[500];
+
+ if (arg[0] == 0) {
+ if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL))
+ fprintf(ctxt->output, "%s\n", dir);
+ } else {
+ ctxt->pctxt->node = ctxt->node;
+#ifdef LIBXML_XPATH_ENABLED
+ list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt);
+#else
+ list = NULL;
+#endif /* LIBXML_XPATH_ENABLED */
+ if (list != NULL) {
+ switch (list->type) {
+ case XPATH_UNDEFINED:
+ xmlGenericError(xmlGenericErrorContext,
+ "%s: no such node\n", arg);
+ break;
+ case XPATH_NODESET:{
+ int indx;
+
+ if (list->nodesetval == NULL)
+ break;
+
+ for (indx = 0;
+ indx < list->nodesetval->nodeNr;
+ indx++) {
+ if (!xmlShellPwd(ctxt, dir, list->nodesetval->
+ nodeTab[indx], NULL))
+ fprintf(ctxt->output, "%s\n", dir);
+ }
+ break;
+ }
+ case XPATH_BOOLEAN:
+ xmlGenericError(xmlGenericErrorContext,
+ "%s is a Boolean\n", arg);
+ break;
+ case XPATH_NUMBER:
+ xmlGenericError(xmlGenericErrorContext,
+ "%s is a number\n", arg);
+ break;
+ case XPATH_STRING:
+ xmlGenericError(xmlGenericErrorContext,
+ "%s is a string\n", arg);
+ break;
+ case XPATH_POINT:
+ xmlGenericError(xmlGenericErrorContext,
+ "%s is a point\n", arg);
+ break;
+ case XPATH_RANGE:
+ xmlGenericError(xmlGenericErrorContext,
+ "%s is a range\n", arg);
+ break;
+ case XPATH_LOCATIONSET:
+ xmlGenericError(xmlGenericErrorContext,
+ "%s is a range\n", arg);
+ break;
+ case XPATH_USERS:
+ xmlGenericError(xmlGenericErrorContext,
+ "%s is user-defined\n", arg);
+ break;
+ case XPATH_XSLT_TREE:
+ xmlGenericError(xmlGenericErrorContext,
+ "%s is an XSLT value tree\n",
+ arg);
+ break;
+ }
+#ifdef LIBXML_XPATH_ENABLED
+ xmlXPathFreeObject(list);
+#endif
+ } else {
+ xmlGenericError(xmlGenericErrorContext,
+ "%s: no such node\n", arg);
+ }
+ ctxt->pctxt->node = NULL;
+ }
} else if (!strcmp(command, "cd")) {
if (arg[0] == 0) {
ctxt->node = (xmlNodePtr) ctxt->doc;

0 comments on commit 0cd29a3

Please sign in to comment.