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