]> diplodocus.org Git - nmh/blob - uip/flist.c
fdcompare.c: Move interface to own file.
[nmh] / uip / flist.c
1 /* flist.c -- list nmh folders containing messages
2 * -- in a given sequence
3 *
4 * originally by
5 * David Nichols, Xerox-PARC, November, 1992
6 *
7 * Copyright (c) 1994 Xerox Corporation.
8 * Use and copying of this software and preparation of derivative works based
9 * upon this software are permitted. Any distribution of this software or
10 * derivative works must comply with all applicable United States export
11 * control laws. This software is made available AS IS, and Xerox Corporation
12 * makes no warranty about the software, its performance or its conformity to
13 * any specification.
14 */
15
16 #include "h/mh.h"
17 #include "sbr/folder_read.h"
18 #include "sbr/folder_free.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_replace.h"
21 #include "sbr/context_find.h"
22 #include "sbr/brkstring.h"
23 #include "sbr/ambigsw.h"
24 #include "sbr/path.h"
25 #include "sbr/print_version.h"
26 #include "sbr/print_help.h"
27 #include "sbr/seq_getnum.h"
28 #include "sbr/error.h"
29 #include "h/utils.h"
30 #include "h/done.h"
31 #include "sbr/m_maildir.h"
32
33 /*
34 * We allocate space to record the names of folders
35 * (foldersToDo array), this number of elements at a time.
36 */
37 #define MAXFOLDERS 100
38
39
40 #define FLIST_SWITCHES \
41 X("sequence name", 0, SEQSW) \
42 X("all", 0, ALLSW) \
43 X("noall", 0, NOALLSW) \
44 X("recurse", 0, RECURSE) \
45 X("norecurse", 0, NORECURSE) \
46 X("showzero", 0, SHOWZERO) \
47 X("noshowzero", 0, NOSHOWZERO) \
48 X("alpha", 0, ALPHASW) \
49 X("noalpha", 0, NOALPHASW) \
50 X("fast", 0, FASTSW) \
51 X("nofast", 0, NOFASTSW) \
52 X("total", -5, TOTALSW) \
53 X("nototal", -7, NOTOTALSW) \
54 X("version", 0, VERSIONSW) \
55 X("help", 0, HELPSW) \
56
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(FLIST);
59 #undef X
60
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(FLIST, switches);
63 #undef X
64
65 struct Folder {
66 char *name; /* name of folder */
67 int priority;
68 int error; /* error == 1 for unreadable folder */
69 int nMsgs; /* number of messages in folder */
70 ivector_t nSeq; /* number of messages in each sequence */
71 ivector_t private; /* is given sequence, public or private */
72 };
73
74 static struct Folder *orders = NULL;
75 static int nOrders = 0;
76 static int nOrdersAlloced = 0;
77 static struct Folder *folders = NULL;
78 static unsigned int nFolders = 0;
79 static int nFoldersAlloced = 0;
80
81 /* info on folders to search */
82 static char **foldersToDo;
83 static int numfolders;
84 static int maxfolders;
85
86 /* info on sequences to search for */
87 static svector_t sequencesToDo;
88
89 static bool all; /* scan all folders in top level? */
90 static bool alphaOrder; /* want alphabetical order only */
91 static bool recurse; /* show nested folders? */
92 static bool showzero = true; /* show folders even if no messages in seq? */
93 static bool Total = true; /* display info on number of messages in
94 * sequence found, and total num messages */
95
96 static char curfolder[BUFSIZ]; /* name of the current folder */
97 static char *nmhdir; /* base nmh mail directory */
98
99 /*
100 * Type for a compare function for qsort. This keeps
101 * the compiler happy.
102 */
103 typedef int (*qsort_comp) (const void *, const void *);
104
105 /*
106 * static prototypes
107 */
108 static int CompareFolders(struct Folder *, struct Folder *);
109 static void GetFolderOrder(void);
110 static void ScanFolders(void);
111 static int AddFolder(char *, int);
112 static void BuildFolderList(char *, int);
113 static void BuildFolderListRecurse(char *, struct stat *, int);
114 static void PrintFolders(void);
115 static void AllocFolders(struct Folder **, int *, int);
116 static int AssignPriority(char *);
117 static void do_readonly_folders(void);
118
119
120
121 int
122 main(int argc, char **argv)
123 {
124 char *cp, **argp;
125 char **arguments;
126 char buf[BUFSIZ];
127
128 if (nmh_init(argv[0], true, true)) { return 1; }
129
130 /*
131 * If program was invoked with name ending
132 * in `s', then add switch `-all'.
133 */
134 all = has_suffix_c(argv[0], 's');
135
136 arguments = getarguments (invo_name, argc, argv, 1);
137 argp = arguments;
138
139 /* allocate the initial space to record the folder names */
140 numfolders = 0;
141 maxfolders = MAXFOLDERS;
142 foldersToDo = mh_xmalloc ((size_t) (maxfolders * sizeof(*foldersToDo)));
143
144 /* no sequences yet */
145 sequencesToDo = svector_create (0);
146
147 /* parse arguments */
148 while ((cp = *argp++)) {
149 if (*cp == '-') {
150 switch (smatch(++cp, switches)) {
151 case AMBIGSW:
152 ambigsw(cp, switches);
153 done(1);
154 case UNKWNSW:
155 die("-%s unknown", cp);
156
157 case HELPSW:
158 snprintf(buf, sizeof(buf), "%s [+folder1 [+folder2 ...]][switches]",
159 invo_name);
160 print_help(buf, switches, 1);
161 done(0);
162 case VERSIONSW:
163 print_version(invo_name);
164 done (0);
165
166 case SEQSW:
167 if (!(cp = *argp++) || *cp == '-')
168 die("missing argument to %s", argp[-2]);
169
170 svector_push_back (sequencesToDo, cp);
171 break;
172
173 case ALLSW:
174 all = true;
175 break;
176 case NOALLSW:
177 all = false;
178 break;
179
180 case SHOWZERO:
181 showzero = true;
182 break;
183 case NOSHOWZERO:
184 showzero = false;
185 break;
186
187 case ALPHASW:
188 alphaOrder = true;
189 break;
190 case NOALPHASW:
191 alphaOrder = false;
192 break;
193
194 case NOFASTSW:
195 case TOTALSW:
196 Total = true;
197 break;
198
199 case FASTSW:
200 case NOTOTALSW:
201 Total = false;
202 break;
203
204 case RECURSE:
205 recurse = true;
206 break;
207 case NORECURSE:
208 recurse = false;
209 break;
210 }
211 } else {
212 /*
213 * Check if we need to allocate more space
214 * for folder names.
215 */
216 if (numfolders >= maxfolders) {
217 maxfolders += MAXFOLDERS;
218 foldersToDo = mh_xrealloc (foldersToDo,
219 (size_t) (maxfolders * sizeof(*foldersToDo)));
220 }
221 if (*cp == '+' || *cp == '@') {
222 foldersToDo[numfolders++] =
223 pluspath (cp);
224 } else
225 foldersToDo[numfolders++] = cp;
226 }
227 }
228
229 if (!context_find ("path"))
230 free (path ("./", TFOLDER));
231
232 /* get current folder */
233 strncpy (curfolder, getfolder(1), sizeof(curfolder));
234
235 /* get nmh base directory */
236 nmhdir = m_maildir ("");
237
238 /*
239 * If we didn't specify any sequences, we search
240 * for the "Unseen-Sequence" profile entry and use
241 * all the sequences defined there.
242 */
243 if (svector_size (sequencesToDo) == 0) {
244 if ((cp = context_find(usequence)) && *cp) {
245 char **ap, *dp;
246
247 dp = mh_xstrdup(cp);
248 ap = brkstring (dp, " ", "\n");
249 for (; ap && *ap; ap++)
250 svector_push_back (sequencesToDo, *ap);
251 } else {
252 die("no sequence specified or %s profile entry found", usequence);
253 }
254 }
255
256 GetFolderOrder();
257 ScanFolders();
258 qsort(folders, nFolders, sizeof(struct Folder), (qsort_comp) CompareFolders);
259 PrintFolders();
260 svector_free (sequencesToDo);
261 done (0);
262 return 1;
263 }
264
265 /*
266 * Read the Flist-Order profile entry to determine
267 * how to sort folders for output.
268 */
269
270 static void
271 GetFolderOrder(void)
272 {
273 char *p, *s;
274 int priority = 1;
275 struct Folder *o;
276
277 if (!(p = context_find("Flist-Order")))
278 return;
279 for (;;) {
280 while (isspace((unsigned char) *p))
281 ++p;
282 s = p;
283 while (*p && !isspace((unsigned char) *p))
284 ++p;
285 if (p != s) {
286 /* Found one. */
287 AllocFolders(&orders, &nOrdersAlloced, nOrders + 1);
288 o = &orders[nOrders++];
289 o->priority = priority++;
290 o->name = mh_xmalloc(p - s + 1);
291 strncpy(o->name, s, p - s);
292 o->name[p - s] = 0;
293 } else
294 break;
295 }
296 }
297
298 /*
299 * Scan all the necessary folders
300 */
301
302 static void
303 ScanFolders(void)
304 {
305 int i;
306
307 /*
308 * change directory to base of nmh directory
309 */
310 if (chdir (nmhdir) == NOTOK)
311 adios (nmhdir, "unable to change directory to");
312
313 if (numfolders > 0) {
314 /* Update context */
315 strncpy (curfolder, foldersToDo[numfolders - 1], sizeof(curfolder));
316 context_replace (pfolder, curfolder);/* update current folder */
317 context_save (); /* save the context file */
318
319 /*
320 * Scan each given folder. If -all is given,
321 * then also scan the 1st level subfolders under
322 * each given folder.
323 */
324 for (i = 0; i < numfolders; ++i)
325 BuildFolderList(foldersToDo[i], all ? 1 : 0);
326 } else {
327 if (all) {
328 /*
329 * Do the readonly folders
330 */
331 do_readonly_folders();
332
333 /*
334 * Now scan the entire nmh directory for folders
335 */
336 BuildFolderList(".", 0);
337 } else {
338 /*
339 * Else scan current folder
340 */
341 BuildFolderList(curfolder, 0);
342 }
343 }
344 }
345
346 /*
347 * Initial building of folder list for
348 * the top of our search tree.
349 */
350
351 static void
352 BuildFolderList(char *dirName, int searchdepth)
353 {
354 struct stat st;
355
356 /* Make sure we have a directory */
357 if ((stat(dirName, &st) == -1) || !S_ISDIR(st.st_mode))
358 return;
359
360 /*
361 * If base directory, don't add it to the
362 * folder list. We just recurse into it.
363 */
364 if (!strcmp (dirName, ".")) {
365 BuildFolderListRecurse (".", &st, 0);
366 return;
367 }
368
369 /*
370 * Add this folder to the list.
371 * If recursing and directory has subfolders,
372 * then build folder list for subfolders.
373 */
374 if (AddFolder(dirName, showzero) && (recurse || searchdepth) && st.st_nlink > 2)
375 BuildFolderListRecurse(dirName, &st, searchdepth - 1);
376 }
377
378 /*
379 * Recursive building of folder list
380 */
381
382 static void
383 BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
384 {
385 char *base, name[PATH_MAX];
386 char *n;
387 int nlinks;
388 DIR *dir;
389 struct dirent *dp;
390 struct stat st;
391
392 /*
393 * Keep track of number of directories we've seen so we can
394 * stop stat'ing entries in this directory once we've seen
395 * them all. This optimization will fail if you have extra
396 * directories beginning with ".", since we don't bother to
397 * stat them. But that shouldn't generally be a problem.
398 */
399 nlinks = s->st_nlink;
400 if (nlinks == 1) {
401 /* Disable the optimization under conditions where st_nlink
402 is set to 1. That happens on Cygwin, for example:
403 http://cygwin.com/ml/cygwin-apps/2008-08/msg00264.html */
404 nlinks = INT_MAX;
405 }
406
407 if (!(dir = opendir(dirName)))
408 adios(dirName, "can't open directory");
409
410 /*
411 * A hack so that we don't see a
412 * leading "./" in folder names.
413 */
414 base = strcmp (dirName, ".") ? dirName : dirName + 1;
415
416 while (nlinks && (dp = readdir(dir))) {
417 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) {
418 nlinks--;
419 continue;
420 }
421 if (dp->d_name[0] == '.')
422 continue;
423 /* Check to see if the name of the file is a number
424 * if it is, we assume it's a mail file and skip it
425 */
426 for (n = dp->d_name; isdigit((unsigned char)*n); n++);
427 if (!*n)
428 continue;
429 strncpy (name, base, sizeof(name) - 2);
430 if (*base)
431 strcat(name, "/");
432 strncat(name, dp->d_name, sizeof(name) - strlen(name) - 1);
433 if ((stat(name, &st) != -1) && S_ISDIR(st.st_mode)) {
434 /*
435 * Check if this was really a symbolic link pointing
436 * to a directory. If not, then decrement link count.
437 */
438 if (lstat (name, &st) == -1)
439 nlinks--;
440 /* Add this folder to the list */
441 if (AddFolder(name, showzero) &&
442 (recurse || searchdepth) && st.st_nlink > 2)
443 BuildFolderListRecurse(name, &st, searchdepth - 1);
444 }
445 }
446 closedir(dir);
447 }
448
449 /*
450 * Add this folder to our list, counting the total number of
451 * messages and the number of messages in each sequence.
452 */
453
454 static int
455 AddFolder(char *name, int force)
456 {
457 unsigned int i;
458 int msgnum;
459 bool nonzero;
460 ivector_t seqnum = ivector_create (0), nSeq = ivector_create (0);
461 struct Folder *f;
462 struct msgs *mp;
463 char *cp;
464
465 /* Read folder and create message structure */
466 if (!(mp = folder_read (name, 0))) {
467 /* Oops, error occurred. Record it and continue. */
468 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
469 f = &folders[nFolders++];
470 f->name = mh_xstrdup(name);
471 f->error = 1;
472 f->priority = AssignPriority(f->name);
473 return 0;
474 }
475
476 for (i = 0; i < svector_size (sequencesToDo); i++) {
477 /* Convert sequences to their sequence numbers */
478 if ((cp = svector_at (sequencesToDo, i)))
479 ivector_push_back (seqnum, seq_getnum(mp, cp));
480 else
481 ivector_push_back (seqnum, -1);
482
483 /* Now count messages in this sequence */
484 ivector_push_back (nSeq, 0);
485 if (mp->nummsg > 0 && ivector_at (seqnum, i) != -1) {
486 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
487 if (in_sequence(mp, ivector_at (seqnum, i), msgnum))
488 (*ivector_atp (nSeq, i))++;
489 }
490 }
491 }
492
493 /* Check if any of the sequence checks were nonzero */
494 nonzero = false;
495 for (i = 0; i < svector_size (sequencesToDo); i++) {
496 if (ivector_at (nSeq, i) > 0) {
497 nonzero = true;
498 break;
499 }
500 }
501
502 if (nonzero || force) {
503 /* save general folder information */
504 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
505 f = &folders[nFolders++];
506 f->name = mh_xstrdup(name);
507 f->nMsgs = mp->nummsg;
508 f->nSeq = ivector_create (0);
509 f->private = ivector_create (0);
510 f->error = 0;
511 f->priority = AssignPriority(f->name);
512
513 /* record the sequence information */
514 for (i = 0; i < svector_size (sequencesToDo); i++) {
515 *ivector_atp (f->nSeq, i) = ivector_at (nSeq, i);
516 ivector_push_back (f->private,
517 ivector_at (seqnum, i) != -1
518 ? is_seq_private(mp, ivector_at (seqnum, i))
519 : 0);
520 }
521 }
522
523 ivector_free (nSeq);
524 ivector_free (seqnum);
525 folder_free (mp); /* free folder/message structure */
526 return 1;
527 }
528
529 /*
530 * Print the folder/sequence information
531 */
532
533 static void
534 PrintFolders(void)
535 {
536 char tmpname[BUFSIZ];
537 unsigned int i, j, len;
538 bool has_private = false;
539 unsigned int maxfolderlen = 0, maxseqlen = 0;
540 int maxnum = 0, maxseq = 0;
541
542 if (!Total) {
543 for (i = 0; i < nFolders; i++)
544 puts(folders[i].name);
545 return;
546 }
547
548 /*
549 * Find the width we need for various fields
550 */
551 for (i = 0; i < nFolders; ++i) {
552 /* find the length of longest folder name */
553 len = strlen(folders[i].name);
554 if (len > maxfolderlen)
555 maxfolderlen = len;
556
557 /* If folder had error, skip the rest */
558 if (folders[i].error)
559 continue;
560
561 /* find the maximum total messages */
562 if (folders[i].nMsgs > maxnum)
563 maxnum = folders[i].nMsgs;
564
565 for (j = 0; j < svector_size (sequencesToDo); j++) {
566 /* find maximum width of sequence name */
567 len = strlen (svector_at (sequencesToDo, j));
568 if ((ivector_at (folders[i].nSeq, j) > 0 || showzero) &&
569 (len > maxseqlen))
570 maxseqlen = len;
571
572 /* find the maximum number of messages in sequence */
573 if (ivector_at (folders[i].nSeq, j) > maxseq)
574 maxseq = ivector_at (folders[i].nSeq, j);
575
576 /* check if this sequence is private in any of the folders */
577 if (ivector_at (folders[i].private, j))
578 has_private = true;
579 }
580 }
581
582 /* Now print all the folder/sequence information */
583 for (i = 0; i < nFolders; i++) {
584 for (j = 0; j < svector_size (sequencesToDo); j++) {
585 if (ivector_at (folders[i].nSeq, j) > 0 || showzero) {
586 /* Add `+' to end of name of current folder */
587 if (strcmp(curfolder, folders[i].name))
588 snprintf(tmpname, sizeof(tmpname), "%s", folders[i].name);
589 else
590 snprintf(tmpname, sizeof(tmpname), "%s+", folders[i].name);
591
592 if (folders[i].error) {
593 printf("%-*s is unreadable\n", maxfolderlen+1, tmpname);
594 continue;
595 }
596
597 printf("%-*s has %*d in sequence %-*s%s; out of %*d\n",
598 maxfolderlen+1, tmpname,
599 num_digits(maxseq), ivector_at (folders[i].nSeq, j),
600 maxseqlen, svector_at (sequencesToDo, j),
601 !has_private ? "" : ivector_at (folders[i].private, j)
602 ? " (private)" : " ",
603 num_digits(maxnum), folders[i].nMsgs);
604 }
605 }
606 }
607 }
608
609 /*
610 * Put them in priority order.
611 */
612
613 static int
614 CompareFolders(struct Folder *f1, struct Folder *f2)
615 {
616 if (!alphaOrder && f1->priority != f2->priority)
617 return f1->priority - f2->priority;
618 return strcmp(f1->name, f2->name);
619 }
620
621 /*
622 * Make sure we have at least n folders allocated.
623 */
624
625 static void
626 AllocFolders(struct Folder **f, int *nfa, int n)
627 {
628 if (n <= *nfa)
629 return;
630 if (*f == NULL) {
631 *nfa = 10;
632 *f = mh_xmalloc (*nfa * (sizeof(struct Folder)));
633 } else {
634 *nfa *= 2;
635 *f = mh_xrealloc (*f, *nfa * (sizeof(struct Folder)));
636 }
637 }
638
639 /*
640 * Return the priority for a name. The highest comes from an exact match.
641 * After that, the longest match (then first) assigns the priority.
642 */
643 static int
644 AssignPriority(char *name)
645 {
646 int i, ol, nl;
647 int best = nOrders;
648 int bestLen = 0;
649 struct Folder *o;
650
651 nl = strlen(name);
652 for (i = 0; i < nOrders; ++i) {
653 o = &orders[i];
654 if (!strcmp(name, o->name))
655 return o->priority;
656 ol = strlen(o->name);
657 if (nl < ol - 1)
658 continue;
659 if (ol < bestLen)
660 continue;
661 if (o->name[0] == '*' && !strcmp(o->name + 1, name + (nl - ol + 1))) {
662 best = o->priority;
663 bestLen = ol;
664 } else if (o->name[ol - 1] == '*' && strncmp(o->name, name, ol - 1) == 0) {
665 best = o->priority;
666 bestLen = ol;
667 }
668 }
669 return best;
670 }
671
672 /*
673 * Do the read only folders
674 */
675
676 static void
677 do_readonly_folders (void)
678 {
679 int atrlen;
680 char atrcur[BUFSIZ];
681 struct node *np;
682
683 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
684 atrlen = strlen (atrcur);
685
686 for (np = m_defs; np; np = np->n_next)
687 if (ssequal (atrcur, np->n_name)
688 && !ssequal (nmhdir, np->n_name + atrlen))
689 BuildFolderList (np->n_name + atrlen, 0);
690 }