1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*
* select -- extract elements matching a selector
*
* Assumes that class selectors (".foo") refer to an attribute called
* "class".
*
* Assumes that ID selectors ("#foo") refer to an attribute called
* "id".
*
* Options:
*
* -l language
*
* Sets the default language, in case the root element doesn't
* have an xml: lang attribute. Example: -l en. Default: none.
*
* -s separator
*
* A string to print after each match. Accepts C-like escapes.
* Example: -s '\n\n' to print an empty line after each match.
* Default: empty string.
*
* -i
*
* Match case-insensitively. Useful for HTML and some other
* SGML-based languages.
*
* -c
*
* Print content only. Without -c, the start and end tag of the
* matched element are printed as well; with -c only the contents
* of the matched element are printed.
*
* TODO: Escape double quotes when printing an attribute that matches
* ::attr().
*
* Part of HTML-XML-utils, see:
* http://www.w3.org/Tools/HTML-XML-utils/
*
* Copyright © 2001-2017 World Wide Web Consortium
* See http://www.w3.org/Consortium/Legal/copyright-software
*
* Author: Bert Bos <bert@w3.org>
* Created: 5 Jul 2001
* Version: $Id: hxselect.c,v 1.10 2017/11/24 09:50:25 bbos Exp $
*
**/
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <stdbool.h>
#if STDC_HEADERS
# include <string.h>
#else
# ifndef HAVE_STRCHR
# define strchr index
# define strrchr rindex
# endif
# ifndef HAVE_STRSTR
# include "strstr.e"
# endif
#endif
#include "types.e"
#include "tree.e"
#include "selector.e"
#include "heap.e"
#include "errexit.e"
#include "html.e"
#include "scan.e"
#include "selmatch.e"
static Tree tree = NULL; /* Current elt in tree */
static Selector selector; /* The selector to match */
static bool content_only = false; /* Omit start/end tag */
static string separator = ""; /* Printed between matches */
/* print_starttag -- print a start tag */
static void print_starttag(Node *n)
{
pairlist p;
printf("<%s", n->name);
for (p = n->attribs; p; p = p->next) printf(" %s=\"%s\"", p->name, p->value);
putchar('>');
}
/* printsep -- print the separator string, interpret escapes */
static void printsep(const string separator)
{
string s = separator;
int c;
while (*s) {
if (*s != '\\') putchar(*(s++));
else if ('0' <= *(++s) && *s <= '7') {
c = *s - '0';
if ('0' <= *(++s) && *s <= '7') {
c = 8 * c + *s - '0';
if ('0' <= *(++s) && *s <= '7') c = 8 * c + *s - '0';
}
putchar(c); s++;
} else
switch (*s) {
case '\0': putchar('\\'); break;
case 'n': putchar('\n'); s++; break;
case 't': putchar('\t'); s++; break;
case 'r': putchar('\r'); s++; break;
case 'f': putchar('\f'); s++; break;
default: putchar(*(s++)); break;
}
}
}
/* print_recursively -- print the element t, its children and sisters */
static void print_recursively(Tree t)
{
if (!t) return;
switch (t->tp) {
case Element:
print_starttag(t);
print_recursively(t->children);
printf("</%s>", t->name);
break;
case Text: printf("%s", t->text); break;
case Comment: printf("<!--%s-->", t->text); break;
case Declaration: assert(!"Cannot happen"); break;
case Procins: printf("<?%s>", t->text); break;
case Root: print_recursively(t->children); break;
default: assert(!"Cannot happen");
}
print_recursively(t->sister);
}
/* print_tree -- print the element t, with or without its tags */
static void print_tree(Tree t)
{
assert(t->tp == Element);
if (!content_only) print_starttag(t);
print_recursively(t->children);
if (!content_only) printf("</%s>", t->name);
printsep(separator);
}
/* match_pseudoelts -- print pseudo-elements of node t that match the selector */
static void match_pseudoelts(Tree t)
{
pairlist p;
assert(selector->pseudoelts);
/* ::attr() is the only pseudo-element we can handle so far */
if (selector->pseudoelts->type == AttrNode) {
assert(selector->combinator == Child && selector->context);
p = t->attribs;
while (p && !same(p->name, selector->pseudoelts->s)) p = p->next;
if (p && matches_sel(t, selector->context)) {
if (!content_only) printf("%s=\"", p->name);
printf("%s", p->value);
if (!content_only) printf("\"");
printsep(separator);
}
}
}
/* walk_tree -- find all nodes in the tree that match the selector */
static void walk_tree(Tree t)
{
if (!t) return;
switch (t->tp) {
case Element:
if (selector->pseudoelts) match_pseudoelts(t);
else if (matches_sel(t, selector)) print_tree(t);
walk_tree(t->children);
break;
case Text: break;
case Comment: break;
case Declaration: break;
case Procins: break;
case Root: walk_tree(t->children); break;
default: assert(!"Cannot happen");
}
walk_tree(t->sister);
}
/*********************** Parser callback API ***********************/
/* handle_error -- called when a parse error occurred */
static void handle_error(void *clientdata, const string s, int lineno)
{
fprintf(stderr, "%d: %s\n", lineno, s);
}
/* handle_start -- called before the first event is reported */
static void* handle_start(void)
{
tree = create();
return NULL;
}
/* handle_end -- called after the last event is reported */
static void handle_end(void *clientdata)
{
walk_tree(tree); /* Find all matches for the selector */
tree_delete(tree); /* Clean up */
}
/* handle_comment -- called after a comment is parsed */
static void handle_comment(void *clientdata, const string commenttext)
{
free(commenttext);
}
/* handle_text -- called after a text chunk is parsed */
static void handle_text(void *clientdata, const string text)
{
tree = tree_append_text(tree, text);
}
/* handle_decl -- called after a declaration is parsed */
static void handle_decl(void *clientdata, const string gi, const string fpi,
const string url)
{
free(gi);
free(fpi);
free(url);
}
/* handle_pi -- called after a Processing Instruction is parsed */
static void handle_pi(void *clientdata, const string pi_text)
{
free(pi_text);
}
/* handle_starttag -- called after a start tag is parsed */
static void handle_starttag(void *clientdata, const string name,
pairlist attribs)
{
tree = tree_push(tree, name, attribs); /* Add to tree */
free(name);
}
/* handle_emptytag -- called after an empty tag is parsed */
static void handle_emptytag(void *clientdata, const string name,
pairlist attribs)
{
tree = tree_push(tree, name, attribs); /* Add to tree */
tree = tree_pop(tree, name); /* Remove from tree again */
free(name);
}
/* handle_endtag -- called after an endtag is parsed (name may be "") */
static void handle_endtag(void *clientdata, const string name)
{
tree = tree_pop(tree, name);
free(name);
}
/* handle_endincl -- called at the end of an included file */
/* void handle_endincl(void *clientdata) */
/********************* End of parser callbacks *********************/
/* usage -- print usage message and exit */
static void usage(const conststring progname)
{
errexit("Usage: %s [-v] [-i] [-c] [-l language] [-s separator] selector\n",
progname);
}
int main(int argc, char *argv[])
{
string s;
int c;
/* Command line options */
while ((c = getopt(argc, argv, "icl:s:v")) != -1) {
switch (c) {
case 'c': content_only = true; break;
case 'l': init_language(optarg); break;
case 's': separator = optarg; break;
case 'i': set_case_insensitive(); break;
case 'v': printf("Version: %s %s\n", PACKAGE, VERSION); return 0;
case '?': usage(argv[0]); break;
default: assert(!"Cannot happen");
}
}
/* Bind the parser callback routines to our handlers */
set_error_handler(handle_error);
set_start_handler(handle_start);
set_end_handler(handle_end);
set_comment_handler(handle_comment);
set_text_handler(handle_text);
set_decl_handler(handle_decl);
set_pi_handler(handle_pi);
set_starttag_handler(handle_starttag);
set_emptytag_handler(handle_emptytag);
set_endtag_handler(handle_endtag);
/* Parse the selector */
if (optind >= argc) usage(argv[0]); /* Need at least 1 arg */
for (s = newstring(argv[optind++]); optind < argc; optind++)
strapp(&s, " ", argv[optind], NULL);
selector = parse_selector(s, &s);
if (*s) errexit("Syntax error at \"%c\"\n", *s);
/* Walk the tree */
yyin = stdin;
if (yyparse() != 0) exit(3);
return 0;
}
|