Debian Codegraph @

texk/kpathsea/lib.h

raw
1 /* lib.h: declarations for common, low-level routines in kpathsea.
2
3 Copyright 1992, 1993, 1994, 1995, 1996, 2008, 2009, 2010, 2011,
4 2012, 2015, 2025 Karl Berry.
5 Copyright 1999, 2000, 2003, 2005 Olaf Weber.
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this library; if not, see <http://www.gnu.org/licenses/>. */
19
20 #ifndef KPATHSEA_LIB_H
21 #define KPATHSEA_LIB_H
22
23 #include <kpathsea/c-proto.h>
24 #include <kpathsea/systypes.h>
25 #include <kpathsea/types.h>
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /* Define common sorts of messages. */
32
33 /* This should be called only after a system call fails. Don't exit
34 with status `errno', because that might be 256, which would mean
35 success (exit statuses are truncated to eight bits). */
36
37 #if defined (KPSE_COMPAT_API)
38 /* This branch works as expected also in programs that use the new interface,
39 because kpathsea_set_program_name() sets up the kpse_def->invocation_name
40 whenever it is available.
41 */
42 #define FATAL_PERROR(str) do { \
43 fprintf (stderr, "%s: ", kpse_def->invocation_name); \
44 perror (str); exit (EXIT_FAILURE); } while (0)
45 #else
46 /* If there is no global variable available, just output the error */
47 #define FATAL_PERROR(str) do { \
48 perror (str); exit (EXIT_FAILURE); } while (0)
49 #endif
50
51 #if defined (KPSE_COMPAT_API)
52 #define START_FATAL() do { \
53 fprintf (stderr, "%s: fatal: ", kpse_def->invocation_name);
54 #else
55 #define START_FATAL() do { \
56 fprintf (stderr, "fatal: ");
57 #endif
58
59 #define END_FATAL() fputs (".\n", stderr); exit (1); } while (0)
60
61 #define FATAL(str) \
62 START_FATAL (); fputs (str, stderr); END_FATAL ()
63 #define FATAL1(str, e1) \
64 START_FATAL (); fprintf (stderr, str, e1); END_FATAL ()
65 #define FATAL2(str, e1, e2) \
66 START_FATAL (); fprintf (stderr, str, e1, e2); END_FATAL ()
67 #define FATAL3(str, e1, e2, e3) \
68 START_FATAL (); fprintf (stderr, str, e1, e2, e3); END_FATAL ()
69 #define FATAL4(str, e1, e2, e3, e4) \
70 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4); END_FATAL ()
71 #define FATAL5(str, e1, e2, e3, e4, e5) \
72 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5); END_FATAL ()
73 #define FATAL6(str, e1, e2, e3, e4, e5, e6) \
74 START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5, e6); END_FATAL ()
75
76 #define START_WARNING() do { fputs ("warning: ", stderr)
77 #define END_WARNING() fputs (".\n", stderr); fflush (stderr); } while (0)
78
79 #define WARNING(str) \
80 START_WARNING (); fputs (str, stderr); END_WARNING ()
81 #define WARNING1(str, e1) \
82 START_WARNING (); fprintf (stderr, str, e1); END_WARNING ()
83 #define WARNING2(str, e1, e2) \
84 START_WARNING (); fprintf (stderr, str, e1, e2); END_WARNING ()
85 #define WARNING3(str, e1, e2, e3) \
86 START_WARNING (); fprintf (stderr, str, e1, e2, e3); END_WARNING ()
87 #define WARNING4(str, e1, e2, e3, e4) \
88 START_WARNING (); fprintf (stderr, str, e1, e2, e3, e4); END_WARNING ()
89
90 /* This requires an identifier `kpse' to be set up correctly. Sorry. */
91 #define LIB_START_FATAL() do { \
92 fprintf (stderr, "%s: fatal: ", kpse->invocation_name);
93
94 #define LIB_FATAL(str) \
95 LIB_START_FATAL (); fputs (str, stderr); END_FATAL ()
96 #define LIB_FATAL1(str, e1) \
97 LIB_START_FATAL (); fprintf (stderr, str, e1); END_FATAL ()
98 #define LIB_FATAL2(str, e1, e2) \
99 LIB_START_FATAL (); fprintf (stderr, str, e1, e2); END_FATAL ()
100
101
102 /* I find this easier to read. */
103 #define STREQ(s1, s2) (((s1) != NULL) && ((s2) != NULL) && (strcmp (s1, s2) == 0))
104 #define STRNEQ(s1, s2, n) ((s1) && (s2) && (strncmp (s1, s2, n) == 0))
105
106 /* Support for FAT/ISO-9660 filesystems. Theoretically this should be
107 done at runtime, per filesystem, but that's painful to program. */
108 #ifdef MONOCASE_FILENAMES
109 #define FILESTRCASEEQ(s1,s2) ((s1) && (s2) && (strcasecmp (s1, s2) == 0))
110 #define FILESTRNCASEEQ(s1,s2,l) ((s1) && (s2) && (strncasecmp (s1,s2,l) == 0))
111 #define FILECHARCASEEQ(c1,c2) (toupper (c1) == toupper (c2))
112 #else
113 #define FILESTRCASEEQ STREQ
114 #define FILESTRNCASEEQ STRNEQ
115 #define FILECHARCASEEQ(c1,c2) ((c1) == (c2))
116 #endif
117
118 /* This is the maximum number of numerals that result when a 64-bit
119 integer is converted to a string, plus one for a trailing null byte,
120 plus one for a sign. */
121 #define MAX_INT_LENGTH 21
122
123 /* If the environment variable TEST is set, return it; otherwise,
124 DEFAULT. This is useful for paths that use more than one envvar. */
125 #define ENVVAR(test, default) (getenv (test) ? (test) : (default))
126
127 /* Return whether a string value S is (some sort of) true. Check
128 for negation values, so a value like "2" will be true. This is used
129 only for texmf.cnf configuration values, where we want to be generous
130 in what we accept as true (or false).
131
132 Nowadays we use the function `kpse_cnf_p' (lowercase) to avoid
133 evaluating the argument multiple times, but leaving the macro just in
134 case something outside Kpathsea uses -- fairly sure there are no such
135 uses, but doesn't hurt to leave it in. */
136 #define KPSE_CNF_P(val) \
137 ((val) && *(val) && *(val) != 'f' && *(val) != '0')
138
139 /* The function that does the same thing. */
140 extern KPSEDLL boolean kpse_cnf_p (const_string s);
141
142
143 /* Return a fresh copy of S1 followed by S2, et al. */
144 extern KPSEDLL string concat (const_string s1, const_string s2);
145 extern KPSEDLL string concat3 (const_string, const_string, const_string);
146 /* `concatn' is declared in its own include file, to avoid pulling in
147 all the varargs stuff. */
148
149 /* A fresh copy of just S. */
150 extern KPSEDLL string xstrdup (const_string s);
151
152 /* Convert all lowercase characters in S to uppercase. */
153 extern KPSEDLL string uppercasify (const_string s);
154
155 /* Like `atoi', but disallow negative numbers. */
156 extern KPSEDLL unsigned atou (const_string);
157
158 /* True if FILENAME1 and FILENAME2 are the same file. If stat fails on
159 either name, return false, no error message.
160 Cf. `SAME_FILE_P' in xstat.h. */
161 extern KPSEDLL boolean same_file_p (const_string filename1,
162 const_string filename2);
163
164 /* Return NAME with any leading path stripped off. This returns a
165 pointer into NAME. */
166 extern KPSEDLL const_string xbasename (const_string name);
167
168 /* Return directory part of NAME. This returns a new string. */
169 extern KPSEDLL string xdirname (const_string name);
170
171 /* If NAME has a suffix, return a pointer to its first character (i.e.,
172 the one after the `.'); otherwise, return NULL. */
173 extern KPSEDLL const_string find_suffix (const_string name);
174
175 /* Return NAME with any suffix removed. */
176 extern KPSEDLL string remove_suffix (const_string name);
177
178 /* Return S with the suffix SUFFIX, removing any suffix already present.
179 For example, `make_suffix ("/foo/bar.baz", "quux")' returns
180 `/foo/bar.quux'. Returns a string allocated with malloc. */
181 extern KPSEDLL string make_suffix (const_string s, const_string suffix);
182
183 /* Return NAME with STEM_PREFIX prepended to the stem. For example,
184 `make_prefix ("/foo/bar.baz", "x")' returns `/foo/xbar.baz'.
185 Returns a string allocated with malloc. */
186 extern KPSEDLL string make_prefix (string stem_prefix, string name);
187
188 /* If NAME has a suffix, simply return it; otherwise, return
189 `NAME.SUFFIX'. */
190 extern KPSEDLL const_string extend_filename (const_string name,
191 const_string suffix);
192
193 /* Call putenv with the string `VAR=VALUE' and abort on error. */
194 extern KPSEDLL void kpathsea_xputenv (kpathsea kpse, const_string var,
195 const_string value);
196 extern KPSEDLL void kpathsea_xputenv_int (kpathsea kpse, const_string var,
197 int value);
198 #if defined (KPSE_COMPAT_API)
199 extern KPSEDLL void xputenv (const_string var, const_string value);
200 extern KPSEDLL void xputenv_int (const_string var, int value);
201 #endif
202
203 /* Return the current working directory. */
204 extern KPSEDLL string xgetcwd (void);
205
206 /* Returns true if FN is a directory or a symlink to a directory. */
207 extern KPSEDLL boolean kpathsea_dir_p (kpathsea kpse, string fn);
208 #if defined (KPSE_COMPAT_API)
209 extern KPSEDLL boolean dir_p (string fn);
210 #endif
211
212 /* If FN is a readable directory, return the number of links it has.
213 Otherwise, return -1. The nlinks parameter is a dummy on UNIX. */
214 #if defined (KPSE_COMPAT_API)
215 extern KPSEDLL int dir_links (const_string fn, long nlinks);
216 #endif
217 extern KPSEDLL int kpathsea_dir_links (kpathsea kpse, const_string fn,
218 long nlinks);
219
220 /* Like their stdio counterparts, but abort on error, after calling
221 perror(3) with FILENAME as its argument. */
222 extern KPSEDLL FILE *xfopen (const_string filename, const_string mode);
223 extern KPSEDLL void xfclose (FILE *fp, const_string filename);
224 extern KPSEDLL void xfseek (FILE *fp, long offset, int wherefrom, const_string filename);
225 extern KPSEDLL void xfseeko (FILE *fp, off_t offset, int wherefrom, const_string filename);
226 extern KPSEDLL long xftell (FILE *fp, const_string filename);
227 extern KPSEDLL off_t xftello (FILE *fp, const_string filename);
228
229 /* These call the corresponding function in the standard library, and
230 abort if those routines fail. Also, `xrealloc' calls `xmalloc' if
231 OLD_ADDRESS is null. */
232 extern KPSEDLL address xmalloc (size_t size);
233 extern KPSEDLL address xrealloc (address old_address, size_t new_size);
234 extern KPSEDLL address xcalloc (size_t nelem, size_t elsize);
235
236 /* (Re)Allocate N items of type T using xmalloc/xrealloc. */
237 #define XTALLOC(n, t) ((t *) xmalloc ((n) * sizeof (t)))
238 #define XTALLOC1(t) XTALLOC (1, t)
239 #define XRETALLOC(addr, n, t) ((addr) = (t *) xrealloc (addr, (n) * sizeof(t)))
240
241 #ifdef __cplusplus
242 }
243 #endif
244
245 #endif /* not KPATHSEA_LIB_H */