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, by the authors of nmh. See the
10 * COPYRIGHT file in the root directory of the nmh distribution for
11 * complete copyright information.
18 static struct swit switches[] = {
70 static int fshort = 0; /* output only folder names */
71 static int fcreat = 0; /* should we ask to create new folders? */
72 static int fpack = 0; /* are we packing the folder? */
73 static int fverb = 0; /* print actions taken while packing folder */
74 static int fheader = 0; /* should we output a header? */
75 static int frecurse = 0; /* recurse through subfolders */
76 static int ftotal = 0; /* should we output the totals? */
77 static int all = 0; /* should we output all folders */
79 static int total_folders = 0; /* total number of folders */
85 static char *stack = "Folder-Stack";
86 static char folder[BUFSIZ];
88 #define NUMFOLDERS 100
91 * This is how many folders we currently can hold in the array
92 * `folds'. We increase this amount by NUMFOLDERS at a time.
94 static int maxfolders;
98 * Structure to hold information about
99 * folders as we scan them.
107 int others; /* others == 1 if other files in folder */
108 int error; /* error == 1 for unreadable folder */
112 * Dynamically allocated space to hold
113 * all the folder information.
115 static struct FolderInfo *fi;
116 static int maxFolderInfo;
121 static void dodir (char *);
122 static int get_folder_info (char *, char *);
123 static void print_folders (void);
124 static int num_digits (int);
125 static int sfold (struct msgs *, char *);
126 static void addir (char *);
127 static void addfold (char *);
128 static int compare (char *, char *);
129 static void readonly_folders (void);
133 main (int argc, char **argv)
135 int printsw = 0, listsw = 0;
136 int pushsw = 0, popsw = 0;
137 char *cp, *dp, *msg = NULL, *argfolder = NULL;
138 char **ap, **argp, buf[BUFSIZ], **arguments;
142 setlocale(LC_ALL, "");
144 invo_name = r1bindex (argv[0], '/');
146 /* read user profile/context */
150 * If program was invoked with name ending
151 * in `s', then add switch `-all'.
153 if (argv[0][strlen (argv[0]) - 1] == 's')
156 arguments = getarguments (invo_name, argc, argv, 1);
159 while ((cp = *argp++)) {
161 switch (smatch (++cp, switches)) {
163 ambigsw (cp, switches);
166 adios (NULL, "-%s unknown", cp);
169 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
171 print_help (buf, switches, 1);
174 print_version(invo_name);
260 if (*cp == '+' || *cp == '@') {
262 adios (NULL, "only one folder at a time!");
264 argfolder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
267 adios (NULL, "only one (current) message at a time!");
273 if (!context_find ("path"))
274 free (path ("./", TFOLDER));
275 nmhdir = concat (m_maildir (""), "/", NULL);
278 * If we aren't working with the folder stack
279 * (-push, -pop, -list) then the default is to print.
281 if (pushsw == 0 && popsw == 0 && listsw == 0)
284 /* Pushing a folder onto the folder stack */
287 /* If no folder is given, the current folder and */
288 /* the top of the folder stack are swapped. */
289 if ((cp = context_find (stack))) {
291 ap = brkstring (dp, " ", "\n");
292 argfolder = getcpy(*ap++);
294 adios (NULL, "no other folder");
296 for (cp = getcpy (getfolder (1)); *ap; ap++)
297 cp = add (*ap, add (" ", cp));
299 context_replace (stack, cp); /* update folder stack */
301 /* update folder stack */
302 context_replace (stack,
303 (cp = context_find (stack))
304 ? concat (getfolder (1), " ", cp, NULL)
305 : getcpy (getfolder (1)));
309 /* Popping a folder off of the folder stack */
312 adios (NULL, "sorry, no folders allowed with -pop");
313 if ((cp = context_find (stack))) {
315 ap = brkstring (dp, " ", "\n");
316 argfolder = getcpy(*ap++);
318 adios (NULL, "folder stack empty");
321 /* if there's anything left in the stack */
324 cp = add (*ap, add (" ", cp));
325 context_replace (stack, cp); /* update folder stack */
327 context_del (stack); /* delete folder stack entry from context */
331 if (pushsw || popsw) {
332 cp = m_maildir(argfolder);
333 if (access (cp, F_OK) == NOTOK)
334 adios (cp, "unable to find folder");
335 context_replace (pfolder, argfolder); /* update current folder */
336 context_save (); /* save the context file */
340 /* Listing the folder stack */
342 printf ("%s", argfolder ? argfolder : getfolder (1));
343 if ((cp = context_find (stack))) {
345 for (ap = brkstring (dp, " ", "\n"); *ap; ap++)
355 /* Allocate initial space to record folder names */
356 maxfolders = NUMFOLDERS;
357 folds = mh_xmalloc (maxfolders * sizeof(char *));
359 /* Allocate initial space to record folder information */
360 maxFolderInfo = NUMFOLDERS;
361 fi = mh_xmalloc (maxFolderInfo * sizeof(*fi));
366 if (all || ftotal > 0) {
368 * If no folder is given, do them all
372 admonish (NULL, "no folder given for message %s", msg);
373 readonly_folders (); /* do any readonly folders */
374 strncpy (folder, (cp = context_find (pfolder)) ? cp : "", sizeof(folder));
377 strncpy (folder, argfolder, sizeof(folder));
378 if (get_folder_info (argfolder, msg)) {
379 context_replace (pfolder, argfolder);/* update current folder */
380 context_save (); /* save the context file */
383 * Since recurse wasn't done in get_folder_info(),
384 * we still need to list all level-1 sub-folders.
390 strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
393 * Check if folder exists. If not, then see if
394 * we should create it, or just exit.
396 if (stat (strncpy (buf, m_maildir (folder), sizeof(buf)), &st) == -1) {
398 adios (buf, "error on folder");
400 /* ask before creating folder */
401 cp = concat ("Create folder \"", buf, "\"? ", NULL);
405 } else if (fcreat == -1) {
406 /* do not create, so exit */
410 adios (NULL, "unable to create folder %s", buf);
413 if (get_folder_info (folder, msg) && argfolder) {
414 /* update current folder */
415 context_replace (pfolder, argfolder);
420 * Print out folder information
424 context_save (); /* save the context file */
429 * Base routine for scanning a folder
442 /* change directory to base of nmh directory */
443 if (chdir (nmhdir) == NOTOK)
444 adios (nmhdir, "unable to change directory to");
446 addir (strncpy (buffer, dir, sizeof(buffer)));
448 for (i = start; i < foldp; i++) {
449 get_folder_info (folds[i], NULL);
458 get_folder_info (char *fold, char *msg)
462 struct msgs *mp = NULL;
467 * if necessary, reallocate the space
468 * for folder information
470 if (total_folders >= maxFolderInfo) {
471 maxFolderInfo += NUMFOLDERS;
472 if ((fi = realloc (fi, maxFolderInfo * sizeof(*fi))) == NULL)
473 adios (NULL, "unable to re-allocate storage for folder info");
484 mailfile = m_maildir (fold);
486 if (!chdir (mailfile)) {
487 if ((ftotal > 0) || !fshort || msg || fpack) {
489 * create message structure and get folder info
491 if (!(mp = folder_read (fold))) {
492 admonish (NULL, "unable to read folder %s", fold);
496 /* set the current message */
497 if (msg && !sfold (mp, msg))
501 if (folder_pack (&mp, fverb) == -1)
503 seq_save (mp); /* synchronize the sequences */
504 context_save (); /* save the context file */
507 /* record info for this folder */
508 if ((ftotal > 0) || !fshort) {
509 fi[i].nummsg = mp->nummsg;
510 fi[i].curmsg = mp->curmsg;
511 fi[i].lowmsg = mp->lowmsg;
512 fi[i].hghmsg = mp->hghmsg;
513 fi[i].others = other_files (mp);
516 folder_free (mp); /* free folder/message structure */
522 if (frecurse && (fshort || fi[i].others) && (fi[i].error == 0))
528 * Print folder information
534 int i, len, hasempty = 0, curprinted;
535 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
536 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
537 int nummsgdigits, lowmsgdigits;
538 int hghmsgdigits, curmsgdigits;
539 char tmpname[BUFSIZ];
542 * compute a few values needed to for
543 * printing various fields
545 for (i = 0; i < total_folders; i++) {
546 /* length of folder name */
547 len = strlen (fi[i].name);
551 /* If folder has error, skip the rest */
555 /* calculate total number of messages */
556 total_msgs += fi[i].nummsg;
558 /* maximum number of messages */
559 if (fi[i].nummsg > maxnummsg)
560 maxnummsg = fi[i].nummsg;
562 /* maximum low message */
563 if (fi[i].lowmsg > maxlowmsg)
564 maxlowmsg = fi[i].lowmsg;
566 /* maximum high message */
567 if (fi[i].hghmsg > maxhghmsg)
568 maxhghmsg = fi[i].hghmsg;
570 /* maximum current message */
571 if (fi[i].curmsg >= fi[i].lowmsg &&
572 fi[i].curmsg <= fi[i].hghmsg &&
573 fi[i].curmsg > maxcurmsg)
574 maxcurmsg = fi[i].curmsg;
576 /* check for empty folders */
577 if (fi[i].nummsg == 0)
580 nummsgdigits = num_digits (maxnummsg);
581 lowmsgdigits = num_digits (maxlowmsg);
582 hghmsgdigits = num_digits (maxhghmsg);
583 curmsgdigits = num_digits (maxcurmsg);
585 if (hasempty && nummsgdigits < 2)
591 if (fheader > 0 || (all && !fshort && fheader >= 0))
592 printf ("%-*s %*s %-*s; %-*s %*s\n",
594 nummsgdigits + 13, "# MESSAGES",
595 lowmsgdigits + hghmsgdigits + 4, " RANGE",
596 curmsgdigits + 4, "CUR",
600 * Print folder information
602 if (all || fshort || ftotal < 1) {
603 for (i = 0; i < total_folders; i++) {
605 printf ("%s\n", fi[i].name);
609 /* Add `+' to end of name, if folder is current */
610 if (strcmp (folder, fi[i].name))
611 snprintf (tmpname, sizeof(tmpname), "%s", fi[i].name);
613 snprintf (tmpname, sizeof(tmpname), "%s+", fi[i].name);
616 printf ("%-*s is unreadable\n", maxlen+1, tmpname);
620 printf ("%-*s ", maxlen+1, tmpname);
622 curprinted = 0; /* remember if we print cur */
623 if (fi[i].nummsg == 0) {
624 printf ("has %*s messages%*s",
626 fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
628 printf ("has %*d message%s (%*d-%*d)",
629 nummsgdigits, fi[i].nummsg,
630 (fi[i].nummsg == 1) ? " " : "s",
631 lowmsgdigits, fi[i].lowmsg,
632 hghmsgdigits, fi[i].hghmsg);
633 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
635 printf ("; cur=%*d", curmsgdigits, fi[i].curmsg);
640 printf (";%*s (others)", curprinted ? 0 : curmsgdigits + 6, "");
646 * Print folder/message totals
648 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
651 printf ("TOTAL = %d message%c in %d folder%s.\n",
652 total_msgs, total_msgs != 1 ? 's' : ' ',
653 total_folders, total_folders != 1 ? "s" : "");
660 * Calculate the number of digits in a nonnegative integer
669 adios (NULL, "oops, num_digits called with negative value");
683 * Set the current message and sychronize sequences
687 sfold (struct msgs *mp, char *msg)
689 /* parse the message range/sequence/name and set SELECTED */
690 if (!m_convert (mp, msg))
693 if (mp->numsel > 1) {
694 admonish (NULL, "only one message at a time!");
697 seq_setprev (mp); /* set the previous-sequence */
698 seq_setcur (mp, mp->lowsel);/* set current message */
699 seq_save (mp); /* synchronize message sequences */
700 context_save (); /* save the context file */
715 cp = name + strlen (name);
720 * A hack to skip over a leading
721 * "./" in folder names.
723 base = strcmp (name, "./") ? name : name + 2;
725 /* short-cut to see if directory has any sub-directories */
726 if (stat (name, &st) != -1 && st.st_nlink == 2)
729 if (!(dd = opendir (name))) {
730 admonish (name, "unable to read directory ");
735 * Keep track of the number of directories we've seen
736 * so we can quit stat'ing early, if we've seen them all.
740 while (nlink && (dp = readdir (dd))) {
741 if (!strcmp (dp->d_name, ".") || !strcmp (dp->d_name, "..")) {
745 if (cp + NLENGTH(dp) + 2 >= name + BUFSIZ)
747 strcpy (cp, dp->d_name);
748 if (stat (name, &st) != -1 && S_ISDIR(st.st_mode)) {
750 * Check if this was really a symbolic link pointing at
751 * a directory. If not, then decrement link count.
753 if (lstat (name, &st) == -1)
764 * Add the folder name into the
765 * list in a sorted fashion.
774 /* if necessary, reallocate the space for folder names */
775 if (foldp >= maxfolders) {
776 maxfolders += NUMFOLDERS;
777 if ((folds = realloc (folds, maxfolders * sizeof(char *))) == NULL)
778 adios (NULL, "unable to re-allocate storage for folder names");
782 for (i = start; i < foldp; i++)
783 if (compare (cp, folds[i]) < 0) {
784 for (j = foldp - 1; j >= i; j--)
785 folds[j + 1] = folds[j];
796 compare (char *s1, char *s2)
801 if ((i = *s1++ - *s2++))
808 * Do the read only folders
812 readonly_folders (void)
816 register struct node *np;
818 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
819 atrlen = strlen (atrcur);
821 for (np = m_defs; np; np = np->n_next)
822 if (ssequal (atrcur, np->n_name)
823 && !ssequal (nmhdir, np->n_name + atrlen))
824 get_folder_info (np->n_name + atrlen, NULL);