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