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