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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
|
/*
* ChkTeX v1.5, utility functions.
* Copyright (C) 1995-96 Jens T. Berger Thielemann
*
* This program 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.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Contact the author at:
* Jens Berger
* Spektrumvn. 4
* N-0666 Oslo
* Norway
* E-mail: <jensthi@ifi.uio.no>
*
*
*/
#include "ChkTeX.h"
#include "Utility.h"
#include "Resource.h"
#include "OpSys.h"
#ifdef ASM_HASHWORD
extern UWORD HashWord(STRPTR a);
typedef UWORD HASH_TYPE;
#else
typedef ULONG HASH_TYPE;
#endif
/***************************** SUPPORT FUNCTIONS ************************/
/*
* Copies a string with a maximum length of `len' starting at `pos' in
* `source' into `dest'.
* Returns -1 if the pos value is beyond the length of the source value,
* else NULL.
*/
WORD substring(const STRPTR source, STRPTR dest, ULONG pos, LONG len)
{
STRPTR Start;
WORD Retval = -1;
if(len >= 0)
{
if(strlen(source) > pos)
{
Start = &source[pos];
while((len-- > 0) && (*dest++ = *Start++))
;
if(len == -1)
Retval = 0;
}
}
else
Retval = 0L;
*dest = 0;
return(Retval);
}
/*
* Determine whether a file exists.
*
*/
BOOL fexists(const STRPTR Filename)
{
BOOL Retval;
#if defined(F_OK) && defined(R_OK) && defined(HAVE_ACCESS)
Retval = access(Filename, F_OK|R_OK) == 0;
#else
FILE *fh;
if(fh = fopen(Filename, "r"))
{
Retval = TRUE;
fclose(fh);
}
else
Retval = FALSE;
#endif
return(Retval);
}
/*
* 'Safe' memset() replacement.
* Just tries to check the parameters, so that the risk of killing
* innocent memory is lowered.
* Please note that the `n' parameter now is an signed longword, not
* an size_t. Won't permit that `n' exceeds BUFLEN, nor negative sizes.
* Returns `to' if some memset()'ing was done, NULL if not.
*/
APTR sfmemset(APTR to, int c, LONG n)
{
if(to && (n > 0))
{
n = min(n, BUFSIZ);
return(memset(to, c, (size_t) n));
}
return(NULL);
}
/*
* Quick replace function
* Replaces every occurrence of a character in a string with another one.
*/
void strrep(STRPTR String, /* String to replace within. */
const TEXT From, /* Character to search for. */
const TEXT To) /* Character to put instead. */
{
register int c;
while((c = *String++))
{
if(c == From)
String[-1] = To;
}
}
/*
* Replaces every char not in Prot with To in Buf
*/
void strxrep(STRPTR Buf,
const STRPTR Prot,
const TEXT To)
{
int c;
while((c = *Buf))
{
if(!strchr(Prot, c))
*Buf = To;
Buf++;
}
}
/*
* Strips tail and/or front of a string
* Kills trailing/leading spaces. The macro/function LATEX_SPACE(char c)
* is used to decide whether a space should be skipped. This function
* should return TRUE if the character should be skipped, FALSE if not.
* Returns the string which was passed onto it.
*/
STRPTR strip(STRPTR str, /* String to strip */
const enum Strip flags)
/* One of the following: */
/* STRP_LFT - Strips leading blanks */
/* STRP_RGT - Strips trailing blanks */
/* STRP_BTH - Strips on both sides */
{
STRPTR bufptr = str, nlptr;
TEXT c;
if(bufptr && (c = *bufptr))
{
if(flags & STRP_LFT)
{
if(LATEX_SPACE(c) && c)
{
do
{
c = *++bufptr;
} while(LATEX_SPACE(c) && c);
}
}
if(flags & STRP_RGT)
{
if(c && *bufptr)
{
nlptr = bufptr;
while(*++nlptr);
do
{
*nlptr = 0;
c = *--nlptr;
} while(LATEX_SPACE(c) && c && (nlptr > bufptr));
}
else
*bufptr = 0;
}
}
return(bufptr);
}
/*
* Converts all the chars in the string passed into lowercase.
*/
#ifndef HAVE_STRLWR
STRPTR strlwr(STRPTR String)
{
STRPTR Bufptr;
TEXT TmpC;
for(Bufptr = String;
(TmpC = *Bufptr);
Bufptr++)
*Bufptr = tolower(TmpC);
return(String);
}
#endif
/*
* Returns a duplicate of the string passed.
*/
#ifndef HAVE_STRDUP
STRPTR strdup(const STRPTR String)
{
STRPTR Retval = NULL;
size_t Len;
if(String)
{
Len = strlen(String) + 1;
if((Retval = malloc(Len)))
memcpy(Retval, String, Len);
}
return(Retval);
}
#endif
/*
* Does the same as strdup, but adds a zero-filled padding, length extra bytes.
*/
STRPTR strdupx(const STRPTR String, int Extra)
{
STRPTR Retval = NULL;
size_t Len;
if(String)
{
Len = strlen(String) + 1 + Extra;
if((Retval = malloc(Len)))
strncpy(Retval, String, Len);
}
return(Retval);
}
/*
* Case-insensitive comparison of two strings.
*/
#ifndef HAVE_STRCASECMP
int strcasecmp(const char* a, const char *b)
{
int aa, bb;
do
{
aa = *a++;
bb = *b++;
} while(aa && (tolower(aa) == tolower(bb)));
/* bb != 0 is implicit */
return(tolower(aa) - tolower(bb));
}
#endif
/*
* Not all reallocs are intelligent enough to handle NULL's as
* parameters. This fixes this.
*/
void *saferealloc(void *b, size_t n)
{
APTR Retval = NULL;
if(b)
{
if(n)
Retval = realloc(b, n);
else
free(b);
}
else
Retval = malloc(n);
return(Retval);
}
/*
* Repeatedly writes the From string over To so that we overwrite Len bytes.
* Does nothing if passed empty/NULL string.
*/
void strwrite (STRPTR To, const STRPTR From, ULONG Len)
{
ULONG i, j;
ULONG FromLen = strlen(From);
Len = min(Len, BUFSIZ);
if(To && From)
{
switch(FromLen)
{
case 0:
break;
case 1:
memset(To, *From, Len);
break;
default:
for(i = j = 0;
i < Len;
i++, j++)
{
if(j >= FromLen)
j = 0;
To[i] = From[j];
}
}
}
}
/*
* Checks whether Cmp comes after Str.
*
*/
int strafter(const STRPTR Str, const STRPTR Cmp)
{
return(strncmp(Str, Cmp, strlen(Cmp)));
}
/*
* Checks whether Cmp comes before Str. Returns 0 if so, non-zero if not.
*
*/
int strinfront(STRPTR Str, STRPTR Cmp)
{
int CmpLen;
if((CmpLen = strlen(Cmp)))
{
Cmp += CmpLen;
Str++;
while((*--Cmp == *--Str) && (--CmpLen > 0))
;
return(CmpLen);
}
else
return(1);
}
/*************************** HASH INDEX **************************/
/*
* Hashes a string. The string ought be rather short. We use an asm
* version the Amiga; note that this returns an UWORD instead.
*
* The algorithm was designed by Peter Weinberger. This version was
* adapted from Dr Dobb's Journal April 1996 page 26.
*/
#ifndef ASM_HASHWORD
inline static ULONG HashWord(STRPTR str)
{
register ULONG h = 0, hbit, c;
while((c = *str++))
{
h = (h<<4) ^ c;
if((hbit = h & 0xf0000000))
h ^= hbit>>24;
h &= ~hbit;
}
return(h);
}
#endif
/*
* Inserts a string into a hash index. Note: You'll have to
* duplicate the string yourself.
*/
inline void InsertHash(const STRPTR a, struct Hash *h)
{
struct HashEntry **he, *newhe;
if(!h->Index)
{
ifn((h->Index = calloc(HASH_SIZE, sizeof(struct HashEntry *))))
PrintPrgErr(pmWordListErr);
}
he = &h->Index[HashWord(a) % HASH_SIZE];
if((newhe = malloc(sizeof(struct HashEntry))))
{
newhe->Next = *he;
newhe->Str = a;
*he = newhe;
}
else
PrintPrgErr(pmWordListErr);
}
/*
* Checks whether a string previously has been registered in a
* hash index.
*/
inline STRPTR HasHash(const STRPTR a, const struct Hash *h)
{
struct HashEntry *he;
HASH_TYPE i; /* Special magic to optimize SAS/C */
i = HashWord(a);
i %= HASH_SIZE;
he = h->Index[i];
while(he)
{
if(!strcmp(he->Str, a))
return(he->Str);
else
he = he->Next;
}
return(NULL);
}
/*
* Clears a hash table.
*/
void ClearHash(struct Hash *h)
{
if(h && h->Index)
memset(h->Index, '\0', HASH_SIZE * sizeof(struct HashEntry *));
}
/*
* Rehashes a wordlist. If you change any of the elem's, you must
* call this.
*
*/
static void ReHash(struct WordList *WL)
{
ULONG i = 0;
ClearHash(&WL->Hash);
FORWL(i, *WL)
InsertHash(WL->Stack.Data[i], &WL->Hash);
}
/*************************** WORDLIST HANDLING **************************/
/*
* Inserts a duplicate of `Word' into the `Wordlist' structure. You do thus
* not need to make a duplicate of `Word' yourself.
*/
BOOL InsertWord(const STRPTR Word, struct WordList *WL)
{
STRPTR WrdCpy;
ULONG Len;
if((WrdCpy = strdupx(Word, WALLBYTES)))
{
if(StkPush(WrdCpy, &WL->Stack))
{
Len = strlen(Word);
if(WL->MaxLen < Len)
WL->MaxLen = Len;
InsertHash(WrdCpy, &WL->Hash);
return(TRUE);
}
free(WrdCpy);
}
return(FALSE);
}
/*
* Clears a WordList; removing all items.
*/
void ClearWord(struct WordList *WL)
{
if(WL)
{
WL->Stack.Used = 0;
WL->MaxLen = 0;
ClearHash(&WL->Hash);
if(WL->NonEmpty)
InsertWord("", WL);
}
}
/*
* Query whether a `Word' is previously InsertWord()'ed into the WL
* structure. Does case-sensitive comparison.
*
* Returns the data in the list.
*/
STRPTR HasWord(const STRPTR Word, struct WordList *WL)
{
return(HasHash(Word, &WL->Hash));
}
/*
* Make all the words in a list lower case for later case-insensitive
* comparison.
*/
void MakeLower(struct WordList *wl)
{
ULONG i;
FORWL(i, *wl)
strlwr(wl->Stack.Data[i]);
ReHash(wl);
}
/*
* Calls strrep on each argument in a list.
*/
void ListRep(struct WordList *wl, const TEXT From, const TEXT To)
{
ULONG i;
FORWL(i, *wl)
strrep(wl->Stack.Data[i], From, To);
ReHash(wl);
}
/************************** GENERIC STACK ******************************/
/*
* Push something onto a stack. Returns TRUE if successful, else FALSE.
* Note: You can not push a NULL Data element.
*/
BOOL StkPush(const APTR Data, struct Stack *Stack)
{
ULONG NewSize;
APTR *NewBuf;
if(Data && Stack)
{
if(Stack->Used >= Stack->Size)
{
NewSize = Stack->Size + MINPUDDLE;
if((NewBuf = saferealloc(Stack->Data,
(size_t) NewSize * sizeof(APTR))))
{
Stack->Size = NewSize;
Stack->Data = NewBuf;
}
else
return(FALSE);
}
Stack->Data[Stack->Used++] = Data;
return(TRUE);
}
return(FALSE);
}
/*
* Pops an element from the stack.
*
*/
inline APTR StkPop(struct Stack *Stack)
{
APTR Retval = NULL;
if(Stack && (Stack->Used > 0))
{
Retval = Stack->Data[--Stack->Used];
#ifdef NO_DIRTY_TRICKS
{
APTR *NewBuf;
if(Stack->Used < (Stack->Size/2))
{
ULONG NewSize;
NewSize = Stack->Size - MINPUDDLE;
NewSize = max(NewSize, MINPUDDLE);
if(NewBuf = saferealloc(Stack->Data,
(size_t) NewSize * sizeof(APTR)))
{
Stack->Size = NewSize;
Stack->Data = NewBuf;
}
}
}
#endif
}
return(Retval);
}
/*
* Returns the topmost element of the stack.
*/
APTR StkTop(struct Stack *Stack)
{
if(Stack && (Stack->Used > 0))
return(Stack->Data[Stack->Used - 1]);
else
return(NULL);
}
/****************************** INPUT STACK *****************************/
BOOL PushFileName(const STRPTR Name, struct Stack *stack)
{
FILE *fh = NULL;
static
TEXT NameBuf [BUFSIZ];
if(Name && stack)
{
if(LocateFile(Name, NameBuf, ".tex", &TeXInputs))
{
if((fh = fopen(NameBuf, "r")))
{
return(PushFile(NameBuf, fh, stack));
}
}
PrintPrgErr(pmNoTeXOpen, Name);
}
return(FALSE);
}
BOOL PushFile(STRPTR Name, FILE *fh, struct Stack *stack)
{
struct FileNode *fn;
if(Name && fh && stack)
{
if((fn = malloc(sizeof(struct FileNode))))
{
if((fn->Name = strdup(Name)))
{
fn->fh = fh;
fn->Line = 0L;
if(StkPush(fn, stack))
return(TRUE);
free(fn->Name);
}
free(fn);
}
PrintPrgErr(pmNoStackMem);
}
return(FALSE);
}
STRPTR FGetsStk(STRPTR Dest, ULONG len, struct Stack *stack)
{
struct FileNode *fn;
STRPTR Retval = NULL;
if((fn = StkTop(stack)))
{
do
{
if((Retval = fgets(Dest, (int) len, fn->fh)))
{
fn->Line++;
break;
}
fn = StkPop(stack);
fclose(fn->fh);
free(fn);
} while(!Retval && (fn = StkTop(stack)));
}
return(Retval);
}
STRPTR CurStkName(struct Stack *stack)
{
struct FileNode *fn;
static
STRPTR LastName = "";
if(PseudoInName && (stack->Used <= 1))
return(PseudoInName);
else
{
if((fn = StkTop(stack)))
return(LastName = fn->Name);
else
return(LastName);
}
}
FILE *CurStkFile(struct Stack *stack)
{
struct FileNode *fn;
if((fn = StkTop(stack)))
return(fn->fh);
else
return(NULL);
}
ULONG CurStkLine(struct Stack *stack)
{
struct FileNode *fn;
static
ULONG LastLine = 0L;
if((fn = StkTop(stack)))
return(LastLine = fn->Line);
else
return(LastLine);
}
/************************** CHARACTER STACK ******************************/
/*
* Pushes the character on the stack.
*/
struct ErrInfo *PushChar(const TEXT c, const ULONG Line,
const ULONG Column, struct Stack *Stk,
const STRPTR LineCpy)
{
TEXT Buf[2];
Buf[0] = c; Buf[1] = 0;
return(PushErr( Buf, Line, Column, 1, LineCpy, Stk));
}
struct ErrInfo *PushErr(const STRPTR Data, const ULONG Line,
const ULONG Column, const ULONG ErrLen,
const STRPTR LineCpy, struct Stack *Stk)
{
struct ErrInfo *ci;
if((ci = malloc(sizeof(struct ErrInfo))))
{
if((ci->Data = strdup(Data)))
{
ci->File = CurStkName(&InputStack);
ci->Line = Line;
ci->ErrLen = ErrLen;
ci->Column = Column;
ci->LineBuf = LineCpy;
ci->Flags = efNone;
if(StkPush(ci, Stk))
return(ci);
}
else
PrintPrgErr(pmStrDupErr);
free(ci);
}
return(NULL);
}
/*
* Finds the uppermost entry in the stack with a data matching
* String.
*/
struct ErrInfo *TopMatch(struct Stack *Stack, STRPTR String)
{
int i;
struct ErrInfo *retval = NULL;
if(Stack && String)
{
for(i = Stack->Used - 1;
i >= 0;
i--)
{
if(!strcmp(String, ((struct ErrInfo *) Stack->Data[i])->Data))
{
retval = (struct ErrInfo *) Stack->Data[i];
break;
}
}
}
return(retval);
}
/*
* Returns and removes a character from the stack, returns NULL if
* the stack is empty.
*/
struct ErrInfo *PopErr(struct Stack *Stack)
{
return((struct ErrInfo *) StkPop(Stack));
}
/*
* Same as PopChar(), but lets the error alone on the stack.
*/
struct ErrInfo *TopErr(struct Stack *Stack)
{
return((struct ErrInfo *) StkTop(Stack));
}
/*
* Free all resources associated with a struct FreeInfo.
*/
void FreeErrInfo(struct ErrInfo* ei)
{
if(ei) {
if(ei->Data)
free(ei->Data);
free(ei);
}
}
/************************* OPEN/CLOSE COUNTING **************************/
/*
* Returns the index a given bracket (`()[]{}') character has in the
* BrOrder array. Returns ~0 if the character was not a bracket.
*/
LONG BrackIndex(const TEXT c)
{
switch(c)
{
case '(':
return(0);
case ')':
return(1);
case '[':
return(2);
case ']':
return(3);
case '{':
return(4);
case '}':
return(5);
default:
return(~0L);
}
}
/*
* Counts brackets for you. Give it a bracket, and it will update the
* corresponding counter.
*/
void AddBracket(const TEXT c)
{
LONG Index;
if((Index = BrackIndex(c)) != -1)
Brackets[Index]++;
}
/*
* Returns the character that matches the given bracket, NULL if `c'
* wasn't a bracket character.
*/
TEXT MatchBracket(const TEXT c)
{
ULONG Index;
TEXT Char = 0;
if((Index = BrackIndex(c)) != ~0UL)
Char = BrOrder[Index ^ 1];
return(Char);
}
|