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)
462 boolean crawl_children;
463 get_folder_info_body (fold, NULL, &crawl_children);
465 return crawl_children;
469 get_folder_info (char *fold, char *msg)
471 boolean crawl_children;
474 retval = get_folder_info_body (fold, msg, &crawl_children);
476 if (crawl_children) {
477 crawl_folders (fold, get_folder_info_callback, NULL);
484 * Print folder information
490 int i, len, hasempty = 0, curprinted;
491 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
492 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
493 int nummsgdigits, lowmsgdigits;
494 int hghmsgdigits, curmsgdigits;
495 char tmpname[BUFSIZ];
498 * compute a few values needed to for
499 * printing various fields
501 for (i = 0; i < total_folders; i++) {
502 /* length of folder name */
503 len = strlen (fi[i].name);
507 /* If folder has error, skip the rest */
511 /* calculate total number of messages */
512 total_msgs += fi[i].nummsg;
514 /* maximum number of messages */
515 if (fi[i].nummsg > maxnummsg)
516 maxnummsg = fi[i].nummsg;
518 /* maximum low message */
519 if (fi[i].lowmsg > maxlowmsg)
520 maxlowmsg = fi[i].lowmsg;
522 /* maximum high message */
523 if (fi[i].hghmsg > maxhghmsg)
524 maxhghmsg = fi[i].hghmsg;
526 /* maximum current message */
527 if (fi[i].curmsg >= fi[i].lowmsg &&
528 fi[i].curmsg <= fi[i].hghmsg &&
529 fi[i].curmsg > maxcurmsg)
530 maxcurmsg = fi[i].curmsg;
532 /* check for empty folders */
533 if (fi[i].nummsg == 0)
536 nummsgdigits = num_digits (maxnummsg);
537 lowmsgdigits = num_digits (maxlowmsg);
538 hghmsgdigits = num_digits (maxhghmsg);
539 curmsgdigits = num_digits (maxcurmsg);
541 if (hasempty && nummsgdigits < 2)
547 if (fheader > 0 || (all && !fshort && fheader >= 0))
548 printf ("%-*s %*s %-*s; %-*s %*s\n",
550 nummsgdigits + 13, "# MESSAGES",
551 lowmsgdigits + hghmsgdigits + 4, " RANGE",
552 curmsgdigits + 4, "CUR",
556 * Print folder information
558 if (all || fshort || ftotal < 1) {
559 for (i = 0; i < total_folders; i++) {
561 printf ("%s\n", fi[i].name);
565 /* Add `+' to end of name, if folder is current */
566 if (strcmp (folder, fi[i].name))
567 snprintf (tmpname, sizeof(tmpname), "%s", fi[i].name);
569 snprintf (tmpname, sizeof(tmpname), "%s+", fi[i].name);
572 printf ("%-*s is unreadable\n", maxlen+1, tmpname);
576 printf ("%-*s ", maxlen+1, tmpname);
578 curprinted = 0; /* remember if we print cur */
579 if (fi[i].nummsg == 0) {
580 printf ("has %*s messages%*s",
582 fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
584 printf ("has %*d message%s (%*d-%*d)",
585 nummsgdigits, fi[i].nummsg,
586 (fi[i].nummsg == 1) ? " " : "s",
587 lowmsgdigits, fi[i].lowmsg,
588 hghmsgdigits, fi[i].hghmsg);
589 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
591 printf ("; cur=%*d", curmsgdigits, fi[i].curmsg);
596 printf (";%*s (others)", curprinted ? 0 : curmsgdigits + 6, "");
602 * Print folder/message totals
604 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
607 printf ("TOTAL = %d message%c in %d folder%s.\n",
608 total_msgs, total_msgs != 1 ? 's' : ' ',
609 total_folders, total_folders != 1 ? "s" : "");
616 * Set the current message and sychronize sequences
620 sfold (struct msgs *mp, char *msg)
622 /* parse the message range/sequence/name and set SELECTED */
623 if (!m_convert (mp, msg))
626 if (mp->numsel > 1) {
627 admonish (NULL, "only one message at a time!");
630 seq_setprev (mp); /* set the previous-sequence */
631 seq_setcur (mp, mp->lowsel);/* set current message */
632 seq_save (mp); /* synchronize message sequences */
633 context_save (); /* save the context file */
640 * Do the read only folders
644 readonly_folders (void)
648 register struct node *np;
650 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
651 atrlen = strlen (atrcur);
653 for (np = m_defs; np; np = np->n_next)
654 if (ssequal (atrcur, np->n_name)
655 && !ssequal (nmhdir, np->n_name + atrlen))
656 get_folder_info (np->n_name + atrlen, NULL);