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));
333 ** change directory to base of nmh directory for
336 if (chdir(nmhdir) == NOTOK) {
337 adios(EX_OSERR, nmhdir, "unable to change directory to");
339 if (all || ftotal > 0) {
341 ** If no folder is given, do them all
345 admonish(NULL, "no folder given for message %s", msg);
346 readonly_folders(); /* do any readonly folders */
347 strncpy(folder, (cp = context_find(curfolder)) ?
348 cp : "", sizeof(folder));
349 crawl_folders(".", get_folder_info_callback, NULL);
351 strncpy(folder, argfolder, sizeof(folder));
352 if (get_folder_info(argfolder, msg)) {
353 /* update current folder */
354 context_replace(curfolder, argfolder);
358 ** Since recurse wasn't done in get_folder_info(),
359 ** we still need to list all level-1 sub-folders.
362 crawl_folders(folder, get_folder_info_callback,
366 strncpy(folder, argfolder ? argfolder : getcurfol(),
370 ** Check if folder exists. If not, then see if
371 ** we should create it, or just exit.
373 create_folder(toabsdir(folder), fcreat, exit);
375 if (get_folder_info(folder, msg) && argfolder) {
376 /* update current folder */
377 context_replace(curfolder, argfolder);
382 ** Print out folder information
391 get_folder_info_body(char *fold, char *msg, boolean *crawl_children)
394 struct msgs *mp = NULL;
399 ** if necessary, reallocate the space
400 ** for folder information
402 if (total_folders >= maxFolderInfo) {
403 maxFolderInfo += CRAWL_NUMFOLDERS;
404 fi = mh_xrealloc(fi, maxFolderInfo * sizeof(*fi));
415 if ((ftotal > 0) || !fshort || msg || fpack) {
417 ** create message structure and get folder info
419 if (!(mp = folder_read(fold))) {
420 admonish(NULL, "unable to read folder %s", fold);
424 /* set the current message */
425 if (msg && !sfold(mp, msg))
429 if (folder_pack(&mp, fverb) == -1) {
432 seq_save(mp); /* synchronize the sequences */
433 context_save(); /* save the context file */
436 /* record info for this folder */
437 if ((ftotal > 0) || !fshort) {
438 fi[i].nummsg = mp->nummsg;
439 fi[i].curmsg = mp->curmsg;
440 fi[i].lowmsg = mp->lowmsg;
441 fi[i].hghmsg = mp->hghmsg;
442 fi[i].others = other_files(mp);
445 folder_free(mp); /* free folder/message structure */
448 *crawl_children = (frecurse && (fshort || fi[i].others)
449 && (fi[i].error == 0));
454 get_folder_info_callback(char *fold, void *baton)
456 boolean crawl_children;
457 get_folder_info_body(fold, NULL, &crawl_children);
459 return crawl_children;
463 get_folder_info(char *fold, char *msg)
465 boolean crawl_children;
468 retval = get_folder_info_body(fold, msg, &crawl_children);
470 if (crawl_children) {
471 crawl_folders(fold, get_folder_info_callback, NULL);
477 ** Print folder information
483 int i, len, hasempty = 0, curprinted;
484 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
485 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
486 int nummsgdigits, lowmsgdigits;
487 int hghmsgdigits, curmsgdigits;
488 char tmpname[BUFSIZ];
491 ** compute a few values needed to for
492 ** printing various fields
494 for (i = 0; i < total_folders; i++) {
495 /* length of folder name */
496 len = strlen(fi[i].name);
500 /* If folder has error, skip the rest */
504 /* calculate total number of messages */
505 total_msgs += fi[i].nummsg;
507 /* maximum number of messages */
508 if (fi[i].nummsg > maxnummsg)
509 maxnummsg = fi[i].nummsg;
511 /* maximum low message */
512 if (fi[i].lowmsg > maxlowmsg)
513 maxlowmsg = fi[i].lowmsg;
515 /* maximum high message */
516 if (fi[i].hghmsg > maxhghmsg)
517 maxhghmsg = fi[i].hghmsg;
519 /* maximum current message */
520 if (fi[i].curmsg >= fi[i].lowmsg &&
521 fi[i].curmsg <= fi[i].hghmsg &&
522 fi[i].curmsg > maxcurmsg)
523 maxcurmsg = fi[i].curmsg;
525 /* check for empty folders */
526 if (fi[i].nummsg == 0)
529 nummsgdigits = num_digits(maxnummsg);
530 lowmsgdigits = num_digits(maxlowmsg);
531 hghmsgdigits = num_digits(maxhghmsg);
532 curmsgdigits = num_digits(maxcurmsg);
534 if (hasempty && nummsgdigits < 2)
538 ** Print folder information
540 if (all || fshort || ftotal < 1) {
541 for (i = 0; i < total_folders; i++) {
543 printf("%s\n", fi[i].name);
547 /* Add `+' to end of name, if folder is current */
548 if (strcmp(folder, fi[i].name)!=0)
549 snprintf(tmpname, sizeof(tmpname), "%s",
552 snprintf(tmpname, sizeof(tmpname), "%s+",
556 printf("%-*s is unreadable\n", maxlen+1,
561 printf("%-*s ", maxlen+1, tmpname);
563 curprinted = 0; /* remember if we print cur */
564 if (fi[i].nummsg == 0) {
565 printf("has %*s messages%*s", nummsgdigits, "no", fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
567 printf("has %*d message%s (%*d-%*d)",
568 nummsgdigits, fi[i].nummsg,
569 (fi[i].nummsg == 1) ?
571 lowmsgdigits, fi[i].lowmsg,
572 hghmsgdigits, fi[i].hghmsg);
573 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
575 printf("; cur=%*d", curmsgdigits,
581 printf(";%*s (others)", curprinted ?
582 0 : curmsgdigits + 6, "");
588 ** Print folder/message totals
590 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
593 printf("TOTAL = %d message%c in %d folder%s\n",
594 total_msgs, total_msgs != 1 ? 's' : ' ',
595 total_folders, total_folders != 1 ? "s" : "");
602 ** Set the current message and sychronize sequences
606 sfold(struct msgs *mp, char *msg)
608 /* parse the message range/sequence/name and set SELECTED */
609 if (!m_convert(mp, msg))
612 if (mp->numsel > 1) {
613 admonish(NULL, "only one message at a time!");
616 seq_setprev(mp); /* set the previous-sequence */
617 seq_setcur(mp, mp->lowsel); /* set current message */
618 seq_save(mp); /* synchronize message sequences */
619 context_save(); /* save the context file */
626 ** Do the read only folders
630 readonly_folders(void)
636 snprintf(atrcur, sizeof(atrcur), "atr-%s-", seq_cur);
637 atrlen = strlen(atrcur);
639 for (np = m_defs; np; np = np->n_next)
640 if (strncmp(np->n_name, atrcur, atrlen)==0 &&
641 strncmp(np->n_name+atrlen, nmhdir, strlen(nmhdir))!=0)
642 /* Why do we exclude absolute path names? --meillo */
643 get_folder_info(np->n_name + atrlen, NULL);
648 ** pack (renumber) the messages in a folder
649 ** into a contiguous range from 1 to n.
650 ** Return -1 if error, else return 0.
653 folder_pack(struct msgs **mpp, int verbose)
655 int hole, msgnum, newcurrent = 0;
656 char newmsg[BUFSIZ], oldmsg[BUFSIZ];
662 ** Just return if folder is empty.
668 ** Make sure we have message status space allocated
669 ** for all numbers from 1 to current high message.
671 if (mp->lowoff > 1) {
672 if ((mp = folder_realloc(mp, 1, mp->hghmsg)))
675 advise(NULL, "unable to allocate folder storage");
680 for (msgnum = mp->lowmsg, hole = 1; msgnum <= mp->hghmsg; msgnum++) {
681 if (does_exist(mp, msgnum)) {
682 if (msgnum != hole) {
683 strncpy(newmsg, m_name(hole), sizeof(newmsg));
684 strncpy(oldmsg, m_name(msgnum), sizeof(oldmsg));
686 printf("message %s becomes %s\n", oldmsg, newmsg);
689 ** Invoke the external refile hook for each
690 ** message being renamed. This is done
691 ** before the file is renamed so that the
692 ** old message file is around for the hook.
695 snprintf(oldmsg, sizeof (oldmsg), "%s/%d",
696 mp->foldpath, msgnum);
697 snprintf(newmsg, sizeof (newmsg), "%s/%d",
699 ext_hook("ref-hook", oldmsg, newmsg);
701 /* move the message file */
702 if (rename(oldmsg, newmsg) == -1) {
703 advise(newmsg, "unable to rename %s to", oldmsg);
707 /* check if this is the current message */
708 if (msgnum == mp->curmsg)
711 /* copy the attribute flags for this message */
712 copy_msg_flags(mp, hole, msgnum);
714 if (msgnum == mp->lowsel)
716 if (msgnum == mp->hghsel)
720 ** mark that sequence information has
723 mp->msgflags |= SEQMOD;
729 /* record the new number for the high/low message */
731 mp->hghmsg = hole - 1;
734 seq_setcur(mp, newcurrent);