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 sfold (struct msgs *, char *);
125 static void addir (char *);
126 static void addfold (char *);
127 static int compare (char *, char *);
128 static void readonly_folders (void);
132 main (int argc, char **argv)
134 int printsw = 0, listsw = 0;
135 int pushsw = 0, popsw = 0;
136 char *cp, *dp, *msg = NULL, *argfolder = NULL;
137 char **ap, **argp, buf[BUFSIZ], **arguments;
140 setlocale(LC_ALL, "");
142 invo_name = r1bindex (argv[0], '/');
144 /* read user profile/context */
148 * If program was invoked with name ending
149 * in `s', then add switch `-all'.
151 if (argv[0][strlen (argv[0]) - 1] == 's')
154 arguments = getarguments (invo_name, argc, argv, 1);
157 while ((cp = *argp++)) {
159 switch (smatch (++cp, switches)) {
161 ambigsw (cp, switches);
164 adios (NULL, "-%s unknown", cp);
167 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
169 print_help (buf, switches, 1);
172 print_version(invo_name);
258 if (*cp == '+' || *cp == '@') {
260 adios (NULL, "only one folder at a time!");
262 argfolder = pluspath (cp);
265 adios (NULL, "only one (current) message at a time!");
271 if (!context_find ("path"))
272 free (path ("./", TFOLDER));
273 nmhdir = concat (m_maildir (""), "/", NULL);
276 * If we aren't working with the folder stack
277 * (-push, -pop, -list) then the default is to print.
279 if (pushsw == 0 && popsw == 0 && listsw == 0)
282 /* Pushing a folder onto the folder stack */
285 /* If no folder is given, the current folder and */
286 /* the top of the folder stack are swapped. */
287 if ((cp = context_find (stack))) {
289 ap = brkstring (dp, " ", "\n");
290 argfolder = getcpy(*ap++);
292 adios (NULL, "no other folder");
294 for (cp = getcpy (getfolder (1)); *ap; ap++)
295 cp = add (*ap, add (" ", cp));
297 context_replace (stack, cp); /* update folder stack */
299 /* update folder stack */
300 context_replace (stack,
301 (cp = context_find (stack))
302 ? concat (getfolder (1), " ", cp, NULL)
303 : getcpy (getfolder (1)));
307 /* Popping a folder off of the folder stack */
310 adios (NULL, "sorry, no folders allowed with -pop");
311 if ((cp = context_find (stack))) {
313 ap = brkstring (dp, " ", "\n");
314 argfolder = getcpy(*ap++);
316 adios (NULL, "folder stack empty");
319 /* if there's anything left in the stack */
322 cp = add (*ap, add (" ", cp));
323 context_replace (stack, cp); /* update folder stack */
325 context_del (stack); /* delete folder stack entry from context */
329 if (pushsw || popsw) {
330 cp = m_maildir(argfolder);
331 if (access (cp, F_OK) == NOTOK)
332 adios (cp, "unable to find folder");
333 context_replace (pfolder, argfolder); /* update current folder */
334 context_save (); /* save the context file */
338 /* Listing the folder stack */
340 printf ("%s", argfolder ? argfolder : getfolder (1));
341 if ((cp = context_find (stack))) {
343 for (ap = brkstring (dp, " ", "\n"); *ap; ap++)
353 /* Allocate initial space to record folder names */
354 maxfolders = NUMFOLDERS;
355 folds = mh_xmalloc (maxfolders * sizeof(char *));
357 /* Allocate initial space to record folder information */
358 maxFolderInfo = NUMFOLDERS;
359 fi = mh_xmalloc (maxFolderInfo * sizeof(*fi));
364 if (all || ftotal > 0) {
366 * If no folder is given, do them all
368 /* change directory to base of nmh directory for dodir */
369 if (chdir (nmhdir) == NOTOK)
370 adios (nmhdir, "unable to change directory to");
373 admonish (NULL, "no folder given for message %s", msg);
374 readonly_folders (); /* do any readonly folders */
375 strncpy (folder, (cp = context_find (pfolder)) ? cp : "", sizeof(folder));
378 strncpy (folder, argfolder, sizeof(folder));
379 if (get_folder_info (argfolder, msg)) {
380 context_replace (pfolder, argfolder);/* update current folder */
381 context_save (); /* save the context file */
384 * Since recurse wasn't done in get_folder_info(),
385 * we still need to list all level-1 sub-folders.
391 strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
394 * Check if folder exists. If not, then see if
395 * we should create it, or just exit.
397 create_folder (m_maildir (folder), fcreat, done);
399 if (get_folder_info (folder, msg) && argfolder) {
400 /* update current folder */
401 context_replace (pfolder, argfolder);
406 * Print out folder information
410 context_save (); /* save the context file */
416 * Base routine for scanning a folder
430 for (i = start; i < foldp; i++) {
431 get_folder_info (folds[i], NULL);
440 get_folder_info (char *fold, char *msg)
443 struct msgs *mp = NULL;
448 * if necessary, reallocate the space
449 * for folder information
451 if (total_folders >= maxFolderInfo) {
452 maxFolderInfo += NUMFOLDERS;
453 fi = mh_xrealloc (fi, maxFolderInfo * sizeof(*fi));
464 if ((ftotal > 0) || !fshort || msg || fpack) {
466 * create message structure and get folder info
468 if (!(mp = folder_read (fold))) {
469 admonish (NULL, "unable to read folder %s", fold);
473 /* set the current message */
474 if (msg && !sfold (mp, msg))
478 if (folder_pack (&mp, fverb) == -1)
480 seq_save (mp); /* synchronize the sequences */
481 context_save (); /* save the context file */
484 /* record info for this folder */
485 if ((ftotal > 0) || !fshort) {
486 fi[i].nummsg = mp->nummsg;
487 fi[i].curmsg = mp->curmsg;
488 fi[i].lowmsg = mp->lowmsg;
489 fi[i].hghmsg = mp->hghmsg;
490 fi[i].others = other_files (mp);
493 folder_free (mp); /* free folder/message structure */
496 if (frecurse && (fshort || fi[i].others) && (fi[i].error == 0))
502 * Print folder information
508 int i, len, hasempty = 0, curprinted;
509 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
510 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
511 int nummsgdigits, lowmsgdigits;
512 int hghmsgdigits, curmsgdigits;
513 char tmpname[BUFSIZ];
516 * compute a few values needed to for
517 * printing various fields
519 for (i = 0; i < total_folders; i++) {
520 /* length of folder name */
521 len = strlen (fi[i].name);
525 /* If folder has error, skip the rest */
529 /* calculate total number of messages */
530 total_msgs += fi[i].nummsg;
532 /* maximum number of messages */
533 if (fi[i].nummsg > maxnummsg)
534 maxnummsg = fi[i].nummsg;
536 /* maximum low message */
537 if (fi[i].lowmsg > maxlowmsg)
538 maxlowmsg = fi[i].lowmsg;
540 /* maximum high message */
541 if (fi[i].hghmsg > maxhghmsg)
542 maxhghmsg = fi[i].hghmsg;
544 /* maximum current message */
545 if (fi[i].curmsg >= fi[i].lowmsg &&
546 fi[i].curmsg <= fi[i].hghmsg &&
547 fi[i].curmsg > maxcurmsg)
548 maxcurmsg = fi[i].curmsg;
550 /* check for empty folders */
551 if (fi[i].nummsg == 0)
554 nummsgdigits = num_digits (maxnummsg);
555 lowmsgdigits = num_digits (maxlowmsg);
556 hghmsgdigits = num_digits (maxhghmsg);
557 curmsgdigits = num_digits (maxcurmsg);
559 if (hasempty && nummsgdigits < 2)
565 if (fheader > 0 || (all && !fshort && fheader >= 0))
566 printf ("%-*s %*s %-*s; %-*s %*s\n",
568 nummsgdigits + 13, "# MESSAGES",
569 lowmsgdigits + hghmsgdigits + 4, " RANGE",
570 curmsgdigits + 4, "CUR",
574 * Print folder information
576 if (all || fshort || ftotal < 1) {
577 for (i = 0; i < total_folders; i++) {
579 printf ("%s\n", fi[i].name);
583 /* Add `+' to end of name, if folder is current */
584 if (strcmp (folder, fi[i].name))
585 snprintf (tmpname, sizeof(tmpname), "%s", fi[i].name);
587 snprintf (tmpname, sizeof(tmpname), "%s+", fi[i].name);
590 printf ("%-*s is unreadable\n", maxlen+1, tmpname);
594 printf ("%-*s ", maxlen+1, tmpname);
596 curprinted = 0; /* remember if we print cur */
597 if (fi[i].nummsg == 0) {
598 printf ("has %*s messages%*s",
600 fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
602 printf ("has %*d message%s (%*d-%*d)",
603 nummsgdigits, fi[i].nummsg,
604 (fi[i].nummsg == 1) ? " " : "s",
605 lowmsgdigits, fi[i].lowmsg,
606 hghmsgdigits, fi[i].hghmsg);
607 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
609 printf ("; cur=%*d", curmsgdigits, fi[i].curmsg);
614 printf (";%*s (others)", curprinted ? 0 : curmsgdigits + 6, "");
620 * Print folder/message totals
622 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
625 printf ("TOTAL = %d message%c in %d folder%s.\n",
626 total_msgs, total_msgs != 1 ? 's' : ' ',
627 total_folders, total_folders != 1 ? "s" : "");
634 * Set the current message and sychronize sequences
638 sfold (struct msgs *mp, char *msg)
640 /* parse the message range/sequence/name and set SELECTED */
641 if (!m_convert (mp, msg))
644 if (mp->numsel > 1) {
645 admonish (NULL, "only one message at a time!");
648 seq_setprev (mp); /* set the previous-sequence */
649 seq_setcur (mp, mp->lowsel);/* set current message */
650 seq_save (mp); /* synchronize message sequences */
651 context_save (); /* save the context file */
660 char *prefix, *child;
666 if (!(dd = opendir (name))) {
667 admonish (name, "unable to read directory ");
671 if (strcmp (name, ".") == 0) {
672 prefix = getcpy ("");
674 prefix = concat (name, "/", (void *)NULL);
677 while ((dp = readdir (dd))) {
678 /* If the system supports it, try to skip processing of children we
679 * know are not directories or symlinks. */
680 child_is_folder = -1;
681 #if defined(HAVE_STRUCT_DIRENT_D_TYPE)
682 if (dp->d_type == DT_DIR) {
684 } else if (dp->d_type != DT_LNK && dp->d_type != DT_UNKNOWN) {
688 if (!strcmp (dp->d_name, ".") || !strcmp (dp->d_name, "..")) {
691 child = concat (prefix, dp->d_name, (void *)NULL);
692 /* If we have no d_type or d_type is DT_LNK or DT_UNKNOWN, stat the
693 * child to see what it is. */
694 if (child_is_folder == -1) {
695 child_is_folder = (stat (child, &st) != -1 && S_ISDIR(st.st_mode));
697 if (child_is_folder) {
698 /* addfold saves child in the list, don't free it */
710 * Add the folder name into the
711 * list in a sorted fashion.
719 /* if necessary, reallocate the space for folder names */
720 if (foldp >= maxfolders) {
721 maxfolders += NUMFOLDERS;
722 folds = mh_xrealloc (folds, maxfolders * sizeof(char *));
725 for (i = start; i < foldp; i++)
726 if (compare (fold, folds[i]) < 0) {
727 for (j = foldp - 1; j >= i; j--)
728 folds[j + 1] = folds[j];
734 folds[foldp++] = fold;
739 compare (char *s1, char *s2)
744 if ((i = *s1++ - *s2++))
751 * Do the read only folders
755 readonly_folders (void)
759 register struct node *np;
761 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
762 atrlen = strlen (atrcur);
764 for (np = m_defs; np; np = np->n_next)
765 if (ssequal (atrcur, np->n_name)
766 && !ssequal (nmhdir, np->n_name + atrlen))
767 get_folder_info (np->n_name + atrlen, NULL);