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 static int fshort = 0; /* output only folder names */
68 static int fcreat = 0; /* should we ask to create new folders? */
69 static int fpack = 0; /* are we packing the folder? */
70 static int fverb = 0; /* print actions taken while packing folder */
71 static int frecurse = 0; /* recurse through subfolders */
72 static int ftotal = 0; /* should we output the totals? */
73 static int all = 0; /* should we output all folders */
75 static int total_folders = 0; /* total number of folders */
78 static char *stack = "Folder-Stack";
79 static char folder[BUFSIZ];
82 ** Structure to hold information about
83 ** folders as we scan them.
91 int others; /* others == 1 if other files in folder */
92 int error; /* error == 1 for unreadable folder */
96 ** Dynamically allocated space to hold
97 ** all the folder information.
99 static struct FolderInfo *fi;
100 static int maxFolderInfo;
105 static int get_folder_info(char *, char *);
106 static crawl_callback_t get_folder_info_callback;
107 static void print_folders(void);
108 static int sfold(struct msgs *, char *);
109 static void readonly_folders(void);
110 static int folder_pack(struct msgs **, int);
114 main(int argc, char **argv)
116 int printsw = 0, listsw = 0;
117 int pushsw = 0, popsw = 0;
118 char *cp, *dp, *msg = NULL, *argfolder = NULL;
119 char **ap, **argp, buf[BUFSIZ], **arguments;
121 setlocale(LC_ALL, "");
122 invo_name = mhbasename(argv[0]);
124 /* read user profile/context */
128 ** If program was invoked with name ending
129 ** in `s', then add switch `-all'.
131 if (argv[0][strlen(argv[0]) - 1] == 's')
134 arguments = getarguments(invo_name, argc, argv, 1);
137 while ((cp = *argp++)) {
139 switch (smatch(++cp, switches)) {
141 ambigsw(cp, switches);
144 adios(EX_USAGE, NULL, "-%s unknown", cp);
147 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
148 print_help(buf, switches, 1);
149 exit(argc == 2 ? EX_OK : EX_USAGE);
151 print_version(invo_name);
152 exit(argc == 2 ? EX_OK : EX_USAGE);
230 if (*cp == '+' || *cp == '@') {
232 adios(EX_USAGE, NULL, "only one folder at a time!");
234 argfolder = getcpy(expandfol(cp));
237 adios(EX_USAGE, NULL, "only one (current) message at a time!");
243 nmhdir = concat(toabsdir("+"), "/", NULL);
246 ** If we aren't working with the folder stack
247 ** (-push, -pop, -list) then the default is to print.
249 if (pushsw == 0 && popsw == 0 && listsw == 0)
252 /* Pushing a folder onto the folder stack */
255 /* If no folder is given, the current folder and */
256 /* the top of the folder stack are swapped. */
257 if ((cp = context_find(stack))) {
259 ap = brkstring(dp, " ", "\n");
260 argfolder = getcpy(*ap++);
262 adios(EX_USAGE, NULL, "no other folder");
264 for (cp = getcpy(getcurfol()); *ap; ap++)
265 cp = add(*ap, add(" ", cp));
267 context_replace(stack, cp); /* update folder stack */
269 /* update folder stack */
270 context_replace(stack, (cp = context_find (stack)) ?
271 concat(getcurfol(), " ", cp, NULL) :
272 getcpy(getcurfol()));
276 /* Popping a folder off of the folder stack */
279 adios(EX_USAGE, NULL, "sorry, no folders allowed with -pop");
280 if ((cp = context_find(stack))) {
282 ap = brkstring(dp, " ", "\n");
283 argfolder = getcpy(*ap++);
285 adios(EX_DATAERR, NULL, "folder stack empty");
288 /* if there's anything left in the stack */
291 cp = add(*ap, add(" ", cp));
292 context_replace(stack, cp); /* update folder stack */
294 /* delete folder stack entry from context */
299 if (pushsw || popsw) {
300 cp = toabsdir(argfolder);
301 if (access(cp, F_OK) == NOTOK)
302 adios(EX_DATAERR, cp, "unable to find folder");
303 /* update current folder */
304 context_replace(curfolder, argfolder);
305 context_save(); /* save the context file */
309 /* Listing the folder stack */
311 printf("%s", argfolder ? argfolder : getcurfol());
312 if ((cp = context_find(stack))) {
314 for (ap = brkstring(dp, " ", "\n"); *ap; ap++)
325 /* Allocate initial space to record folder information */
326 maxFolderInfo = CRAWL_NUMFOLDERS;
327 fi = mh_xmalloc(maxFolderInfo * sizeof(*fi));
332 if (all || ftotal > 0) {
334 ** If no folder is given, do them all
337 ** change directory to base of nmh directory for
340 if (chdir(nmhdir) == NOTOK)
341 adios(EX_OSERR, nmhdir, "unable to change directory to");
344 admonish(NULL, "no folder given for message %s", msg);
345 readonly_folders(); /* do any readonly folders */
346 strncpy(folder, (cp = context_find(curfolder)) ?
347 cp : "", sizeof(folder));
348 crawl_folders(".", get_folder_info_callback, NULL);
350 strncpy(folder, argfolder, sizeof(folder));
351 if (get_folder_info(argfolder, msg)) {
352 /* update current folder */
353 context_replace(curfolder, argfolder);
357 ** Since recurse wasn't done in get_folder_info(),
358 ** we still need to list all level-1 sub-folders.
361 crawl_folders(folder, get_folder_info_callback,
365 strncpy(folder, argfolder ? argfolder : getcurfol(),
369 ** Check if folder exists. If not, then see if
370 ** we should create it, or just exit.
372 create_folder(toabsdir(folder), fcreat, exit);
374 if (get_folder_info(folder, msg) && argfolder) {
375 /* update current folder */
376 context_replace(curfolder, argfolder);
381 ** Print out folder information
390 get_folder_info_body(char *fold, char *msg, boolean *crawl_children)
393 struct msgs *mp = NULL;
398 ** if necessary, reallocate the space
399 ** for folder information
401 if (total_folders >= maxFolderInfo) {
402 maxFolderInfo += CRAWL_NUMFOLDERS;
403 fi = mh_xrealloc(fi, maxFolderInfo * sizeof(*fi));
414 if ((ftotal > 0) || !fshort || msg || fpack) {
416 ** create message structure and get folder info
418 if (!(mp = folder_read(fold))) {
419 admonish(NULL, "unable to read folder %s", fold);
423 /* set the current message */
424 if (msg && !sfold(mp, msg))
428 if (folder_pack(&mp, fverb) == -1) {
431 seq_save(mp); /* synchronize the sequences */
432 context_save(); /* save the context file */
435 /* record info for this folder */
436 if ((ftotal > 0) || !fshort) {
437 fi[i].nummsg = mp->nummsg;
438 fi[i].curmsg = mp->curmsg;
439 fi[i].lowmsg = mp->lowmsg;
440 fi[i].hghmsg = mp->hghmsg;
441 fi[i].others = other_files(mp);
444 folder_free(mp); /* free folder/message structure */
447 *crawl_children = (frecurse && (fshort || fi[i].others)
448 && (fi[i].error == 0));
453 get_folder_info_callback(char *fold, void *baton)
455 boolean crawl_children;
456 get_folder_info_body(fold, NULL, &crawl_children);
458 return crawl_children;
462 get_folder_info(char *fold, char *msg)
464 boolean crawl_children;
467 retval = get_folder_info_body(fold, msg, &crawl_children);
469 if (crawl_children) {
470 crawl_folders(fold, get_folder_info_callback, NULL);
476 ** Print folder information
482 int i, len, hasempty = 0, curprinted;
483 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
484 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
485 int nummsgdigits, lowmsgdigits;
486 int hghmsgdigits, curmsgdigits;
487 char tmpname[BUFSIZ];
490 ** compute a few values needed to for
491 ** printing various fields
493 for (i = 0; i < total_folders; i++) {
494 /* length of folder name */
495 len = strlen(fi[i].name);
499 /* If folder has error, skip the rest */
503 /* calculate total number of messages */
504 total_msgs += fi[i].nummsg;
506 /* maximum number of messages */
507 if (fi[i].nummsg > maxnummsg)
508 maxnummsg = fi[i].nummsg;
510 /* maximum low message */
511 if (fi[i].lowmsg > maxlowmsg)
512 maxlowmsg = fi[i].lowmsg;
514 /* maximum high message */
515 if (fi[i].hghmsg > maxhghmsg)
516 maxhghmsg = fi[i].hghmsg;
518 /* maximum current message */
519 if (fi[i].curmsg >= fi[i].lowmsg &&
520 fi[i].curmsg <= fi[i].hghmsg &&
521 fi[i].curmsg > maxcurmsg)
522 maxcurmsg = fi[i].curmsg;
524 /* check for empty folders */
525 if (fi[i].nummsg == 0)
528 nummsgdigits = num_digits(maxnummsg);
529 lowmsgdigits = num_digits(maxlowmsg);
530 hghmsgdigits = num_digits(maxhghmsg);
531 curmsgdigits = num_digits(maxcurmsg);
533 if (hasempty && nummsgdigits < 2)
537 ** Print folder information
539 if (all || fshort || ftotal < 1) {
540 for (i = 0; i < total_folders; i++) {
542 printf("%s\n", fi[i].name);
546 /* Add `+' to end of name, if folder is current */
547 if (strcmp(folder, fi[i].name)!=0)
548 snprintf(tmpname, sizeof(tmpname), "%s",
551 snprintf(tmpname, sizeof(tmpname), "%s+",
555 printf("%-*s is unreadable\n", maxlen+1,
560 printf("%-*s ", maxlen+1, tmpname);
562 curprinted = 0; /* remember if we print cur */
563 if (fi[i].nummsg == 0) {
564 printf("has %*s messages%*s", nummsgdigits, "no", fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
566 printf("has %*d message%s (%*d-%*d)",
567 nummsgdigits, fi[i].nummsg,
568 (fi[i].nummsg == 1) ?
570 lowmsgdigits, fi[i].lowmsg,
571 hghmsgdigits, fi[i].hghmsg);
572 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
574 printf("; cur=%*d", curmsgdigits,
580 printf(";%*s (others)", curprinted ?
581 0 : curmsgdigits + 6, "");
587 ** Print folder/message totals
589 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
592 printf("TOTAL = %d message%c in %d folder%s\n",
593 total_msgs, total_msgs != 1 ? 's' : ' ',
594 total_folders, total_folders != 1 ? "s" : "");
601 ** Set the current message and sychronize sequences
605 sfold(struct msgs *mp, char *msg)
607 /* parse the message range/sequence/name and set SELECTED */
608 if (!m_convert(mp, msg))
611 if (mp->numsel > 1) {
612 admonish(NULL, "only one message at a time!");
615 seq_setprev(mp); /* set the previous-sequence */
616 seq_setcur(mp, mp->lowsel); /* set current message */
617 seq_save(mp); /* synchronize message sequences */
618 context_save(); /* save the context file */
625 ** Do the read only folders
629 readonly_folders(void)
635 snprintf(atrcur, sizeof(atrcur), "atr-%s-", seq_cur);
636 atrlen = strlen(atrcur);
638 for (np = m_defs; np; np = np->n_next)
639 if (strncmp(np->n_name, atrcur, atrlen)==0 &&
640 strncmp(np->n_name+atrlen, nmhdir, strlen(nmhdir))!=0)
641 /* Why do we exclude absolute path names? --meillo */
642 get_folder_info(np->n_name + atrlen, NULL);
647 ** pack (renumber) the messages in a folder
648 ** into a contiguous range from 1 to n.
649 ** Return -1 if error, else return 0.
652 folder_pack(struct msgs **mpp, int verbose)
654 int hole, msgnum, newcurrent = 0;
655 char newmsg[BUFSIZ], oldmsg[BUFSIZ];
661 ** Just return if folder is empty.
667 ** Make sure we have message status space allocated
668 ** for all numbers from 1 to current high message.
670 if (mp->lowoff > 1) {
671 if ((mp = folder_realloc(mp, 1, mp->hghmsg)))
674 advise(NULL, "unable to allocate folder storage");
679 for (msgnum = mp->lowmsg, hole = 1; msgnum <= mp->hghmsg; msgnum++) {
680 if (does_exist(mp, msgnum)) {
681 if (msgnum != hole) {
682 strncpy(newmsg, m_name(hole), sizeof(newmsg));
683 strncpy(oldmsg, m_name(msgnum), sizeof(oldmsg));
685 printf("message %s becomes %s\n", oldmsg, newmsg);
688 ** Invoke the external refile hook for each
689 ** message being renamed. This is done
690 ** before the file is renamed so that the
691 ** old message file is around for the hook.
694 snprintf(oldmsg, sizeof (oldmsg), "%s/%d",
695 mp->foldpath, msgnum);
696 snprintf(newmsg, sizeof (newmsg), "%s/%d",
698 ext_hook("ref-hook", oldmsg, newmsg);
700 /* move the message file */
701 if (rename(oldmsg, newmsg) == -1) {
702 advise(newmsg, "unable to rename %s to", oldmsg);
706 /* check if this is the current message */
707 if (msgnum == mp->curmsg)
710 /* copy the attribute flags for this message */
711 copy_msg_flags(mp, hole, msgnum);
713 if (msgnum == mp->lowsel)
715 if (msgnum == mp->hghsel)
719 ** mark that sequence information has
722 mp->msgflags |= SEQMOD;
728 /* record the new number for the high/low message */
730 mp->hghmsg = hole - 1;
733 seq_setcur(mp, newcurrent);