]>
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/folder_read.h"
18 #include "sbr/folder_free.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_replace.h"
21 #include "sbr/context_find.h"
22 #include "sbr/brkstring.h"
23 #include "sbr/ambigsw.h"
25 #include "sbr/print_version.h"
26 #include "sbr/print_help.h"
27 #include "sbr/seq_getnum.h"
28 #include "sbr/error.h"
31 #include "sbr/m_maildir.h"
34 * We allocate space to record the names of folders
35 * (foldersToDo array), this number of elements at a time.
37 #define MAXFOLDERS 100
40 #define FLIST_SWITCHES \
41 X("sequence name", 0, SEQSW) \
43 X("noall", 0, NOALLSW) \
44 X("recurse", 0, RECURSE) \
45 X("norecurse", 0, NORECURSE) \
46 X("showzero", 0, SHOWZERO) \
47 X("noshowzero", 0, NOSHOWZERO) \
48 X("alpha", 0, ALPHASW) \
49 X("noalpha", 0, NOALPHASW) \
50 X("fast", 0, FASTSW) \
51 X("nofast", 0, NOFASTSW) \
52 X("total", -5, TOTALSW) \
53 X("nototal", -7, NOTOTALSW) \
54 X("version", 0, VERSIONSW) \
55 X("help", 0, HELPSW) \
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(FLIST
);
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(FLIST
, switches
);
66 char *name
; /* name of folder */
68 int error
; /* error == 1 for unreadable folder */
69 int nMsgs
; /* number of messages in folder */
70 ivector_t nSeq
; /* number of messages in each sequence */
71 ivector_t
private; /* is given sequence, public or private */
74 static struct Folder
*orders
= NULL
;
75 static int nOrders
= 0;
76 static int nOrdersAlloced
= 0;
77 static struct Folder
*folders
= NULL
;
78 static unsigned int nFolders
= 0;
79 static int nFoldersAlloced
= 0;
81 /* info on folders to search */
82 static char **foldersToDo
;
83 static int numfolders
;
84 static int maxfolders
;
86 /* info on sequences to search for */
87 static svector_t sequencesToDo
;
89 static bool all
; /* scan all folders in top level? */
90 static bool alphaOrder
; /* want alphabetical order only */
91 static bool recurse
; /* show nested folders? */
92 static bool showzero
= true; /* show folders even if no messages in seq? */
93 static bool Total
= true; /* display info on number of messages in
94 * sequence found, and total num messages */
96 static char curfolder
[BUFSIZ
]; /* name of the current folder */
97 static char *nmhdir
; /* base nmh mail directory */
100 * Type for a compare function for qsort. This keeps
101 * the compiler happy.
103 typedef int (*qsort_comp
) (const void *, const void *);
108 static int CompareFolders(struct Folder
*, struct Folder
*);
109 static void GetFolderOrder(void);
110 static void ScanFolders(void);
111 static int AddFolder(char *, int);
112 static void BuildFolderList(char *, int);
113 static void BuildFolderListRecurse(char *, struct stat
*, int);
114 static void PrintFolders(void);
115 static void AllocFolders(struct Folder
**, int *, int);
116 static int AssignPriority(char *);
117 static void do_readonly_folders(void);
122 main(int argc
, char **argv
)
128 if (nmh_init(argv
[0], true, true)) { return 1; }
131 * If program was invoked with name ending
132 * in `s', then add switch `-all'.
134 all
= has_suffix_c(argv
[0], 's');
136 arguments
= getarguments (invo_name
, argc
, argv
, 1);
139 /* allocate the initial space to record the folder names */
141 maxfolders
= MAXFOLDERS
;
142 foldersToDo
= mh_xmalloc ((size_t) (maxfolders
* sizeof(*foldersToDo
)));
144 /* no sequences yet */
145 sequencesToDo
= svector_create (0);
147 /* parse arguments */
148 while ((cp
= *argp
++)) {
150 switch (smatch(++cp
, switches
)) {
152 ambigsw(cp
, switches
);
155 die("-%s unknown", cp
);
158 snprintf(buf
, sizeof(buf
), "%s [+folder1 [+folder2 ...]][switches]",
160 print_help(buf
, switches
, 1);
163 print_version(invo_name
);
167 if (!(cp
= *argp
++) || *cp
== '-')
168 die("missing argument to %s", argp
[-2]);
170 svector_push_back (sequencesToDo
, cp
);
213 * Check if we need to allocate more space
216 if (numfolders
>= maxfolders
) {
217 maxfolders
+= MAXFOLDERS
;
218 foldersToDo
= mh_xrealloc (foldersToDo
,
219 (size_t) (maxfolders
* sizeof(*foldersToDo
)));
221 if (*cp
== '+' || *cp
== '@') {
222 foldersToDo
[numfolders
++] =
225 foldersToDo
[numfolders
++] = cp
;
229 if (!context_find ("path"))
230 free (path ("./", TFOLDER
));
232 /* get current folder */
233 strncpy (curfolder
, getfolder(1), sizeof(curfolder
));
235 /* get nmh base directory */
236 nmhdir
= m_maildir ("");
239 * If we didn't specify any sequences, we search
240 * for the "Unseen-Sequence" profile entry and use
241 * all the sequences defined there.
243 if (svector_size (sequencesToDo
) == 0) {
244 if ((cp
= context_find(usequence
)) && *cp
) {
248 ap
= brkstring (dp
, " ", "\n");
249 for (; ap
&& *ap
; ap
++)
250 svector_push_back (sequencesToDo
, *ap
);
252 die("no sequence specified or %s profile entry found", usequence
);
258 qsort(folders
, nFolders
, sizeof(struct Folder
), (qsort_comp
) CompareFolders
);
260 svector_free (sequencesToDo
);
266 * Read the Flist-Order profile entry to determine
267 * how to sort folders for output.
277 if (!(p
= context_find("Flist-Order")))
280 while (isspace((unsigned char) *p
))
283 while (*p
&& !isspace((unsigned char) *p
))
287 AllocFolders(&orders
, &nOrdersAlloced
, nOrders
+ 1);
288 o
= &orders
[nOrders
++];
289 o
->priority
= priority
++;
290 o
->name
= mh_xmalloc(p
- s
+ 1);
291 strncpy(o
->name
, s
, p
- s
);
299 * Scan all the necessary folders
308 * change directory to base of nmh directory
310 if (chdir (nmhdir
) == NOTOK
)
311 adios (nmhdir
, "unable to change directory to");
313 if (numfolders
> 0) {
315 strncpy (curfolder
, foldersToDo
[numfolders
- 1], sizeof(curfolder
));
316 context_replace (pfolder
, curfolder
);/* update current folder */
317 context_save (); /* save the context file */
320 * Scan each given folder. If -all is given,
321 * then also scan the 1st level subfolders under
324 for (i
= 0; i
< numfolders
; ++i
)
325 BuildFolderList(foldersToDo
[i
], all
? 1 : 0);
329 * Do the readonly folders
331 do_readonly_folders();
334 * Now scan the entire nmh directory for folders
336 BuildFolderList(".", 0);
339 * Else scan current folder
341 BuildFolderList(curfolder
, 0);
347 * Initial building of folder list for
348 * the top of our search tree.
352 BuildFolderList(char *dirName
, int searchdepth
)
356 /* Make sure we have a directory */
357 if ((stat(dirName
, &st
) == -1) || !S_ISDIR(st
.st_mode
))
361 * If base directory, don't add it to the
362 * folder list. We just recurse into it.
364 if (!strcmp (dirName
, ".")) {
365 BuildFolderListRecurse (".", &st
, 0);
370 * Add this folder to the list.
371 * If recursing and directory has subfolders,
372 * then build folder list for subfolders.
374 if (AddFolder(dirName
, showzero
) && (recurse
|| searchdepth
) && st
.st_nlink
> 2)
375 BuildFolderListRecurse(dirName
, &st
, searchdepth
- 1);
379 * Recursive building of folder list
383 BuildFolderListRecurse(char *dirName
, struct stat
*s
, int searchdepth
)
385 char *base
, name
[PATH_MAX
];
393 * Keep track of number of directories we've seen so we can
394 * stop stat'ing entries in this directory once we've seen
395 * them all. This optimization will fail if you have extra
396 * directories beginning with ".", since we don't bother to
397 * stat them. But that shouldn't generally be a problem.
399 nlinks
= s
->st_nlink
;
401 /* Disable the optimization under conditions where st_nlink
402 is set to 1. That happens on Cygwin, for example:
403 http://cygwin.com/ml/cygwin-apps/2008-08/msg00264.html */
407 if (!(dir
= opendir(dirName
)))
408 adios(dirName
, "can't open directory");
411 * A hack so that we don't see a
412 * leading "./" in folder names.
414 base
= strcmp (dirName
, ".") ? dirName
: dirName
+ 1;
416 while (nlinks
&& (dp
= readdir(dir
))) {
417 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, "..")) {
421 if (dp
->d_name
[0] == '.')
423 /* Check to see if the name of the file is a number
424 * if it is, we assume it's a mail file and skip it
426 for (n
= dp
->d_name
; isdigit((unsigned char)*n
); n
++);
429 strncpy (name
, base
, sizeof(name
) - 2);
432 strncat(name
, dp
->d_name
, sizeof(name
) - strlen(name
) - 1);
433 if ((stat(name
, &st
) != -1) && S_ISDIR(st
.st_mode
)) {
435 * Check if this was really a symbolic link pointing
436 * to a directory. If not, then decrement link count.
438 if (lstat (name
, &st
) == -1)
440 /* Add this folder to the list */
441 if (AddFolder(name
, showzero
) &&
442 (recurse
|| searchdepth
) && st
.st_nlink
> 2)
443 BuildFolderListRecurse(name
, &st
, searchdepth
- 1);
450 * Add this folder to our list, counting the total number of
451 * messages and the number of messages in each sequence.
455 AddFolder(char *name
, int force
)
460 ivector_t seqnum
= ivector_create (0), nSeq
= ivector_create (0);
465 /* Read folder and create message structure */
466 if (!(mp
= folder_read (name
, 0))) {
467 /* Oops, error occurred. Record it and continue. */
468 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
469 f
= &folders
[nFolders
++];
470 f
->name
= mh_xstrdup(name
);
472 f
->priority
= AssignPriority(f
->name
);
476 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
477 /* Convert sequences to their sequence numbers */
478 if ((cp
= svector_at (sequencesToDo
, i
)))
479 ivector_push_back (seqnum
, seq_getnum(mp
, cp
));
481 ivector_push_back (seqnum
, -1);
483 /* Now count messages in this sequence */
484 ivector_push_back (nSeq
, 0);
485 if (mp
->nummsg
> 0 && ivector_at (seqnum
, i
) != -1) {
486 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
487 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
))
488 (*ivector_atp (nSeq
, i
))++;
493 /* Check if any of the sequence checks were nonzero */
495 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
496 if (ivector_at (nSeq
, i
) > 0) {
502 if (nonzero
|| force
) {
503 /* save general folder information */
504 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
505 f
= &folders
[nFolders
++];
506 f
->name
= mh_xstrdup(name
);
507 f
->nMsgs
= mp
->nummsg
;
508 f
->nSeq
= ivector_create (0);
509 f
->private = ivector_create (0);
511 f
->priority
= AssignPriority(f
->name
);
513 /* record the sequence information */
514 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
515 *ivector_atp (f
->nSeq
, i
) = ivector_at (nSeq
, i
);
516 ivector_push_back (f
->private,
517 ivector_at (seqnum
, i
) != -1
518 ? is_seq_private(mp
, ivector_at (seqnum
, i
))
524 ivector_free (seqnum
);
525 folder_free (mp
); /* free folder/message structure */
530 * Print the folder/sequence information
536 char tmpname
[BUFSIZ
];
537 unsigned int i
, j
, len
;
538 bool has_private
= false;
539 unsigned int maxfolderlen
= 0, maxseqlen
= 0;
540 int maxnum
= 0, maxseq
= 0;
543 for (i
= 0; i
< nFolders
; i
++)
544 puts(folders
[i
].name
);
549 * Find the width we need for various fields
551 for (i
= 0; i
< nFolders
; ++i
) {
552 /* find the length of longest folder name */
553 len
= strlen(folders
[i
].name
);
554 if (len
> maxfolderlen
)
557 /* If folder had error, skip the rest */
558 if (folders
[i
].error
)
561 /* find the maximum total messages */
562 if (folders
[i
].nMsgs
> maxnum
)
563 maxnum
= folders
[i
].nMsgs
;
565 for (j
= 0; j
< svector_size (sequencesToDo
); j
++) {
566 /* find maximum width of sequence name */
567 len
= strlen (svector_at (sequencesToDo
, j
));
568 if ((ivector_at (folders
[i
].nSeq
, j
) > 0 || showzero
) &&
572 /* find the maximum number of messages in sequence */
573 if (ivector_at (folders
[i
].nSeq
, j
) > maxseq
)
574 maxseq
= ivector_at (folders
[i
].nSeq
, j
);
576 /* check if this sequence is private in any of the folders */
577 if (ivector_at (folders
[i
].private, j
))
582 /* Now print all the folder/sequence information */
583 for (i
= 0; i
< nFolders
; i
++) {
584 for (j
= 0; j
< svector_size (sequencesToDo
); j
++) {
585 if (ivector_at (folders
[i
].nSeq
, j
) > 0 || showzero
) {
586 /* Add `+' to end of name of current folder */
587 if (strcmp(curfolder
, folders
[i
].name
))
588 snprintf(tmpname
, sizeof(tmpname
), "%s", folders
[i
].name
);
590 snprintf(tmpname
, sizeof(tmpname
), "%s+", folders
[i
].name
);
592 if (folders
[i
].error
) {
593 printf("%-*s is unreadable\n", maxfolderlen
+1, tmpname
);
597 printf("%-*s has %*d in sequence %-*s%s; out of %*d\n",
598 maxfolderlen
+1, tmpname
,
599 num_digits(maxseq
), ivector_at (folders
[i
].nSeq
, j
),
600 maxseqlen
, svector_at (sequencesToDo
, j
),
601 !has_private
? "" : ivector_at (folders
[i
].private, j
)
602 ? " (private)" : " ",
603 num_digits(maxnum
), folders
[i
].nMsgs
);
610 * Put them in priority order.
614 CompareFolders(struct Folder
*f1
, struct Folder
*f2
)
616 if (!alphaOrder
&& f1
->priority
!= f2
->priority
)
617 return f1
->priority
- f2
->priority
;
618 return strcmp(f1
->name
, f2
->name
);
622 * Make sure we have at least n folders allocated.
626 AllocFolders(struct Folder
**f
, int *nfa
, int n
)
632 *f
= mh_xmalloc (*nfa
* (sizeof(struct Folder
)));
635 *f
= mh_xrealloc (*f
, *nfa
* (sizeof(struct Folder
)));
640 * Return the priority for a name. The highest comes from an exact match.
641 * After that, the longest match (then first) assigns the priority.
644 AssignPriority(char *name
)
652 for (i
= 0; i
< nOrders
; ++i
) {
654 if (!strcmp(name
, o
->name
))
656 ol
= strlen(o
->name
);
661 if (o
->name
[0] == '*' && !strcmp(o
->name
+ 1, name
+ (nl
- ol
+ 1))) {
664 } else if (o
->name
[ol
- 1] == '*' && strncmp(o
->name
, name
, ol
- 1) == 0) {
673 * Do the read only folders
677 do_readonly_folders (void)
683 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
684 atrlen
= strlen (atrcur
);
686 for (np
= m_defs
; np
; np
= np
->n_next
)
687 if (ssequal (atrcur
, np
->n_name
)
688 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
689 BuildFolderList (np
->n_name
+ atrlen
, 0);