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);
112 static int folder_pack(struct msgs **, int);
116 main(int argc, char **argv)
118 int printsw = 0, listsw = 0;
119 int pushsw = 0, popsw = 0;
120 char *cp, *dp, *msg = NULL, *argfolder = NULL;
121 char **ap, **argp, buf[BUFSIZ], **arguments;
124 setlocale(LC_ALL, "");
126 invo_name = mhbasename(argv[0]);
128 /* read user profile/context */
132 ** If program was invoked with name ending
133 ** in `s', then add switch `-all'.
135 if (argv[0][strlen(argv[0]) - 1] == 's')
138 arguments = getarguments(invo_name, argc, argv, 1);
141 while ((cp = *argp++)) {
143 switch (smatch(++cp, switches)) {
145 ambigsw(cp, switches);
148 adios(NULL, "-%s unknown", cp);
151 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
152 print_help(buf, switches, 1);
155 print_version(invo_name);
241 if (*cp == '+' || *cp == '@') {
243 adios(NULL, "only one folder at a time!");
245 argfolder = getcpy(expandfol(cp));
248 adios(NULL, "only one (current) message at a time!");
254 nmhdir = concat(toabsdir("+"), "/", NULL);
257 ** If we aren't working with the folder stack
258 ** (-push, -pop, -list) then the default is to print.
260 if (pushsw == 0 && popsw == 0 && listsw == 0)
263 /* Pushing a folder onto the folder stack */
266 /* If no folder is given, the current folder and */
267 /* the top of the folder stack are swapped. */
268 if ((cp = context_find(stack))) {
270 ap = brkstring(dp, " ", "\n");
271 argfolder = getcpy(*ap++);
273 adios(NULL, "no other folder");
275 for (cp = getcpy(getcurfol()); *ap; ap++)
276 cp = add(*ap, add(" ", cp));
278 context_replace(stack, cp); /* update folder stack */
280 /* update folder stack */
281 context_replace(stack, (cp = context_find (stack)) ?
282 concat(getcurfol(), " ", cp, NULL) :
283 getcpy(getcurfol()));
287 /* Popping a folder off of the folder stack */
290 adios(NULL, "sorry, no folders allowed with -pop");
291 if ((cp = context_find(stack))) {
293 ap = brkstring(dp, " ", "\n");
294 argfolder = getcpy(*ap++);
296 adios(NULL, "folder stack empty");
299 /* if there's anything left in the stack */
302 cp = add(*ap, add(" ", cp));
303 context_replace(stack, cp); /* update folder stack */
305 /* delete folder stack entry from context */
310 if (pushsw || popsw) {
311 cp = toabsdir(argfolder);
312 if (access(cp, F_OK) == NOTOK)
313 adios(cp, "unable to find folder");
314 /* update current folder */
315 context_replace(curfolder, argfolder);
316 context_save(); /* save the context file */
320 /* Listing the folder stack */
322 printf("%s", argfolder ? argfolder : getcurfol());
323 if ((cp = context_find(stack))) {
325 for (ap = brkstring(dp, " ", "\n"); *ap; ap++)
335 /* Allocate initial space to record folder information */
336 maxFolderInfo = CRAWL_NUMFOLDERS;
337 fi = mh_xmalloc(maxFolderInfo * sizeof(*fi));
342 if (all || ftotal > 0) {
344 ** If no folder is given, do them all
347 ** change directory to base of nmh directory for
350 if (chdir(nmhdir) == NOTOK)
351 adios(nmhdir, "unable to change directory to");
354 admonish(NULL, "no folder given for message %s", msg);
355 readonly_folders(); /* do any readonly folders */
356 strncpy(folder, (cp = context_find(curfolder)) ?
357 cp : "", sizeof(folder));
358 crawl_folders(".", get_folder_info_callback, NULL);
360 strncpy(folder, argfolder, sizeof(folder));
361 if (get_folder_info(argfolder, msg)) {
362 /* update current folder */
363 context_replace(curfolder, argfolder);
367 ** Since recurse wasn't done in get_folder_info(),
368 ** we still need to list all level-1 sub-folders.
371 crawl_folders(folder, get_folder_info_callback,
375 strncpy(folder, argfolder ? argfolder : getcurfol(),
379 ** Check if folder exists. If not, then see if
380 ** we should create it, or just exit.
382 create_folder(toabsdir(folder), fcreat, done);
384 if (get_folder_info(folder, msg) && argfolder) {
385 /* update current folder */
386 context_replace(curfolder, argfolder);
391 ** Print out folder information
401 get_folder_info_body(char *fold, char *msg, boolean *crawl_children)
404 struct msgs *mp = NULL;
409 ** if necessary, reallocate the space
410 ** for folder information
412 if (total_folders >= maxFolderInfo) {
413 maxFolderInfo += CRAWL_NUMFOLDERS;
414 fi = mh_xrealloc(fi, maxFolderInfo * sizeof(*fi));
425 if ((ftotal > 0) || !fshort || msg || fpack) {
427 ** create message structure and get folder info
429 if (!(mp = folder_read(fold))) {
430 admonish(NULL, "unable to read folder %s", fold);
434 /* set the current message */
435 if (msg && !sfold(mp, msg))
439 if (folder_pack(&mp, fverb) == -1)
441 seq_save(mp); /* synchronize the sequences */
442 context_save(); /* save the context file */
445 /* record info for this folder */
446 if ((ftotal > 0) || !fshort) {
447 fi[i].nummsg = mp->nummsg;
448 fi[i].curmsg = mp->curmsg;
449 fi[i].lowmsg = mp->lowmsg;
450 fi[i].hghmsg = mp->hghmsg;
451 fi[i].others = other_files(mp);
454 folder_free(mp); /* free folder/message structure */
457 *crawl_children = (frecurse && (fshort || fi[i].others)
458 && (fi[i].error == 0));
463 get_folder_info_callback(char *fold, void *baton)
465 boolean crawl_children;
466 get_folder_info_body(fold, NULL, &crawl_children);
468 return crawl_children;
472 get_folder_info(char *fold, char *msg)
474 boolean crawl_children;
477 retval = get_folder_info_body(fold, msg, &crawl_children);
479 if (crawl_children) {
480 crawl_folders(fold, get_folder_info_callback, NULL);
486 ** Print folder information
492 int i, len, hasempty = 0, curprinted;
493 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
494 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
495 int nummsgdigits, lowmsgdigits;
496 int hghmsgdigits, curmsgdigits;
497 char tmpname[BUFSIZ];
500 ** compute a few values needed to for
501 ** printing various fields
503 for (i = 0; i < total_folders; i++) {
504 /* length of folder name */
505 len = strlen(fi[i].name);
509 /* If folder has error, skip the rest */
513 /* calculate total number of messages */
514 total_msgs += fi[i].nummsg;
516 /* maximum number of messages */
517 if (fi[i].nummsg > maxnummsg)
518 maxnummsg = fi[i].nummsg;
520 /* maximum low message */
521 if (fi[i].lowmsg > maxlowmsg)
522 maxlowmsg = fi[i].lowmsg;
524 /* maximum high message */
525 if (fi[i].hghmsg > maxhghmsg)
526 maxhghmsg = fi[i].hghmsg;
528 /* maximum current message */
529 if (fi[i].curmsg >= fi[i].lowmsg &&
530 fi[i].curmsg <= fi[i].hghmsg &&
531 fi[i].curmsg > maxcurmsg)
532 maxcurmsg = fi[i].curmsg;
534 /* check for empty folders */
535 if (fi[i].nummsg == 0)
538 nummsgdigits = num_digits(maxnummsg);
539 lowmsgdigits = num_digits(maxlowmsg);
540 hghmsgdigits = num_digits(maxhghmsg);
541 curmsgdigits = num_digits(maxcurmsg);
543 if (hasempty && nummsgdigits < 2)
549 if (fheader > 0 || (all && !fshort && fheader >= 0))
550 printf("%-*s %*s %-*s; %-*s %*s\n",
552 nummsgdigits + 13, "# MESSAGES",
553 lowmsgdigits + hghmsgdigits + 4, " RANGE",
554 curmsgdigits + 4, "CUR",
558 ** Print folder information
560 if (all || fshort || ftotal < 1) {
561 for (i = 0; i < total_folders; i++) {
563 printf("%s\n", fi[i].name);
567 /* Add `+' to end of name, if folder is current */
568 if (strcmp(folder, fi[i].name)!=0)
569 snprintf(tmpname, sizeof(tmpname), "%s",
572 snprintf(tmpname, sizeof(tmpname), "%s+",
576 printf("%-*s is unreadable\n", maxlen+1,
581 printf("%-*s ", maxlen+1, tmpname);
583 curprinted = 0; /* remember if we print cur */
584 if (fi[i].nummsg == 0) {
585 printf("has %*s messages%*s", nummsgdigits, "no", fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
587 printf("has %*d message%s (%*d-%*d)",
588 nummsgdigits, fi[i].nummsg,
589 (fi[i].nummsg == 1) ?
591 lowmsgdigits, fi[i].lowmsg,
592 hghmsgdigits, fi[i].hghmsg);
593 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
595 printf("; cur=%*d", curmsgdigits,
601 printf(";%*s (others)", curprinted ?
602 0 : curmsgdigits + 6, "");
608 ** Print folder/message totals
610 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
613 printf("TOTAL = %d message%c in %d folder%s.\n",
614 total_msgs, total_msgs != 1 ? 's' : ' ',
615 total_folders, total_folders != 1 ? "s" : "");
622 ** Set the current message and sychronize sequences
626 sfold(struct msgs *mp, char *msg)
628 /* parse the message range/sequence/name and set SELECTED */
629 if (!m_convert(mp, msg))
632 if (mp->numsel > 1) {
633 admonish(NULL, "only one message at a time!");
636 seq_setprev(mp); /* set the previous-sequence */
637 seq_setcur(mp, mp->lowsel); /* set current message */
638 seq_save(mp); /* synchronize message sequences */
639 context_save(); /* save the context file */
646 ** Do the read only folders
650 readonly_folders(void)
654 register struct node *np;
656 snprintf(atrcur, sizeof(atrcur), "atr-%s-", seq_cur);
657 atrlen = strlen(atrcur);
659 for (np = m_defs; np; np = np->n_next)
660 if (strncmp(np->n_name, atrcur, atrlen)==0 &&
661 strncmp(np->n_name+atrlen, nmhdir, strlen(nmhdir))!=0)
662 /* Why do we exclude absolute path names? --meillo */
663 get_folder_info(np->n_name + atrlen, NULL);
668 ** pack (renumber) the messages in a folder
669 ** into a contiguous range from 1 to n.
670 ** Return -1 if error, else return 0.
673 folder_pack(struct msgs **mpp, int verbose)
675 int hole, msgnum, newcurrent = 0;
676 char newmsg[BUFSIZ], oldmsg[BUFSIZ];
682 ** Just return if folder is empty.
688 ** Make sure we have message status space allocated
689 ** for all numbers from 1 to current high message.
691 if (mp->lowoff > 1) {
692 if ((mp = folder_realloc(mp, 1, mp->hghmsg)))
695 advise(NULL, "unable to allocate folder storage");
700 for (msgnum = mp->lowmsg, hole = 1; msgnum <= mp->hghmsg; msgnum++) {
701 if (does_exist(mp, msgnum)) {
702 if (msgnum != hole) {
703 strncpy(newmsg, m_name(hole), sizeof(newmsg));
704 strncpy(oldmsg, m_name(msgnum), sizeof(oldmsg));
706 printf("message %s becomes %s\n", oldmsg, newmsg);
709 ** Invoke the external refile hook for each
710 ** message being renamed. This is done
711 ** before the file is renamed so that the
712 ** old message file is around for the hook.
715 snprintf(oldmsg, sizeof (oldmsg), "%s/%d",
716 mp->foldpath, msgnum);
717 snprintf(newmsg, sizeof (newmsg), "%s/%d",
719 ext_hook("ref-hook", oldmsg, newmsg);
721 /* move the message file */
722 if (rename(oldmsg, newmsg) == -1) {
723 advise(newmsg, "unable to rename %s to", oldmsg);
727 /* check if this is the current message */
728 if (msgnum == mp->curmsg)
731 /* copy the attribute flags for this message */
732 copy_msg_flags(mp, hole, msgnum);
734 if (msgnum == mp->lowsel)
736 if (msgnum == mp->hghsel)
740 ** mark that sequence information has
743 mp->msgflags |= SEQMOD;
749 /* record the new number for the high/low message */
751 mp->hghmsg = hole - 1;
754 seq_setcur(mp, newcurrent);