PageRenderTime 72ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/diff/lib/error.c

https://bitbucket.org/freebsd/freebsd-head/
C | 310 lines | 226 code | 45 blank | 39 comment | 34 complexity | cbf3121a545da0c7bad6053c31661b62 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. /* Error handler for noninteractive utilities
  2. Copyright (C) 1990-1998, 2000-2002, 2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  16. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include "error.h"
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #ifdef _LIBC
  25. # include <libintl.h>
  26. #else
  27. # include "gettext.h"
  28. #endif
  29. #ifdef _LIBC
  30. # include <wchar.h>
  31. # define mbsrtowcs __mbsrtowcs
  32. #endif
  33. #if !_LIBC
  34. # include "unlocked-io.h"
  35. #endif
  36. #ifndef _
  37. # define _(String) String
  38. #endif
  39. /* If NULL, error will flush stdout, then print on stderr the program
  40. name, a colon and a space. Otherwise, error will call this
  41. function without parameters instead. */
  42. void (*error_print_progname) (void);
  43. /* This variable is incremented each time `error' is called. */
  44. unsigned int error_message_count;
  45. #ifdef _LIBC
  46. /* In the GNU C library, there is a predefined variable for this. */
  47. # define program_name program_invocation_name
  48. # include <errno.h>
  49. # include <libio/libioP.h>
  50. /* In GNU libc we want do not want to use the common name `error' directly.
  51. Instead make it a weak alias. */
  52. extern void __error (int status, int errnum, const char *message, ...)
  53. __attribute__ ((__format__ (__printf__, 3, 4)));
  54. extern void __error_at_line (int status, int errnum, const char *file_name,
  55. unsigned int line_number, const char *message,
  56. ...)
  57. __attribute__ ((__format__ (__printf__, 5, 6)));;
  58. # define error __error
  59. # define error_at_line __error_at_line
  60. # include <libio/iolibio.h>
  61. # define fflush(s) INTUSE(_IO_fflush) (s)
  62. # undef putc
  63. # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
  64. # include <bits/libc-lock.h>
  65. #else /* not _LIBC */
  66. # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
  67. # ifndef HAVE_DECL_STRERROR_R
  68. "this configure-time declaration test was not run"
  69. # endif
  70. char *strerror_r ();
  71. # endif
  72. # ifndef SIZE_MAX
  73. # define SIZE_MAX ((size_t) -1)
  74. # endif
  75. /* The calling program should define program_name and set it to the
  76. name of the executing program. */
  77. extern char *program_name;
  78. # if HAVE_STRERROR_R || defined strerror_r
  79. # define __strerror_r strerror_r
  80. # endif
  81. #endif /* not _LIBC */
  82. static void
  83. print_errno_message (int errnum)
  84. {
  85. char const *s;
  86. #if defined HAVE_STRERROR_R || _LIBC
  87. char errbuf[1024];
  88. # if STRERROR_R_CHAR_P || _LIBC
  89. s = __strerror_r (errnum, errbuf, sizeof errbuf);
  90. # else
  91. if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
  92. s = errbuf;
  93. else
  94. s = 0;
  95. # endif
  96. #else
  97. s = strerror (errnum);
  98. #endif
  99. #if !_LIBC
  100. if (! s)
  101. s = _("Unknown system error");
  102. #endif
  103. #if _LIBC
  104. if (_IO_fwide (stderr, 0) > 0)
  105. {
  106. __fwprintf (stderr, L": %s", s);
  107. return;
  108. }
  109. #endif
  110. fprintf (stderr, ": %s", s);
  111. }
  112. static void
  113. error_tail (int status, int errnum, const char *message, va_list args)
  114. {
  115. #if _LIBC
  116. if (_IO_fwide (stderr, 0) > 0)
  117. {
  118. # define ALLOCA_LIMIT 2000
  119. size_t len = strlen (message) + 1;
  120. const wchar_t *wmessage = L"out of memory";
  121. wchar_t *wbuf = (len < ALLOCA_LIMIT
  122. ? alloca (len * sizeof *wbuf)
  123. : len <= SIZE_MAX / sizeof *wbuf
  124. ? malloc (len * sizeof *wbuf)
  125. : NULL);
  126. if (wbuf)
  127. {
  128. size_t res;
  129. mbstate_t st;
  130. const char *tmp = message;
  131. memset (&st, '\0', sizeof (st));
  132. res = mbsrtowcs (wbuf, &tmp, len, &st);
  133. wmessage = res == (size_t) -1 ? L"???" : wbuf;
  134. }
  135. __vfwprintf (stderr, wmessage, args);
  136. if (! (len < ALLOCA_LIMIT))
  137. free (wbuf);
  138. }
  139. else
  140. #endif
  141. vfprintf (stderr, message, args);
  142. va_end (args);
  143. ++error_message_count;
  144. if (errnum)
  145. print_errno_message (errnum);
  146. #if _LIBC
  147. if (_IO_fwide (stderr, 0) > 0)
  148. putwc (L'\n', stderr);
  149. else
  150. #endif
  151. putc ('\n', stderr);
  152. fflush (stderr);
  153. if (status)
  154. exit (status);
  155. }
  156. /* Print the program name and error message MESSAGE, which is a printf-style
  157. format string with optional args.
  158. If ERRNUM is nonzero, print its corresponding system error message.
  159. Exit with status STATUS if it is nonzero. */
  160. void
  161. error (int status, int errnum, const char *message, ...)
  162. {
  163. va_list args;
  164. #if defined _LIBC && defined __libc_ptf_call
  165. /* We do not want this call to be cut short by a thread
  166. cancellation. Therefore disable cancellation for now. */
  167. int state = PTHREAD_CANCEL_ENABLE;
  168. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  169. 0);
  170. #endif
  171. fflush (stdout);
  172. #ifdef _LIBC
  173. _IO_flockfile (stderr);
  174. #endif
  175. if (error_print_progname)
  176. (*error_print_progname) ();
  177. else
  178. {
  179. #if _LIBC
  180. if (_IO_fwide (stderr, 0) > 0)
  181. __fwprintf (stderr, L"%s: ", program_name);
  182. else
  183. #endif
  184. fprintf (stderr, "%s: ", program_name);
  185. }
  186. va_start (args, message);
  187. error_tail (status, errnum, message, args);
  188. #ifdef _LIBC
  189. _IO_funlockfile (stderr);
  190. # ifdef __libc_ptf_call
  191. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  192. # endif
  193. #endif
  194. }
  195. /* Sometimes we want to have at most one error per line. This
  196. variable controls whether this mode is selected or not. */
  197. int error_one_per_line;
  198. void
  199. error_at_line (int status, int errnum, const char *file_name,
  200. unsigned int line_number, const char *message, ...)
  201. {
  202. va_list args;
  203. if (error_one_per_line)
  204. {
  205. static const char *old_file_name;
  206. static unsigned int old_line_number;
  207. if (old_line_number == line_number
  208. && (file_name == old_file_name
  209. || strcmp (old_file_name, file_name) == 0))
  210. /* Simply return and print nothing. */
  211. return;
  212. old_file_name = file_name;
  213. old_line_number = line_number;
  214. }
  215. #if defined _LIBC && defined __libc_ptf_call
  216. /* We do not want this call to be cut short by a thread
  217. cancellation. Therefore disable cancellation for now. */
  218. int state = PTHREAD_CANCEL_ENABLE;
  219. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  220. 0);
  221. #endif
  222. fflush (stdout);
  223. #ifdef _LIBC
  224. _IO_flockfile (stderr);
  225. #endif
  226. if (error_print_progname)
  227. (*error_print_progname) ();
  228. else
  229. {
  230. #if _LIBC
  231. if (_IO_fwide (stderr, 0) > 0)
  232. __fwprintf (stderr, L"%s: ", program_name);
  233. else
  234. #endif
  235. fprintf (stderr, "%s:", program_name);
  236. }
  237. if (file_name != NULL)
  238. {
  239. #if _LIBC
  240. if (_IO_fwide (stderr, 0) > 0)
  241. __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
  242. else
  243. #endif
  244. fprintf (stderr, "%s:%d: ", file_name, line_number);
  245. }
  246. va_start (args, message);
  247. error_tail (status, errnum, message, args);
  248. #ifdef _LIBC
  249. _IO_funlockfile (stderr);
  250. # ifdef __libc_ptf_call
  251. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  252. # endif
  253. #endif
  254. }
  255. #ifdef _LIBC
  256. /* Make the weak alias. */
  257. # undef error
  258. # undef error_at_line
  259. weak_alias (__error, error)
  260. weak_alias (__error_at_line, error_at_line)
  261. #endif