File: textcodes.w

package info (click to toggle)
texlive-bin 2018.20181218.49446-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 186,920 kB
  • sloc: ansic: 873,264; cpp: 311,278; perl: 82,918; sh: 23,243; makefile: 8,590; lex: 4,939; python: 4,462; pascal: 3,813; java: 3,569; yacc: 2,901; tcl: 2,379; exp: 2,031; xml: 844; ruby: 678; lisp: 398; sed: 331; asm: 140; csh: 46; awk: 30
file content (490 lines) | stat: -rw-r--r-- 11,446 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
% textcodes.w
%
% Copyright 2006-2010 Taco Hoekwater <taco@@luatex.org>
%
% This file is part of LuaTeX.
%
% LuaTeX is free software; you can redistribute it and/or modify it under
% the terms of the GNU General Public License as published by the Free
% Software Foundation; either version 2 of the License, or (at your
% option) any later version.
%
% LuaTeX is distributed in the hope that it will be useful, but WITHOUT
% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
% FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
% License for more details.
%
% You should have received a copy of the GNU General Public License along
% with LuaTeX; if not, see <http://www.gnu.org/licenses/>.

@ @c

#include "ptexlib.h"

@ catcodes @c

#define CATCODESTACK       8
#define CATCODEDEFAULT    12
#define CATCODE_MAX    32767

static sa_tree *catcode_heads = NULL;
static int catcode_max = 0;
static unsigned char *catcode_valid = NULL;

void set_cat_code(int h, int n, halfword v, quarterword gl)
{
    sa_tree_item sa_value = { 0 };
    sa_tree s = catcode_heads[h];
    if (h > catcode_max)
        catcode_max = h;
    if (s == NULL) {
        sa_value.int_value = CATCODEDEFAULT;
        s = new_sa_tree(CATCODESTACK, 1, sa_value);
        catcode_heads[h] = s;
    }
    sa_value.int_value = (int) v;
    set_sa_item(s, n, sa_value, gl);
}

halfword get_cat_code(int h, int n)
{
    sa_tree_item sa_value = { 0 };
    sa_tree s = catcode_heads[h];
    if (h > catcode_max)
        catcode_max = h;
    if (s == NULL) {
        sa_value.int_value = CATCODEDEFAULT;
        s = new_sa_tree(CATCODESTACK, 1, sa_value);
        catcode_heads[h] = s;
    }
    return (halfword) get_sa_item(s, n).int_value;
}

void unsave_cat_codes(int h, quarterword gl)
{
    int k;
    if (h > catcode_max)
        catcode_max = h;
    for (k = 0; k <= catcode_max; k++) {
        if (catcode_heads[k] != NULL)
            restore_sa_stack(catcode_heads[k], gl);
    }
}

static void initializecatcodes(void)
{
    sa_tree_item sa_value = { 0 };
    catcode_max = 0;
    catcode_heads = Mxmalloc_array(sa_tree, (CATCODE_MAX + 1));
    catcode_valid = Mxmalloc_array(unsigned char, (CATCODE_MAX + 1));
    memset(catcode_heads, 0, sizeof(sa_tree) * (CATCODE_MAX + 1));
    memset(catcode_valid, 0, sizeof(unsigned char) * (CATCODE_MAX + 1));
    catcode_valid[0] = 1;
    sa_value.int_value = CATCODEDEFAULT;
    catcode_heads[0] = new_sa_tree(CATCODESTACK, 1, sa_value);
}

static void dumpcatcodes(void)
{
    int total = 0;
    int k;
    for (k = 0; k <= catcode_max; k++) {
        if (catcode_valid[k]) {
            total++;
        }
    }
    dump_int(catcode_max);
    dump_int(total);
    for (k = 0; k <= catcode_max; k++) {
        if (catcode_valid[k]) {
            dump_int(k);
            dump_sa_tree(catcode_heads[k],"catcodes");
        }
    }
}

static void undumpcatcodes(void)
{
    int total, k, x;
    xfree(catcode_heads);
    xfree(catcode_valid);
    catcode_heads = Mxmalloc_array(sa_tree, (CATCODE_MAX + 1));
    catcode_valid = Mxmalloc_array(unsigned char, (CATCODE_MAX + 1));
    memset(catcode_heads, 0, sizeof(sa_tree) * (CATCODE_MAX + 1));
    memset(catcode_valid, 0, sizeof(unsigned char) * (CATCODE_MAX + 1));
    undump_int(catcode_max);
    undump_int(total);
    for (k = 0; k < total; k++) {
        undump_int(x);
        catcode_heads[x] = undump_sa_tree("catcodes");
        catcode_valid[x] = 1;
    }
}

int valid_catcode_table(int h)
{
    if (h <= CATCODE_MAX && h >= 0 && catcode_valid[h]) {
        return 1;
    }
    return 0;
}

void copy_cat_codes(int from, int to)
{
    if (from < 0 || from > CATCODE_MAX || catcode_valid[from] == 0) {
        uexit(1);
    }
    if (to > catcode_max)
        catcode_max = to;
    destroy_sa_tree(catcode_heads[to]);
    catcode_heads[to] = copy_sa_tree(catcode_heads[from]);
    catcode_valid[to] = 1;
}

void initex_cat_codes(int h)
{
    int k;
    if (h > catcode_max)
        catcode_max = h;
    destroy_sa_tree(catcode_heads[h]);
    catcode_heads[h] = NULL;
    set_cat_code(h, '\r', car_ret_cmd, 1);
    set_cat_code(h, ' ', spacer_cmd, 1);
    set_cat_code(h, '\\', escape_cmd, 1);
    set_cat_code(h, '%', comment_cmd, 1);
    set_cat_code(h, 127, invalid_char_cmd, 1);
    set_cat_code(h, 0, ignore_cmd, 1);
    set_cat_code(h, 0xFEFF, ignore_cmd, 1);
    for (k = 'A'; k <= 'Z'; k++) {
        set_cat_code(h, k, letter_cmd, 1);
        set_cat_code(h, k + 'a' - 'A', letter_cmd, 1);
    }
    catcode_valid[h] = 1;
}

static void freecatcodes(void)
{
    int k;
    for (k = 0; k <= catcode_max; k++) {
        if (catcode_valid[k]) {
            destroy_sa_tree(catcode_heads[k]);
        }
    }
    xfree(catcode_heads);
    xfree(catcode_valid);
}

@ lccodes @c

#define LCCODESTACK   8
#define LCCODEDEFAULT 0

static sa_tree lccode_head = NULL;

void set_lc_code(int n, halfword v, quarterword gl)
{
    sa_tree_item sa_value = { 0 };
    sa_value.int_value = (int) v;
    set_sa_item(lccode_head, n, sa_value, gl);
}

halfword get_lc_code(int n)
{
    return (halfword) get_sa_item(lccode_head, n).int_value;
}

static void unsavelccodes(quarterword gl)
{
    restore_sa_stack(lccode_head, gl);
}

static void initializelccodes(void)
{
    sa_tree_item sa_value = { 0 };
    sa_value.int_value = LCCODEDEFAULT;
    lccode_head = new_sa_tree(LCCODESTACK, 1, sa_value);
}

static void dumplccodes(void)
{
    dump_sa_tree(lccode_head,"lccodes");
}

static void undumplccodes(void)
{
    lccode_head = undump_sa_tree("lccodes");
}

static void freelccodes(void)
{
    destroy_sa_tree(lccode_head);
}

@ uccodes @c

#define UCCODESTACK   8
#define UCCODEDEFAULT 0

static sa_tree uccode_head = NULL;

void set_uc_code(int n, halfword v, quarterword gl)
{
    sa_tree_item sa_value = { 0 };
    sa_value.int_value = (int) v;
    set_sa_item(uccode_head, n, sa_value, gl);
}

halfword get_uc_code(int n)
{
    return (halfword) get_sa_item(uccode_head, n).int_value;
}

static void unsaveuccodes(quarterword gl)
{
    restore_sa_stack(uccode_head, gl);
}

static void initializeuccodes(void)
{
    sa_tree_item sa_value = { 0 };
    sa_value.int_value = UCCODEDEFAULT;
    uccode_head = new_sa_tree(UCCODESTACK, 1, sa_value);
}

static void dumpuccodes(void)
{
    dump_sa_tree(uccode_head,"uccodes");
}

static void undumpuccodes(void)
{
    uccode_head = undump_sa_tree("uccodes");
}

static void freeuccodes(void)
{
    destroy_sa_tree(uccode_head);
}

@ sfcodes @c

#define SFCODESTACK      8
#define SFCODEDEFAULT 1000

static sa_tree sfcode_head = NULL;

void set_sf_code(int n, halfword v, quarterword gl)
{
    sa_tree_item sa_value = { 0 };
    sa_value.int_value = (int) v;
    set_sa_item(sfcode_head, n, sa_value, gl);
}

halfword get_sf_code(int n)
{
    return (halfword) get_sa_item(sfcode_head, n).int_value;
}

static void unsavesfcodes(quarterword gl)
{
    restore_sa_stack(sfcode_head, gl);
}

static void initializesfcodes(void)
{
    sa_tree_item sa_value = { 0 };
    sa_value.int_value = SFCODEDEFAULT;
    sfcode_head = new_sa_tree(SFCODESTACK, 1, sa_value);
}

static void dumpsfcodes(void)
{
    dump_sa_tree(sfcode_head,"sfcodes");
}

static void undumpsfcodes(void)
{
    sfcode_head = undump_sa_tree("sfcodes");
}

static void freesfcodes(void)
{
    destroy_sa_tree(sfcode_head);
}

@ hjcodes @c

#define HJCODESTACK       8
#define HJCODEDEFAULT     0
#define HJCODE_MAX    16383 /* number of languages */

static sa_tree *hjcode_heads = NULL;
static int hjcode_max = 0;
static unsigned char *hjcode_valid = NULL;

@ Here we set codes but we don't initialize from lccodes.

@c void set_hj_code(int h, int n, halfword v, quarterword gl)
{
    sa_tree_item sa_value = { 0 };
    sa_tree s = hjcode_heads[h];
    if (h > hjcode_max)
        hjcode_max = h;
    if (s == NULL) {
        sa_value.int_value = HJCODEDEFAULT;
        s = new_sa_tree(HJCODESTACK, 1, sa_value);
        hjcode_heads[h] = s;
    }
    sa_value.int_value = (int) v;
    set_sa_item(s, n, sa_value, gl);
}

@ We just return the lccodes when nothing is set.

@c halfword get_hj_code(int h, int n)
{
    sa_tree s = hjcode_heads[h];
    if (s == NULL) {
        s = lccode_head;
    }
    return (halfword) get_sa_item(s, n).int_value;
}

@ We don't restore as we can have more languages in a paragraph
and hyphenation takes place at a later stage so we would get
weird grouping side effects .. so, one can overload settings
but management is then upto the used, so no:

@c
/*
    static void unsavehjcodes(quarterword gl) { }
*/

static void initializehjcodes(void)
{
    /*
        sa_tree_item sa_value = { 0 };
    */
    hjcode_max = 0;
    hjcode_heads = Mxmalloc_array(sa_tree, (HJCODE_MAX + 1));
    hjcode_valid = Mxmalloc_array(unsigned char, (HJCODE_MAX + 1));
    memset(hjcode_heads, 0, sizeof(sa_tree) * (HJCODE_MAX + 1));
    memset(hjcode_valid, 0, sizeof(unsigned char) * (HJCODE_MAX + 1));
    /*
        hjcode_valid[0] = 1;
        sa_value.int_value = HJCODEDEFAULT;
        hjcode_heads[0] = new_sa_tree(HJCODESTACK, 1, sa_value);
    */
}

void hj_codes_from_lc_codes(int h)
{
    sa_tree_item sa_value = { 0 };
    sa_tree s = hjcode_heads[h];
    if (s != NULL) {
        destroy_sa_tree(s);
    } else if (h > hjcode_max) {
        hjcode_max = h;
    }
    if (s == NULL) {
        sa_value.int_value = HJCODEDEFAULT;
        s = new_sa_tree(HJCODESTACK, 1, sa_value);
        hjcode_heads[h] = s;
    }
    hjcode_heads[h] = copy_sa_tree(lccode_head);
    hjcode_valid[h] = 1;
}

static void dumphjcodes(void)
{
    int total = 0;
    int k;
    for (k = 0; k <= hjcode_max; k++) {
        if (hjcode_valid[k]) {
            total++;
        }
    }
    dump_int(hjcode_max);
    dump_int(total);
    for (k = 0; k <= hjcode_max; k++) {
        if (hjcode_valid[k]) {
            dump_int(k);
            dump_sa_tree(hjcode_heads[k],"hjcodes");
        }
    }
}

static void undumphjcodes(void)
{
    int total, k, x;
    xfree(hjcode_heads);
    xfree(hjcode_valid);
    hjcode_heads = Mxmalloc_array(sa_tree, (HJCODE_MAX + 1));
    hjcode_valid = Mxmalloc_array(unsigned char, (HJCODE_MAX + 1));
    memset(hjcode_heads, 0, sizeof(sa_tree) * (HJCODE_MAX + 1));
    memset(hjcode_valid, 0, sizeof(unsigned char) * (HJCODE_MAX + 1));
    undump_int(hjcode_max);
    undump_int(total);
    for (k = 0; k < total; k++) {
        undump_int(x);
        hjcode_heads[x] = undump_sa_tree("hjcodes");
        hjcode_valid[x] = 1;
    }
}

static void freehjcodes(void)
{
    int k;
    for (k = 0; k <= hjcode_max; k++) {
        if (hjcode_valid[k]) {
            destroy_sa_tree(hjcode_heads[k]);
        }
    }
    xfree(hjcode_heads);
    xfree(hjcode_valid);
}

/* management */

void unsave_text_codes(quarterword grouplevel)
{
    unsavelccodes(grouplevel);
    unsaveuccodes(grouplevel);
    unsavesfcodes(grouplevel);
}

void initialize_text_codes(void)
{
    initializecatcodes();
    initializelccodes();
    initializeuccodes();
    initializesfcodes();
    initializehjcodes();
}

void free_text_codes(void)
{
    freecatcodes();
    freelccodes();
    freeuccodes();
    freesfcodes();
    freehjcodes();
}

void dump_text_codes(void)
{
    dumpcatcodes();
    dumplccodes();
    dumpuccodes();
    dumpsfcodes();
    dumphjcodes();
}

void undump_text_codes(void)
{
    undumpcatcodes();
    undumplccodes();
    undumpuccodes();
    undumpsfcodes();
    undumphjcodes();
}