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[] = {
64 static int fshort = 0; /* output only folder names */
65 static int fcreat = 0; /* should we ask to create new folders? */
66 static int fpack = 0; /* are we packing the folder? */
67 static int fverb = 0; /* print actions taken while packing folder */
68 static int frecurse = 0; /* recurse through subfolders */
69 static int ftotal = 0; /* should we output the totals? */
70 static int all = 0; /* should we output all folders */
72 static int total_folders = 0; /* total number of folders */
75 static char *stack = "Folder-Stack";
76 static char folder[BUFSIZ];
79 ** Structure to hold information about
80 ** folders as we scan them.
88 int others; /* others == 1 if other files in folder */
89 int error; /* error == 1 for unreadable folder */
93 ** Dynamically allocated space to hold
94 ** all the folder information.
96 static struct FolderInfo *fi;
97 static int maxFolderInfo;
102 static int get_folder_info(char *, char *);
103 static crawl_callback_t get_folder_info_callback;
104 static void print_folders(void);
105 static int sfold(struct msgs *, char *);
106 static void readonly_folders(void);
107 static int folder_pack(struct msgs **, int);
111 main(int argc, char **argv)
113 int printsw = 0, listsw = 0;
114 int pushsw = 0, popsw = 0;
115 char *cp, *dp, *msg = NULL, *argfolder = NULL;
116 char **ap, **argp, buf[BUFSIZ], **arguments;
119 setlocale(LC_ALL, "");
121 invo_name = mhbasename(argv[0]);
123 /* read user profile/context */
127 ** If program was invoked with name ending
128 ** in `s', then add switch `-all'.
130 if (argv[0][strlen(argv[0]) - 1] == 's')
133 arguments = getarguments(invo_name, argc, argv, 1);
136 while ((cp = *argp++)) {
138 switch (smatch(++cp, switches)) {
140 ambigsw(cp, switches);
143 adios(NULL, "-%s unknown", cp);
146 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
147 print_help(buf, switches, 1);
150 print_version(invo_name);
229 if (*cp == '+' || *cp == '@') {
231 adios(NULL, "only one folder at a time!");
233 argfolder = getcpy(expandfol(cp));
236 adios(NULL, "only one (current) message at a time!");
242 nmhdir = concat(toabsdir("+"), "/", NULL);
245 ** If we aren't working with the folder stack
246 ** (-push, -pop, -list) then the default is to print.
248 if (pushsw == 0 && popsw == 0 && listsw == 0)
251 /* Pushing a folder onto the folder stack */
254 /* If no folder is given, the current folder and */
255 /* the top of the folder stack are swapped. */
256 if ((cp = context_find(stack))) {
258 ap = brkstring(dp, " ", "\n");
259 argfolder = getcpy(*ap++);
261 adios(NULL, "no other folder");
263 for (cp = getcpy(getcurfol()); *ap; ap++)
264 cp = add(*ap, add(" ", cp));
266 context_replace(stack, cp); /* update folder stack */
268 /* update folder stack */
269 context_replace(stack, (cp = context_find (stack)) ?
270 concat(getcurfol(), " ", cp, NULL) :
271 getcpy(getcurfol()));
275 /* Popping a folder off of the folder stack */
278 adios(NULL, "sorry, no folders allowed with -pop");
279 if ((cp = context_find(stack))) {
281 ap = brkstring(dp, " ", "\n");
282 argfolder = getcpy(*ap++);
284 adios(NULL, "folder stack empty");
287 /* if there's anything left in the stack */
290 cp = add(*ap, add(" ", cp));
291 context_replace(stack, cp); /* update folder stack */
293 /* delete folder stack entry from context */
298 if (pushsw || popsw) {
299 cp = toabsdir(argfolder);
300 if (access(cp, F_OK) == NOTOK)
301 adios(cp, "unable to find folder");
302 /* update current folder */
303 context_replace(curfolder, argfolder);
304 context_save(); /* save the context file */
308 /* Listing the folder stack */
310 printf("%s", argfolder ? argfolder : getcurfol());
311 if ((cp = context_find(stack))) {
313 for (ap = brkstring(dp, " ", "\n"); *ap; ap++)
323 /* Allocate initial space to record folder information */
324 maxFolderInfo = CRAWL_NUMFOLDERS;
325 fi = mh_xmalloc(maxFolderInfo * sizeof(*fi));
330 if (all || ftotal > 0) {
332 ** If no folder is given, do them all
335 ** change directory to base of nmh directory for
338 if (chdir(nmhdir) == NOTOK)
339 adios(nmhdir, "unable to change directory to");
342 admonish(NULL, "no folder given for message %s", msg);
343 readonly_folders(); /* do any readonly folders */
344 strncpy(folder, (cp = context_find(curfolder)) ?
345 cp : "", sizeof(folder));
346 crawl_folders(".", get_folder_info_callback, NULL);
348 strncpy(folder, argfolder, sizeof(folder));
349 if (get_folder_info(argfolder, msg)) {
350 /* update current folder */
351 context_replace(curfolder, argfolder);
355 ** Since recurse wasn't done in get_folder_info(),
356 ** we still need to list all level-1 sub-folders.
359 crawl_folders(folder, get_folder_info_callback,
363 strncpy(folder, argfolder ? argfolder : getcurfol(),
367 ** Check if folder exists. If not, then see if
368 ** we should create it, or just exit.
370 create_folder(toabsdir(folder), fcreat, done);
372 if (get_folder_info(folder, msg) && argfolder) {
373 /* update current folder */
374 context_replace(curfolder, argfolder);
379 ** Print out folder information
389 get_folder_info_body(char *fold, char *msg, boolean *crawl_children)
392 struct msgs *mp = NULL;
397 ** if necessary, reallocate the space
398 ** for folder information
400 if (total_folders >= maxFolderInfo) {
401 maxFolderInfo += CRAWL_NUMFOLDERS;
402 fi = mh_xrealloc(fi, maxFolderInfo * sizeof(*fi));
413 if ((ftotal > 0) || !fshort || msg || fpack) {
415 ** create message structure and get folder info
417 if (!(mp = folder_read(fold))) {
418 admonish(NULL, "unable to read folder %s", fold);
422 /* set the current message */
423 if (msg && !sfold(mp, msg))
427 if (folder_pack(&mp, fverb) == -1)
429 seq_save(mp); /* synchronize the sequences */
430 context_save(); /* save the context file */
433 /* record info for this folder */
434 if ((ftotal > 0) || !fshort) {
435 fi[i].nummsg = mp->nummsg;
436 fi[i].curmsg = mp->curmsg;
437 fi[i].lowmsg = mp->lowmsg;
438 fi[i].hghmsg = mp->hghmsg;
439 fi[i].others = other_files(mp);
442 folder_free(mp); /* free folder/message structure */
445 *crawl_children = (frecurse && (fshort || fi[i].others)
446 && (fi[i].error == 0));
451 get_folder_info_callback(char *fold, void *baton)
453 boolean crawl_children;
454 get_folder_info_body(fold, NULL, &crawl_children);
456 return crawl_children;
460 get_folder_info(char *fold, char *msg)
462 boolean crawl_children;
465 retval = get_folder_info_body(fold, msg, &crawl_children);
467 if (crawl_children) {
468 crawl_folders(fold, get_folder_info_callback, NULL);
474 ** Print folder information
480 int i, len, hasempty = 0, curprinted;
481 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
482 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
483 int nummsgdigits, lowmsgdigits;
484 int hghmsgdigits, curmsgdigits;
485 char tmpname[BUFSIZ];
488 ** compute a few values needed to for
489 ** printing various fields
491 for (i = 0; i < total_folders; i++) {
492 /* length of folder name */
493 len = strlen(fi[i].name);
497 /* If folder has error, skip the rest */
501 /* calculate total number of messages */
502 total_msgs += fi[i].nummsg;
504 /* maximum number of messages */
505 if (fi[i].nummsg > maxnummsg)
506 maxnummsg = fi[i].nummsg;
508 /* maximum low message */
509 if (fi[i].lowmsg > maxlowmsg)
510 maxlowmsg = fi[i].lowmsg;
512 /* maximum high message */
513 if (fi[i].hghmsg > maxhghmsg)
514 maxhghmsg = fi[i].hghmsg;
516 /* maximum current message */
517 if (fi[i].curmsg >= fi[i].lowmsg &&
518 fi[i].curmsg <= fi[i].hghmsg &&
519 fi[i].curmsg > maxcurmsg)
520 maxcurmsg = fi[i].curmsg;
522 /* check for empty folders */
523 if (fi[i].nummsg == 0)
526 nummsgdigits = num_digits(maxnummsg);
527 lowmsgdigits = num_digits(maxlowmsg);
528 hghmsgdigits = num_digits(maxhghmsg);
529 curmsgdigits = num_digits(maxcurmsg);
531 if (hasempty && nummsgdigits < 2)
535 ** Print folder information
537 if (all || fshort || ftotal < 1) {
538 for (i = 0; i < total_folders; i++) {
540 printf("%s\n", fi[i].name);
544 /* Add `+' to end of name, if folder is current */
545 if (strcmp(folder, fi[i].name)!=0)
546 snprintf(tmpname, sizeof(tmpname), "%s",
549 snprintf(tmpname, sizeof(tmpname), "%s+",
553 printf("%-*s is unreadable\n", maxlen+1,
558 printf("%-*s ", maxlen+1, tmpname);
560 curprinted = 0; /* remember if we print cur */
561 if (fi[i].nummsg == 0) {
562 printf("has %*s messages%*s", nummsgdigits, "no", fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
564 printf("has %*d message%s (%*d-%*d)",
565 nummsgdigits, fi[i].nummsg,
566 (fi[i].nummsg == 1) ?
568 lowmsgdigits, fi[i].lowmsg,
569 hghmsgdigits, fi[i].hghmsg);
570 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
572 printf("; cur=%*d", curmsgdigits,
578 printf(";%*s (others)", curprinted ?
579 0 : curmsgdigits + 6, "");
585 ** Print folder/message totals
587 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
590 printf("TOTAL = %d message%c in %d folder%s\n",
591 total_msgs, total_msgs != 1 ? 's' : ' ',
592 total_folders, total_folders != 1 ? "s" : "");
599 ** Set the current message and sychronize sequences
603 sfold(struct msgs *mp, char *msg)
605 /* parse the message range/sequence/name and set SELECTED */
606 if (!m_convert(mp, msg))
609 if (mp->numsel > 1) {
610 admonish(NULL, "only one message at a time!");
613 seq_setprev(mp); /* set the previous-sequence */
614 seq_setcur(mp, mp->lowsel); /* set current message */
615 seq_save(mp); /* synchronize message sequences */
616 context_save(); /* save the context file */
623 ** Do the read only folders
627 readonly_folders(void)
631 register struct node *np;
633 snprintf(atrcur, sizeof(atrcur), "atr-%s-", seq_cur);
634 atrlen = strlen(atrcur);
636 for (np = m_defs; np; np = np->n_next)
637 if (strncmp(np->n_name, atrcur, atrlen)==0 &&
638 strncmp(np->n_name+atrlen, nmhdir, strlen(nmhdir))!=0)
639 /* Why do we exclude absolute path names? --meillo */
640 get_folder_info(np->n_name + atrlen, NULL);
645 ** pack (renumber) the messages in a folder
646 ** into a contiguous range from 1 to n.
647 ** Return -1 if error, else return 0.
650 folder_pack(struct msgs **mpp, int verbose)
652 int hole, msgnum, newcurrent = 0;
653 char newmsg[BUFSIZ], oldmsg[BUFSIZ];
659 ** Just return if folder is empty.
665 ** Make sure we have message status space allocated
666 ** for all numbers from 1 to current high message.
668 if (mp->lowoff > 1) {
669 if ((mp = folder_realloc(mp, 1, mp->hghmsg)))
672 advise(NULL, "unable to allocate folder storage");
677 for (msgnum = mp->lowmsg, hole = 1; msgnum <= mp->hghmsg; msgnum++) {
678 if (does_exist(mp, msgnum)) {
679 if (msgnum != hole) {
680 strncpy(newmsg, m_name(hole), sizeof(newmsg));
681 strncpy(oldmsg, m_name(msgnum), sizeof(oldmsg));
683 printf("message %s becomes %s\n", oldmsg, newmsg);
686 ** Invoke the external refile hook for each
687 ** message being renamed. This is done
688 ** before the file is renamed so that the
689 ** old message file is around for the hook.
692 snprintf(oldmsg, sizeof (oldmsg), "%s/%d",
693 mp->foldpath, msgnum);
694 snprintf(newmsg, sizeof (newmsg), "%s/%d",
696 ext_hook("ref-hook", oldmsg, newmsg);
698 /* move the message file */
699 if (rename(oldmsg, newmsg) == -1) {
700 advise(newmsg, "unable to rename %s to", oldmsg);
704 /* check if this is the current message */
705 if (msgnum == mp->curmsg)
708 /* copy the attribute flags for this message */
709 copy_msg_flags(mp, hole, msgnum);
711 if (msgnum == mp->lowsel)
713 if (msgnum == mp->hghsel)
717 ** mark that sequence information has
720 mp->msgflags |= SEQMOD;
726 /* record the new number for the high/low message */
728 mp->hghmsg = hole - 1;
731 seq_setcur(mp, newcurrent);