Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1987, 1988 by MIT Student Information Processing Board. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 3 | * |
Theodore Ts'o | 06cefee | 1999-10-23 01:16:22 +0000 | [diff] [blame] | 4 | * Permission to use, copy, modify, and distribute this software and |
| 5 | * its documentation for any purpose is hereby granted, provided that |
| 6 | * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in |
| 7 | * advertising or publicity pertaining to distribution of the software |
| 8 | * without specific, written prior permission. M.I.T. and the |
| 9 | * M.I.T. S.I.P.B. make no representations about the suitability of |
| 10 | * this software for any purpose. It is provided "as is" without |
| 11 | * express or implied warranty. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 14 | #include "config.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
Theodore Ts'o | 822c10e | 2010-05-15 07:48:25 -0400 | [diff] [blame] | 16 | #ifdef HAVE_TERMIOS_H |
| 17 | #include <termios.h> |
| 18 | #endif |
| 19 | #ifdef HAVE_UNISTD_H |
| 20 | #include <unistd.h> |
| 21 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 22 | #include "com_err.h" |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 23 | #include "error_table.h" |
| 24 | #include "internal.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 26 | static void |
Theodore Ts'o | 91835c1 | 2003-03-30 22:26:13 -0500 | [diff] [blame] | 27 | default_com_err_proc (const char *whoami, errcode_t code, const |
| 28 | char *fmt, va_list args) |
Theodore Ts'o | 2540bb7 | 2003-11-26 23:19:28 -0500 | [diff] [blame] | 29 | COM_ERR_ATTR((format(printf, 3, 0))); |
| 30 | |
| 31 | static void |
| 32 | default_com_err_proc (const char *whoami, errcode_t code, const |
| 33 | char *fmt, va_list args) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 34 | { |
Theodore Ts'o | 822c10e | 2010-05-15 07:48:25 -0400 | [diff] [blame] | 35 | int do_cr = 1, fd = fileno(stderr); |
| 36 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 37 | if (whoami) { |
| 38 | fputs(whoami, stderr); |
| 39 | fputs(": ", stderr); |
| 40 | } |
| 41 | if (code) { |
| 42 | fputs(error_message(code), stderr); |
| 43 | fputs(" ", stderr); |
| 44 | } |
| 45 | if (fmt) { |
| 46 | vfprintf (stderr, fmt, args); |
| 47 | } |
Theodore Ts'o | 822c10e | 2010-05-15 07:48:25 -0400 | [diff] [blame] | 48 | if (!isatty(fd)) |
| 49 | do_cr = 0; |
| 50 | #ifdef HAVE_TERMIOS_H |
| 51 | else { |
| 52 | struct termios t; |
| 53 | |
| 54 | if ((tcgetattr(fd, &t)) == 0 && |
| 55 | (t.c_oflag & OPOST) && (t.c_oflag & ONLCR)) |
| 56 | do_cr = 0; |
| 57 | } |
| 58 | #endif |
| 59 | if (do_cr) |
| 60 | fputc('\r', stderr); |
| 61 | fputc('\n', stderr); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 62 | fflush(stderr); |
| 63 | } |
| 64 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 65 | typedef void (*errf) (const char *, errcode_t, const char *, va_list); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 66 | |
| 67 | errf com_err_hook = default_com_err_proc; |
| 68 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 69 | void com_err_va (const char *whoami, errcode_t code, const char *fmt, |
| 70 | va_list args) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 71 | { |
| 72 | (*com_err_hook) (whoami, code, fmt, args); |
| 73 | } |
| 74 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 75 | void com_err (const char *whoami, |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 76 | errcode_t code, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 77 | const char *fmt, ...) |
| 78 | { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 79 | va_list pvar; |
| 80 | |
| 81 | if (!com_err_hook) |
| 82 | com_err_hook = default_com_err_proc; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 83 | va_start(pvar, fmt); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 84 | com_err_va (whoami, code, fmt, pvar); |
| 85 | va_end(pvar); |
| 86 | } |
| 87 | |
| 88 | errf set_com_err_hook (new_proc) |
| 89 | errf new_proc; |
| 90 | { |
| 91 | errf x = com_err_hook; |
| 92 | |
| 93 | if (new_proc) |
| 94 | com_err_hook = new_proc; |
| 95 | else |
| 96 | com_err_hook = default_com_err_proc; |
| 97 | |
| 98 | return x; |
| 99 | } |
| 100 | |
| 101 | errf reset_com_err_hook () { |
| 102 | errf x = com_err_hook; |
| 103 | com_err_hook = default_com_err_proc; |
| 104 | return x; |
| 105 | } |