Back to index

tetex-bin  3.0
lib.h
Go to the documentation of this file.
00001 /* lib.h: declarations for common, low-level routines in kpathsea.
00002 
00003 Copyright (C) 1992, 93, 94, 95, 96, 2000 Free Software Foundation, Inc.
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Library General Public
00007 License as published by the Free Software Foundation; either
00008 version 2 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Library General Public License for more details.
00014 
00015 You should have received a copy of the GNU Library General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00018 
00019 #ifndef KPATHSEA_LIB_H
00020 #define KPATHSEA_LIB_H
00021 
00022 #include <kpathsea/c-proto.h>
00023 #include <kpathsea/types.h>
00024 
00025 /* Define common sorts of messages.  */
00026 
00027 /* This should be called only after a system call fails.  Don't exit
00028    with status `errno', because that might be 256, which would mean
00029    success (exit statuses are truncated to eight bits).  */
00030 #define FATAL_PERROR(str) do { \
00031   fprintf (stderr, "%s: ", program_invocation_name); \
00032   perror (str); exit (EXIT_FAILURE); } while (0)
00033 
00034 #define START_FATAL() do { \
00035   fprintf (stderr, "%s: fatal: ", program_invocation_name);
00036 #define END_FATAL() fputs (".\n", stderr); exit (1); } while (0)
00037 
00038 #define FATAL(str)                                             \
00039   START_FATAL (); fputs (str, stderr); END_FATAL ()
00040 #define FATAL1(str, e1)                                               \
00041   START_FATAL (); fprintf (stderr, str, e1); END_FATAL ()
00042 #define FATAL2(str, e1, e2)                                    \
00043   START_FATAL (); fprintf (stderr, str, e1, e2); END_FATAL ()
00044 #define FATAL3(str, e1, e2, e3)                                       \
00045   START_FATAL (); fprintf (stderr, str, e1, e2, e3); END_FATAL ()
00046 #define FATAL4(str, e1, e2, e3, e4)                                   \
00047   START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4); END_FATAL ()
00048 #define FATAL5(str, e1, e2, e3, e4, e5)                               \
00049   START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5); END_FATAL ()
00050 #define FATAL6(str, e1, e2, e3, e4, e5, e6)                           \
00051   START_FATAL (); fprintf (stderr, str, e1, e2, e3, e4, e5, e6); END_FATAL ()
00052 
00053 
00054 #define START_WARNING() do { fputs ("warning: ", stderr)
00055 #define END_WARNING() fputs (".\n", stderr); fflush (stderr); } while (0)
00056 
00057 #define WARNING(str)                                           \
00058   START_WARNING (); fputs (str, stderr); END_WARNING ()
00059 #define WARNING1(str, e1)                                      \
00060   START_WARNING (); fprintf (stderr, str, e1); END_WARNING ()
00061 #define WARNING2(str, e1, e2)                                         \
00062   START_WARNING (); fprintf (stderr, str, e1, e2); END_WARNING ()
00063 #define WARNING3(str, e1, e2, e3)                              \
00064   START_WARNING (); fprintf (stderr, str, e1, e2, e3); END_WARNING ()
00065 #define WARNING4(str, e1, e2, e3, e4)                                 \
00066   START_WARNING (); fprintf (stderr, str, e1, e2, e3, e4); END_WARNING ()
00067 
00068 
00069 /* I find this easier to read.  */
00070 #define STREQ(s1, s2) ((s1) && (s2) && (strcmp (s1, s2) == 0))
00071 #define STRNEQ(s1, s2, n) ((s1) && (s2) && (strncmp (s1, s2, n) == 0))
00072       
00073 /* Support for FAT/ISO-9660 filesystems.  Theoretically this should be
00074    done at runtime, per filesystem, but that's painful to program.  */
00075 #ifdef MONOCASE_FILENAMES
00076 #define FILESTRCASEEQ(s1, s2) ((s1) && (s2) && (strcasecmp (s1, s2) == 0))
00077 #define FILESTRNCASEEQ(s1, s2, l) ((s1) && (s2) && (strncasecmp (s1, s2, l) == 0))
00078 #define FILECHARCASEEQ(c1, c2) (toupper (c1) == toupper (c2))
00079 #else
00080 #define FILESTRCASEEQ STREQ
00081 #define FILESTRNCASEEQ STRNEQ
00082 #define FILECHARCASEEQ(c1, c2) ((c1) == (c2))
00083 #endif
00084 
00085 /* This is the maximum number of numerals that result when a 64-bit
00086    integer is converted to a string, plus one for a trailing null byte,
00087    plus one for a sign.  */
00088 #define MAX_INT_LENGTH 21
00089 
00090 /* If the environment variable TEST is set, return it; otherwise,
00091    DEFAULT.  This is useful for paths that use more than one envvar.  */
00092 #define ENVVAR(test, default) (getenv (test) ? (test) : (default))
00093 
00094 /* Return a fresh copy of S1 followed by S2, et al.  */
00095 extern KPSEDLL string concat P2H(const_string s1, const_string s2);
00096 extern KPSEDLL string concat3 P3H(const_string, const_string, const_string);
00097 /* `concatn' is declared in its own include file, to avoid pulling in
00098    all the varargs stuff.  */
00099 
00100 /* A fresh copy of just S.  */
00101 extern KPSEDLL string xstrdup P1H(const_string s);
00102 
00103 /* Convert all lowercase characters in S to uppercase.  */
00104 extern KPSEDLL string uppercasify P1H(const_string s);
00105 
00106 /* Like `atoi', but disallow negative numbers.  */
00107 extern KPSEDLL unsigned atou P1H(const_string);
00108 
00109 /* True if FILENAME1 and FILENAME2 are the same file.  If stat fails on
00110    either name, return false, no error message.
00111    Cf. `SAME_FILE_P' in xstat.h.  */
00112 extern KPSEDLL boolean same_file_p P2H(const_string filename1,
00113                                          const_string filename2);
00114 
00115 /* Return NAME with any leading path stripped off.  This returns a
00116    pointer into NAME.  */
00117 extern KPSEDLL const_string xbasename P1H(const_string name);
00118 
00119 /* Return directory part of NAME. This returns a new string. */
00120 extern KPSEDLL string xdirname P1H(const_string name);
00121 
00122 #ifndef HAVE_STRSTR
00123 extern string strstr P2H(const_string haystack, const_string needle);
00124 #endif
00125 
00126 /* If NAME has a suffix, return a pointer to its first character (i.e.,
00127    the one after the `.'); otherwise, return NULL.  */
00128 extern KPSEDLL string find_suffix P1H(const_string name);
00129 
00130 /* Return NAME with any suffix removed.  */
00131 extern KPSEDLL string remove_suffix P1H(const_string name);
00132 
00133 /* Return S with the suffix SUFFIX, removing any suffix already present.
00134    For example, `make_suffix ("/foo/bar.baz", "quux")' returns
00135    `/foo/bar.quux'.  Returns a string allocated with malloc.  */
00136 extern KPSEDLL string make_suffix P2H(const_string s,  const_string suffix);
00137 
00138 /* Return NAME with STEM_PREFIX prepended to the stem. For example,
00139    `make_prefix ("/foo/bar.baz", "x")' returns `/foo/xbar.baz'.
00140    Returns a string allocated with malloc.  */
00141 extern KPSEDLL string make_prefix P2H(string stem_prefix, string name);
00142 
00143 /* If NAME has a suffix, simply return it; otherwise, return
00144    `NAME.SUFFIX'.  */
00145 extern KPSEDLL string extend_filename P2H(const_string name,
00146                                             const_string suffix);
00147 
00148 /* Call putenv with the string `VAR=VALUE' and abort on error.  */
00149 extern KPSEDLL void xputenv P2H(const_string var, const_string value);
00150 extern KPSEDLL void xputenv_int P2H(const_string var, int value);
00151 
00152 /* Return the current working directory.  */
00153 extern KPSEDLL string xgetcwd P1H(void);
00154 
00155 /* Returns true if FN is a directory or a symlink to a directory.  */
00156 extern KPSEDLL boolean dir_p P1H(const_string fn);
00157 
00158 /* If FN is a readable directory, return the number of links it has.
00159    Otherwise, return -1.  The nlinks parameter is a dummy on UNIX. */
00160 extern KPSEDLL int dir_links P2H(const_string fn, long nlinks);
00161 
00162 /* Like their stdio counterparts, but abort on error, after calling
00163    perror(3) with FILENAME as its argument.  */
00164 extern KPSEDLL FILE *xfopen P2H(const_string filename, const_string mode);
00165 extern KPSEDLL void xfclose P2H(FILE *, const_string filename);
00166 extern KPSEDLL void xfseek P4H(FILE *, long, int, string filename);
00167 extern KPSEDLL unsigned long xftell P2H(FILE *, string filename);
00168 
00169 /* These call the corresponding function in the standard library, and
00170    abort if those routines fail.  Also, `xrealloc' calls `xmalloc' if
00171    OLD_ADDRESS is null.  */
00172 extern KPSEDLL address xmalloc P1H(unsigned size);
00173 extern KPSEDLL address xrealloc P2H(address old_address, unsigned new_size);
00174 extern KPSEDLL address xcalloc P2H(unsigned nelem, unsigned elsize);
00175 
00176 /* (Re)Allocate N items of type T using xmalloc/xrealloc.  */
00177 #define XTALLOC(n, t) ((t *) xmalloc ((n) * sizeof (t)))
00178 #define XTALLOC1(t) XTALLOC (1, t)
00179 #define XRETALLOC(addr, n, t) ((addr) = (t *) xrealloc (addr, (n) * sizeof(t)))
00180 
00181 #endif /* not KPATHSEA_LIB_H */