Debian Codegraph @

texk/chktex/chktex-src/OpSys.c

raw
1 /*
2 * ChkTeX, operating system specific code for ChkTeX.
3 * Copyright (C) 1995-96 Jens T. Berger Thielemann
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 * Contact the author at:
20 * Jens Berger
21 * Spektrumvn. 4
22 * N-0666 Oslo
23 * Norway
24 * E-mail: <[email protected]>
25 *
26 *
27 */
28
29
30 /*
31 * Some functions which have to be made different from OS to OS,
32 * unfortunately...:\
33 *
34 */
35
36 #include "ChkTeX.h"
37 #include "OpSys.h"
38 #include "Utility.h"
39
40 #ifdef KPATHSEA
41 #include <kpathsea/variable.h>
42 #endif
43
44 #ifdef HAVE_SYS_STAT_H
45 # include <sys/stat.h>
46 #endif
47 #ifdef HAVE_STAT_H
48 # include <stat.h>
49 #endif
50
51 #if HAVE_DIRENT_H
52 # include <dirent.h>
53 # define NAMLEN(dirent) strlen((dirent)->d_name)
54 #else
55 # define dirent direct
56 # define NAMLEN(dirent) (dirent)->d_namlen
57 # if HAVE_SYS_NDIR_H
58 # include <sys/ndir.h>
59 # endif
60 # if HAVE_SYS_DIR_H
61 # include <sys/dir.h>
62 # endif
63 # if HAVE_NDIR_H
64 # include <ndir.h>
65 # endif
66 #endif
67
68 #if defined(HAVE_OPENDIR) && defined(HAVE_CLOSEDIR) && \
69 defined(HAVE_READDIR) && defined(HAVE_STAT) && \
70 defined(S_IFDIR) && defined(SLASH)
71 # define USE_RECURSE 1
72 #else
73 # define USE_RECURSE 0
74 #endif
75
76 #if defined(HAVE_LIBTERMCAP) || defined(HAVE_LIBTERMLIB)
77 # define USE_TERMCAP 1
78 #endif
79
80
81 #ifdef USE_TERMCAP
82 # ifdef HAVE_TERMCAP_H
83 # include <termcap.h>
84 # elif HAVE_TERMLIB_H
85 # include <termlib.h>
86 # else
87 int tgetent(char *BUFFER, char *TERMTYPE);
88 char *tgetstr(char *NAME, char **AREA);
89 # endif
90 static char term_buffer[2048];
91 #endif
92
93 /* -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- */
94
95 /*
96 * This is the name of the global resource file.
97 */
98
99 #ifndef SYSCONFDIR
100 # if defined(__unix__)
101 # define SYSCONFDIR "/usr/local/lib/"
102 # elif defined(__MSDOS__)
103 # define SYSCONFDIR "\\emtex\\data\\"
104 # else
105 # define SYSCONFDIR
106 # endif
107 #endif
108 #define RCBASENAME "chktexrc"
109
110 #ifdef __MSDOS__
111 # define LOCALRCFILE RCBASENAME
112 #elif defined(WIN32)
113 # define LOCALRCFILE RCBASENAME
114 #else
115 # define LOCALRCFILE "." RCBASENAME
116 #endif
117
118 char ConfigFile[BUFFER_SIZE] = LOCALRCFILE;
119 struct WordList ConfigFiles;
120
121 const char *ReverseOn;
122 const char *ReverseOff;
123
124
125 static int HasFile(char *Dir, const char *Filename, const char *App);
126
127 #if USE_RECURSE
128 static int SearchFile(char *Dir, const char *Filename, const char *App);
129 #endif /* USE_RECURSE */
130
131 /* -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- */
132
133
134 /*
135 * Modify this one to suit your needs. In any case, it should fill
136 * the ConfigFile (sized BUFLEN) buffer above with full name & path
137 * for the configuration file. The macro RCFILE will give you the
138 * filename part of the file, if you need that.
139 *
140 * Note: This routine will be called several times. Your mission will
141 * be to look in each location, and see whether a .chktexrc file exist
142 * there.
143 *
144 * If you choose to do nothing, only the current directory will be
145 * searched.
146 *
147 */
148
149
150 enum LookIn
151 {
152 liMin,
153 liSysDir,
154 liUsrDir,
155 liXdgDir,
156 liEnvir,
157 liCurDir,
158 liNFound,
159 liMax
160 };
161
162
163 int SetupVars(void)
164 {
165 char *Env;
166 #ifdef __MSDOS__
167
168 char *Ptr;
169 #endif
170
171 static enum LookIn i = liMin;
172 static int FoundFile;
173
174 while (++i < liMax)
175 {
176 switch (i)
177 {
178 case liCurDir: /* Current directory */
179 strcpy(ConfigFile, LOCALRCFILE);
180 break;
181 case liEnvir: /* Environment defined */
182 #ifdef __MSDOS__
183 if ((Env = getenv("CHKTEXRC")) || (Env = getenv("CHKTEX_HOME")))
184 #elif defined(TEX_LIVE)
185 if ((Env = kpse_var_value("CHKTEXRC")))
186 #else
187 if ((Env = getenv("CHKTEXRC")))
188 #endif
189
190 {
191 strcpy(ConfigFile, Env);
192 tackon(ConfigFile, LOCALRCFILE);
193 #ifdef TEX_LIVE
194 free(Env);
195 #endif
196 }
197 else
198 #ifdef __MSDOS__
199 if ((Env = getenv("EMTEXDIR")))
200 {
201 strcpy(ConfigFile, Env);
202 tackon(ConfigFile, "data");
203 tackon(ConfigFile, LOCALRCFILE);
204 }
205 else
206 #endif
207
208 *ConfigFile = 0;
209 break;
210
211 case liXdgDir: /* Cross-desktop group dir for resource files */
212
213 /* XDG is really unix specific, but it shouldn't hurt to
214 * support it on Windows, should someone set the variables. */
215 if ((Env = getenv("XDG_CONFIG_HOME")) && *Env)
216 {
217 strcpy(ConfigFile, Env);
218 tackon(ConfigFile, RCBASENAME);
219 }
220 else if ((Env = getenv("HOME")) && *Env)
221 {
222 strcpy(ConfigFile, Env);
223 tackon(ConfigFile, ".config");
224 tackon(ConfigFile, RCBASENAME);
225 }
226 else
227 *ConfigFile = 0;
228
229 break;
230
231 case liUsrDir: /* User dir for resource files */
232 #if defined(__unix__)
233
234 if ((Env = getenv("HOME")) || (Env = getenv("LOGDIR")))
235 {
236 strcpy(ConfigFile, Env);
237 tackon(ConfigFile, LOCALRCFILE);
238 }
239 else
240 #elif defined(__MSDOS__)
241
242 strcpy(ConfigFile, PrgName);
243 if ((Ptr = strrchr(ConfigFile, '\\')) ||
244 (Ptr = strchr(ConfigFile, ':')))
245 strcpy(++Ptr, RCBASENAME);
246 else
247 #endif
248 *ConfigFile = 0;
249
250 break;
251 case liSysDir: /* System dir for resource files */
252 #ifdef TEX_LIVE
253 if ((Env = kpse_var_value("CHKTEX_CONFIG")))
254 {
255 strcpy(ConfigFile, Env);
256 free(Env);
257 }
258 else if ((Env = kpse_var_value("TEXMFMAIN")))
259 {
260 strcpy(ConfigFile, Env);
261 tackon(ConfigFile, "chktex");
262 tackon(ConfigFile, RCBASENAME);
263 free(Env);
264 }
265 else
266 *ConfigFile = 0;
267 #else /* TEX_LIVE */
268 #if defined(__unix__) || defined(__MSDOS__)
269 strcpy(ConfigFile, SYSCONFDIR);
270 tackon(ConfigFile, RCBASENAME);
271 #else
272 *ConfigFile = 0;
273 #endif
274 #endif /* TEX_LIVE */
275
276 break;
277 case liNFound:
278 case liMin:
279 case liMax:
280 *ConfigFile = 0;
281 if (!FoundFile)
282 PrintPrgErr(pmNoRsrc);
283 }
284
285 if (*ConfigFile && fexists(ConfigFile))
286 break;
287 }
288 FoundFile |= *ConfigFile;
289
290 return (*ConfigFile);
291 }
292
293
294 /* -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- */
295
296 /*
297 * This function should initialize the global variables ReverseOn
298 * and ReverseOff to magic cookies, which when printed, makes the
299 * text in between stand out.
300 */
301
302 void SetupTerm(void)
303 {
304 #ifdef USE_TERMCAP
305 char *termtype = getenv("TERM");
306 int success;
307 char *buffer;
308 static char str_so[3] = "so", str_se[3] = "se";
309
310 if (termtype)
311 {
312
313 success = tgetent(term_buffer, termtype);
314 if (success < 0)
315 PrintPrgErr(pmNoTermData);
316 if (success == 0)
317 PrintPrgErr(pmNoTermDefd);
318
319 buffer = (char *) malloc(sizeof(term_buffer));
320 ReverseOn = tgetstr(str_so, &buffer);
321 ReverseOff = tgetstr(str_se, &buffer);
322
323 if (ReverseOn && ReverseOff)
324 return;
325 }
326 #endif
327
328 ReverseOn = PRE_ERROR_STR;
329 ReverseOff = POST_ERROR_STR;
330 }
331
332 /* -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- */
333
334 /*
335 * Concatenates the `File' string to the `Dir' string, leaving the result
336 * in the `Dir' buffer. Takes care of inserting `directory' characters;
337 * if we've got the strings "/usr/foo" and "bar", we'll get
338 * "/usr/foo/bar".
339 *
340 * Behaviour somewhat controlled by the macros SLASH and DIRCHARS in the
341 * OpSys.h file.
342 *
343 */
344
345 void tackon(char *Dir, const char *File)
346 {
347 int EndC;
348 unsigned long SLen;
349
350 if (Dir && (SLen = strlen(Dir)))
351 {
352 EndC = Dir[SLen - 1];
353 if (!(strchr(DIRCHARS, EndC)))
354 {
355 Dir[SLen++] = SLASH;
356 Dir[SLen] = 0L;
357 }
358 }
359
360 strcat(Dir, File);
361 }
362
363 /*
364 * This function should add the appendix App to the filename Name.
365 * If the resulting filename gets too long due to this, it may
366 * overwrite the old appendix.
367 *
368 * Name may be assumed to be a legal filename under your OS.
369 *
370 * The appendix should contain a leading dot.
371 */
372
373 void AddAppendix(char *Name, const char *App)
374 {
375 #ifdef __MSDOS__
376 char *p;
377
378 if ((p = strrchr(Name, '.')))
379 strcpy(p, App);
380 else
381 strcat(Name, App);
382 #else
383 /*
384 * NOTE! This may fail if your system has a claustrophobic file
385 * name length limit.
386 */
387 strcat(Name, App);
388 #endif
389
390 }
391
392 /* -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- -=><=- */
393
394
395 /*
396 * Locates a file, given a wordlist containing paths. If a
397 * dir ends in a double SLASH, we'll search it recursively.
398 *
399 * We assume that
400 * a) a deeper level in the dir. tree. has a longer path than
401 * one above.
402 * b) adding a level doesn't change any of the previous levels.
403 *
404 * If this function returns TRUE, Dest is guaranteed to contain
405 * path & name of the found file.
406 *
407 * FALSE indicates that the file was not found; Dest is then
408 * unspecified.
409 */
410
411
412 int LocateFile(const char *Filename, /* File to search for */
413 char *Dest, /* Where to put final file */
414 const char *App, /* Extra optional appendix */
415 struct WordList *wl) /* List of paths, entries
416 * ending in // will be recursed
417 */
418 {
419 unsigned long i;
420 #if USE_RECURSE
421
422 unsigned long Len;
423 #endif
424
425 FORWL(i, *wl)
426 {
427 strcpy(Dest, wl->Stack.Data[i]);
428
429 #if USE_RECURSE
430
431 Len = strlen(Dest);
432
433 if (Len && (Dest[Len - 1] == SLASH) && (Dest[Len - 2] == SLASH))
434 {
435 Dest[Len - 1] = Dest[Len - 2] = 0;
436 if (SearchFile(Dest, Filename, App))
437 return (TRUE);
438 }
439 else
440 #endif /* USE_RECURSE */
441
442 {
443 if (HasFile(Dest, Filename, App))
444 return (TRUE);
445 }
446 }
447 return (FALSE);
448 }
449
450 static int HasFile(char *Dir, const char *Filename, const char *App)
451 {
452 int DirLen = strlen(Dir);
453
454 tackon(Dir, Filename);
455 if (fexists(Dir))
456 return (TRUE);
457
458 if (App)
459 {
460 AddAppendix(Dir, App);
461 if (fexists(Dir))
462 return (TRUE);
463 }
464
465 Dir[DirLen] = 0;
466 return (FALSE);
467
468 }
469
470
471 /*
472 * If Filename is contains a directory component, then add a fully qualified
473 * directory to the TeXInputs WordList.
474 *
475 * I'm not sure how it will work with some Windows paths,
476 * e.g. C:path\to\file.tex since it doesn't really understand the leading C:
477 * But I'm not sure it would even work with a path like that, and I have no
478 * way to test it.
479 *
480 * Behaviour somewhat controlled by the macros SLASH and DIRCHARS in the
481 * OpSys.h file.
482 *
483 */
484
485 void AddDirectoryFromRelativeFile(const char * Filename, struct WordList *TeXInputs)
486 {
487 if ( ! Filename )
488 return;
489
490 /* There are no path delimiters, so it's just a file, return null */
491 if ( ! strstr( Filename, DIRCHARS ) )
492 return;
493
494
495 char buf[BUFFER_SIZE];
496 if ( strchr(DIRCHARS,Filename[0]) ) {
497 strcpy(buf,Filename);
498 } else {
499 getcwd(buf, BUFFER_SIZE);
500 tackon(buf,Filename);
501 }
502
503 /* Keep up to the final SLASH -- that will be the directory. */
504 char * end = strrchr(buf,SLASH);
505 *end = '\0';
506 InsertWord(buf,TeXInputs);
507 }
508
509
510 #if USE_RECURSE
511 static int SearchFile(char *Dir, const char *Filename, const char *App)
512 {
513 struct stat *statbuf;
514 struct dirent *de;
515 DIR *dh;
516
517 int DirLen = strlen(Dir);
518 int Found = FALSE;
519
520 DEBUG(("Searching %s for %s\n", Dir, Filename));
521
522 if (HasFile(Dir, Filename, App))
523 return (TRUE);
524 else
525 {
526 if ((statbuf = malloc(sizeof(struct stat))))
527 {
528 if ((dh = opendir(Dir)))
529 {
530 while (!Found && (de = readdir(dh)))
531 {
532 Dir[DirLen] = 0;
533 if (strcmp(de->d_name, ".") && strcmp(de->d_name, ".."))
534 {
535 tackon(Dir, de->d_name);
536
537 if (!stat(Dir, statbuf))
538 {
539 if ((statbuf->st_mode & S_IFMT) == S_IFDIR)
540 Found = SearchFile(Dir, Filename, App);
541 }
542 }
543 }
544 closedir(dh);
545 }
546 else
547 PrintPrgErr(pmNoOpenDir, Dir);
548 free(statbuf);
549 }
550 }
551 return (Found);
552 }
553 #endif /* USE_RECURSE */
554
555 #if defined(HAVE_STAT)
556 int IsRegFile(const char *Filename)
557 {
558 int Retval = FALSE;
559 struct stat *statbuf;
560 if ((statbuf = malloc(sizeof(struct stat))))
561 {
562 if (!stat(Filename, statbuf))
563 {
564 if ((statbuf->st_mode & S_IFMT) == S_IFREG)
565 Retval = TRUE;
566 }
567 free(statbuf);
568 }
569
570 return Retval;
571 }
572 #else
573 int IsRegFile(const char *Filename) { printf("WTF\n");return TRUE; }
574 #endif