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