xref: /csrg-svn/usr.sbin/sendmail/src/err.c (revision 7674)
13311Seric # include "sendmail.h"
2295Seric 
3*7674Seric SCCSID(@(#)err.c	3.26		08/08/82);
4406Seric 
5295Seric /*
61514Seric **  SYSERR -- Print error message.
7295Seric **
8295Seric **	Prints an error message via printf to the diagnostic
9295Seric **	output.  If LOG is defined, it logs it also.
10295Seric **
11295Seric **	Parameters:
12295Seric **		f -- the format string
13295Seric **		a, b, c, d, e -- parameters
14295Seric **
15295Seric **	Returns:
164084Seric **		none
17295Seric **
18295Seric **	Side Effects:
191514Seric **		increments Errors.
201514Seric **		sets ExitStat.
21295Seric */
22295Seric 
234084Seric # ifdef lint
244084Seric int	sys_nerr;
254084Seric char	*sys_errlist[];
264084Seric # endif lint
277525Seric static char	MsgBuf[BUFSIZ*2];	/* text of most recent message */
284084Seric 
29295Seric /*VARARGS1*/
30295Seric syserr(fmt, a, b, c, d, e)
31295Seric 	char *fmt;
32295Seric {
334167Seric 	extern char Arpa_Syserr[];
34295Seric 
357525Seric 	/* format and output the error message */
36*7674Seric 	fmtmsg(MsgBuf, (char *) NULL, Arpa_Syserr, fmt, a, b, c, d, e);
377613Seric 	putmsg(MsgBuf);
384063Seric 
397525Seric 	/* mark the error as having occured */
401514Seric 	Errors++;
416048Seric 	FatalErrors = TRUE;
42295Seric 
43295Seric 	/* determine exit status if not already set */
44295Seric 	if (ExitStat == EX_OK)
45295Seric 	{
46295Seric 		if (errno == 0)
47295Seric 			ExitStat = EX_SOFTWARE;
48295Seric 		else
491598Seric 			ExitStat = EX_OSERR;
50295Seric 	}
51295Seric 
52295Seric # ifdef LOG
53*7674Seric 	if (LogLevel > 0)
54*7674Seric 		syslog(LOG_ERR, "%s: %s", MsgId, &MsgBuf[4]);
55295Seric # endif LOG
56295Seric 	errno = 0;
57295Seric }
58295Seric /*
59295Seric **  USRERR -- Signal user error.
60295Seric **
61295Seric **	This is much like syserr except it is for user errors.
62295Seric **
63295Seric **	Parameters:
64295Seric **		fmt, a, b, c, d -- printf strings
65295Seric **
66295Seric **	Returns:
674084Seric **		none
68295Seric **
69295Seric **	Side Effects:
701514Seric **		increments Errors.
71295Seric */
72295Seric 
73295Seric /*VARARGS1*/
74295Seric usrerr(fmt, a, b, c, d, e)
75295Seric 	char *fmt;
76295Seric {
77295Seric 	extern char SuprErrs;
784167Seric 	extern char Arpa_Usrerr[];
79295Seric 
80295Seric 	if (SuprErrs)
814084Seric 		return;
824063Seric 	Errors++;
836048Seric 	FatalErrors = TRUE;
84295Seric 
857613Seric 	fmtmsg(MsgBuf, CurEnv->e_to, Arpa_Usrerr, fmt, a, b, c, d, e);
867613Seric 	putmsg(MsgBuf);
874063Seric }
884063Seric /*
894063Seric **  MESSAGE -- print message (not necessarily an error)
904063Seric **
914063Seric **	Parameters:
924063Seric **		num -- the default ARPANET error number (in ascii)
934063Seric **		msg -- the message (printf fmt) -- if it begins
944063Seric **			with a digit, this number overrides num.
954063Seric **		a, b, c, d, e -- printf arguments
964063Seric **
974063Seric **	Returns:
984063Seric **		none
994063Seric **
1004063Seric **	Side Effects:
1014063Seric **		none.
1024063Seric */
1034063Seric 
1044084Seric /*VARARGS2*/
1054063Seric message(num, msg, a, b, c, d, e)
1064063Seric 	register char *num;
1074063Seric 	register char *msg;
1084063Seric {
1094711Seric 	errno = 0;
1107525Seric 	fmtmsg(MsgBuf, CurEnv->e_to, num, msg, a, b, c, d, e);
1117613Seric 	putmsg(MsgBuf);
1127613Seric }
1137613Seric /*
1147613Seric **  PUTMSG -- output error message to transcript and channel
1157613Seric **
1167613Seric **	Parameters:
1177613Seric **		msg -- message to output (in SMTP format).
1187613Seric **
1197613Seric **	Returns:
1207613Seric **		none.
1217613Seric **
1227613Seric **	Side Effects:
1237613Seric **		Outputs msg to the transcript.
1247613Seric **		If appropriate, outputs it to the channel.
1257613Seric **		Deletes SMTP reply code number as appropriate.
1267613Seric */
1274711Seric 
1287613Seric putmsg(msg)
1297613Seric 	char *msg;
1307613Seric {
1314711Seric 	/* output to transcript */
1327613Seric 	fprintf(Xscript, "%s\n", Smtp ? msg : &msg[4]);
1334711Seric 
1344711Seric 	/* output to channel if appropriate */
1357613Seric 	if (!HoldErrs && (Verbose || msg[0] != '0'))
1364063Seric 	{
1377275Seric 		(void) fflush(stdout);
1384711Seric 		if (ArpaMode)
1397613Seric 			fprintf(OutChannel, "%s\r\n", msg);
1404711Seric 		else
1417613Seric 			fprintf(OutChannel, "%s\n", &msg[4]);
1424711Seric 		(void) fflush(OutChannel);
1434063Seric 	}
1444711Seric }
1454711Seric /*
1464711Seric **  FMTMSG -- format a message into buffer.
1474711Seric **
1484711Seric **	Parameters:
1494711Seric **		eb -- error buffer to get result.
1504711Seric **		to -- the recipient tag for this message.
1514711Seric **		num -- arpanet error number.
1524711Seric **		fmt -- format of string.
1534711Seric **		a, b, c, d, e -- arguments.
1544711Seric **
1554711Seric **	Returns:
1564711Seric **		none.
1574711Seric **
1584711Seric **	Side Effects:
1594711Seric **		none.
1604711Seric */
1614063Seric 
1624834Seric /*VARARGS4*/
1634711Seric static
1644711Seric fmtmsg(eb, to, num, fmt, a, b, c, d, e)
1654711Seric 	register char *eb;
1664711Seric 	char *to;
1674711Seric 	char *num;
1684711Seric 	char *fmt;
1694711Seric {
1704711Seric 	char del;
1714711Seric 
1724711Seric 	/* output the reply code */
1734711Seric 	if (isdigit(*fmt))
1744577Seric 	{
1754711Seric 		num = fmt;
1764711Seric 		fmt += 4;
1774711Seric 	}
1784711Seric 	if (num[3] == '-')
1794711Seric 		del = '-';
1804711Seric 	else
1814711Seric 		del = ' ';
1824711Seric 	(void) sprintf(eb, "%3.3s%c", num, del);
1834711Seric 	eb += 4;
1844063Seric 
1854711Seric 	/* output the "to" person */
1864711Seric 	if (to != NULL && to[0] != '\0')
1874711Seric 	{
1884711Seric 		(void) sprintf(eb, "%s... ", to);
1895201Seric 		while (*eb != '\0')
1905201Seric 			*eb++ &= 0177;
1914711Seric 	}
1924711Seric 
1934711Seric 	/* output the message */
1944711Seric 	(void) sprintf(eb, fmt, a, b, c, d, e);
1955201Seric 	while (*eb != '\0')
1965201Seric 		*eb++ &= 0177;
1974711Seric 
1984711Seric 	/* output the error code, if any */
1994711Seric 	if (errno != 0)
2004711Seric 	{
2014711Seric 		extern int sys_nerr;
2024711Seric 		extern char *sys_errlist[];
2034711Seric 		if (errno < sys_nerr && errno > 0)
2044711Seric 			(void) sprintf(eb, ": %s", sys_errlist[errno]);
2054577Seric 		else
2064711Seric 			(void) sprintf(eb, ": error %d", errno);
2074711Seric 		eb += strlen(eb);
2084577Seric 	}
209295Seric }
210