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>
19 static struct swit switches[] = {
67 char *version=VERSION;
69 static int fshort = 0; /* output only folder names */
70 static int fcreat = 0; /* should we ask to create new folders? */
71 static int fpack = 0; /* are we packing the folder? */
72 static int fverb = 0; /* print actions taken while packing folder */
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;
123 setlocale(LC_ALL, "");
124 invo_name = mhbasename(argv[0]);
126 /* read user profile/context */
130 ** If program was invoked with name ending
131 ** in `s', then add switch `-all'.
133 if (argv[0][strlen(argv[0]) - 1] == 's')
136 arguments = getarguments(invo_name, argc, argv, 1);
139 while ((cp = *argp++)) {
141 switch (smatch(++cp, switches)) {
143 ambigsw(cp, switches);
146 adios(EX_USAGE, NULL, "-%s unknown", cp);
149 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
150 print_help(buf, switches, 1);
151 exit(argc == 2 ? EX_OK : EX_USAGE);
153 print_version(invo_name);
154 exit(argc == 2 ? EX_OK : EX_USAGE);
232 if (*cp == '+' || *cp == '@') {
234 adios(EX_USAGE, NULL, "only one folder at a time!");
236 argfolder = mh_xstrdup(expandfol(cp));
239 adios(EX_USAGE, NULL, "only one (current) message at a time!");
245 nmhdir = concat(toabsdir("+"), "/", NULL);
248 ** If we aren't working with the folder stack
249 ** (-push, -pop, -list) then the default is to print.
251 if (pushsw == 0 && popsw == 0 && listsw == 0)
254 /* Pushing a folder onto the folder stack */
257 /* If no folder is given, the current folder and */
258 /* the top of the folder stack are swapped. */
259 if ((cp = context_find(stack))) {
261 ap = brkstring(dp, " ", "\n");
262 argfolder = mh_xstrdup(*ap++);
264 adios(EX_USAGE, NULL, "no other folder");
266 for (cp = mh_xstrdup(getcurfol()); *ap; ap++)
267 cp = add(*ap, add(" ", cp));
269 context_replace(stack, cp); /* update folder stack */
271 /* update folder stack */
272 context_replace(stack, (cp = context_find (stack)) ?
273 concat(getcurfol(), " ", cp, NULL) :
274 mh_xstrdup(getcurfol()));
278 /* Popping a folder off of the folder stack */
281 adios(EX_USAGE, NULL, "sorry, no folders allowed with -pop");
282 if ((cp = context_find(stack))) {
284 ap = brkstring(dp, " ", "\n");
285 argfolder = mh_xstrdup(*ap++);
287 adios(EX_DATAERR, NULL, "folder stack empty");
290 /* if there's anything left in the stack */
291 cp = mh_xstrdup(*ap++);
293 cp = add(*ap, add(" ", cp));
294 context_replace(stack, cp); /* update folder stack */
296 /* delete folder stack entry from context */
301 if (pushsw || popsw) {
302 cp = toabsdir(argfolder);
303 if (access(cp, F_OK) == NOTOK)
304 adios(EX_DATAERR, cp, "unable to find folder");
305 /* update current folder */
306 context_replace(curfolder, argfolder);
307 context_save(); /* save the context file */
311 /* Listing the folder stack */
313 printf("%s", argfolder ? argfolder : getcurfol());
314 if ((cp = context_find(stack))) {
316 for (ap = brkstring(dp, " ", "\n"); *ap; ap++)
327 /* Allocate initial space to record folder information */
328 maxFolderInfo = CRAWL_NUMFOLDERS;
329 fi = mh_xcalloc(maxFolderInfo, sizeof(*fi));
335 ** change directory to base of nmh directory for
338 if (chdir(nmhdir) == NOTOK) {
339 adios(EX_OSERR, nmhdir, "unable to change directory to");
341 if (all || ftotal > 0) {
343 ** If no folder is given, do them all
347 admonish(NULL, "no folder given for message %s", msg);
348 readonly_folders(); /* do any readonly folders */
349 strncpy(folder, (cp = context_find(curfolder)) ?
350 cp : "", sizeof(folder));
351 crawl_folders(".", get_folder_info_callback, NULL);
353 strncpy(folder, argfolder, sizeof(folder));
354 if (get_folder_info(argfolder, msg)) {
355 /* update current folder */
356 context_replace(curfolder, argfolder);
360 ** Since recurse wasn't done in get_folder_info(),
361 ** we still need to list all level-1 sub-folders.
364 crawl_folders(folder, get_folder_info_callback,
368 strncpy(folder, argfolder ? argfolder : getcurfol(),
372 ** Check if folder exists. If not, then see if
373 ** we should create it, or just exit.
375 create_folder(toabsdir(folder), fcreat, exit);
377 if (get_folder_info(folder, msg) && argfolder) {
378 /* update current folder */
379 context_replace(curfolder, argfolder);
384 ** Print out folder information
393 get_folder_info_body(char *fold, char *msg, boolean *crawl_children)
396 struct msgs *mp = NULL;
401 ** if necessary, reallocate the space
402 ** for folder information
404 if (total_folders >= maxFolderInfo) {
405 maxFolderInfo += CRAWL_NUMFOLDERS;
406 fi = mh_xrealloc(fi, maxFolderInfo * sizeof(*fi));
417 if ((ftotal > 0) || !fshort || msg || fpack) {
419 ** create message structure and get folder info
421 if (!(mp = folder_read(fold))) {
422 admonish(NULL, "unable to read folder %s", fold);
426 /* set the current message */
427 if (msg && !sfold(mp, msg))
431 if (folder_pack(&mp, fverb) == -1) {
434 seq_save(mp); /* synchronize the sequences */
435 context_save(); /* save the context file */
438 /* record info for this folder */
439 if ((ftotal > 0) || !fshort) {
440 fi[i].nummsg = mp->nummsg;
441 fi[i].curmsg = mp->curmsg;
442 fi[i].lowmsg = mp->lowmsg;
443 fi[i].hghmsg = mp->hghmsg;
444 fi[i].others = other_files(mp);
447 folder_free(mp); /* free folder/message structure */
450 *crawl_children = (frecurse && (fshort || fi[i].others)
451 && (fi[i].error == 0));
456 get_folder_info_callback(char *fold, void *baton)
458 boolean crawl_children;
459 get_folder_info_body(fold, NULL, &crawl_children);
461 return crawl_children;
465 get_folder_info(char *fold, char *msg)
467 boolean crawl_children;
470 retval = get_folder_info_body(fold, msg, &crawl_children);
472 if (crawl_children) {
473 crawl_folders(fold, get_folder_info_callback, NULL);
479 ** Print folder information
485 int i, len, hasempty = 0, curprinted;
486 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
487 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
488 int nummsgdigits, lowmsgdigits;
489 int hghmsgdigits, curmsgdigits;
490 char tmpname[BUFSIZ];
493 ** compute a few values needed to for
494 ** printing various fields
496 for (i = 0; i < total_folders; i++) {
497 /* length of folder name */
498 len = strlen(fi[i].name);
502 /* If folder has error, skip the rest */
506 /* calculate total number of messages */
507 total_msgs += fi[i].nummsg;
509 /* maximum number of messages */
510 if (fi[i].nummsg > maxnummsg)
511 maxnummsg = fi[i].nummsg;
513 /* maximum low message */
514 if (fi[i].lowmsg > maxlowmsg)
515 maxlowmsg = fi[i].lowmsg;
517 /* maximum high message */
518 if (fi[i].hghmsg > maxhghmsg)
519 maxhghmsg = fi[i].hghmsg;
521 /* maximum current message */
522 if (fi[i].curmsg >= fi[i].lowmsg &&
523 fi[i].curmsg <= fi[i].hghmsg &&
524 fi[i].curmsg > maxcurmsg)
525 maxcurmsg = fi[i].curmsg;
527 /* check for empty folders */
528 if (fi[i].nummsg == 0)
531 nummsgdigits = num_digits(maxnummsg);
532 lowmsgdigits = num_digits(maxlowmsg);
533 hghmsgdigits = num_digits(maxhghmsg);
534 curmsgdigits = num_digits(maxcurmsg);
536 if (hasempty && nummsgdigits < 2)
540 ** Print folder information
542 if (all || fshort || ftotal < 1) {
543 for (i = 0; i < total_folders; i++) {
545 printf("%s\n", fi[i].name);
549 /* Add `+' to end of name, if folder is current */
550 if (strcmp(folder, fi[i].name)!=0)
551 snprintf(tmpname, sizeof(tmpname), "%s",
554 snprintf(tmpname, sizeof(tmpname), "%s+",
558 printf("%-*s is unreadable\n", maxlen+1,
563 printf("%-*s ", maxlen+1, tmpname);
565 curprinted = 0; /* remember if we print cur */
566 if (fi[i].nummsg == 0) {
567 printf("has %*s messages%*s", nummsgdigits, "no", fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
569 printf("has %*d message%s (%*d-%*d)",
570 nummsgdigits, fi[i].nummsg,
571 (fi[i].nummsg == 1) ?
573 lowmsgdigits, fi[i].lowmsg,
574 hghmsgdigits, fi[i].hghmsg);
575 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
577 printf("; cur=%*d", curmsgdigits,
583 printf(";%*s (others)", curprinted ?
584 0 : curmsgdigits + 6, "");
590 ** Print folder/message totals
592 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
595 printf("TOTAL = %d message%c in %d folder%s\n",
596 total_msgs, total_msgs != 1 ? 's' : ' ',
597 total_folders, total_folders != 1 ? "s" : "");
604 ** Set the current message and sychronize sequences
608 sfold(struct msgs *mp, char *msg)
610 /* parse the message range/sequence/name and set SELECTED */
611 if (!m_convert(mp, msg))
614 if (mp->numsel > 1) {
615 admonish(NULL, "only one message at a time!");
618 seq_setprev(mp); /* set the previous-sequence */
619 seq_setcur(mp, mp->lowsel); /* set current message */
620 seq_save(mp); /* synchronize message sequences */
621 context_save(); /* save the context file */
628 ** Do the read only folders
632 readonly_folders(void)
638 snprintf(atrcur, sizeof(atrcur), "atr-%s-", seq_cur);
639 atrlen = strlen(atrcur);
641 for (np = m_defs; np; np = np->n_next)
642 if (strncmp(np->n_name, atrcur, atrlen)==0 &&
643 strncmp(np->n_name+atrlen, nmhdir, strlen(nmhdir))!=0)
644 /* Why do we exclude absolute path names? --meillo */
645 get_folder_info(np->n_name + atrlen, NULL);
650 ** pack (renumber) the messages in a folder
651 ** into a contiguous range from 1 to n.
652 ** Return -1 if error, else return 0.
655 folder_pack(struct msgs **mpp, int verbose)
657 int hole, msgnum, newcurrent = 0;
658 char newmsg[BUFSIZ], oldmsg[BUFSIZ];
664 ** Just return if folder is empty.
670 ** Make sure we have message status space allocated
671 ** for all numbers from 1 to current high message.
673 if (mp->lowoff > 1) {
674 if ((mp = folder_realloc(mp, 1, mp->hghmsg)))
677 advise(NULL, "unable to allocate folder storage");
682 for (msgnum = mp->lowmsg, hole = 1; msgnum <= mp->hghmsg; msgnum++) {
683 if (does_exist(mp, msgnum)) {
684 if (msgnum != hole) {
685 strncpy(newmsg, m_name(hole), sizeof(newmsg));
686 strncpy(oldmsg, m_name(msgnum), sizeof(oldmsg));
688 printf("message %s becomes %s\n", oldmsg, newmsg);
691 ** Invoke the external refile hook for each
692 ** message being renamed. This is done
693 ** before the file is renamed so that the
694 ** old message file is around for the hook.
697 snprintf(oldmsg, sizeof (oldmsg), "%s/%d",
698 mp->foldpath, msgnum);
699 snprintf(newmsg, sizeof (newmsg), "%s/%d",
701 ext_hook("ref-hook", oldmsg, newmsg);
703 /* move the message file */
704 if (rename(oldmsg, newmsg) == -1) {
705 advise(newmsg, "unable to rename %s to", oldmsg);
709 /* check if this is the current message */
710 if (msgnum == mp->curmsg)
713 /* copy the attribute flags for this message */
714 copy_msg_flags(mp, hole, msgnum);
716 if (msgnum == mp->lowsel)
718 if (msgnum == mp->hghsel)
722 ** mark that sequence information has
725 mp->msgflags |= SEQMOD;
731 /* record the new number for the high/low message */
733 mp->hghmsg = hole - 1;
736 seq_setcur(mp, newcurrent);