summaryrefslogtreecommitdiff
path: root/Build/source/texk/chktex/chktex-src/OpSys.c
blob: 7d055b7228499422b3859ac41cd80ff8d6694aa9 (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
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
/*
 *  ChkTeX, operating system specific code for ChkTeX.
 *  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>
 *
 *
 */


/*
 * Some functions which have to be made different from OS to OS,
 * unfortunately...:\
 *
 */

#include "ChkTeX.h"
#include "OpSys.h"
#include "Utility.h"

#ifdef KPATHSEA
#include <kpathsea/variable.h>
#endif

#ifdef HAVE_SYS_STAT_H
#  include <sys/stat.h>
#endif
#ifdef HAVE_STAT_H
#  include <stat.h>
#endif

#if HAVE_DIRENT_H
#  include <dirent.h>
#  define NAMLEN(dirent) strlen((dirent)->d_name)
#else
#  define dirent direct
#  define NAMLEN(dirent) (dirent)->d_namlen
#  if HAVE_SYS_NDIR_H
#    include <sys/ndir.h>
#  endif
#  if HAVE_SYS_DIR_H
#    include <sys/dir.h>
#  endif
#  if HAVE_NDIR_H
#    include <ndir.h>
#  endif
#endif

#if defined(HAVE_OPENDIR) && defined(HAVE_CLOSEDIR) && \
    defined(HAVE_READDIR) && defined(HAVE_STAT) && \
    defined(S_IFDIR) && defined(SLASH)
#  define USE_RECURSE 1
#else
#  define USE_RECURSE 0
#endif

#if defined(HAVE_LIBTERMCAP) || defined(HAVE_LIBTERMLIB)
#  define USE_TERMCAP 1
#endif


#ifdef USE_TERMCAP
#  ifdef HAVE_TERMCAP_H
#    include <termcap.h>
#  elif HAVE_TERMLIB_H
#    include <termlib.h>
#  else
int tgetent(char *BUFFER, char *TERMTYPE);
char *tgetstr(char *NAME, char **AREA);
#  endif
static char term_buffer[2048];
#endif

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

/*
 * This is the name of the global resource file.
 */

#ifndef SYSCONFDIR
#  if defined(__unix__)
#    define SYSCONFDIR "/usr/local/lib/"
#  elif defined(__MSDOS__)
#    define SYSCONFDIR "\\emtex\\data\\"
#  else
#    define SYSCONFDIR
#  endif
#endif
#define RCBASENAME              "chktexrc"

#ifdef __MSDOS__
#  define LOCALRCFILE             RCBASENAME
#elif defined(WIN32)
#  define LOCALRCFILE             RCBASENAME
#else
#  define LOCALRCFILE             "." RCBASENAME
#endif

char ConfigFile[BUFSIZ] = LOCALRCFILE;
const char *ReverseOn;
const char *ReverseOff;


static int HasFile(char *Dir, const char *Filename, const char *App);

#if USE_RECURSE
static int SearchFile(char *Dir, const char *Filename, const char *App);
#endif /* USE_RECURSE */

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


/*
 * Modify this one to suit your needs. In any case, it should fill
 * the ConfigFile (sized BUFLEN) buffer above with full name & path
 * for the configuration file. The macro RCFILE will give you the
 * filename part of the file, if you need that.
 *
 * Note: This routine will be called several times. Your mission will
 * be to look in each location, and see whether a .chktexrc file exist
 * there.
 *
 * If you choose to do nothing, only the current directory will be
 * searched.
 *
 */


enum LookIn
{
    liMin,
    liSysDir,
    liUsrDir,
    liEnvir,
    liCurDir,
    liNFound,
    liMax
};


int SetupVars(void)
{
    char *Env;
#ifdef __MSDOS__

    char *Ptr;
#endif

    static enum LookIn i = liMin;
    static int FoundFile;

    while (++i < liMax)
    {
        switch (i)
        {
        case liCurDir:         /* Current directory */
            strcpy(ConfigFile, LOCALRCFILE);
            break;
        case liEnvir:          /* Environment defined */
#ifdef __MSDOS__

            if ((Env = getenv("CHKTEXRC")) || (Env = getenv("CHKTEX_HOME")))
#elif defined(TEX_LIVE)
            if ((Env = kpse_var_value("CHKTEXRC")))
#else

            if ((Env = getenv("CHKTEXRC")))
#endif

            {
                strcpy(ConfigFile, Env);
                tackon(ConfigFile, LOCALRCFILE);
#ifdef TEX_LIVE
                free(Env);
#endif
            }
            else
#ifdef __MSDOS__
            if ((Env = getenv("EMTEXDIR")))
            {
                strcpy(ConfigFile, Env);
                tackon(ConfigFile, "data");
                tackon(ConfigFile, LOCALRCFILE);
            }
            else
#endif

                *ConfigFile = 0;
            break;
        case liUsrDir:         /* User dir for resource files */
#if defined(__unix__)

            if ((Env = getenv("HOME")) || (Env = getenv("LOGDIR")))
            {
                strcpy(ConfigFile, Env);
                tackon(ConfigFile, LOCALRCFILE);
            }
            else
#elif defined(__MSDOS__)

            strcpy(ConfigFile, PrgName);
            if ((Ptr = strrchr(ConfigFile, '\\')) ||
                (Ptr = strchr(ConfigFile, ':')))
                strcpy(++Ptr, RCBASENAME);
            else
#endif
                *ConfigFile = 0;

            break;
        case liSysDir:         /* System dir for resource files */
#ifdef TEX_LIVE
            if ((Env = kpse_var_value("CHKTEX_CONFIG")))
            {
                strcpy(ConfigFile, Env);
                free(Env);
            }
            else if ((Env = kpse_var_value("TEXMFMAIN")))
            {
                strcpy(ConfigFile, Env);
                tackon(ConfigFile, "chktex");
                tackon(ConfigFile, RCBASENAME);
                free(Env);
            }
            else
                *ConfigFile = 0;
#else /* TEX_LIVE */
#if defined(__unix__) || defined(__MSDOS__)
            strcpy(ConfigFile, SYSCONFDIR);
            tackon(ConfigFile, RCBASENAME);
#else
            *ConfigFile = 0;
#endif
#endif /* TEX_LIVE */

            break;
        case liNFound:
        case liMin:
        case liMax:
            *ConfigFile = 0;
            if (!FoundFile)
                PrintPrgErr(pmNoRsrc);
        }

        if (*ConfigFile && fexists(ConfigFile))
            break;
    }
    FoundFile |= *ConfigFile;

    return (*ConfigFile);
}


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

/*
 * This function should initialize the global variables ReverseOn
 * and ReverseOff to magic cookies, which when printed, makes the
 * text in between stand out.
 */

void SetupTerm(void)
{
#ifdef USE_TERMCAP
    char *termtype = getenv("TERM");
    int success;
    char *buffer;
    static char str_so[3] = "so", str_se[3] = "se";

    if (termtype)
    {

        success = tgetent(term_buffer, termtype);
        if (success < 0)
            PrintPrgErr(pmNoTermData);
        if (success == 0)
            PrintPrgErr(pmNoTermDefd);

        buffer = (char *) malloc(sizeof(term_buffer));
        ReverseOn = tgetstr(str_so, &buffer);
        ReverseOff = tgetstr(str_se, &buffer);

        if (ReverseOn && ReverseOff)
            return;
    }
#endif

    ReverseOn = PRE_ERROR_STR;
    ReverseOff = POST_ERROR_STR;
}

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

/*
 * Concatenates the `File' string to the `Dir' string, leaving the result
 * in the `Dir' buffer. Takes care of inserting `directory' characters;
 * if we've got the strings "/usr/foo" and "bar", we'll get
 * "/usr/foo/bar".
 *
 * Behaviour somewhat controlled by the macros SLASH and DIRCHARS in the
 * OpSys.h file.
 *
 */

void tackon(char *Dir, const char *File)
{
    int EndC;
    unsigned long SLen;

    if (Dir && (SLen = strlen(Dir)))
    {
        EndC = Dir[SLen - 1];
        if (!(strchr(DIRCHARS, EndC)))
        {
            Dir[SLen++] = SLASH;
            Dir[SLen] = 0L;
        }
    }

    strcat(Dir, File);
}

/*
 * This function should add the appendix App to the filename Name.
 * If the resulting filename gets too long due to this, it may
 * overwrite the old appendix.
 *
 * Name may be assumed to be a legal filename under your OS.
 *
 * The appendix should contain a leading dot.
 */

void AddAppendix(char *Name, const char *App)
{
#ifdef __MSDOS__
    char *p;

    if ((p = strrchr(Name, '.')))
        strcpy(p, App);
    else
        strcat(Name, App);
#else
    /*
     * NOTE! This may fail if your system has a claustrophobic file
     * name length limit.
     */
    strcat(Name, App);
#endif

}

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


/*
 * Locates a file, given a wordlist containing paths. If a
 * dir ends in a double SLASH, we'll search it recursively.
 *
 * We assume that
 *   a) a deeper level in the dir. tree. has a longer path than
 *      one above.
 *   b) adding a level doesn't change any of the previous levels.
 *
 * If this function returns TRUE, Dest is guaranteed to contain
 * path & name of the found file.
 *
 * FALSE indicates that the file was not found; Dest is then
 * unspecified.
 */


int LocateFile(const char *Filename,    /* File to search for */
               char *Dest,      /* Where to put final file */
               const char *App, /* Extra optional appendix */
               struct WordList *wl)     /* List of paths, entries
                                         * ending in // will be recursed
                                         */
{
    unsigned long i;
#if USE_RECURSE

    unsigned long Len;
#endif

    FORWL(i, *wl)
    {
        strcpy(Dest, wl->Stack.Data[i]);

#if USE_RECURSE

        Len = strlen(Dest);

        if (Len && (Dest[Len - 1] == SLASH) && (Dest[Len - 2] == SLASH))
        {
            Dest[Len - 1] = Dest[Len - 2] = 0;
            if (SearchFile(Dest, Filename, App))
                return (TRUE);
        }
        else
#endif /* USE_RECURSE */

        {
            if (HasFile(Dest, Filename, App))
                return (TRUE);
        }
    }
    return (FALSE);
}

static int HasFile(char *Dir, const char *Filename, const char *App)
{
    int DirLen = strlen(Dir);

    tackon(Dir, Filename);
    if (fexists(Dir))
        return (TRUE);

    if (App)
    {
        AddAppendix(Dir, App);
        if (fexists(Dir))
            return (TRUE);
    }

    Dir[DirLen] = 0;
    return (FALSE);

}


#if USE_RECURSE
static int SearchFile(char *Dir, const char *Filename, const char *App)
{
    struct stat *statbuf;
    struct dirent *de;
    DIR *dh;

    int DirLen = strlen(Dir);
    int Found = FALSE;

    DEBUG(("Searching %s for %s\n", Dir, Filename));

    if (HasFile(Dir, Filename, App))
        return (TRUE);
    else
    {
        if ((statbuf = malloc(sizeof(struct stat))))
        {
            if ((dh = opendir(Dir)))
            {
                while (!Found && (de = readdir(dh)))
                {
                    Dir[DirLen] = 0;
                    if (strcmp(de->d_name, ".") && strcmp(de->d_name, ".."))
                    {
                        tackon(Dir, de->d_name);

                        if (!stat(Dir, statbuf))
                        {
                            if ((statbuf->st_mode & S_IFMT) == S_IFDIR)
                                Found = SearchFile(Dir, Filename, App);
                        }
                    }
                }
                closedir(dh);
            }
            else
                PrintPrgErr(pmNoOpenDir, Dir);
            free(statbuf);
        }
    }
    return (Found);
}
#endif /* USE_RECURSE */