-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
1285 lines (1030 loc) · 38.2 KB
/
parser.c
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
/* parser.c - parser for LaTeX code
Copyright (C) 1998-2002 The Free Software Foundation
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
This file is available from http://sourceforge.net/projects/latex2rtf/
Authors:
1998-2000 Georg Lehner
2001-2007 Scott Prahl
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "main.h"
#include "commands.h"
#include "cfg.h"
#include "stack.h"
#include "utils.h"
#include "parser.h"
#include "fonts.h"
#include "lengths.h"
#include "definitions.h"
#include "funct1.h"
#include "encodings.h"
typedef struct InputStackType {
char *string;
char *string_start;
FILE *file;
char *file_name;
int file_line;
} InputStackType;
#define PARSER_SOURCE_MAX 100
#define SCAN_BUFFER_SIZE 5000
static InputStackType g_parser_stack[PARSER_SOURCE_MAX];
static int g_parser_depth = -1;
static char *g_parser_string = "stdin";
static FILE *g_parser_file = NULL;
static int g_parser_line = 1;
static int g_parser_include_level = 0;
static char g_parser_currentChar; /* Global current character */
static char g_parser_lastChar;
static char g_parser_penultimateChar;
static int g_parser_backslashes;
#define TRACK_LINE_NUMBER_MAX 10
static int g_track_line_number_stack[TRACK_LINE_NUMBER_MAX];
static int g_track_line_number = -1;
void PushTrackLineNumber(int flag)
/***************************************************************************
purpose: set whether or not line numbers should be tracked in LaTeX source file
****************************************************************************/
{
if (g_track_line_number >= TRACK_LINE_NUMBER_MAX)
diagnostics(ERROR, "scan ahead stack too large! Sorry.");
g_track_line_number++;
g_track_line_number_stack[g_track_line_number] = flag;
}
void PopTrackLineNumber(void)
/***************************************************************************
purpose: restore last state of line numbers tracking in LaTeX source file
****************************************************************************/
{
if (g_track_line_number < 0)
diagnostics(ERROR, "scan ahead stack too small! Sorry.");
g_track_line_number--;
}
/***************************************************************************
purpose: returns the current line number of the text being processed
****************************************************************************/
int CurrentLineNumber(void)
{
return g_parser_line;
}
void UpdateLineNumber(char *s)
/***************************************************************************
purpose: advances the line number for each '\n' in s
****************************************************************************/
{
if (s == NULL)
return;
while (*s != '\0') {
if (*s == '\n')
g_parser_line++;
s++;
}
}
/***************************************************************************
purpose: returns the current file descriptor
****************************************************************************/
int CurrentFileDescriptor(void)
{
int fd=0;
if (g_parser_file)
fd = fileno(g_parser_file);
return fd;
}
char *CurrentFileName(void)
/***************************************************************************
purpose: returns the filename of the text being processed
****************************************************************************/
{
char *s = "(Not set)";
if (g_parser_stack[g_parser_depth].file_name)
return g_parser_stack[g_parser_depth].file_name;
else
return s;
}
/*
The following two routines allow parsing of multiple files and strings
*/
int PushSource(const char *filename, const char *string)
/***************************************************************************
purpose: change the source used by getRawTexChar() to either file or string
--> pass NULL for unused argument (both NULL means use stdin)
--> PushSource duplicates string
****************************************************************************/
{
char s[50];
FILE *p = NULL;
char *name = NULL;
int i;
int line = 1;
if (0) {
diagnostics(WARNING, "Before PushSource** line=%d, g_parser_depth=%d, g_parser_include_level=%d",
g_parser_line, g_parser_depth, g_parser_include_level);
for (i = 0; i <= g_parser_depth; i++) {
if (g_parser_stack[i].file)
diagnostics(WARNING, "i=%d file =%s, line=%d", i, g_parser_stack[i].file_name, g_parser_stack[i].file_line);
else {
strncpy_printable(s, g_parser_stack[i].string, 25);
diagnostics(WARNING, "i=%d string =%s, line=%d", i, s, g_parser_stack[i].file_line);
}
}
}
/* save current values for linenumber and string */
if (g_parser_depth >= 0) {
g_parser_stack[g_parser_depth].file_line = g_parser_line;
g_parser_stack[g_parser_depth].string = g_parser_string;
}
/* first test to see if we should use stdin */
if ((filename == NULL || strcmp(filename, "-") == 0) && string == NULL) {
g_parser_include_level++;
g_parser_line = 1;
name = strdup("stdin");
p = stdin;
/* if not then try to open a file */
} else if (filename) {
p = my_fopen((char *)filename, "rb");
if (p == NULL)
return 1;
g_parser_include_level++;
g_parser_line = 1;
name = strdup(filename);
} else {
name = CurrentFileName();
line = CurrentLineNumber();
}
g_parser_depth++;
if (g_parser_depth >= PARSER_SOURCE_MAX)
diagnostics(ERROR, "More than %d PushSource() calls", (int) PARSER_SOURCE_MAX);
g_parser_string = (string) ? strdup(string) : NULL;
g_parser_stack[g_parser_depth].string = g_parser_string;
g_parser_stack[g_parser_depth].string_start = g_parser_string;
g_parser_stack[g_parser_depth].file = p;
g_parser_stack[g_parser_depth].file_line = line;
g_parser_stack[g_parser_depth].file_name = name;
g_parser_file = p;
g_parser_string = g_parser_stack[g_parser_depth].string;
if (g_parser_file) {
diagnostics(3, "Opening Source File %s", g_parser_stack[g_parser_depth].file_name);
} else {
diagnostics(4, "Opening Source string");
show_string(5, g_parser_string, "opening");
}
if (0) {
diagnostics(WARNING, "After PushSource** line=%d, g_parser_depth=%d, g_parser_include_level=%d",
g_parser_line, g_parser_depth, g_parser_include_level);
for (i = 0; i <= g_parser_depth; i++) {
if (g_parser_stack[i].file)
diagnostics(WARNING, "i=%d file =%s, line=%d", i, g_parser_stack[i].file_name, g_parser_stack[i].file_line);
else {
strncpy_printable(s, g_parser_stack[i].string, 25);
diagnostics(WARNING, "i=%d string =%s, line=%d", i, s, g_parser_stack[i].file_line);
}
}
}
return 0;
}
int StillSource(void)
/***************************************************************************
purpose: figure out if text remains to be processed
****************************************************************************/
{
if (g_parser_file)
return (!feof(g_parser_file));
else
return (*g_parser_string != '\0');
}
void EndSource(void)
{
if (g_parser_file)
fseek(g_parser_file, 0, SEEK_END);
else
*g_parser_string = '\0';
return;
}
void PopSource(void)
/***************************************************************************
purpose: return to the previous source
****************************************************************************/
{
char s[50];
int i;
if (g_parser_depth < 0) {
diagnostics(1, "Hmmm. More PopSource() calls than PushSource() calls");
return;
}
if (0) {
diagnostics(WARNING, "Before PopSource** line=%d, g_parser_depth=%d, g_parser_include_level=%d",
g_parser_line, g_parser_depth, g_parser_include_level);
for (i = 0; i <= g_parser_depth; i++) {
if (g_parser_stack[i].file)
diagnostics(WARNING, "i=%d file =%s, line=%d", i, g_parser_stack[i].file_name, g_parser_stack[i].file_line);
else {
strncpy_printable(s, g_parser_stack[i].string, 25);
diagnostics(WARNING, "i=%d string =%s, line=%d", i, s, g_parser_stack[i].file_line);
}
}
}
if (g_parser_file) {
diagnostics(3, "Closing Source File '%s'", g_parser_stack[g_parser_depth].file_name);
fclose(g_parser_file);
free(g_parser_stack[g_parser_depth].file_name);
g_parser_stack[g_parser_depth].file_name = NULL;
g_parser_include_level--;
}
if (g_parser_string) {
if (strlen(g_parser_stack[g_parser_depth].string_start) < 49)
strcpy(s, g_parser_stack[g_parser_depth].string_start);
else {
strncpy(s, g_parser_stack[g_parser_depth].string_start, 49);
s[49] = '\0';
}
show_string(5, s, "closing");
free(g_parser_stack[g_parser_depth].string_start);
g_parser_stack[g_parser_depth].string_start = NULL;
}
g_parser_depth--;
if (g_parser_depth >= 0) {
g_parser_string = g_parser_stack[g_parser_depth].string;
g_parser_file = g_parser_stack[g_parser_depth].file;
}
if (g_parser_file && 0) {
g_parser_line = g_parser_stack[g_parser_depth].file_line;
}
if (g_parser_file)
diagnostics(4, "Resuming Source File '%s'", g_parser_stack[g_parser_depth].file_name);
else {
diagnostics(5, "Resuming Source string");
show_string(5,g_parser_string,"resuming");
}
if (0) {
diagnostics(WARNING, "After PopSource** line=%d, g_parser_depth=%d, g_parser_include_level=%d",
g_parser_line, g_parser_depth, g_parser_include_level);
for (i = 0; i <= g_parser_depth; i++) {
if (g_parser_stack[i].file)
diagnostics(WARNING, "i=%d file =%s, line=%d", i, g_parser_stack[i].file_name, g_parser_stack[i].file_line);
else {
strncpy_printable(s, g_parser_stack[i].string, 25);
diagnostics(WARNING, "i=%d string =%s, line=%d", i, s, g_parser_stack[i].file_line);
}
}
}
}
void CmdInclude(int code)
/******************************************************************************
purpose: handles \input file, \input{file}, \include{file}
code == 0 for \include
code == 1 for \input
******************************************************************************/
{
int cNext;
char name[100];
int i;
char *basename=NULL;
char *texname=NULL;
cNext = getNonSpace();
if (cNext == '{') { /* \input{gnu} or \include{gnu} */
ungetTexChar(cNext);
basename = getBraceParam();
} else { /* \input gnu */
i = 0;
while (cNext != '\0' && !isspace(cNext)) {
if (i<99) name[i] = (char) cNext;
i++;
cNext = getTexChar();
}
if (i<99)
name[i] = '\0';
else {
name[99] = '\0';
diagnostics(WARNING, "\\input filename '%s' more than 100 chars, skipping",name);
return;
}
basename = strdup(name);
}
if (strstr(basename, "german.sty") != NULL) {
GermanMode = TRUE;
PushEnvironment(GERMAN_MODE);
free(basename);
return;
} else if (strstr(basename, "french.sty") != NULL) {
FrenchMode = TRUE;
PushEnvironment(FRENCH_MODE);
free(basename);
return;
}
if (basename && strstr(basename, ".tex") == NULL && strstr(basename, ".ltx") == NULL) /* append .tex if missing */
texname = strdup_together(basename, ".tex");
if (texname && PushSource(texname, NULL) == 0) /* Try the .tex name first*/
diagnostics(WARNING, "Including file <%s> (.tex appended)", texname);
else if (basename && PushSource(basename, NULL) == 0) /* Try the basename second*/
diagnostics(WARNING, "Including file <%s>", basename);
/* \include{file} always starts a new page */
if (code == 0)
PushSource(NULL, "\\pagebreak ");
if (basename) free(basename);
if (texname) free(texname);
}
#define CR (char) 0x0d
#define LF (char) 0x0a
int getParserDepth(void)
{
return g_parser_depth;
}
char getRawTexChar(void)
/***************************************************************************
purpose: get the next character from the input stream with minimal
filtering (CRLF or CR or LF -> \n) and '\t' -> ' '
it also keeps track of the line number
should only be used by \verb and \verbatim and getTexChar()
****************************************************************************/
{
int thechar;
if (g_parser_file) {
thechar = getc(g_parser_file);
while (thechar == EOF) {
if (!feof(g_parser_file))
diagnostics(ERROR, "Unknown file I/O error reading latex file\n");
else if (g_parser_include_level > 1) {
PopSource(); /* go back to parsing parent */
thechar = getRawTexChar(); /* get next char from parent file */
} else
thechar = '\0';
}
if (thechar == CR) { /* convert CR, CRLF, or LF to \n */
thechar = getc(g_parser_file);
if (thechar != LF && !feof(g_parser_file))
ungetc(thechar, g_parser_file);
thechar = '\n';
} else if (thechar == LF)
thechar = '\n';
else if (thechar == '\t')
thechar = ' ';
g_parser_currentChar = (char) thechar;
} else {
if (g_parser_string && *g_parser_string) {
thechar = *g_parser_string;
/* convert CR, CRLF, or LF to \n */
if (thechar == CR) {
g_parser_string++;
thechar = *g_parser_string;
if (thechar != LF)
g_parser_string--;
thechar = '\n';
} else if (thechar == LF)
thechar = '\n';
else if (thechar == '\t')
thechar = ' ';
g_parser_currentChar = thechar;
g_parser_string++;
}
else if (g_parser_depth > 15)
{
PopSource(); /* go back to parsing parent */
g_parser_currentChar = getRawTexChar(); /* get next char from parent file */
} else
g_parser_currentChar = '\0';
}
if (g_parser_currentChar == '\n' && g_track_line_number_stack[g_track_line_number])
g_parser_line++;
g_parser_penultimateChar = g_parser_lastChar;
g_parser_lastChar = g_parser_currentChar;
if (0) {
if (g_parser_currentChar=='\n')
diagnostics(5,"getRawTexChar = <\\n>");
else if (g_parser_currentChar=='\0')
diagnostics(5,"getRawTexChar = <\\0> depth=%d, files=%d", g_parser_depth, g_parser_include_level);
else
diagnostics(5,"getRawTexChar = <%2c>",g_parser_currentChar);
}
/* if (g_parser_currentChar=='\0') exit(0);*/
return g_parser_currentChar;
}
#undef CR
#undef LF
void ungetTexChar(char c)
/****************************************************************************
purpose: rewind the filepointer in the LaTeX-file by one
****************************************************************************/
{
if (c == '\0')
return;
if (g_parser_file) {
ungetc(c, g_parser_file);
} else {
g_parser_string--;
if (g_parser_string && *g_parser_string) {
*g_parser_string = c;
}
}
if (c == '\n' && g_track_line_number_stack[g_track_line_number])
g_parser_line--;
g_parser_currentChar = g_parser_lastChar;
g_parser_lastChar = g_parser_penultimateChar;
g_parser_penultimateChar = '\0'; /* no longer know what that it was */
g_parser_backslashes = 0;
diagnostics(6, "after ungetTexChar=<%c> backslashes=%d line=%ld", c, g_parser_backslashes, g_parser_line);
}
int skipBOM(int cThis)
{
/* UTF8 Byte Order Mark */
if (cThis == 0xEF) {
cThis = getRawTexChar();
if (cThis == 0xBB) {
cThis = getRawTexChar();
if (cThis == 0xBF) {
CmdFontEncoding(ENCODING_UTF8);
cThis = getRawTexChar();
diagnostics(2, "UTF 8 BOM encountered, now assuming UTF8 input");
}
}
}
/* UTF16 Byte Order Mark */
if (cThis == 0xFE) {
cThis = getRawTexChar();
if (cThis == 0xFF)
diagnostics(2, "UTF 16 is not supported, you might try converting to UTF8");
}
return cThis;
}
char getTexChar()
/***************************************************************************
purpose: get the next character from the input stream
This should be the usual place to access the LaTeX file
It filters the input stream so that % is handled properly
****************************************************************************/
{
int cThis;
cThis = getRawTexChar();
cThis = skipBOM(cThis);
if (cThis == '\\')
g_parser_backslashes++;
else
g_parser_backslashes = 0;
if (0) {
if (cThis=='\n')
diagnostics(6,"getRawTexChar = <\\n> backslashes=%d line=%ld", g_parser_backslashes, g_parser_line);
else if (cThis=='\0')
diagnostics(6,"getRawTexChar = <\\0> backslashes=%d line=%ld", g_parser_backslashes, g_parser_line);
else
diagnostics(6,"getRawTexChar = <%2c> backslashes=%d line=%ld",cThis, g_parser_backslashes, g_parser_line);
}
return cThis;
}
void skipToEOL(void)
/****************************************************************************
purpose: ignores anything from inputfile until the end of line.
uses getRawTexChar() because % are not important
****************************************************************************/
{
char cThis;
while ((cThis = getRawTexChar()) && cThis != '\n') {
}
}
char getNonBlank(void)
/***************************************************************************
Description: get the next non-blank character from the input stream
****************************************************************************/
{
char c;
c = getTexChar();
while (c == ' ' || c == '\n') {
c = getTexChar();
}
return c;
}
char getNonSpace(void)
/***************************************************************************
Description: get the next non-space character from the input stream
****************************************************************************/
{
char c;
while ((c = getTexChar()) && c == ' ') {
}
return c;
}
void skipSpaces(void)
/***************************************************************************
Description: skip to the next non-space character from the input stream
****************************************************************************/
{
char c;
while ((c = getTexChar()) && c == ' ') {
}
ungetTexChar(c);
}
void skipWhiteSpace(void)
/***************************************************************************
Description: skip over spaces and linefeeds
****************************************************************************/
{
char c=getNonBlank();
ungetTexChar(c);
}
int getSameChar(char c)
/***************************************************************************
Description: returns the number of characters that are the same as c
****************************************************************************/
{
char cThis;
int count = -1;
do {
cThis = getTexChar();
count++;
} while (cThis == c);
ungetTexChar(cThis);
return count;
}
char *getDelimitedText(char left, char right, int raw)
/******************************************************************************
purpose: general scanning routine that allocates and returns a string
that is between "left" and "right" that accounts for escaping by '\'
Example for getDelimitedText('{','}',TRUE)
"the \{ is shown {\it by} a\\} blah blah" ----> "the \{ is shown {\it by} a\\"
Note the missing opening brace in the example above
It turns out that for getDelimitedText('[',']',TRUE)
"the \] is shown {]} a\\] blah blah blah" ----> "the \] is shown {]} a\\"
******************************************************************************/
{
char buffer[SCAN_BUFFER_SIZE];
int size = -1;
int lefts_needed = 1;
int brace_level = 0;
int last_char_was_backslash = FALSE;
while (lefts_needed && size < SCAN_BUFFER_SIZE-1) {
size++;
buffer[size] = (raw) ? getRawTexChar() : getTexChar();
if (last_char_was_backslash) { /* ignore \{ etc. */
if (buffer[size] == '\\') { /* two backslashes in a row */
last_char_was_backslash = FALSE; /* next char is not special */
continue;
}
}
else if (buffer[size] == right && brace_level == 0)
lefts_needed--;
else if (buffer[size] == '{')
brace_level++;
else if (buffer[size] == '}')
brace_level--;
last_char_was_backslash = (buffer[size] == '\\') ? TRUE : FALSE;
}
buffer[size] = '\0'; /* overwrite final delimeter */
if (size == SCAN_BUFFER_SIZE-1) {
diagnostics(WARNING, "Could not find closing '%c' in %d chars", right, SCAN_BUFFER_SIZE);
return strdup(" NOT FOUND ");
}
return strdup(buffer);
}
void parseBrace(void)
/****************************************************************************
Description: Skip text to balancing close brace
****************************************************************************/
{
char *s = getDelimitedText('{', '}', FALSE);
free(s);
}
static void parseBracket(void)
/****************************************************************************
Description: Skip text to balancing close bracket
****************************************************************************/
{
char *s = getDelimitedText('[', ']', FALSE);
free(s);
}
void CmdIgnoreParameter(int code)
/****************************************************************************
Description: Ignore the parameters of a command
Example : CmdIgnoreParameter(21) for \command[opt1]{reg1}{reg2}
code is a decimal # of the form "op" where `o' is the number of
optional parameters (0-9) and `p' is the # of required parameters.
The specified number of parameters is ignored. The order of the parameters
in the LaTeX file does not matter.
****************************************************************************/
{
int optParmCount = code / 10;
int regParmCount = code % 10;
char cThis;
diagnostics(4, "CmdIgnoreParameter [%d] {%d}", optParmCount, regParmCount);
while (regParmCount) {
cThis = getNonBlank();
switch (cThis) {
case '{':
regParmCount--;
parseBrace();
break;
case '[':
optParmCount--;
parseBracket();
break;
default:
diagnostics(WARNING, "Ignored command missing {} expected %d - found %d", code % 10,
code % 10 - regParmCount);
ungetTexChar(cThis);
return;
}
}
/* Check for trailing optional parameter e.g., \item[label] */
if (optParmCount > 0) {
cThis = getNonSpace();
if (cThis == '[')
parseBracket();
else {
ungetTexChar(cThis);
return;
}
}
return;
}
char *getSimpleCommand(void)
/**************************************************************************
purpose: returns a simple command e.g., \alpha\beta will return "\beta"
^
\! will return \!
**************************************************************************/
{
char buffer[128];
int size;
buffer[0] = getTexChar();
if (buffer[0] != '\\')
return NULL;
buffer[1] = getTexChar();
for (size = 2; size < 127; size++) {
buffer[size] = getRawTexChar(); /* \t \r '%' all end command */
if (!isalpha((int) buffer[size])) {
ungetTexChar(buffer[size]);
break;
}
}
buffer[size] = '\0';
if (size == 127) {
diagnostics(WARNING, "Misplaced brace.");
diagnostics(ERROR, "Cannot find close brace in 127 characters");
}
diagnostics(5, "getSimpleCommand result <%s>", buffer);
return strdup(buffer);
}
char *getBracketParam(void)
/******************************************************************************
purpose: return bracketed parameter
\item[1] ---> "1" \item[] ---> "" \item the ---> NULL
^ ^ ^
\item [1] ---> "1" \item [] ---> "" \item the ---> NULL
^ ^ ^
******************************************************************************/
{
char c, *text;
c = getNonBlank();
PushTrackLineNumber(FALSE);
if (c == '[') {
text = getDelimitedText('[', ']', FALSE);
diagnostics(6, "getBracketParam [%s]", text);
} else {
ungetTexChar(c);
text = NULL;
diagnostics(6, "getBracketParam []");
}
PopTrackLineNumber();
return text;
}
static char *getBraceParam0(int raw_flag)
/**************************************************************************
purpose: allocates and returns the next parameter in the LaTeX file
Examples: (^ indicates the current file position)
\alpha\beta ---> "\beta" \bar \alpha ---> "\alpha"
^ ^
\bar{text} ---> "text" \bar text ---> "t"
^ ^
_\alpha ---> "\alpha" _{\alpha} ---> "\alpha"
^ ^
_2 ---> "2" _{2} ---> "2"
^ ^
**************************************************************************/
{
char s[2], *text;
s[0] = getNonSpace(); /* skip spaces and one possible newline */
if (s[0] == '\n')
s[0] = getNonSpace();
PushTrackLineNumber(FALSE);
if (s[0] == '\\') {
ungetTexChar(s[0]);
text = getSimpleCommand();
} else if (s[0] == '{')
text = getDelimitedText('{', '}', raw_flag);
else {
s[1] = '\0';
text = strdup(s);
}
PopTrackLineNumber();
diagnostics(6, "Leaving getBraceParam {%s}", text);
return text;
}
char *getBraceParam(void)
{
return getBraceParam0(FALSE);
}
char *getBraceRawParam(void)
{
return getBraceParam0(TRUE);
}
void ignoreBraceParam(void) {
char *p = getBraceParam();
if (NULL != p) free(p);
}
void ignoreBracketParam(void) {
char *p = getBracketParam();
if (NULL != p) free(p);
}
char *getLeftRightParam(void)
/**************************************************************************
purpose: get text between \left ... \right
**************************************************************************/
{
char text[5000], s, *command;
int i = 0;
int lrdepth = 1;
text[0] = '\0';
for (;;) {
s = getTexChar();
if (s == '\\') {
ungetTexChar(s);
command = getSimpleCommand();
if (strcmp(command, "\\right") == 0) {
lrdepth--;
if (lrdepth == 0) {
free(command);
return strdup(text);
}
}
my_strlcat(text + i, command, 5000);
i += (int) strlen(command);
if (i > 4950)
diagnostics(ERROR, "Contents of \\left .. \\right too large.");
if (strcmp(command, "\\left") == 0)
lrdepth++;
free(command);
} else {
text[i] = s;
i++;
text[i] = '\0';
}
}
return NULL;
}
char *getTexUntil(char *target, int raw)
/**************************************************************************
purpose: returns the portion of the file to the beginning of target
returns: NULL if not found
**************************************************************************/
{
enum { BUFFSIZE = 200000 };
char *s;
char buffer[BUFFSIZE];
int last_i = -1;
int i = 0; /* size of string that has been read */
int j = 0; /* number of found characters */
int end_of_file_reached = FALSE;
int len = (int) strlen(target);
PushTrackLineNumber(FALSE);
diagnostics(5, "getTexUntil target = <%s> raw_search = %d ", target, raw);
while (j < len && i < BUFFSIZE) {