~ubuntu-branches/ubuntu/vivid/tidy/vivid

« back to all changes in this revision

Viewing changes to src/entities.c

  • Committer: Bazaar Package Importer
  • Author(s): Jason Thomas
  • Date: 2002-03-08 10:58:30 UTC
  • Revision ID: james.westby@ubuntu.com-20020308105830-he5azqenf1sq4jak
Tags: 20020301-1
* New upstream version.
* fix '--add-xml-decl yes --add-xml-space yes' crash bug
  thanks to  Cesar Eduardo Barros <cesarb@nitnet.com.br>
  and upstream.
  (closes: #137124)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* entities.c -- recognize HTML ISO entities
 
2
 
 
3
  (c) 1998-2001 (W3C) MIT, INRIA, Keio University
 
4
  See tidy.c for the copyright notice.
 
5
 
 
6
  CVS Info :
 
7
 
 
8
    $Author: terry_teague $ 
 
9
    $Date: 2001/08/19 19:18:57 $ 
 
10
    $Revision: 1.6 $ 
 
11
 
 
12
*/
 
13
 
 
14
#include <stdio.h>
 
15
#include "platform.h"
 
16
#include "html.h"
 
17
 
 
18
#define HASHSIZE 731
 
19
 
 
20
struct nlist
 
21
{
 
22
    struct nlist *next;
 
23
    char *name;
 
24
    unsigned code;
 
25
};
 
26
 
 
27
static struct nlist *hashtab[HASHSIZE];
 
28
 
 
29
struct entity
 
30
{
 
31
    char *name;
 
32
    uint code;
 
33
} entities[] =
 
34
{
 
35
    {"nbsp",   160},
 
36
    {"iexcl",  161},
 
37
    {"cent",   162},
 
38
    {"pound",  163},
 
39
    {"curren", 164},
 
40
    {"yen",    165},
 
41
    {"brvbar", 166},
 
42
    {"sect",   167},
 
43
    {"uml",    168},
 
44
    {"copy",   169},
 
45
    {"ordf",   170},
 
46
    {"laquo",  171},
 
47
    {"not",    172},
 
48
    {"shy",    173},
 
49
    {"reg",    174},
 
50
    {"macr",   175},
 
51
    {"deg",    176},
 
52
    {"plusmn", 177},
 
53
    {"sup2",   178},
 
54
    {"sup3",   179},
 
55
    {"acute",  180},
 
56
    {"micro",  181},
 
57
    {"para",   182},
 
58
    {"middot", 183},
 
59
    {"cedil",  184},
 
60
    {"sup1",   185},
 
61
    {"ordm",   186},
 
62
    {"raquo",  187},
 
63
    {"frac14", 188},
 
64
    {"frac12", 189},
 
65
    {"frac34", 190},
 
66
    {"iquest", 191},
 
67
    {"Agrave", 192},
 
68
    {"Aacute", 193},
 
69
    {"Acirc",  194},
 
70
    {"Atilde", 195},
 
71
    {"Auml",   196},
 
72
    {"Aring",  197},
 
73
    {"AElig",  198},
 
74
    {"Ccedil", 199},
 
75
    {"Egrave", 200},
 
76
    {"Eacute", 201},
 
77
    {"Ecirc",  202},
 
78
    {"Euml",   203},
 
79
    {"Igrave", 204},
 
80
    {"Iacute", 205},
 
81
    {"Icirc",  206},
 
82
    {"Iuml",   207},
 
83
    {"ETH",    208},
 
84
    {"Ntilde", 209},
 
85
    {"Ograve", 210},
 
86
    {"Oacute", 211},
 
87
    {"Ocirc",  212},
 
88
    {"Otilde", 213},
 
89
    {"Ouml",   214},
 
90
    {"times",  215},
 
91
    {"Oslash", 216},
 
92
    {"Ugrave", 217},
 
93
    {"Uacute", 218},
 
94
    {"Ucirc",  219},
 
95
    {"Uuml",   220},
 
96
    {"Yacute", 221},
 
97
    {"THORN",  222},
 
98
    {"szlig",  223},
 
99
    {"agrave", 224},
 
100
    {"aacute", 225},
 
101
    {"acirc",  226},
 
102
    {"atilde", 227},
 
103
    {"auml",   228},
 
104
    {"aring",  229},
 
105
    {"aelig",  230},
 
106
    {"ccedil", 231},
 
107
    {"egrave", 232},
 
108
    {"eacute", 233},
 
109
    {"ecirc",  234},
 
110
    {"euml",   235},
 
111
    {"igrave", 236},
 
112
    {"iacute", 237},
 
113
    {"icirc",  238},
 
114
    {"iuml",   239},
 
115
    {"eth",    240},
 
116
    {"ntilde", 241},
 
117
    {"ograve", 242},
 
118
    {"oacute", 243},
 
119
    {"ocirc",  244},
 
120
    {"otilde", 245},
 
121
    {"ouml",   246},
 
122
    {"divide", 247},
 
123
    {"oslash", 248},
 
124
    {"ugrave", 249},
 
125
    {"uacute", 250},
 
126
    {"ucirc",  251},
 
127
    {"uuml",   252},
 
128
    {"yacute", 253},
 
129
    {"thorn",  254},
 
130
    {"yuml",   255},
 
131
    {"fnof",     402},
 
132
    {"Alpha",    913},
 
133
    {"Beta",     914},
 
134
    {"Gamma",    915},
 
135
    {"Delta",    916},
 
136
    {"Epsilon",  917},
 
137
    {"Zeta",     918},
 
138
    {"Eta",      919},
 
139
    {"Theta",    920},
 
140
    {"Iota",     921},
 
141
    {"Kappa",    922},
 
142
    {"Lambda",   923},
 
143
    {"Mu",       924},
 
144
    {"Nu",       925},
 
145
    {"Xi",       926},
 
146
    {"Omicron",  927},
 
147
    {"Pi",       928},
 
148
    {"Rho",      929},
 
149
    {"Sigma",    931},
 
150
    {"Tau",      932},
 
151
    {"Upsilon",  933},
 
152
    {"Phi",      934},
 
153
    {"Chi",      935},
 
154
    {"Psi",      936},
 
155
    {"Omega",    937},
 
156
    {"alpha",    945},
 
157
    {"beta",     946},
 
158
    {"gamma",    947},
 
159
    {"delta",    948},
 
160
    {"epsilon",  949},
 
161
    {"zeta",     950},
 
162
    {"eta",      951},
 
163
    {"theta",    952},
 
164
    {"iota",     953},
 
165
    {"kappa",    954},
 
166
    {"lambda",   955},
 
167
    {"mu",       956},
 
168
    {"nu",       957},
 
169
    {"xi",       958},
 
170
    {"omicron",  959},
 
171
    {"pi",       960},
 
172
    {"rho",      961},
 
173
    {"sigmaf",   962},
 
174
    {"sigma",    963},
 
175
    {"tau",      964},
 
176
    {"upsilon",  965},
 
177
    {"phi",      966},
 
178
    {"chi",      967},
 
179
    {"psi",      968},
 
180
    {"omega",    969},
 
181
    {"thetasym", 977},
 
182
    {"upsih",    978},
 
183
    {"piv",      982},
 
184
    {"bull",     8226},
 
185
    {"hellip",   8230},
 
186
    {"prime",    8242},
 
187
    {"Prime",    8243},
 
188
    {"oline",    8254},
 
189
    {"frasl",    8260},
 
190
    {"weierp",   8472},
 
191
    {"image",    8465},
 
192
    {"real",     8476},
 
193
    {"trade",    8482},
 
194
    {"alefsym",  8501},
 
195
    {"larr",     8592},
 
196
    {"uarr",     8593},
 
197
    {"rarr",     8594},
 
198
    {"darr",     8595},
 
199
    {"harr",     8596},
 
200
    {"crarr",    8629},
 
201
    {"lArr",     8656},
 
202
    {"uArr",     8657},
 
203
    {"rArr",     8658},
 
204
    {"dArr",     8659},
 
205
    {"hArr",     8660},
 
206
    {"forall",   8704},
 
207
    {"part",     8706},
 
208
    {"exist",    8707},
 
209
    {"empty",    8709},
 
210
    {"nabla",    8711},
 
211
    {"isin",     8712},
 
212
    {"notin",    8713},
 
213
    {"ni",       8715},
 
214
    {"prod",     8719},
 
215
    {"sum",      8721},
 
216
    {"minus",    8722},
 
217
    {"lowast",   8727},
 
218
    {"radic",    8730},
 
219
    {"prop",     8733},
 
220
    {"infin",    8734},
 
221
    {"ang",      8736},
 
222
    {"and",      8743},
 
223
    {"or",       8744},
 
224
    {"cap",      8745},
 
225
    {"cup",      8746},
 
226
    {"int",      8747},
 
227
    {"there4",   8756},
 
228
    {"sim",      8764},
 
229
    {"cong",     8773},
 
230
    {"asymp",    8776},
 
231
    {"ne",       8800},
 
232
    {"equiv",    8801},
 
233
    {"le",       8804},
 
234
    {"ge",       8805},
 
235
    {"sub",      8834},
 
236
    {"sup",      8835},
 
237
    {"nsub",     8836},
 
238
    {"sube",     8838},
 
239
    {"supe",     8839},
 
240
    {"oplus",    8853},
 
241
    {"otimes",   8855},
 
242
    {"perp",     8869},
 
243
    {"sdot",     8901},
 
244
    {"lceil",    8968},
 
245
    {"rceil",    8969},
 
246
    {"lfloor",   8970},
 
247
    {"rfloor",   8971},
 
248
    {"lang",     9001},
 
249
    {"rang",     9002},
 
250
    {"loz",      9674},
 
251
    {"spades",   9824},
 
252
    {"clubs",    9827},
 
253
    {"hearts",   9829},
 
254
    {"diams",    9830},
 
255
    {"quot",    34},
 
256
    {"amp",     38},
 
257
    {"apos",    39},
 
258
    {"lt",      60},
 
259
    {"gt",      62},
 
260
    {"OElig",   338},
 
261
    {"oelig",   339},
 
262
    {"Scaron",  352},
 
263
    {"scaron",  353},
 
264
    {"Yuml",    376},
 
265
    {"circ",    710},
 
266
    {"tilde",   732},
 
267
    {"ensp",    8194},
 
268
    {"emsp",    8195},
 
269
    {"thinsp",  8201},
 
270
    {"zwnj",    8204},
 
271
    {"zwj",     8205},
 
272
    {"lrm",     8206},
 
273
    {"rlm",     8207},
 
274
    {"ndash",   8211},
 
275
    {"mdash",   8212},
 
276
    {"lsquo",   8216},
 
277
    {"rsquo",   8217},
 
278
    {"sbquo",   8218},
 
279
    {"ldquo",   8220},
 
280
    {"rdquo",   8221},
 
281
    {"bdquo",   8222},
 
282
    {"dagger",  8224},
 
283
    {"Dagger",  8225},
 
284
    {"permil",  8240},
 
285
    {"lsaquo",  8249},
 
286
    {"rsaquo",  8250},
 
287
    {"euro",    8364},
 
288
    {null,      0}
 
289
};
 
290
 
 
291
static unsigned hash(char *s)
 
292
{
 
293
    uint hashval;
 
294
 
 
295
    for (hashval = 0; *s != '\0'; s++)
 
296
        hashval = *s + 31*hashval;
 
297
 
 
298
    return hashval % HASHSIZE;
 
299
}
 
300
 
 
301
static struct nlist *lookup(char *s)
 
302
{
 
303
    struct nlist *np;
 
304
 
 
305
    for (np = hashtab[hash(s)]; np != null; np = np->next)
 
306
        if (wstrcmp(s, np->name) == 0)
 
307
            return np;
 
308
    return null;
 
309
}
 
310
 
 
311
static struct nlist *install(char *name, uint code)
 
312
{
 
313
    struct nlist *np;
 
314
    uint hashval;
 
315
 
 
316
    if ((np = lookup(name)) == null)
 
317
    {
 
318
        np = (struct nlist *)MemAlloc(sizeof(*np));
 
319
 
 
320
        if (np == null || (np->name = wstrdup(name)) == null)
 
321
            return null;
 
322
 
 
323
        hashval = hash(name);
 
324
        np->next = hashtab[hashval];
 
325
        hashtab[hashval] = np;
 
326
    }
 
327
 
 
328
    np->code = code;
 
329
    return np;
 
330
}
 
331
 
 
332
 
 
333
/* entity starting with "&" returns zero on error */
 
334
uint EntityCode(char *name)
 
335
{
 
336
    int c;
 
337
    struct nlist *np;
 
338
 
 
339
    /* numeric entitity: name = "&#" followed by number */
 
340
    if (name[1] == '#')
 
341
    {
 
342
        c = 0;  /* zero on missing/bad number */
 
343
 
 
344
        /* 'x' prefix denotes hexadecimal number format */
 
345
        if (name[2] == 'x' || (!XmlTags && name[2] == 'X')) /* #427833 - fix by Bj�rn H�hrmann 05 Jun 01 */
 
346
            sscanf(name+3, "%x", &c);
 
347
        else
 
348
            sscanf(name+2, "%d", &c);
 
349
 
 
350
        return c;
 
351
    }
 
352
 
 
353
   /* Named entity: name ="&" followed by a name */
 
354
    if ((np = lookup(name+1)))
 
355
        return np->code;
 
356
 
 
357
    return 0;   /* zero signifies unknown entity name */
 
358
}
 
359
 
 
360
void InitEntities(void)
 
361
{
 
362
    struct entity *ep;
 
363
    
 
364
    for(ep = entities; ep->name != null; ++ep)
 
365
        install(ep->name, ep->code);
 
366
}
 
367
 
 
368
void FreeEntities(void)
 
369
{
 
370
    struct nlist *prev, *next;
 
371
    int i;
 
372
 
 
373
    for (i = 0; i < HASHSIZE; ++i)
 
374
    {
 
375
        prev = null;
 
376
        next = hashtab[i];
 
377
 
 
378
        while(next)
 
379
        {
 
380
            prev = next->next;
 
381
            MemFree(next->name);
 
382
            MemFree(next);
 
383
            next = prev;
 
384
        }
 
385
 
 
386
        hashtab[i] = null;
 
387
    }
 
388
}
 
389
 
 
390
 
 
391
char *EntityName(uint n)
 
392
{
 
393
    struct entity *ep;
 
394
    
 
395
    for(ep = entities; ep->name != null; ++ep)
 
396
    {
 
397
        if (ep->code == n)
 
398
            return ep->name;
 
399
    }
 
400
 
 
401
    return null;
 
402
}