3 * folder(s).c -- set/list the current message and/or folder
4 * -- push/pop a folder onto/from the folder stack
5 * -- list the folder stack
7 * This code is Copyright (c) 2002, 2008, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/crawl_folders.h>
17 static struct swit switches[] = {
69 static int fshort = 0; /* output only folder names */
70 static int fcreat = 0; /* should we ask to create new folders? */
71 static int fpack = 0; /* are we packing the folder? */
72 static int fverb = 0; /* print actions taken while packing folder */
73 static int fheader = 0; /* should we output a header? */
74 static int frecurse = 0; /* recurse through subfolders */
75 static int ftotal = 0; /* should we output the totals? */
76 static int all = 0; /* should we output all folders */
78 static int total_folders = 0; /* total number of folders */
81 static char *stack = "Folder-Stack";
82 static char folder[BUFSIZ];
85 * Structure to hold information about
86 * folders as we scan them.
94 int others; /* others == 1 if other files in folder */
95 int error; /* error == 1 for unreadable folder */
99 * Dynamically allocated space to hold
100 * all the folder information.
102 static struct FolderInfo *fi;
103 static int maxFolderInfo;
108 static int get_folder_info (char *, char *);
109 static crawl_callback_t get_folder_info_callback;
110 static void print_folders (void);
111 static int sfold (struct msgs *, char *);
112 static void readonly_folders (void);
116 main (int argc, char **argv)
118 int printsw = 0, listsw = 0;
119 int pushsw = 0, popsw = 0;
120 char *cp, *dp, *msg = NULL, *argfolder = NULL;
121 char **ap, **argp, buf[BUFSIZ], **arguments;
124 setlocale(LC_ALL, "");
126 invo_name = r1bindex (argv[0], '/');
128 /* read user profile/context */
132 * If program was invoked with name ending
133 * in `s', then add switch `-all'.
135 if (argv[0][strlen (argv[0]) - 1] == 's')
138 arguments = getarguments (invo_name, argc, argv, 1);
141 while ((cp = *argp++)) {
143 switch (smatch (++cp, switches)) {
145 ambigsw (cp, switches);
148 adios (NULL, "-%s unknown", cp);
151 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
153 print_help (buf, switches, 1);
156 print_version(invo_name);
242 if (*cp == '+' || *cp == '@') {
244 adios (NULL, "only one folder at a time!");
246 argfolder = pluspath (cp);
249 adios (NULL, "only one (current) message at a time!");
255 if (!context_find ("path"))
256 free (path ("./", TFOLDER));
257 nmhdir = concat (m_maildir (""), "/", NULL);
260 * If we aren't working with the folder stack
261 * (-push, -pop, -list) then the default is to print.
263 if (pushsw == 0 && popsw == 0 && listsw == 0)
266 /* Pushing a folder onto the folder stack */
269 /* If no folder is given, the current folder and */
270 /* the top of the folder stack are swapped. */
271 if ((cp = context_find (stack))) {
273 ap = brkstring (dp, " ", "\n");
274 argfolder = getcpy(*ap++);
276 adios (NULL, "no other folder");
278 for (cp = getcpy (getfolder (1)); *ap; ap++)
279 cp = add (*ap, add (" ", cp));
281 context_replace (stack, cp); /* update folder stack */
283 /* update folder stack */
284 context_replace (stack,
285 (cp = context_find (stack))
286 ? concat (getfolder (1), " ", cp, NULL)
287 : getcpy (getfolder (1)));
291 /* Popping a folder off of the folder stack */
294 adios (NULL, "sorry, no folders allowed with -pop");
295 if ((cp = context_find (stack))) {
297 ap = brkstring (dp, " ", "\n");
298 argfolder = getcpy(*ap++);
300 adios (NULL, "folder stack empty");
303 /* if there's anything left in the stack */
306 cp = add (*ap, add (" ", cp));
307 context_replace (stack, cp); /* update folder stack */
309 context_del (stack); /* delete folder stack entry from context */
313 if (pushsw || popsw) {
314 cp = m_maildir(argfolder);
315 if (access (cp, F_OK) == NOTOK)
316 adios (cp, "unable to find folder");
317 context_replace (pfolder, argfolder); /* update current folder */
318 context_save (); /* save the context file */
322 /* Listing the folder stack */
324 printf ("%s", argfolder ? argfolder : getfolder (1));
325 if ((cp = context_find (stack))) {
327 for (ap = brkstring (dp, " ", "\n"); *ap; ap++)
337 /* Allocate initial space to record folder information */
338 maxFolderInfo = CRAWL_NUMFOLDERS;
339 fi = mh_xmalloc (maxFolderInfo * sizeof(*fi));
344 if (all || ftotal > 0) {
346 * If no folder is given, do them all
348 /* change directory to base of nmh directory for crawl_folders */
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 (pfolder)) ? cp : "", sizeof(folder));
356 crawl_folders (".", get_folder_info_callback, NULL);
358 strncpy (folder, argfolder, sizeof(folder));
359 if (get_folder_info (argfolder, msg)) {
360 context_replace (pfolder, argfolder);/* update current folder */
361 context_save (); /* save the context file */
364 * Since recurse wasn't done in get_folder_info(),
365 * we still need to list all level-1 sub-folders.
368 crawl_folders (folder, get_folder_info_callback, NULL);
371 strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
374 * Check if folder exists. If not, then see if
375 * we should create it, or just exit.
377 create_folder (m_maildir (folder), fcreat, done);
379 if (get_folder_info (folder, msg) && argfolder) {
380 /* update current folder */
381 context_replace (pfolder, argfolder);
386 * Print out folder information
390 context_save (); /* save the context file */
396 get_folder_info_body (char *fold, char *msg, boolean *crawl_children)
399 struct msgs *mp = NULL;
404 * if necessary, reallocate the space
405 * for folder information
407 if (total_folders >= maxFolderInfo) {
408 maxFolderInfo += CRAWL_NUMFOLDERS;
409 fi = mh_xrealloc (fi, maxFolderInfo * sizeof(*fi));
420 if ((ftotal > 0) || !fshort || msg || fpack) {
422 * create message structure and get folder info
424 if (!(mp = folder_read (fold))) {
425 admonish (NULL, "unable to read folder %s", fold);
429 /* set the current message */
430 if (msg && !sfold (mp, msg))
434 if (folder_pack (&mp, fverb) == -1)
436 seq_save (mp); /* synchronize the sequences */
437 context_save (); /* save the context file */
440 /* record info for this folder */
441 if ((ftotal > 0) || !fshort) {
442 fi[i].nummsg = mp->nummsg;
443 fi[i].curmsg = mp->curmsg;
444 fi[i].lowmsg = mp->lowmsg;
445 fi[i].hghmsg = mp->hghmsg;
446 fi[i].others = other_files (mp);
449 folder_free (mp); /* free folder/message structure */
452 *crawl_children = (frecurse && (fshort || fi[i].others)
453 && (fi[i].error == 0));
458 get_folder_info_callback (char *fold, void *baton)
460 boolean crawl_children;
461 get_folder_info_body (fold, NULL, &crawl_children);
463 return crawl_children;
467 get_folder_info (char *fold, char *msg)
469 boolean crawl_children;
472 retval = get_folder_info_body (fold, msg, &crawl_children);
474 if (crawl_children) {
475 crawl_folders (fold, get_folder_info_callback, NULL);
482 * Print folder information
488 int i, len, hasempty = 0, curprinted;
489 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
490 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
491 int nummsgdigits, lowmsgdigits;
492 int hghmsgdigits, curmsgdigits;
493 char tmpname[BUFSIZ];
496 * compute a few values needed to for
497 * printing various fields
499 for (i = 0; i < total_folders; i++) {
500 /* length of folder name */
501 len = strlen (fi[i].name);
505 /* If folder has error, skip the rest */
509 /* calculate total number of messages */
510 total_msgs += fi[i].nummsg;
512 /* maximum number of messages */
513 if (fi[i].nummsg > maxnummsg)
514 maxnummsg = fi[i].nummsg;
516 /* maximum low message */
517 if (fi[i].lowmsg > maxlowmsg)
518 maxlowmsg = fi[i].lowmsg;
520 /* maximum high message */
521 if (fi[i].hghmsg > maxhghmsg)
522 maxhghmsg = fi[i].hghmsg;
524 /* maximum current message */
525 if (fi[i].curmsg >= fi[i].lowmsg &&
526 fi[i].curmsg <= fi[i].hghmsg &&
527 fi[i].curmsg > maxcurmsg)
528 maxcurmsg = fi[i].curmsg;
530 /* check for empty folders */
531 if (fi[i].nummsg == 0)
534 nummsgdigits = num_digits (maxnummsg);
535 lowmsgdigits = num_digits (maxlowmsg);
536 hghmsgdigits = num_digits (maxhghmsg);
537 curmsgdigits = num_digits (maxcurmsg);
539 if (hasempty && nummsgdigits < 2)
545 if (fheader > 0 || (all && !fshort && fheader >= 0))
546 printf ("%-*s %*s %-*s; %-*s %*s\n",
548 nummsgdigits + 13, "# MESSAGES",
549 lowmsgdigits + hghmsgdigits + 4, " RANGE",
550 curmsgdigits + 4, "CUR",
554 * Print folder information
556 if (all || fshort || ftotal < 1) {
557 for (i = 0; i < total_folders; i++) {
559 printf ("%s\n", fi[i].name);
563 /* Add `+' to end of name, if folder is current */
564 if (strcmp (folder, fi[i].name))
565 snprintf (tmpname, sizeof(tmpname), "%s", fi[i].name);
567 snprintf (tmpname, sizeof(tmpname), "%s+", fi[i].name);
570 printf ("%-*s is unreadable\n", maxlen+1, tmpname);
574 printf ("%-*s ", maxlen+1, tmpname);
576 curprinted = 0; /* remember if we print cur */
577 if (fi[i].nummsg == 0) {
578 printf ("has %*s messages%*s",
580 fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
582 printf ("has %*d message%s (%*d-%*d)",
583 nummsgdigits, fi[i].nummsg,
584 (fi[i].nummsg == 1) ? " " : "s",
585 lowmsgdigits, fi[i].lowmsg,
586 hghmsgdigits, fi[i].hghmsg);
587 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
589 printf ("; cur=%*d", curmsgdigits, fi[i].curmsg);
594 printf (";%*s (others)", curprinted ? 0 : curmsgdigits + 6, "");
600 * Print folder/message totals
602 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
605 printf ("TOTAL = %d message%c in %d folder%s.\n",
606 total_msgs, total_msgs != 1 ? 's' : ' ',
607 total_folders, total_folders != 1 ? "s" : "");
614 * Set the current message and sychronize sequences
618 sfold (struct msgs *mp, char *msg)
620 /* parse the message range/sequence/name and set SELECTED */
621 if (!m_convert (mp, msg))
624 if (mp->numsel > 1) {
625 admonish (NULL, "only one message at a time!");
628 seq_setprev (mp); /* set the previous-sequence */
629 seq_setcur (mp, mp->lowsel);/* set current message */
630 seq_save (mp); /* synchronize message sequences */
631 context_save (); /* save the context file */
638 * Do the read only folders
642 readonly_folders (void)
646 register struct node *np;
648 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
649 atrlen = strlen (atrcur);
651 for (np = m_defs; np; np = np->n_next)
652 if (ssequal (atrcur, np->n_name)
653 && !ssequal (nmhdir, np->n_name + atrlen))
654 get_folder_info (np->n_name + atrlen, NULL);