summaryrefslogtreecommitdiff
path: root/support/chktex/ChkTeX.h
blob: 01275d38b059c6f7bbbebe44759caf0cf70cdc83 (plain)
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
/*
 *  ChkTeX, header file for main program.
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  Contact the author at:
 *              Jens Berger
 *              Spektrumvn. 4
 *              N-0666 Oslo
 *              Norway
 *              E-mail: <jensthi@ifi.uio.no>
 *
 *
 */

#ifndef CHKTEX_H
#define CHKTEX_H 1
#if defined(HAVE_CONFIG_H) || defined(CONFIG_H_NAME)
#  ifndef CONFIG_H_NAME
#    define  CONFIG_H_NAME   "config.h"
#  endif
#  ifndef HAVE_CONFIG_H
#    define HAVE_CONFIG_H 1
#  endif
#  include CONFIG_H_NAME
#endif

/* For TeX Live */
#ifdef KPATHSEA
#include <kpathsea/config.h>
#endif

#include <ctype.h>
#include <math.h>
#include <stdio.h>

#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#else
#error Sorry, ChkTeX needs an _ANSI_ compiler w/stdarg.h installed to compile.
#endif

#ifndef HAVE_VPRINTF
#error Sorry, ChkTeX needs an _ANSI_ compiler w/vprintf() to compile.
#endif

#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif

#ifdef HAVE_LIMITS_H
#  include <limits.h>
#endif

#ifdef HAVE_STRINGS_H
#  include <strings.h>
#endif

#ifdef HAVE_INTTYPES_H
#  include <inttypes.h>
#endif

#ifdef HAVE_STDINT_H
#  include <stdint.h>
#endif

#include "types.h"

#ifndef LONG_MAX
#  define LONG_MAX (0x7fffffff)
#endif

#ifndef CHAR_BIT
#  define CHAR_BIT (8)
#endif

#include "FindErrs.h"

#if defined(HAVE_FILENO) && defined(HAVE_ISATTY)
#  include <stdio.h>
#endif

/*
 * Define aliases for preprocessor statements
 */

#if defined(__unix__) || defined(__unix) || defined(unix)
#undef __unix__
#define __unix__ 1
#elif defined(__APPLE__) && defined(__MACH__)
/* OS X should be considered a UNIX */
#undef __unix__
#define __unix__ 1
#elif defined(MSDOS) || defined(__MSDOS__) || defined(__msdos__)
#undef __MSDOS__
#define __MSDOS__ 1
#endif

/*  -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=-  */

/*
 * NOTE: All macros below should return FALSE (0) if the input
 * character is 0.
 *
 * The next macro should return TRUE if LaTeX (and you?) considers
 * the character `c' as a space, which should be detected when
 * we're checking whether commands are terminated by spaces.
 *
 * Note: If you wish to change this macro, you'll have to update
 * the main loop in FindErrs.c as well.
 */

#define LATEX_SPACE(c)  (c > 0 && c <= ' ')

/*  -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=-  */






#define betw(a,b,c)             ((a < b) && (b < c))
#define within(a,b,c)           ((a <= b) && (b <= c))

#ifndef min
#  define min(a,b) ((a)<=(b)?(a):(b))
#endif

#ifndef max
#  define max(a,b) ((a)>(b)?(a):(b))
#endif

#ifndef abs
#  define abs(a) (a>=0?a:(-a))
#endif

#define LOOP(name, body)  {{body} _end__##name:;}
#define LAST(name)        goto _end__##name

#define NUMBRACKETS          6

#define NEWBUF(name,len) \
static char _ ## name [len  + (WALLBYTES<<1)] = {0};  \
char *name = &_ ## name[WALLBYTES]

#define INTFAULTMSG "INTERNAL FAULT OCCURED! PLEASE SUBMIT A BUG REPORT!\n"

#define INTERNFAULT   INTFAULTMSG

#define BITDEF1(a)    BIT_ ## a
#define BITDEF2(a)    FLG_ ## a = (1<<BIT_ ## a)

/*
 * Debugging info
 */

#define DEBUGBITS(a) enum a { \
    BIT(DbgMsgs),        /* Dump messages */ \
    BIT(DbgListInfo),    /* Dump list info */ \
    BIT(DbgListCont),    /* Dump list contents */ \
    BIT(DbgOtherInfo),   /* Dump misc. other info */ \
    BIT(DbgRunTime)      /* Dump runtime info */ \
};

#undef BIT
#define BIT BITDEF1
DEBUGBITS(Debug_BIT)
#undef BIT
#define BIT BITDEF2
    DEBUGBITS(Debug_FLG)
#ifdef STRIP_DEBUG
#  define DEBUG(a)
#else
#  define DEBUG(a)    if(DebugLevel & FLG_DbgRunTime) ErrPrintf a
#endif
#define PRGMSGS \
 MSG(pmMinFault, etWarn, TRUE, 0, INTERNFAULT)\
 MSG(pmNoFileMatch, etWarn, TRUE, 0,\
     "No files matched the pattern `%s'.")\
 MSG(pmNoTeXOpen,   etWarn,  TRUE, 0,\
     "Unable to open the TeX file `%s'.")\
 MSG(pmRename,      etMsg,  TRUE, 0,\
     "Renaming `%s' as `%s'.")\
 MSG(pmRenameErr,   etErr,  TRUE, 0,\
     "Could not rename `%s' to `%s'.")\
 MSG(pmOutOpen,     etErr,  TRUE, 0,\
     "Unable to open output file.")\
 MSG(pmOutTwice,    etErr,  TRUE, 0,\
     "You can specify output file only once.")\
 MSG(pmStrDupErr,   etErr,  TRUE, 0,\
     "Unable to duplicate strings - no memory?")\
 MSG(pmWordListErr, etErr,  TRUE, 0,\
     "Unable to create wordlist - no memory?")\
 MSG(pmNoStackMem,  etErr,  TRUE, 0,\
     "Unable to create stack - no memory?\n")\
 MSG(pmWarnNumErr,  etErr,  TRUE, 0,\
     "Illegal warning number used.")\
 MSG(pmVerbLevErr,  etErr,  TRUE, 0,\
     "Illegal verbosity level.")\
 MSG(pmNotPSDigit,  etWarn, TRUE, 0,\
     "`%c' is not a %s digit - ignored!")\
 MSG(pmEscCode,     etWarn, TRUE, 0,\
     "Unknown escape code `%c%c' - ignored!")\
 MSG(pmKeyWord,     etErr,  TRUE, 0,\
     "Unsupported control word (`%s') encountered in file `%s'.")\
 MSG(pmFaultFmt,    etErr,  TRUE, 0,\
     "\"%s\", line %d: Faulty format - unexpected %s found.")\
 MSG(pmRsrcOpen,    etWarn, TRUE, 0,\
     "Could not open `%s', may cause unwanted behaviour.")\
 MSG(pmSlowAbbr,    etMsg,  FALSE, 0,\
     "The abbreviation `%s' requires slow abbreviation searching.")\
 MSG(pmEmptyToken,  etWarn,  TRUE,  0,\
     "Empty token isolated in `%s' - probably faulty format")\
 MSG(pmAssert,      etErr,  TRUE,  0,\
     "Assertion failed. Please report bug.")\
 MSG(pmNoRsrc,      etWarn, TRUE, 0,\
     "Could not find global resource file.")\
 MSG(pmQuoteStyle,    etWarn, TRUE, 0,\
     "Illegal quotestyle `%s'.")\
 MSG(pmWrongWipeTemp, etWarn, TRUE, 0,\
     "Wrong template for wiping arguments, \"%s\"")\
 MSG(pmSpecifyTerm, etErr, TRUE, 0,\
     "Specify a terminal type with `setenv TERM <yourtype>'.")\
 MSG(pmNoTermData, etErr, TRUE, 0,\
     "Could not access the termcap data base.")\
 MSG(pmNoTermDefd, etErr, TRUE, 0,\
     "Terminal type `%s' is not defined.")\
 MSG(pmNoRegExp,   etWarn, TRUE, 0,\
     "Regular expressions not compiled in. Please recompile.")\
 MSG(pmNoCommon,   etErr, TRUE, 0,\
     "`%s' can't be a member of both `%s' and `%s'.") \
 MSG(pmNoOpenDir,  etWarn, TRUE, 0, \
     "Could not open the directory `%s'.") \
 MSG(pmNoDebugFlag, etWarn, TRUE, 0, \
     "This version of doesn't support the -d flag. Please recompile.")\
 MSG(pmNoRegexMem,  etErr,  TRUE, 0,\
     "Unable to allocate regular expressions - no memory?\n")\
 MSG(pmRegexCompileFailed,  etWarn,  TRUE, 0,\
     "Compilation of regular expression %s failed with error %s.\n")\
 MSG(pmRegexMatchingError,  etErr,  TRUE, 0,\
     "PCRE matching error %d.\n") \
 MSG(pmSuppTooHigh,  etWarn,  TRUE, 0,\
     "Warning %d is numbered too high (max %d) and won't be suppressed.\n") \
MSG(pmLongLines,  etWarn,  TRUE, 0,\
     "ChkTeX does not handle lines over %d bytes correctly.  Some errors and line numbers may be wrong in this file.") \
MSG(pmTabExpands,  etWarn,  TRUE, 0,\
     "ChkTeX could not fully expand tabs because the resulting line would be more than %d bytes.  Some errors and line numbers may be wrong in this file.")

#undef MSG
#define MSG(num, type, inuse, ctxt, text) num,
enum PrgErrNum
{
    PRGMSGS pmMaxFault
};
#undef MSG

extern struct ErrMsg PrgMsgs[pmMaxFault + 1];

struct ErrInfo
{
    char *Data;
    char *LineBuf;
    char *File;
    unsigned long Line, Column, ErrLen;
    enum
    {
        efNone   = 0x00,
        efNoItal = 0x01,
        efItal   = 0x02,
        efNoMath = 0x04,
        efMath   = 0x08
    } Flags;
};

extern char *ReadBuffer;
extern char *CmdBuffer;
extern char *TmpBuffer;

extern const char BrOrder[NUMBRACKETS + 1];

extern unsigned long Brackets[NUMBRACKETS];


extern FILE *OutputFile, *InputFile;

/* We include semicolons on all but the last so that we can include a
 * semicolon where it's called.  This helps with indentation at the
 * calling location.  It also means that DEF must never have a
 * semicolon.  The reason for the care is that isolated semicolons
 * have been known to break some compilers. */
/* See also RESOURCE_INFO where the same trick doesn't work, and we
 * cannot include semicolons at the calling site, regardless of
 * indentation concerns. */
#define OPTION_DEFAULTS        \
  DEF(int, GlobalRC,    TRUE); \
  DEF(int, WipeVerb,    TRUE); \
  DEF(int, BackupOut,   TRUE); \
  DEF(int, Quiet,       FALSE); \
  DEF(int, LicenseOnly, FALSE); \
  DEF(int, UsingStdIn,  FALSE); \
  DEF(int, InputFiles,  TRUE); \
  DEF(int, HeadErrOut,  TRUE); \
  DEF(const char *, OutputName, ""); \
  DEF(const char *, PseudoInName, NULL); \
  DEF(char *, OutputFormat, VerbNormal); \
  DEF(char *, PipeOutputFormat, NULL); \
  DEF(const char *, Delimit, ":"); \
  DEF(long,  DebugLevel, 0); \
  DEF(int,  NoLineSupp, FALSE)

#define STATE_VARS \
    DEF(enum ItState, ItState, itOff); /* Are we doing italics? */      \
  DEF(int, AtLetter, FALSE); /* Whether `@' is a letter or not. */      \
  DEF(int, InHeader, TRUE);  /* Whether we're in the header */          \
  DEF(int, VerbMode, FALSE); /* Whether we're in complete ignore-mode */ \
  DEF(long, MathMode, 0);    /* Whether we're in math mode or not */    \
  DEF(const char *, VerbStr, "");     /* String we'll terminate verbmode upon */ \
  DEF(unsigned long, ErrPrint, 0);    /* # errors printed */            \
  DEF(unsigned long, WarnPrint, 0);   /* # warnings printed */          \
  DEF(unsigned long, UserSupp, 0);    /* # user suppressed warnings */  \
  DEF(unsigned long, LineSupp, 0);    /* # warnings suppressed on a single line */ \
  DEF(uint64_t, FileSuppressions, 0);     /* # warnings suppressed in a file */ \
  DEF(uint64_t, UserFileSuppressions, 0) /* # User warnings suppressed in a file */

#define DEF(type, name, value) extern type name
OPTION_DEFAULTS;
STATE_VARS;
#undef DEF
extern struct Stack CharStack, InputStack, EnvStack;

enum Quote
{
    quLogic,
    quTrad
};

extern enum Quote Quote;
extern int StdInTTY, StdOutTTY, UsingStdIn;

int main(int argc, char **argv);
void PrintPrgErr(enum PrgErrNum, ...);
void ErrPrintf(const char *fmt, ...);

extern char *PrgName;

#endif /* CHKTEX_H */