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