xref: /csrg-svn/usr.sbin/sendmail/src/err.c (revision 7762)
13311Seric # include "sendmail.h"
2295Seric 
3*7762Seric SCCSID(@(#)err.c	3.27		08/15/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
17*7762Seric **		Through TopFrame if QuickAbort is set.
18295Seric **
19295Seric **	Side Effects:
201514Seric **		increments Errors.
211514Seric **		sets ExitStat.
22295Seric */
23295Seric 
244084Seric # ifdef lint
254084Seric int	sys_nerr;
264084Seric char	*sys_errlist[];
274084Seric # endif lint
287525Seric static char	MsgBuf[BUFSIZ*2];	/* text of most recent message */
294084Seric 
30295Seric /*VARARGS1*/
31295Seric syserr(fmt, a, b, c, d, e)
32295Seric 	char *fmt;
33295Seric {
344167Seric 	extern char Arpa_Syserr[];
35295Seric 
367525Seric 	/* format and output the error message */
377674Seric 	fmtmsg(MsgBuf, (char *) NULL, Arpa_Syserr, fmt, a, b, c, d, e);
387613Seric 	putmsg(MsgBuf);
394063Seric 
407525Seric 	/* mark the error as having occured */
411514Seric 	Errors++;
426048Seric 	FatalErrors = TRUE;
43295Seric 
44295Seric 	/* determine exit status if not already set */
45295Seric 	if (ExitStat == EX_OK)
46295Seric 	{
47295Seric 		if (errno == 0)
48295Seric 			ExitStat = EX_SOFTWARE;
49295Seric 		else
501598Seric 			ExitStat = EX_OSERR;
51295Seric 	}
52295Seric 
53295Seric # ifdef LOG
547674Seric 	if (LogLevel > 0)
557674Seric 		syslog(LOG_ERR, "%s: %s", MsgId, &MsgBuf[4]);
56295Seric # endif LOG
57295Seric 	errno = 0;
58*7762Seric 	if (QuickAbort)
59*7762Seric 		longjmp(TopFrame, 2);
60295Seric }
61295Seric /*
62295Seric **  USRERR -- Signal user error.
63295Seric **
64295Seric **	This is much like syserr except it is for user errors.
65295Seric **
66295Seric **	Parameters:
67295Seric **		fmt, a, b, c, d -- printf strings
68295Seric **
69295Seric **	Returns:
704084Seric **		none
71*7762Seric **		Through TopFrame if QuickAbort is set.
72295Seric **
73295Seric **	Side Effects:
741514Seric **		increments Errors.
75295Seric */
76295Seric 
77295Seric /*VARARGS1*/
78295Seric usrerr(fmt, a, b, c, d, e)
79295Seric 	char *fmt;
80295Seric {
81295Seric 	extern char SuprErrs;
824167Seric 	extern char Arpa_Usrerr[];
83295Seric 
84295Seric 	if (SuprErrs)
854084Seric 		return;
864063Seric 	Errors++;
876048Seric 	FatalErrors = TRUE;
88295Seric 
897613Seric 	fmtmsg(MsgBuf, CurEnv->e_to, Arpa_Usrerr, fmt, a, b, c, d, e);
907613Seric 	putmsg(MsgBuf);
91*7762Seric 	if (QuickAbort)
92*7762Seric 		longjmp(TopFrame, 1);
934063Seric }
944063Seric /*
954063Seric **  MESSAGE -- print message (not necessarily an error)
964063Seric **
974063Seric **	Parameters:
984063Seric **		num -- the default ARPANET error number (in ascii)
994063Seric **		msg -- the message (printf fmt) -- if it begins
1004063Seric **			with a digit, this number overrides num.
1014063Seric **		a, b, c, d, e -- printf arguments
1024063Seric **
1034063Seric **	Returns:
1044063Seric **		none
1054063Seric **
1064063Seric **	Side Effects:
1074063Seric **		none.
1084063Seric */
1094063Seric 
1104084Seric /*VARARGS2*/
1114063Seric message(num, msg, a, b, c, d, e)
1124063Seric 	register char *num;
1134063Seric 	register char *msg;
1144063Seric {
1154711Seric 	errno = 0;
1167525Seric 	fmtmsg(MsgBuf, CurEnv->e_to, num, msg, a, b, c, d, e);
1177613Seric 	putmsg(MsgBuf);
1187613Seric }
1197613Seric /*
1207613Seric **  PUTMSG -- output error message to transcript and channel
1217613Seric **
1227613Seric **	Parameters:
1237613Seric **		msg -- message to output (in SMTP format).
1247613Seric **
1257613Seric **	Returns:
1267613Seric **		none.
1277613Seric **
1287613Seric **	Side Effects:
1297613Seric **		Outputs msg to the transcript.
1307613Seric **		If appropriate, outputs it to the channel.
1317613Seric **		Deletes SMTP reply code number as appropriate.
1327613Seric */
1334711Seric 
1347613Seric putmsg(msg)
1357613Seric 	char *msg;
1367613Seric {
1374711Seric 	/* output to transcript */
1387613Seric 	fprintf(Xscript, "%s\n", Smtp ? msg : &msg[4]);
1394711Seric 
1404711Seric 	/* output to channel if appropriate */
1417613Seric 	if (!HoldErrs && (Verbose || msg[0] != '0'))
1424063Seric 	{
1437275Seric 		(void) fflush(stdout);
1444711Seric 		if (ArpaMode)
1457613Seric 			fprintf(OutChannel, "%s\r\n", msg);
1464711Seric 		else
1477613Seric 			fprintf(OutChannel, "%s\n", &msg[4]);
1484711Seric 		(void) fflush(OutChannel);
1494063Seric 	}
1504711Seric }
1514711Seric /*
1524711Seric **  FMTMSG -- format a message into buffer.
1534711Seric **
1544711Seric **	Parameters:
1554711Seric **		eb -- error buffer to get result.
1564711Seric **		to -- the recipient tag for this message.
1574711Seric **		num -- arpanet error number.
1584711Seric **		fmt -- format of string.
1594711Seric **		a, b, c, d, e -- arguments.
1604711Seric **
1614711Seric **	Returns:
1624711Seric **		none.
1634711Seric **
1644711Seric **	Side Effects:
1654711Seric **		none.
1664711Seric */
1674063Seric 
1684834Seric /*VARARGS4*/
1694711Seric static
1704711Seric fmtmsg(eb, to, num, fmt, a, b, c, d, e)
1714711Seric 	register char *eb;
1724711Seric 	char *to;
1734711Seric 	char *num;
1744711Seric 	char *fmt;
1754711Seric {
1764711Seric 	char del;
1774711Seric 
1784711Seric 	/* output the reply code */
1794711Seric 	if (isdigit(*fmt))
1804577Seric 	{
1814711Seric 		num = fmt;
1824711Seric 		fmt += 4;
1834711Seric 	}
1844711Seric 	if (num[3] == '-')
1854711Seric 		del = '-';
1864711Seric 	else
1874711Seric 		del = ' ';
1884711Seric 	(void) sprintf(eb, "%3.3s%c", num, del);
1894711Seric 	eb += 4;
1904063Seric 
1914711Seric 	/* output the "to" person */
1924711Seric 	if (to != NULL && to[0] != '\0')
1934711Seric 	{
1944711Seric 		(void) sprintf(eb, "%s... ", to);
1955201Seric 		while (*eb != '\0')
1965201Seric 			*eb++ &= 0177;
1974711Seric 	}
1984711Seric 
1994711Seric 	/* output the message */
2004711Seric 	(void) sprintf(eb, fmt, a, b, c, d, e);
2015201Seric 	while (*eb != '\0')
2025201Seric 		*eb++ &= 0177;
2034711Seric 
2044711Seric 	/* output the error code, if any */
2054711Seric 	if (errno != 0)
2064711Seric 	{
2074711Seric 		extern int sys_nerr;
2084711Seric 		extern char *sys_errlist[];
2094711Seric 		if (errno < sys_nerr && errno > 0)
2104711Seric 			(void) sprintf(eb, ": %s", sys_errlist[errno]);
2114577Seric 		else
2124711Seric 			(void) sprintf(eb, ": error %d", errno);
2134711Seric 		eb += strlen(eb);
2144577Seric 	}
215295Seric }
216