home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / kpathsea / lib.h < prev    next >
C/C++ Source or Header  |  2000-01-15  |  8KB  |  180 lines

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