1 /* lib.h: declarations for shared routines.
3 Copyright 1992, 1993 Karl Berry
4 Copyright 2007 TeX Users Group
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 /* Define common sorts of messages. */
28 /* This should be called only after a system call fails. */
29 // #define FATAL_PERROR(s) do { perror (s); exit (errno); } while (0)
30 #define FATAL_PERROR(s) do { perrormod (s); exit (errno); } while (0)
32 #define START_FATAL() do { fputs ("fatal: ", stderr)
33 #define END_FATAL() fputs (".\n", stderr); exit (1); } while (0)
36 START_FATAL(); fprintf (stderr, "%s", str); END_FATAL ()
37 #define FATAL1(str, e1) \
38 START_FATAL(); fprintf (stderr, str, e1); END_FATAL ()
39 #define FATAL2(str, e1, e2) \
40 START_FATAL(); fprintf (stderr, str, e1, e2); END_FATAL ()
41 #define FATAL3(str, e1, e2, e3) \
42 START_FATAL(); fprintf (stderr, str, e1, e2, e3); END_FATAL ()
43 #define FATAL4(str, e1, e2, e3, e4) \
44 START_FATAL(); fprintf (stderr, str, e1, e2, e3, e4); END_FATAL ()
47 #define START_WARNING() do { fputs ("warning: ", stderr)
48 #define END_WARNING() fputs (".\n", stderr); fflush (stderr); } while (0)
50 #define WARNING(str) \
51 START_WARNING(); fprintf (stderr, "%s", str); END_WARNING ()
52 #define WARNING1(str, e1) \
53 START_WARNING(); fprintf (stderr, str, e1); END_WARNING ()
54 #define WARNING2(str, e1, e2) \
55 START_WARNING(); fprintf (stderr, str, e1, e2); END_WARNING ()
56 #define WARNING3(str, e1, e2, e3) \
57 START_WARNING(); fprintf (stderr, str, e1, e2, e3); END_WARNING ()
58 #define WARNING4(str, e1, e2, e3, e4) \
59 START_WARNING(); fprintf (stderr, str, e1, e2, e3, e4); END_WARNING ()
61 /* I find this easier to read. */
62 #define STREQ(s1, s2) (strcmp (s1, s2) == 0)
64 /* This is the maximum number of numerals that result when a 64-bit
65 integer is converted to a string, plus one for a trailing null byte,
66 plus one for a sign. */
67 #define MAX_INT_LENGTH 21
69 /* Return a fresh copy of S1 followed by S2, et al. */
70 extern string concat (string s1, string s2);
71 extern string concat3 (string, string, string);
72 extern string concat4 (string, string, string, string);
73 extern string concat5 (string, string, string, string, string);
76 /* A fresh copy of just S. */
77 extern string xstrdup (string s);
80 /* True if FILENAME1 and FILENAME2 are the same file. If stat fails on
81 either name, returns false, with no error message. */
82 extern bool same_file_p (string filename1, string filename2);
85 /* If NAME has a suffix, return a pointer to its first character (i.e.,
86 the one after the `.'); otherwise, return NULL. */
87 extern string find_suffix (string name);
89 /* Return NAME with any suffix removed. */
90 extern string remove_suffix (string name);
92 /* Return S with the suffix SUFFIX, removing any suffix already present.
93 For example, `make_suffix ("/foo/bar.baz", "karl")' returns
94 `/foo/bar.karl'. Returns a string allocated with malloc. */
95 extern string make_suffix (string s, string suffix);
97 /* Return NAME with STEM_PREFIX prepended to the stem. For example,
98 `make_prefix ("/foo/bar.baz", "x")' returns `/foo/xbar.baz'.
99 Returns a string allocated with malloc. */
100 extern string make_prefix (string stem_prefix, string name);
102 /* If NAME has a suffix, simply return it; otherwise, return
104 extern string extend_filename (string name, string suffix);
107 /* Like their stdio counterparts, but abort on error, after calling
108 perror(3) with FILENAME as its argument. */
109 extern FILE *xfopen (string filename, string mode);
110 // extern void xfclose (FILE *, string filename);
111 extern int xfclose (FILE *, string filename);
112 extern void xfseek (FILE *, long, int, string filename);
113 extern long xftell (FILE *, string filename);
116 /* These call the corresponding function in the standard library, and
117 abort if those routines fail. Also, `xrealloc' calls `xmalloc' if
118 OLD_ADDRESS is null. */
119 extern address xmalloc (unsigned size);
120 extern address xrealloc (address old_address, unsigned new_size);
121 extern address xcalloc (unsigned nelem, unsigned elsize);
123 /* (Re)Allocate N items of type T using xmalloc/xrealloc. */
124 #define XTALLOC(n, t) (t *) xmalloc ((n) * sizeof (t))
125 #define XTALLOC1(t) XTALLOC (1, t)
126 #define XRETALLOC(addr, n, t) ((addr) = (t *) xrealloc (addr, (n) * sizeof(t)))
128 #endif /* not LIB_H */