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
9 * This code is Copyright (c) 2002, 2008, by the authors of nmh. See the
10 * COPYRIGHT file in the root directory of the nmh distribution for
11 * complete copyright information.
15 #include <h/crawl_folders.h>
19 static struct swit switches[] = {
71 static int fshort = 0; /* output only folder names */
72 static int fcreat = 0; /* should we ask to create new folders? */
73 static int fpack = 0; /* are we packing the folder? */
74 static int fverb = 0; /* print actions taken while packing folder */
75 static int fheader = 0; /* should we output a header? */
76 static int frecurse = 0; /* recurse through subfolders */
77 static int ftotal = 0; /* should we output the totals? */
78 static int all = 0; /* should we output all folders */
80 static int total_folders = 0; /* total number of folders */
83 static char *stack = "Folder-Stack";
84 static char folder[BUFSIZ];
87 * Structure to hold information about
88 * folders as we scan them.
96 int others; /* others == 1 if other files in folder */
97 int error; /* error == 1 for unreadable folder */
101 * Dynamically allocated space to hold
102 * all the folder information.
104 static struct FolderInfo *fi;
105 static int maxFolderInfo;
110 static int get_folder_info (char *, char *);
111 static crawl_callback_t get_folder_info_callback;
112 static void print_folders (void);
113 static int sfold (struct msgs *, char *);
114 static void readonly_folders (void);
118 main (int argc, char **argv)
120 int printsw = 0, listsw = 0;
121 int pushsw = 0, popsw = 0;
122 char *cp, *dp, *msg = NULL, *argfolder = NULL;
123 char **ap, **argp, buf[BUFSIZ], **arguments;
126 setlocale(LC_ALL, "");
128 invo_name = r1bindex (argv[0], '/');
130 /* read user profile/context */
134 * If program was invoked with name ending
135 * in `s', then add switch `-all'.
137 if (argv[0][strlen (argv[0]) - 1] == 's')
140 arguments = getarguments (invo_name, argc, argv, 1);
143 while ((cp = *argp++)) {
145 switch (smatch (++cp, switches)) {
147 ambigsw (cp, switches);
150 adios (NULL, "-%s unknown", cp);
153 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
155 print_help (buf, switches, 1);
158 print_version(invo_name);
244 if (*cp == '+' || *cp == '@') {
246 adios (NULL, "only one folder at a time!");
248 argfolder = pluspath (cp);
251 adios (NULL, "only one (current) message at a time!");
257 if (!context_find ("path"))
258 free (path ("./", TFOLDER));
259 nmhdir = concat (m_maildir (""), "/", NULL);
262 * If we aren't working with the folder stack
263 * (-push, -pop, -list) then the default is to print.
265 if (pushsw == 0 && popsw == 0 && listsw == 0)
268 /* Pushing a folder onto the folder stack */
271 /* If no folder is given, the current folder and */
272 /* the top of the folder stack are swapped. */
273 if ((cp = context_find (stack))) {
275 ap = brkstring (dp, " ", "\n");
276 argfolder = getcpy(*ap++);
278 adios (NULL, "no other folder");
280 for (cp = getcpy (getfolder (1)); *ap; ap++)
281 cp = add (*ap, add (" ", cp));
283 context_replace (stack, cp); /* update folder stack */
285 /* update folder stack */
286 context_replace (stack,
287 (cp = context_find (stack))
288 ? concat (getfolder (1), " ", cp, NULL)
289 : getcpy (getfolder (1)));
293 /* Popping a folder off of the folder stack */
296 adios (NULL, "sorry, no folders allowed with -pop");
297 if ((cp = context_find (stack))) {
299 ap = brkstring (dp, " ", "\n");
300 argfolder = getcpy(*ap++);
302 adios (NULL, "folder stack empty");
305 /* if there's anything left in the stack */
308 cp = add (*ap, add (" ", cp));
309 context_replace (stack, cp); /* update folder stack */
311 context_del (stack); /* delete folder stack entry from context */
315 if (pushsw || popsw) {
316 cp = m_maildir(argfolder);
317 if (access (cp, F_OK) == NOTOK)
318 adios (cp, "unable to find folder");
319 context_replace (pfolder, argfolder); /* update current folder */
320 context_save (); /* save the context file */
324 /* Listing the folder stack */
326 printf ("%s", argfolder ? argfolder : getfolder (1));
327 if ((cp = context_find (stack))) {
329 for (ap = brkstring (dp, " ", "\n"); *ap; ap++)
339 /* Allocate initial space to record folder information */
340 maxFolderInfo = CRAWL_NUMFOLDERS;
341 fi = mh_xmalloc (maxFolderInfo * sizeof(*fi));
346 if (all || ftotal > 0) {
348 * If no folder is given, do them all
350 /* change directory to base of nmh directory for crawl_folders */
351 if (chdir (nmhdir) == NOTOK)
352 adios (nmhdir, "unable to change directory to");
355 admonish (NULL, "no folder given for message %s", msg);
356 readonly_folders (); /* do any readonly folders */
357 strncpy (folder, (cp = context_find (pfolder)) ? cp : "", sizeof(folder));
358 crawl_folders (".", get_folder_info_callback, NULL);
360 strncpy (folder, argfolder, sizeof(folder));
361 if (get_folder_info (argfolder, msg)) {
362 context_replace (pfolder, argfolder);/* update current folder */
363 context_save (); /* save the context file */
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, NULL);
373 strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
376 * Check if folder exists. If not, then see if
377 * we should create it, or just exit.
379 create_folder (m_maildir (folder), fcreat, done);
381 if (get_folder_info (folder, msg) && argfolder) {
382 /* update current folder */
383 context_replace (pfolder, argfolder);
388 * Print out folder information
392 context_save (); /* save the context file */
398 get_folder_info_body (char *fold, char *msg, boolean *crawl_children)
401 struct msgs *mp = NULL;
406 * if necessary, reallocate the space
407 * for folder information
409 if (total_folders >= maxFolderInfo) {
410 maxFolderInfo += CRAWL_NUMFOLDERS;
411 fi = mh_xrealloc (fi, maxFolderInfo * sizeof(*fi));
422 if ((ftotal > 0) || !fshort || msg || fpack) {
424 * create message structure and get folder info
426 if (!(mp = folder_read (fold))) {
427 admonish (NULL, "unable to read folder %s", fold);
431 /* set the current message */
432 if (msg && !sfold (mp, msg))
436 if (folder_pack (&mp, fverb) == -1)
438 seq_save (mp); /* synchronize the sequences */
439 context_save (); /* save the context file */
442 /* record info for this folder */
443 if ((ftotal > 0) || !fshort) {
444 fi[i].nummsg = mp->nummsg;
445 fi[i].curmsg = mp->curmsg;
446 fi[i].lowmsg = mp->lowmsg;
447 fi[i].hghmsg = mp->hghmsg;
448 fi[i].others = other_files (mp);
451 folder_free (mp); /* free folder/message structure */
454 *crawl_children = (frecurse && (fshort || fi[i].others)
455 && (fi[i].error == 0));
460 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);