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.
17 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 */
86 static char *stack = "Folder-Stack";
87 static char folder[BUFSIZ];
89 #define NUMFOLDERS 100
92 * This is how many folders we currently can hold in the array
93 * `folds'. We increase this amount by NUMFOLDERS at a time.
95 static int maxfolders;
99 * Structure to hold information about
100 * folders as we scan them.
108 int others; /* others == 1 if other files in folder */
109 int error; /* error == 1 for unreadable folder */
113 * Dynamically allocated space to hold
114 * all the folder information.
116 static struct FolderInfo *fi;
117 static int maxFolderInfo;
122 static void dodir (char *);
123 static int get_folder_info (char *, char *);
124 static void print_folders (void);
125 static int num_digits (int);
126 static int sfold (struct msgs *, char *);
127 static void addir (char *);
128 static void addfold (char *);
129 static int compare (char *, char *);
130 static void readonly_folders (void);
134 main (int argc, char **argv)
136 int printsw = 0, listsw = 0;
137 int pushsw = 0, popsw = 0;
138 char *cp, *dp, *msg = NULL, *argfolder = NULL;
139 char **ap, **argp, buf[BUFSIZ], **arguments;
143 setlocale(LC_ALL, "");
145 invo_name = r1bindex (argv[0], '/');
147 /* read user profile/context */
151 * If program was invoked with name ending
152 * in `s', then add switch `-all'.
154 if (argv[0][strlen (argv[0]) - 1] == 's')
157 arguments = getarguments (invo_name, argc, argv, 1);
160 while ((cp = *argp++)) {
162 switch (smatch (++cp, switches)) {
164 ambigsw (cp, switches);
167 adios (NULL, "-%s unknown", cp);
170 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
172 print_help (buf, switches, 1);
175 print_version(invo_name);
261 if (*cp == '+' || *cp == '@') {
263 adios (NULL, "only one folder at a time!");
265 argfolder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
268 adios (NULL, "only one (current) message at a time!");
274 if (!context_find ("path"))
275 free (path ("./", TFOLDER));
276 nmhdir = concat (m_maildir (""), "/", NULL);
279 * If we aren't working with the folder stack
280 * (-push, -pop, -list) then the default is to print.
282 if (pushsw == 0 && popsw == 0 && listsw == 0)
285 /* Pushing a folder onto the folder stack */
288 /* If no folder is given, the current folder and */
289 /* the top of the folder stack are swapped. */
290 if ((cp = context_find (stack))) {
292 ap = brkstring (dp, " ", "\n");
293 argfolder = getcpy(*ap++);
295 adios (NULL, "no other folder");
297 for (cp = getcpy (getfolder (1)); *ap; ap++)
298 cp = add (*ap, add (" ", cp));
300 context_replace (stack, cp); /* update folder stack */
302 /* update folder stack */
303 context_replace (stack,
304 (cp = context_find (stack))
305 ? concat (getfolder (1), " ", cp, NULL)
306 : getcpy (getfolder (1)));
310 /* Popping a folder off of the folder stack */
313 adios (NULL, "sorry, no folders allowed with -pop");
314 if ((cp = context_find (stack))) {
316 ap = brkstring (dp, " ", "\n");
317 argfolder = getcpy(*ap++);
319 adios (NULL, "folder stack empty");
322 /* if there's anything left in the stack */
325 cp = add (*ap, add (" ", cp));
326 context_replace (stack, cp); /* update folder stack */
328 context_del (stack); /* delete folder stack entry from context */
332 if (pushsw || popsw) {
333 cp = m_maildir(argfolder);
334 if (access (cp, F_OK) == NOTOK)
335 adios (cp, "unable to find folder");
336 context_replace (pfolder, argfolder); /* update current folder */
337 context_save (); /* save the context file */
341 /* Listing the folder stack */
343 printf ("%s", argfolder ? argfolder : getfolder (1));
344 if ((cp = context_find (stack))) {
346 for (ap = brkstring (dp, " ", "\n"); *ap; ap++)
356 /* Allocate initial space to record folder names */
357 maxfolders = NUMFOLDERS;
358 if ((folds = malloc (maxfolders * sizeof(char *))) == NULL)
359 adios (NULL, "unable to allocate storage for folder names");
361 /* Allocate initial space to record folder information */
362 maxFolderInfo = NUMFOLDERS;
363 if ((fi = malloc (maxFolderInfo * sizeof(*fi))) == NULL)
364 adios (NULL, "unable to allocate storage for folder info");
369 if (all || ftotal > 0) {
371 * If no folder is given, do them all
375 admonish (NULL, "no folder given for message %s", msg);
376 readonly_folders (); /* do any readonly folders */
377 strncpy (folder, (cp = context_find (pfolder)) ? cp : "", sizeof(folder));
380 strncpy (folder, argfolder, sizeof(folder));
381 if (get_folder_info (argfolder, msg)) {
382 context_replace (pfolder, argfolder);/* update current folder */
383 context_save (); /* save the context file */
386 * Since recurse wasn't done in get_folder_info(),
387 * we still need to list all level-1 sub-folders.
393 strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
396 * Check if folder exists. If not, then see if
397 * we should create it, or just exit.
399 if (stat (strncpy (buf, m_maildir (folder), sizeof(buf)), &st) == -1) {
401 adios (buf, "error on folder");
403 /* ask before creating folder */
404 cp = concat ("Create folder \"", buf, "\"? ", NULL);
408 } else if (fcreat == -1) {
409 /* do not create, so exit */
413 adios (NULL, "unable to create folder %s", buf);
416 if (get_folder_info (folder, msg) && argfolder) {
417 /* update current folder */
418 context_replace (pfolder, argfolder);
423 * Print out folder information
427 context_save (); /* save the context file */
432 * Base routine for scanning a folder
445 /* change directory to base of nmh directory */
446 if (chdir (nmhdir) == NOTOK)
447 adios (nmhdir, "unable to change directory to");
449 addir (strncpy (buffer, dir, sizeof(buffer)));
451 for (i = start; i < foldp; i++) {
452 get_folder_info (folds[i], NULL);
461 get_folder_info (char *fold, char *msg)
465 struct msgs *mp = NULL;
470 * if necessary, reallocate the space
471 * for folder information
473 if (total_folders >= maxFolderInfo) {
474 maxFolderInfo += NUMFOLDERS;
475 if ((fi = realloc (fi, maxFolderInfo * sizeof(*fi))) == NULL)
476 adios (NULL, "unable to re-allocate storage for folder info");
487 mailfile = m_maildir (fold);
489 if (!chdir (mailfile)) {
490 if ((ftotal > 0) || !fshort || msg || fpack) {
492 * create message structure and get folder info
494 if (!(mp = folder_read (fold))) {
495 admonish (NULL, "unable to read folder %s", fold);
499 /* set the current message */
500 if (msg && !sfold (mp, msg))
504 if (folder_pack (&mp, fverb) == -1)
506 seq_save (mp); /* synchronize the sequences */
507 context_save (); /* save the context file */
510 /* record info for this folder */
511 if ((ftotal > 0) || !fshort) {
512 fi[i].nummsg = mp->nummsg;
513 fi[i].curmsg = mp->curmsg;
514 fi[i].lowmsg = mp->lowmsg;
515 fi[i].hghmsg = mp->hghmsg;
516 fi[i].others = other_files (mp);
519 folder_free (mp); /* free folder/message structure */
525 if (frecurse && (fshort || fi[i].others) && (fi[i].error == 0))
531 * Print folder information
537 int i, len, hasempty = 0, curprinted;
538 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
539 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
540 int nummsgdigits, lowmsgdigits;
541 int hghmsgdigits, curmsgdigits;
542 char tmpname[BUFSIZ];
545 * compute a few values needed to for
546 * printing various fields
548 for (i = 0; i < total_folders; i++) {
549 /* length of folder name */
550 len = strlen (fi[i].name);
554 /* If folder has error, skip the rest */
558 /* calculate total number of messages */
559 total_msgs += fi[i].nummsg;
561 /* maximum number of messages */
562 if (fi[i].nummsg > maxnummsg)
563 maxnummsg = fi[i].nummsg;
565 /* maximum low message */
566 if (fi[i].lowmsg > maxlowmsg)
567 maxlowmsg = fi[i].lowmsg;
569 /* maximum high message */
570 if (fi[i].hghmsg > maxhghmsg)
571 maxhghmsg = fi[i].hghmsg;
573 /* maximum current message */
574 if (fi[i].curmsg >= fi[i].lowmsg &&
575 fi[i].curmsg <= fi[i].hghmsg &&
576 fi[i].curmsg > maxcurmsg)
577 maxcurmsg = fi[i].curmsg;
579 /* check for empty folders */
580 if (fi[i].nummsg == 0)
583 nummsgdigits = num_digits (maxnummsg);
584 lowmsgdigits = num_digits (maxlowmsg);
585 hghmsgdigits = num_digits (maxhghmsg);
586 curmsgdigits = num_digits (maxcurmsg);
588 if (hasempty && nummsgdigits < 2)
594 if (fheader > 0 || (all && !fshort && fheader >= 0))
595 printf ("%-*s %*s %-*s; %-*s %*s\n",
597 nummsgdigits + 13, "# MESSAGES",
598 lowmsgdigits + hghmsgdigits + 4, " RANGE",
599 curmsgdigits + 4, "CUR",
603 * Print folder information
605 if (all || fshort || ftotal < 1) {
606 for (i = 0; i < total_folders; i++) {
608 printf ("%s\n", fi[i].name);
612 /* Add `+' to end of name, if folder is current */
613 if (strcmp (folder, fi[i].name))
614 snprintf (tmpname, sizeof(tmpname), "%s", fi[i].name);
616 snprintf (tmpname, sizeof(tmpname), "%s+", fi[i].name);
619 printf ("%-*s is unreadable\n", maxlen+1, tmpname);
623 printf ("%-*s ", maxlen+1, tmpname);
625 curprinted = 0; /* remember if we print cur */
626 if (fi[i].nummsg == 0) {
627 printf ("has %*s messages%*s",
629 fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
631 printf ("has %*d message%s (%*d-%*d)",
632 nummsgdigits, fi[i].nummsg,
633 (fi[i].nummsg == 1) ? " " : "s",
634 lowmsgdigits, fi[i].lowmsg,
635 hghmsgdigits, fi[i].hghmsg);
636 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
638 printf ("; cur=%*d", curmsgdigits, fi[i].curmsg);
643 printf (";%*s (others)", curprinted ? 0 : curmsgdigits + 6, "");
649 * Print folder/message totals
651 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
654 printf ("TOTAL = %d message%c in %d folder%s.\n",
655 total_msgs, total_msgs != 1 ? 's' : ' ',
656 total_folders, total_folders != 1 ? "s" : "");
663 * Calculate the number of digits in a nonnegative integer
672 adios (NULL, "oops, num_digits called with negative value");
686 * Set the current message and sychronize sequences
690 sfold (struct msgs *mp, char *msg)
692 /* parse the message range/sequence/name and set SELECTED */
693 if (!m_convert (mp, msg))
696 if (mp->numsel > 1) {
697 admonish (NULL, "only one message at a time!");
700 seq_setprev (mp); /* set the previous-sequence */
701 seq_setcur (mp, mp->lowsel);/* set current message */
702 seq_save (mp); /* synchronize message sequences */
703 context_save (); /* save the context file */
718 cp = name + strlen (name);
723 * A hack to skip over a leading
724 * "./" in folder names.
726 base = strcmp (name, "./") ? name : name + 2;
728 /* short-cut to see if directory has any sub-directories */
729 if (stat (name, &st) != -1 && st.st_nlink == 2)
732 if (!(dd = opendir (name))) {
733 admonish (name, "unable to read directory ");
738 * Keep track of the number of directories we've seen
739 * so we can quit stat'ing early, if we've seen them all.
743 while (nlink && (dp = readdir (dd))) {
744 if (!strcmp (dp->d_name, ".") || !strcmp (dp->d_name, "..")) {
748 if (cp + NLENGTH(dp) + 2 >= name + BUFSIZ)
750 strcpy (cp, dp->d_name);
751 if (stat (name, &st) != -1 && S_ISDIR(st.st_mode)) {
753 * Check if this was really a symbolic link pointing at
754 * a directory. If not, then decrement link count.
756 if (lstat (name, &st) == -1)
767 * Add the folder name into the
768 * list in a sorted fashion.
777 /* if necessary, reallocate the space for folder names */
778 if (foldp >= maxfolders) {
779 maxfolders += NUMFOLDERS;
780 if ((folds = realloc (folds, maxfolders * sizeof(char *))) == NULL)
781 adios (NULL, "unable to re-allocate storage for folder names");
785 for (i = start; i < foldp; i++)
786 if (compare (cp, folds[i]) < 0) {
787 for (j = foldp - 1; j >= i; j--)
788 folds[j + 1] = folds[j];
799 compare (char *s1, char *s2)
804 if ((i = *s1++ - *s2++))
811 * Do the read only folders
815 readonly_folders (void)
819 register struct node *np;
821 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
822 atrlen = strlen (atrcur);
824 for (np = m_defs; np; np = np->n_next)
825 if (ssequal (atrcur, np->n_name)
826 && !ssequal (nmhdir, np->n_name + atrlen))
827 get_folder_info (np->n_name + atrlen, NULL);