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 340 341 342 343 344 345 346 347 348 349 350 351 352
|
/*
* incl - expand included files
*
* Searches for <!--include "file"--> and expands the referenced file
* in place. File may be a URL. Works recursively. Other accepted
* syntaxes:
*
* <!--include "file"-->
* <!--include 'file'-->
* <!--include file-->
* <!--begin-include "file"-->...<!--end-include-->
* <!--begin-include 'file'-->...<!--end-include-->
* <!--begin-include file-->...<!--end-include-->
*
* If there are no quotes, the file name may not include whitespace.
*
* Copyright 1994-2008 World Wide Web Consortium
* See http://www.w3.org/Consortium/Legal/copyright-software
*
* Author: Bert Bos
* Created: 2 Dec 1998
* Version: $Id: hxincl.c,v 1.8 2011/08/26 00:29:06 bbos Exp $
*
**/
#include <config.h>
#include <stdio.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <ctype.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 <stdlib.h>
#include <assert.h>
#include "export.h"
#include "types.e"
#include "errexit.e"
#include "html.e"
#include "scan.e"
#include "dict.e"
#include "openurl.e"
#include "heap.e"
#include "url.e"
#define INCLUDE "include"
#define BEGIN "begin-include"
#define END "end-include"
typedef struct _stack {
Boolean skipping;
struct _stack *next;
} *stack;
typedef enum {KNone, KIncl, KBegin, KEnd} Key;
static Boolean do_xml = False;
static Boolean final = False; /* Final means to remove the comments */
static Boolean has_error = False;
static string base = NULL;
static stack skipping = NULL;
static Dictionary substitutions = NULL;
/* push -- push a skipping state on the stack */
static void push(stack *skipping, Boolean s)
{
stack h;
new(h);
h->next = *skipping;
h->skipping = s;
*skipping = h;
}
/* pop -- pop a skipping state off the stack */
static void pop(stack *skipping)
{
stack h;
assert(*skipping);
h = *skipping;
*skipping = (*skipping)->next;
dispose(h);
}
/* top -- return value of top of skipping stack */
static Boolean top(stack skipping)
{
assert(skipping);
return skipping->skipping;
}
/* word_to_key -- check whether word s is one of the recognized keywords */
static Key word_to_key(const string s, int len)
{
if (len == sizeof(END) - 1 && strncmp(s, END, len)== 0) return KEnd;
if (len == sizeof(INCLUDE) - 1 && strncmp(s, INCLUDE, len)== 0) return KIncl;
if (len == sizeof(BEGIN) - 1 && strncmp(s, BEGIN, len)== 0) return KBegin;
return KNone;
}
/* add_substitution -- add a file name substitution to the dictionary */
static void add_substitution(const string assignment)
{
string s;
/* To do: handle file names containing '='. Add escapes? */
if (!substitutions) substitutions = dict_create(10);
if (!substitutions) errexit("Out of memory.\n");
if (!(s = strchr(assignment, '='))) errexit("No '=' found in option -s\n");
*s = '\0';
if (!dict_add(substitutions, assignment, s + 1)) errexit("Out of memory?\n");
*s = '=';
}
/* --------------- implements interface api.h -------------------------- */
/* handle_error -- called when a parse error occurred */
void handle_error(void *clientdata, const string s, int lineno)
{
fprintf(stderr, "%d: %s\n", lineno, s);
has_error = True;
}
/* start -- called before the first event is reported */
void* start(void)
{
push(&skipping, False); /* Start by not skipping */
return NULL;
}
/* end -- called after the last event is reported */
void end(void *clientdata)
{
assert(clientdata == NULL);
assert(top(skipping) == False);
/* skip */
}
/* handle_comment -- called after a comment is parsed */
void handle_comment(void *clientdata, string commenttext)
{
/* A push() occurs at <!--begin-include...--> and at include_file() */
/* A pop() occurs at <!--end-include...--> and at ENDINCL */
int i, j, status;
conststring s, url;
FILE *f;
Key key;
i = strspn(commenttext, " \t\n\r\f"); /* Skip whitespace */
j = strcspn(commenttext + i, " \t\n\r\f"); /* First word */
key = word_to_key(commenttext + i, j);
if (key == KEnd) { /* <!--end-include...--> */
/* Don't print anything, just pop a level */
pop(&skipping);
} else if (top(skipping)) { /* Are we already skipping? */
/* Don't print anything; push a level if this is a begin-include */
if (key == KBegin) push(&skipping, True);
} else if (key == KNone) { /* Unrecognized comment? */
/* Print the comment verbatim */
printf("<!--%s-->", commenttext);
} else { /* include or begin-include */
/* Push a level if this is a begin-include */
if (key == KBegin) push(&skipping, True);
/* Find start of file name */
i += j;
i += strspn(commenttext + i, " \t\n\r\f"); /* Skip whitespace */
/* Accept either "...", '...', or any string without spaces */
if (commenttext[i] == '"') {
j = strcspn(commenttext + i + 1, "\"");
url = newnstring(commenttext + i + 1, j);
} else if (commenttext[i] == '\'') {
j = strcspn(commenttext + (++i), "'");
url = newnstring(commenttext + i + 1, j);
} else {
j = strcspn(commenttext + i, " \t\n\r\f");
url = newnstring(commenttext + i, j);
}
/* If we have a substitution for it, use that instead */
if (substitutions && (s = dict_find(substitutions, url))) url = s;
/* Get the file and recursively parse it */
s = URL_s_absolutize(base, url);
if (!(f = fopenurl(s, "r", &status)))
perror(url);
else if (status != 200)
fprintf(stderr, "%s : %s\n", url, http_strerror(status));
else {
if (!final) printf("<!--%s %s-->", BEGIN, commenttext + i);
push(&skipping, False);
include_file(f);
}
dispose(url);
dispose(s);
}
free(commenttext);
}
/* handle_text -- called after a text chunk is parsed */
void handle_text(void *clientdata, string text)
{
if (top(skipping) == False) printf("%s", text);
free(text);
}
/* handle_decl -- called after a declaration is parsed */
void handle_decl(void *clientdata, string gi, string fpi, string url)
{
printf("<!DOCTYPE %s", gi);
if (fpi) printf(" PUBLIC \"%s\"", fpi);
if (url) printf(" %s\"%s\"", fpi ? "" : "SYSTEM ", url);
printf(">");
free(gi);
free(fpi);
free(url);
}
/* handle_pi -- called after a PI is parsed */
void handle_pi(void *clientdata, string pi_text)
{
if (top(skipping) == False) printf("<?%s>", pi_text);
free(pi_text);
}
/* handle_starttag -- called after a start tag is parsed */
void handle_starttag(void *clientdata, string name, pairlist attribs)
{
pairlist p;
if (top(skipping) == False) {
printf("<%s", name);
for (p = attribs; p; p = p->next) {
if (p->value != NULL) printf(" %s=\"%s\"", p->name, p->value);
else if (do_xml) printf(" %s=\"%s\"", p->name, p->name);
else printf(" %s", p->name);
}
printf(">");
}
free(name);
pairlist_delete(attribs);
}
/* handle_emptytag -- called after an empty tag is parsed */
void handle_emptytag(void *clientdata, string name, pairlist attribs)
{
pairlist p;
if (top(skipping) == False) {
printf("<%s", name);
for (p = attribs; p; p = p->next) {
if (p->value != NULL) printf(" %s=\"%s\"", p->name, p->value);
else if (do_xml) printf(" %s=\"%s\"", p->name, p->name);
else printf(" %s", p->name);
}
printf(do_xml ? " />" : ">");
}
free(name);
pairlist_delete(attribs);
}
/* handle_endtag -- called after an endtag is parsed (name may be "") */
void handle_endtag(void *clientdata, string name)
{
if (top(skipping) == False) printf("</%s>", name);
free(name);
}
/* handle_endincl -- called after the end of an included file is reached */
void handle_endincl(void *clientdata)
{
pop(&skipping);
/* Mark the end of the inclusion */
if (!final) printf("<!--%s-->", END);
}
/* --------------------------------------------------------------------- */
/* usage -- print usage message and exit */
static void usage(string prog)
{
fprintf(stderr, "Version %s\nUsage: %s [-x] [-b base] [-s name=subst ...] [file-or-url]\n",
VERSION, prog);
exit(2);
}
int main(int argc, char *argv[])
{
int c, status = 200;
/* Bind the parser callback routines to our handlers */
set_error_handler(handle_error);
set_start_handler(start);
set_end_handler(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);
set_endincl_handler(handle_endincl);
/* Parse command line arguments */
while ((c = getopt(argc, argv, ":xb:fs:")) != -1)
switch (c) {
case 'x': do_xml = True; break;
case 'b': base = optarg; break;
case 'f': final = True; break;
case 's': add_substitution(optarg); break;
default: usage(argv[0]);
}
if (optind == argc) {
yyin = stdin;
base = ".";
} else if (optind == argc - 1) {
yyin = fopenurl(argv[optind], "r", &status);
if (!base) base = argv[optind];
} else {
usage(argv[0]);
}
if (yyin == NULL) {perror(argv[optind]); exit(1);}
if (status != 200) errexit("%s : %s\n", argv[optind], http_strerror(status));
if (yyparse() != 0) exit(3);
return has_error ? 1 : 0;
}
|