]>
diplodocus.org Git - nmh/blob - uip/flist.c
1584b0c47a03f494a70cd249ee56624da4502b3e
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/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"
23 #include "sbr/print_version.h"
24 #include "sbr/print_help.h"
25 #include "sbr/seq_getnum.h"
26 #include "sbr/error.h"
29 #include "sbr/m_maildir.h"
32 * We allocate space to record the names of folders
33 * (foldersToDo array), this number of elements at a time.
35 #define MAXFOLDERS 100
38 #define FLIST_SWITCHES \
39 X("sequence name", 0, SEQSW) \
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) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(FLIST
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(FLIST
, switches
);
64 char *name
; /* name of folder */
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 */
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;
79 /* info on folders to search */
80 static char **foldersToDo
;
81 static int numfolders
;
82 static int maxfolders
;
84 /* info on sequences to search for */
85 static svector_t sequencesToDo
;
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 */
94 static char curfolder
[BUFSIZ
]; /* name of the current folder */
95 static char *nmhdir
; /* base nmh mail directory */
98 * Type for a compare function for qsort. This keeps
101 typedef int (*qsort_comp
) (const void *, const void *);
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);
120 main(int argc
, char **argv
)
126 if (nmh_init(argv
[0], true, true)) { return 1; }
129 * If program was invoked with name ending
130 * in `s', then add switch `-all'.
132 all
= has_suffix_c(argv
[0], 's');
134 arguments
= getarguments (invo_name
, argc
, argv
, 1);
137 /* allocate the initial space to record the folder names */
139 maxfolders
= MAXFOLDERS
;
140 foldersToDo
= mh_xmalloc ((size_t) (maxfolders
* sizeof(*foldersToDo
)));
142 /* no sequences yet */
143 sequencesToDo
= svector_create (0);
145 /* parse arguments */
146 while ((cp
= *argp
++)) {
148 switch (smatch(++cp
, switches
)) {
150 ambigsw(cp
, switches
);
153 die("-%s unknown", cp
);
156 snprintf(buf
, sizeof(buf
), "%s [+folder1 [+folder2 ...]][switches]",
158 print_help(buf
, switches
, 1);
161 print_version(invo_name
);
165 if (!(cp
= *argp
++) || *cp
== '-')
166 die("missing argument to %s", argp
[-2]);
168 svector_push_back (sequencesToDo
, cp
);
211 * Check if we need to allocate more space
214 if (numfolders
>= maxfolders
) {
215 maxfolders
+= MAXFOLDERS
;
216 foldersToDo
= mh_xrealloc (foldersToDo
,
217 (size_t) (maxfolders
* sizeof(*foldersToDo
)));
219 if (*cp
== '+' || *cp
== '@') {
220 foldersToDo
[numfolders
++] =
223 foldersToDo
[numfolders
++] = cp
;
227 if (!context_find ("path"))
228 free (path ("./", TFOLDER
));
230 /* get current folder */
231 strncpy (curfolder
, getfolder(1), sizeof(curfolder
));
233 /* get nmh base directory */
234 nmhdir
= m_maildir ("");
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.
241 if (svector_size (sequencesToDo
) == 0) {
242 if ((cp
= context_find(usequence
)) && *cp
) {
246 ap
= brkstring (dp
, " ", "\n");
247 for (; ap
&& *ap
; ap
++)
248 svector_push_back (sequencesToDo
, *ap
);
250 die("no sequence specified or %s profile entry found", usequence
);
256 qsort(folders
, nFolders
, sizeof(struct Folder
), (qsort_comp
) CompareFolders
);
258 svector_free (sequencesToDo
);
264 * Read the Flist-Order profile entry to determine
265 * how to sort folders for output.
275 if (!(p
= context_find("Flist-Order")))
278 while (isspace((unsigned char) *p
))
281 while (*p
&& !isspace((unsigned char) *p
))
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
);
297 * Scan all the necessary folders
306 * change directory to base of nmh directory
308 if (chdir (nmhdir
) == NOTOK
)
309 adios (nmhdir
, "unable to change directory to");
311 if (numfolders
> 0) {
313 strncpy (curfolder
, foldersToDo
[numfolders
- 1], sizeof(curfolder
));
314 context_replace (pfolder
, curfolder
);/* update current folder */
315 context_save (); /* save the context file */
318 * Scan each given folder. If -all is given,
319 * then also scan the 1st level subfolders under
322 for (i
= 0; i
< numfolders
; ++i
)
323 BuildFolderList(foldersToDo
[i
], all
? 1 : 0);
327 * Do the readonly folders
329 do_readonly_folders();
332 * Now scan the entire nmh directory for folders
334 BuildFolderList(".", 0);
337 * Else scan current folder
339 BuildFolderList(curfolder
, 0);
345 * Initial building of folder list for
346 * the top of our search tree.
350 BuildFolderList(char *dirName
, int searchdepth
)
354 /* Make sure we have a directory */
355 if ((stat(dirName
, &st
) == -1) || !S_ISDIR(st
.st_mode
))
359 * If base directory, don't add it to the
360 * folder list. We just recurse into it.
362 if (!strcmp (dirName
, ".")) {
363 BuildFolderListRecurse (".", &st
, 0);
368 * Add this folder to the list.
369 * If recursing and directory has subfolders,
370 * then build folder list for subfolders.
372 if (AddFolder(dirName
, showzero
) && (recurse
|| searchdepth
) && st
.st_nlink
> 2)
373 BuildFolderListRecurse(dirName
, &st
, searchdepth
- 1);
377 * Recursive building of folder list
381 BuildFolderListRecurse(char *dirName
, struct stat
*s
, int searchdepth
)
383 char *base
, name
[PATH_MAX
];
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.
397 nlinks
= s
->st_nlink
;
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 */
405 if (!(dir
= opendir(dirName
)))
406 adios(dirName
, "can't open directory");
409 * A hack so that we don't see a
410 * leading "./" in folder names.
412 base
= strcmp (dirName
, ".") ? dirName
: dirName
+ 1;
414 while (nlinks
&& (dp
= readdir(dir
))) {
415 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, "..")) {
419 if (dp
->d_name
[0] == '.')
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
424 for (n
= dp
->d_name
; isdigit((unsigned char)*n
); n
++);
427 strncpy (name
, base
, sizeof(name
) - 2);
430 strncat(name
, dp
->d_name
, sizeof(name
) - strlen(name
) - 1);
431 if ((stat(name
, &st
) != -1) && S_ISDIR(st
.st_mode
)) {
433 * Check if this was really a symbolic link pointing
434 * to a directory. If not, then decrement link count.
436 if (lstat (name
, &st
) == -1)
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);
448 * Add this folder to our list, counting the total number of
449 * messages and the number of messages in each sequence.
453 AddFolder(char *name
, int force
)
458 ivector_t seqnum
= ivector_create (0), nSeq
= ivector_create (0);
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
);
470 f
->priority
= AssignPriority(f
->name
);
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
));
479 ivector_push_back (seqnum
, -1);
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
))++;
491 /* Check if any of the sequence checks were nonzero */
493 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
494 if (ivector_at (nSeq
, i
) > 0) {
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);
509 f
->priority
= AssignPriority(f
->name
);
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
))
522 ivector_free (seqnum
);
523 folder_free (mp
); /* free folder/message structure */
528 * Print the folder/sequence information
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;
541 for (i
= 0; i
< nFolders
; i
++)
542 puts(folders
[i
].name
);
547 * Find the width we need for various fields
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
)
555 /* If folder had error, skip the rest */
556 if (folders
[i
].error
)
559 /* find the maximum total messages */
560 if (folders
[i
].nMsgs
> maxnum
)
561 maxnum
= folders
[i
].nMsgs
;
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
) &&
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
);
574 /* check if this sequence is private in any of the folders */
575 if (ivector_at (folders
[i
].private, j
))
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
);
588 snprintf(tmpname
, sizeof(tmpname
), "%s+", folders
[i
].name
);
590 if (folders
[i
].error
) {
591 printf("%-*s is unreadable\n", maxfolderlen
+1, tmpname
);
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
);
608 * Put them in priority order.
612 CompareFolders(struct Folder
*f1
, struct Folder
*f2
)
614 if (!alphaOrder
&& f1
->priority
!= f2
->priority
)
615 return f1
->priority
- f2
->priority
;
616 return strcmp(f1
->name
, f2
->name
);
620 * Make sure we have at least n folders allocated.
624 AllocFolders(struct Folder
**f
, int *nfa
, int n
)
630 *f
= mh_xmalloc (*nfa
* (sizeof(struct Folder
)));
633 *f
= mh_xrealloc (*f
, *nfa
* (sizeof(struct Folder
)));
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.
642 AssignPriority(char *name
)
650 for (i
= 0; i
< nOrders
; ++i
) {
652 if (!strcmp(name
, o
->name
))
654 ol
= strlen(o
->name
);
659 if (o
->name
[0] == '*' && !strcmp(o
->name
+ 1, name
+ (nl
- ol
+ 1))) {
662 } else if (o
->name
[ol
- 1] == '*' && strncmp(o
->name
, name
, ol
- 1) == 0) {
671 * Do the read only folders
675 do_readonly_folders (void)
681 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
682 atrlen
= strlen (atrcur
);
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);