OSDN Git Service

9e5ebf64045d517e60b3d9cc68e10299bf195ec4
[putex/putex.git] / src / texsourc / lib.h
1 /* lib.h: declarations for shared routines.
2
3    Copyright 1992, 1993 Karl Berry
4    Copyright 2007 TeX Users Group
5
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.
10
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.
15
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
19    02110-1301 USA.  */
20
21 #ifndef LIB_H
22 #define LIB_H
23
24 #include "types.h"
25
26 /* Define common sorts of messages.  */
27
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)
31
32 #define START_FATAL() do { fputs ("fatal: ", stderr)
33 #define END_FATAL() fputs (".\n", stderr); exit (1); } while (0)
34
35 #define FATAL(str)                                                      \
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 ()
45
46
47 #define START_WARNING() do { fputs ("warning: ", stderr)
48 #define END_WARNING() fputs (".\n", stderr); fflush (stderr); } while (0)
49
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 ()
60
61 /* I find this easier to read.  */
62 #define STREQ(s1, s2) (strcmp (s1, s2) == 0)
63
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
68 \f
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);
74
75
76 /* A fresh copy of just S.  */
77 extern string xstrdup (string s);
78
79
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);
83
84
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);
88
89 /* Return NAME with any suffix removed.  */
90 extern string remove_suffix (string name);
91
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);
96
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);
101
102 /* If NAME has a suffix, simply return it; otherwise, return
103    `NAME.SUFFIX'.  */
104 extern string extend_filename (string name, string suffix);
105
106
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);
114
115
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);
122
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)))
127
128 #endif /* not LIB_H */
129 \1a