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