[xml] patch for adding "shell" scripting



The xmlShell is a really cool feature. I was using my guile libxmlxsl
module for navigating doms but now I've discovered the xmlShell I
don't need to.

Another thing I use my guile code is for scripting small xpath
extractions from xml documents. xmllint won't currently run a script
from the command line.

I've patched it so that it will. My patch is inlined at the end of
this email.

Once you've patched and rebuilt you can do this:


   xmllint --shellx 'cd someroot/somelement ; ls ; cat anotherchild'

on an xml file.

I wanted to use the --shell parameter but because xmllint doesn't use
getopt that proved a bit too difficult.

I'd be interested in adding getopt support to xmllint if it was going
to be accepted. 


Anyway, here's the uni-diff:

--- /home/nferrier/projects/libxmlxsl/gnome-xml/xmllint.c       Thu Aug 28 13:32:04 2003
+++ /home/nferrier/projects/libxml2-2.5.7/xmllint.c     Thu Sep  4 18:22:34 2003
@@ -525,6 +522,22 @@
  *                                                                     *
  ************************************************************************/
 #ifdef LIBXML_DEBUG_ENABLED
+
+static char* (*xmlShellScriptSourcer)(char*);
+
+// Read the xmlShell commands from a script string
+static char *cmdLineScript;
+
+static char*
+xmlShellScriptReader (char* prompt)
+{
+  char* scriptLine = strdup(strsep (&cmdLineScript, ";"));
+  if (scriptLine != NULL)
+      if (strlen(scriptLine) < 1)
+          return NULL;
+  return (scriptLine);
+}
+
 /**
  * xmlShellReadline:
  * @prompt:  the prompt value
@@ -965,7 +953,7 @@
      * shell interaction
      */
     if (shell)  
-        xmlShell(doc, filename, xmlShellReadline, stdout);
+      xmlShell(doc, filename, xmlShellScriptSourcer, stdout);
 #endif
 
     /*
@@ -1297,6 +1233,7 @@
 #ifdef LIBXML_DEBUG_ENABLED
     printf("\t--debug : dump a debug tree of the in-memory document\n");
     printf("\t--shell : run a navigating shell\n");
+    printf("\t--shellx script : run a navigating shell\n");
     printf("\t--debugent : debug the entities defined in the document\n");
 #else
     printf("\t--debug : dump the nodes content when using --stream\n");
@@ -1392,11 +1329,26 @@
            debug++;
        else
 #ifdef LIBXML_DEBUG_ENABLED
-       if ((!strcmp(argv[i], "-shell")) ||
-                (!strcmp(argv[i], "--shell"))) {
-           shell++;
+        if ((!strcmp(argv[i], "-shell")) ||
+            (!strcmp(argv[i], "--shell"))) {
+            shell++;
             noout = 1;
-        } else 
+            xmlShellScriptSourcer = xmlShellReadline;
+        }
+        else if ((!strncmp(argv[i], "-shellx", 7)) ||
+                 (!strncmp(argv[i], "--shellx", 8))) {
+            shell++;
+            noout = 1;
+            if (++i < argc) {
+                cmdLineScript = (char*) malloc(strlen(argv[i]) + 2);
+                strncpy(cmdLineScript, argv[i], strlen(argv[i]));
+                // Put the terminator char at the end.
+                cmdLineScript[strlen(argv[i])] = ';';
+                cmdLineScript[strlen(argv[i]) + 1] = 0;
+                xmlShellScriptSourcer = xmlShellScriptReader;
+            }
+        }
+        else 
 #endif
        if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
            copy++;



--------------
Nic Ferrier 
http://www.tapsellferrier.co.uk




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]