error.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* Error handler for noninteractive utilities
  2. Copyright (C) 1990-1998, 2000, 2001, 2002 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 <[email protected]>. */
  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. # ifdef USE_IN_LIBIO
  79. # include <libio/iolibio.h>
  80. # define fflush(s) INTUSE(_IO_fflush) (s)
  81. # undef putc
  82. # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
  83. # endif
  84. #else /* not _LIBC */
  85. # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
  86. # ifndef HAVE_DECL_STRERROR_R
  87. "this configure-time declaration test was not run"
  88. # endif
  89. char *strerror_r ();
  90. # endif
  91. /* The calling program should define program_name and set it to the
  92. name of the executing program. */
  93. extern char *program_name;
  94. # if HAVE_STRERROR_R || defined strerror_r
  95. # define __strerror_r strerror_r
  96. # else
  97. # if HAVE_STRERROR
  98. # ifndef HAVE_DECL_STRERROR
  99. "this configure-time declaration test was not run"
  100. # endif
  101. # if !HAVE_DECL_STRERROR
  102. char *strerror ();
  103. # endif
  104. # else
  105. static char *
  106. private_strerror (int errnum)
  107. {
  108. extern char *sys_errlist[];
  109. extern int sys_nerr;
  110. if (errnum > 0 && errnum <= sys_nerr)
  111. return _(sys_errlist[errnum]);
  112. return _("Unknown system error");
  113. }
  114. # define strerror private_strerror
  115. # endif /* HAVE_STRERROR */
  116. # endif /* HAVE_STRERROR_R || defined strerror_r */
  117. #endif /* not _LIBC */
  118. static void
  119. print_errno_message (int errnum)
  120. {
  121. char const *s;
  122. #if defined HAVE_STRERROR_R || _LIBC
  123. char errbuf[1024];
  124. # if STRERROR_R_CHAR_P || _LIBC
  125. s = __strerror_r (errnum, errbuf, sizeof errbuf);
  126. # else
  127. if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
  128. s = errbuf;
  129. else
  130. s = 0;
  131. # endif
  132. #else
  133. s = strerror (errnum);
  134. #endif
  135. #if !_LIBC
  136. if (! s)
  137. s = _("Unknown system error");
  138. #endif
  139. #if _LIBC && USE_IN_LIBIO
  140. if (_IO_fwide (stderr, 0) > 0)
  141. {
  142. __fwprintf (stderr, L": %s", s);
  143. return;
  144. }
  145. #endif
  146. fprintf (stderr, ": %s", s);
  147. }
  148. #ifdef VA_START
  149. static void
  150. error_tail (int status, int errnum, const char *message, va_list args)
  151. {
  152. # if HAVE_VPRINTF || _LIBC
  153. # if _LIBC && USE_IN_LIBIO
  154. if (_IO_fwide (stderr, 0) > 0)
  155. {
  156. # define ALLOCA_LIMIT 2000
  157. size_t len = strlen (message) + 1;
  158. wchar_t *wmessage = NULL;
  159. mbstate_t st;
  160. size_t res;
  161. const char *tmp;
  162. do
  163. {
  164. if (len < ALLOCA_LIMIT)
  165. wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
  166. else
  167. {
  168. if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
  169. wmessage = NULL;
  170. wmessage = (wchar_t *) realloc (wmessage,
  171. len * sizeof (wchar_t));
  172. if (wmessage == NULL)
  173. {
  174. fputws_unlocked (L"out of memory\n", stderr);
  175. return;
  176. }
  177. }
  178. memset (&st, '\0', sizeof (st));
  179. tmp =message;
  180. }
  181. while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
  182. if (res == (size_t) -1)
  183. /* The string cannot be converted. */
  184. wmessage = (wchar_t *) L"???";
  185. __vfwprintf (stderr, wmessage, args);
  186. }
  187. else
  188. # endif
  189. vfprintf (stderr, message, args);
  190. # else
  191. _doprnt (message, args, stderr);
  192. # endif
  193. va_end (args);
  194. ++error_message_count;
  195. if (errnum)
  196. print_errno_message (errnum);
  197. # if _LIBC && USE_IN_LIBIO
  198. if (_IO_fwide (stderr, 0) > 0)
  199. putwc (L'\n', stderr);
  200. else
  201. # endif
  202. putc ('\n', stderr);
  203. fflush (stderr);
  204. if (status)
  205. exit (status);
  206. }
  207. #endif
  208. /* Print the program name and error message MESSAGE, which is a printf-style
  209. format string with optional args.
  210. If ERRNUM is nonzero, print its corresponding system error message.
  211. Exit with status STATUS if it is nonzero. */
  212. /* VARARGS */
  213. void
  214. #if defined VA_START && __STDC__
  215. error (int status, int errnum, const char *message, ...)
  216. #else
  217. error (status, errnum, message, va_alist)
  218. int status;
  219. int errnum;
  220. char *message;
  221. va_dcl
  222. #endif
  223. {
  224. #ifdef VA_START
  225. va_list args;
  226. #endif
  227. fflush (stdout);
  228. #ifdef _LIBC
  229. # ifdef USE_IN_LIBIO
  230. _IO_flockfile (stderr);
  231. # else
  232. __flockfile (stderr);
  233. # endif
  234. #endif
  235. if (error_print_progname)
  236. (*error_print_progname) ();
  237. else
  238. {
  239. #if _LIBC && USE_IN_LIBIO
  240. if (_IO_fwide (stderr, 0) > 0)
  241. __fwprintf (stderr, L"%s: ", program_name);
  242. else
  243. #endif
  244. fprintf (stderr, "%s: ", program_name);
  245. }
  246. #ifdef VA_START
  247. VA_START (args, message);
  248. error_tail (status, errnum, message, args);
  249. #else
  250. fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  251. ++error_message_count;
  252. if (errnum)
  253. print_errno_message (errnum);
  254. putc ('\n', stderr);
  255. fflush (stderr);
  256. if (status)
  257. exit (status);
  258. #endif
  259. #ifdef _LIBC
  260. # ifdef USE_IN_LIBIO
  261. _IO_funlockfile (stderr);
  262. # else
  263. __funlockfile (stderr);
  264. # endif
  265. #endif
  266. }
  267. /* Sometimes we want to have at most one error per line. This
  268. variable controls whether this mode is selected or not. */
  269. int error_one_per_line;
  270. void
  271. #if defined VA_START && __STDC__
  272. error_at_line (int status, int errnum, const char *file_name,
  273. unsigned int line_number, const char *message, ...)
  274. #else
  275. error_at_line (status, errnum, file_name, line_number, message, va_alist)
  276. int status;
  277. int errnum;
  278. const char *file_name;
  279. unsigned int line_number;
  280. char *message;
  281. va_dcl
  282. #endif
  283. {
  284. #ifdef VA_START
  285. va_list args;
  286. #endif
  287. if (error_one_per_line)
  288. {
  289. static const char *old_file_name;
  290. static unsigned int old_line_number;
  291. if (old_line_number == line_number
  292. && (file_name == old_file_name
  293. || strcmp (old_file_name, file_name) == 0))
  294. /* Simply return and print nothing. */
  295. return;
  296. old_file_name = file_name;
  297. old_line_number = line_number;
  298. }
  299. fflush (stdout);
  300. #ifdef _LIBC
  301. # ifdef USE_IN_LIBIO
  302. _IO_flockfile (stderr);
  303. # else
  304. __flockfile (stderr);
  305. # endif
  306. #endif
  307. if (error_print_progname)
  308. (*error_print_progname) ();
  309. else
  310. {
  311. #if _LIBC && USE_IN_LIBIO
  312. if (_IO_fwide (stderr, 0) > 0)
  313. __fwprintf (stderr, L"%s: ", program_name);
  314. else
  315. #endif
  316. fprintf (stderr, "%s:", program_name);
  317. }
  318. if (file_name != NULL)
  319. {
  320. #if _LIBC && USE_IN_LIBIO
  321. if (_IO_fwide (stderr, 0) > 0)
  322. __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
  323. else
  324. #endif
  325. fprintf (stderr, "%s:%d: ", file_name, line_number);
  326. }
  327. #ifdef VA_START
  328. VA_START (args, message);
  329. error_tail (status, errnum, message, args);
  330. #else
  331. fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  332. ++error_message_count;
  333. if (errnum)
  334. print_errno_message (errnum);
  335. putc ('\n', stderr);
  336. fflush (stderr);
  337. if (status)
  338. exit (status);
  339. #endif
  340. #ifdef _LIBC
  341. # ifdef USE_IN_LIBIO
  342. _IO_funlockfile (stderr);
  343. # else
  344. __funlockfile (stderr);
  345. # endif
  346. #endif
  347. }
  348. #ifdef _LIBC
  349. /* Make the weak alias. */
  350. # undef error
  351. # undef error_at_line
  352. weak_alias (__error, error)
  353. weak_alias (__error_at_line, error_at_line)
  354. #endif