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[] = {
68 static int fshort = 0; /* output only folder names */
69 static int fcreat = 0; /* should we ask to create new folders? */
70 static int fpack = 0; /* are we packing the folder? */
71 static int fverb = 0; /* print actions taken while packing folder */
72 static int fheader = 0; /* should we output a header? */
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);
115 main(int argc, char **argv)
117 int printsw = 0, listsw = 0;
118 int pushsw = 0, popsw = 0;
119 char *cp, *dp, *msg = NULL, *argfolder = NULL;
120 char **ap, **argp, buf[BUFSIZ], **arguments;
123 setlocale(LC_ALL, "");
125 invo_name = mhbasename(argv[0]);
127 /* read user profile/context */
131 ** If program was invoked with name ending
132 ** in `s', then add switch `-all'.
134 if (argv[0][strlen(argv[0]) - 1] == 's')
137 arguments = getarguments(invo_name, argc, argv, 1);
140 while ((cp = *argp++)) {
142 switch (smatch(++cp, switches)) {
144 ambigsw(cp, switches);
147 adios(NULL, "-%s unknown", cp);
150 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
151 print_help(buf, switches, 1);
154 print_version(invo_name);
240 if (*cp == '+' || *cp == '@') {
242 adios(NULL, "only one folder at a time!");
244 argfolder = getcpy(expandfol(cp));
247 adios(NULL, "only one (current) message at a time!");
253 nmhdir = concat(toabsdir("+"), "/", NULL);
256 ** If we aren't working with the folder stack
257 ** (-push, -pop, -list) then the default is to print.
259 if (pushsw == 0 && popsw == 0 && listsw == 0)
262 /* Pushing a folder onto the folder stack */
265 /* If no folder is given, the current folder and */
266 /* the top of the folder stack are swapped. */
267 if ((cp = context_find(stack))) {
269 ap = brkstring(dp, " ", "\n");
270 argfolder = getcpy(*ap++);
272 adios(NULL, "no other folder");
274 for (cp = getcpy(getcurfol()); *ap; ap++)
275 cp = add(*ap, add(" ", cp));
277 context_replace(stack, cp); /* update folder stack */
279 /* update folder stack */
280 context_replace(stack, (cp = context_find (stack)) ?
281 concat(getcurfol(), " ", cp, NULL) :
282 getcpy(getcurfol()));
286 /* Popping a folder off of the folder stack */
289 adios(NULL, "sorry, no folders allowed with -pop");
290 if ((cp = context_find(stack))) {
292 ap = brkstring(dp, " ", "\n");
293 argfolder = getcpy(*ap++);
295 adios(NULL, "folder stack empty");
298 /* if there's anything left in the stack */
301 cp = add(*ap, add(" ", cp));
302 context_replace(stack, cp); /* update folder stack */
304 /* delete folder stack entry from context */
309 if (pushsw || popsw) {
310 cp = toabsdir(argfolder);
311 if (access(cp, F_OK) == NOTOK)
312 adios(cp, "unable to find folder");
313 /* update current folder */
314 context_replace(curfolder, argfolder);
315 context_save(); /* save the context file */
319 /* Listing the folder stack */
321 printf("%s", argfolder ? argfolder : getcurfol());
322 if ((cp = context_find(stack))) {
324 for (ap = brkstring(dp, " ", "\n"); *ap; ap++)
334 /* Allocate initial space to record folder information */
335 maxFolderInfo = CRAWL_NUMFOLDERS;
336 fi = mh_xmalloc(maxFolderInfo * sizeof(*fi));
341 if (all || ftotal > 0) {
343 ** If no folder is given, do them all
346 ** change directory to base of nmh directory for
349 if (chdir(nmhdir) == NOTOK)
350 adios(nmhdir, "unable to change directory to");
353 admonish(NULL, "no folder given for message %s", msg);
354 readonly_folders(); /* do any readonly folders */
355 strncpy(folder, (cp = context_find(curfolder)) ?
356 cp : "", sizeof(folder));
357 crawl_folders(".", get_folder_info_callback, NULL);
359 strncpy(folder, argfolder, sizeof(folder));
360 if (get_folder_info(argfolder, msg)) {
361 /* update current folder */
362 context_replace(curfolder, argfolder);
366 ** Since recurse wasn't done in get_folder_info(),
367 ** we still need to list all level-1 sub-folders.
370 crawl_folders(folder, get_folder_info_callback,
374 strncpy(folder, argfolder ? argfolder : getcurfol(),
378 ** Check if folder exists. If not, then see if
379 ** we should create it, or just exit.
381 create_folder(toabsdir(folder), fcreat, done);
383 if (get_folder_info(folder, msg) && argfolder) {
384 /* update current folder */
385 context_replace(curfolder, argfolder);
390 ** Print out folder information
400 get_folder_info_body(char *fold, char *msg, boolean *crawl_children)
403 struct msgs *mp = NULL;
408 ** if necessary, reallocate the space
409 ** for folder information
411 if (total_folders >= maxFolderInfo) {
412 maxFolderInfo += CRAWL_NUMFOLDERS;
413 fi = mh_xrealloc(fi, maxFolderInfo * sizeof(*fi));
424 if ((ftotal > 0) || !fshort || msg || fpack) {
426 ** create message structure and get folder info
428 if (!(mp = folder_read(fold))) {
429 admonish(NULL, "unable to read folder %s", fold);
433 /* set the current message */
434 if (msg && !sfold(mp, msg))
438 if (folder_pack(&mp, fverb) == -1)
440 seq_save(mp); /* synchronize the sequences */
441 context_save(); /* save the context file */
444 /* record info for this folder */
445 if ((ftotal > 0) || !fshort) {
446 fi[i].nummsg = mp->nummsg;
447 fi[i].curmsg = mp->curmsg;
448 fi[i].lowmsg = mp->lowmsg;
449 fi[i].hghmsg = mp->hghmsg;
450 fi[i].others = other_files(mp);
453 folder_free(mp); /* free folder/message structure */
456 *crawl_children = (frecurse && (fshort || fi[i].others)
457 && (fi[i].error == 0));
462 get_folder_info_callback(char *fold, void *baton)
464 boolean crawl_children;
465 get_folder_info_body(fold, NULL, &crawl_children);
467 return crawl_children;
471 get_folder_info(char *fold, char *msg)
473 boolean crawl_children;
476 retval = get_folder_info_body(fold, msg, &crawl_children);
478 if (crawl_children) {
479 crawl_folders(fold, get_folder_info_callback, NULL);
485 ** Print folder information
491 int i, len, hasempty = 0, curprinted;
492 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
493 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
494 int nummsgdigits, lowmsgdigits;
495 int hghmsgdigits, curmsgdigits;
496 char tmpname[BUFSIZ];
499 ** compute a few values needed to for
500 ** printing various fields
502 for (i = 0; i < total_folders; i++) {
503 /* length of folder name */
504 len = strlen(fi[i].name);
508 /* If folder has error, skip the rest */
512 /* calculate total number of messages */
513 total_msgs += fi[i].nummsg;
515 /* maximum number of messages */
516 if (fi[i].nummsg > maxnummsg)
517 maxnummsg = fi[i].nummsg;
519 /* maximum low message */
520 if (fi[i].lowmsg > maxlowmsg)
521 maxlowmsg = fi[i].lowmsg;
523 /* maximum high message */
524 if (fi[i].hghmsg > maxhghmsg)
525 maxhghmsg = fi[i].hghmsg;
527 /* maximum current message */
528 if (fi[i].curmsg >= fi[i].lowmsg &&
529 fi[i].curmsg <= fi[i].hghmsg &&
530 fi[i].curmsg > maxcurmsg)
531 maxcurmsg = fi[i].curmsg;
533 /* check for empty folders */
534 if (fi[i].nummsg == 0)
537 nummsgdigits = num_digits(maxnummsg);
538 lowmsgdigits = num_digits(maxlowmsg);
539 hghmsgdigits = num_digits(maxhghmsg);
540 curmsgdigits = num_digits(maxcurmsg);
542 if (hasempty && nummsgdigits < 2)
548 if (fheader > 0 || (all && !fshort && fheader >= 0))
549 printf("%-*s %*s %-*s; %-*s %*s\n",
551 nummsgdigits + 13, "# MESSAGES",
552 lowmsgdigits + hghmsgdigits + 4, " RANGE",
553 curmsgdigits + 4, "CUR",
557 ** Print folder information
559 if (all || fshort || ftotal < 1) {
560 for (i = 0; i < total_folders; i++) {
562 printf("%s\n", fi[i].name);
566 /* Add `+' to end of name, if folder is current */
567 if (strcmp(folder, fi[i].name))
568 snprintf(tmpname, sizeof(tmpname), "%s",
571 snprintf(tmpname, sizeof(tmpname), "%s+",
575 printf("%-*s is unreadable\n", maxlen+1,
580 printf("%-*s ", maxlen+1, tmpname);
582 curprinted = 0; /* remember if we print cur */
583 if (fi[i].nummsg == 0) {
584 printf("has %*s messages%*s", nummsgdigits, "no", fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
586 printf("has %*d message%s (%*d-%*d)",
587 nummsgdigits, fi[i].nummsg,
588 (fi[i].nummsg == 1) ?
590 lowmsgdigits, fi[i].lowmsg,
591 hghmsgdigits, fi[i].hghmsg);
592 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
594 printf("; cur=%*d", curmsgdigits,
600 printf(";%*s (others)", curprinted ?
601 0 : curmsgdigits + 6, "");
607 ** Print folder/message totals
609 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
612 printf("TOTAL = %d message%c in %d folder%s.\n",
613 total_msgs, total_msgs != 1 ? 's' : ' ',
614 total_folders, total_folders != 1 ? "s" : "");
621 ** Set the current message and sychronize sequences
625 sfold(struct msgs *mp, char *msg)
627 /* parse the message range/sequence/name and set SELECTED */
628 if (!m_convert(mp, msg))
631 if (mp->numsel > 1) {
632 admonish(NULL, "only one message at a time!");
635 seq_setprev(mp); /* set the previous-sequence */
636 seq_setcur(mp, mp->lowsel); /* set current message */
637 seq_save(mp); /* synchronize message sequences */
638 context_save(); /* save the context file */
645 ** Do the read only folders
649 readonly_folders(void)
653 register struct node *np;
655 snprintf(atrcur, sizeof(atrcur), "atr-%s-", current);
656 atrlen = strlen(atrcur);
658 for (np = m_defs; np; np = np->n_next)
659 if (isprefix(atrcur, np->n_name)
660 && !isprefix(nmhdir, np->n_name + atrlen))
661 get_folder_info(np->n_name + atrlen, NULL);