File: lacheck.lex

package info (click to toggle)
lacheck 1.26-14
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 312 kB
  • ctags: 229
  • sloc: makefile: 223
file content (1118 lines) | stat: -rw-r--r-- 26,174 bytes parent folder | download | duplicates (8)
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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
/*                    -*- Mode: C -*-
 * 
 * lacheck.lex - A consistency checker checker for LaTeX documents
 *	
 * Copyright (C) 1991, 1992 Kresten Krab Thorup.
 * Copyright (C) 1993 --- 1998 Per Abrahamsen.
 *
 * 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 1, 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.
 *
 * Please send bugs, suggestions, and queries to auc-tex_mgr@sunsite.auc.dk.
 * 
 * $Revision: 1.26 $
 * Author          : Kresten Krab Thorup
 * Created On      : Sun May 26 18:11:58 1991
 * 
 * HISTORY
 * 07-Mar-1998		Per Abrahamsen
 *    Added return to yywrap.  Patch by Fabrice POPINEAU 
 *    <popineau@esemetz.ese-metz.fr>.
 * 14-Jan-1998		Per Abrahamsen
 *    Added GPL blurp.
 * 27-Oct-1997		Per Abrahamsen
 *    Count newline after newenvironment and newcommand.
 * 12-Jan-1996          Per Abrahamsen
 *    \\} used not to end a group in definitions.  Reported by Piet
 *    van Oostrum <piet@cs.ruu.nl>.
 * 03-Jan-1995		Per Abrahamsen
 *    Fix bug which prevented detection of multiple illegal characters
 *    in labels.  Reported by eeide@jaguar.cs.utah.edu (Eric Eide).
 * 30-Jul-1994		Per Abrahamsen
 *    Define dummy yywrap so we no longer depend on `libl.a'.
 * 26-Apr-1994		Per Abrahamsen
 *    Removed a few warnings, by Richard Lloyd <R.K.Lloyd@csc.liv.ac.uk>.
 * 23-Apr-1994		Per Abrahamsen
 *    Changed all `%i' to `%d' for VMS portability.  Reported by
 *    Stephen Harker <PHS172M@vaxc.cc.monash.edu.au>.
 * 16-Feb-1994		Per Abrahamsen
 *    Try file name with `.tex' appended before trying it bare.  This
 *    will make the case where a directory and a TeX file share the
 *    same name work.
 * 19-Jan-1994		Per Abrahamsen
 *    Comments don't imply whitespace.  Pointed out by Jacco van
 *    Ossenbruggen <jrvosse@cs.vu.nl>.
 * 14-Jan-1994		Per Abrahamsen
 *    Don't complain about \ref at the beginning of a paragraph.
 *    Suggested by Jean-Marc Lasgouttes <Jean-Marc.Lasgouttes@inria.fr>.
 * 11-Jan-1994		Per Abrahamsen
 *    Added version string to usage message.  Suggested by Uwe Bonnes
 *    <bon@LTE.E-TECHNIK.uni-erlangen.de> .
 * 04-Jan-1994		Per Abrahamsen
 *    Warn about newlines in \verb.  Suggested by Mark Burton
 *    <markb@ordern.demon.co.uk>.  The LaTeX Book agrees (p. 168).
 * 10-Sep-1993          Per Abrahamsen
 *    Removed complain about missing ~ before \cite.  Requested by 
 *    Nelson H. F. Beebe <beebe@math.utah.edu>.  The LaTeX Book seems
 *    to agree.  
 * 03-Sep-1993	        Per Abrahamsen
 *    Check for illegal characters in labels.
 * 16-Aug-1993	        Per Abrahamsen
 *    Recognize \endinput.  Suggested by Stefan Farestam
 *    <Stefan.Farestam@cerfacs.fr>.
 * 13-Aug-1993          Per Abrahamsen
 *    } was eaten after display math.  Reported by Eckhard R�ggeberg
 *    <eckhard@ts.go.dlr.de>.
 * 13-Aug-1993          Per Abrahamsen
 *    Recognize \verb*.  Reported by Eckhard R�ggeberg
 *    <eckhard@ts.go.dlr.de>.  
 * 08-Aug-1993          Per Abrahamsen
 *    Better catch begin and end without arguments.
 * 08-Aug-1993          Per Abrahamsen
 *    Removed free(NULL) as reported by Darrel R. Hankerson 
 *    <hankedr@mail.auburn.edu>.
 * 08-Aug-1993		Per Abrahamsen
 *    Removed declaration of realloc for some C compilers.  Reported by 
 *    Darrel R. Hankerson <hankedr@mail.auburn.edu>
 * 30-Jul-1993          Per Abrahamsen
 *    Added check for italic correction after normal text.
 * 29-Jul-1993          Per Abrahamsen
 *    Added cast for (char*) malloc as suggested by John Interrante
 *    <interran@uluru.Stanford.EDU>.
 * 29-Jul-1993          Per Abrahamsen
 *    Added check for missing and extra italic correction.
 * 29-Jul-1993	        Per Abrahamsen
 *    Made line number counting more reliable (but it still needs a rewrite)!
 * 28-Jul-1993	        Per Abrahamsen
 *    Added check for italic correction before point or comma.
 * 6-Jun-1992		Kresten Krab Thorup	
 *    Last Modified: Sat Jun  6 16:37:44 1992 #48 (Kresten Krab Thorup)
 *    Added test for whitespace before punctuation mark
 * 17-Dec-1991  (Last Mod: Tue Dec 17 21:01:24 1991 #41)  Kresten Krab Thorup
 *    Added 'word word` and missing ~ before cite and ref
 * 18-Jun-1991  (Last Mod: Tue Jun 18 19:20:43 1991 #17)  Kresten Krab Thorup
 *    Added check (or rather management) for \newenvironment and
 *    \newcommand - as suggested by Per Abrahamsen abrham@hugin.dk
 * 30-May-1991  (Last Mod: Thu May 30 02:22:33 1991 #15)  Kresten Krab Thorup
 *    Added check for `$${punct}' and `{punct}$' constructions
 * 30-May-1991  (Last Mod: Wed May 29 10:31:35 1991 #6)  Kresten Krab Thorup
 *    Improved (dynamic) stack management from Andreas Stolcke ...
 *                                       <stolcke@ICSI.Berkeley.EDU> 
 * 26-May-1991  Kresten Krab Thorup
 *    Initial distribution version.
 */

%{ 

#include <stdio.h>
#include <string.h>
/* #include <sys/param.h> */

/* extern char *realloc(); */

int yywrap() { return 1; }

#ifdef NEED_STRSTR
char *strstr();
#endif

#define GROUP_STACK_SIZE 10
#define INPUT_STACK_SIZE 10

#define PROGNAME "LaCheck"

  /* macros */

#define CG_NAME (char *)gstack[gstackp-1].s_name
#define CG_TYPE gstack[gstackp-1].s_type
#define CG_LINE gstack[gstackp-1].s_line
#define CG_ITALIC gstack[gstackp-1].italic
#define CG_FILE gstack[gstackp-1].s_file

char *bg_command();
void pop();
void push();
void linecount();
void g_checkend();
void e_checkend();
void f_checkend();
void input_file();
void print_bad_match();
int check_top_level_end();

  /* global variables */

char returnval[100];
int line_count = 1;
int warn_count = 0;
char *file_name;
char verb_char;

  /* the group stack */

typedef struct tex_group 
 {
    unsigned char *s_name;
    int s_type;
    int s_line;
    int italic;
    char *s_file; 
 } tex_group;

tex_group *gstack;
int gstack_size = GROUP_STACK_SIZE;
int gstackp = 0;

typedef struct input_ 
 {
    YY_BUFFER_STATE stream;
    char *name;
    int linenum;
 } input_;

input_ *istack;
int istack_size = INPUT_STACK_SIZE;
int istackp = 0;

int def_count = 0;

%}

%x B_ENVIRONMENT E_ENVIRONMENT VERBATIM INCLUDE MATH COMMENT VERB DEF
%x AFTER_DISPLAY ENV_DEF ICOR GETICOR

b_group ("{"|\\bgroup)
e_group ("}"|\\egroup)

b_math \\\(
e_math \\\)
math \$

b_display \\\[
e_display \\\]
display \$\$

par ([ \t]*\n[ \t]*\n[ \t\n]*)
non_par_ws ([ \t]+\n?[ \t]*|[ \t]*\n[ \t]*|[ \t]*\n?[ \t]+)

ws [ \n\t](%[^\n]\n)*
space ({ws}|\~|\\space)
hard_space (\~|\\space)

u_letter [A-Z���] 
l_letter [a-z���] 
punct [\!\.\?]
atoz [a-zA-Z]
letter [A-Z���a-z���]

c_bin ("-"|"+"|"\\cdot"|"\\oplus"|"\\otimes"|"\\times")
l_bin (",")

general_abbrev {letter}+{punct}

non_abbrev {u_letter}{u_letter}+{punct}

italic_spec (sl|it)
normal_spec normalshape
swap_spec em
font_spec (rm|bf|{italic_spec}|tt|{swap_spec}|mediumseries|{normal_spec})

primitive \\(above|advance|catcode|chardef|closein|closeout|copy|count|countdef|cr|crcr|csname|delcode|dimendef|dimen|divide|expandafter|font|hskp|vskip|openout)

symbol ("$"("\\"{atoz}+|.)"$"|"\\#"|"\\$"|"\\%"|"\\ref")

%%

<*>"\\\\" { ; }

<ENV_DEF,DEF,INITIAL>"\\\%" { ; }

<ICOR,GETICOR,ENV_DEF,DEF,INITIAL>"%"[^\n]*\n { line_count++; }

<ICOR,GETICOR,ENV_DEF,DEF,INITIAL>\n 	{ line_count++; }

<ENV_DEF,DEF,INITIAL>"\\\{" { ; }

<ENV_DEF,DEF,INITIAL>"\\\}" { ; }

"\\\$" { ; }

<ICOR,INITIAL,GETICOR>"{"{ws} {  
  if (CG_TYPE != 4 && CG_TYPE != 5) {
    if (!(CG_TYPE == 2 && strstr(CG_NAME, "array"))) {
      printf( "\"%s\", line %d: possible unwanted space at \"{\"\n", 
	     file_name, line_count); 
      ++warn_count ;
    }
  }
  push( "{", 0, line_count);
  linecount();
 }

<ICOR,INITIAL,GETICOR>{b_group} {  push( "{", 0, line_count);}

<INITIAL,GETICOR>{e_group} {  
  {
    int italic = CG_ITALIC;
    g_checkend(0); 
    if (italic && !CG_ITALIC)
      BEGIN(GETICOR) ;
    else
      BEGIN(INITIAL);
  }}

<ICOR>{e_group} {  g_checkend(0); }

<GETICOR>[A-Za-z������0-9;:!()]+ {
 {
   if (!CG_ITALIC)
     {
       printf("\"%s\", line %d: you may need a \\/ before \"%s\"\n",
	      file_name, line_count, yytext); 
       ++warn_count;
     }
    BEGIN(INITIAL); 
 }}

<ICOR>[A-Za-z������0-9;:!?()`']+ {
 {
   if (CG_ITALIC)
     {
       printf("\"%s\", line %d: \\/ not needed before italic text \"%s\"\n",
	      file_name, line_count, yytext); 
       ++warn_count;
     }
    BEGIN(INITIAL); 
 }}

^[A-Za-z������0-9;:!?()`',.]+{ws}*/\\\/ {
  {
   linecount();
   if (!CG_ITALIC)
     {
       printf("\"%s\", line %d: \\/ not needed after non-italic text \"%s\"\n",
              file_name, line_count, yytext);
       ++warn_count;
     }
 }}

{ws}[A-Za-z������0-9;:!?()`',.]+{ws}*/\\\/ {
  {
   linecount();
   if (!CG_ITALIC)
     {
       printf("\"%s\", line %d: \\/ is not needed after non-italic \"%s\"\n",
              file_name, line_count, yytext);
       ++warn_count;
     }
 }}

<GETICOR>\\\/ { BEGIN(INITIAL); }

<INITIAL>\\\/ { BEGIN(ICOR); }

<ICOR>\\\/ {
  {
    printf("\"%s\", line %d: double \\/ found \"%s\"\n",
           file_name, line_count, yytext);
    ++warn_count;
    BEGIN(ICOR);
  }}

<INITIAL,GETICOR,ICOR>\\{italic_spec}/[^a-zA-Z] { CG_ITALIC = 1; }

<INITIAL,GETICOR>\\{normal_spec}/[^a-zA-Z] {
  {
    if(CG_ITALIC)
      BEGIN(GETICOR);
    else
      BEGIN(INITIAL);
    CG_ITALIC = 0;
  }}

<ICOR>\\{normal_spec}/[^a-zA-Z] { CG_ITALIC = 0; }

<INITIAL,GETICOR>\\{swap_spec}/[^a-zA-Z] {
  {
    if(CG_ITALIC)
      BEGIN(GETICOR);
    else
      BEGIN(INITIAL);
    CG_ITALIC = !CG_ITALIC;
  }}

<ICOR>\\{swap_spec}/[^a-zA-Z] { CG_ITALIC = !CG_ITALIC; }

<ICOR>[,.] {
 {
    printf("\"%s\", line %d: do not use \\/ before \"%s\"\n",
	   file_name, line_count, yytext); 
    ++warn_count; 
    BEGIN(INITIAL);
 }}

<GETICOR,ICOR>{ws} { ; }

<GETICOR,ICOR>~ { ; }

<GETICOR,ICOR>[^\n] { 
  {
    unput(yytext[0]);
    BEGIN(INITIAL); 
  }}

"\\"[exg]?(def|newcommand)[^\n\{]+ 	BEGIN(DEF);

<DEF>{b_group} { ++def_count; }

<DEF>{e_group} { --def_count;
		 if(def_count == 0)
		     BEGIN(INITIAL); }

<DEF>. { ; }

"\\"newenvironment"{"[a-zA-Z]+"}"[^\n\{]+ 	BEGIN(ENV_DEF);

<ENV_DEF>{b_group} { ++def_count; }

<ENV_DEF>{e_group} { --def_count;
		 if(def_count == 0)
		     BEGIN(DEF); }

<ENV_DEF>. { ; }

{b_math} {
    if(CG_TYPE == 4 || CG_TYPE == 5)
	print_bad_match(yytext,4);
    else
    {
	push( yytext, 4, line_count);
    }}

{e_math} {  g_checkend(4); }

{b_display} {
    if(CG_TYPE == 4 || CG_TYPE == 5)
	print_bad_match(yytext,5);
    else 
    {
	push( yytext, 5, line_count);
    }}


{e_display} {  g_checkend(5);     BEGIN(AFTER_DISPLAY);}

<AFTER_DISPLAY>{punct} { 

    printf( "\"%s\", line %d: punctuation mark \"%s\" should be placed before end of displaymath\n", 
	   file_name, line_count, yytext); 
    ++warn_count ; 

  BEGIN(INITIAL); }

<AFTER_DISPLAY>(\n|.) { unput(yytext[0]); BEGIN(INITIAL); }

<ICOR,INITIAL,GETICOR>{punct}/("\$"|"\\)") { if (CG_TYPE == 4)
       {
	 printf( "\"%s\", line %d: punctuation mark \"%s\" should be placed after end of math mode\n", 
		file_name, line_count, yytext); 
	 ++warn_count ;
	 BEGIN(INITIAL);
       }}

{math} {

    if(CG_TYPE == 5)
	print_bad_match(yytext, 4);
    else 

    if(CG_TYPE == 4)
    {
	e_checkend(4, yytext);
    }
    else
    {
	push( yytext, 4, line_count); 
    }}


{display}  {

    if(CG_TYPE == 4)
	print_bad_match(yytext,5);
    else 

    if(CG_TYPE == 5)
    {
	e_checkend(5, yytext);
        BEGIN(AFTER_DISPLAY);
    }
    else
    {
	push( yytext, 5, line_count);
    }}

\\begingroup/[^a-zA-Z]  {
 {
    push((unsigned char *)"\\begingroup", 1, line_count); 
 }}


\\endgroup/[^a-zA-Z]  {
 {
    g_checkend(1);
 }}


\\begin[ \t]*"{" { BEGIN(B_ENVIRONMENT); }

\\begin[ \t]*(%[^\n]*)?/\n { 
 {
    
    printf("\"%s\", line %d: {argument} missing for \\begin\n",
	   file_name, line_count) ;
    ++warn_count;
 }}

<B_ENVIRONMENT>[^\}\n]+ { 
 {
    if (strcmp( yytext, "verbatim" ) == 0 )
	{
	 input();
	 BEGIN(VERBATIM);
	}
    else
	{
    	 push(yytext, 2, line_count);

	 if (   strcmp (yytext, "sl" ) == 0
	     || strcmp (yytext, "it" ) == 0)
	   CG_ITALIC = 1;
	 else if (strcmp (yytext, "normalshape") == 0)
	   CG_ITALIC = 0;
	 else if (strcmp (yytext, "em") == 0)
	   CG_ITALIC = !CG_ITALIC;
	   
 	 input();
	 BEGIN(INITIAL);
	}
 }}

<VERBATIM>\\end[ \t]*\{verbatim\} { BEGIN(INITIAL); }

<VERBATIM>\t {
     printf("\"%s\", line %d: TAB character in verbatim environment\n",
	   file_name, line_count) ;
    ++warn_count;
 }

<VERBATIM>. { ; }

<VERBATIM>\n { ++line_count; }


<ICOR,INITIAL,GETICOR>\\verb\*?. { 
          verb_char = yytext[yyleng-1];
	  BEGIN(VERB); 
	}

<VERB>\n {
  printf("\"%s\", line %d: \\verb should not contain end of line characters\n",
	 file_name, line_count) ;
  ++line_count;
} 

<VERB>. {
  if ( *yytext == verb_char )
    BEGIN(INITIAL); 
} 


\\end[ \t]*"{" { BEGIN(E_ENVIRONMENT); }

\\end[ \t]*(%[^\n]*)?/\n { 
 {
    printf("\"%s\", line %d: {argument} missing for \\end\n",
	   file_name, line_count) ;
    ++warn_count;
 }}


<E_ENVIRONMENT>[^\}\n]+ { 
 {
    e_checkend(2, yytext);
    input();
    
    BEGIN(INITIAL);
 }}


<ICOR,INITIAL,GETICOR>{ws}({letter}".")*{letter}*{l_letter}"."/{non_par_ws}+{l_letter}    { 
 {
    linecount();
    printf( "\"%s\", line %d: missing `\\ ' after \"%s\"\n", 
	   file_name, line_count, ++yytext); 
    ++warn_count ;
    BEGIN(INITIAL);
 }}

<ICOR,INITIAL,GETICOR>({l_letter}".")*{letter}*{l_letter}"."/{non_par_ws}+{l_letter}    { 
 {
    printf( "\"%s\", line %d: missing `\\ ' after \"%s\"\n", 
	   file_name, line_count, yytext); 
    ++warn_count ; 
    BEGIN(INITIAL);
 }}

<ICOR,INITIAL,GETICOR>{non_abbrev}/{non_par_ws}{u_letter}   { 
 {
   linecount();
   printf("\"%s\", line %d: missing `\\@' before `.' in \"%s\"\n", 
	  file_name, line_count, yytext); 
   ++warn_count ; 
   BEGIN(INITIAL);
 }}

<ICOR,INITIAL,GETICOR>({hard_space}{space}|{space}{hard_space})  { 

    printf("\"%s\", line %d: double space at \"%s\"\n",
	   file_name, line_count, yytext); 
    ++warn_count;
	linecount();
    BEGIN(INITIAL);
  }

{c_bin}{ws}?(\\(\.|\,|\;|\:))*{ws}?\\ldots{ws}?(\\(\.|\,|\;|\:))*{ws}?{c_bin} {
	printf("\"%s\", line %d: \\ldots should be \\cdots in \"%s\"\n",
	   file_name, line_count, yytext); 
	++warn_count;
	linecount();
  }

<ICOR,INITIAL,GETICOR>[^\\]{l_bin}{ws}?(\\(\.|\,|\;|\:))*{ws}?\\cdots{ws}?(\\(\.|\,|\;|\:))*{ws}?[^\\]{l_bin} {
	printf("\"%s\", line %d: \\cdots should be \\ldots in \"%s\"\n",
	   file_name, line_count, yytext); 
	++warn_count;
	linecount();
    BEGIN(INITIAL);
  }

{c_bin}{ws}?(\\(\.|\,|\;|\:))*{ws}?"."+{ws}?(\\(\.|\,|\;|\:))*{ws}?{c_bin} {
	printf("\"%s\", line %d: Dots should be \\cdots in \"%s\"\n",
	   file_name, line_count, yytext); 
	++warn_count;
	linecount();
  }

<ICOR,INITIAL,GETICOR>[^\\]{l_bin}{ws}?(\\(\.|\,|\;|\:))*{ws}?"."+{ws}?(\\(\.|\,|\;|\:))*{ws}?[^\\]{l_bin} {
	printf("\"%s\", line %d: Dots should be \\ldots in \"%s\"\n",
	   file_name, line_count, yytext); 
	++warn_count;
	linecount();
    BEGIN(INITIAL);
  }


<ICOR,INITIAL,GETICOR>\.\.\. { 
    printf("\"%s\", line %d: Dots should be ellipsis \"%s\"\n",
	   file_name, line_count, yytext); 
    ++warn_count;
    BEGIN(INITIAL);
  }

<ICOR,INITIAL,GETICOR>\\label\{[^#$%^&*+={\~"<>\n\t }]*[#$%^&*+={\~"<>\n\t ][^%}]*\} {
    linecount();
    printf("\"%s\", line %d: bad character in label \"%s\", see C.10.2\n",
           file_name, line_count, yytext);
  }
    
<ICOR,INITIAL,GETICOR>{par}"\\"ref/[^A-Za-z]  {
    linecount();
    BEGIN(INITIAL);
  }

<ICOR,INITIAL,GETICOR>{ws}"\\"ref/[^A-Za-z]  {
    linecount();
    printf("\"%s\", line %d: perhaps you should insert a `~' before \"%s\"\n",
	   file_name, line_count, ++yytext); 
    BEGIN(INITIAL);
  }

<ICOR,INITIAL,GETICOR>{ws}"\\"footnote/[^A-Za-z]  {
    linecount();
    printf("\"%s\", line %d: whitespace before footnote in \"%s\"\n",
	   file_name, line_count, ++yytext); 
    BEGIN(INITIAL);
  }

 
{primitive}/[^a-zA-Z] {
 {
    printf("\"%s\", line %d: Don't use \"%s\" in LaTeX documents\n", 
	   file_name, line_count, yytext); 
    ++warn_count ; 
 }}    

\\left{ws}*\\?. { linecount() ;}
\\right{ws}*\\?. {	linecount(); }

<ICOR,INITIAL,GETICOR>[^\{]\\{font_spec}/[ \t]*"{" { 
 {
   linecount();
    printf("\"%s\", line %d: Fontspecifiers don't take arguments. \"%s\"\n", 
	   file_name, line_count, yytext); 
    ++warn_count; 
  /*    (void) input(); */
    BEGIN(INITIAL);
 }}

\\([a-zA-Z\@]+\@[a-zA-Z\@]*|[a-zA-Z\@]*\@[a-zA-Z\@]+) { 
 {
    printf("\"%s\", line %d: Do not use @ in LaTeX macro names. \"%s\"\n", 
	   file_name, line_count, yytext); 
    ++warn_count; 
 }}

<ICOR,INITIAL,GETICOR>{ws}"'"+{letter}+ { 
 {
   linecount();
    printf("\"%s\", line %d: Use ` to begin quotation, not ' \"%s\"\n", 
	   file_name, line_count, yytext); 
    ++warn_count; 
    BEGIN(INITIAL);
 }}

<ICOR,INITIAL,GETICOR>{letter}+"`" { 
 {
    printf("\"%s\", line %d: Use ' to end quotation, not ` \"%s\"\n", 
	   file_name, line_count, yytext); 
    ++warn_count; 
    BEGIN(INITIAL);
 }}


<ICOR,INITIAL,GETICOR>{ws}+{punct} { 
 {
    printf("\"%s\", line %d: Whitespace before punctation mark in \"%s\"\n", 
	   file_name, line_count, yytext); 
    ++warn_count; 
	linecount();
    BEGIN(INITIAL);
 }}

"%"  { BEGIN(COMMENT); }

<COMMENT>\n	{ BEGIN(INITIAL); ++line_count; }

<COMMENT>.	{ ; }


\\(input|include)([ \t]|"{")	{ BEGIN(INCLUDE); }

<INCLUDE>[^\}\n]+	{
 {
	if ( strstr(yytext,".sty") == NULL )
	{
	  printf("** %s:\n", yytext);
	  input_file(yytext);
	}
	else
	{
		printf("\"%s\", line %d: Style file `%s\' omitted.\n",
			file_name,
			line_count,
			yytext);
		input();
	}
	BEGIN(INITIAL);
 }}

\\endinput/[^A-Za-z] |
<<EOF>> { 
	  if (--istackp < 0)
		  yyterminate(); 

	  else
		{ 
		  fclose(yyin);
	  	  f_checkend(file_name);
		  yy_switch_to_buffer(istack[istackp].stream);
		  free(file_name);
		  line_count = istack[istackp].linenum;
		  file_name = istack[istackp].name;
		  input();
		  BEGIN(INITIAL);
		}    	
	 
	}


. { ; }
%%
int main( argc, argv )
int argc;
char *argv[];
{
    /* allocate initial stacks */
    gstack = (tex_group *)malloc(gstack_size * sizeof(tex_group));
    istack = (input_ *)malloc(istack_size * sizeof(input_));
    if ( gstack == NULL || istack == NULL ) {
	fprintf(stderr, "%s: not enough memory for stacks\n", PROGNAME);
	exit(3);
    }
	
    if(argc > 1)
    {
        if ( (file_name = (char*) malloc(strlen(argv[1]) + 5)) == NULL ) {
		fprintf(stderr, "%s: out of memory\n", PROGNAME);
		exit(3);
	}
	
	strcpy(file_name, argv[1]);
	strcat(file_name, ".tex" );
	
	if ((yyin = fopen( file_name, "r")) != NULL )
	{
	    push(file_name, 3, 1);
	    yylex();
	    f_checkend(file_name);
	}
	else {   
                 file_name[strlen(file_name) - 4] = '\0';
		 if ((yyin = fopen( file_name, "r")) != NULL )
		 {
		     push(file_name, 3, 1);
		     yylex();
		     f_checkend(file_name);
		 }
		 else
		     fprintf(stderr,
			     "%s: Could not open : %s\n",PROGNAME, argv[1]);
	     }
    }
    else
    {
	printf("\n* %s *\n\n",PROGNAME);
	printf("\t...a consistency checker for LaTeX documents.\n");
	printf("$Id: lacheck.lex,v 1.26 1998/03/07 07:46:45 abraham Exp $\n\n");

	printf("Usage:\n\tlacheck filename[.tex] <return>\n\n\n");

	printf("\tFrom within Emacs:\n\n");
	printf("\tM-x compile RET lacheck filename[.tex] RET\n\n");
	printf("\tUse C-x ` to step through the messages.\n\n");
	printf("\n\tThe found context is displayed in \"double quotes\"\n\n");
	printf("Remark:\n\tAll messages are only warnings!\n\n");
	printf("\tYour document may be right even though LaCheck says ");
	printf("something else.\n\n");
    }
    return(0);
}

#ifdef NEED_STRSTR
char *
strstr(string, substring)
    register char *string;	/* String to search. */
    char *substring;		/* Substring to try to find in string. */
{
    register char *a, *b;

    /* First scan quickly through the two strings looking for a
     * single-character match.  When it's found, then compare the
     * rest of the substring.
     */

    b = substring;
    if (*b == 0) {
	return string;
    }
    for ( ; *string != 0; string += 1) {
	if (*string != *b) {
	    continue;
	}
	a = string;
	while (1) {
	    if (*b == 0) {
		return string;
	    }
	    if (*a++ != *b++) {
		break;
	    }
	}
	b = substring;
    }
    return (char *) 0;
}
#endif /* NEED_STRSTR */

void push(p_name, p_type, p_line)
unsigned char *p_name;
int p_type;
int p_line;
{
    if ( gstackp == gstack_size ) {	/* extend stack */
	gstack_size *= 2;
	gstack = (tex_group *)realloc(gstack, gstack_size * sizeof(tex_group));
	if ( gstack == NULL ) {
		fprintf(stderr, "%s: stack out of memory", PROGNAME);
	exit(3);
    }
    }
    
    if ( (gstack[gstackp].s_name =
		(unsigned char *)malloc(strlen((char *)p_name) + 1)) == NULL ||
         (gstack[gstackp].s_file =
		(char *)malloc(strlen(file_name) + 1)) == NULL ) {
	fprintf(stderr, "%s: out of memory\n", PROGNAME);
	exit(3);
    }

    strcpy((char *)gstack[gstackp].s_name,(char *)p_name);
    gstack[gstackp].s_type = p_type;
    gstack[gstackp].s_line = p_line;	
    gstack[gstackp].italic = (  (p_type == 4 || p_type == 5)
			      ? 1
			      : (  gstackp
				 ? gstack[gstackp - 1].italic
				 : 0));
    strcpy(gstack[gstackp].s_file,file_name);
    ++gstackp;	

}

void input_file(file_nam)
char *file_nam;
{
    char *tmp_file_name;
    FILE *tmp_yyin;

    if ( (tmp_file_name = (char*) malloc(strlen(file_nam) + 5)) == NULL ) {
	fprintf(stderr, "%s: out of memory\n", PROGNAME);
	exit(3);
    }
    strcpy(tmp_file_name,file_nam);

    if (istackp == istack_size) {	/* extend stack */
	istack_size *= 2;
	istack = (input_ *)realloc(istack, istack_size * sizeof(input_));
	if ( istack == NULL ) {
		fprintf(stderr, "%s: \\input stack out of memory\n", PROGNAME);
	exit(3);
        } 
    } 
    	
    istack[istackp].stream = YY_CURRENT_BUFFER;
    istack[istackp].linenum = line_count;
    istack[istackp].name = file_name;
    ++istackp;    

    (void) strcat(tmp_file_name, ".tex");
    if ((tmp_yyin = fopen( tmp_file_name, "r")) != NULL )
	{
	  yyin = tmp_yyin;
	  yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE));
	  file_name = tmp_file_name;
	  push(file_name, 3, 1);
          line_count = 1;
	}
    else {
          tmp_file_name[strlen(tmp_file_name) - 4] = '\0';
	  if ((tmp_yyin = fopen( tmp_file_name , "r")) != NULL )
	    {
		yyin = tmp_yyin;
	   	yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE));
		file_name = tmp_file_name;
		push(file_name, 3, 1);
   	        line_count = 1;
	    }
          else
	  {
	       --istackp;
	       free(tmp_file_name);
	       printf("\"%s\", line %d: Could not open \"%s\"\n", 
			file_name,
			line_count,
			file_nam);
	       input();
	  }
	 }
}

void pop()
{
    if ( gstackp == 0 )
    {
       	fprintf(stderr, "%s: Stack underflow\n", PROGNAME);
	exit(4);
    }
    --gstackp;

    free(gstack[gstackp].s_name);
    free(gstack[gstackp].s_file);
}

char *bg_command(name)
char *name;
{
    
    switch (CG_TYPE) {
	
    case 2:
	(void) strcpy( returnval, "\\begin{" );
	(void) strcat( returnval, (char *) name);
	(void) strcat( returnval, "}" );
	break;
	
    case 3:
	(void) strcpy( returnval, "beginning of file " );
	(void) strcat( returnval, (char *) name);
	break;
    
    case 4:
	(void) strcpy( returnval, "math begin " );
	(void) strcat( returnval, (char *) name);
	break;
    
    case 5:
	(void) strcpy( returnval, "display math begin " );
	(void) strcat( returnval, (char *) name);
	break;
    
    default:
    	(void) strcpy( returnval, name );
	
    }
    
    return ((char *)returnval);
}

char *eg_command(name,type)
int type;
char *name;
{
    
    switch (type) {
	
    case 2:
	(void) strcpy( returnval, "\\end{" );
	(void) strcat( returnval, (char *) name);
	(void) strcat( returnval, "}" );
	break;
	
    case 3:
	(void) strcpy( returnval, "end of file " );
	(void) strcat( returnval, (char *) name);
	break;
    
    case 4:
	(void) strcpy( returnval, "math end " );
	(void) strcat( returnval, (char *) name);
	break;
    
    case 5:
	(void) strcpy( returnval, "display math end " );
	(void) strcat( returnval, (char *) name);
	break;
    
    default:
    	(void) strcpy( returnval, name );
	break;
    }
    
    return ((char *)returnval);
}


void g_checkend(n)
int n;
{
    if ( check_top_level_end(yytext,n) == 1 ) 
       if (  CG_TYPE != n  )
	 print_bad_match(yytext,n);
       else
	pop();
}

void e_checkend(n, name)
int n;
char *name;
{
   if ( check_top_level_end(name,n) == 1 )
    {
     if (  CG_TYPE != n  ||  strcmp( CG_NAME, name ) != 0 )
    	print_bad_match(name,n);

     pop();

    }
}

void f_checkend(name)
char *name;
{
    if ( check_top_level_end(name,3) == 1 )
     {
       if (  CG_TYPE != 3  ||  strcmp( CG_NAME, name ) != 0 )

    	while( CG_TYPE != 3  )
	{
	  print_bad_match(name,3);
          pop();
        }

         pop();  
     }
}

void print_bad_match(end_command,type)
char *end_command;	      
int type;
{
	  printf("\"%s\", line %d: <- unmatched \"%s\"\n",
	         file_name, 
		 line_count, 
		 eg_command( end_command , type) ) ;

	  printf("\"%s\", line %d: -> unmatched \"%s\"\n",
	         CG_FILE, 
		 CG_LINE, 
		 bg_command( CG_NAME ) ) ;
	  warn_count += 2;
}

int check_top_level_end(end_command,type)
char *end_command;	      
int type;
{
    if ( gstackp == 0 )
	{
	 printf("\"%s\", line %d: \"%s\" found at top level\n",
	       file_name, 
	       line_count, 
	       eg_command( end_command, type )) ;
	 ++warn_count;
         return(0);
	}
    else
    	return(1);
}

void linecount()
{
  int i;
  for (i = 0; i < yyleng; i++)
    if(yytext[i] == '\n')
      line_count++;
}