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;
118 setlocale(LC_ALL, "");
119 invo_name = mhbasename(argv[0]);
121 /* read user profile/context */
125 ** If program was invoked with name ending
126 ** in `s', then add switch `-all'.
128 if (argv[0][strlen(argv[0]) - 1] == 's')
131 arguments = getarguments(invo_name, argc, argv, 1);
134 while ((cp = *argp++)) {
136 switch (smatch(++cp, switches)) {
138 ambigsw(cp, switches);
141 adios(NULL, "-%s unknown", cp);
144 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
145 print_help(buf, switches, 1);
148 print_version(invo_name);
227 if (*cp == '+' || *cp == '@') {
229 adios(NULL, "only one folder at a time!");
231 argfolder = getcpy(expandfol(cp));
234 adios(NULL, "only one (current) message at a time!");
240 nmhdir = concat(toabsdir("+"), "/", NULL);
243 ** If we aren't working with the folder stack
244 ** (-push, -pop, -list) then the default is to print.
246 if (pushsw == 0 && popsw == 0 && listsw == 0)
249 /* Pushing a folder onto the folder stack */
252 /* If no folder is given, the current folder and */
253 /* the top of the folder stack are swapped. */
254 if ((cp = context_find(stack))) {
256 ap = brkstring(dp, " ", "\n");
257 argfolder = getcpy(*ap++);
259 adios(NULL, "no other folder");
261 for (cp = getcpy(getcurfol()); *ap; ap++)
262 cp = add(*ap, add(" ", cp));
264 context_replace(stack, cp); /* update folder stack */
266 /* update folder stack */
267 context_replace(stack, (cp = context_find (stack)) ?
268 concat(getcurfol(), " ", cp, NULL) :
269 getcpy(getcurfol()));
273 /* Popping a folder off of the folder stack */
276 adios(NULL, "sorry, no folders allowed with -pop");
277 if ((cp = context_find(stack))) {
279 ap = brkstring(dp, " ", "\n");
280 argfolder = getcpy(*ap++);
282 adios(NULL, "folder stack empty");
285 /* if there's anything left in the stack */
288 cp = add(*ap, add(" ", cp));
289 context_replace(stack, cp); /* update folder stack */
291 /* delete folder stack entry from context */
296 if (pushsw || popsw) {
297 cp = toabsdir(argfolder);
298 if (access(cp, F_OK) == NOTOK)
299 adios(cp, "unable to find folder");
300 /* update current folder */
301 context_replace(curfolder, argfolder);
302 context_save(); /* save the context file */
306 /* Listing the folder stack */
308 printf("%s", argfolder ? argfolder : getcurfol());
309 if ((cp = context_find(stack))) {
311 for (ap = brkstring(dp, " ", "\n"); *ap; ap++)
321 /* Allocate initial space to record folder information */
322 maxFolderInfo = CRAWL_NUMFOLDERS;
323 fi = mh_xmalloc(maxFolderInfo * sizeof(*fi));
328 if (all || ftotal > 0) {
330 ** If no folder is given, do them all
333 ** change directory to base of nmh directory for
336 if (chdir(nmhdir) == NOTOK)
337 adios(nmhdir, "unable to change directory to");
340 admonish(NULL, "no folder given for message %s", msg);
341 readonly_folders(); /* do any readonly folders */
342 strncpy(folder, (cp = context_find(curfolder)) ?
343 cp : "", sizeof(folder));
344 crawl_folders(".", get_folder_info_callback, NULL);
346 strncpy(folder, argfolder, sizeof(folder));
347 if (get_folder_info(argfolder, msg)) {
348 /* update current folder */
349 context_replace(curfolder, argfolder);
353 ** Since recurse wasn't done in get_folder_info(),
354 ** we still need to list all level-1 sub-folders.
357 crawl_folders(folder, get_folder_info_callback,
361 strncpy(folder, argfolder ? argfolder : getcurfol(),
365 ** Check if folder exists. If not, then see if
366 ** we should create it, or just exit.
368 create_folder(toabsdir(folder), fcreat, done);
370 if (get_folder_info(folder, msg) && argfolder) {
371 /* update current folder */
372 context_replace(curfolder, argfolder);
377 ** Print out folder information
387 get_folder_info_body(char *fold, char *msg, boolean *crawl_children)
390 struct msgs *mp = NULL;
395 ** if necessary, reallocate the space
396 ** for folder information
398 if (total_folders >= maxFolderInfo) {
399 maxFolderInfo += CRAWL_NUMFOLDERS;
400 fi = mh_xrealloc(fi, maxFolderInfo * sizeof(*fi));
411 if ((ftotal > 0) || !fshort || msg || fpack) {
413 ** create message structure and get folder info
415 if (!(mp = folder_read(fold))) {
416 admonish(NULL, "unable to read folder %s", fold);
420 /* set the current message */
421 if (msg && !sfold(mp, msg))
425 if (folder_pack(&mp, fverb) == -1)
427 seq_save(mp); /* synchronize the sequences */
428 context_save(); /* save the context file */
431 /* record info for this folder */
432 if ((ftotal > 0) || !fshort) {
433 fi[i].nummsg = mp->nummsg;
434 fi[i].curmsg = mp->curmsg;
435 fi[i].lowmsg = mp->lowmsg;
436 fi[i].hghmsg = mp->hghmsg;
437 fi[i].others = other_files(mp);
440 folder_free(mp); /* free folder/message structure */
443 *crawl_children = (frecurse && (fshort || fi[i].others)
444 && (fi[i].error == 0));
449 get_folder_info_callback(char *fold, void *baton)
451 boolean crawl_children;
452 get_folder_info_body(fold, NULL, &crawl_children);
454 return crawl_children;
458 get_folder_info(char *fold, char *msg)
460 boolean crawl_children;
463 retval = get_folder_info_body(fold, msg, &crawl_children);
465 if (crawl_children) {
466 crawl_folders(fold, get_folder_info_callback, NULL);
472 ** Print folder information
478 int i, len, hasempty = 0, curprinted;
479 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
480 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
481 int nummsgdigits, lowmsgdigits;
482 int hghmsgdigits, curmsgdigits;
483 char tmpname[BUFSIZ];
486 ** compute a few values needed to for
487 ** printing various fields
489 for (i = 0; i < total_folders; i++) {
490 /* length of folder name */
491 len = strlen(fi[i].name);
495 /* If folder has error, skip the rest */
499 /* calculate total number of messages */
500 total_msgs += fi[i].nummsg;
502 /* maximum number of messages */
503 if (fi[i].nummsg > maxnummsg)
504 maxnummsg = fi[i].nummsg;
506 /* maximum low message */
507 if (fi[i].lowmsg > maxlowmsg)
508 maxlowmsg = fi[i].lowmsg;
510 /* maximum high message */
511 if (fi[i].hghmsg > maxhghmsg)
512 maxhghmsg = fi[i].hghmsg;
514 /* maximum current message */
515 if (fi[i].curmsg >= fi[i].lowmsg &&
516 fi[i].curmsg <= fi[i].hghmsg &&
517 fi[i].curmsg > maxcurmsg)
518 maxcurmsg = fi[i].curmsg;
520 /* check for empty folders */
521 if (fi[i].nummsg == 0)
524 nummsgdigits = num_digits(maxnummsg);
525 lowmsgdigits = num_digits(maxlowmsg);
526 hghmsgdigits = num_digits(maxhghmsg);
527 curmsgdigits = num_digits(maxcurmsg);
529 if (hasempty && nummsgdigits < 2)
533 ** Print folder information
535 if (all || fshort || ftotal < 1) {
536 for (i = 0; i < total_folders; i++) {
538 printf("%s\n", fi[i].name);
542 /* Add `+' to end of name, if folder is current */
543 if (strcmp(folder, fi[i].name)!=0)
544 snprintf(tmpname, sizeof(tmpname), "%s",
547 snprintf(tmpname, sizeof(tmpname), "%s+",
551 printf("%-*s is unreadable\n", maxlen+1,
556 printf("%-*s ", maxlen+1, tmpname);
558 curprinted = 0; /* remember if we print cur */
559 if (fi[i].nummsg == 0) {
560 printf("has %*s messages%*s", nummsgdigits, "no", fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
562 printf("has %*d message%s (%*d-%*d)",
563 nummsgdigits, fi[i].nummsg,
564 (fi[i].nummsg == 1) ?
566 lowmsgdigits, fi[i].lowmsg,
567 hghmsgdigits, fi[i].hghmsg);
568 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
570 printf("; cur=%*d", curmsgdigits,
576 printf(";%*s (others)", curprinted ?
577 0 : curmsgdigits + 6, "");
583 ** Print folder/message totals
585 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
588 printf("TOTAL = %d message%c in %d folder%s\n",
589 total_msgs, total_msgs != 1 ? 's' : ' ',
590 total_folders, total_folders != 1 ? "s" : "");
597 ** Set the current message and sychronize sequences
601 sfold(struct msgs *mp, char *msg)
603 /* parse the message range/sequence/name and set SELECTED */
604 if (!m_convert(mp, msg))
607 if (mp->numsel > 1) {
608 admonish(NULL, "only one message at a time!");
611 seq_setprev(mp); /* set the previous-sequence */
612 seq_setcur(mp, mp->lowsel); /* set current message */
613 seq_save(mp); /* synchronize message sequences */
614 context_save(); /* save the context file */
621 ** Do the read only folders
625 readonly_folders(void)
629 register struct node *np;
631 snprintf(atrcur, sizeof(atrcur), "atr-%s-", seq_cur);
632 atrlen = strlen(atrcur);
634 for (np = m_defs; np; np = np->n_next)
635 if (strncmp(np->n_name, atrcur, atrlen)==0 &&
636 strncmp(np->n_name+atrlen, nmhdir, strlen(nmhdir))!=0)
637 /* Why do we exclude absolute path names? --meillo */
638 get_folder_info(np->n_name + atrlen, NULL);
643 ** pack (renumber) the messages in a folder
644 ** into a contiguous range from 1 to n.
645 ** Return -1 if error, else return 0.
648 folder_pack(struct msgs **mpp, int verbose)
650 int hole, msgnum, newcurrent = 0;
651 char newmsg[BUFSIZ], oldmsg[BUFSIZ];
657 ** Just return if folder is empty.
663 ** Make sure we have message status space allocated
664 ** for all numbers from 1 to current high message.
666 if (mp->lowoff > 1) {
667 if ((mp = folder_realloc(mp, 1, mp->hghmsg)))
670 advise(NULL, "unable to allocate folder storage");
675 for (msgnum = mp->lowmsg, hole = 1; msgnum <= mp->hghmsg; msgnum++) {
676 if (does_exist(mp, msgnum)) {
677 if (msgnum != hole) {
678 strncpy(newmsg, m_name(hole), sizeof(newmsg));
679 strncpy(oldmsg, m_name(msgnum), sizeof(oldmsg));
681 printf("message %s becomes %s\n", oldmsg, newmsg);
684 ** Invoke the external refile hook for each
685 ** message being renamed. This is done
686 ** before the file is renamed so that the
687 ** old message file is around for the hook.
690 snprintf(oldmsg, sizeof (oldmsg), "%s/%d",
691 mp->foldpath, msgnum);
692 snprintf(newmsg, sizeof (newmsg), "%s/%d",
694 ext_hook("ref-hook", oldmsg, newmsg);
696 /* move the message file */
697 if (rename(oldmsg, newmsg) == -1) {
698 advise(newmsg, "unable to rename %s to", oldmsg);
702 /* check if this is the current message */
703 if (msgnum == mp->curmsg)
706 /* copy the attribute flags for this message */
707 copy_msg_flags(mp, hole, msgnum);
709 if (msgnum == mp->lowsel)
711 if (msgnum == mp->hghsel)
715 ** mark that sequence information has
718 mp->msgflags |= SEQMOD;
724 /* record the new number for the high/low message */
726 mp->hghmsg = hole - 1;
729 seq_setcur(mp, newcurrent);