2 ** folder(s).c -- set/list the current message and/or folder
3 ** -- push/pop a folder onto/from the folder stack
4 ** -- list the folder stack
6 ** This code is Copyright (c) 2002, 2008, by the authors of nmh. See the
7 ** COPYRIGHT file in the root directory of the nmh distribution for
8 ** complete copyright information.
12 #include <h/crawl_folders.h>
16 static struct swit switches[] = {
68 static int fshort = 0; /* output only folder names */
69 static int fcreat = 0; /* should we ask to create new folders? */
70 static int fpack = 0; /* are we packing the folder? */
71 static int fverb = 0; /* print actions taken while packing folder */
72 static int fheader = 0; /* should we output a header? */
73 static int frecurse = 0; /* recurse through subfolders */
74 static int ftotal = 0; /* should we output the totals? */
75 static int all = 0; /* should we output all folders */
77 static int total_folders = 0; /* total number of folders */
80 static char *stack = "Folder-Stack";
81 static char folder[BUFSIZ];
84 ** Structure to hold information about
85 ** folders as we scan them.
93 int others; /* others == 1 if other files in folder */
94 int error; /* error == 1 for unreadable folder */
98 ** Dynamically allocated space to hold
99 ** all the folder information.
101 static struct FolderInfo *fi;
102 static int maxFolderInfo;
107 static int get_folder_info(char *, char *);
108 static crawl_callback_t get_folder_info_callback;
109 static void print_folders(void);
110 static int sfold(struct msgs *, char *);
111 static void readonly_folders(void);
115 main(int argc, char **argv)
117 int printsw = 0, listsw = 0;
118 int pushsw = 0, popsw = 0;
119 char *cp, *dp, *msg = NULL, *argfolder = NULL;
120 char **ap, **argp, buf[BUFSIZ], **arguments;
123 setlocale(LC_ALL, "");
125 invo_name = r1bindex(argv[0], '/');
127 /* read user profile/context */
131 ** If program was invoked with name ending
132 ** in `s', then add switch `-all'.
134 if (argv[0][strlen(argv[0]) - 1] == 's')
137 arguments = getarguments(invo_name, argc, argv, 1);
140 while ((cp = *argp++)) {
142 switch (smatch(++cp, switches)) {
144 ambigsw(cp, switches);
147 adios(NULL, "-%s unknown", cp);
150 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
151 print_help(buf, switches, 1);
154 print_version(invo_name);
240 if (*cp == '+' || *cp == '@') {
242 adios(NULL, "only one folder at a time!");
244 argfolder = pluspath(cp);
247 adios(NULL, "only one (current) message at a time!");
253 if (!context_find("path"))
254 free(path("./", TFOLDER));
255 nmhdir = concat(m_maildir(""), "/", NULL);
258 ** If we aren't working with the folder stack
259 ** (-push, -pop, -list) then the default is to print.
261 if (pushsw == 0 && popsw == 0 && listsw == 0)
264 /* Pushing a folder onto the folder stack */
267 /* If no folder is given, the current folder and */
268 /* the top of the folder stack are swapped. */
269 if ((cp = context_find(stack))) {
271 ap = brkstring(dp, " ", "\n");
272 argfolder = getcpy(*ap++);
274 adios(NULL, "no other folder");
276 for (cp = getcpy(getfolder(1)); *ap; ap++)
277 cp = add(*ap, add(" ", cp));
279 context_replace(stack, cp); /* update folder stack */
281 /* update folder stack */
282 context_replace(stack, (cp = context_find (stack)) ?
283 concat(getfolder(1), " ", cp, NULL) :
284 getcpy(getfolder(1)));
288 /* Popping a folder off of the folder stack */
291 adios(NULL, "sorry, no folders allowed with -pop");
292 if ((cp = context_find(stack))) {
294 ap = brkstring(dp, " ", "\n");
295 argfolder = getcpy(*ap++);
297 adios(NULL, "folder stack empty");
300 /* if there's anything left in the stack */
303 cp = add(*ap, add(" ", cp));
304 context_replace(stack, cp); /* update folder stack */
306 /* delete folder stack entry from context */
311 if (pushsw || popsw) {
312 cp = m_maildir(argfolder);
313 if (access(cp, F_OK) == NOTOK)
314 adios(cp, "unable to find folder");
315 /* update current folder */
316 context_replace(pfolder, argfolder);
317 context_save(); /* save the context file */
321 /* Listing the folder stack */
323 printf("%s", argfolder ? argfolder : getfolder(1));
324 if ((cp = context_find(stack))) {
326 for (ap = brkstring(dp, " ", "\n"); *ap; ap++)
336 /* Allocate initial space to record folder information */
337 maxFolderInfo = CRAWL_NUMFOLDERS;
338 fi = mh_xmalloc(maxFolderInfo * sizeof(*fi));
343 if (all || ftotal > 0) {
345 ** If no folder is given, do them all
348 ** change directory to base of nmh directory for
351 if (chdir(nmhdir) == NOTOK)
352 adios(nmhdir, "unable to change directory to");
355 admonish(NULL, "no folder given for message %s", msg);
356 readonly_folders(); /* do any readonly folders */
357 strncpy(folder, (cp = context_find(pfolder)) ?
358 cp : "", sizeof(folder));
359 crawl_folders(".", get_folder_info_callback, NULL);
361 strncpy(folder, argfolder, sizeof(folder));
362 if (get_folder_info(argfolder, msg)) {
363 /* update current folder */
364 context_replace(pfolder, argfolder);
368 ** Since recurse wasn't done in get_folder_info(),
369 ** we still need to list all level-1 sub-folders.
372 crawl_folders(folder, get_folder_info_callback,
376 strncpy(folder, argfolder ? argfolder : getfolder (1),
380 ** Check if folder exists. If not, then see if
381 ** we should create it, or just exit.
383 create_folder(m_maildir(folder), fcreat, done);
385 if (get_folder_info(folder, msg) && argfolder) {
386 /* update current folder */
387 context_replace(pfolder, argfolder);
392 ** Print out folder information
402 get_folder_info_body(char *fold, char *msg, boolean *crawl_children)
405 struct msgs *mp = NULL;
410 ** if necessary, reallocate the space
411 ** for folder information
413 if (total_folders >= maxFolderInfo) {
414 maxFolderInfo += CRAWL_NUMFOLDERS;
415 fi = mh_xrealloc(fi, maxFolderInfo * sizeof(*fi));
426 if ((ftotal > 0) || !fshort || msg || fpack) {
428 ** create message structure and get folder info
430 if (!(mp = folder_read(fold))) {
431 admonish(NULL, "unable to read folder %s", fold);
435 /* set the current message */
436 if (msg && !sfold(mp, msg))
440 if (folder_pack(&mp, fverb) == -1)
442 seq_save(mp); /* synchronize the sequences */
443 context_save(); /* save the context file */
446 /* record info for this folder */
447 if ((ftotal > 0) || !fshort) {
448 fi[i].nummsg = mp->nummsg;
449 fi[i].curmsg = mp->curmsg;
450 fi[i].lowmsg = mp->lowmsg;
451 fi[i].hghmsg = mp->hghmsg;
452 fi[i].others = other_files(mp);
455 folder_free(mp); /* free folder/message structure */
458 *crawl_children = (frecurse && (fshort || fi[i].others)
459 && (fi[i].error == 0));
464 get_folder_info_callback(char *fold, void *baton)
466 boolean crawl_children;
467 get_folder_info_body(fold, NULL, &crawl_children);
469 return crawl_children;
473 get_folder_info(char *fold, char *msg)
475 boolean crawl_children;
478 retval = get_folder_info_body(fold, msg, &crawl_children);
480 if (crawl_children) {
481 crawl_folders(fold, get_folder_info_callback, NULL);
487 ** Print folder information
493 int i, len, hasempty = 0, curprinted;
494 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
495 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
496 int nummsgdigits, lowmsgdigits;
497 int hghmsgdigits, curmsgdigits;
498 char tmpname[BUFSIZ];
501 ** compute a few values needed to for
502 ** printing various fields
504 for (i = 0; i < total_folders; i++) {
505 /* length of folder name */
506 len = strlen(fi[i].name);
510 /* If folder has error, skip the rest */
514 /* calculate total number of messages */
515 total_msgs += fi[i].nummsg;
517 /* maximum number of messages */
518 if (fi[i].nummsg > maxnummsg)
519 maxnummsg = fi[i].nummsg;
521 /* maximum low message */
522 if (fi[i].lowmsg > maxlowmsg)
523 maxlowmsg = fi[i].lowmsg;
525 /* maximum high message */
526 if (fi[i].hghmsg > maxhghmsg)
527 maxhghmsg = fi[i].hghmsg;
529 /* maximum current message */
530 if (fi[i].curmsg >= fi[i].lowmsg &&
531 fi[i].curmsg <= fi[i].hghmsg &&
532 fi[i].curmsg > maxcurmsg)
533 maxcurmsg = fi[i].curmsg;
535 /* check for empty folders */
536 if (fi[i].nummsg == 0)
539 nummsgdigits = num_digits(maxnummsg);
540 lowmsgdigits = num_digits(maxlowmsg);
541 hghmsgdigits = num_digits(maxhghmsg);
542 curmsgdigits = num_digits(maxcurmsg);
544 if (hasempty && nummsgdigits < 2)
550 if (fheader > 0 || (all && !fshort && fheader >= 0))
551 printf("%-*s %*s %-*s; %-*s %*s\n",
553 nummsgdigits + 13, "# MESSAGES",
554 lowmsgdigits + hghmsgdigits + 4, " RANGE",
555 curmsgdigits + 4, "CUR",
559 ** Print folder information
561 if (all || fshort || ftotal < 1) {
562 for (i = 0; i < total_folders; i++) {
564 printf("%s\n", fi[i].name);
568 /* Add `+' to end of name, if folder is current */
569 if (strcmp(folder, fi[i].name))
570 snprintf(tmpname, sizeof(tmpname), "%s",
573 snprintf(tmpname, sizeof(tmpname), "%s+",
577 printf("%-*s is unreadable\n", maxlen+1,
582 printf("%-*s ", maxlen+1, tmpname);
584 curprinted = 0; /* remember if we print cur */
585 if (fi[i].nummsg == 0) {
586 printf("has %*s messages%*s", nummsgdigits, "no", fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
588 printf("has %*d message%s (%*d-%*d)",
589 nummsgdigits, fi[i].nummsg,
590 (fi[i].nummsg == 1) ?
592 lowmsgdigits, fi[i].lowmsg,
593 hghmsgdigits, fi[i].hghmsg);
594 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
596 printf("; cur=%*d", curmsgdigits,
602 printf(";%*s (others)", curprinted ?
603 0 : curmsgdigits + 6, "");
609 ** Print folder/message totals
611 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
614 printf("TOTAL = %d message%c in %d folder%s.\n",
615 total_msgs, total_msgs != 1 ? 's' : ' ',
616 total_folders, total_folders != 1 ? "s" : "");
623 ** Set the current message and sychronize sequences
627 sfold(struct msgs *mp, char *msg)
629 /* parse the message range/sequence/name and set SELECTED */
630 if (!m_convert(mp, msg))
633 if (mp->numsel > 1) {
634 admonish(NULL, "only one message at a time!");
637 seq_setprev(mp); /* set the previous-sequence */
638 seq_setcur(mp, mp->lowsel); /* set current message */
639 seq_save(mp); /* synchronize message sequences */
640 context_save(); /* save the context file */
647 ** Do the read only folders
651 readonly_folders(void)
655 register struct node *np;
657 snprintf(atrcur, sizeof(atrcur), "atr-%s-", current);
658 atrlen = strlen(atrcur);
660 for (np = m_defs; np; np = np->n_next)
661 if (ssequal(atrcur, np->n_name)
662 && !ssequal(nmhdir, np->n_name + atrlen))
663 get_folder_info(np->n_name + atrlen, NULL);