blob: 4ea52defd59eabe5d3cb585d4847b0ff8eea4715 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * Copyright 1987, 1988 by MIT Student Information Processing Board.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o06cefee1999-10-23 01:16:22 +00004 * 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'o3839e651997-04-26 13:21:57 +000012 */
13
Theodore Ts'od1154eb2011-09-18 17:34:37 -040014#include "config.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000015#include <stdio.h>
Theodore Ts'o822c10e2010-05-15 07:48:25 -040016#ifdef HAVE_TERMIOS_H
17#include <termios.h>
18#endif
19#ifdef HAVE_UNISTD_H
20#include <unistd.h>
21#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000022#include "com_err.h"
Theodore Ts'of3db3561997-04-26 13:34:30 +000023#include "error_table.h"
24#include "internal.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000025
Theodore Ts'o3839e651997-04-26 13:21:57 +000026static void
Theodore Ts'o91835c12003-03-30 22:26:13 -050027default_com_err_proc (const char *whoami, errcode_t code, const
28 char *fmt, va_list args)
Theodore Ts'o2540bb72003-11-26 23:19:28 -050029 COM_ERR_ATTR((format(printf, 3, 0)));
30
31static void
32default_com_err_proc (const char *whoami, errcode_t code, const
33 char *fmt, va_list args)
Theodore Ts'o3839e651997-04-26 13:21:57 +000034{
Theodore Ts'o822c10e2010-05-15 07:48:25 -040035 int do_cr = 1, fd = fileno(stderr);
36
Theodore Ts'o3839e651997-04-26 13:21:57 +000037 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'o822c10e2010-05-15 07:48:25 -040048 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'o3839e651997-04-26 13:21:57 +000062 fflush(stderr);
63}
64
Theodore Ts'of3db3561997-04-26 13:34:30 +000065typedef void (*errf) (const char *, errcode_t, const char *, va_list);
Theodore Ts'o3839e651997-04-26 13:21:57 +000066
67errf com_err_hook = default_com_err_proc;
68
Theodore Ts'of3db3561997-04-26 13:34:30 +000069void com_err_va (const char *whoami, errcode_t code, const char *fmt,
70 va_list args)
Theodore Ts'o3839e651997-04-26 13:21:57 +000071{
72 (*com_err_hook) (whoami, code, fmt, args);
73}
74
Theodore Ts'o3839e651997-04-26 13:21:57 +000075void com_err (const char *whoami,
Theodore Ts'of3db3561997-04-26 13:34:30 +000076 errcode_t code,
Theodore Ts'o3839e651997-04-26 13:21:57 +000077 const char *fmt, ...)
78{
Theodore Ts'o3839e651997-04-26 13:21:57 +000079 va_list pvar;
80
81 if (!com_err_hook)
82 com_err_hook = default_com_err_proc;
Theodore Ts'o3839e651997-04-26 13:21:57 +000083 va_start(pvar, fmt);
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 com_err_va (whoami, code, fmt, pvar);
85 va_end(pvar);
86}
87
88errf 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
101errf reset_com_err_hook () {
102 errf x = com_err_hook;
103 com_err_hook = default_com_err_proc;
104 return x;
105}