error.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* Error handler for noninteractive utilities
  2. Copyright (C) 1990-1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <stdio.h>
  19. #ifdef _LIBC
  20. # include <libintl.h>
  21. #else
  22. # include "gettext.h"
  23. #endif
  24. #ifdef _LIBC
  25. # include <wchar.h>
  26. # define mbsrtowcs __mbsrtowcs
  27. #endif
  28. #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
  29. # if __STDC__
  30. # include <stdarg.h>
  31. # define VA_START(args, lastarg) va_start(args, lastarg)
  32. # else
  33. # include <varargs.h>
  34. # define VA_START(args, lastarg) va_start(args)
  35. # endif
  36. #else
  37. # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
  38. # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
  39. #endif
  40. #if STDC_HEADERS || _LIBC
  41. # include <stdlib.h>
  42. # include <string.h>
  43. #else
  44. void exit ();
  45. #endif
  46. #include "error.h"
  47. #if !_LIBC
  48. # include "unlocked-io.h"
  49. #endif
  50. #ifndef _
  51. # define _(String) String
  52. #endif
  53. /* If NULL, error will flush stdout, then print on stderr the program
  54. name, a colon and a space. Otherwise, error will call this
  55. function without parameters instead. */
  56. void (*error_print_progname) (
  57. #if __STDC__ - 0
  58. void
  59. #endif
  60. );
  61. /* This variable is incremented each time `error' is called. */
  62. unsigned int error_message_count;
  63. #ifdef _LIBC
  64. /* In the GNU C library, there is a predefined variable for this. */
  65. # define program_name program_invocation_name
  66. # include <errno.h>
  67. # include <libio/libioP.h>
  68. /* In GNU libc we want do not want to use the common name `error' directly.
  69. Instead make it a weak alias. */
  70. extern void __error (int status, int errnum, const char *message, ...)
  71. __attribute__ ((__format__ (__printf__, 3, 4)));
  72. extern void __error_at_line (int status, int errnum, const char *file_name,
  73. unsigned int line_number, const char *message,
  74. ...)
  75. __attribute__ ((__format__ (__printf__, 5, 6)));;
  76. # define error __error
  77. # define error_at_line __error_at_line
  78. # include <libio/iolibio.h>
  79. # define fflush(s) INTUSE(_IO_fflush) (s)
  80. # undef putc
  81. # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
  82. #else /* not _LIBC */
  83. # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
  84. # ifndef HAVE_DECL_STRERROR_R
  85. "this configure-time declaration test was not run"
  86. # endif
  87. char *strerror_r ();
  88. # endif
  89. /* The calling program should define program_name and set it to the
  90. name of the executing program. */
  91. extern char *progname;
  92. # if HAVE_STRERROR_R || defined strerror_r
  93. # define __strerror_r strerror_r
  94. # else
  95. # if HAVE_STRERROR
  96. # ifndef HAVE_DECL_STRERROR
  97. "this configure-time declaration test was not run"
  98. # endif
  99. # if !HAVE_DECL_STRERROR
  100. char *strerror ();
  101. # endif
  102. # else
  103. static char *
  104. private_strerror (int errnum)
  105. {
  106. extern char *sys_errlist[];
  107. extern int sys_nerr;
  108. if (errnum > 0 && errnum <= sys_nerr)
  109. return _(sys_errlist[errnum]);
  110. return _("Unknown system error");
  111. }
  112. # define strerror private_strerror
  113. # endif /* HAVE_STRERROR */
  114. # endif /* HAVE_STRERROR_R || defined strerror_r */
  115. #endif /* not _LIBC */
  116. static void
  117. print_errno_message (int errnum)
  118. {
  119. char const *s;
  120. #if defined HAVE_STRERROR_R || _LIBC
  121. char errbuf[1024];
  122. # if STRERROR_R_CHAR_P || _LIBC
  123. s = __strerror_r (errnum, errbuf, sizeof errbuf);
  124. # else
  125. if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
  126. s = errbuf;
  127. else
  128. s = 0;
  129. # endif
  130. #else
  131. s = strerror (errnum);
  132. #endif
  133. #if !_LIBC
  134. if (! s)
  135. s = _("Unknown system error");
  136. #endif
  137. #if _LIBC
  138. if (_IO_fwide (stderr, 0) > 0)
  139. {
  140. __fwprintf (stderr, L": %s", s);
  141. return;
  142. }
  143. #endif
  144. fprintf (stderr, ": %s", s);
  145. }
  146. #ifdef VA_START
  147. static void
  148. error_tail (int status, int errnum, const char *message, va_list args)
  149. {
  150. # if HAVE_VPRINTF || _LIBC
  151. # if _LIBC
  152. if (_IO_fwide (stderr, 0) > 0)
  153. {
  154. # define ALLOCA_LIMIT 2000
  155. size_t len = strlen (message) + 1;
  156. wchar_t *wmessage = NULL;
  157. mbstate_t st;
  158. size_t res;
  159. const char *tmp;
  160. do
  161. {
  162. if (len < ALLOCA_LIMIT)
  163. wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
  164. else
  165. {
  166. if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
  167. wmessage = NULL;
  168. wmessage = (wchar_t *) realloc (wmessage,
  169. len * sizeof (wchar_t));
  170. if (wmessage == NULL)
  171. {
  172. fputws_unlocked (L"out of memory\n", stderr);
  173. return;
  174. }
  175. }
  176. memset (&st, '\0', sizeof (st));
  177. tmp =message;
  178. }
  179. while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
  180. if (res == (size_t) -1)
  181. /* The string cannot be converted. */
  182. wmessage = (wchar_t *) L"???";
  183. __vfwprintf (stderr, wmessage, args);
  184. }
  185. else
  186. # endif
  187. vfprintf (stderr, message, args);
  188. # else
  189. _doprnt (message, args, stderr);
  190. # endif
  191. va_end (args);
  192. ++error_message_count;
  193. if (errnum)
  194. print_errno_message (errnum);
  195. # if _LIBC
  196. if (_IO_fwide (stderr, 0) > 0)
  197. putwc (L'\n', stderr);
  198. else
  199. # endif
  200. putc ('\n', stderr);
  201. fflush (stderr);
  202. if (status)
  203. exit (status);
  204. }
  205. #endif
  206. /* Print the program name and error message MESSAGE, which is a printf-style
  207. format string with optional args.
  208. If ERRNUM is nonzero, print its corresponding system error message.
  209. Exit with status STATUS if it is nonzero. */
  210. /* VARARGS */
  211. void
  212. #if defined VA_START && __STDC__
  213. error (int status, int errnum, const char *message, ...)
  214. #else
  215. error (status, errnum, message, va_alist)
  216. int status;
  217. int errnum;
  218. char *message;
  219. va_dcl
  220. #endif
  221. {
  222. #ifdef VA_START
  223. va_list args;
  224. #endif
  225. fflush (stdout);
  226. #ifdef _LIBC
  227. _IO_flockfile (stderr);
  228. #endif
  229. if (error_print_progname)
  230. (*error_print_progname) ();
  231. else
  232. {
  233. #if _LIBC
  234. if (_IO_fwide (stderr, 0) > 0)
  235. __fwprintf (stderr, L"%s: ", progname);
  236. else
  237. #endif
  238. fprintf (stderr, "%s: ", progname);
  239. }
  240. #ifdef VA_START
  241. VA_START (args, message);
  242. error_tail (status, errnum, message, args);
  243. #else
  244. fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  245. ++error_message_count;
  246. if (errnum)
  247. print_errno_message (errnum);
  248. putc ('\n', stderr);
  249. fflush (stderr);
  250. if (status)
  251. exit (status);
  252. #endif
  253. #ifdef _LIBC
  254. _IO_funlockfile (stderr);
  255. #endif
  256. }
  257. /* Sometimes we want to have at most one error per line. This
  258. variable controls whether this mode is selected or not. */
  259. int error_one_per_line;
  260. void
  261. #if defined VA_START && __STDC__
  262. error_at_line (int status, int errnum, const char *file_name,
  263. unsigned int line_number, const char *message, ...)
  264. #else
  265. error_at_line (status, errnum, file_name, line_number, message, va_alist)
  266. int status;
  267. int errnum;
  268. const char *file_name;
  269. unsigned int line_number;
  270. char *message;
  271. va_dcl
  272. #endif
  273. {
  274. #ifdef VA_START
  275. va_list args;
  276. #endif
  277. if (error_one_per_line)
  278. {
  279. static const char *old_file_name;
  280. static unsigned int old_line_number;
  281. if (old_line_number == line_number
  282. && (file_name == old_file_name
  283. || strcmp (old_file_name, file_name) == 0))
  284. /* Simply return and print nothing. */
  285. return;
  286. old_file_name = file_name;
  287. old_line_number = line_number;
  288. }
  289. fflush (stdout);
  290. #ifdef _LIBC
  291. _IO_flockfile (stderr);
  292. #endif
  293. if (error_print_progname)
  294. (*error_print_progname) ();
  295. else
  296. {
  297. #if _LIBC
  298. if (_IO_fwide (stderr, 0) > 0)
  299. __fwprintf (stderr, L"%s: ", progname);
  300. else
  301. #endif
  302. fprintf (stderr, "%s:", progname);
  303. }
  304. if (file_name != NULL)
  305. {
  306. #if _LIBC
  307. if (_IO_fwide (stderr, 0) > 0)
  308. __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
  309. else
  310. #endif
  311. fprintf (stderr, "%s:%d: ", file_name, line_number);
  312. }
  313. #ifdef VA_START
  314. VA_START (args, message);
  315. error_tail (status, errnum, message, args);
  316. #else
  317. fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  318. ++error_message_count;
  319. if (errnum)
  320. print_errno_message (errnum);
  321. putc ('\n', stderr);
  322. fflush (stderr);
  323. if (status)
  324. exit (status);
  325. #endif
  326. #ifdef _LIBC
  327. _IO_funlockfile (stderr);
  328. #endif
  329. }
  330. #ifdef _LIBC
  331. /* Make the weak alias. */
  332. # undef error
  333. # undef error_at_line
  334. weak_alias (__error, error)
  335. weak_alias (__error_at_line, error_at_line)
  336. #endif