| 1 |
/* |
| 2 |
* ChkTeX, utility functions. |
| 3 |
* Copyright (C) 1995-96 Jens T. Berger Thielemann |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 |
* |
| 19 |
* Contact the author at: |
| 20 |
* Jens Berger |
| 21 |
* Spektrumvn. 4 |
| 22 |
* N-0666 Oslo |
| 23 |
* Norway |
| 24 |
* E-mail: <[email protected]> |
| 25 |
* |
| 26 |
* |
| 27 |
*/ |
| 28 |
|
| 29 |
#include "ChkTeX.h" |
| 30 |
#include "Utility.h" |
| 31 |
#include "Resource.h" |
| 32 |
#include "OpSys.h" |
| 33 |
|
| 34 |
typedef unsigned long HASH_TYPE; |
| 35 |
|
| 36 |
/***************************** SUPPORT FUNCTIONS ************************/ |
| 37 |
|
| 38 |
|
| 39 |
/* |
| 40 |
* Copies a string with a maximum length of `len' starting at `pos' in |
| 41 |
* `source' into `dest'. |
| 42 |
* Returns -1 if the pos value is beyond the length of the source value, |
| 43 |
* else NULL. |
| 44 |
*/ |
| 45 |
|
| 46 |
|
| 47 |
short substring(const char *source, char *dest, unsigned long pos, long len) |
| 48 |
{ |
| 49 |
const char *Start; |
| 50 |
short Retval = -1; |
| 51 |
|
| 52 |
if (len >= 0) |
| 53 |
{ |
| 54 |
if (strlen(source) > pos) |
| 55 |
{ |
| 56 |
Start = &source[pos]; |
| 57 |
|
| 58 |
while ((len-- > 0) && (*dest++ = *Start++)) |
| 59 |
; |
| 60 |
|
| 61 |
if (len == -1) |
| 62 |
Retval = 0; |
| 63 |
} |
| 64 |
} |
| 65 |
else |
| 66 |
Retval = 0L; |
| 67 |
|
| 68 |
*dest = 0; |
| 69 |
|
| 70 |
return (Retval); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
/* |
| 75 |
* Determine whether a file exists. |
| 76 |
* |
| 77 |
*/ |
| 78 |
|
| 79 |
|
| 80 |
int fexists(const char *Filename) |
| 81 |
{ |
| 82 |
int Retval; |
| 83 |
|
| 84 |
#if defined(F_OK) && defined(R_OK) && defined(HAVE_ACCESS) |
| 85 |
|
| 86 |
Retval = access(Filename, F_OK | R_OK) == 0; |
| 87 |
#else |
| 88 |
|
| 89 |
FILE *fh; |
| 90 |
if ((fh = fopen(Filename, "r"))) |
| 91 |
{ |
| 92 |
Retval = TRUE; |
| 93 |
fclose(fh); |
| 94 |
} |
| 95 |
else |
| 96 |
Retval = FALSE; |
| 97 |
#endif |
| 98 |
|
| 99 |
/* Ensure that it's not a directory */ |
| 100 |
if (Retval && !IsRegFile(Filename)) |
| 101 |
{ |
| 102 |
Retval = FALSE; |
| 103 |
} |
| 104 |
|
| 105 |
return (Retval); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
/* |
| 111 |
* 'Safe' memset() replacement. |
| 112 |
* Just tries to check the parameters, so that the risk of killing |
| 113 |
* innocent memory is lowered. |
| 114 |
* Please note that the `n' parameter now is an signed longword, not |
| 115 |
* an size_t. Won't permit that `n' exceeds BUFLEN, nor negative sizes. |
| 116 |
* Returns `to' if some memset()'ing was done, NULL if not. |
| 117 |
*/ |
| 118 |
|
| 119 |
void *sfmemset(void *to, int c, long n) |
| 120 |
{ |
| 121 |
if (to && (n > 0)) |
| 122 |
{ |
| 123 |
n = min(n, BUFFER_SIZE); |
| 124 |
|
| 125 |
return (memset(to, c, (size_t) n)); |
| 126 |
} |
| 127 |
return (NULL); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
/* |
| 132 |
* Quick replace function |
| 133 |
* Replaces every occurrence of a character in a string with another one. |
| 134 |
*/ |
| 135 |
|
| 136 |
void strrep(char *String, /* String to replace within. */ |
| 137 |
const char From, /* Character to search for. */ |
| 138 |
const char To) /* Character to put instead. */ |
| 139 |
{ |
| 140 |
register int c; |
| 141 |
while ((c = *String++)) |
| 142 |
{ |
| 143 |
if (c == From) |
| 144 |
String[-1] = To; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
* Replaces every char not in Prot with To in Buf |
| 150 |
*/ |
| 151 |
|
| 152 |
void strxrep(char *Buf, const char *Prot, const char To) |
| 153 |
{ |
| 154 |
int c; |
| 155 |
|
| 156 |
while ((c = *Buf)) |
| 157 |
{ |
| 158 |
if (!strchr(Prot, c)) |
| 159 |
*Buf = To; |
| 160 |
Buf++; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/* |
| 165 |
* Strips tail and/or front of a string |
| 166 |
* Kills trailing/leading spaces. The macro/function LATEX_SPACE(char c) |
| 167 |
* is used to decide whether a space should be skipped. This function |
| 168 |
* should return TRUE if the character should be skipped, FALSE if not. |
| 169 |
* Returns the string which was passed onto it. |
| 170 |
*/ |
| 171 |
|
| 172 |
|
| 173 |
char *strip(char *str, /* String to strip */ |
| 174 |
const enum Strip flags) |
| 175 |
/* One of the following: */ |
| 176 |
/* STRP_LFT - Strips leading blanks */ |
| 177 |
/* STRP_RGT - Strips trailing blanks */ |
| 178 |
/* STRP_BTH - Strips on both sides */ |
| 179 |
{ |
| 180 |
char *bufptr = str; |
| 181 |
char *nlptr; |
| 182 |
char c; |
| 183 |
|
| 184 |
if (bufptr && (c = *bufptr)) |
| 185 |
{ |
| 186 |
if (flags & STRP_LFT) |
| 187 |
{ |
| 188 |
if (LATEX_SPACE(c) && c) |
| 189 |
{ |
| 190 |
do |
| 191 |
{ |
| 192 |
c = *++bufptr; |
| 193 |
} |
| 194 |
while (LATEX_SPACE(c) && c); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if (flags & STRP_RGT) |
| 199 |
{ |
| 200 |
if (c && *bufptr) |
| 201 |
{ |
| 202 |
nlptr = bufptr; |
| 203 |
|
| 204 |
while (*++nlptr) |
| 205 |
; |
| 206 |
|
| 207 |
do |
| 208 |
{ |
| 209 |
*nlptr = 0; |
| 210 |
c = *--nlptr; |
| 211 |
} |
| 212 |
while (LATEX_SPACE(c) && c && (nlptr > bufptr)); |
| 213 |
|
| 214 |
} |
| 215 |
else |
| 216 |
*bufptr = 0; |
| 217 |
} |
| 218 |
} |
| 219 |
return (bufptr); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
/* |
| 224 |
* Converts all the chars in the string passed into lowercase. |
| 225 |
*/ |
| 226 |
|
| 227 |
#ifndef HAVE_STRLWR |
| 228 |
char *strlwr(char *String) |
| 229 |
{ |
| 230 |
char *Bufptr; |
| 231 |
char TmpC; |
| 232 |
|
| 233 |
for (Bufptr = String; (TmpC = *Bufptr); Bufptr++) |
| 234 |
*Bufptr = tolower((unsigned char)TmpC); |
| 235 |
|
| 236 |
return (String); |
| 237 |
} |
| 238 |
#endif |
| 239 |
|
| 240 |
/* |
| 241 |
* Returns a duplicate of the string passed. |
| 242 |
*/ |
| 243 |
|
| 244 |
#ifndef HAVE_STRDUP |
| 245 |
char *strdup(const char *String) |
| 246 |
{ |
| 247 |
char *Retval = NULL; |
| 248 |
size_t Len; |
| 249 |
|
| 250 |
if (String) |
| 251 |
{ |
| 252 |
Len = strlen(String) + 1; |
| 253 |
if ((Retval = malloc(Len))) |
| 254 |
memcpy(Retval, String, Len); |
| 255 |
} |
| 256 |
|
| 257 |
return (Retval); |
| 258 |
} |
| 259 |
#endif |
| 260 |
|
| 261 |
/* |
| 262 |
* Does the same as strdup, but adds a zero-filled padding, length extra bytes. |
| 263 |
*/ |
| 264 |
|
| 265 |
char *strdupx(const char *String, int Extra) |
| 266 |
{ |
| 267 |
char *Retval = NULL; |
| 268 |
size_t Len; |
| 269 |
|
| 270 |
if (String) |
| 271 |
{ |
| 272 |
Len = strlen(String) + 1 + Extra; |
| 273 |
if ((Retval = malloc(Len))) |
| 274 |
strncpy(Retval, String, Len); |
| 275 |
} |
| 276 |
|
| 277 |
return (Retval); |
| 278 |
} |
| 279 |
|
| 280 |
/* |
| 281 |
* Case-insensitive comparison of two strings. |
| 282 |
*/ |
| 283 |
|
| 284 |
#ifndef HAVE_STRCASECMP |
| 285 |
int strcasecmp(const char *a, const char *b) |
| 286 |
{ |
| 287 |
int aa, bb; |
| 288 |
|
| 289 |
do |
| 290 |
{ |
| 291 |
aa = *a++; |
| 292 |
bb = *b++; |
| 293 |
} |
| 294 |
while (aa && (tolower((unsigned char)aa) == tolower((unsigned char)bb))); |
| 295 |
/* bb != 0 is implicit */ |
| 296 |
|
| 297 |
return (tolower((unsigned char)aa) - tolower((unsigned char)bb)); |
| 298 |
} |
| 299 |
#endif |
| 300 |
|
| 301 |
/* |
| 302 |
* Not all reallocs are intelligent enough to handle NULL's as |
| 303 |
* parameters. This fixes this. |
| 304 |
*/ |
| 305 |
|
| 306 |
void *saferealloc(void *b, size_t n) |
| 307 |
{ |
| 308 |
void *Retval = NULL; |
| 309 |
|
| 310 |
if (b) |
| 311 |
{ |
| 312 |
if (n) |
| 313 |
Retval = realloc(b, n); |
| 314 |
else |
| 315 |
free(b); |
| 316 |
} |
| 317 |
else |
| 318 |
Retval = malloc(n); |
| 319 |
|
| 320 |
return (Retval); |
| 321 |
} |
| 322 |
|
| 323 |
/* |
| 324 |
* Repeatedly writes the From string over To so that we overwrite Len bytes. |
| 325 |
* Does nothing if passed empty/NULL string. |
| 326 |
*/ |
| 327 |
|
| 328 |
void strwrite(char *To, const char *From, unsigned long Len) |
| 329 |
{ |
| 330 |
unsigned long i, j; |
| 331 |
unsigned long FromLen = strlen(From); |
| 332 |
|
| 333 |
Len = min(Len, BUFFER_SIZE); |
| 334 |
|
| 335 |
if (To && From) |
| 336 |
{ |
| 337 |
switch (FromLen) |
| 338 |
{ |
| 339 |
case 0: |
| 340 |
break; |
| 341 |
case 1: |
| 342 |
memset(To, *From, Len); |
| 343 |
break; |
| 344 |
default: |
| 345 |
for (i = j = 0; i < Len; i++, j++) |
| 346 |
{ |
| 347 |
if (j >= FromLen) |
| 348 |
j = 0; |
| 349 |
To[i] = From[j]; |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
/* |
| 356 |
* Checks whether Cmp comes after Str. |
| 357 |
* |
| 358 |
*/ |
| 359 |
|
| 360 |
int strafter(const char *Str, const char *Cmp) |
| 361 |
{ |
| 362 |
return (strncmp(Str, Cmp, strlen(Cmp))); |
| 363 |
} |
| 364 |
|
| 365 |
/* |
| 366 |
* Checks whether Cmp comes before Str. Returns 0 if so, non-zero if not. |
| 367 |
* |
| 368 |
*/ |
| 369 |
|
| 370 |
int strinfront(const char *Str, const char *Cmp) |
| 371 |
{ |
| 372 |
int CmpLen; |
| 373 |
|
| 374 |
if ((CmpLen = strlen(Cmp))) |
| 375 |
{ |
| 376 |
Cmp += CmpLen; |
| 377 |
Str++; |
| 378 |
|
| 379 |
while ((*--Cmp == *--Str) && (--CmpLen > 0)) |
| 380 |
; |
| 381 |
|
| 382 |
return (CmpLen); |
| 383 |
} |
| 384 |
else |
| 385 |
return (1); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
/*************************** HASH INDEX **************************/ |
| 390 |
|
| 391 |
/* |
| 392 |
* Hashes a string. The string ought be rather short. |
| 393 |
* |
| 394 |
* The algorithm was designed by Peter Weinberger. This version was |
| 395 |
* adapted from Dr Dobb's Journal April 1996 page 26. |
| 396 |
*/ |
| 397 |
|
| 398 |
static unsigned long HashWord(const char *str) |
| 399 |
{ |
| 400 |
register unsigned long h = 0, hbit, c; |
| 401 |
|
| 402 |
while ((c = *str++)) |
| 403 |
{ |
| 404 |
h = (h << 4) ^ c; |
| 405 |
if ((hbit = h & 0xf0000000)) |
| 406 |
h ^= hbit >> 24; |
| 407 |
h &= ~hbit; |
| 408 |
} |
| 409 |
|
| 410 |
return (h); |
| 411 |
} |
| 412 |
|
| 413 |
/* |
| 414 |
* Inserts a string into a hash index. Note: You'll have to |
| 415 |
* duplicate the string yourself. |
| 416 |
*/ |
| 417 |
|
| 418 |
void InsertHash(char *a, struct Hash *h) |
| 419 |
{ |
| 420 |
struct HashEntry **he, *newhe; |
| 421 |
|
| 422 |
if (!h->Index) |
| 423 |
{ |
| 424 |
if (!((h->Index = calloc(HASH_SIZE, sizeof(struct HashEntry *))))) |
| 425 |
PrintPrgErr(pmWordListErr); |
| 426 |
} |
| 427 |
|
| 428 |
he = &h->Index[HashWord(a) % HASH_SIZE]; |
| 429 |
|
| 430 |
if ((newhe = malloc(sizeof(struct HashEntry)))) |
| 431 |
{ |
| 432 |
newhe->Next = *he; |
| 433 |
newhe->Str = a; |
| 434 |
*he = newhe; |
| 435 |
} |
| 436 |
else |
| 437 |
PrintPrgErr(pmWordListErr); |
| 438 |
} |
| 439 |
|
| 440 |
/* |
| 441 |
* Checks whether a string previously has been registered in a |
| 442 |
* hash index. |
| 443 |
*/ |
| 444 |
|
| 445 |
char *HasHash(const char *a, const struct Hash *h) |
| 446 |
{ |
| 447 |
struct HashEntry *he; |
| 448 |
HASH_TYPE i; /* Special magic to optimize SAS/C */ |
| 449 |
|
| 450 |
/* Do we have a hash? */ |
| 451 |
if (!h->Index) |
| 452 |
return NULL; |
| 453 |
|
| 454 |
/* Find entry in hash */ |
| 455 |
i = HashWord(a); |
| 456 |
i %= HASH_SIZE; |
| 457 |
he = h->Index[i]; |
| 458 |
|
| 459 |
/* Search in the entry for the item */ |
| 460 |
while (he) |
| 461 |
{ |
| 462 |
if (!strcmp(he->Str, a)) |
| 463 |
return (he->Str); |
| 464 |
else |
| 465 |
he = he->Next; |
| 466 |
} |
| 467 |
return (NULL); |
| 468 |
} |
| 469 |
|
| 470 |
/* |
| 471 |
* Clears a hash table. |
| 472 |
*/ |
| 473 |
|
| 474 |
void ClearHash(struct Hash *h) |
| 475 |
{ |
| 476 |
int i; |
| 477 |
struct HashEntry *he, *next; |
| 478 |
if (h && h->Index) |
| 479 |
{ |
| 480 |
/* Free all the memory */ |
| 481 |
for ( i = 0; i < HASH_SIZE; ++i ) |
| 482 |
{ |
| 483 |
he = h->Index[i]; |
| 484 |
while ( he ) |
| 485 |
{ |
| 486 |
next = he->Next; |
| 487 |
free( he ); |
| 488 |
he = next; |
| 489 |
} |
| 490 |
} |
| 491 |
/* Reset the hash table */ |
| 492 |
memset(h->Index, '\0', HASH_SIZE * sizeof(struct HashEntry *)); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
/* |
| 497 |
* Rehashes a wordlist. If you change any of the elem's, you must |
| 498 |
* call this. |
| 499 |
* |
| 500 |
*/ |
| 501 |
|
| 502 |
static void ReHash(struct WordList *WL) |
| 503 |
{ |
| 504 |
unsigned long i = 0; |
| 505 |
|
| 506 |
ClearHash(&WL->Hash); |
| 507 |
FORWL(i, *WL) InsertHash(WL->Stack.Data[i], &WL->Hash); |
| 508 |
} |
| 509 |
|
| 510 |
/*************************** WORDLIST HANDLING **************************/ |
| 511 |
|
| 512 |
/* |
| 513 |
* Inserts a duplicate of `Word' into the `Wordlist' structure. You do thus |
| 514 |
* not need to make a duplicate of `Word' yourself. |
| 515 |
*/ |
| 516 |
|
| 517 |
int InsertWord(const char *Word, struct WordList *WL) |
| 518 |
{ |
| 519 |
char *WrdCpy; |
| 520 |
unsigned long Len; |
| 521 |
|
| 522 |
if ((WrdCpy = strdupx(Word, WALLBYTES))) |
| 523 |
{ |
| 524 |
if (StkPush(WrdCpy, &WL->Stack)) |
| 525 |
{ |
| 526 |
Len = strlen(Word); |
| 527 |
if (WL->MaxLen < Len) |
| 528 |
WL->MaxLen = Len; |
| 529 |
|
| 530 |
InsertHash(WrdCpy, &WL->Hash); |
| 531 |
return (TRUE); |
| 532 |
} |
| 533 |
|
| 534 |
free(WrdCpy); |
| 535 |
} |
| 536 |
|
| 537 |
return (FALSE); |
| 538 |
} |
| 539 |
|
| 540 |
/* |
| 541 |
* Clears a WordList; removing all items. |
| 542 |
*/ |
| 543 |
|
| 544 |
void ClearWord(struct WordList *WL) |
| 545 |
{ |
| 546 |
char *Word; |
| 547 |
if (WL) |
| 548 |
{ |
| 549 |
while ( (Word = StkPop( &WL->Stack )) ) |
| 550 |
{ |
| 551 |
free(Word); |
| 552 |
} |
| 553 |
WL->Stack.Used = 0; |
| 554 |
WL->MaxLen = 0; |
| 555 |
ClearHash(&WL->Hash); |
| 556 |
if (WL->NonEmpty) |
| 557 |
InsertWord("", WL); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
/* |
| 562 |
* Query whether a `Word' is previously InsertWord()'ed into the WL |
| 563 |
* structure. Does case-sensitive comparison. |
| 564 |
* |
| 565 |
* Returns the data in the list. |
| 566 |
*/ |
| 567 |
|
| 568 |
|
| 569 |
char *HasWord(const char *Word, struct WordList *WL) |
| 570 |
{ |
| 571 |
return HasHash(Word, &WL->Hash); |
| 572 |
} |
| 573 |
|
| 574 |
/* |
| 575 |
* Make all the words in a list lower case for later case-insensitive |
| 576 |
* comparison. |
| 577 |
*/ |
| 578 |
|
| 579 |
void MakeLower(struct WordList *wl) |
| 580 |
{ |
| 581 |
unsigned long i; |
| 582 |
FORWL(i, *wl) strlwr(wl->Stack.Data[i]); |
| 583 |
ReHash(wl); |
| 584 |
} |
| 585 |
|
| 586 |
/* |
| 587 |
* Calls strrep on each argument in a list. |
| 588 |
*/ |
| 589 |
|
| 590 |
void ListRep(struct WordList *wl, const char From, const char To) |
| 591 |
{ |
| 592 |
unsigned long i; |
| 593 |
FORWL(i, *wl) strrep(wl->Stack.Data[i], From, To); |
| 594 |
ReHash(wl); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
/************************** GENERIC STACK ******************************/ |
| 600 |
|
| 601 |
/* |
| 602 |
* Push something onto a stack. Returns TRUE if successful, else FALSE. |
| 603 |
* Note: You can not push a NULL Data element. |
| 604 |
*/ |
| 605 |
|
| 606 |
int StkPush(void *Data, struct Stack *Stack) |
| 607 |
{ |
| 608 |
unsigned long NewSize; |
| 609 |
void **NewBuf; |
| 610 |
|
| 611 |
if (Data && Stack) |
| 612 |
{ |
| 613 |
if (Stack->Used >= Stack->Size) |
| 614 |
{ |
| 615 |
NewSize = Stack->Size + MINPUDDLE; |
| 616 |
|
| 617 |
if ((NewBuf = saferealloc(Stack->Data, |
| 618 |
(size_t) NewSize * sizeof(void *)))) |
| 619 |
{ |
| 620 |
Stack->Size = NewSize; |
| 621 |
Stack->Data = NewBuf; |
| 622 |
} |
| 623 |
else |
| 624 |
return (FALSE); |
| 625 |
} |
| 626 |
|
| 627 |
Stack->Data[Stack->Used++] = Data; |
| 628 |
return (TRUE); |
| 629 |
} |
| 630 |
|
| 631 |
return (FALSE); |
| 632 |
} |
| 633 |
|
| 634 |
/* |
| 635 |
* Pops an element from the stack. |
| 636 |
* |
| 637 |
*/ |
| 638 |
|
| 639 |
void *StkPop(struct Stack *Stack) |
| 640 |
{ |
| 641 |
void *Retval = NULL; |
| 642 |
|
| 643 |
if (Stack && (Stack->Used > 0)) |
| 644 |
{ |
| 645 |
Retval = Stack->Data[--Stack->Used]; |
| 646 |
|
| 647 |
#ifdef NO_DIRTY_TRICKS |
| 648 |
|
| 649 |
{ |
| 650 |
void **NewBuf; |
| 651 |
|
| 652 |
if (Stack->Used < (Stack->Size / 2)) |
| 653 |
{ |
| 654 |
unsigned long NewSize; |
| 655 |
NewSize = Stack->Size - MINPUDDLE; |
| 656 |
NewSize = max(NewSize, MINPUDDLE); |
| 657 |
|
| 658 |
if (NewBuf = saferealloc(Stack->Data, |
| 659 |
(size_t) NewSize * sizeof(void *))) |
| 660 |
{ |
| 661 |
Stack->Size = NewSize; |
| 662 |
Stack->Data = NewBuf; |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
#endif |
| 667 |
|
| 668 |
} |
| 669 |
return (Retval); |
| 670 |
} |
| 671 |
|
| 672 |
/* |
| 673 |
* Returns the topmost element of the stack. |
| 674 |
*/ |
| 675 |
|
| 676 |
void *StkTop(struct Stack *Stack) |
| 677 |
{ |
| 678 |
if (Stack && (Stack->Used > 0)) |
| 679 |
return (Stack->Data[Stack->Used - 1]); |
| 680 |
else |
| 681 |
return (NULL); |
| 682 |
} |
| 683 |
|
| 684 |
/****************************** INPUT STACK *****************************/ |
| 685 |
|
| 686 |
int PushFileName(const char *Name, struct Stack *stack) |
| 687 |
{ |
| 688 |
FILE *fh = NULL; |
| 689 |
static char NameBuf[BUFFER_SIZE]; |
| 690 |
|
| 691 |
if (Name && stack) |
| 692 |
{ |
| 693 |
if (LocateFile(Name, NameBuf, ".tex", &TeXInputs)) |
| 694 |
{ |
| 695 |
if ((fh = fopen(NameBuf, "r"))) |
| 696 |
{ |
| 697 |
return (PushFile(NameBuf, fh, stack)); |
| 698 |
} |
| 699 |
} |
| 700 |
PrintPrgErr(pmNoTeXOpen, Name); |
| 701 |
} |
| 702 |
return (FALSE); |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
int PushFile(const char *Name, FILE * fh, struct Stack *stack) |
| 707 |
{ |
| 708 |
struct FileNode *fn; |
| 709 |
uint64_t *filesupp; |
| 710 |
|
| 711 |
if (Name && fh && stack) |
| 712 |
{ |
| 713 |
if ((fn = malloc(sizeof(struct FileNode)))) |
| 714 |
{ |
| 715 |
if ((fn->Name = strdup(Name))) |
| 716 |
{ |
| 717 |
fn->fh = fh; |
| 718 |
fn->Line = 0L; |
| 719 |
if ((filesupp = malloc(sizeof(uint64_t)))) |
| 720 |
{ |
| 721 |
*filesupp = 0; |
| 722 |
StkPush(filesupp, &FileSuppStack); |
| 723 |
} |
| 724 |
else |
| 725 |
PrintPrgErr(pmNoStackMem); |
| 726 |
if ((filesupp = malloc(sizeof(uint64_t)))) |
| 727 |
{ |
| 728 |
*filesupp = 0; |
| 729 |
StkPush(filesupp, &UserFileSuppStack); |
| 730 |
} |
| 731 |
else |
| 732 |
PrintPrgErr(pmNoStackMem); |
| 733 |
|
| 734 |
if (StkPush(fn, stack)) |
| 735 |
return (TRUE); |
| 736 |
free(fn->Name); |
| 737 |
} |
| 738 |
free(fn); |
| 739 |
} |
| 740 |
PrintPrgErr(pmNoStackMem); |
| 741 |
} |
| 742 |
|
| 743 |
return (FALSE); |
| 744 |
} |
| 745 |
|
| 746 |
char *FGetsStk(char *Dest, unsigned long len, struct Stack *stack) |
| 747 |
{ |
| 748 |
static short HasSeenLong = 0; |
| 749 |
struct FileNode *fn; |
| 750 |
char *Retval = NULL; |
| 751 |
size_t Retlen = 0; |
| 752 |
|
| 753 |
if ((fn = StkTop(stack))) |
| 754 |
{ |
| 755 |
do |
| 756 |
{ |
| 757 |
Retval = fgets(Dest, (int)len, fn->fh); |
| 758 |
if (Retval) { |
| 759 |
Retlen = strlen(Retval); |
| 760 |
|
| 761 |
if (Retval[Retlen-1] == '\n' || Retlen < len-1) |
| 762 |
fn->Line++; |
| 763 |
/* We only want the long lines warning once per file */ |
| 764 |
else if (!HasSeenLong) |
| 765 |
{ |
| 766 |
PrintPrgErr(pmLongLines, len-2); |
| 767 |
HasSeenLong = 1; |
| 768 |
} |
| 769 |
break; |
| 770 |
} |
| 771 |
|
| 772 |
fn = StkPop(stack); |
| 773 |
fclose(fn->fh); |
| 774 |
if ( fn->Name != NULL ) |
| 775 |
free(fn->Name); |
| 776 |
free(fn); |
| 777 |
HasSeenLong = 0; |
| 778 |
|
| 779 |
StkPop(&FileSuppStack); |
| 780 |
StkPop(&UserFileSuppStack); |
| 781 |
} |
| 782 |
while (!Retval && (fn = StkTop(stack))); |
| 783 |
} |
| 784 |
|
| 785 |
return (Retval); |
| 786 |
} |
| 787 |
|
| 788 |
const char *CurStkName(struct Stack *stack) |
| 789 |
{ |
| 790 |
struct FileNode *fn; |
| 791 |
static const char *LastName = ""; |
| 792 |
|
| 793 |
if (PseudoInName && (stack->Used <= 1)) |
| 794 |
return (PseudoInName); |
| 795 |
else |
| 796 |
{ |
| 797 |
if ((fn = StkTop(stack))) |
| 798 |
{ |
| 799 |
if ( stack->Used == 1 && strlen(LastName) == 0 && fn->Name ) |
| 800 |
{ |
| 801 |
LastName = strdup(fn->Name); |
| 802 |
} |
| 803 |
return (fn->Name); |
| 804 |
} |
| 805 |
else |
| 806 |
return (LastName); |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
FILE *CurStkFile(struct Stack * stack) |
| 812 |
{ |
| 813 |
struct FileNode *fn; |
| 814 |
|
| 815 |
if ((fn = StkTop(stack))) |
| 816 |
return (fn->fh); |
| 817 |
else |
| 818 |
return (NULL); |
| 819 |
} |
| 820 |
|
| 821 |
unsigned long CurStkLine(struct Stack *stack) |
| 822 |
{ |
| 823 |
struct FileNode *fn; |
| 824 |
static unsigned long LastLine = 0L; |
| 825 |
|
| 826 |
if ((fn = StkTop(stack))) |
| 827 |
return (LastLine = fn->Line); |
| 828 |
else |
| 829 |
return (LastLine); |
| 830 |
} |
| 831 |
|
| 832 |
long CurStkMode(struct Stack *stack) |
| 833 |
{ |
| 834 |
long * Mode; |
| 835 |
if ((Mode = StkTop(stack))) |
| 836 |
return *Mode; |
| 837 |
/* printf("Empty stack\n"); */ |
| 838 |
return FALSE; |
| 839 |
} |
| 840 |
|
| 841 |
long *PushMode(long mode, struct Stack *stack) |
| 842 |
{ |
| 843 |
long *m; |
| 844 |
|
| 845 |
if ((m = malloc(sizeof(long)))) |
| 846 |
{ |
| 847 |
*m = mode; |
| 848 |
StkPush(m, stack); |
| 849 |
return m; |
| 850 |
} |
| 851 |
return NULL; |
| 852 |
} |
| 853 |
/************************** CHARACTER STACK ******************************/ |
| 854 |
|
| 855 |
/* |
| 856 |
* Pushes the character on the stack. |
| 857 |
*/ |
| 858 |
|
| 859 |
struct ErrInfo *PushChar(const char c, const unsigned long Line, |
| 860 |
const unsigned long Column, struct Stack *Stk, |
| 861 |
const char *LineCpy) |
| 862 |
{ |
| 863 |
char Buf[2]; |
| 864 |
|
| 865 |
Buf[0] = c; |
| 866 |
Buf[1] = 0; |
| 867 |
|
| 868 |
return (PushErr(Buf, Line, Column, 1, LineCpy, Stk)); |
| 869 |
} |
| 870 |
|
| 871 |
struct ErrInfo *PushErr(const char *Data, const unsigned long Line, |
| 872 |
const unsigned long Column, |
| 873 |
const unsigned long ErrLen, const char *LineCpy, |
| 874 |
struct Stack *Stk) |
| 875 |
{ |
| 876 |
struct ErrInfo *ci; |
| 877 |
|
| 878 |
if ((ci = malloc(sizeof(struct ErrInfo)))) |
| 879 |
{ |
| 880 |
if ((ci->Data = strdup(Data))) |
| 881 |
{ |
| 882 |
if ((ci->File = strdup(CurStkName(&InputStack)))) |
| 883 |
{ |
| 884 |
if ((ci->LineBuf = strdup(LineCpy))) |
| 885 |
{ |
| 886 |
ci->Line = Line; |
| 887 |
ci->ErrLen = ErrLen; |
| 888 |
ci->Column = Column; |
| 889 |
ci->Flags = efNone; |
| 890 |
|
| 891 |
if (StkPush(ci, Stk)) |
| 892 |
return (ci); |
| 893 |
|
| 894 |
free(ci->LineBuf); |
| 895 |
} |
| 896 |
else |
| 897 |
PrintPrgErr(pmStrDupErr); |
| 898 |
|
| 899 |
free(ci->File); |
| 900 |
} |
| 901 |
else |
| 902 |
PrintPrgErr(pmStrDupErr); |
| 903 |
|
| 904 |
free(ci->Data); |
| 905 |
} |
| 906 |
else |
| 907 |
PrintPrgErr(pmStrDupErr); |
| 908 |
free(ci); |
| 909 |
} |
| 910 |
|
| 911 |
return (NULL); |
| 912 |
} |
| 913 |
|
| 914 |
/* |
| 915 |
* Finds the uppermost entry in the stack with a data matching |
| 916 |
* String. |
| 917 |
*/ |
| 918 |
|
| 919 |
struct ErrInfo *TopMatch(struct Stack *Stack, char *String) |
| 920 |
{ |
| 921 |
int i; |
| 922 |
struct ErrInfo *retval = NULL; |
| 923 |
|
| 924 |
if (Stack && String) |
| 925 |
{ |
| 926 |
for (i = Stack->Used - 1; i >= 0; i--) |
| 927 |
{ |
| 928 |
if (!strcmp(String, ((struct ErrInfo *) Stack->Data[i])->Data)) |
| 929 |
{ |
| 930 |
retval = (struct ErrInfo *) Stack->Data[i]; |
| 931 |
break; |
| 932 |
} |
| 933 |
} |
| 934 |
} |
| 935 |
return (retval); |
| 936 |
} |
| 937 |
|
| 938 |
/* |
| 939 |
* Returns and removes a character from the stack, returns NULL if |
| 940 |
* the stack is empty. |
| 941 |
*/ |
| 942 |
|
| 943 |
|
| 944 |
struct ErrInfo *PopErr(struct Stack *Stack) |
| 945 |
{ |
| 946 |
return ((struct ErrInfo *) StkPop(Stack)); |
| 947 |
} |
| 948 |
|
| 949 |
/* |
| 950 |
* Same as PopChar(), but lets the error alone on the stack. |
| 951 |
*/ |
| 952 |
|
| 953 |
|
| 954 |
struct ErrInfo *TopErr(struct Stack *Stack) |
| 955 |
{ |
| 956 |
return ((struct ErrInfo *) StkTop(Stack)); |
| 957 |
} |
| 958 |
|
| 959 |
/* |
| 960 |
* Free all resources associated with a struct FreeInfo. |
| 961 |
*/ |
| 962 |
|
| 963 |
void FreeErrInfo(struct ErrInfo *ei) |
| 964 |
{ |
| 965 |
if (ei) |
| 966 |
{ |
| 967 |
if (ei->Data) |
| 968 |
free(ei->Data); |
| 969 |
if (ei->File) |
| 970 |
free(ei->File); |
| 971 |
if (ei->LineBuf) |
| 972 |
free(ei->LineBuf); |
| 973 |
|
| 974 |
free(ei); |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
/************************* OPEN/CLOSE COUNTING **************************/ |
| 980 |
|
| 981 |
/* |
| 982 |
* Returns the index a given bracket (`()[]{}') character has in the |
| 983 |
* BrOrder array. Returns ~0 if the character was not a bracket. |
| 984 |
*/ |
| 985 |
|
| 986 |
long BrackIndex(const char c) |
| 987 |
{ |
| 988 |
switch (c) |
| 989 |
{ |
| 990 |
case '(': |
| 991 |
return (0); |
| 992 |
case ')': |
| 993 |
return (1); |
| 994 |
case '[': |
| 995 |
return (2); |
| 996 |
case ']': |
| 997 |
return (3); |
| 998 |
case '{': |
| 999 |
return (4); |
| 1000 |
case '}': |
| 1001 |
return (5); |
| 1002 |
default: |
| 1003 |
return (~0L); |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
/* |
| 1008 |
* Counts brackets for you. Give it a bracket, and it will update the |
| 1009 |
* corresponding counter. |
| 1010 |
*/ |
| 1011 |
|
| 1012 |
void AddBracket(const char c) |
| 1013 |
{ |
| 1014 |
long Index; |
| 1015 |
|
| 1016 |
if ((Index = BrackIndex(c)) != -1) |
| 1017 |
Brackets[Index]++; |
| 1018 |
|
| 1019 |
} |
| 1020 |
|
| 1021 |
/* |
| 1022 |
* Returns the character that matches the given bracket, NULL if `c' |
| 1023 |
* wasn't a bracket character. |
| 1024 |
*/ |
| 1025 |
|
| 1026 |
char MatchBracket(const char c) |
| 1027 |
{ |
| 1028 |
unsigned long Index; |
| 1029 |
char Char = 0; |
| 1030 |
|
| 1031 |
|
| 1032 |
if ((Index = BrackIndex(c)) != ~0UL) |
| 1033 |
Char = BrOrder[Index ^ 1]; |
| 1034 |
|
| 1035 |
return (Char); |
| 1036 |
} |