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