PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/wml_backend/p4_gm4/lib/error.c

https://bitbucket.org/shlomif/website-meta-language
C | 252 lines | 182 code | 31 blank | 39 comment | 27 complexity | fe45e9ff9f197f9aab5c5dc73b58428d MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.0
  1. /* Error handler for noninteractive utilities
  2. Copyright (C) 1990,91,92,93,94,95,96,97,98 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library. Its master source is NOT part of
  4. the C library, however. The master source lives in /gd/gnu/lib.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #if defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT) || defined(_LIBC)
  23. # if __STDC__
  24. # include <stdarg.h>
  25. # define VA_START(args, lastarg) va_start(args, lastarg)
  26. # else
  27. # include <varargs.h>
  28. # define VA_START(args, lastarg) va_start(args)
  29. # endif
  30. #else
  31. # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
  32. # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
  33. #endif
  34. #if defined(STDC_HEADERS) || defined(_LIBC)
  35. # include <stdlib.h>
  36. # include <string.h>
  37. #else
  38. void exit ();
  39. #endif
  40. #include "error.h"
  41. #ifndef _
  42. # define _(String) String
  43. #endif
  44. /* If NULL, error will flush stdout, then print on stderr the program
  45. name, a colon and a space. Otherwise, error will call this
  46. function without parameters instead. */
  47. void (*error_print_progname) (
  48. #if __STDC__ - 0
  49. void
  50. #endif
  51. );
  52. /* This variable is incremented each time `error' is called. */
  53. unsigned int error_message_count;
  54. #ifdef _LIBC
  55. /* In the GNU C library, there is a predefined variable for this. */
  56. # define program_name program_invocation_name
  57. # include <errno.h>
  58. /* In GNU libc we want do not want to use the common name `error' directly.
  59. Instead make it a weak alias. */
  60. # define error __error
  61. # define error_at_line __error_at_line
  62. # ifdef USE_IN_LIBIO
  63. # include <libio/iolibio.h>
  64. # define fflush(s) _IO_fflush (s)
  65. # endif
  66. #else /* not _LIBC */
  67. /* The calling program should define program_name and set it to the
  68. name of the executing program. */
  69. extern char *program_name;
  70. # ifdef HAVE_STRERROR_R
  71. # define __strerror_r strerror_r
  72. # else
  73. # ifdef HAVE_STRERROR
  74. # ifndef strerror /* On some systems, strerror is a macro */
  75. char *strerror ();
  76. # endif
  77. # else
  78. static char *
  79. private_strerror (errnum)
  80. int errnum;
  81. {
  82. extern char *sys_errlist[];
  83. extern int sys_nerr;
  84. if (errnum > 0 && errnum <= sys_nerr)
  85. return _(sys_errlist[errnum]);
  86. return _("Unknown system error");
  87. }
  88. # define strerror private_strerror
  89. # endif /* HAVE_STRERROR */
  90. # endif /* HAVE_STRERROR_R */
  91. #endif /* not _LIBC */
  92. /* Print the program name and error message MESSAGE, which is a printf-style
  93. format string with optional args.
  94. If ERRNUM is nonzero, print its corresponding system error message.
  95. Exit with status STATUS if it is nonzero. */
  96. /* VARARGS */
  97. void
  98. #if defined VA_START && __STDC__
  99. error (int status, int errnum, const char *message, ...)
  100. #else
  101. error (status, errnum, message, va_alist)
  102. int status;
  103. int errnum;
  104. char *message;
  105. va_dcl
  106. #endif
  107. {
  108. #ifdef VA_START
  109. va_list args;
  110. #endif
  111. if (error_print_progname)
  112. (*error_print_progname) ();
  113. else
  114. {
  115. fflush (stdout);
  116. fprintf (stderr, "%s: ", program_name);
  117. }
  118. #ifdef VA_START
  119. VA_START (args, message);
  120. # if defined(HAVE_VPRINTF) || defined(_LIBC)
  121. vfprintf (stderr, message, args);
  122. # else
  123. _doprnt (message, args, stderr);
  124. # endif
  125. va_end (args);
  126. #else
  127. fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  128. #endif
  129. ++error_message_count;
  130. if (errnum)
  131. {
  132. #if defined HAVE_STRERROR_R || defined _LIBC
  133. char errbuf[1024];
  134. fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
  135. #else
  136. fprintf (stderr, ": %s", strerror (errnum));
  137. #endif
  138. }
  139. putc ('\n', stderr);
  140. fflush (stderr);
  141. if (status)
  142. exit (status);
  143. }
  144. /* Sometimes we want to have at most one error per line. This
  145. variable controls whether this mode is selected or not. */
  146. int error_one_per_line;
  147. void
  148. #if defined VA_START && __STDC__
  149. error_at_line (int status, int errnum, const char *file_name,
  150. unsigned int line_number, const char *message, ...)
  151. #else
  152. error_at_line (status, errnum, file_name, line_number, message, va_alist)
  153. int status;
  154. int errnum;
  155. const char *file_name;
  156. unsigned int line_number;
  157. char *message;
  158. va_dcl
  159. #endif
  160. {
  161. #ifdef VA_START
  162. va_list args;
  163. #endif
  164. if (error_one_per_line)
  165. {
  166. static const char *old_file_name;
  167. static unsigned int old_line_number;
  168. if (old_line_number == line_number &&
  169. (file_name == old_file_name || !strcmp (old_file_name, file_name)))
  170. /* Simply return and print nothing. */
  171. return;
  172. old_file_name = file_name;
  173. old_line_number = line_number;
  174. }
  175. if (error_print_progname)
  176. (*error_print_progname) ();
  177. else
  178. {
  179. fflush (stdout);
  180. fprintf (stderr, "%s:", program_name);
  181. }
  182. if (file_name != NULL)
  183. fprintf (stderr, "%s:%d: ", file_name, line_number);
  184. #ifdef VA_START
  185. VA_START (args, message);
  186. # if defined(HAVE_VPRINTF) || defined(_LIBC)
  187. vfprintf (stderr, message, args);
  188. # else
  189. _doprnt (message, args, stderr);
  190. # endif
  191. va_end (args);
  192. #else
  193. fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  194. #endif
  195. ++error_message_count;
  196. if (errnum)
  197. {
  198. #if defined HAVE_STRERROR_R || defined _LIBC
  199. char errbuf[1024];
  200. fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
  201. #else
  202. fprintf (stderr, ": %s", strerror (errnum));
  203. #endif
  204. }
  205. putc ('\n', stderr);
  206. fflush (stderr);
  207. if (status)
  208. exit (status);
  209. }
  210. #ifdef _LIBC
  211. /* Make the weak alias. */
  212. # undef error
  213. # undef error_at_line
  214. weak_alias (__error, error)
  215. weak_alias (__error_at_line, error_at_line)
  216. #endif