dvitomp fix from Akira
[mplib] / src / texk / kpathsea / lib.h
1 /* lib.h: declarations for common, low-level routines in kpathsea.
2
3    Copyright 1999, 2000, 2003, 2005 Olaf Weber.
4    Copyright 1992, 1993, 1994, 1995, 1996, 2008 Karl Berry.
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public License
17    along with this library; if not, see <http://www.gnu.org/licenses/>.  */
18
19 #ifndef KPATHSEA_LIB_H
20 #define KPATHSEA_LIB_H
21
22 #include <kpathsea/c-proto.h>
23 #include <kpathsea/systypes.h>
24 #include <kpathsea/types.h>
25
26 /* Define common sorts of messages.  */
27
28 /* This should be called only after a system call fails.  Don't exit
29    with status `errno', because that might be 256, which would mean
30    success (exit statuses are truncated to eight bits).  */
31 #define FATAL_PERROR(str) do { \
32   fprintf (stderr, "%s: ", program_invocation_name); \
33   perror (str); exit (EXIT_FAILURE); } while (0)
34
35 #define START_FATAL() do { \
36   fprintf (stderr, "%s: fatal: ", program_invocation_name);
37 #define END_FATAL() fputs (".\n", stderr); exit (1); } while (0)
38
39 #define FATAL(str)                                                      \
40   START_FATAL (); fputs (str, stderr); END_FATAL ()
41 #define FATAL1(str, e1)                                                 \
42   START_FATAL (); fprintf (stderr, str, e1); END_FATAL ()
43 #define FATAL2(str, e1, e2)                                             \
44   START_FATAL (); fprintf (stderr, str, e1, e2); END_FATAL ()
45 #define FATAL3(str, e1, e2, e3)                                         \
46   START_FATAL (); fprintf (stderr, str, e1, e2, e3); END_FATAL ()
47 #define FATAL4(str, e1, e2, e3, e4)                                     \
48   START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4); END_FATAL ()
49 #define FATAL5(str, e1, e2, e3, e4, e5)                                 \
50   START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5); END_FATAL ()
51 #define FATAL6(str, e1, e2, e3, e4, e5, e6)                             \
52   START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5, e6); END_FATAL ()
53
54
55 #define START_WARNING() do { fputs ("warning: ", stderr)
56 #define END_WARNING() fputs (".\n", stderr); fflush (stderr); } while (0)
57
58 #define WARNING(str)                                                    \
59   START_WARNING (); fputs (str, stderr); END_WARNING ()
60 #define WARNING1(str, e1)                                               \
61   START_WARNING (); fprintf (stderr, str, e1); END_WARNING ()
62 #define WARNING2(str, e1, e2)                                           \
63   START_WARNING (); fprintf (stderr, str, e1, e2); END_WARNING ()
64 #define WARNING3(str, e1, e2, e3)                                       \
65   START_WARNING (); fprintf (stderr, str, e1, e2, e3); END_WARNING ()
66 #define WARNING4(str, e1, e2, e3, e4)                                   \
67   START_WARNING (); fprintf (stderr, str, e1, e2, e3, e4); END_WARNING ()
68
69
70 /* I find this easier to read.  */
71 #define STREQ(s1, s2) ((s1) && (s2) && (strcmp (s1, s2) == 0))
72 #define STRNEQ(s1, s2, n) ((s1) && (s2) && (strncmp (s1, s2, n) == 0))
73       
74 /* Support for FAT/ISO-9660 filesystems.  Theoretically this should be
75    done at runtime, per filesystem, but that's painful to program.  */
76 #ifdef MONOCASE_FILENAMES
77 #define FILESTRCASEEQ(s1, s2) ((s1) && (s2) && (strcasecmp (s1, s2) == 0))
78 #define FILESTRNCASEEQ(s1, s2, l) ((s1) && (s2) && (strncasecmp (s1, s2, l) == 0))
79 #define FILECHARCASEEQ(c1, c2) (toupper (c1) == toupper (c2))
80 #else
81 #define FILESTRCASEEQ STREQ
82 #define FILESTRNCASEEQ STRNEQ
83 #define FILECHARCASEEQ(c1, c2) ((c1) == (c2))
84 #endif
85
86 /* This is the maximum number of numerals that result when a 64-bit
87    integer is converted to a string, plus one for a trailing null byte,
88    plus one for a sign.  */
89 #define MAX_INT_LENGTH 21
90
91 /* If the environment variable TEST is set, return it; otherwise,
92    DEFAULT.  This is useful for paths that use more than one envvar.  */
93 #define ENVVAR(test, default) (getenv (test) ? (test) : (default))
94 \f
95 /* Return a fresh copy of S1 followed by S2, et al.  */
96 extern KPSEDLL string concat P2H(const_string s1, const_string s2);
97 extern KPSEDLL string concat3 P3H(const_string, const_string, const_string);
98 /* `concatn' is declared in its own include file, to avoid pulling in
99    all the varargs stuff.  */
100
101 /* A fresh copy of just S.  */
102 extern KPSEDLL string xstrdup P1H(const_string s);
103
104 /* Convert all lowercase characters in S to uppercase.  */
105 extern KPSEDLL string uppercasify P1H(const_string s);
106
107 /* Like `atoi', but disallow negative numbers.  */
108 extern KPSEDLL unsigned atou P1H(const_string);
109
110 /* True if FILENAME1 and FILENAME2 are the same file.  If stat fails on
111    either name, return false, no error message.
112    Cf. `SAME_FILE_P' in xstat.h.  */
113 extern KPSEDLL boolean same_file_p P2H(const_string filename1,
114                                          const_string filename2);
115
116 /* Return NAME with any leading path stripped off.  This returns a
117    pointer into NAME.  */
118 extern KPSEDLL const_string xbasename P1H(const_string name);
119
120 /* Return directory part of NAME. This returns a new string. */
121 extern KPSEDLL string xdirname P1H(const_string name);
122
123 #if !HAVE_DECL_STRSTR
124 extern string strstr P2H(const_string haystack, const_string needle);
125 #endif
126
127 /* If NAME has a suffix, return a pointer to its first character (i.e.,
128    the one after the `.'); otherwise, return NULL.  */
129 extern KPSEDLL string find_suffix P1H(const_string name);
130
131 /* Return NAME with any suffix removed.  */
132 extern KPSEDLL string remove_suffix P1H(const_string name);
133
134 /* Return S with the suffix SUFFIX, removing any suffix already present.
135    For example, `make_suffix ("/foo/bar.baz", "quux")' returns
136    `/foo/bar.quux'.  Returns a string allocated with malloc.  */
137 extern KPSEDLL string make_suffix P2H(const_string s,  const_string suffix);
138
139 /* Return NAME with STEM_PREFIX prepended to the stem. For example,
140    `make_prefix ("/foo/bar.baz", "x")' returns `/foo/xbar.baz'.
141    Returns a string allocated with malloc.  */
142 extern KPSEDLL string make_prefix P2H(string stem_prefix, string name);
143
144 /* If NAME has a suffix, simply return it; otherwise, return
145    `NAME.SUFFIX'.  */
146 extern KPSEDLL string extend_filename P2H(const_string name,
147                                             const_string suffix);
148 \f
149 /* Call putenv with the string `VAR=VALUE' and abort on error.  */
150 extern KPSEDLL void xputenv P2H(const_string var, const_string value);
151 extern KPSEDLL void xputenv_int P2H(const_string var, int value);
152
153 /* Return the current working directory.  */
154 extern KPSEDLL string xgetcwd P1H(void);
155
156 /* Returns true if FN is a directory or a symlink to a directory.  */
157 extern KPSEDLL boolean dir_p P1H(const_string fn);
158
159 /* If FN is a readable directory, return the number of links it has.
160    Otherwise, return -1.  The nlinks parameter is a dummy on UNIX. */
161 extern KPSEDLL int dir_links P2H(const_string fn, long nlinks);
162
163 /* Like their stdio counterparts, but abort on error, after calling
164    perror(3) with FILENAME as its argument.  */
165 extern KPSEDLL FILE *xfopen P2H(const_string filename, const_string mode);
166 extern KPSEDLL void xfclose P2H(FILE *, const_string filename);
167 extern KPSEDLL void xfseek P4H(FILE *, long, int, string filename);
168 extern KPSEDLL void xfseeko P4H(FILE *, off_t, int, string filename);
169 extern KPSEDLL unsigned long xftell P2H(FILE *, string filename);
170 extern KPSEDLL off_t xftello P2H(FILE *, string filename);
171
172 /* These call the corresponding function in the standard library, and
173    abort if those routines fail.  Also, `xrealloc' calls `xmalloc' if
174    OLD_ADDRESS is null.  */
175 extern KPSEDLL address xmalloc P1H(unsigned size);
176 extern KPSEDLL address xrealloc P2H(address old_address, unsigned new_size);
177 extern KPSEDLL address xcalloc P2H(unsigned nelem, unsigned elsize);
178
179 /* (Re)Allocate N items of type T using xmalloc/xrealloc.  */
180 #define XTALLOC(n, t) ((t *) xmalloc ((n) * sizeof (t)))
181 #define XTALLOC1(t) XTALLOC (1, t)
182 #define XRETALLOC(addr, n, t) ((addr) = (t *) xrealloc (addr, (n) * sizeof(t)))
183
184 #endif /* not KPATHSEA_LIB_H */