| 1 |
/* |
| 2 |
|
| 3 |
primitive.w |
| 4 |
|
| 5 |
Copyright 2008-2010 Taco Hoekwater <taco@@luatex.org> |
| 6 |
|
| 7 |
This file is part of LuaTeX. |
| 8 |
|
| 9 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 10 |
the terms of the GNU General Public License as published by the Free |
| 11 |
Software Foundation; either version 2 of the License, or (at your |
| 12 |
option) any later version. |
| 13 |
|
| 14 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 15 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 17 |
License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License along |
| 20 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
*/ |
| 23 |
|
| 24 |
#include "ptexlib.h" |
| 25 |
|
| 26 |
/*tex |
| 27 |
|
| 28 |
Control sequences are stored and retrieved by means of a fairly standard hash |
| 29 |
table algorithm called the method of ``coalescing lists'' (cf.\ Algorithm 6.4C in |
| 30 |
{\sl The Art of Computer Programming\/}). Once a control sequence enters the |
| 31 |
table, it is never removed, because there are complicated situations involving |
| 32 |
\.{\\gdef} where the removal of a control sequence at the end of a group would be |
| 33 |
a mistake preventable only by the introduction of a complicated reference-count |
| 34 |
mechanism. |
| 35 |
|
| 36 |
The actual sequence of letters forming a control sequence identifier is stored in |
| 37 |
the |str_pool| array together with all the other strings. An auxiliary array |
| 38 |
|hash| consists of items with two halfword fields per word. The first of these, |
| 39 |
called |next(p)|, points to the next identifier belonging to the same coalesced |
| 40 |
list as the identifier corresponding to~|p|; and the other, called |text(p)|, |
| 41 |
points to the |str_start| entry for |p|'s identifier. If position~|p| of the hash |
| 42 |
table is empty, we have |text(p)=0|; if position |p| is either empty or the end |
| 43 |
of a coalesced hash list, we have |next(p)=0|. An auxiliary pointer variable |
| 44 |
called |hash_used| is maintained in such a way that all locations |p>=hash_used| |
| 45 |
are nonempty. The global variable |cs_count| tells how many multiletter control |
| 46 |
sequences have been defined, if statistics are being kept. |
| 47 |
|
| 48 |
A global boolean variable called |no_new_control_sequence| is set to |true| |
| 49 |
during the time that new hash table entries are forbidden. |
| 50 |
|
| 51 |
*/ |
| 52 |
|
| 53 |
/*tex The hash table: */ |
| 54 |
|
| 55 |
two_halves *hash; |
| 56 |
|
| 57 |
/*tex Allocation pointer for |hash|: */ |
| 58 |
|
| 59 |
halfword hash_used; |
| 60 |
|
| 61 |
/*tex |hash_extra=hash| above |eqtb_size|: */ |
| 62 |
|
| 63 |
int hash_extra; |
| 64 |
|
| 65 |
/*tex Maximum of the hash array: */ |
| 66 |
|
| 67 |
halfword hash_top; |
| 68 |
|
| 69 |
/*tex Pointer to next high hash location: */ |
| 70 |
|
| 71 |
halfword hash_high; |
| 72 |
|
| 73 |
/*tex Are new identifiers legal? */ |
| 74 |
|
| 75 |
boolean no_new_control_sequence; |
| 76 |
|
| 77 |
/*tex Total number of known identifiers: */ |
| 78 |
|
| 79 |
int cs_count; |
| 80 |
|
| 81 |
/*tex Test if all positions are occupied: */ |
| 82 |
|
| 83 |
#define hash_is_full (hash_used==hash_base) |
| 84 |
|
| 85 |
/*tex |
| 86 |
|
| 87 |
\.{\\primitive} support needs a few extra variables and definitions, |
| 88 |
like: |
| 89 |
|
| 90 |
*/ |
| 91 |
|
| 92 |
#define prim_base 1 |
| 93 |
|
| 94 |
/*tex |
| 95 |
|
| 96 |
The arrays |prim| and |prim_eqtb| are used for name -> cmd,chr lookups. The are |
| 97 |
modelled after |hash| and |eqtb|, except that primitives do not have an |
| 98 |
|eq_level|, that field is replaced by |origin|. |
| 99 |
|
| 100 |
*/ |
| 101 |
|
| 102 |
/*tex Link for coalesced lists: */ |
| 103 |
|
| 104 |
#define prim_next(a) prim[(a)].lhfield |
| 105 |
|
| 106 |
/*tex String number for control sequence name: */ |
| 107 |
|
| 108 |
#define prim_text(a) prim[(a)].rh |
| 109 |
|
| 110 |
/*tex Test if all positions are occupied: */ |
| 111 |
|
| 112 |
#define prim_is_full (prim_used==prim_base) |
| 113 |
|
| 114 |
#define prim_origin_field(a) (a).hh.b1 |
| 115 |
|
| 116 |
#define prim_eq_type_field(a) (a).hh.b0 |
| 117 |
|
| 118 |
#define prim_equiv_field(a) (a).hh.rh |
| 119 |
|
| 120 |
/*tex Level of definition: */ |
| 121 |
|
| 122 |
#define prim_origin(a) prim_origin_field(prim_eqtb[(a)]) |
| 123 |
|
| 124 |
/*tex Command code for equivalent: */ |
| 125 |
|
| 126 |
#define prim_eq_type(a) prim_eq_type_field(prim_eqtb[(a)]) |
| 127 |
|
| 128 |
/*tex Equivalent value: */ |
| 129 |
|
| 130 |
#define prim_equiv(a) prim_equiv_field(prim_eqtb[(a)]) |
| 131 |
|
| 132 |
/*tex Allocation pointer for |prim|: */ |
| 133 |
|
| 134 |
static pointer prim_used; |
| 135 |
|
| 136 |
/*tex The primitives table: */ |
| 137 |
|
| 138 |
static two_halves prim[(prim_size + 1)]; |
| 139 |
|
| 140 |
static memory_word prim_eqtb[(prim_size + 1)]; |
| 141 |
|
| 142 |
/*tex |
| 143 |
|
| 144 |
The array |prim_data| works the other way around, it is used for cmd,chr -> name |
| 145 |
lookups. |
| 146 |
|
| 147 |
*/ |
| 148 |
|
| 149 |
typedef struct prim_info { |
| 150 |
/*tex Number of name entries: */ |
| 151 |
halfword subids; |
| 152 |
/*tex Offset to be used for |chr_code|s: */ |
| 153 |
halfword offset; |
| 154 |
/*tex Array of names: */ |
| 155 |
str_number *names; |
| 156 |
} prim_info; |
| 157 |
|
| 158 |
static prim_info prim_data[(last_cmd + 1)]; |
| 159 |
|
| 160 |
/*tex |
| 161 |
|
| 162 |
Initialize the memory arrays: |
| 163 |
|
| 164 |
*/ |
| 165 |
|
| 166 |
void init_primitives(void) |
| 167 |
{ |
| 168 |
int k; |
| 169 |
memset(prim_data, 0, (sizeof(prim_info) * (last_cmd + 1))); |
| 170 |
memset(prim, 0, (sizeof(two_halves) * (prim_size + 1))); |
| 171 |
memset(prim_eqtb, 0, (sizeof(memory_word) * (prim_size + 1))); |
| 172 |
for (k = 0; k <= prim_size; k++) { |
| 173 |
prim_eq_type(k) = undefined_cs_cmd; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/*tex Nothing is used (yet). */ |
| 178 |
|
| 179 |
void ini_init_primitives(void) |
| 180 |
{ |
| 181 |
prim_used = prim_size; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
/*tex |
| 186 |
|
| 187 |
The value of |hash_prime| should be roughly 85\%! of |hash_size|, and it should |
| 188 |
be a prime number. The theory of hashing tells us to expect fewer than two table |
| 189 |
probes, on the average, when the search is successful. [See J.~S. Vitter, {\sl |
| 190 |
Journal of the ACM\/ \bf30} (1983), 231--258.] @^Vitter, Jeffrey Scott@> |
| 191 |
|
| 192 |
*/ |
| 193 |
|
| 194 |
static halfword compute_hash(const char *j, unsigned int l, halfword prime_number) |
| 195 |
{ |
| 196 |
int k; |
| 197 |
halfword h = (unsigned char) *j; |
| 198 |
for (k = 1; k <= (int)(l - 1); k++) { |
| 199 |
h = h + h + (unsigned char) *(j + k); |
| 200 |
while (h >= prime_number) { |
| 201 |
h = h - prime_number; |
| 202 |
} |
| 203 |
} |
| 204 |
return h; |
| 205 |
} |
| 206 |
|
| 207 |
/*tex |
| 208 |
|
| 209 |
Here is the subroutine that searches the primitive table for an identifier. |
| 210 |
|
| 211 |
*/ |
| 212 |
|
| 213 |
pointer prim_lookup(str_number s) |
| 214 |
{ |
| 215 |
/*tex The hash code: */ |
| 216 |
int h; |
| 217 |
/*tex The index in the |hash| array: */ |
| 218 |
pointer p; |
| 219 |
unsigned char *j; |
| 220 |
unsigned l; |
| 221 |
if (s < STRING_OFFSET) { |
| 222 |
p = s; |
| 223 |
if ((p < 0) || (get_prim_eq_type(p) == undefined_cs_cmd)) { |
| 224 |
p = undefined_primitive; |
| 225 |
} |
| 226 |
} else { |
| 227 |
j = str_string(s); |
| 228 |
l = (unsigned) str_length(s); |
| 229 |
h = compute_hash((char *) j, l, prim_prime); |
| 230 |
/*tex We start searching here; note that |0<=h<hash_prime|. */ |
| 231 |
p = h + prim_base; |
| 232 |
while (1) { |
| 233 |
if (prim_text(p) > 0) |
| 234 |
if (str_length(prim_text(p)) == l) |
| 235 |
if (str_eq_str(prim_text(p), s)) |
| 236 |
goto FOUND; |
| 237 |
if (prim_next(p) == 0) { |
| 238 |
if (no_new_control_sequence) { |
| 239 |
p = undefined_primitive; |
| 240 |
} else { |
| 241 |
/*tex Insert a new primitive after |p|, then make |p| point to it. */ |
| 242 |
if (prim_text(p) > 0) { |
| 243 |
do { |
| 244 |
/*tex Search for an empty location in |prim| */ |
| 245 |
if (prim_is_full) { |
| 246 |
overflow("primitive size", prim_size); |
| 247 |
} |
| 248 |
decr(prim_used); |
| 249 |
} while (prim_text(prim_used) != 0); |
| 250 |
prim_next(p) = prim_used; |
| 251 |
p = prim_used; |
| 252 |
} |
| 253 |
prim_text(p) = s; |
| 254 |
} |
| 255 |
goto FOUND; |
| 256 |
} |
| 257 |
p = prim_next(p); |
| 258 |
} |
| 259 |
} |
| 260 |
FOUND: |
| 261 |
return p; |
| 262 |
} |
| 263 |
|
| 264 |
/*tex |
| 265 |
|
| 266 |
How to test a csname for primitive-ness? |
| 267 |
|
| 268 |
*/ |
| 269 |
|
| 270 |
boolean is_primitive(str_number csname) |
| 271 |
{ |
| 272 |
int n, m; |
| 273 |
char *ss; |
| 274 |
m = prim_lookup(csname); |
| 275 |
ss = makecstring(csname); |
| 276 |
n = string_lookup(ss, str_length(csname)); |
| 277 |
free(ss); |
| 278 |
return ((n != undefined_cs_cmd) && (m != undefined_primitive) && |
| 279 |
(eq_type(n) == prim_eq_type(m)) && (equiv(n) == prim_equiv(m))); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
/*tex |
| 284 |
|
| 285 |
A few simple accessors. |
| 286 |
|
| 287 |
*/ |
| 288 |
|
| 289 |
quarterword get_prim_eq_type(int p) |
| 290 |
{ |
| 291 |
return prim_eq_type(p); |
| 292 |
} |
| 293 |
|
| 294 |
quarterword get_prim_origin(int p) |
| 295 |
{ |
| 296 |
return prim_origin(p); |
| 297 |
} |
| 298 |
|
| 299 |
halfword get_prim_equiv(int p) |
| 300 |
{ |
| 301 |
return prim_equiv(p); |
| 302 |
} |
| 303 |
|
| 304 |
str_number get_prim_text(int p) |
| 305 |
{ |
| 306 |
return prim_text(p); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
/*tex |
| 311 |
|
| 312 |
Dumping and undumping. |
| 313 |
|
| 314 |
*/ |
| 315 |
|
| 316 |
void dump_primitives(void) |
| 317 |
{ |
| 318 |
int p, q; |
| 319 |
for (p = 0; p <= prim_size; p++) { |
| 320 |
dump_hh(prim[p]); |
| 321 |
} |
| 322 |
for (p = 0; p <= prim_size; p++) { |
| 323 |
dump_wd(prim_eqtb[p]); |
| 324 |
} |
| 325 |
for (p = 0; p <= last_cmd; p++) { |
| 326 |
dump_int(prim_data[p].offset); |
| 327 |
dump_int(prim_data[p].subids); |
| 328 |
for (q = 0; q < prim_data[p].subids; q++) { |
| 329 |
dump_int(prim_data[p].names[q]); |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
void undump_primitives(void) |
| 335 |
{ |
| 336 |
int p, q; |
| 337 |
for (p = 0; p <= prim_size; p++) { |
| 338 |
undump_hh(prim[p]); |
| 339 |
} |
| 340 |
for (p = 0; p <= prim_size; p++) { |
| 341 |
undump_wd(prim_eqtb[p]); |
| 342 |
} |
| 343 |
for (p = 0; p <= last_cmd; p++) { |
| 344 |
undump_int(prim_data[p].offset); |
| 345 |
undump_int(prim_data[p].subids); |
| 346 |
if (prim_data[p].subids > 0) { |
| 347 |
prim_data[p].names = (str_number *) xmalloc((unsigned) ((unsigned) prim_data[p].subids * sizeof(str_number *))); |
| 348 |
for (q = 0; q < prim_data[p].subids; q++) { |
| 349 |
undump_int(prim_data[p].names[q]); |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
/*tex |
| 356 |
|
| 357 |
We need to put \TeX's ``primitive'' control sequences into the hash table, |
| 358 |
together with their command code (which will be the |eq_type|) and an operand |
| 359 |
(which will be the |equiv|). The |primitive| procedure does this, in a way that |
| 360 |
no \TeX\ user can. The global value |cur_val| contains the new |eqtb| pointer |
| 361 |
after |primitive| has acted. |
| 362 |
|
| 363 |
Because the definitions of the actual user-accessible name of a primitive can be |
| 364 |
postponed until runtime, the function |primitive_def| is needed that does nothing |
| 365 |
except creating the control sequence name. |
| 366 |
|
| 367 |
*/ |
| 368 |
|
| 369 |
void primitive_def(const char *s, size_t l, quarterword c, halfword o) |
| 370 |
{ |
| 371 |
int nncs = no_new_control_sequence; |
| 372 |
no_new_control_sequence = false; |
| 373 |
/*tex This creates the |text()| string: */ |
| 374 |
cur_val = string_lookup(s, l); |
| 375 |
no_new_control_sequence = nncs; |
| 376 |
eq_level(cur_val) = level_one; |
| 377 |
eq_type(cur_val) = c; |
| 378 |
equiv(cur_val) = o; |
| 379 |
} |
| 380 |
|
| 381 |
/*tex |
| 382 |
|
| 383 |
The function |store_primitive_name| sets up the bookkeeping for the reverse |
| 384 |
lookup. It is quite paranoid, because it is easy to mess this up accidentally. |
| 385 |
|
| 386 |
The |offset| is needed because sometimes character codes (in |o|) are indices |
| 387 |
into |eqtb| or are offset by a magical value to make sure they do not conflict |
| 388 |
with something else. We don't want the |prim_data[c].names| to have too many |
| 389 |
entries as it will just be wasted room, so |offset| is substracted from |o| |
| 390 |
because creating or accessing the array. The |assert(idx<=0xFFFF)| is not |
| 391 |
strictly needed, but it helps catch errors of this kind. |
| 392 |
|
| 393 |
*/ |
| 394 |
|
| 395 |
static void store_primitive_name(str_number s, quarterword c, halfword o, halfword offset) |
| 396 |
{ |
| 397 |
int idx; |
| 398 |
/* |
| 399 |
if (prim_data[c].offset != 0 && prim_data[c].offset != offset) { |
| 400 |
assert(false); |
| 401 |
} |
| 402 |
*/ |
| 403 |
prim_data[c].offset = offset; |
| 404 |
idx = ((int) o - offset); |
| 405 |
/* |
| 406 |
assert(idx >= 0); |
| 407 |
assert(idx <= 0xFFFF); |
| 408 |
*/ |
| 409 |
if (prim_data[c].subids < (idx + 1)) { |
| 410 |
str_number *new = (str_number *) xcalloc((unsigned) (idx + 1), sizeof(str_number *)); |
| 411 |
if (prim_data[c].names != NULL) { |
| 412 |
/* |
| 413 |
assert(prim_data[c].subids); |
| 414 |
*/ |
| 415 |
memcpy(new, (prim_data[c].names), (unsigned) (prim_data[c].subids) * sizeof(str_number)); |
| 416 |
free(prim_data[c].names); |
| 417 |
} |
| 418 |
prim_data[c].names = new; |
| 419 |
prim_data[c].subids = idx + 1; |
| 420 |
} |
| 421 |
prim_data[c].names[idx] = s; |
| 422 |
} |
| 423 |
|
| 424 |
/*tex |
| 425 |
|
| 426 |
Compared to tex82, |primitive| has two extra parameters. The |off| is an offset |
| 427 |
that will be passed on to |store_primitive_name|, the |cmd_origin| is the bit |
| 428 |
that is used to group primitives by originator. |
| 429 |
|
| 430 |
*/ |
| 431 |
|
| 432 |
void primitive(const char *thes, quarterword c, halfword o, halfword off, int cmd_origin) |
| 433 |
{ |
| 434 |
/*tex Needed to fill |prim_eqtb|: */ |
| 435 |
int prim_val; |
| 436 |
str_number ss; |
| 437 |
ss = maketexstring(thes); |
| 438 |
if (cmd_origin == tex_command || cmd_origin == core_command) { |
| 439 |
primitive_def(thes, strlen(thes), c, o); |
| 440 |
} |
| 441 |
prim_val = prim_lookup(ss); |
| 442 |
prim_origin(prim_val) = (quarterword) cmd_origin; |
| 443 |
prim_eq_type(prim_val) = c; |
| 444 |
prim_equiv(prim_val) = o; |
| 445 |
store_primitive_name(ss, c, o, off); |
| 446 |
} |
| 447 |
|
| 448 |
/*tex |
| 449 |
|
| 450 |
Here is a helper that does the actual hash insertion. This code far from ideal: |
| 451 |
the existance of |hash_extra| changes all the potential (short) coalesced lists |
| 452 |
into a single (long) one. This will create a slowdown. |
| 453 |
|
| 454 |
*/ |
| 455 |
|
| 456 |
static halfword insert_id(halfword p, const unsigned char *j, unsigned int l) |
| 457 |
{ |
| 458 |
unsigned saved_cur_length; |
| 459 |
unsigned saved_cur_string_size; |
| 460 |
unsigned char *saved_cur_string; |
| 461 |
const unsigned char *k; |
| 462 |
if (cs_text(p) > 0) { |
| 463 |
if (hash_high < hash_extra) { |
| 464 |
incr(hash_high); |
| 465 |
/*tex |
| 466 |
Can't we use |eqtb_top| here (perhaps because that is not |
| 467 |
finalized yet when called from |primitive|? |
| 468 |
*/ |
| 469 |
cs_next(p) = hash_high + eqtb_size; |
| 470 |
p = cs_next(p); |
| 471 |
} else { |
| 472 |
/*tex |
| 473 |
Search for an empty location in |hash|. |
| 474 |
*/ |
| 475 |
do { |
| 476 |
if (hash_is_full) |
| 477 |
overflow("hash size", (unsigned) (hash_size + hash_extra)); |
| 478 |
decr(hash_used); |
| 479 |
} while (cs_text(hash_used) != 0); |
| 480 |
cs_next(p) = hash_used; |
| 481 |
p = hash_used; |
| 482 |
} |
| 483 |
} |
| 484 |
saved_cur_length = cur_length; |
| 485 |
saved_cur_string = cur_string; |
| 486 |
saved_cur_string_size = cur_string_size; |
| 487 |
reset_cur_string(); |
| 488 |
for (k = j; k <= j + l - 1; k++) { |
| 489 |
append_char(*k); |
| 490 |
} |
| 491 |
cs_text(p) = make_string(); |
| 492 |
cur_length = saved_cur_length; |
| 493 |
xfree(cur_string); |
| 494 |
cur_string = saved_cur_string; |
| 495 |
cur_string_size = saved_cur_string_size; |
| 496 |
incr(cs_count); |
| 497 |
return p; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
/*tex |
| 502 |
|
| 503 |
Here is the subroutine that searches the hash table for an identifier that |
| 504 |
matches a given string of length |l>1| appearing in |buffer[j.. (j+l-1)]|. If the |
| 505 |
identifier is found, the corresponding hash table address is returned. Otherwise, |
| 506 |
if the global variable |no_new_control_sequence| is |true|, the dummy address |
| 507 |
|undefined_control_sequence| is returned. Otherwise the identifier is inserted |
| 508 |
into the hash table and its location is returned. |
| 509 |
|
| 510 |
*/ |
| 511 |
|
| 512 |
pointer id_lookup(int j, int l) |
| 513 |
{ |
| 514 |
/*tex The hash code: */ |
| 515 |
int h; |
| 516 |
/*tex The index in |hash| array: */ |
| 517 |
pointer p; |
| 518 |
h = compute_hash((char *) (buffer + j), (unsigned) l, hash_prime); |
| 519 |
/*tex We start searching here. Note that |0<=h<hash_prime|: */ |
| 520 |
p = h + hash_base; |
| 521 |
while (1) { |
| 522 |
if (cs_text(p) > 0) |
| 523 |
if (str_length(cs_text(p)) == (unsigned) l) |
| 524 |
if (str_eq_buf(cs_text(p), j)) |
| 525 |
goto FOUND; |
| 526 |
if (cs_next(p) == 0) { |
| 527 |
if (no_new_control_sequence) { |
| 528 |
p = undefined_control_sequence; |
| 529 |
} else { |
| 530 |
p = insert_id(p, (buffer + j), (unsigned) l); |
| 531 |
} |
| 532 |
goto FOUND; |
| 533 |
} |
| 534 |
p = cs_next(p); |
| 535 |
} |
| 536 |
FOUND: |
| 537 |
return p; |
| 538 |
} |
| 539 |
|
| 540 |
/*tex |
| 541 |
|
| 542 |
Here is a similar subroutine for finding a primitive in the hash. |
| 543 |
This one is based on a C string. |
| 544 |
|
| 545 |
*/ |
| 546 |
|
| 547 |
pointer string_lookup(const char *s, size_t l) |
| 548 |
{ |
| 549 |
/*tex The hash code: */ |
| 550 |
int h; |
| 551 |
/*tex The index in |hash| array: */ |
| 552 |
pointer p; |
| 553 |
h = compute_hash(s, (unsigned) l, hash_prime); |
| 554 |
/*tex We start searching here. Note that |0<=h<hash_prime|: */ |
| 555 |
p = h + hash_base; |
| 556 |
while (1) { |
| 557 |
if (cs_text(p) > 0) |
| 558 |
if (str_eq_cstr(cs_text(p), s, l)) |
| 559 |
goto FOUND; |
| 560 |
if (cs_next(p) == 0) { |
| 561 |
if (no_new_control_sequence) { |
| 562 |
p = undefined_control_sequence; |
| 563 |
} else { |
| 564 |
p = insert_id(p, (const unsigned char *) s, (unsigned) l); |
| 565 |
} |
| 566 |
goto FOUND; |
| 567 |
} |
| 568 |
p = cs_next(p); |
| 569 |
} |
| 570 |
FOUND: |
| 571 |
return p; |
| 572 |
} |
| 573 |
|
| 574 |
/*tex |
| 575 |
|
| 576 |
The |print_cmd_chr| routine prints a symbolic interpretation of a command code |
| 577 |
and its modifier. This is used in certain `\.{You can\'t}' error messages, and in |
| 578 |
the implementation of diagnostic routines like \.{\\show}. |
| 579 |
|
| 580 |
The body of |print_cmd_chr| use to be a rather tedious listing of print commands, |
| 581 |
and most of it was essentially an inverse to the |primitive| routine that enters |
| 582 |
a \TeX\ primitive into |eqtb|. |
| 583 |
|
| 584 |
Thanks to |prim_data|, there is no need for all that tediousness. What is left of |
| 585 |
|print_cmd_chr| are just the exceptions to the general rule that the |
| 586 |
|cmd,chr_code| pair represents in a single primitive command. |
| 587 |
|
| 588 |
*/ |
| 589 |
|
| 590 |
#define chr_cmd(A) do { tprint(A); print(chr_code); } while (0) |
| 591 |
|
| 592 |
static void prim_cmd_chr(quarterword cmd, halfword chr_code) |
| 593 |
{ |
| 594 |
int idx = chr_code - prim_data[cmd].offset; |
| 595 |
if (cmd <= last_cmd && |
| 596 |
idx >= 0 && idx < prim_data[cmd].subids && |
| 597 |
prim_data[cmd].names != NULL && prim_data[cmd].names[idx] != 0) { |
| 598 |
tprint_esc(""); |
| 599 |
print(prim_data[cmd].names[idx]); |
| 600 |
} else { |
| 601 |
/* \TEX82 didn't print the |cmd,idx| information, but it may be useful. */ |
| 602 |
tprint("[unknown command code! ("); |
| 603 |
print_int(cmd); |
| 604 |
tprint(", "); |
| 605 |
print_int(idx); |
| 606 |
tprint(")]"); |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
void print_cmd_chr(quarterword cmd, halfword chr_code) |
| 611 |
{ |
| 612 |
int n; |
| 613 |
switch (cmd) { |
| 614 |
case left_brace_cmd: |
| 615 |
chr_cmd("begin-group character "); |
| 616 |
break; |
| 617 |
case right_brace_cmd: |
| 618 |
chr_cmd("end-group character "); |
| 619 |
break; |
| 620 |
case math_shift_cmd: |
| 621 |
chr_cmd("math shift character "); |
| 622 |
break; |
| 623 |
case mac_param_cmd: |
| 624 |
if (chr_code == tab_mark_cmd_code) |
| 625 |
tprint_esc("alignmark"); |
| 626 |
else |
| 627 |
chr_cmd("macro parameter character "); |
| 628 |
break; |
| 629 |
case sup_mark_cmd: |
| 630 |
chr_cmd("superscript character "); |
| 631 |
break; |
| 632 |
case sub_mark_cmd: |
| 633 |
chr_cmd("subscript character "); |
| 634 |
break; |
| 635 |
case endv_cmd: |
| 636 |
tprint("end of alignment template"); |
| 637 |
break; |
| 638 |
case spacer_cmd: |
| 639 |
chr_cmd("blank space "); |
| 640 |
break; |
| 641 |
case letter_cmd: |
| 642 |
chr_cmd("the letter "); |
| 643 |
break; |
| 644 |
case other_char_cmd: |
| 645 |
chr_cmd("the character "); |
| 646 |
break; |
| 647 |
case tab_mark_cmd: |
| 648 |
if (chr_code == span_code) |
| 649 |
tprint_esc("span"); |
| 650 |
else if (chr_code == tab_mark_cmd_code) |
| 651 |
tprint_esc("aligntab"); |
| 652 |
else |
| 653 |
chr_cmd("alignment tab character "); |
| 654 |
break; |
| 655 |
case if_test_cmd: |
| 656 |
if (chr_code >= unless_code) |
| 657 |
tprint_esc("unless"); |
| 658 |
prim_cmd_chr(cmd, (chr_code % unless_code)); |
| 659 |
break; |
| 660 |
case char_given_cmd: |
| 661 |
tprint_esc("char"); |
| 662 |
print_qhex(chr_code); |
| 663 |
break; |
| 664 |
case math_given_cmd: |
| 665 |
/*tex |
| 666 |
Okay, it's better for old macro packages that mess with meaning |
| 667 |
to report a traditional value. A compromise. |
| 668 |
*/ |
| 669 |
tprint_esc("mathchar"); |
| 670 |
show_mathcode_value_old(chr_code); |
| 671 |
break; |
| 672 |
case xmath_given_cmd: |
| 673 |
tprint_esc("Umathchar"); |
| 674 |
show_mathcode_value(mathchar_from_integer(chr_code, umath_mathcode)); |
| 675 |
break; |
| 676 |
case lua_expandable_call_cmd: |
| 677 |
tprint("expandable luacall "); |
| 678 |
print_int(chr_code); |
| 679 |
break; |
| 680 |
case lua_local_call_cmd: |
| 681 |
tprint("local luacall "); |
| 682 |
print_int(chr_code); |
| 683 |
break; |
| 684 |
case lua_call_cmd: |
| 685 |
tprint("luacall "); |
| 686 |
print_int(chr_code); |
| 687 |
break; |
| 688 |
case set_font_cmd: |
| 689 |
tprint("select font "); |
| 690 |
tprint(font_name(chr_code)); |
| 691 |
if (font_size(chr_code) != font_dsize(chr_code)) { |
| 692 |
tprint(" at "); |
| 693 |
print_scaled(font_size(chr_code)); |
| 694 |
tprint("pt"); |
| 695 |
} |
| 696 |
break; |
| 697 |
case undefined_cs_cmd: |
| 698 |
tprint("undefined"); |
| 699 |
break; |
| 700 |
case call_cmd: |
| 701 |
case long_call_cmd: |
| 702 |
case outer_call_cmd: |
| 703 |
case long_outer_call_cmd: |
| 704 |
n = cmd - call_cmd; |
| 705 |
if (token_info(token_link(chr_code)) == protected_token) |
| 706 |
n = n + 4; |
| 707 |
if (odd(n / 4)) |
| 708 |
tprint_esc("protected"); |
| 709 |
if (odd(n)) |
| 710 |
tprint_esc("long"); |
| 711 |
if (odd(n / 2)) |
| 712 |
tprint_esc("outer"); |
| 713 |
if (n > 0) |
| 714 |
tprint(" "); |
| 715 |
tprint("macro"); |
| 716 |
break; |
| 717 |
case assign_glue_cmd: |
| 718 |
case assign_mu_glue_cmd: |
| 719 |
if (chr_code < skip_base) { |
| 720 |
prim_cmd_chr(cmd, chr_code); |
| 721 |
} else if (chr_code < mu_skip_base) { |
| 722 |
tprint_esc("skip"); |
| 723 |
print_int(chr_code - skip_base); |
| 724 |
} else { |
| 725 |
tprint_esc("muskip"); |
| 726 |
print_int(chr_code - mu_skip_base); |
| 727 |
} |
| 728 |
break; |
| 729 |
case assign_toks_cmd: |
| 730 |
if (chr_code >= toks_base) { |
| 731 |
tprint_esc("toks"); |
| 732 |
print_int(chr_code - toks_base); |
| 733 |
} else { |
| 734 |
prim_cmd_chr(cmd, chr_code); |
| 735 |
} |
| 736 |
break; |
| 737 |
case assign_int_cmd: |
| 738 |
if (chr_code < count_base) { |
| 739 |
prim_cmd_chr(cmd, chr_code); |
| 740 |
} else { |
| 741 |
tprint_esc("count"); |
| 742 |
print_int(chr_code - count_base); |
| 743 |
} |
| 744 |
break; |
| 745 |
case assign_attr_cmd: |
| 746 |
tprint_esc("attribute"); |
| 747 |
print_int(chr_code - attribute_base); |
| 748 |
break; |
| 749 |
case assign_dimen_cmd: |
| 750 |
if (chr_code < scaled_base) { |
| 751 |
prim_cmd_chr(cmd, chr_code); |
| 752 |
} else { |
| 753 |
tprint_esc("dimen"); |
| 754 |
print_int(chr_code - scaled_base); |
| 755 |
} |
| 756 |
break; |
| 757 |
case normal_cmd: |
| 758 |
if (chr_code < prim_data[cmd].subids && prim_data[cmd].names[chr_code] != 0) { |
| 759 |
prim_cmd_chr(cmd, chr_code); |
| 760 |
} else { |
| 761 |
tprint("[unknown command! ("); |
| 762 |
print_int(chr_code); |
| 763 |
tprint(")]"); |
| 764 |
} |
| 765 |
break; |
| 766 |
case extension_cmd: |
| 767 |
if (chr_code < prim_data[cmd].subids && prim_data[cmd].names[chr_code] != 0) { |
| 768 |
prim_cmd_chr(cmd, chr_code); |
| 769 |
} else { |
| 770 |
tprint("[unknown extension! ("); |
| 771 |
print_int(chr_code); |
| 772 |
tprint(")]"); |
| 773 |
|
| 774 |
} |
| 775 |
break; |
| 776 |
case node_cmd: |
| 777 |
tprint("node "); |
| 778 |
print_int(chr_code); |
| 779 |
break; |
| 780 |
default: |
| 781 |
/*tex These are most commands, actually. */ |
| 782 |
prim_cmd_chr(cmd, chr_code); |
| 783 |
break; |
| 784 |
} |
| 785 |
} |