]>
diplodocus.org Git - nmh/blob - uip/flist.c
5ea877d8391148710ffce79e339d45a8c8115189
1 /* flist.c -- list nmh folders containing messages
2 * -- in a given sequence
5 * David Nichols, Xerox-PARC, November, 1992
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
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"
26 #include "sbr/print_version.h"
27 #include "sbr/print_help.h"
28 #include "sbr/seq_getnum.h"
29 #include "sbr/error.h"
32 #include "sbr/m_maildir.h"
35 * We allocate space to record the names of folders
36 * (foldersToDo array), this number of elements at a time.
38 #define MAXFOLDERS 100
41 #define FLIST_SWITCHES \
42 X("sequence name", 0, SEQSW) \
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) \
58 #define X(sw, minchars, id) id,
59 DEFINE_SWITCH_ENUM(FLIST
);
62 #define X(sw, minchars, id) { sw, minchars, id },
63 DEFINE_SWITCH_ARRAY(FLIST
, switches
);
67 char *name
; /* name of folder */
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 */
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;
82 /* info on folders to search */
83 static char **foldersToDo
;
84 static int numfolders
;
85 static int maxfolders
;
87 /* info on sequences to search for */
88 static svector_t sequencesToDo
;
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 */
97 static char curfolder
[BUFSIZ
]; /* name of the current folder */
98 static char *nmhdir
; /* base nmh mail directory */
101 * Type for a compare function for qsort. This keeps
102 * the compiler happy.
104 typedef int (*qsort_comp
) (const void *, const void *);
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);
123 main(int argc
, char **argv
)
129 if (nmh_init(argv
[0], true, true)) { return 1; }
132 * If program was invoked with name ending
133 * in `s', then add switch `-all'.
135 all
= has_suffix_c(argv
[0], 's');
137 arguments
= getarguments (invo_name
, argc
, argv
, 1);
140 /* allocate the initial space to record the folder names */
142 maxfolders
= MAXFOLDERS
;
143 foldersToDo
= mh_xmalloc ((size_t) (maxfolders
* sizeof(*foldersToDo
)));
145 /* no sequences yet */
146 sequencesToDo
= svector_create (0);
148 /* parse arguments */
149 while ((cp
= *argp
++)) {
151 switch (smatch(++cp
, switches
)) {
153 ambigsw(cp
, switches
);
156 die("-%s unknown", cp
);
159 snprintf(buf
, sizeof(buf
), "%s [+folder1 [+folder2 ...]][switches]",
161 print_help(buf
, switches
, 1);
164 print_version(invo_name
);
168 if (!(cp
= *argp
++) || *cp
== '-')
169 die("missing argument to %s", argp
[-2]);
171 svector_push_back (sequencesToDo
, cp
);
214 * Check if we need to allocate more space
217 if (numfolders
>= maxfolders
) {
218 maxfolders
+= MAXFOLDERS
;
219 foldersToDo
= mh_xrealloc (foldersToDo
,
220 (size_t) (maxfolders
* sizeof(*foldersToDo
)));
222 if (*cp
== '+' || *cp
== '@') {
223 foldersToDo
[numfolders
++] =
226 foldersToDo
[numfolders
++] = cp
;
230 if (!context_find ("path"))
231 free (path ("./", TFOLDER
));
233 /* get current folder */
234 strncpy (curfolder
, getfolder(1), sizeof(curfolder
));
236 /* get nmh base directory */
237 nmhdir
= m_maildir ("");
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.
244 if (svector_size (sequencesToDo
) == 0) {
245 if ((cp
= context_find(usequence
)) && *cp
) {
249 ap
= brkstring (dp
, " ", "\n");
250 for (; ap
&& *ap
; ap
++)
251 svector_push_back (sequencesToDo
, *ap
);
253 die("no sequence specified or %s profile entry found", usequence
);
259 qsort(folders
, nFolders
, sizeof(struct Folder
), (qsort_comp
) CompareFolders
);
261 svector_free (sequencesToDo
);
267 * Read the Flist-Order profile entry to determine
268 * how to sort folders for output.
278 if (!(p
= context_find("Flist-Order")))
281 while (isspace((unsigned char) *p
))
284 while (*p
&& !isspace((unsigned char) *p
))
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
);
300 * Scan all the necessary folders
309 * change directory to base of nmh directory
311 if (chdir (nmhdir
) == NOTOK
)
312 adios (nmhdir
, "unable to change directory to");
314 if (numfolders
> 0) {
316 strncpy (curfolder
, foldersToDo
[numfolders
- 1], sizeof(curfolder
));
317 context_replace (pfolder
, curfolder
);/* update current folder */
318 context_save (); /* save the context file */
321 * Scan each given folder. If -all is given,
322 * then also scan the 1st level subfolders under
325 for (i
= 0; i
< numfolders
; ++i
)
326 BuildFolderList(foldersToDo
[i
], all
? 1 : 0);
330 * Do the readonly folders
332 do_readonly_folders();
335 * Now scan the entire nmh directory for folders
337 BuildFolderList(".", 0);
340 * Else scan current folder
342 BuildFolderList(curfolder
, 0);
348 * Initial building of folder list for
349 * the top of our search tree.
353 BuildFolderList(char *dirName
, int searchdepth
)
357 /* Make sure we have a directory */
358 if ((stat(dirName
, &st
) == -1) || !S_ISDIR(st
.st_mode
))
362 * If base directory, don't add it to the
363 * folder list. We just recurse into it.
365 if (!strcmp (dirName
, ".")) {
366 BuildFolderListRecurse (".", &st
, 0);
371 * Add this folder to the list.
372 * If recursing and directory has subfolders,
373 * then build folder list for subfolders.
375 if (AddFolder(dirName
, showzero
) && (recurse
|| searchdepth
) && st
.st_nlink
> 2)
376 BuildFolderListRecurse(dirName
, &st
, searchdepth
- 1);
380 * Recursive building of folder list
384 BuildFolderListRecurse(char *dirName
, struct stat
*s
, int searchdepth
)
386 char *base
, name
[PATH_MAX
];
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.
400 nlinks
= s
->st_nlink
;
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 */
408 if (!(dir
= opendir(dirName
)))
409 adios(dirName
, "can't open directory");
412 * A hack so that we don't see a
413 * leading "./" in folder names.
415 base
= strcmp (dirName
, ".") ? dirName
: dirName
+ 1;
417 while (nlinks
&& (dp
= readdir(dir
))) {
418 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, "..")) {
422 if (dp
->d_name
[0] == '.')
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
427 for (n
= dp
->d_name
; isdigit((unsigned char)*n
); n
++);
430 strncpy (name
, base
, sizeof(name
) - 2);
433 strncat(name
, dp
->d_name
, sizeof(name
) - strlen(name
) - 1);
434 if ((stat(name
, &st
) != -1) && S_ISDIR(st
.st_mode
)) {
436 * Check if this was really a symbolic link pointing
437 * to a directory. If not, then decrement link count.
439 if (lstat (name
, &st
) == -1)
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);
451 * Add this folder to our list, counting the total number of
452 * messages and the number of messages in each sequence.
456 AddFolder(char *name
, int force
)
461 ivector_t seqnum
= ivector_create (0), nSeq
= ivector_create (0);
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
);
473 f
->priority
= AssignPriority(f
->name
);
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
));
482 ivector_push_back (seqnum
, -1);
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
))++;
494 /* Check if any of the sequence checks were nonzero */
496 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
497 if (ivector_at (nSeq
, i
) > 0) {
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);
512 f
->priority
= AssignPriority(f
->name
);
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
))
525 ivector_free (seqnum
);
526 folder_free (mp
); /* free folder/message structure */
531 * Print the folder/sequence information
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;
544 for (i
= 0; i
< nFolders
; i
++)
545 puts(folders
[i
].name
);
550 * Find the width we need for various fields
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
)
558 /* If folder had error, skip the rest */
559 if (folders
[i
].error
)
562 /* find the maximum total messages */
563 if (folders
[i
].nMsgs
> maxnum
)
564 maxnum
= folders
[i
].nMsgs
;
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
) &&
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
);
577 /* check if this sequence is private in any of the folders */
578 if (ivector_at (folders
[i
].private, j
))
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
);
591 snprintf(tmpname
, sizeof(tmpname
), "%s+", folders
[i
].name
);
593 if (folders
[i
].error
) {
594 printf("%-*s is unreadable\n", maxfolderlen
+1, tmpname
);
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
);
611 * Put them in priority order.
615 CompareFolders(struct Folder
*f1
, struct Folder
*f2
)
617 if (!alphaOrder
&& f1
->priority
!= f2
->priority
)
618 return f1
->priority
- f2
->priority
;
619 return strcmp(f1
->name
, f2
->name
);
623 * Make sure we have at least n folders allocated.
627 AllocFolders(struct Folder
**f
, int *nfa
, int n
)
633 *f
= mh_xmalloc (*nfa
* (sizeof(struct Folder
)));
636 *f
= mh_xrealloc (*f
, *nfa
* (sizeof(struct Folder
)));
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.
645 AssignPriority(char *name
)
653 for (i
= 0; i
< nOrders
; ++i
) {
655 if (!strcmp(name
, o
->name
))
657 ol
= strlen(o
->name
);
662 if (o
->name
[0] == '*' && !strcmp(o
->name
+ 1, name
+ (nl
- ol
+ 1))) {
665 } else if (o
->name
[ol
- 1] == '*' && strncmp(o
->name
, name
, ol
- 1) == 0) {
674 * Do the read only folders
678 do_readonly_folders (void)
684 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
685 atrlen
= strlen (atrcur
);
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);