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