4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / usr.bin / mail / cmd2.c
CommitLineData
d0aeaf5a 1/*
a12ff486
KB
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
0c5f72fb 4 *
f15db449 5 * %sccs.include.redist.c%
d0aeaf5a
DF
6 */
7
acfc7e9b 8#ifndef lint
a12ff486 9static char sccsid[] = "@(#)cmd2.c 8.1 (Berkeley) %G%";
acfc7e9b 10#endif /* not lint */
a13d1f28
KS
11
12#include "rcv.h"
828615a1 13#include <sys/wait.h>
a0d23834 14#include "extern.h"
a13d1f28
KS
15
16/*
17 * Mail -- a mail program
18 *
19 * More user commands.
20 */
21
a13d1f28
KS
22/*
23 * If any arguments were given, go to the next applicable argument
24 * following dot, otherwise, go to the next applicable message.
25 * If given as first command with no arguments, print first message.
26 */
a0d23834 27int
a13d1f28
KS
28next(msgvec)
29 int *msgvec;
30{
31 register struct message *mp;
32 register int *ip, *ip2;
33 int list[2], mdot;
34
35 if (*msgvec != NULL) {
36
37 /*
38 * If some messages were supplied, find the
39 * first applicable one following dot using
40 * wrap around.
41 */
42
43 mdot = dot - &message[0] + 1;
30629819
KS
44
45 /*
46 * Find the first message in the supplied
47 * message list which follows dot.
48 */
49
a13d1f28
KS
50 for (ip = msgvec; *ip != NULL; ip++)
51 if (*ip > mdot)
52 break;
53 if (*ip == NULL)
54 ip = msgvec;
55 ip2 = ip;
56 do {
a13d1f28
KS
57 mp = &message[*ip2 - 1];
58 if ((mp->m_flag & MDELETED) == 0) {
59 dot = mp;
60 goto hitit;
61 }
30629819
KS
62 if (*ip2 != NULL)
63 ip2++;
64 if (*ip2 == NULL)
65 ip2 = msgvec;
a13d1f28
KS
66 } while (ip2 != ip);
67 printf("No messages applicable\n");
68 return(1);
69 }
70
71 /*
72 * If this is the first command, select message 1.
73 * Note that this must exist for us to get here at all.
74 */
75
799afc8f 76 if (!sawcom)
a13d1f28 77 goto hitit;
a13d1f28
KS
78
79 /*
80 * Just find the next good message after dot, no
81 * wraparound.
82 */
83
84 for (mp = dot+1; mp < &message[msgCount]; mp++)
85 if ((mp->m_flag & (MDELETED|MSAVED)) == 0)
86 break;
87 if (mp >= &message[msgCount]) {
88 printf("At EOF\n");
89 return(0);
90 }
91 dot = mp;
92hitit:
93 /*
94 * Print dot.
95 */
96
97 list[0] = dot - &message[0] + 1;
98 list[1] = NULL;
99 return(type(list));
100}
101
102/*
c7db23a4
KS
103 * Save a message in a file. Mark the message as saved
104 * so we can discard when the user quits.
a13d1f28 105 */
a0d23834 106int
a13d1f28
KS
107save(str)
108 char str[];
109{
c7db23a4 110
2de8fc95 111 return save1(str, 1, "save", saveignore);
c7db23a4
KS
112}
113
114/*
115 * Copy a message to a file without affected its saved-ness
116 */
a0d23834 117int
c7db23a4
KS
118copycmd(str)
119 char str[];
120{
121
2de8fc95 122 return save1(str, 0, "copy", saveignore);
c7db23a4
KS
123}
124
125/*
126 * Save/copy the indicated messages at the end of the passed file name.
127 * If mark is true, mark the message "saved."
128 */
a0d23834 129int
2de8fc95 130save1(str, mark, cmd, ignore)
c7db23a4 131 char str[];
a0d23834 132 int mark;
2de8fc95
EW
133 char *cmd;
134 struct ignoretab *ignore;
c7db23a4 135{
470c33f3 136 register int *ip;
a13d1f28 137 register struct message *mp;
2de8fc95
EW
138 char *file, *disp;
139 int f, *msgvec;
a13d1f28 140 FILE *obuf;
a13d1f28
KS
141
142 msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
143 if ((file = snarf(str, &f)) == NOSTR)
144 return(1);
145 if (!f) {
146 *msgvec = first(0, MMNORM);
147 if (*msgvec == NULL) {
c7db23a4 148 printf("No messages to %s.\n", cmd);
a13d1f28
KS
149 return(1);
150 }
151 msgvec[1] = NULL;
152 }
153 if (f && getmsglist(str, msgvec, 0) < 0)
154 return(1);
155 if ((file = expand(file)) == NOSTR)
156 return(1);
157 printf("\"%s\" ", file);
80187484 158 fflush(stdout);
2de8fc95 159 if (access(file, 0) >= 0)
a13d1f28
KS
160 disp = "[Appended]";
161 else
162 disp = "[New file]";
07b0d286 163 if ((obuf = Fopen(file, "a")) == NULL) {
a13d1f28
KS
164 perror(NOSTR);
165 return(1);
166 }
a13d1f28 167 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
470c33f3
EW
168 mp = &message[*ip - 1];
169 touch(mp);
2de8fc95 170 if (send(mp, obuf, ignore, NOSTR) < 0) {
a13d1f28 171 perror(file);
07b0d286 172 Fclose(obuf);
a13d1f28
KS
173 return(1);
174 }
c7db23a4
KS
175 if (mark)
176 mp->m_flag |= MSAVED;
a13d1f28
KS
177 }
178 fflush(obuf);
179 if (ferror(obuf))
180 perror(file);
07b0d286 181 Fclose(obuf);
2de8fc95 182 printf("%s\n", disp);
a13d1f28
KS
183 return(0);
184}
185
186/*
187 * Write the indicated messages at the end of the passed
188 * file name, minus header and trailing blank line.
189 */
a0d23834 190int
a13d1f28
KS
191swrite(str)
192 char str[];
193{
a13d1f28 194
2de8fc95 195 return save1(str, 1, "write", ignoreall);
a13d1f28
KS
196}
197
198/*
199 * Snarf the file from the end of the command line and
200 * return a pointer to it. If there is no file attached,
201 * just return NOSTR. Put a null in front of the file
202 * name so that the message list processing won't see it,
203 * unless the file name is the only thing on the line, in
204 * which case, return 0 in the reference flag variable.
205 */
206
207char *
208snarf(linebuf, flag)
209 char linebuf[];
210 int *flag;
211{
212 register char *cp;
213
214 *flag = 1;
215 cp = strlen(linebuf) + linebuf - 1;
216
217 /*
218 * Strip away trailing blanks.
219 */
220
828615a1 221 while (cp > linebuf && isspace(*cp))
a13d1f28
KS
222 cp--;
223 *++cp = 0;
224
225 /*
226 * Now search for the beginning of the file name.
227 */
228
828615a1 229 while (cp > linebuf && !isspace(*cp))
a13d1f28
KS
230 cp--;
231 if (*cp == '\0') {
232 printf("No file specified.\n");
233 return(NOSTR);
234 }
828615a1 235 if (isspace(*cp))
a13d1f28
KS
236 *cp++ = 0;
237 else
238 *flag = 0;
239 return(cp);
240}
241
242/*
243 * Delete messages.
244 */
a0d23834 245int
a13d1f28
KS
246delete(msgvec)
247 int msgvec[];
248{
2ee3bce2
EW
249 delm(msgvec);
250 return 0;
a13d1f28
KS
251}
252
253/*
254 * Delete messages, then type the new dot.
255 */
a0d23834 256int
a13d1f28
KS
257deltype(msgvec)
258 int msgvec[];
259{
260 int list[2];
12c002da 261 int lastdot;
a13d1f28 262
12c002da 263 lastdot = dot - &message[0] + 1;
a13d1f28 264 if (delm(msgvec) >= 0) {
470c33f3 265 list[0] = dot - &message[0] + 1;
12c002da 266 if (list[0] > lastdot) {
470c33f3 267 touch(dot);
12c002da
KS
268 list[1] = NULL;
269 return(type(list));
270 }
271 printf("At EOF\n");
2ee3bce2 272 } else
a13d1f28 273 printf("No more messages\n");
2ee3bce2 274 return(0);
a13d1f28
KS
275}
276
277/*
278 * Delete the indicated messages.
279 * Set dot to some nice place afterwards.
280 * Internal interface.
281 */
a0d23834 282int
a13d1f28
KS
283delm(msgvec)
284 int *msgvec;
285{
286 register struct message *mp;
470c33f3 287 register *ip;
a13d1f28
KS
288 int last;
289
290 last = NULL;
291 for (ip = msgvec; *ip != NULL; ip++) {
470c33f3
EW
292 mp = &message[*ip - 1];
293 touch(mp);
3c139aaf
KS
294 mp->m_flag |= MDELETED|MTOUCH;
295 mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
470c33f3 296 last = *ip;
a13d1f28
KS
297 }
298 if (last != NULL) {
299 dot = &message[last-1];
300 last = first(0, MDELETED);
301 if (last != NULL) {
302 dot = &message[last-1];
303 return(0);
304 }
305 else {
306 dot = &message[0];
307 return(-1);
308 }
309 }
310
311 /*
312 * Following can't happen -- it keeps lint happy
313 */
314
315 return(-1);
316}
317
318/*
319 * Undelete the indicated messages.
320 */
a0d23834 321int
a13d1f28
KS
322undelete(msgvec)
323 int *msgvec;
324{
325 register struct message *mp;
470c33f3
EW
326 register *ip;
327
328 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
329 mp = &message[*ip - 1];
330 touch(mp);
a13d1f28
KS
331 dot = mp;
332 mp->m_flag &= ~MDELETED;
333 }
0b2e8a24 334 return 0;
a13d1f28
KS
335}
336
337/*
338 * Interactively dump core on "core"
339 */
a0d23834 340int
a13d1f28
KS
341core()
342{
322c8626
EW
343 int pid;
344 extern union wait wait_status;
a13d1f28 345
322c8626
EW
346 switch (pid = vfork()) {
347 case -1:
a13d1f28
KS
348 perror("fork");
349 return(1);
322c8626 350 case 0:
a13d1f28
KS
351 abort();
352 _exit(1);
353 }
354 printf("Okie dokie");
355 fflush(stdout);
322c8626
EW
356 wait_child(pid);
357 if (wait_status.w_coredump)
358 printf(" -- Core dumped.\n");
a13d1f28 359 else
322c8626 360 printf(" -- Can't dump core.\n");
828615a1 361 return 0;
a13d1f28 362}
47e37b8b
KS
363
364/*
365 * Clobber as many bytes of stack as the user requests.
366 */
a0d23834 367int
47e37b8b
KS
368clobber(argv)
369 char **argv;
370{
371 register int times;
372
373 if (argv[0] == 0)
374 times = 1;
375 else
376 times = (atoi(argv[0]) + 511) / 512;
ab61fde0 377 clob1(times);
0b2e8a24 378 return 0;
47e37b8b
KS
379}
380
381/*
382 * Clobber the stack.
383 */
a0d23834 384void
ab61fde0 385clob1(n)
a0d23834 386 int n;
47e37b8b
KS
387{
388 char buf[512];
389 register char *cp;
390
391 if (n <= 0)
392 return;
393 for (cp = buf; cp < &buf[512]; *cp++ = 0xFF)
394 ;
ab61fde0 395 clob1(n - 1);
47e37b8b 396}
eed74ec1 397
46053c99
S
398/*
399 * Add the given header fields to the retained list.
400 * If no arguments, print the current list of retained fields.
401 */
a0d23834 402int
46053c99
S
403retfield(list)
404 char *list[];
405{
46053c99 406
887efe38 407 return ignore1(list, ignore + 1, "retained");
46053c99
S
408}
409
410/*
887efe38
EW
411 * Add the given header fields to the ignored list.
412 * If no arguments, print the current list of ignored fields.
46053c99 413 */
a0d23834 414int
887efe38
EW
415igfield(list)
416 char *list[];
46053c99 417{
46053c99 418
887efe38 419 return ignore1(list, ignore, "ignored");
46053c99
S
420}
421
a0d23834 422int
887efe38
EW
423saveretfield(list)
424 char *list[];
425{
426
427 return ignore1(list, saveignore + 1, "retained");
428}
429
a0d23834 430int
887efe38 431saveigfield(list)
eed74ec1
KS
432 char *list[];
433{
887efe38
EW
434
435 return ignore1(list, saveignore, "ignored");
436}
437
a0d23834 438int
887efe38
EW
439ignore1(list, tab, which)
440 char *list[];
441 struct ignoretab *tab;
442 char *which;
443{
eed74ec1
KS
444 char field[BUFSIZ];
445 register int h;
446 register struct ignore *igp;
447 char **ap;
448
470c33f3 449 if (*list == NOSTR)
887efe38 450 return igshow(tab, which);
eed74ec1
KS
451 for (ap = list; *ap != 0; ap++) {
452 istrcpy(field, *ap);
887efe38
EW
453 if (member(field, tab))
454 continue;
eed74ec1
KS
455 h = hash(field);
456 igp = (struct ignore *) calloc(1, sizeof (struct ignore));
828615a1
EW
457 igp->i_field = calloc((unsigned) strlen(field) + 1,
458 sizeof (char));
eed74ec1 459 strcpy(igp->i_field, field);
887efe38
EW
460 igp->i_link = tab->i_head[h];
461 tab->i_head[h] = igp;
462 tab->i_count++;
eed74ec1 463 }
887efe38 464 return 0;
eed74ec1
KS
465}
466
467/*
887efe38 468 * Print out all currently retained fields.
eed74ec1 469 */
a0d23834 470int
887efe38
EW
471igshow(tab, which)
472 struct ignoretab *tab;
473 char *which;
eed74ec1 474{
887efe38 475 register int h;
eed74ec1 476 struct ignore *igp;
93be40ac
KS
477 char **ap, **ring;
478 int igcomp();
eed74ec1 479
887efe38
EW
480 if (tab->i_count == 0) {
481 printf("No fields currently being %s.\n", which);
482 return 0;
93be40ac 483 }
887efe38 484 ring = (char **) salloc((tab->i_count + 1) * sizeof (char *));
93be40ac
KS
485 ap = ring;
486 for (h = 0; h < HSHSIZE; h++)
887efe38 487 for (igp = tab->i_head[h]; igp != 0; igp = igp->i_link)
93be40ac
KS
488 *ap++ = igp->i_field;
489 *ap = 0;
a0d23834 490 qsort(ring, tab->i_count, sizeof (char *), igcomp);
93be40ac
KS
491 for (ap = ring; *ap != 0; ap++)
492 printf("%s\n", *ap);
887efe38 493 return 0;
eed74ec1 494}
93be40ac
KS
495
496/*
497 * Compare two names for sorting ignored field list.
498 */
a0d23834 499int
93be40ac 500igcomp(l, r)
a0d23834 501 const void *l, *r;
93be40ac 502{
a0d23834 503 return (strcmp(*(char **)l, *(char **)r));
93be40ac 504}