1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-07-29 11:41:22 +03:00

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
This commit is contained in:
Ryan
2012-05-07 19:53:19 +08:00
committed by Daniel Veillard
parent 996449273f
commit 0cd29a3a25

View File

@ -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");
@ -3152,6 +3153,83 @@ xmlShell(xmlDocPtr doc, char *filename, xmlShellReadlineFunc input,
}
#ifdef LIBXML_XPATH_ENABLED
xmlXPathFreeObject(list);
#endif
} else {
xmlGenericError(xmlGenericErrorContext,
"%s: no such node\n", arg);
}
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,