File: hxname2id.c

package info (click to toggle)
html-xml-utils 7.7-1.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,540 kB
  • sloc: ansic: 11,213; sh: 7,996; lex: 243; makefile: 192; yacc: 125
file content (247 lines) | stat: -rwxr-xr-x 6,211 bytes parent folder | download | duplicates (3)
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
/*
 * Move target anchors to the element they belong to, i.e., look for
 * <FOO><A NAME=bar> and <FOO><A ID=bar> and replace it with <FOO
 * ID=bar><A>
 *
 * There is no attempt to check if the name is a valid SGML/XML token
 * or whether it is unique. The replacement is syntactical only.
 *
 * Copyright © 2004 World Wide Web Consortium
 * See http://www.w3.org/Consortium/Legal/copyright-software
 *
 * Author: Bert Bos <bert@w3.org>
 * Created: Dec 2004
 * Version: $Id: hxname2id.c,v 1.7 2017/11/24 09:50:25 bbos Exp $
 *
 **/
#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.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

#ifdef HAVE_ERRNO_H
#  include <errno.h>
#endif
#ifdef HAVE_SEARCH_H
#  include <search.h>
#else
#  include "search-freebsd.h"
#endif

#include "export.h"
#include "types.e"
#include "heap.e"
#include "tree.e"
#include "html.e"
#include "scan.e"
#include "dict.e"
#include "openurl.e"
#include "errexit.e"


static Tree tree;
static bool xml = false;			/* Use <empty /> convention */


/* 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);
}

/* start -- called before the first event is reported */
static void* start(void)
{
  tree = create();
  return NULL;
}
  
/* end -- called after the last event is reported */
static void end(void *clientdata)
{
  /* skip */
}

/* handle_comment -- called after a comment is parsed */
static void handle_comment(void *clientdata, string commenttext)
{
  tree = append_comment(tree, commenttext);
}

/* handle_text -- called after a tex chunk is parsed */
static void handle_text(void *clientdata, string text)
{
  tree = append_text(tree, text);
}

/* handle_declaration -- called after a declaration is parsed */
static void handle_decl(void *clientdata, string gi,
			string fpi, string url)
{
  tree = append_declaration(tree, gi, fpi, url);
}

/* handle_proc_instr -- called after a PI is parsed */
static void handle_pi(void *clientdata, string pi_text)
{
  tree = append_procins(tree, pi_text);
}

/* handle_starttag -- called after a start tag is parsed */
static void handle_starttag(void *clientdata, string name, pairlist attribs)
{
  tree = html_push(tree, name, attribs);
}

/* handle_emptytag -- called after an empty tag is parsed */
static void handle_emptytag(void *clientdata, string name, pairlist attribs)
{
  handle_starttag(clientdata, name, attribs);
}

/* handle_endtag -- called after an endtag is parsed (name may be "") */
static void handle_endtag(void *clientdata, string name)
{
  tree = html_pop(tree, name);
}

/* has_anchor_child -- check if the first thing in the element is an <A NAME> */
static bool has_anchor_child(Tree t, conststring *nameval)
{
  Tree h;

  /* Loop until either text or an element is found */
  for (h = t->children; h != NULL; h = h->sister) {
    switch (h->tp) {
    case Comment:		/* Skip these */
    case Procins:
      break;
    case Text:			/* Skip if whitespace, otherwise return false */
      if (! only_space(h->text)) return false;
      break;
    case Element:		/* true if <A NAME> or <A ID>, else false */
      return eq(h->name, "a") &&
	((*nameval = get_attrib(h, "id")) ||
	 (*nameval = get_attrib(h, "name")));
    default:
      assert(! "Cannot happen");
    }
  }
  return false;
}

/* process -- write the tree, add IDs at elements with an <A NAME> child */
static void process(Tree t, bool remove_anchor)
{
  Tree h;
  conststring nameval;
  bool remove_next_anchor = false;
  pairlist a;

  for (h = t->children; h != NULL; h = h->sister) {
    switch (h->tp) {
      case Text:
	printf("%s", h->text);
	break;
      case Comment:
	printf("<!--%s-->", h->text);
	break;
      case Declaration:
	printf("<!DOCTYPE %s", h->name);
	if (h->text) printf(" PUBLIC \"%s\"", h->text);
	if (h->url) printf(" %s\"%s\"", h->text ? "" : "SYSTEM ", h->url);
	printf(">");
	break;
      case Procins:
	printf("<?%s>", h->text);
	break;
      case Element:
	if (!get_attrib(h, "id") && has_anchor_child(h, &nameval)) {
	  /* Put the anchor on this element and remove it from the child */
	  set_attrib(h, "id", nameval);
	  remove_next_anchor = true;
	}
	printf("<%s", h->name);
	for (a = h->attribs; a != NULL; a = a->next) {
	  /* Print attribs, except id/name that the parent wants us to remove */
	  if (!remove_anchor || (!eq(a->name, "id") && !eq(a->name, "name"))) {
	    printf(" %s", a->name);
	    if (a->value != NULL) printf("=\"%s\"", a->value);
	  }
	}
	if (is_empty(h->name)) {
	  assert(h->children == NULL);
	  printf(xml ? " />" : ">");
	} else {
	  printf(">");
	  process(h, remove_next_anchor);
	  printf("</%s>", h->name);
	}
	break;
      case Root:
	assert(! "Cannot happen");
	break;
      default:
	assert(! "Cannot happen");
    }
  }
}

/* usage -- print usage message and exit */
static void usage(string name)
{
  errexit("Version %s\nUsage: %s [-x] [html-file]\n", VERSION, name);
}


int main(int argc, char *argv[])
{
  int i, status;

  /* 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);

  yyin = stdin;
  for (i = 1; i < argc; i++) {
    if (eq(argv[i], "-x")) {
      xml = true;
    } else if (eq(argv[i], "-?")) {
      usage(argv[0]);
    } else if (eq(argv[i], "-")) {
      /* yyin = stdin; */
    } else {
      yyin = fopenurl(argv[i], "r", &status);
      if (yyin == NULL) {perror(argv[1]); exit(2);}
      if (status != 200) errexit("%s : %s\n", argv[i], http_strerror(status));
    }
  }
  if (yyparse() != 0) exit(3);

  tree = get_root(tree);
  process(tree, false);
  tree_delete(tree);				/* Just to test memory mgmt */
  return 0;
}