10e20ee16Smrg  /*\
22f167a42Smrg   *                          COPYRIGHT 1990
32f167a42Smrg   *                    DIGITAL EQUIPMENT CORPORATION
42f167a42Smrg   *                       MAYNARD, MASSACHUSETTS
52f167a42Smrg   *                        ALL RIGHTS RESERVED.
60e20ee16Smrg   *
70e20ee16Smrg   * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
80e20ee16Smrg   * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
92f167a42Smrg   * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE
102f167a42Smrg   * FOR ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED
110e20ee16Smrg   * WARRANTY.
120e20ee16Smrg   *
130e20ee16Smrg   * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
140e20ee16Smrg   * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
150e20ee16Smrg   * ADDITION TO THAT SET FORTH ABOVE.
160e20ee16Smrg   *
170e20ee16Smrg   * Permission to use, copy, modify, and distribute this software and its
180e20ee16Smrg   * documentation for any purpose and without fee is hereby granted, provided
190e20ee16Smrg   * that the above copyright notice appear in all copies and that both that
200e20ee16Smrg   * copyright notice and this permission notice appear in supporting
210e20ee16Smrg   * documentation, and that the name of Digital Equipment Corporation not be
222f167a42Smrg   * used in advertising or publicity pertaining to distribution of the
230e20ee16Smrg   * software without specific, written prior permission.
240e20ee16Smrg  \*/
252f167a42Smrg
262f167a42Smrg#ifdef HAVE_CONFIG_H
272f167a42Smrg#include "config.h"
282f167a42Smrg#endif
290e20ee16Smrg
300e20ee16Smrg#include 	"utils.h"
310e20ee16Smrg#include	<ctype.h>
320e20ee16Smrg#include	<stdlib.h>
330e20ee16Smrg
343e6697d4Smrgunsigned int debugFlags;
353e6697d4Smrg
363e6697d4Smrg
370e20ee16Smrg/***====================================================================***/
380e20ee16Smrg
392f167a42Smrgstatic FILE *errorFile = NULL;
400e20ee16Smrg
410e20ee16SmrgBoolean
422f167a42SmrguSetErrorFile(const char *name)
430e20ee16Smrg{
442f167a42Smrg    if ((errorFile != NULL) && (errorFile != stderr)) {
452f167a42Smrg        fprintf(errorFile, "switching to %s\n", name ? name : "stderr");
462f167a42Smrg        fclose(errorFile);
470e20ee16Smrg    }
482f167a42Smrg    if (name != NullString)
492f167a42Smrg        errorFile = fopen(name, "w");
502f167a42Smrg    else
512f167a42Smrg        errorFile = stderr;
522f167a42Smrg    if (errorFile == NULL) {
532f167a42Smrg        errorFile = stderr;
542f167a42Smrg        return (False);
550e20ee16Smrg    }
562f167a42Smrg    return (True);
570e20ee16Smrg}
580e20ee16Smrg
590e20ee16Smrgvoid
602f167a42SmrguInformation(const char *s, ...)
610e20ee16Smrg{
620e20ee16Smrg    va_list ap;
630e20ee16Smrg
640e20ee16Smrg    va_start(ap, s);
652f167a42Smrg    vfprintf(errorFile, s, ap);
660e20ee16Smrg    fflush(errorFile);
670e20ee16Smrg    va_end(ap);
680e20ee16Smrg    return;
690e20ee16Smrg}
700e20ee16Smrg
710e20ee16Smrg/***====================================================================***/
720e20ee16Smrg
730e20ee16Smrgvoid
742f167a42SmrguAction(const char *s, ...)
750e20ee16Smrg{
760e20ee16Smrg    va_list ap;
770e20ee16Smrg
780e20ee16Smrg    va_start(ap, s);
792f167a42Smrg    fprintf(errorFile, "                  ");
802f167a42Smrg    vfprintf(errorFile, s, ap);
810e20ee16Smrg    fflush(errorFile);
820e20ee16Smrg    va_end(ap);
830e20ee16Smrg    return;
840e20ee16Smrg}
850e20ee16Smrg
860e20ee16Smrg/***====================================================================***/
870e20ee16Smrg
880e20ee16Smrgvoid
892f167a42SmrguWarning(const char *s, ...)
900e20ee16Smrg{
910e20ee16Smrg    va_list ap;
920e20ee16Smrg
930e20ee16Smrg    va_start(ap, s);
942f167a42Smrg    fprintf(errorFile, "Warning:          ");
952f167a42Smrg    vfprintf(errorFile, s, ap);
960e20ee16Smrg    fflush(errorFile);
970e20ee16Smrg    va_end(ap);
980e20ee16Smrg    return;
990e20ee16Smrg}
1000e20ee16Smrg
1010e20ee16Smrg/***====================================================================***/
1020e20ee16Smrg
1030e20ee16Smrgvoid
1042f167a42SmrguError(const char *s, ...)
1050e20ee16Smrg{
1060e20ee16Smrg    va_list ap;
1070e20ee16Smrg
1080e20ee16Smrg    va_start(ap, s);
1092f167a42Smrg    fprintf(errorFile, "Error:            ");
1102f167a42Smrg    vfprintf(errorFile, s, ap);
1110e20ee16Smrg    fflush(errorFile);
1120e20ee16Smrg    va_end(ap);
1130e20ee16Smrg    return;
1140e20ee16Smrg}
1150e20ee16Smrg
1160e20ee16Smrg/***====================================================================***/
1170e20ee16Smrg
1180e20ee16Smrgvoid
1192f167a42SmrguFatalError(const char *s, ...)
1200e20ee16Smrg{
1210e20ee16Smrg    va_list ap;
1220e20ee16Smrg
1230e20ee16Smrg    va_start(ap, s);
1242f167a42Smrg    fprintf(errorFile, "Fatal Error:      ");
1252f167a42Smrg    vfprintf(errorFile, s, ap);
1262f167a42Smrg    fprintf(errorFile, "                  Exiting\n");
1270e20ee16Smrg    fflush(errorFile);
1280e20ee16Smrg    va_end(ap);
1290e20ee16Smrg    exit(1);
1300e20ee16Smrg    /* NOTREACHED */
1310e20ee16Smrg}
1320e20ee16Smrg
1330e20ee16Smrg/***====================================================================***/
1340e20ee16Smrg
1350e20ee16Smrgvoid
1362f167a42SmrguInternalError(const char *s, ...)
1370e20ee16Smrg{
1380e20ee16Smrg    va_list ap;
1390e20ee16Smrg
1400e20ee16Smrg    va_start(ap, s);
1412f167a42Smrg    fprintf(errorFile, "Internal error:   ");
1422f167a42Smrg    vfprintf(errorFile, s, ap);
1430e20ee16Smrg    fflush(errorFile);
1440e20ee16Smrg    va_end(ap);
1450e20ee16Smrg    return;
1460e20ee16Smrg}
1470e20ee16Smrg
1480e20ee16Smrg/***====================================================================***/
1490e20ee16Smrg
1502f167a42Smrg#ifndef HAVE_ASPRINTF
1512f167a42Smrgint
1522f167a42SmrguAsprintf(char ** ret, const char *format, ...)
1530e20ee16Smrg{
1542f167a42Smrg    char buf[256];
1552f167a42Smrg    int len;
1562f167a42Smrg    va_list ap;
1570e20ee16Smrg
1582f167a42Smrg    va_start(ap, format);
1592f167a42Smrg    len = vsnprintf(buf, sizeof(buf), format, ap);
1602f167a42Smrg    va_end(ap);
1610e20ee16Smrg
1622f167a42Smrg    if (len < 0)
1632f167a42Smrg        return -1;
1640e20ee16Smrg
1652f167a42Smrg    if (len < sizeof(buf))
1662f167a42Smrg    {
1672f167a42Smrg        *ret = strdup(buf);
1680e20ee16Smrg    }
1692f167a42Smrg    else
1702f167a42Smrg    {
1712f167a42Smrg        *ret = malloc(len + 1); /* snprintf doesn't count trailing '\0' */
1722f167a42Smrg        if (*ret != NULL)
1732f167a42Smrg        {
1742f167a42Smrg            va_start(ap, format);
1752f167a42Smrg            len = vsnprintf(*ret, len + 1, format, ap);
1762f167a42Smrg            va_end(ap);
1772f167a42Smrg            if (len < 0) {
1782f167a42Smrg                free(*ret);
1792f167a42Smrg                *ret = NULL;
1802f167a42Smrg            }
1812f167a42Smrg        }
1820e20ee16Smrg    }
1830e20ee16Smrg
1842f167a42Smrg    if (*ret == NULL)
1852f167a42Smrg        return -1;
1860e20ee16Smrg
1872f167a42Smrg    return len;
1882f167a42Smrg}
1892f167a42Smrg#endif /* HAVE_ASPRINTF */
190