]>
diplodocus.org Git - nmh/blob - uip/flist.c
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/getarguments.h"
18 #include "sbr/smatch.h"
19 #include "sbr/ssequal.h"
20 #include "sbr/getfolder.h"
21 #include "sbr/folder_read.h"
22 #include "sbr/folder_free.h"
23 #include "sbr/context_save.h"
24 #include "sbr/context_replace.h"
25 #include "sbr/context_find.h"
26 #include "sbr/brkstring.h"
27 #include "sbr/ambigsw.h"
29 #include "sbr/print_version.h"
30 #include "sbr/print_help.h"
31 #include "sbr/seq_getnum.h"
32 #include "sbr/error.h"
35 #include "sbr/m_maildir.h"
38 * We allocate space to record the names of folders
39 * (foldersToDo array), this number of elements at a time.
41 #define MAXFOLDERS 100
44 #define FLIST_SWITCHES \
45 X("sequence name", 0, SEQSW) \
47 X("noall", 0, NOALLSW) \
48 X("recurse", 0, RECURSE) \
49 X("norecurse", 0, NORECURSE) \
50 X("showzero", 0, SHOWZERO) \
51 X("noshowzero", 0, NOSHOWZERO) \
52 X("alpha", 0, ALPHASW) \
53 X("noalpha", 0, NOALPHASW) \
54 X("fast", 0, FASTSW) \
55 X("nofast", 0, NOFASTSW) \
56 X("total", -5, TOTALSW) \
57 X("nototal", -7, NOTOTALSW) \
58 X("version", 0, VERSIONSW) \
59 X("help", 0, HELPSW) \
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(FLIST
);
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(FLIST
, switches
);
70 char *name
; /* name of folder */
72 int error
; /* error == 1 for unreadable folder */
73 int nMsgs
; /* number of messages in folder */
74 ivector_t nSeq
; /* number of messages in each sequence */
75 ivector_t
private; /* is given sequence, public or private */
78 static struct Folder
*orders
= NULL
;
79 static int nOrders
= 0;
80 static int nOrdersAlloced
= 0;
81 static struct Folder
*folders
= NULL
;
82 static unsigned int nFolders
= 0;
83 static int nFoldersAlloced
= 0;
85 /* info on folders to search */
86 static char **foldersToDo
;
87 static int numfolders
;
88 static int maxfolders
;
90 /* info on sequences to search for */
91 static svector_t sequencesToDo
;
93 static bool all
; /* scan all folders in top level? */
94 static bool alphaOrder
; /* want alphabetical order only */
95 static bool recurse
; /* show nested folders? */
96 static bool showzero
= true; /* show folders even if no messages in seq? */
97 static bool Total
= true; /* display info on number of messages in
98 * sequence found, and total num messages */
100 static char curfolder
[BUFSIZ
]; /* name of the current folder */
101 static char *nmhdir
; /* base nmh mail directory */
104 * Type for a compare function for qsort. This keeps
105 * the compiler happy.
107 typedef int (*qsort_comp
) (const void *, const void *);
112 static int CompareFolders(struct Folder
*, struct Folder
*);
113 static void GetFolderOrder(void);
114 static void ScanFolders(void);
115 static int AddFolder(char *, int);
116 static void BuildFolderList(char *, int);
117 static void BuildFolderListRecurse(char *, struct stat
*, int);
118 static void PrintFolders(void);
119 static void AllocFolders(struct Folder
**, int *, int);
120 static int AssignPriority(char *);
121 static void do_readonly_folders(void);
126 main(int argc
, char **argv
)
132 if (nmh_init(argv
[0], true, true)) { return 1; }
135 * If program was invoked with name ending
136 * in `s', then add switch `-all'.
138 all
= has_suffix_c(argv
[0], 's');
140 arguments
= getarguments (invo_name
, argc
, argv
, 1);
143 /* allocate the initial space to record the folder names */
145 maxfolders
= MAXFOLDERS
;
146 foldersToDo
= mh_xmalloc ((size_t) (maxfolders
* sizeof(*foldersToDo
)));
148 /* no sequences yet */
149 sequencesToDo
= svector_create (0);
151 /* parse arguments */
152 while ((cp
= *argp
++)) {
154 switch (smatch(++cp
, switches
)) {
156 ambigsw(cp
, switches
);
159 die("-%s unknown", cp
);
162 snprintf(buf
, sizeof(buf
), "%s [+folder1 [+folder2 ...]][switches]",
164 print_help(buf
, switches
, 1);
167 print_version(invo_name
);
171 if (!(cp
= *argp
++) || *cp
== '-')
172 die("missing argument to %s", argp
[-2]);
174 svector_push_back (sequencesToDo
, cp
);
217 * Check if we need to allocate more space
220 if (numfolders
>= maxfolders
) {
221 maxfolders
+= MAXFOLDERS
;
222 foldersToDo
= mh_xrealloc (foldersToDo
,
223 (size_t) (maxfolders
* sizeof(*foldersToDo
)));
225 if (*cp
== '+' || *cp
== '@') {
226 foldersToDo
[numfolders
++] =
229 foldersToDo
[numfolders
++] = cp
;
233 if (!context_find ("path"))
234 free (path ("./", TFOLDER
));
236 /* get current folder */
237 strncpy (curfolder
, getfolder(1), sizeof(curfolder
));
239 /* get nmh base directory */
240 nmhdir
= m_maildir ("");
243 * If we didn't specify any sequences, we search
244 * for the "Unseen-Sequence" profile entry and use
245 * all the sequences defined there.
247 if (svector_size (sequencesToDo
) == 0) {
248 if ((cp
= context_find(usequence
)) && *cp
) {
252 ap
= brkstring (dp
, " ", "\n");
253 for (; ap
&& *ap
; ap
++)
254 svector_push_back (sequencesToDo
, *ap
);
256 die("no sequence specified or %s profile entry found", usequence
);
262 qsort(folders
, nFolders
, sizeof(struct Folder
), (qsort_comp
) CompareFolders
);
264 svector_free (sequencesToDo
);
270 * Read the Flist-Order profile entry to determine
271 * how to sort folders for output.
281 if (!(p
= context_find("Flist-Order")))
284 while (isspace((unsigned char) *p
))
287 while (*p
&& !isspace((unsigned char) *p
))
291 AllocFolders(&orders
, &nOrdersAlloced
, nOrders
+ 1);
292 o
= &orders
[nOrders
++];
293 o
->priority
= priority
++;
294 o
->name
= mh_xmalloc(p
- s
+ 1);
295 strncpy(o
->name
, s
, p
- s
);
303 * Scan all the necessary folders
312 * change directory to base of nmh directory
314 if (chdir (nmhdir
) == NOTOK
)
315 adios (nmhdir
, "unable to change directory to");
317 if (numfolders
> 0) {
319 strncpy (curfolder
, foldersToDo
[numfolders
- 1], sizeof(curfolder
));
320 context_replace (pfolder
, curfolder
);/* update current folder */
321 context_save (); /* save the context file */
324 * Scan each given folder. If -all is given,
325 * then also scan the 1st level subfolders under
328 for (i
= 0; i
< numfolders
; ++i
)
329 BuildFolderList(foldersToDo
[i
], all
? 1 : 0);
333 * Do the readonly folders
335 do_readonly_folders();
338 * Now scan the entire nmh directory for folders
340 BuildFolderList(".", 0);
343 * Else scan current folder
345 BuildFolderList(curfolder
, 0);
351 * Initial building of folder list for
352 * the top of our search tree.
356 BuildFolderList(char *dirName
, int searchdepth
)
360 /* Make sure we have a directory */
361 if ((stat(dirName
, &st
) == -1) || !S_ISDIR(st
.st_mode
))
365 * If base directory, don't add it to the
366 * folder list. We just recurse into it.
368 if (!strcmp (dirName
, ".")) {
369 BuildFolderListRecurse (".", &st
, 0);
374 * Add this folder to the list.
375 * If recursing and directory has subfolders,
376 * then build folder list for subfolders.
378 if (AddFolder(dirName
, showzero
) && (recurse
|| searchdepth
) && st
.st_nlink
> 2)
379 BuildFolderListRecurse(dirName
, &st
, searchdepth
- 1);
383 * Recursive building of folder list
387 BuildFolderListRecurse(char *dirName
, struct stat
*s
, int searchdepth
)
389 char *base
, name
[PATH_MAX
];
397 * Keep track of number of directories we've seen so we can
398 * stop stat'ing entries in this directory once we've seen
399 * them all. This optimization will fail if you have extra
400 * directories beginning with ".", since we don't bother to
401 * stat them. But that shouldn't generally be a problem.
403 nlinks
= s
->st_nlink
;
405 /* Disable the optimization under conditions where st_nlink
406 is set to 1. That happens on Cygwin, for example:
407 http://cygwin.com/ml/cygwin-apps/2008-08/msg00264.html */
411 if (!(dir
= opendir(dirName
)))
412 adios(dirName
, "can't open directory");
415 * A hack so that we don't see a
416 * leading "./" in folder names.
418 base
= strcmp (dirName
, ".") ? dirName
: dirName
+ 1;
420 while (nlinks
&& (dp
= readdir(dir
))) {
421 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, "..")) {
425 if (dp
->d_name
[0] == '.')
427 /* Check to see if the name of the file is a number
428 * if it is, we assume it's a mail file and skip it
430 for (n
= dp
->d_name
; isdigit((unsigned char)*n
); n
++);
433 strncpy (name
, base
, sizeof(name
) - 2);
436 strncat(name
, dp
->d_name
, sizeof(name
) - strlen(name
) - 1);
437 if ((stat(name
, &st
) != -1) && S_ISDIR(st
.st_mode
)) {
439 * Check if this was really a symbolic link pointing
440 * to a directory. If not, then decrement link count.
442 if (lstat (name
, &st
) == -1)
444 /* Add this folder to the list */
445 if (AddFolder(name
, showzero
) &&
446 (recurse
|| searchdepth
) && st
.st_nlink
> 2)
447 BuildFolderListRecurse(name
, &st
, searchdepth
- 1);
454 * Add this folder to our list, counting the total number of
455 * messages and the number of messages in each sequence.
459 AddFolder(char *name
, int force
)
464 ivector_t seqnum
= ivector_create (0), nSeq
= ivector_create (0);
469 /* Read folder and create message structure */
470 if (!(mp
= folder_read (name
, 0))) {
471 /* Oops, error occurred. Record it and continue. */
472 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
473 f
= &folders
[nFolders
++];
474 f
->name
= mh_xstrdup(name
);
476 f
->priority
= AssignPriority(f
->name
);
480 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
481 /* Convert sequences to their sequence numbers */
482 if ((cp
= svector_at (sequencesToDo
, i
)))
483 ivector_push_back (seqnum
, seq_getnum(mp
, cp
));
485 ivector_push_back (seqnum
, -1);
487 /* Now count messages in this sequence */
488 ivector_push_back (nSeq
, 0);
489 if (mp
->nummsg
> 0 && ivector_at (seqnum
, i
) != -1) {
490 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
491 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
))
492 (*ivector_atp (nSeq
, i
))++;
497 /* Check if any of the sequence checks were nonzero */
499 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
500 if (ivector_at (nSeq
, i
) > 0) {
506 if (nonzero
|| force
) {
507 /* save general folder information */
508 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
509 f
= &folders
[nFolders
++];
510 f
->name
= mh_xstrdup(name
);
511 f
->nMsgs
= mp
->nummsg
;
512 f
->nSeq
= ivector_create (0);
513 f
->private = ivector_create (0);
515 f
->priority
= AssignPriority(f
->name
);
517 /* record the sequence information */
518 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
519 *ivector_atp (f
->nSeq
, i
) = ivector_at (nSeq
, i
);
520 ivector_push_back (f
->private,
521 ivector_at (seqnum
, i
) != -1
522 ? is_seq_private(mp
, ivector_at (seqnum
, i
))
528 ivector_free (seqnum
);
529 folder_free (mp
); /* free folder/message structure */
534 * Print the folder/sequence information
540 char tmpname
[BUFSIZ
];
541 unsigned int i
, j
, len
;
542 bool has_private
= false;
543 unsigned int maxfolderlen
= 0, maxseqlen
= 0;
544 int maxnum
= 0, maxseq
= 0;
547 for (i
= 0; i
< nFolders
; i
++)
548 puts(folders
[i
].name
);
553 * Find the width we need for various fields
555 for (i
= 0; i
< nFolders
; ++i
) {
556 /* find the length of longest folder name */
557 len
= strlen(folders
[i
].name
);
558 if (len
> maxfolderlen
)
561 /* If folder had error, skip the rest */
562 if (folders
[i
].error
)
565 /* find the maximum total messages */
566 if (folders
[i
].nMsgs
> maxnum
)
567 maxnum
= folders
[i
].nMsgs
;
569 for (j
= 0; j
< svector_size (sequencesToDo
); j
++) {
570 /* find maximum width of sequence name */
571 len
= strlen (svector_at (sequencesToDo
, j
));
572 if ((ivector_at (folders
[i
].nSeq
, j
) > 0 || showzero
) &&
576 /* find the maximum number of messages in sequence */
577 if (ivector_at (folders
[i
].nSeq
, j
) > maxseq
)
578 maxseq
= ivector_at (folders
[i
].nSeq
, j
);
580 /* check if this sequence is private in any of the folders */
581 if (ivector_at (folders
[i
].private, j
))
586 /* Now print all the folder/sequence information */
587 for (i
= 0; i
< nFolders
; i
++) {
588 for (j
= 0; j
< svector_size (sequencesToDo
); j
++) {
589 if (ivector_at (folders
[i
].nSeq
, j
) > 0 || showzero
) {
590 /* Add `+' to end of name of current folder */
591 if (strcmp(curfolder
, folders
[i
].name
))
592 snprintf(tmpname
, sizeof(tmpname
), "%s", folders
[i
].name
);
594 snprintf(tmpname
, sizeof(tmpname
), "%s+", folders
[i
].name
);
596 if (folders
[i
].error
) {
597 printf("%-*s is unreadable\n", maxfolderlen
+1, tmpname
);
601 printf("%-*s has %*d in sequence %-*s%s; out of %*d\n",
602 maxfolderlen
+1, tmpname
,
603 num_digits(maxseq
), ivector_at (folders
[i
].nSeq
, j
),
604 maxseqlen
, svector_at (sequencesToDo
, j
),
605 !has_private
? "" : ivector_at (folders
[i
].private, j
)
606 ? " (private)" : " ",
607 num_digits(maxnum
), folders
[i
].nMsgs
);
614 * Put them in priority order.
618 CompareFolders(struct Folder
*f1
, struct Folder
*f2
)
620 if (!alphaOrder
&& f1
->priority
!= f2
->priority
)
621 return f1
->priority
- f2
->priority
;
622 return strcmp(f1
->name
, f2
->name
);
626 * Make sure we have at least n folders allocated.
630 AllocFolders(struct Folder
**f
, int *nfa
, int n
)
636 *f
= mh_xmalloc (*nfa
* (sizeof(struct Folder
)));
639 *f
= mh_xrealloc (*f
, *nfa
* (sizeof(struct Folder
)));
644 * Return the priority for a name. The highest comes from an exact match.
645 * After that, the longest match (then first) assigns the priority.
648 AssignPriority(char *name
)
656 for (i
= 0; i
< nOrders
; ++i
) {
658 if (!strcmp(name
, o
->name
))
660 ol
= strlen(o
->name
);
665 if (o
->name
[0] == '*' && !strcmp(o
->name
+ 1, name
+ (nl
- ol
+ 1))) {
668 } else if (o
->name
[ol
- 1] == '*' && strncmp(o
->name
, name
, ol
- 1) == 0) {
677 * Do the read only folders
681 do_readonly_folders (void)
687 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
688 atrlen
= strlen (atrcur
);
690 for (np
= m_defs
; np
; np
= np
->n_next
)
691 if (ssequal (atrcur
, np
->n_name
)
692 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
693 BuildFolderList (np
->n_name
+ atrlen
, 0);