/* 
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/
#ident	"$Header: diag.c 1.3 90/01/11 $@(#)diag.c	1.1 89/12/06"

/* Preprocessor Statements */
#include "colltbl.h"
#include <varargs.h>
#define SYNERR		0
#define SYNWARN		1
#define CMDERR		2
#define CMDWARN		3
#define ERRINDEX	(index > maxdiags) ? maxdiags + 1 : index

/* External Variables */
extern int Status, Lineno;
extern char *Cmd, *Infile;

void
error(va_alist) va_dcl
{
	register int index;
	va_list ap;
	static char *diag_front[] = {
		" \"%s\", line %d: error: ",
		" \"%s\", line %d: warning: ",
		" error: ",
		" warning: "
	};
	static struct {
		int type;
		int status;
		char *msg;
	} diagnostics[] = {
		SYNERR, _ERROR, "error in %s\n",
		SYNERR, _ERROR, "duplicate %s name: %s\n",
		SYNERR, _ERROR, "%s expected\n",
		SYNERR, _ERROR, "illegal %s: %s\n",
		SYNWARN, WARNING, "%s too long: %s\n",
		SYNWARN, WARNING, "inserted %s\n",
		SYNERR, _ERROR, "%s not found: %s\n",
		SYNERR, _ERROR, "no %s defined\n",
		SYNERR, ABORT, "too many %s\n",
		SYNERR, _ERROR, "invalid %s: %s\n", 
		CMDERR, _ERROR, "can't open %s\n", 
		CMDERR, ABORT, "%s out of space!\n", 
		SYNERR, _ERROR, "found newline in %s\n",
		SYNERR, _ERROR, "regular expression error: %s\n",
		CMDWARN, WARNING, "no diagnostic message available\n"
	};
	static int maxdiags = (sizeof(diagnostics) / sizeof(diagnostics[0])) - 1;

	va_start(ap);
	index = (int) va_arg(ap, int *);
	fprintf(stderr,"%s:",Cmd);
	fprintf(stderr,diag_front[diagnostics[ERRINDEX].type],Infile,Lineno);
	vfprintf(stderr,diagnostics[ERRINDEX].msg,ap);
	va_end(ap);
	if(diagnostics[ERRINDEX].status == ABORT)
		exit(ABORT);
	Status = diagnostics[ERRINDEX].status;
}


void
usage()
{
	fprintf(stderr,"\007%s format:  %s  [ options ] [ file ]\n\
	options available:	-d  (for debugging)\n\
				-r  (use regular expressions in substitutions)\n\
				-?  (to print this synopsis)\n", Cmd, Cmd);
	exit(Status);
}


void
regerr(i)
int	i;
{
	switch(i) {
	case 11:
		error(REGERR, "range endpoint too large.");
		break;
	case 16:
		error(REGERR, "bad number.");
		break;
	case 25:
		error(REGERR, "\digit out of range.");
		break;
	case 36:
		error(REGERR, "illegal or missing delimiter.");
		break;
	case 41:
		error(REGERR, "no remembered search string.");
		break;
	case 42:
		error(REGERR, "\( \) imbalance.");
		break;
	case 43:
		error(REGERR, "too many \(.");
		break;
	case 44:
		error(REGERR, "more than 2 numbers given in \{ \}.");
		break;
	case 45:
		error(REGERR, "} expected after \\.");
		break;
	case 46:
		error(REGERR, "first number exceeds second in \{ \}.");
		break;
	case 49:
		error(REGERR, "[ ] imbalance.");
		break;
	case 50:
		error(REGERR, "regular expression overflow.");
		break;
	case 51:
		error(REGERR, "<< >> imbalance.");
		break;
	case 52:
		error(REGERR, "unknown macro.");
		break;
	}
}

Error(fmt, string)
char	*fmt;
char	*string;
{
	fprintf(stderr, fmt, string);
}
