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