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