#include "xml/from-txt.h"
#include "xml/listpcre.h"
#include "xml/gerror.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif

#define ___ {
#define ____ }

int
main (int argc, char** argv)
{
    xml_g_set_prgname(argv[0]); 

    if (argc < 3)
    
{
	g_printerr ("> %s filename xpath [options]\n"
		    "\t print the text enclosed by the xpath spec given\n"
		    "\t as the argument (abbreviated regex forms)\n"
		    "\t option: -text (enclose in <text> markups)\n"
		    "\t option: -node (enclose in nodes own markups)\n"
		    "\t option: -space (space as line separator)\n"
		    "\t option: -all  (print text of all matching nodes)\n",
		    g_get_prgname());
	return 1;
    }
___ gchar *filename = 0; const gchar *xpath = 0, *styleflag = "";
{   int i ; for (i=1; i < argc ; i++) 
{
	if (argv[i][0] == '-') 
{ styleflag = argv[i]; continue; }
if (! filename)
{ filename = argv[i]; continue; }
if (! xpath)
{ xpath = argv[i]; continue; }
g_warning ("extra argument on commandline, ignored: %s", argv[i]); }
}
___ xml_GNode* tree; GError* error = 0; tree = xml_g_markup_parse_file (0, filename, &error); if (error) xml_g_show_error (&error, "after parsing file:\n\t'%s'", filename); ___ GList* list = xml_path_pcre_list (tree, xpath); if (! list) g_error ("node not found: %s\n", xpath); ___ int style = 0; if (strstr (styleflag, "-text")) style |= 1; if (strstr (styleflag, "-space")) style |= 2; if (strstr (styleflag, "-node")) style |= 4; if (strstr (styleflag, "-all")) style |= 8; all: ___ xml_GNode* node = list->data; # define writ3(F,A,B,C) \ { write (F, A,strlen(A)); write (F, B,strlen(B)); write (F,C,strlen(C)); } if (node->text)
{
	if (style&1) write (STDOUT_FILENO, "<text>", 6);
	if (style&4) writ3 (STDOUT_FILENO, "<", node->name, ">");
	write (STDOUT_FILENO, 
	       node->text->str + node->off, node->end - node->off);
	if (style&4) writ3 (STDOUT_FILENO, "</", node->name, ">");
	if (style&1) write (STDOUT_FILENO, "</text>", 7);
	if (style) write (STDOUT_FILENO, ((style&2 &&list->next)?" ":"\n"), 1);
    }
else if (node->name[0] == '<')
{
	write (STDOUT_FILENO, node->name, strlen(node->name));
    }
else if (! style)
{
	g_printerr ("node has no text associated\n");
	return 2;
    }
___ GList* next = list->next; g_list_free_1 (list); list = next; if (list)
{
	if (style) goto all;
	g_printerr ("<!-- %i more hits -->\n", g_list_length (list));
	g_list_free (list);
    }
return 0; ____;____;____;____;____;____; }
/* 
   Local variables:
   c-file-style: "stroustrup"
   End:
 */