PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/libiconv/srclib/error.c

http://github.com/elan/plex
C | 338 lines | 248 code | 50 blank | 40 comment | 39 complexity | 50eef2b8a8beacfb5fef8c6b693dd020 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-3.0, CC-BY-SA-3.0, 0BSD, LGPL-2.1, GPL-3.0, BSD-3-Clause, CC0-1.0, Unlicense, GPL-2.0, AGPL-1.0
  1. /* Error handler for noninteractive utilities
  2. Copyright (C) 1990-1998, 2000-2007 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 3 of the License, or
  7. (at your option) 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
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  15. #if !_LIBC
  16. # include <config.h>
  17. #endif
  18. #include "error.h"
  19. #include <stdarg.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #if !_LIBC && ENABLE_NLS
  24. # include "gettext.h"
  25. # define _(msgid) gettext (msgid)
  26. #endif
  27. #ifdef _LIBC
  28. # include <libintl.h>
  29. # include <stdbool.h>
  30. # include <stdint.h>
  31. # include <wchar.h>
  32. # define mbsrtowcs __mbsrtowcs
  33. #endif
  34. #if USE_UNLOCKED_IO
  35. # include "unlocked-io.h"
  36. #endif
  37. #ifndef _
  38. # define _(String) String
  39. #endif
  40. /* If NULL, error will flush stdout, then print on stderr the program
  41. name, a colon and a space. Otherwise, error will call this
  42. function without parameters instead. */
  43. void (*error_print_progname) (void);
  44. /* This variable is incremented each time `error' is called. */
  45. unsigned int error_message_count;
  46. #ifdef _LIBC
  47. /* In the GNU C library, there is a predefined variable for this. */
  48. # define program_name program_invocation_name
  49. # include <errno.h>
  50. # include <limits.h>
  51. # include <libio/libioP.h>
  52. /* In GNU libc we want do not want to use the common name `error' directly.
  53. Instead make it a weak alias. */
  54. extern void __error (int status, int errnum, const char *message, ...)
  55. __attribute__ ((__format__ (__printf__, 3, 4)));
  56. extern void __error_at_line (int status, int errnum, const char *file_name,
  57. unsigned int line_number, const char *message,
  58. ...)
  59. __attribute__ ((__format__ (__printf__, 5, 6)));;
  60. # define error __error
  61. # define error_at_line __error_at_line
  62. # include <libio/iolibio.h>
  63. # define fflush(s) INTUSE(_IO_fflush) (s)
  64. # undef putc
  65. # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
  66. # include <bits/libc-lock.h>
  67. #else /* not _LIBC */
  68. # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
  69. # ifndef HAVE_DECL_STRERROR_R
  70. "this configure-time declaration test was not run"
  71. # endif
  72. char *strerror_r ();
  73. # endif
  74. /* The calling program should define program_name and set it to the
  75. name of the executing program. */
  76. extern char *program_name;
  77. # if HAVE_STRERROR_R || defined strerror_r
  78. # define __strerror_r strerror_r
  79. # endif /* HAVE_STRERROR_R || defined strerror_r */
  80. #endif /* not _LIBC */
  81. static void
  82. print_errno_message (int errnum)
  83. {
  84. char const *s;
  85. #if defined HAVE_STRERROR_R || _LIBC
  86. char errbuf[1024];
  87. # if STRERROR_R_CHAR_P || _LIBC
  88. s = __strerror_r (errnum, errbuf, sizeof errbuf);
  89. # else
  90. if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
  91. s = errbuf;
  92. else
  93. s = 0;
  94. # endif
  95. #else
  96. s = strerror (errnum);
  97. #endif
  98. #if !_LIBC
  99. if (! s)
  100. s = _("Unknown system error");
  101. #endif
  102. #if _LIBC
  103. __fxprintf (NULL, ": %s", s);
  104. #else
  105. fprintf (stderr, ": %s", s);
  106. #endif
  107. }
  108. static void
  109. error_tail (int status, int errnum, const char *message, va_list args)
  110. {
  111. #if _LIBC
  112. if (_IO_fwide (stderr, 0) > 0)
  113. {
  114. # define ALLOCA_LIMIT 2000
  115. size_t len = strlen (message) + 1;
  116. wchar_t *wmessage = NULL;
  117. mbstate_t st;
  118. size_t res;
  119. const char *tmp;
  120. bool use_malloc = false;
  121. while (1)
  122. {
  123. if (__libc_use_alloca (len * sizeof (wchar_t)))
  124. wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
  125. else
  126. {
  127. if (!use_malloc)
  128. wmessage = NULL;
  129. wchar_t *p = (wchar_t *) realloc (wmessage,
  130. len * sizeof (wchar_t));
  131. if (p == NULL)
  132. {
  133. free (wmessage);
  134. fputws_unlocked (L"out of memory\n", stderr);
  135. return;
  136. }
  137. wmessage = p;
  138. use_malloc = true;
  139. }
  140. memset (&st, '\0', sizeof (st));
  141. tmp = message;
  142. res = mbsrtowcs (wmessage, &tmp, len, &st);
  143. if (res != len)
  144. break;
  145. if (__builtin_expect (len >= SIZE_MAX / 2, 0))
  146. {
  147. /* This really should not happen if everything is fine. */
  148. res = (size_t) -1;
  149. break;
  150. }
  151. len *= 2;
  152. }
  153. if (res == (size_t) -1)
  154. {
  155. /* The string cannot be converted. */
  156. if (use_malloc)
  157. {
  158. free (wmessage);
  159. use_malloc = false;
  160. }
  161. wmessage = (wchar_t *) L"???";
  162. }
  163. __vfwprintf (stderr, wmessage, args);
  164. if (use_malloc)
  165. free (wmessage);
  166. }
  167. else
  168. #endif
  169. vfprintf (stderr, message, args);
  170. va_end (args);
  171. ++error_message_count;
  172. if (errnum)
  173. print_errno_message (errnum);
  174. #if _LIBC
  175. __fxprintf (NULL, "\n");
  176. #else
  177. putc ('\n', stderr);
  178. #endif
  179. fflush (stderr);
  180. if (status)
  181. exit (status);
  182. }
  183. /* Print the program name and error message MESSAGE, which is a printf-style
  184. format string with optional args.
  185. If ERRNUM is nonzero, print its corresponding system error message.
  186. Exit with status STATUS if it is nonzero. */
  187. void
  188. error (int status, int errnum, const char *message, ...)
  189. {
  190. va_list args;
  191. #if defined _LIBC && defined __libc_ptf_call
  192. /* We do not want this call to be cut short by a thread
  193. cancellation. Therefore disable cancellation for now. */
  194. int state = PTHREAD_CANCEL_ENABLE;
  195. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  196. 0);
  197. #endif
  198. fflush (stdout);
  199. #ifdef _LIBC
  200. _IO_flockfile (stderr);
  201. #endif
  202. if (error_print_progname)
  203. (*error_print_progname) ();
  204. else
  205. {
  206. #if _LIBC
  207. __fxprintf (NULL, "%s: ", program_name);
  208. #else
  209. fprintf (stderr, "%s: ", program_name);
  210. #endif
  211. }
  212. va_start (args, message);
  213. error_tail (status, errnum, message, args);
  214. #ifdef _LIBC
  215. _IO_funlockfile (stderr);
  216. # ifdef __libc_ptf_call
  217. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  218. # endif
  219. #endif
  220. }
  221. /* Sometimes we want to have at most one error per line. This
  222. variable controls whether this mode is selected or not. */
  223. int error_one_per_line;
  224. void
  225. error_at_line (int status, int errnum, const char *file_name,
  226. unsigned int line_number, const char *message, ...)
  227. {
  228. va_list args;
  229. if (error_one_per_line)
  230. {
  231. static const char *old_file_name;
  232. static unsigned int old_line_number;
  233. if (old_line_number == line_number
  234. && (file_name == old_file_name
  235. || strcmp (old_file_name, file_name) == 0))
  236. /* Simply return and print nothing. */
  237. return;
  238. old_file_name = file_name;
  239. old_line_number = line_number;
  240. }
  241. #if defined _LIBC && defined __libc_ptf_call
  242. /* We do not want this call to be cut short by a thread
  243. cancellation. Therefore disable cancellation for now. */
  244. int state = PTHREAD_CANCEL_ENABLE;
  245. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  246. 0);
  247. #endif
  248. fflush (stdout);
  249. #ifdef _LIBC
  250. _IO_flockfile (stderr);
  251. #endif
  252. if (error_print_progname)
  253. (*error_print_progname) ();
  254. else
  255. {
  256. #if _LIBC
  257. __fxprintf (NULL, "%s:", program_name);
  258. #else
  259. fprintf (stderr, "%s:", program_name);
  260. #endif
  261. }
  262. #if _LIBC
  263. __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
  264. file_name, line_number);
  265. #else
  266. fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
  267. file_name, line_number);
  268. #endif
  269. va_start (args, message);
  270. error_tail (status, errnum, message, args);
  271. #ifdef _LIBC
  272. _IO_funlockfile (stderr);
  273. # ifdef __libc_ptf_call
  274. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  275. # endif
  276. #endif
  277. }
  278. #ifdef _LIBC
  279. /* Make the weak alias. */
  280. # undef error
  281. # undef error_at_line
  282. weak_alias (__error, error)
  283. weak_alias (__error_at_line, error_at_line)
  284. #endif