Debian Codegraph @

bin/mewl.c

raw symbols SCIP
1 /*
2 * mewl -- Message scanner for Mew 4 or later
3 *
4 * Author: Kazu Yamamoto <[email protected]>
5 * Created: Jul 7, 2000
6 *
7 * Code:
8 */
9
10 #include "mew.h"
11
12 private char version_message[] = "version 6.8 20180607 Kazu Yamamoto";
13
14 #ifdef HAVE_UNISTD_H
15 # include <sys/types.h>
16 # include <unistd.h>
17 #endif /* HAVE_UNISTD_H */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include <sys/stat.h>
24
25 #ifdef HAVE_DIRENT_H
26 # include <dirent.h>
27 # define NAMLEN(dirent) strlen((dirent)->d_name)
28 #else /* HAVE_DIRENT_H */
29 # define dirent direct
30 # define NAMLEN(dirent) (dirent)->d_namlen
31 # ifdef HAVE_SYS_NDIR_H
32 # include <sys/ndir.h>
33 # endif /* HAVE_SYS_NDIR_H */
34 # ifdef HAVE_SYS_DIR_H
35 # include <sys/dir.h>
36 # endif /* HAVE_SYS_DIR_H */
37 # ifdef HAVE_NDIR_H
38 # include <ndir.h>
39 # endif /* HAVE_NDIR_H */
40 #endif /* HAVE_DIRENT_H */
41 #ifdef HAVE_DIRECT_H
42 #include <direct.h>
43 #endif
44
45 /* for chdir() */
46 #ifdef HAVE_DIR_H
47 # include <dir.h>
48 #endif /* HAVE_DIR_H */
49
50 #ifdef HAVE_PWD_H
51 # include <pwd.h>
52 #endif /* HAVE_PWD_H */
53
54 /****************************************************************
55 *
56 * Macro, structure, and tables
57 *
58 */
59
60 #define MAILHOME "Mail"
61 #define CHDIR "CD:"
62 #define INBOX "+inbox"
63 #define STDINFILE "-"
64 #define HEAD "head"
65 #define FILE_SEPARATOR '\7'
66
67 /* 64k, now even 16k is not enough. */
68 #define MAX_HEADER 65536
69 #define MAX_FIELD_LEN 5
70 #define MAX_BODY_LEN 20
71 #define MAX_FOLDER 16
72 #define NUMBER_OF_FILE 128
73 #define MAX_ARGC 16
74
75 #ifdef PATH_MAX
76 # define MAX_FILE_NAME_LEN PATH_MAX
77 #else
78 # define MAX_FILE_NAME_LEN 1024
79 #endif
80
81 #define DEFAULT_FIELDS \
82 "Subject:,Date:,From:,To:,Cc:,Content-Type:,Content-Transfer-Encoding:,X-Mew-UIDL:,Message-ID:,In-Reply-To:,References:,X-Mew-Ref:"
83
84 #define REFERENCES "References:"
85
86 struct fld_rng {
87 char *folder;
88 unsigned int first;
89 unsigned int last;
90 unsigned int num;
91 };
92
93 private char Buf_header[MAX_HEADER];
94 private char Buf_filenm[MAX_FILE_NAME_LEN];
95 private char Buf_filenm2[MAX_FILE_NAME_LEN];
96 private char *Mail_home;
97 private char Current_folder[MAX_FILE_NAME_LEN];
98 private struct fld_rng Folders[MAX_FOLDER];
99 private char *Argvec[MAX_ARGC];
100
101 private char *Key_field = NULL;
102 private char *Exec_cmd = NULL;
103 private char *Exec_arg = NULL;
104 private char *Input_file = NULL;
105 private char *Suffix = NULL;
106 private char *Suffix_fmt = NULL;
107
108 private int All_fields = 0;
109 private int Field_len = MAX_FIELD_LEN;
110 private int Body_len = MAX_BODY_LEN;
111 private int PrintNumOfMsg = NO;
112 private int Suffix_len = 0;
113
114 private int no_fld_flag = 0;
115 private int default_fld_error_flag = 0;
116
117 /****************************************************************
118 *
119 * prototype
120 *
121 */
122
123 private void usage(const char *);
124 private void help(const char *);
125 private void version(const char *);
126 private void ch_home(void);
127 private void ch_mail_home(char *);
128 private void ch_folder(char *);
129 private char *nextline(char *, char *, int);
130 private char *nextfield(char *, char *, char **, char **, int);
131 private char *eoh(char *, char *);
132 private void init_fields(char *);
133 private void print_field(char *, int, char *, char *);
134 private void init_search(char *, int);
135 private char *mystrcasechr(char *, int);
136 private char *mystrcasestr(char *, char *);
137 private int print_for_scan(FILE *, char *, char *);
138 private int print_for_sort(FILE *, char *, char *);
139 private int print_for_pick(FILE *, char *, char *);
140 private int scan_numfile(char *, unsigned int, unsigned int);
141 private int scan_intcompare(unsigned int *, unsigned int *);
142 private int scan_folder(struct fld_rng *);
143 private void scan_init(unsigned int);
144 private FILE *scan_getfile(char **, char **);
145 private void exec_init(unsigned int);
146 private FILE *exec_getfile(char **, char **);
147 private void stdin_init(unsigned int);
148 private void set_fld_rng(struct fld_rng *, char *, char *);
149
150 /****************************************************************
151 *
152 * options and usages
153 *
154 */
155
156 private void
157 usage(const char *progname) {
158 fprintf(stderr, "Usage: %s [options] [folder [range]]\n", progname);
159 }
160
161 private const char *
162 help_message[] = {
163 " -a Print all necessary fields when picking.",
164 " -b <mdir> Mail home.",
165 " -c <bodylen> Max body length to dump.",
166 " -d <field> A field to be extracted.",
167 " -e <command> An external command to fetch mailbox.",
168 " -m <options> The second argument for the external command.",
169 " -f <fields> Fields to display.",
170 " -h Display this help message.",
171 " -i <file> A file name to be read.",
172 " -l <length> Field max length to dump. (0 means no limit)",
173 " -n Don't use fstat().",
174 " -p <pattern> Pick pattern.",
175 " -s <src> Message source: '+folder range'.",
176 " -w Wait for stdio input to synchronize.",
177 " -v Display the version.",
178 " -x Use this suffix.",
179 "",
180 "<range> = N | [start]-[end] | last:N",
181 "<pattern> = 'key=val' | 'key!=val' | '!<pattern>' | '(<pattern>)' ",
182 " | '<pattern>&<pattern>' ",
183 " | '<pattern>|<pattern>' ",
184 "",
185 "Default is mdir = ~/Mail,",
186 "length = 5, folder = +inbox, range = 0, bodylen = 20",
187 "field = Subject:,Date:,From:,To:,Cc:,Content-Type:,Content-Transfer-Encoding:,X-Mew-UIDL:,Message-ID:,In-Reply-To:,References:,X-Mew-Ref:",
188 NULL
189 };
190
191 private void
192 help(const char *progname) {
193 const char **p = help_message;
194
195 fprintf(stderr, "Help: %s\n\n", progname);
196 fprintf(stderr, " Message scanner.\n\n");
197 usage(progname);
198 while (*p) fprintf(stderr, "%s\n", *p++);
199 }
200
201 private void
202 version(const char *progname) {
203 fprintf(stderr, "%s %s\n", progname, version_message);
204 }
205
206 /****************************************************************
207 *
208 * folder function
209 *
210 */
211
212 private void
213 ch_home() {
214 char *home = getenv("HOME");
215 #ifdef HAVE_GETPWUID
216 if(home == NULL || home[0] == NUL) {
217 struct passwd *pw = getpwuid(getuid());
218 if(pw == NULL)
219 warn_exit("failed in getting home directory.");
220 home = pw->pw_dir;
221 }
222 #endif /* HAVE_GETPWUID */
223 if (home == NULL)
224 warn_exit("can't change directory to home.");
225 if (chdir(home) != 0)
226 warn_exit("can't change directory to %s.", home);
227 }
228
229 private void
230 ch_mail_home(char *dir) {
231 char *p = dir + 1, c;
232
233 if (dir == NULL) warn_exit("%s not exist.", dir);
234 c = *dir;
235 switch (c) {
236 case '~':
237 ch_home();
238 if (*p == NUL)
239 break;
240 if (*p++ != FILESEP)
241 warn_exit("can't change directory to %s.", dir);
242 if (*p == NUL)
243 break;
244 if (chdir(p) != 0)
245 warn_exit("can't change directory to %s.", dir);
246 break;
247 case FILESEP:
248 if (chdir(dir) != 0)
249 warn_exit("can't change directory to %s.", dir);
250 break;
251 default:
252 ch_home();
253 if (chdir(dir) != 0)
254 warn_exit("can't change directory to %s.", dir);
255 break;
256 }
257 }
258
259 private void
260 ch_folder(char *folder) {
261 char c = *folder, *p = folder + 1;
262 switch (c) {
263 case '+':
264 ch_mail_home(Mail_home);
265 if (*p == NUL)
266 break;
267 if (chdir(p) != 0) {
268 if( no_fld_flag != 0 )
269 default_fld_error_flag = 1;
270 else
271 warn_exit("can't change folder to %s.", folder);
272 }
273 break;
274 case '~':
275 ch_home();
276 if (*p == NUL)
277 break;
278 if (*p++ != FILESEP)
279 warn_exit("can't change folder to %s.", folder);
280 if (*p == NUL)
281 break;
282 if (chdir(p) != 0)
283 warn_exit("can't change folder to %s.", folder);
284 break;
285 default:
286 if (chdir(folder) != 0)
287 warn_exit("can't change folder to %s.", folder);
288 break;
289 }
290 }
291
292 /****************************************************************
293 *
294 * header sub functions
295 *
296 */
297
298 /*
299 * p: a point to be scanned in the buffer.
300 * lim: the end of the buffer.
301 * hdr_only: if YES, scan the header only (not the body).
302 *
303 * return value: the beginning point of the next line. NULL pointer is
304 * returned if either the buffer or the header ends.
305 */
306
307 private char *
308 nextline(char *p, char *lim, int hdr_only) {
309 if (p == lim)
310 return NULL;
311 if (*p == LF && hdr_only == YES)
312 return NULL;
313 while (p < lim) {
314 if (*p == LF)
315 break;
316 p++;
317 }
318 p++;
319 if (p < lim)
320 return p;
321 else {
322 *(lim - 1) = LF;
323 return NULL;
324 }
325 }
326
327 /*
328 * p: a point to be scanned in the buffer.
329 * lim: the end of the buffer.
330 * truncated_end: if the number of the lines of the field is over
331 * Field_len, it is truncated. And the end point is
332 * set to truncated_end.
333 * prev_beg: if not NULL pointer, the beginning point of the previous
334 * line of the last line is set to prev_beg. This is used
335 * for the References: field.
336 * hdr_only: if YES, scan the header only (not the body).
337 *
338 * return value: a point of the next field. NULL pointer is returned
339 * if either the buffer or the header ends.
340 */
341
342 private char *
343 nextfield(char *p, char *lim, char **truncated_end, char **prev_beg, int hdr_only) {
344 int i = 0;
345 char *q;
346 *truncated_end = NULL;
347
348 do {
349 q = p;
350 p = nextline(p, lim, hdr_only);
351 if (p == NULL) break;
352 i++;
353 if (Field_len != 0 && i == Field_len)
354 *truncated_end = p;
355 } while (*p == SP || *p == TAB);
356 if (prev_beg != NULL) *prev_beg = q;
357 return p;
358 }
359
360 private char *
361 eoh(char *p, char *lim) {
362 while (p < lim) {
363 if (*p == LF && p + 1 < lim && *(p + 1) == LF)
364 return p + 1;
365 p++;
366 }
367 return lim - 1;
368 }
369
370 /****************************************************************
371 *
372 * print sub-functions
373 *
374 */
375
376 private char **Scan_ctx_beg;
377 private char **Scan_ctx_end;
378 private char **Scan_ctx_fields;
379 private unsigned int *Scan_ctx_slen;
380 private int Scan_ctx_ref_idx = -1;
381 private unsigned int Scan_ctx_ptr_size;
382 private unsigned int Scan_ctx_fld_num;
383
384 private void
385 init_fields(char *fields) {
386 unsigned int i;
387 char *start, *p;
388
389 if (fields != NULL)
390 p = fields;
391 else
392 p = DEFAULT_FIELDS;
393 STRDUP(start, p);
394
395 for (i = 1, p = start; (p = strchr(p, ',')) != NULL; i++)
396 *p++ = NUL;
397
398 Scan_ctx_fld_num = i;
399
400 Scan_ctx_ptr_size = Scan_ctx_fld_num * sizeof(char *);
401
402 MALLOC(Scan_ctx_fields, Scan_ctx_ptr_size);
403 MALLOC(Scan_ctx_slen, Scan_ctx_fld_num * sizeof(int));
404 MALLOC(Scan_ctx_beg, Scan_ctx_ptr_size);
405 MALLOC(Scan_ctx_end, Scan_ctx_ptr_size);
406
407 i = 0;
408 p = start;
409 do {
410 Scan_ctx_fields[i] = p;
411 Scan_ctx_slen[i] = strlen(p);
412 if (strcmp(p, REFERENCES) == 0) Scan_ctx_ref_idx = i;
413 p = strchr(p, NUL);
414 p++;
415 i++;
416 } while (i < Scan_ctx_fld_num);
417 }
418
419 private void
420 print_field(char *buf, int lim, char *fname, char *fld) {
421 unsigned int i;
422 int match;
423 char *p, *q, *limp, *end, *prev, *body_beg = NULL;
424
425 memset(Scan_ctx_beg, 0, Scan_ctx_ptr_size);
426 memset(Scan_ctx_end, 0, Scan_ctx_ptr_size);
427
428 p = buf;
429 limp = p + lim;
430
431 for (;;) {
432 i = 0;
433 match = NO;
434 while (i < Scan_ctx_fld_num) {
435 if (strncasecmp(Scan_ctx_fields[i], p,
436 Scan_ctx_slen[i]) == 0) {
437 match = YES;
438 break;
439 }
440 i++;
441 }
442
443 if (match == YES) {
444 Scan_ctx_beg[i] = p;
445 p = nextfield(p, limp, &end, &prev, NO);
446
447 if (i == Scan_ctx_ref_idx) {
448 /* For the References: field, the bottom
449 part of its value is important. */
450 if (Scan_ctx_beg[i] == prev)
451 /* Skip the field name since
452 it will be printed below. */
453 Scan_ctx_beg[i] = Scan_ctx_beg[i]
454 + Scan_ctx_slen[i];
455 else
456 /* The last line only */
457 Scan_ctx_beg[i] = prev;
458 if (p == NULL)
459 Scan_ctx_end[i] = limp;
460 else
461 Scan_ctx_end[i] = p;
462
463 } else {
464 if (end != NULL)
465 Scan_ctx_end[i] = end;
466 else if (p == NULL)
467 Scan_ctx_end[i] = limp;
468 else
469 Scan_ctx_end[i] = p;
470 }
471 } else
472 /* not a target field. let's skip this */
473 p = nextfield(p, limp, &end, NULL, NO);
474
475 if (p == NULL) break; /* for */
476
477 if (*p == LF) {
478 /* header ends */
479 p++;
480 body_beg = p;
481 break; /* for */
482 }
483 }
484
485 if (fld != NULL)
486 printf("Folder: %s\n", fld);
487 printf("Filename: %s\n", fname);
488 for (i = 0; i < Scan_ctx_fld_num; i++)
489 if (Scan_ctx_beg[i] != NULL) {
490 if (i == Scan_ctx_ref_idx) printf("%s", REFERENCES);
491 p = Scan_ctx_beg[i];
492 while (p < Scan_ctx_end[i])
493 putchar(*p++);
494 }
495 printf("\n");
496 if (body_beg != NULL) {
497 p = body_beg;
498 i = 0;
499 while (i < Body_len) {
500 q = p;
501 p = nextline(q, limp, NO);
502 if (p == NULL) {
503 while (q < limp) {
504 putchar(*q);
505 q++;
506 }
507 break;
508 }
509 i++;
510 if (*q == '.') printf(".");
511 while (q < p) {
512 putchar(*q);
513 q++;
514 }
515 }
516 }
517 printf(".\n");
518 }
519
520 /****************************************************************
521 *
522 * String search
523 *
524 */
525
526 private char *Search_ctx_buf;
527 private int Search_ctx_lim;
528
529 private void
530 init_search(char *buf, int lim) {
531 Search_ctx_buf = buf;
532 Search_ctx_lim = lim;
533 }
534
535 private char *
536 mystrcasechr(char *s, int c) {
537 int lc = tolower(c);
538 int uc = toupper(c);
539
540 do {
541 if (*s == lc || *s == uc)
542 return s;
543 } while (*s++);
544 return NULL;
545 }
546
547 private char *
548 mystrcasestr(char *s1, char *s2) {
549 char *p;
550 unsigned int len = strlen(s2);
551
552 for (p = s1; (p = mystrcasechr(p, *s2)) != NULL; p++)
553 if (strncasecmp(p, s2, len) == 0)
554 return p;
555 return NULL;
556 }
557
558 public int
559 search_string(char *key, char *value, int case_sensitive) {
560 unsigned int len = strlen(key);
561 char *beg = NULL, *end = NULL;
562 char *p = Search_ctx_buf, *limp = p + Search_ctx_lim, tmp;
563
564 if (strcmp(HEAD, key) == 0) {
565 end = eoh(p, limp);
566 tmp = *end;
567 *end = NUL;
568 if (case_sensitive == YES) {
569 if (strstr(p, value) != NULL) {
570 *end = tmp;
571 return TRUE;
572 }
573 } else {
574 if (mystrcasestr(p, value) != NULL) {
575 *end = tmp;
576 return TRUE;
577 }
578 }
579 *end = tmp;
580 return FALSE;
581 }
582
583 for (;;) {
584 if (strncasecmp(key, p, len) == 0) {
585 beg = p + len;
586 p = nextfield(p, limp, &end, NULL, YES);
587 if (end != NULL)
588 end--; /* end == LF */
589 else if (p == NULL)
590 end = limp - 1; /* end == LF, see nextline */
591 else
592 end = p - 1; /* end == LF */
593 tmp = *end;
594 *end = NUL;
595 if (case_sensitive == YES) {
596 if (strstr(beg, value) != NULL) {
597 *end = tmp;
598 return TRUE;
599 }
600 } else {
601 if (mystrcasestr(beg, value) != NULL) {
602 *end = tmp;
603 return TRUE;
604 }
605 }
606 *end = tmp;
607 } else
608 p = nextfield(p, limp, &end, NULL, YES);
609 if (p == NULL)
610 break;
611 }
612 return FALSE;
613 }
614
615 /****************************************************************
616 *
617 * print functions
618 *
619 */
620
621 private int
622 print_for_scan(FILE *fp, char *fname, char *fld) {
623 int lim = fread(Buf_header, sizeof(char), sizeof(Buf_header), fp);
624 print_field(Buf_header, lim, fname, fld);
625 return TRUE;
626 }
627
628 private int
629 print_for_sort(FILE *fp, char *fname, char *fld) {
630 int lim = fread(Buf_header, sizeof(char), sizeof(Buf_header), fp);
631 unsigned int len = strlen(Key_field);
632 char *beg = NULL, *end = NULL;
633 char *p = Buf_header, *limp = p + lim;
634
635 for (;;) {
636 if (strncasecmp(Key_field, p, len) == 0) {
637 beg = p + len;
638 p = nextfield(p, limp, &end, NULL, YES);
639 if (end != NULL)
640 end--;
641 else if (p == NULL) {
642 end = limp;
643 } else
644 end = p - 1;
645 break;
646 }
647 p = nextfield(p, limp, &end, NULL, YES);
648 if (p == NULL)
649 break;
650 }
651
652 if (beg == NULL)
653 printf("%s: \n", fname);
654 else {
655 printf("%s:", fname);
656 p = beg;
657 while (p < end)
658 putchar(*p++);
659 printf("\n");
660 }
661
662 return FALSE;
663 }
664
665 private int
666 print_for_pick(FILE *fp, char *fname, char *fld) {
667 int lim = fread(Buf_header, sizeof(char), sizeof(Buf_header), fp);
668
669 init_search(Buf_header, lim);
670 if (pattern_match() == TRUE) {
671 if (All_fields > 0) {
672 print_field(Buf_header, lim, fname, fld);
673 return TRUE;
674 } else {
675 printf("%s\n", fname);
676 return FALSE;
677 }
678 }
679 return FALSE;
680 }
681
682 /****************************************************************
683 *
684 * scan functions
685 *
686 */
687
688 private unsigned int Scan_ctx_folders;
689 private unsigned int Scan_ctx_messages;
690 private unsigned int Scan_ctx_buf_size;
691 private unsigned int *Scan_ctx_buf;
692
693 private int
694 scan_numfile(char *s, unsigned int first, unsigned int last) {
695 int num = 0;
696 unsigned char c;
697
698 while ((c = *s) != NUL && isdigit(c)) {
699 num = num * 10 + c - '0';
700 s++;
701 }
702
703 if (num == 0)
704 return FALSE;
705 else if (*s == NUL)
706 ;
707 else if (strncmp(s, Suffix, Suffix_len) == 0 && s[Suffix_len] == NUL)
708 ;
709 else
710 return FALSE;
711
712 if (num >= first && (last == 0 || num <= last))
713 return TRUE;
714 else
715 return FALSE;
716 }
717
718 private int
719 scan_intcompare(unsigned int *i, unsigned int *j) {
720 if (*i > *j)
721 return 1;
722 if (*i < *j)
723 return -1;
724 return 0;
725 }
726
727 private int
728 scan_folder(struct fld_rng *fr) {
729 unsigned int num = 0;
730 unsigned int first = fr->first, last = fr->last;
731 DIR *dirp;
732 struct dirent *dp;
733 char *fname;
734 int i;
735
736 ch_folder(fr->folder);
737
738 if ((dirp = opendir(".")) == NULL)
739 warn_exit("can't open folder: %s.", fr->folder);
740
741 while ((dp = readdir(dirp)) != NULL) {
742 fname = dp->d_name;
743
744 if (scan_numfile(fname, first, last)) {
745 if (num == Scan_ctx_buf_size) {
746 Scan_ctx_buf_size += NUMBER_OF_FILE;
747 Scan_ctx_buf = (unsigned int *)realloc(Scan_ctx_buf, Scan_ctx_buf_size * sizeof(unsigned int));
748 }
749 *(Scan_ctx_buf + num) = atoi(fname);
750 num++;
751 }
752 }
753 closedir(dirp);
754
755 qsort(Scan_ctx_buf, num, sizeof(int),
756 (int (*)(const void *, const void *)) scan_intcompare);
757
758 if (fr->num > 0 && fr->num < num) {
759 for (i = 0; i < fr->num; i++)
760 Scan_ctx_buf[i] = Scan_ctx_buf[num - fr->num + i] ;
761 num = fr->num;
762 }
763
764 if (PrintNumOfMsg == YES && num > 0 && Scan_ctx_folders == 1) {
765 printf("NumOfMsg: %d\n", num);
766 fflush(stdout);
767 }
768 return num;
769 }
770
771 private void
772 scan_init(unsigned int folders) {
773 Scan_ctx_folders = folders;
774 Scan_ctx_messages = 0;
775 Scan_ctx_buf_size = NUMBER_OF_FILE;
776 MALLOC(Scan_ctx_buf, Scan_ctx_buf_size * sizeof(int));
777 }
778
779 private FILE *
780 scan_getfile(char **filename, char **foldername) {
781 static unsigned int i = 0, j = 0;
782 FILE *fp;
783
784 again:
785 if (i == Scan_ctx_messages) {
786 if (j < Scan_ctx_folders) {
787 Scan_ctx_messages = scan_folder(&Folders[j++]);
788 i = 0;
789 } else
790 return NULL;
791 }
792
793 *foldername = Folders[j - 1].folder;
794
795 while (i < Scan_ctx_messages) {
796 snprintf(Buf_filenm, sizeof(Buf_filenm), "%d", *(Scan_ctx_buf + i));
797 snprintf(Buf_filenm2, sizeof(Buf_filenm2), Suffix_fmt, "", *(Scan_ctx_buf + i));
798 fp = fopen(Buf_filenm2, FDREAD);
799 if (fp == NULL) fp = fopen(Buf_filenm, FDREAD);
800 if (fp != NULL) {
801 i++;
802 *filename = Buf_filenm;
803 return fp;
804 }
805 snprintf(Buf_filenm, sizeof(Buf_filenm), "0%d", *(Scan_ctx_buf + i));
806 snprintf(Buf_filenm2, sizeof(Buf_filenm2), Suffix_fmt, "0", *(Scan_ctx_buf + i));
807 fp = fopen(Buf_filenm2, FDREAD);
808 if (fp == NULL) fp = fopen(Buf_filenm, FDREAD);
809 if (fp != NULL) {
810 i++;
811 *filename = Buf_filenm;
812 return fp;
813 }
814 i++;
815 }
816 goto again;
817 }
818
819 /****************************************************************
820 *
821 * exec functions
822 *
823 */
824
825 #ifdef HAVE_FORK
826 private void
827 exec_init(unsigned int num) { /* num == 0 */
828 int childpid;
829 int pipes[2];
830
831 if (strlen(Folders[0].folder) >= sizeof(Current_folder))
832 warn_exit("folder name is too long.");
833 STRCPY(Current_folder, Folders[0].folder);
834 ch_folder(Current_folder);
835
836 if (pipe(pipes) != 0) warn_exit("can't open pipe.");
837 childpid = fork();
838 if (childpid < 0) warn_exit("can't fork.");
839
840 if (childpid == 0) { /* I'm the child. */
841 int i = 0;
842 int lim = MAX_ARGC - 2;
843 char *p;
844
845 close(WRITE);
846 dup(pipes[WRITE]);
847 close(pipes[READ]);
848
849 Argvec[i++] = Exec_cmd;
850 if (Exec_arg != NULL) {
851 p = Exec_arg;
852 Argvec[i++] = p;
853 while (i < lim) {
854 if ((p = strchr(p, SP)) == NULL) break;
855 *p++ = NUL;
856 Argvec[i++] = p;
857 }
858 }
859 Argvec[i++] = Current_folder;
860 Argvec[i++] = NULL;
861
862 execvp(Exec_cmd, Argvec);
863 warn_exit("'%s' command not found.", Exec_cmd);
864 }
865
866 /* I'm the parent. */
867 close(READ);
868 dup(pipes[READ]);
869 close(pipes[WRITE]);
870 }
871 #else /* HAVE_FORK */
872 private void
873 exec_init(unsigned int num) { /* num == 0 */
874 if (strlen(Folders[0].folder) >= sizeof(Current_folder))
875 warn_exit("folder name is too long.");
876 STRCPY(Current_folder, Folders[0].folder);
877 ch_folder(Current_folder);
878 }
879 #endif /* HAVE_FORK */
880
881 private FILE *
882 exec_getfile(char **filename, char **foldername) {
883 FILE *fp;
884 char *p, *q, *r;
885 int c;
886
887 *foldername = Current_folder;
888 while ((p = fgets(Buf_filenm, sizeof(Buf_filenm), stdin)) != NULL) {
889 if ((q = strchr(Buf_filenm, LF)) != NULL)
890 *q = NUL;
891 else {
892 Buf_filenm[MAX_FILE_NAME_LEN - 1] = NUL;
893 while ((c = getchar()) != LF && c != EOF) ;
894 }
895 if (STRCMP(p, CHDIR) == 0) {
896 no_fld_flag = 0;
897 default_fld_error_flag = 0;
898 p = p + strlen(CHDIR);
899 while (*p == SP || *p == TAB) p++;
900 if (strlen(p) >= sizeof(Current_folder))
901 warn_exit("folder name is too long.");
902 STRCPY(Current_folder, p);
903 ch_folder(Current_folder);
904 *foldername = Current_folder;
905 continue;
906 }
907 if(default_fld_error_flag != 0)
908 warn_exit("default folder is not exist.");
909 while (*p == SP || *p == TAB) p++;
910 if (isdigit((unsigned char)*p) == 0) continue;
911 *filename = p;
912 while (isdigit((unsigned char)*p)) p++;
913 if (STRCMP(p, Suffix)==0) {
914 r = p;
915 p = p + Suffix_len;
916 } else
917 r = NULL;
918 *p = NUL;
919 fp = fopen(*filename, FDREAD);
920 if (fp != NULL) {
921 if (r != NULL) *r = NUL;
922 return fp;
923 }
924 if (r != NULL) {
925 *r = NUL;
926 fp = fopen(*filename, FDREAD);
927 if (fp != NULL) return fp;
928 }
929 }
930 return NULL;
931 }
932
933 /****************************************************************
934 *
935 * stdin functions
936 *
937 */
938
939 private void
940 stdin_init(unsigned int num) { /* num == 0 */
941 if (strcmp(Input_file, STDINFILE) != 0)
942 if (freopen(Input_file, FDREAD, stdin) == NULL)
943 warn_exit("can't open %s.", Input_file);
944
945 if (strlen(Folders[0].folder) >= sizeof(Current_folder))
946 warn_exit("folder name is too long.");
947 STRCPY(Current_folder, Folders[0].folder);
948 ch_folder(Current_folder);
949 }
950
951 /****************************************************************
952 *
953 * parser for folder and range
954 *
955 */
956
957 private void
958 set_fld_rng(struct fld_rng *fr, char *arg1, char *arg2) {
959 char *folder = arg1;
960 char *range = NULL, *last = NULL;
961 char *p;
962
963 if (arg1 != NULL) range = strchr(arg1, SP);
964 if (range == NULL)
965 range = arg2;
966 else
967 *range++ = NUL;
968 if (range == NULL)
969 fr->first = fr->last = fr->num = 0;
970 else if (strncmp(range, "last:", 5) == 0) {
971 range += 5;
972 fr->first = fr->last = 0;
973 fr->num = atoi(range);
974 } else if ((last = strchr(range, '-')) == NULL) {
975 fr->first = fr->last = atoi(range);
976 fr->num = 0;
977 } else {
978 *last++ = NUL;
979 fr->first = atoi(range);
980 fr->last = atoi(last);
981 fr->num = 0;
982 }
983
984 if (folder == NULL) folder = INBOX;
985 STRDUP(fr->folder, folder);
986 while ((p = strchr(fr->folder, FILE_SEPARATOR)) != NULL) *p = SP;
987 }
988
989 /****************************************************************
990 *
991 * main
992 *
993 */
994
995 int
996 main(int argc, char **argv) {
997 int optc, rest;
998 int eflag = 0, nflag = 1, sflag = 0, wflag = 0;
999 int wait;
1000 unsigned int lim = 0;
1001 struct stat st;
1002 int (*func_print)(FILE *, char *, char *);
1003 void (*func_init)(unsigned int);
1004 FILE *(*func_getfile)(char **, char **);
1005 FILE *fp, *sync = NULL;
1006 char *filename, *foldername, *fields = NULL, *pattern = NULL;
1007 char *progname = getprognm(argv[0]);
1008 char *fld_rng;
1009
1010 warn_prog = progname;
1011 STRDUP(Mail_home, MAILHOME);
1012
1013 while ((optc = Getopt(argc, argv, "ab:c:d:e:f:hi:l:m:np:s:vwx:")) != EOF) {
1014 switch (optc) {
1015 case 'a':
1016 All_fields++;
1017 break;
1018 case 'b':
1019 STRDUP(Mail_home, Optarg);
1020 break;
1021 case 'c':
1022 Body_len = atoi(Optarg);
1023 break;
1024 case 'd':
1025 STRDUP(Key_field, Optarg);
1026 break;
1027 case 'e':
1028 STRDUP(Exec_cmd, Optarg);
1029 eflag++;
1030 break;
1031 case 'f':
1032 STRDUP(fields, Optarg);
1033 PrintNumOfMsg = YES;
1034 break;
1035 case 'h':
1036 help(progname);
1037 exit(EXIT_SUCCESS);
1038 break;
1039 case 'i':
1040 STRDUP(Input_file, Optarg);
1041 break;
1042 case 'l':
1043 Field_len = atoi(Optarg);
1044 break;
1045 case 'm':
1046 STRDUP(Exec_arg, Optarg);
1047 break;
1048 case 'n':
1049 nflag = 0;
1050 break;
1051 case 'p':
1052 STRDUP(pattern, Optarg);
1053 break;
1054 case 's':
1055 if (lim >= MAX_FOLDER)
1056 warn_exit("too many folders.");
1057 STRDUP(fld_rng, Optarg);
1058 set_fld_rng(&Folders[lim++], fld_rng, NULL);
1059 sflag++;
1060 break;
1061 case 'v':
1062 version(progname);
1063 exit(EXIT_SUCCESS);
1064 break;
1065 case 'w':
1066 wflag++;
1067 break;
1068 case 'x':
1069 STRDUP(Suffix, Optarg);
1070 break;
1071 default:
1072 usage(progname);
1073 exit(EXIT_FAILURE);
1074 }
1075 }
1076
1077 rest = argc - Optind;
1078
1079 /* Suffix */
1080 if (Suffix == NULL) STRDUP(Suffix, SUFFIX);
1081 Suffix_len = strlen(Suffix);
1082 MALLOC(Suffix_fmt, (Suffix_len + 10)); /* xxx */
1083 snprintf(Suffix_fmt, Suffix_len + 10, "%%s%%d%s", Suffix);
1084
1085 if (sflag == 0)
1086 switch (rest) { /* lim == 0 */
1087 case 0:
1088 set_fld_rng(&Folders[lim++], NULL, NULL);
1089 no_fld_flag = 1;
1090 break;
1091 case 1:
1092 set_fld_rng(&Folders[lim++], argv[Optind], NULL);
1093 break;
1094 case 2:
1095 set_fld_rng(&Folders[lim++], argv[Optind], argv[Optind + 1]);
1096 break;
1097 default:
1098 usage(progname);
1099 exit(EXIT_FAILURE);
1100 }
1101 else if (rest != 0)
1102 warn_exit("the 's' option can't co-exist with arguments.");
1103
1104 if (Input_file != NULL) {
1105 func_init = stdin_init;
1106 func_getfile = exec_getfile;
1107 } else if (eflag > 0) {
1108 func_init = exec_init;
1109 func_getfile = exec_getfile;
1110 } else {
1111 func_init = scan_init;
1112 func_getfile = scan_getfile;
1113 }
1114
1115 init_fields(fields); /* lengthy sometime */
1116
1117 if (pattern != NULL) {
1118 pattern_init(pattern);
1119 func_print = print_for_pick;
1120 PrintNumOfMsg = NO;
1121 } else if (Key_field != NULL)
1122 func_print = print_for_sort;
1123 else
1124 func_print = print_for_scan;
1125
1126 dup2(READ, SYNC);
1127 sync = fdopen(SYNC, FDREAD);
1128 (*func_init)(lim);
1129 while ((fp = (*func_getfile)(&filename, &foldername)) != NULL) {
1130 if (nflag != 0) {
1131 fstat(fileno(fp), &st);
1132 if (!(st.st_mode & S_IFREG)) {
1133 fclose(fp);
1134 continue;
1135 }
1136 }
1137 wait = (*func_print)(fp, filename, foldername);
1138 if (wait == TRUE && wflag > 0 && sync != NULL) {
1139 fflush(stdout);
1140 fgetc(sync);
1141 }
1142 fclose(fp);
1143 }
1144
1145 exit(EXIT_SUCCESS);
1146 }
1147
1148 /*
1149 * Copyright (C) 2000-2005 Mew developing team.
1150 * All rights reserved.
1151 *
1152 * Redistribution and use in source and binary forms, with or without
1153 * modification, are permitted provided that the following conditions
1154 * are met:
1155 *
1156 * 1. Redistributions of source code must retain the above copyright
1157 * notice, this list of conditions and the following disclaimer.
1158 * 2. Redistributions in binary form must reproduce the above copyright
1159 * notice, this list of conditions and the following disclaimer in the
1160 * documentation and/or other materials provided with the distribution.
1161 * 3. Neither the name of the team nor the names of its contributors
1162 * may be used to endorse or promote products derived from this software
1163 * without specific prior written permission.
1164 *
1165 * THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
1166 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1167 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1168 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
1169 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1170 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1171 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
1172 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
1173 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
1174 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
1175 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1176 */
1177
1178 /*
1179 * mewl.c ends here
1180 */