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 /* 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 /* update current folder */
318 context_replace (pfolder, argfolder);
319 context_save (); /* save the context file */
323 /* Listing the folder stack */
325 printf ("%s", argfolder ? argfolder : getfolder (1));
326 if ((cp = context_find (stack))) {
328 for (ap = brkstring (dp, " ", "\n"); *ap; ap++)
338 /* Allocate initial space to record folder information */
339 maxFolderInfo = CRAWL_NUMFOLDERS;
340 fi = mh_xmalloc (maxFolderInfo * sizeof(*fi));
345 if (all || ftotal > 0) {
347 ** If no folder is given, do them all
350 ** change directory to base of nmh directory for
353 if (chdir (nmhdir) == NOTOK)
354 adios (nmhdir, "unable to change directory to");
357 admonish (NULL, "no folder given for message %s", msg);
358 readonly_folders (); /* do any readonly folders */
359 strncpy (folder, (cp = context_find (pfolder)) ? cp : "", sizeof(folder));
360 crawl_folders (".", get_folder_info_callback, NULL);
362 strncpy (folder, argfolder, sizeof(folder));
363 if (get_folder_info (argfolder, msg)) {
364 /* update current folder */
365 context_replace (pfolder, argfolder);
369 ** Since recurse wasn't done in get_folder_info(),
370 ** we still need to list all level-1 sub-folders.
373 crawl_folders (folder, get_folder_info_callback, NULL);
376 strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
379 ** Check if folder exists. If not, then see if
380 ** we should create it, or just exit.
382 create_folder (m_maildir (folder), fcreat, done);
384 if (get_folder_info (folder, msg) && argfolder) {
385 /* update current folder */
386 context_replace (pfolder, argfolder);
391 ** Print out folder information
401 get_folder_info_body (char *fold, char *msg, boolean *crawl_children)
404 struct msgs *mp = NULL;
409 ** if necessary, reallocate the space
410 ** for folder information
412 if (total_folders >= maxFolderInfo) {
413 maxFolderInfo += CRAWL_NUMFOLDERS;
414 fi = mh_xrealloc (fi, maxFolderInfo * sizeof(*fi));
425 if ((ftotal > 0) || !fshort || msg || fpack) {
427 ** create message structure and get folder info
429 if (!(mp = folder_read (fold))) {
430 admonish (NULL, "unable to read folder %s", fold);
434 /* set the current message */
435 if (msg && !sfold (mp, msg))
439 if (folder_pack (&mp, fverb) == -1)
441 seq_save (mp); /* synchronize the sequences */
442 context_save (); /* save the context file */
445 /* record info for this folder */
446 if ((ftotal > 0) || !fshort) {
447 fi[i].nummsg = mp->nummsg;
448 fi[i].curmsg = mp->curmsg;
449 fi[i].lowmsg = mp->lowmsg;
450 fi[i].hghmsg = mp->hghmsg;
451 fi[i].others = other_files (mp);
454 folder_free (mp); /* free folder/message structure */
457 *crawl_children = (frecurse && (fshort || fi[i].others)
458 && (fi[i].error == 0));
463 get_folder_info_callback (char *fold, void *baton)
465 boolean crawl_children;
466 get_folder_info_body (fold, NULL, &crawl_children);
468 return crawl_children;
472 get_folder_info (char *fold, char *msg)
474 boolean crawl_children;
477 retval = get_folder_info_body (fold, msg, &crawl_children);
479 if (crawl_children) {
480 crawl_folders (fold, get_folder_info_callback, NULL);
486 ** Print folder information
492 int i, len, hasempty = 0, curprinted;
493 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
494 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
495 int nummsgdigits, lowmsgdigits;
496 int hghmsgdigits, curmsgdigits;
497 char tmpname[BUFSIZ];
500 ** compute a few values needed to for
501 ** printing various fields
503 for (i = 0; i < total_folders; i++) {
504 /* length of folder name */
505 len = strlen (fi[i].name);
509 /* If folder has error, skip the rest */
513 /* calculate total number of messages */
514 total_msgs += fi[i].nummsg;
516 /* maximum number of messages */
517 if (fi[i].nummsg > maxnummsg)
518 maxnummsg = fi[i].nummsg;
520 /* maximum low message */
521 if (fi[i].lowmsg > maxlowmsg)
522 maxlowmsg = fi[i].lowmsg;
524 /* maximum high message */
525 if (fi[i].hghmsg > maxhghmsg)
526 maxhghmsg = fi[i].hghmsg;
528 /* maximum current message */
529 if (fi[i].curmsg >= fi[i].lowmsg &&
530 fi[i].curmsg <= fi[i].hghmsg &&
531 fi[i].curmsg > maxcurmsg)
532 maxcurmsg = fi[i].curmsg;
534 /* check for empty folders */
535 if (fi[i].nummsg == 0)
538 nummsgdigits = num_digits (maxnummsg);
539 lowmsgdigits = num_digits (maxlowmsg);
540 hghmsgdigits = num_digits (maxhghmsg);
541 curmsgdigits = num_digits (maxcurmsg);
543 if (hasempty && nummsgdigits < 2)
549 if (fheader > 0 || (all && !fshort && fheader >= 0))
550 printf ("%-*s %*s %-*s; %-*s %*s\n",
552 nummsgdigits + 13, "# MESSAGES",
553 lowmsgdigits + hghmsgdigits + 4, " RANGE",
554 curmsgdigits + 4, "CUR",
558 ** Print folder information
560 if (all || fshort || ftotal < 1) {
561 for (i = 0; i < total_folders; i++) {
563 printf ("%s\n", fi[i].name);
567 /* Add `+' to end of name, if folder is current */
568 if (strcmp (folder, fi[i].name))
569 snprintf (tmpname, sizeof(tmpname), "%s", fi[i].name);
571 snprintf (tmpname, sizeof(tmpname), "%s+", fi[i].name);
574 printf ("%-*s is unreadable\n", maxlen+1, tmpname);
578 printf ("%-*s ", maxlen+1, tmpname);
580 curprinted = 0; /* remember if we print cur */
581 if (fi[i].nummsg == 0) {
582 printf ("has %*s messages%*s",
584 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) ? " " : "s",
589 lowmsgdigits, fi[i].lowmsg,
590 hghmsgdigits, fi[i].hghmsg);
591 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
593 printf ("; cur=%*d", curmsgdigits, fi[i].curmsg);
598 printf (";%*s (others)", curprinted ? 0 : curmsgdigits + 6, "");
604 ** Print folder/message totals
606 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
609 printf ("TOTAL = %d message%c in %d folder%s.\n",
610 total_msgs, total_msgs != 1 ? 's' : ' ',
611 total_folders, total_folders != 1 ? "s" : "");
618 ** Set the current message and sychronize sequences
622 sfold (struct msgs *mp, char *msg)
624 /* parse the message range/sequence/name and set SELECTED */
625 if (!m_convert (mp, msg))
628 if (mp->numsel > 1) {
629 admonish (NULL, "only one message at a time!");
632 seq_setprev (mp); /* set the previous-sequence */
633 seq_setcur (mp, mp->lowsel); /* set current message */
634 seq_save (mp); /* synchronize message sequences */
635 context_save (); /* save the context file */
642 ** Do the read only folders
646 readonly_folders (void)
650 register struct node *np;
652 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
653 atrlen = strlen (atrcur);
655 for (np = m_defs; np; np = np->n_next)
656 if (ssequal (atrcur, np->n_name)
657 && !ssequal (nmhdir, np->n_name + atrlen))
658 get_folder_info (np->n_name + atrlen, NULL);