collect.c revision 1.34 1 1.34 christos /* $NetBSD: collect.c,v 1.34 2006/09/29 14:59:31 christos Exp $ */
2 1.6 christos
3 1.1 cgd /*
4 1.3 deraadt * Copyright (c) 1980, 1993
5 1.3 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.30 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.11 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.6 christos #if 0
35 1.6 christos static char sccsid[] = "@(#)collect.c 8.2 (Berkeley) 4/19/94";
36 1.6 christos #else
37 1.34 christos __RCSID("$NetBSD: collect.c,v 1.34 2006/09/29 14:59:31 christos Exp $");
38 1.6 christos #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd /*
42 1.1 cgd * Mail -- a mail program
43 1.1 cgd *
44 1.1 cgd * Collect input from standard input, handling
45 1.1 cgd * ~ escapes.
46 1.1 cgd */
47 1.1 cgd
48 1.1 cgd #include "rcv.h"
49 1.3 deraadt #include "extern.h"
50 1.1 cgd
51 1.21 christos
52 1.1 cgd /*
53 1.29 wiz * Read a message from standard input and return a read file to it
54 1.1 cgd * or NULL on error.
55 1.1 cgd */
56 1.1 cgd
57 1.1 cgd /*
58 1.1 cgd * The following hokiness with global variables is so that on
59 1.1 cgd * receipt of an interrupt signal, the partial message can be salted
60 1.1 cgd * away on dead.letter.
61 1.1 cgd */
62 1.1 cgd
63 1.1 cgd static sig_t saveint; /* Previous SIGINT value */
64 1.1 cgd static sig_t savehup; /* Previous SIGHUP value */
65 1.1 cgd static sig_t savetstp; /* Previous SIGTSTP value */
66 1.1 cgd static sig_t savettou; /* Previous SIGTTOU value */
67 1.1 cgd static sig_t savettin; /* Previous SIGTTIN value */
68 1.1 cgd static FILE *collf; /* File for saving away */
69 1.1 cgd static int hadintr; /* Have seen one SIGINT so far */
70 1.1 cgd
71 1.1 cgd static jmp_buf colljmp; /* To get back to work */
72 1.1 cgd static int colljmp_p; /* whether to long jump */
73 1.1 cgd static jmp_buf collabort; /* To end collection with error */
74 1.1 cgd
75 1.33 christos #if 0
76 1.33 christos static void show_name(const char *prefix, struct name *np)
77 1.33 christos {
78 1.33 christos int i = 0;
79 1.33 christos
80 1.33 christos for (/* EMPTY */; np ; np = np->n_flink) {
81 1.33 christos printf("%s[%d]: %s\n", prefix, i, np->n_name);
82 1.33 christos i++;
83 1.33 christos }
84 1.33 christos }
85 1.33 christos
86 1.33 christos void show_header(struct header *hp);
87 1.33 christos void show_header(struct header *hp)
88 1.33 christos {
89 1.33 christos show_name("TO", hp->h_to);
90 1.33 christos printf("SUBJECT: %s\n", hp->h_subject);
91 1.33 christos show_name("CC", hp->h_cc);
92 1.33 christos show_name("BCC", hp->h_bcc);
93 1.33 christos show_name("SMOPTS", hp->h_smopts);
94 1.33 christos }
95 1.33 christos #endif
96 1.33 christos
97 1.33 christos
98 1.1 cgd FILE *
99 1.22 wiz collect(struct header *hp, int printheaders)
100 1.1 cgd {
101 1.1 cgd FILE *fbuf;
102 1.34 christos int lc, cc;
103 1.27 wiz int c, fd, t;
104 1.31 christos char linebuf[LINESIZE];
105 1.31 christos const char *cp;
106 1.27 wiz char tempname[PATHSIZE];
107 1.28 wiz char mailtempname[PATHSIZE];
108 1.34 christos int lastlong, rc; /* So we don't make 2 or more lines
109 1.8 phil out of a long input line. */
110 1.34 christos
111 1.34 christos /* The following are declared volatile to avoid longjmp clobbering. */
112 1.34 christos volatile char getsub;
113 1.34 christos volatile int escape;
114 1.34 christos volatile sigset_t nset;
115 1.34 christos volatile int eofcount;
116 1.34 christos volatile int longline;
117 1.34 christos
118 1.1 cgd
119 1.32 christos (void)memset(mailtempname, 0, sizeof(mailtempname));
120 1.1 cgd collf = NULL;
121 1.1 cgd /*
122 1.1 cgd * Start catching signals from here, but we're still die on interrupts
123 1.1 cgd * until we're in the main loop.
124 1.1 cgd */
125 1.34 christos (void)sigemptyset(__UNVOLATILE(&nset));
126 1.34 christos (void)sigaddset(__UNVOLATILE(&nset), SIGINT);
127 1.34 christos (void)sigaddset(__UNVOLATILE(&nset), SIGHUP);
128 1.34 christos (void)sigprocmask(SIG_BLOCK, __UNVOLATILE(&nset), NULL);
129 1.1 cgd if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
130 1.32 christos (void)signal(SIGINT, collint);
131 1.1 cgd if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
132 1.32 christos (void)signal(SIGHUP, collhup);
133 1.1 cgd savetstp = signal(SIGTSTP, collstop);
134 1.1 cgd savettou = signal(SIGTTOU, collstop);
135 1.1 cgd savettin = signal(SIGTTIN, collstop);
136 1.1 cgd if (setjmp(collabort) || setjmp(colljmp)) {
137 1.28 wiz (void)rm(mailtempname);
138 1.1 cgd goto err;
139 1.1 cgd }
140 1.34 christos (void)sigprocmask(SIG_UNBLOCK, __UNVOLATILE(&nset), NULL);
141 1.1 cgd
142 1.1 cgd noreset++;
143 1.28 wiz (void)snprintf(mailtempname, sizeof(mailtempname),
144 1.28 wiz "%s/mail.RsXXXXXXXXXX", tmpdir);
145 1.28 wiz if ((fd = mkstemp(mailtempname)) == -1 ||
146 1.28 wiz (collf = Fdopen(fd, "w+")) == NULL) {
147 1.28 wiz if (fd != -1)
148 1.32 christos (void)close(fd);
149 1.28 wiz warn("%s", mailtempname);
150 1.1 cgd goto err;
151 1.1 cgd }
152 1.28 wiz (void)rm(mailtempname);
153 1.1 cgd
154 1.1 cgd /*
155 1.1 cgd * If we are going to prompt for a subject,
156 1.1 cgd * refrain from printing a newline after
157 1.1 cgd * the headers (since some people mind).
158 1.1 cgd */
159 1.33 christos t = GTO|GSUBJECT|GCC|GNL|GSMOPTS;
160 1.1 cgd getsub = 0;
161 1.24 wiz if (hp->h_subject == NULL && value("interactive") != NULL &&
162 1.24 wiz (value("ask") != NULL || value("asksub") != NULL))
163 1.1 cgd t &= ~GNL, getsub++;
164 1.1 cgd if (printheaders) {
165 1.32 christos (void)puthead(hp, stdout, t);
166 1.32 christos (void)fflush(stdout);
167 1.1 cgd }
168 1.24 wiz if ((cp = value("escape")) != NULL)
169 1.1 cgd escape = *cp;
170 1.1 cgd else
171 1.1 cgd escape = ESCAPE;
172 1.1 cgd eofcount = 0;
173 1.1 cgd hadintr = 0;
174 1.8 phil lastlong = 0;
175 1.8 phil longline = 0;
176 1.1 cgd
177 1.1 cgd if (!setjmp(colljmp)) {
178 1.1 cgd if (getsub)
179 1.32 christos (void)grabh(hp, GSUBJECT);
180 1.1 cgd } else {
181 1.1 cgd /*
182 1.1 cgd * Come here for printing the after-signal message.
183 1.1 cgd * Duplicate messages won't be printed because
184 1.1 cgd * the write is aborted if we get a SIGTTOU.
185 1.1 cgd */
186 1.1 cgd cont:
187 1.1 cgd if (hadintr) {
188 1.32 christos (void)fflush(stdout);
189 1.32 christos (void)fprintf(stderr,
190 1.1 cgd "\n(Interrupt -- one more to kill letter)\n");
191 1.1 cgd } else {
192 1.32 christos (void)printf("(continue)\n");
193 1.32 christos (void)fflush(stdout);
194 1.1 cgd }
195 1.1 cgd }
196 1.1 cgd for (;;) {
197 1.1 cgd colljmp_p = 1;
198 1.1 cgd c = readline(stdin, linebuf, LINESIZE);
199 1.1 cgd colljmp_p = 0;
200 1.33 christos #ifdef USE_READLINE
201 1.33 christos if (c < 0) {
202 1.33 christos char *p;
203 1.33 christos if (value("interactive") != NULL &&
204 1.33 christos (p = value("ignoreeof")) != NULL &&
205 1.33 christos ++eofcount < (*p == 0 ? 25 : atoi(p))) {
206 1.33 christos (void)printf("Use \".\" to terminate letter\n");
207 1.33 christos continue;
208 1.33 christos }
209 1.33 christos break;
210 1.33 christos }
211 1.33 christos #else
212 1.1 cgd if (c < 0) {
213 1.24 wiz if (value("interactive") != NULL &&
214 1.24 wiz value("ignoreeof") != NULL && ++eofcount < 25) {
215 1.32 christos (void)printf("Use \".\" to terminate letter\n");
216 1.1 cgd continue;
217 1.1 cgd }
218 1.1 cgd break;
219 1.1 cgd }
220 1.33 christos #endif
221 1.8 phil lastlong = longline;
222 1.8 phil longline = c == LINESIZE-1;
223 1.1 cgd eofcount = 0;
224 1.1 cgd hadintr = 0;
225 1.1 cgd if (linebuf[0] == '.' && linebuf[1] == '\0' &&
226 1.24 wiz value("interactive") != NULL && !lastlong &&
227 1.24 wiz (value("dot") != NULL || value("ignoreeof") != NULL))
228 1.1 cgd break;
229 1.24 wiz if (linebuf[0] != escape || value("interactive") == NULL ||
230 1.8 phil lastlong) {
231 1.8 phil if (putline(collf, linebuf, !longline) < 0)
232 1.1 cgd goto err;
233 1.1 cgd continue;
234 1.1 cgd }
235 1.1 cgd c = linebuf[1];
236 1.1 cgd switch (c) {
237 1.1 cgd default:
238 1.1 cgd /*
239 1.1 cgd * On double escape, just send the single one.
240 1.1 cgd * Otherwise, it's an error.
241 1.1 cgd */
242 1.1 cgd if (c == escape) {
243 1.8 phil if (putline(collf, &linebuf[1], !longline) < 0)
244 1.1 cgd goto err;
245 1.1 cgd else
246 1.1 cgd break;
247 1.1 cgd }
248 1.32 christos (void)printf("Unknown tilde escape.\n");
249 1.1 cgd break;
250 1.1 cgd case 'C':
251 1.1 cgd /*
252 1.1 cgd * Dump core.
253 1.1 cgd */
254 1.32 christos (void)core(NULL);
255 1.1 cgd break;
256 1.1 cgd case '!':
257 1.1 cgd /*
258 1.1 cgd * Shell escape, send the balance of the
259 1.1 cgd * line to sh -c.
260 1.1 cgd */
261 1.32 christos (void)shell(&linebuf[2]);
262 1.1 cgd break;
263 1.1 cgd case ':':
264 1.5 jtc case '_':
265 1.1 cgd /*
266 1.1 cgd * Escape to command mode, but be nice!
267 1.1 cgd */
268 1.32 christos (void)execute(&linebuf[2], 1);
269 1.1 cgd goto cont;
270 1.1 cgd case '.':
271 1.1 cgd /*
272 1.1 cgd * Simulate end of file on input.
273 1.1 cgd */
274 1.1 cgd goto out;
275 1.1 cgd case 'q':
276 1.1 cgd /*
277 1.1 cgd * Force a quit of sending mail.
278 1.1 cgd * Act like an interrupt happened.
279 1.1 cgd */
280 1.1 cgd hadintr++;
281 1.1 cgd collint(SIGINT);
282 1.1 cgd exit(1);
283 1.32 christos /*NOTREACHED*/
284 1.19 mjl
285 1.19 mjl case 'x': /* exit, do not save in dead.letter */
286 1.19 mjl goto err;
287 1.19 mjl
288 1.1 cgd case 'h':
289 1.1 cgd /*
290 1.1 cgd * Grab a bunch of headers.
291 1.1 cgd */
292 1.33 christos (void)grabh(hp, GTO|GSUBJECT|GCC|GBCC|GSMOPTS);
293 1.1 cgd goto cont;
294 1.1 cgd case 't':
295 1.1 cgd /*
296 1.1 cgd * Add to the To list.
297 1.1 cgd */
298 1.1 cgd hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
299 1.1 cgd break;
300 1.1 cgd case 's':
301 1.1 cgd /*
302 1.1 cgd * Set the Subject list.
303 1.1 cgd */
304 1.1 cgd cp = &linebuf[2];
305 1.18 christos while (isspace((unsigned char)*cp))
306 1.1 cgd cp++;
307 1.1 cgd hp->h_subject = savestr(cp);
308 1.1 cgd break;
309 1.1 cgd case 'c':
310 1.1 cgd /*
311 1.1 cgd * Add to the CC list.
312 1.1 cgd */
313 1.1 cgd hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
314 1.1 cgd break;
315 1.1 cgd case 'b':
316 1.1 cgd /*
317 1.1 cgd * Add stuff to blind carbon copies list.
318 1.1 cgd */
319 1.1 cgd hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
320 1.1 cgd break;
321 1.19 mjl case 'i':
322 1.19 mjl case 'A':
323 1.19 mjl case 'a':
324 1.19 mjl /*
325 1.19 mjl * Insert named variable in message
326 1.19 mjl */
327 1.19 mjl
328 1.19 mjl switch(c) {
329 1.19 mjl case 'i':
330 1.19 mjl cp = &linebuf[2];
331 1.19 mjl while(isspace((unsigned char) *cp))
332 1.19 mjl cp++;
333 1.19 mjl
334 1.19 mjl break;
335 1.19 mjl case 'a':
336 1.19 mjl cp = "sign";
337 1.19 mjl break;
338 1.19 mjl case 'A':
339 1.19 mjl cp = "Sign";
340 1.19 mjl break;
341 1.19 mjl default:
342 1.19 mjl goto err;
343 1.19 mjl }
344 1.19 mjl
345 1.24 wiz if(*cp && (cp = value(cp)) != NULL) {
346 1.32 christos (void)printf("%s\n", cp);
347 1.19 mjl if(putline(collf, cp, 1) < 0)
348 1.19 mjl goto err;
349 1.19 mjl }
350 1.19 mjl
351 1.19 mjl break;
352 1.19 mjl
353 1.1 cgd case 'd':
354 1.32 christos (void)strcpy(linebuf + 2, getdeadletter());
355 1.32 christos /* FALLTHROUGH */
356 1.1 cgd case 'r':
357 1.5 jtc case '<':
358 1.1 cgd /*
359 1.1 cgd * Invoke a file:
360 1.1 cgd * Search for the file name,
361 1.1 cgd * then open it and copy the contents to collf.
362 1.1 cgd */
363 1.1 cgd cp = &linebuf[2];
364 1.18 christos while (isspace((unsigned char)*cp))
365 1.1 cgd cp++;
366 1.1 cgd if (*cp == '\0') {
367 1.32 christos (void)printf("Interpolate what file?\n");
368 1.1 cgd break;
369 1.1 cgd }
370 1.19 mjl
371 1.1 cgd cp = expand(cp);
372 1.24 wiz if (cp == NULL)
373 1.1 cgd break;
374 1.19 mjl
375 1.19 mjl if (*cp == '!') { /* insert stdout of command */
376 1.31 christos const char *shellcmd;
377 1.19 mjl int nullfd;
378 1.23 wiz int rc2;
379 1.19 mjl
380 1.19 mjl if((nullfd = open("/dev/null", O_RDONLY, 0)) == -1) {
381 1.26 wiz warn("/dev/null");
382 1.19 mjl break;
383 1.19 mjl }
384 1.19 mjl
385 1.27 wiz (void)snprintf(tempname, sizeof(tempname),
386 1.28 wiz "%s/mail.ReXXXXXXXXXX", tmpdir);
387 1.27 wiz if ((fd = mkstemp(tempname)) == -1 ||
388 1.27 wiz (fbuf = Fdopen(fd, "w+")) == NULL) {
389 1.27 wiz if (fd != -1)
390 1.32 christos (void)close(fd);
391 1.27 wiz warn("%s", tempname);
392 1.19 mjl break;
393 1.19 mjl }
394 1.27 wiz (void)unlink(tempname);
395 1.19 mjl
396 1.24 wiz if ((shellcmd = value("SHELL")) == NULL)
397 1.23 wiz shellcmd = _PATH_CSHELL;
398 1.19 mjl
399 1.24 wiz rc2 = run_command(shellcmd, 0, nullfd, fileno(fbuf), "-c", cp+1, NULL);
400 1.19 mjl
401 1.32 christos (void)close(nullfd);
402 1.19 mjl
403 1.23 wiz if (rc2 < 0) {
404 1.25 wiz (void)Fclose(fbuf);
405 1.19 mjl break;
406 1.19 mjl }
407 1.19 mjl
408 1.19 mjl if (fsize(fbuf) == 0) {
409 1.32 christos (void)fprintf(stderr, "No bytes from command \"%s\"\n", cp+1);
410 1.25 wiz (void)Fclose(fbuf);
411 1.19 mjl break;
412 1.19 mjl }
413 1.19 mjl
414 1.19 mjl rewind(fbuf);
415 1.19 mjl }
416 1.19 mjl else if (isdir(cp)) {
417 1.32 christos (void)printf("%s: Directory\n", cp);
418 1.1 cgd break;
419 1.1 cgd }
420 1.19 mjl else if ((fbuf = Fopen(cp, "r")) == NULL) {
421 1.26 wiz warn("%s", cp);
422 1.1 cgd break;
423 1.1 cgd }
424 1.32 christos (void)printf("\"%s\" ", cp);
425 1.32 christos (void)fflush(stdout);
426 1.1 cgd lc = 0;
427 1.1 cgd cc = 0;
428 1.8 phil while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) {
429 1.8 phil if (rc != LINESIZE-1) lc++;
430 1.8 phil if ((t = putline(collf, linebuf,
431 1.8 phil rc != LINESIZE-1)) < 0) {
432 1.32 christos (void)Fclose(fbuf);
433 1.1 cgd goto err;
434 1.1 cgd }
435 1.1 cgd cc += t;
436 1.1 cgd }
437 1.32 christos (void)Fclose(fbuf);
438 1.32 christos (void)printf("%d/%d\n", lc, cc);
439 1.1 cgd break;
440 1.1 cgd case 'w':
441 1.1 cgd /*
442 1.1 cgd * Write the message on a file.
443 1.1 cgd */
444 1.1 cgd cp = &linebuf[2];
445 1.1 cgd while (*cp == ' ' || *cp == '\t')
446 1.1 cgd cp++;
447 1.1 cgd if (*cp == '\0') {
448 1.32 christos (void)fprintf(stderr, "Write what file!?\n");
449 1.1 cgd break;
450 1.1 cgd }
451 1.24 wiz if ((cp = expand(cp)) == NULL)
452 1.1 cgd break;
453 1.1 cgd rewind(collf);
454 1.32 christos (void)exwrite(cp, collf, 1);
455 1.1 cgd break;
456 1.1 cgd case 'm':
457 1.1 cgd case 'M':
458 1.1 cgd case 'f':
459 1.1 cgd case 'F':
460 1.1 cgd /*
461 1.1 cgd * Interpolate the named messages, if we
462 1.1 cgd * are in receiving mail mode. Does the
463 1.1 cgd * standard list processing garbage.
464 1.1 cgd * If ~f is given, we don't shift over.
465 1.1 cgd */
466 1.28 wiz if (forward(linebuf + 2, collf, mailtempname, c) < 0)
467 1.1 cgd goto err;
468 1.1 cgd goto cont;
469 1.1 cgd case '?':
470 1.1 cgd if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
471 1.26 wiz warn(_PATH_TILDE);
472 1.1 cgd break;
473 1.1 cgd }
474 1.1 cgd while ((t = getc(fbuf)) != EOF)
475 1.25 wiz (void)putchar(t);
476 1.32 christos (void)Fclose(fbuf);
477 1.1 cgd break;
478 1.1 cgd case 'p':
479 1.1 cgd /*
480 1.1 cgd * Print out the current state of the
481 1.1 cgd * message without altering anything.
482 1.1 cgd */
483 1.1 cgd rewind(collf);
484 1.32 christos (void)printf("-------\nMessage contains:\n");
485 1.32 christos (void)puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
486 1.1 cgd while ((t = getc(collf)) != EOF)
487 1.25 wiz (void)putchar(t);
488 1.1 cgd goto cont;
489 1.1 cgd case '|':
490 1.1 cgd /*
491 1.1 cgd * Pipe message through command.
492 1.1 cgd * Collect output as new message.
493 1.1 cgd */
494 1.1 cgd rewind(collf);
495 1.1 cgd mespipe(collf, &linebuf[2]);
496 1.1 cgd goto cont;
497 1.1 cgd case 'v':
498 1.1 cgd case 'e':
499 1.1 cgd /*
500 1.1 cgd * Edit the current message.
501 1.1 cgd * 'e' means to use EDITOR
502 1.1 cgd * 'v' means to use VISUAL
503 1.1 cgd */
504 1.1 cgd rewind(collf);
505 1.1 cgd mesedit(collf, c);
506 1.1 cgd goto cont;
507 1.1 cgd }
508 1.1 cgd }
509 1.1 cgd goto out;
510 1.1 cgd err:
511 1.1 cgd if (collf != NULL) {
512 1.32 christos (void)Fclose(collf);
513 1.1 cgd collf = NULL;
514 1.1 cgd }
515 1.1 cgd out:
516 1.1 cgd if (collf != NULL)
517 1.1 cgd rewind(collf);
518 1.1 cgd noreset--;
519 1.34 christos (void)sigprocmask(SIG_BLOCK, __UNVOLATILE(&nset), NULL);
520 1.32 christos (void)signal(SIGINT, saveint);
521 1.32 christos (void)signal(SIGHUP, savehup);
522 1.32 christos (void)signal(SIGTSTP, savetstp);
523 1.32 christos (void)signal(SIGTTOU, savettou);
524 1.32 christos (void)signal(SIGTTIN, savettin);
525 1.34 christos (void)sigprocmask(SIG_UNBLOCK, __UNVOLATILE(&nset), NULL);
526 1.1 cgd return collf;
527 1.1 cgd }
528 1.1 cgd
529 1.1 cgd /*
530 1.1 cgd * Write a file, ex-like if f set.
531 1.1 cgd */
532 1.3 deraadt int
533 1.31 christos exwrite(const char name[], FILE *fp, int f)
534 1.1 cgd {
535 1.11 lukem FILE *of;
536 1.11 lukem int c;
537 1.1 cgd long cc;
538 1.1 cgd int lc;
539 1.1 cgd struct stat junk;
540 1.1 cgd
541 1.1 cgd if (f) {
542 1.32 christos (void)printf("\"%s\" ", name);
543 1.32 christos (void)fflush(stdout);
544 1.1 cgd }
545 1.12 mycroft if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) {
546 1.1 cgd if (!f)
547 1.32 christos (void)fprintf(stderr, "%s: ", name);
548 1.32 christos (void)fprintf(stderr, "File exists\n");
549 1.1 cgd return(-1);
550 1.1 cgd }
551 1.1 cgd if ((of = Fopen(name, "w")) == NULL) {
552 1.26 wiz warn("%s", name);
553 1.1 cgd return(-1);
554 1.1 cgd }
555 1.1 cgd lc = 0;
556 1.1 cgd cc = 0;
557 1.1 cgd while ((c = getc(fp)) != EOF) {
558 1.1 cgd cc++;
559 1.1 cgd if (c == '\n')
560 1.1 cgd lc++;
561 1.25 wiz (void)putc(c, of);
562 1.1 cgd if (ferror(of)) {
563 1.26 wiz warn("%s", name);
564 1.32 christos (void)Fclose(of);
565 1.1 cgd return(-1);
566 1.1 cgd }
567 1.1 cgd }
568 1.32 christos (void)Fclose(of);
569 1.32 christos (void)printf("%d/%ld\n", lc, cc);
570 1.32 christos (void)fflush(stdout);
571 1.1 cgd return(0);
572 1.1 cgd }
573 1.1 cgd
574 1.1 cgd /*
575 1.1 cgd * Edit the message being collected on fp.
576 1.1 cgd * On return, make the edit file the new temp file.
577 1.1 cgd */
578 1.3 deraadt void
579 1.22 wiz mesedit(FILE *fp, int c)
580 1.1 cgd {
581 1.1 cgd sig_t sigint = signal(SIGINT, SIG_IGN);
582 1.1 cgd FILE *nf = run_editor(fp, (off_t)-1, c, 0);
583 1.1 cgd
584 1.1 cgd if (nf != NULL) {
585 1.32 christos (void)fseek(nf, 0L, 2);
586 1.1 cgd collf = nf;
587 1.32 christos (void)Fclose(fp);
588 1.1 cgd }
589 1.25 wiz (void)signal(SIGINT, sigint);
590 1.1 cgd }
591 1.1 cgd
592 1.1 cgd /*
593 1.1 cgd * Pipe the message through the command.
594 1.1 cgd * Old message is on stdin of command;
595 1.1 cgd * New message collected from stdout.
596 1.1 cgd * Sh -c must return 0 to accept the new message.
597 1.1 cgd */
598 1.3 deraadt void
599 1.22 wiz mespipe(FILE *fp, char cmd[])
600 1.1 cgd {
601 1.1 cgd FILE *nf;
602 1.1 cgd sig_t sigint = signal(SIGINT, SIG_IGN);
603 1.31 christos const char *shellcmd;
604 1.27 wiz int fd;
605 1.27 wiz char tempname[PATHSIZE];
606 1.1 cgd
607 1.28 wiz (void)snprintf(tempname, sizeof(tempname),
608 1.28 wiz "%s/mail.ReXXXXXXXXXX", tmpdir);
609 1.27 wiz if ((fd = mkstemp(tempname)) == -1 ||
610 1.27 wiz (nf = Fdopen(fd, "w+")) == NULL) {
611 1.27 wiz if (fd != -1)
612 1.32 christos (void)close(fd);
613 1.27 wiz warn("%s", tempname);
614 1.1 cgd goto out;
615 1.1 cgd }
616 1.27 wiz (void)unlink(tempname);
617 1.1 cgd /*
618 1.1 cgd * stdin = current message.
619 1.1 cgd * stdout = new message.
620 1.1 cgd */
621 1.24 wiz if ((shellcmd = value("SHELL")) == NULL)
622 1.23 wiz shellcmd = _PATH_CSHELL;
623 1.23 wiz if (run_command(shellcmd,
624 1.24 wiz 0, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) {
625 1.25 wiz (void)Fclose(nf);
626 1.1 cgd goto out;
627 1.1 cgd }
628 1.1 cgd if (fsize(nf) == 0) {
629 1.32 christos (void)fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
630 1.25 wiz (void)Fclose(nf);
631 1.1 cgd goto out;
632 1.1 cgd }
633 1.1 cgd /*
634 1.1 cgd * Take new files.
635 1.1 cgd */
636 1.25 wiz (void)fseek(nf, 0L, 2);
637 1.1 cgd collf = nf;
638 1.25 wiz (void)Fclose(fp);
639 1.1 cgd out:
640 1.25 wiz (void)signal(SIGINT, sigint);
641 1.1 cgd }
642 1.1 cgd
643 1.1 cgd /*
644 1.1 cgd * Interpolate the named messages into the current
645 1.1 cgd * message, preceding each line with a tab.
646 1.1 cgd * Return a count of the number of characters now in
647 1.1 cgd * the message, or -1 if an error is encountered writing
648 1.1 cgd * the message temporary. The flag argument is 'm' if we
649 1.1 cgd * should shift over and 'f' if not.
650 1.1 cgd */
651 1.3 deraadt int
652 1.28 wiz forward(char ms[], FILE *fp, char *fn, int f)
653 1.1 cgd {
654 1.11 lukem int *msgvec;
655 1.1 cgd struct ignoretab *ig;
656 1.31 christos const char *tabst;
657 1.1 cgd
658 1.31 christos msgvec = salloc((msgCount+1) * sizeof *msgvec);
659 1.31 christos if (msgvec == NULL)
660 1.1 cgd return(0);
661 1.1 cgd if (getmsglist(ms, msgvec, 0) < 0)
662 1.1 cgd return(0);
663 1.1 cgd if (*msgvec == 0) {
664 1.1 cgd *msgvec = first(0, MMNORM);
665 1.7 pk if (*msgvec == 0) {
666 1.32 christos (void)printf("No appropriate messages\n");
667 1.1 cgd return(0);
668 1.1 cgd }
669 1.7 pk msgvec[1] = 0;
670 1.1 cgd }
671 1.1 cgd if (f == 'f' || f == 'F')
672 1.24 wiz tabst = NULL;
673 1.24 wiz else if ((tabst = value("indentprefix")) == NULL)
674 1.1 cgd tabst = "\t";
675 1.1 cgd ig = isupper(f) ? NULL : ignore;
676 1.32 christos (void)printf("Interpolating:");
677 1.1 cgd for (; *msgvec != 0; msgvec++) {
678 1.1 cgd struct message *mp = message + *msgvec - 1;
679 1.1 cgd
680 1.1 cgd touch(mp);
681 1.32 christos (void)printf(" %d", *msgvec);
682 1.20 tron if (sendmessage(mp, fp, ig, tabst) < 0) {
683 1.28 wiz warn("%s", fn);
684 1.1 cgd return(-1);
685 1.1 cgd }
686 1.1 cgd }
687 1.32 christos (void)printf("\n");
688 1.1 cgd return(0);
689 1.1 cgd }
690 1.1 cgd
691 1.1 cgd /*
692 1.1 cgd * Print (continue) when continued after ^Z.
693 1.1 cgd */
694 1.1 cgd /*ARGSUSED*/
695 1.1 cgd void
696 1.22 wiz collstop(int s)
697 1.1 cgd {
698 1.1 cgd sig_t old_action = signal(s, SIG_DFL);
699 1.6 christos sigset_t nset;
700 1.1 cgd
701 1.32 christos (void)sigemptyset(&nset);
702 1.32 christos (void)sigaddset(&nset, s);
703 1.32 christos (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
704 1.32 christos (void)kill(0, s);
705 1.32 christos (void)sigprocmask(SIG_BLOCK, &nset, NULL);
706 1.32 christos (void)signal(s, old_action);
707 1.1 cgd if (colljmp_p) {
708 1.1 cgd colljmp_p = 0;
709 1.1 cgd hadintr = 0;
710 1.1 cgd longjmp(colljmp, 1);
711 1.1 cgd }
712 1.1 cgd }
713 1.1 cgd
714 1.1 cgd /*
715 1.1 cgd * On interrupt, come here to save the partial message in ~/dead.letter.
716 1.1 cgd * Then jump out of the collection loop.
717 1.1 cgd */
718 1.1 cgd /*ARGSUSED*/
719 1.1 cgd void
720 1.22 wiz collint(int s)
721 1.1 cgd {
722 1.1 cgd /*
723 1.1 cgd * the control flow is subtle, because we can be called from ~q.
724 1.1 cgd */
725 1.1 cgd if (!hadintr) {
726 1.24 wiz if (value("ignore") != NULL) {
727 1.32 christos (void)puts("@");
728 1.32 christos (void)fflush(stdout);
729 1.1 cgd clearerr(stdin);
730 1.1 cgd return;
731 1.1 cgd }
732 1.1 cgd hadintr = 1;
733 1.1 cgd longjmp(colljmp, 1);
734 1.1 cgd }
735 1.1 cgd rewind(collf);
736 1.24 wiz if (value("nosave") == NULL)
737 1.1 cgd savedeadletter(collf);
738 1.1 cgd longjmp(collabort, 1);
739 1.1 cgd }
740 1.1 cgd
741 1.1 cgd /*ARGSUSED*/
742 1.1 cgd void
743 1.22 wiz collhup(int s)
744 1.1 cgd {
745 1.1 cgd rewind(collf);
746 1.1 cgd savedeadletter(collf);
747 1.1 cgd /*
748 1.1 cgd * Let's pretend nobody else wants to clean up,
749 1.1 cgd * a true statement at this time.
750 1.1 cgd */
751 1.1 cgd exit(1);
752 1.1 cgd }
753 1.1 cgd
754 1.3 deraadt void
755 1.22 wiz savedeadletter(FILE *fp)
756 1.1 cgd {
757 1.11 lukem FILE *dbuf;
758 1.32 christos mode_t m;
759 1.11 lukem int c;
760 1.31 christos const char *cp;
761 1.1 cgd
762 1.1 cgd if (fsize(fp) == 0)
763 1.1 cgd return;
764 1.1 cgd cp = getdeadletter();
765 1.32 christos m = umask(077);
766 1.1 cgd dbuf = Fopen(cp, "a");
767 1.32 christos (void)umask(m);
768 1.1 cgd if (dbuf == NULL)
769 1.1 cgd return;
770 1.1 cgd while ((c = getc(fp)) != EOF)
771 1.25 wiz (void)putc(c, dbuf);
772 1.32 christos (void)Fclose(dbuf);
773 1.1 cgd rewind(fp);
774 1.1 cgd }
775