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 = r1bindex (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]",
152 print_help (buf, switches, 1);
155 print_version(invo_name);
241 if (*cp == '+' || *cp == '@') {
243 adios (NULL, "only one folder at a time!");
245 argfolder = pluspath (cp);
248 adios (NULL, "only one (current) message at a time!");
254 if (!context_find ("path"))
255 free (path ("./", TFOLDER));
256 nmhdir = concat (m_maildir (""), "/", NULL);
259 * If we aren't working with the folder stack
260 * (-push, -pop, -list) then the default is to print.
262 if (pushsw == 0 && popsw == 0 && listsw == 0)
265 /* Pushing a folder onto the folder stack */
268 /* If no folder is given, the current folder and */
269 /* the top of the folder stack are swapped. */
270 if ((cp = context_find (stack))) {
272 ap = brkstring (dp, " ", "\n");
273 argfolder = getcpy(*ap++);
275 adios (NULL, "no other folder");
277 for (cp = getcpy (getfolder (1)); *ap; ap++)
278 cp = add (*ap, add (" ", cp));
280 context_replace (stack, cp); /* update folder stack */
282 /* update folder stack */
283 context_replace (stack,
284 (cp = context_find (stack))
285 ? concat (getfolder (1), " ", cp, NULL)
286 : getcpy (getfolder (1)));
290 /* Popping a folder off of the folder stack */
293 adios (NULL, "sorry, no folders allowed with -pop");
294 if ((cp = context_find (stack))) {
296 ap = brkstring (dp, " ", "\n");
297 argfolder = getcpy(*ap++);
299 adios (NULL, "folder stack empty");
302 /* if there's anything left in the stack */
305 cp = add (*ap, add (" ", cp));
306 context_replace (stack, cp); /* update folder stack */
308 context_del (stack); /* delete folder stack entry from context */
312 if (pushsw || popsw) {
313 cp = m_maildir(argfolder);
314 if (access (cp, F_OK) == NOTOK)
315 adios (cp, "unable to find folder");
316 context_replace (pfolder, argfolder); /* update current folder */
317 context_save (); /* save the context file */
321 /* Listing the folder stack */
323 printf ("%s", argfolder ? argfolder : getfolder (1));
324 if ((cp = context_find (stack))) {
326 for (ap = brkstring (dp, " ", "\n"); *ap; ap++)
336 /* Allocate initial space to record folder information */
337 maxFolderInfo = CRAWL_NUMFOLDERS;
338 fi = mh_xmalloc (maxFolderInfo * sizeof(*fi));
343 if (all || ftotal > 0) {
345 * If no folder is given, do them all
347 /* change directory to base of nmh directory for crawl_folders */
348 if (chdir (nmhdir) == NOTOK)
349 adios (nmhdir, "unable to change directory to");
352 admonish (NULL, "no folder given for message %s", msg);
353 readonly_folders (); /* do any readonly folders */
354 strncpy (folder, (cp = context_find (pfolder)) ? cp : "", sizeof(folder));
355 crawl_folders (".", get_folder_info_callback, NULL);
357 strncpy (folder, argfolder, sizeof(folder));
358 if (get_folder_info (argfolder, msg)) {
359 context_replace (pfolder, argfolder);/* update current folder */
360 context_save (); /* save the context file */
363 * Since recurse wasn't done in get_folder_info(),
364 * we still need to list all level-1 sub-folders.
367 crawl_folders (folder, get_folder_info_callback, NULL);
370 strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
373 * Check if folder exists. If not, then see if
374 * we should create it, or just exit.
376 create_folder (m_maildir (folder), fcreat, done);
378 if (get_folder_info (folder, msg) && argfolder) {
379 /* update current folder */
380 context_replace (pfolder, argfolder);
385 * Print out folder information
389 context_save (); /* save the context file */
395 get_folder_info_body (char *fold, char *msg, boolean *crawl_children)
398 struct msgs *mp = NULL;
403 * if necessary, reallocate the space
404 * for folder information
406 if (total_folders >= maxFolderInfo) {
407 maxFolderInfo += CRAWL_NUMFOLDERS;
408 fi = mh_xrealloc (fi, maxFolderInfo * sizeof(*fi));
419 if ((ftotal > 0) || !fshort || msg || fpack) {
421 * create message structure and get folder info
423 if (!(mp = folder_read (fold))) {
424 admonish (NULL, "unable to read folder %s", fold);
428 /* set the current message */
429 if (msg && !sfold (mp, msg))
433 if (folder_pack (&mp, fverb) == -1)
435 seq_save (mp); /* synchronize the sequences */
436 context_save (); /* save the context file */
439 /* record info for this folder */
440 if ((ftotal > 0) || !fshort) {
441 fi[i].nummsg = mp->nummsg;
442 fi[i].curmsg = mp->curmsg;
443 fi[i].lowmsg = mp->lowmsg;
444 fi[i].hghmsg = mp->hghmsg;
445 fi[i].others = other_files (mp);
448 folder_free (mp); /* free folder/message structure */
451 *crawl_children = (frecurse && (fshort || fi[i].others)
452 && (fi[i].error == 0));
457 get_folder_info_callback (char *fold, void *baton)
459 boolean crawl_children;
460 get_folder_info_body (fold, NULL, &crawl_children);
462 return crawl_children;
466 get_folder_info (char *fold, char *msg)
468 boolean crawl_children;
471 retval = get_folder_info_body (fold, msg, &crawl_children);
473 if (crawl_children) {
474 crawl_folders (fold, get_folder_info_callback, NULL);
481 * Print folder information
487 int i, len, hasempty = 0, curprinted;
488 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
489 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
490 int nummsgdigits, lowmsgdigits;
491 int hghmsgdigits, curmsgdigits;
492 char tmpname[BUFSIZ];
495 * compute a few values needed to for
496 * printing various fields
498 for (i = 0; i < total_folders; i++) {
499 /* length of folder name */
500 len = strlen (fi[i].name);
504 /* If folder has error, skip the rest */
508 /* calculate total number of messages */
509 total_msgs += fi[i].nummsg;
511 /* maximum number of messages */
512 if (fi[i].nummsg > maxnummsg)
513 maxnummsg = fi[i].nummsg;
515 /* maximum low message */
516 if (fi[i].lowmsg > maxlowmsg)
517 maxlowmsg = fi[i].lowmsg;
519 /* maximum high message */
520 if (fi[i].hghmsg > maxhghmsg)
521 maxhghmsg = fi[i].hghmsg;
523 /* maximum current message */
524 if (fi[i].curmsg >= fi[i].lowmsg &&
525 fi[i].curmsg <= fi[i].hghmsg &&
526 fi[i].curmsg > maxcurmsg)
527 maxcurmsg = fi[i].curmsg;
529 /* check for empty folders */
530 if (fi[i].nummsg == 0)
533 nummsgdigits = num_digits (maxnummsg);
534 lowmsgdigits = num_digits (maxlowmsg);
535 hghmsgdigits = num_digits (maxhghmsg);
536 curmsgdigits = num_digits (maxcurmsg);
538 if (hasempty && nummsgdigits < 2)
544 if (fheader > 0 || (all && !fshort && fheader >= 0))
545 printf ("%-*s %*s %-*s; %-*s %*s\n",
547 nummsgdigits + 13, "# MESSAGES",
548 lowmsgdigits + hghmsgdigits + 4, " RANGE",
549 curmsgdigits + 4, "CUR",
553 * Print folder information
555 if (all || fshort || ftotal < 1) {
556 for (i = 0; i < total_folders; i++) {
558 printf ("%s\n", fi[i].name);
562 /* Add `+' to end of name, if folder is current */
563 if (strcmp (folder, fi[i].name))
564 snprintf (tmpname, sizeof(tmpname), "%s", fi[i].name);
566 snprintf (tmpname, sizeof(tmpname), "%s+", fi[i].name);
569 printf ("%-*s is unreadable\n", maxlen+1, tmpname);
573 printf ("%-*s ", maxlen+1, tmpname);
575 curprinted = 0; /* remember if we print cur */
576 if (fi[i].nummsg == 0) {
577 printf ("has %*s messages%*s",
579 fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
581 printf ("has %*d message%s (%*d-%*d)",
582 nummsgdigits, fi[i].nummsg,
583 (fi[i].nummsg == 1) ? " " : "s",
584 lowmsgdigits, fi[i].lowmsg,
585 hghmsgdigits, fi[i].hghmsg);
586 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
588 printf ("; cur=%*d", curmsgdigits, fi[i].curmsg);
593 printf (";%*s (others)", curprinted ? 0 : curmsgdigits + 6, "");
599 * Print folder/message totals
601 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
604 printf ("TOTAL = %d message%c in %d folder%s.\n",
605 total_msgs, total_msgs != 1 ? 's' : ' ',
606 total_folders, total_folders != 1 ? "s" : "");
613 * Set the current message and sychronize sequences
617 sfold (struct msgs *mp, char *msg)
619 /* parse the message range/sequence/name and set SELECTED */
620 if (!m_convert (mp, msg))
623 if (mp->numsel > 1) {
624 admonish (NULL, "only one message at a time!");
627 seq_setprev (mp); /* set the previous-sequence */
628 seq_setcur (mp, mp->lowsel); /* set current message */
629 seq_save (mp); /* synchronize message sequences */
630 context_save (); /* save the context file */
637 * Do the read only folders
641 readonly_folders (void)
645 register struct node *np;
647 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
648 atrlen = strlen (atrcur);
650 for (np = m_defs; np; np = np->n_next)
651 if (ssequal (atrcur, np->n_name)
652 && !ssequal (nmhdir, np->n_name + atrlen))
653 get_folder_info (np->n_name + atrlen, NULL);