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