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