/*
 *
 *	Copyright (C) 1988, 1989 by the Massachusetts Institute of Technology
 *    	Developed by the MIT Student Information Processing Board (SIPB).
 *    	For copying information, see the file mit-copyright.h in this release.
 *
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdio.h>

#if STDC_HEADERS
#include <stdarg.h>
#define VA_START(args, lastarg) va_start(args, lastarg)
#else /* STDC_HEADERS */
#include <varargs.h>
#define VA_START(args, lastarg) va_start(args)
#endif /* STDC_HEADERS */

/* We force the use of _doprnt on Suns because of a bug in the vfprintf
   in the ucb compatibility library in Solaris (and maybe other machines).
   People may be using this for the Kerberos & Zephyr libraries */
#if !defined(HAVE_VPRINTF) || defined(sparc)
#undef vfprintf
#define vfprintf(stream,fmt,args) _doprnt(fmt,args,stream)
#endif /* HAVE_VPRINTF */

#include "error_table.h"
#include "internal.h"

/*
 * Protect us from header version (externally visible) of com_err, so
 * we can survive in a <varargs.h> environment.  I think.
 */
#define com_err com_err_external
#include "com_err.h"
#undef com_err

extern char const *error_message();

static void
#if defined(HAVE_VPRINTF) && __STDC__
    default_com_err_proc (const char *whoami, long code, const char *fmt, va_list args)
#else /* !HAVE_VPRINTF or !__STDC__ */
    default_com_err_proc (whoami, code, fmt, args)
    const char *whoami;
    long code;
    const char *fmt;
    va_list args;
#endif
{
    if (whoami) {
	fputs(whoami, stderr);
	fputs(": ", stderr);
    }
    if (code) {
	fputs(error_message(code), stderr);
	fputs(" ", stderr);
    }
    if (fmt) {
        vfprintf (stderr, fmt, args);
    }
    putc('\n', stderr);
    /* should do this only on a tty in raw mode */
    putc('\r', stderr);
    fflush(stderr);
}

#ifdef __STDC__
typedef void (*errf) (const char *, long, const char *, va_list);
#else
typedef void (*errf) ();
#endif

errf com_err_hook = default_com_err_proc;

void com_err_va (whoami, code, fmt, args)
    const char *whoami;
    long code;
    const char *fmt;
    va_list args;
{
  if (! com_err_hook)
        com_err_hook = default_com_err_proc;
    (*com_err_hook) (whoami, code, fmt, args);
}

#if defined(__STDC__) || defined(STDC_HEADERS)
void com_err (const char *whoami,
	      long code,
	      const char *fmt, ...)
{
#else
void com_err (va_alist)
va_dcl
{
     char *whoami;
     long code;
     char *fmt;
#endif
     va_list pvar;
     
     if (!com_err_hook)
	  com_err_hook = default_com_err_proc;
     
     VA_START(pvar, fmt);
#ifndef __STDC__
     whoami = va_arg(pvar, char *);
     code = va_arg(pvar, long);
     fmt = va_arg(pvar, char *);
#endif
     com_err_va (whoami, code, fmt, pvar);
     va_end(pvar);
}

errf set_com_err_hook (new_proc)
    errf new_proc;
{
    errf x = com_err_hook;

    if (new_proc)
	com_err_hook = new_proc;
    else
	com_err_hook = default_com_err_proc;

    return x;
}

errf reset_com_err_hook () {
    errf x = com_err_hook;
    com_err_hook = default_com_err_proc;
    return x;
}
