xref: /csrg-svn/usr.sbin/sendmail/src/err.c (revision 7810)
1 # include "sendmail.h"
2 
3 SCCSID(@(#)err.c	3.28		08/22/82);
4 
5 /*
6 **  SYSERR -- Print error message.
7 **
8 **	Prints an error message via printf to the diagnostic
9 **	output.  If LOG is defined, it logs it also.
10 **
11 **	Parameters:
12 **		f -- the format string
13 **		a, b, c, d, e -- parameters
14 **
15 **	Returns:
16 **		none
17 **		Through TopFrame if QuickAbort is set.
18 **
19 **	Side Effects:
20 **		increments Errors.
21 **		sets ExitStat.
22 */
23 
24 # ifdef lint
25 int	sys_nerr;
26 char	*sys_errlist[];
27 # endif lint
28 static char	MsgBuf[BUFSIZ*2];	/* text of most recent message */
29 
30 /*VARARGS1*/
31 syserr(fmt, a, b, c, d, e)
32 	char *fmt;
33 {
34 	extern char Arpa_Syserr[];
35 
36 	/* format and output the error message */
37 	fmtmsg(MsgBuf, (char *) NULL, Arpa_Syserr, fmt, a, b, c, d, e);
38 	putmsg(MsgBuf);
39 
40 	/* mark the error as having occured */
41 	Errors++;
42 	FatalErrors = TRUE;
43 
44 	/* determine exit status if not already set */
45 	if (ExitStat == EX_OK)
46 	{
47 		if (errno == 0)
48 			ExitStat = EX_SOFTWARE;
49 		else
50 			ExitStat = EX_OSERR;
51 	}
52 
53 	(void) queuename(CurEnv, '\0');
54 # ifdef LOG
55 	if (LogLevel > 0)
56 		syslog(LOG_ERR, "%s: %s", CurEnv->e_id, &MsgBuf[4]);
57 # endif LOG
58 	errno = 0;
59 	if (QuickAbort)
60 		longjmp(TopFrame, 2);
61 }
62 /*
63 **  USRERR -- Signal user error.
64 **
65 **	This is much like syserr except it is for user errors.
66 **
67 **	Parameters:
68 **		fmt, a, b, c, d -- printf strings
69 **
70 **	Returns:
71 **		none
72 **		Through TopFrame if QuickAbort is set.
73 **
74 **	Side Effects:
75 **		increments Errors.
76 */
77 
78 /*VARARGS1*/
79 usrerr(fmt, a, b, c, d, e)
80 	char *fmt;
81 {
82 	extern char SuprErrs;
83 	extern char Arpa_Usrerr[];
84 
85 	if (SuprErrs)
86 		return;
87 	Errors++;
88 	FatalErrors = TRUE;
89 
90 	fmtmsg(MsgBuf, CurEnv->e_to, Arpa_Usrerr, fmt, a, b, c, d, e);
91 	putmsg(MsgBuf);
92 	if (QuickAbort)
93 		longjmp(TopFrame, 1);
94 }
95 /*
96 **  MESSAGE -- print message (not necessarily an error)
97 **
98 **	Parameters:
99 **		num -- the default ARPANET error number (in ascii)
100 **		msg -- the message (printf fmt) -- if it begins
101 **			with a digit, this number overrides num.
102 **		a, b, c, d, e -- printf arguments
103 **
104 **	Returns:
105 **		none
106 **
107 **	Side Effects:
108 **		none.
109 */
110 
111 /*VARARGS2*/
112 message(num, msg, a, b, c, d, e)
113 	register char *num;
114 	register char *msg;
115 {
116 	errno = 0;
117 	fmtmsg(MsgBuf, CurEnv->e_to, num, msg, a, b, c, d, e);
118 	putmsg(MsgBuf);
119 }
120 /*
121 **  PUTMSG -- output error message to transcript and channel
122 **
123 **	Parameters:
124 **		msg -- message to output (in SMTP format).
125 **
126 **	Returns:
127 **		none.
128 **
129 **	Side Effects:
130 **		Outputs msg to the transcript.
131 **		If appropriate, outputs it to the channel.
132 **		Deletes SMTP reply code number as appropriate.
133 */
134 
135 putmsg(msg)
136 	char *msg;
137 {
138 	/* output to transcript */
139 	fprintf(Xscript, "%s\n", Smtp ? msg : &msg[4]);
140 
141 	/* output to channel if appropriate */
142 	if (!HoldErrs && (Verbose || msg[0] != '0'))
143 	{
144 		(void) fflush(stdout);
145 		if (ArpaMode)
146 			fprintf(OutChannel, "%s\r\n", msg);
147 		else
148 			fprintf(OutChannel, "%s\n", &msg[4]);
149 		(void) fflush(OutChannel);
150 	}
151 }
152 /*
153 **  FMTMSG -- format a message into buffer.
154 **
155 **	Parameters:
156 **		eb -- error buffer to get result.
157 **		to -- the recipient tag for this message.
158 **		num -- arpanet error number.
159 **		fmt -- format of string.
160 **		a, b, c, d, e -- arguments.
161 **
162 **	Returns:
163 **		none.
164 **
165 **	Side Effects:
166 **		none.
167 */
168 
169 /*VARARGS4*/
170 static
171 fmtmsg(eb, to, num, fmt, a, b, c, d, e)
172 	register char *eb;
173 	char *to;
174 	char *num;
175 	char *fmt;
176 {
177 	char del;
178 
179 	/* output the reply code */
180 	if (isdigit(*fmt))
181 	{
182 		num = fmt;
183 		fmt += 4;
184 	}
185 	if (num[3] == '-')
186 		del = '-';
187 	else
188 		del = ' ';
189 	(void) sprintf(eb, "%3.3s%c", num, del);
190 	eb += 4;
191 
192 	/* output the "to" person */
193 	if (to != NULL && to[0] != '\0')
194 	{
195 		(void) sprintf(eb, "%s... ", to);
196 		while (*eb != '\0')
197 			*eb++ &= 0177;
198 	}
199 
200 	/* output the message */
201 	(void) sprintf(eb, fmt, a, b, c, d, e);
202 	while (*eb != '\0')
203 		*eb++ &= 0177;
204 
205 	/* output the error code, if any */
206 	if (errno != 0)
207 	{
208 		extern int sys_nerr;
209 		extern char *sys_errlist[];
210 		if (errno < sys_nerr && errno > 0)
211 			(void) sprintf(eb, ": %s", sys_errlist[errno]);
212 		else
213 			(void) sprintf(eb, ": error %d", errno);
214 		eb += strlen(eb);
215 	}
216 }
217