1 /* folder.c -- set/list the current message and/or folder
2 * -- push/pop a folder onto/from the folder stack
3 * -- list the folder stack
5 * This code is Copyright (c) 2002, 2008, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include "sbr/getarguments.h"
12 #include "sbr/concat.h"
13 #include "sbr/seq_setprev.h"
14 #include "sbr/seq_setcur.h"
15 #include "sbr/seq_save.h"
16 #include "sbr/smatch.h"
17 #include "sbr/ssequal.h"
18 #include "sbr/getcpy.h"
19 #include "sbr/m_convert.h"
20 #include "sbr/getfolder.h"
21 #include "sbr/folder_read.h"
22 #include "sbr/folder_pack.h"
23 #include "sbr/folder_free.h"
24 #include "sbr/context_save.h"
25 #include "sbr/context_replace.h"
26 #include "sbr/context_del.h"
27 #include "sbr/context_find.h"
28 #include "sbr/brkstring.h"
29 #include "sbr/ambigsw.h"
31 #include "sbr/print_version.h"
32 #include "sbr/print_help.h"
33 #include "sbr/error.h"
34 #include "sbr/crawl_folders.h"
37 #include "sbr/m_maildir.h"
39 #define FOLDER_SWITCHES \
41 X("noall", 0, NALLSW) \
42 X("create", 0, CREATSW) \
43 X("nocreate", 0, NCREATSW) \
44 X("fast", 0, FASTSW) \
45 X("nofast", 0, NFASTSW) \
46 X("header", 0, HDRSW) \
47 X("noheader", 0, NHDRSW) \
48 X("pack", 0, PACKSW) \
49 X("nopack", 0, NPACKSW) \
50 X("verbose", 0, VERBSW) \
51 X("noverbose", 0, NVERBSW) \
52 X("recurse", 0, RECURSW) \
53 X("norecurse", 0, NRECRSW) \
54 X("total", 0, TOTALSW) \
55 X("nototal", 0, NTOTLSW) \
56 X("list", 0, LISTSW) \
57 X("nolist", 0, NLISTSW) \
58 X("print", 0, PRNTSW) \
59 X("noprint", 0, NPRNTSW) \
60 X("push", 0, PUSHSW) \
62 X("version", 0, VERSIONSW) \
63 X("help", 0, HELPSW) \
65 #define X(sw, minchars, id) id,
66 DEFINE_SWITCH_ENUM(FOLDER
);
69 #define X(sw, minchars, id) { sw, minchars, id },
70 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
73 static bool fshort
; /* output only folder names */
74 static int fcreat
= 0; /* should we ask to create new folders? */
75 static bool fpack
; /* are we packing the folder? */
76 static bool fverb
; /* print actions taken while packing folder */
77 static int fheader
= 0; /* should we output a header? */
78 static bool frecurse
; /* recurse through subfolders */
79 static int ftotal
= 0; /* should we output the totals? */
80 static bool all
; /* should we output all folders */
82 static int total_folders
= 0; /* total number of folders */
85 static char *stack
= "Folder-Stack";
86 static char folder
[BUFSIZ
];
89 * Structure to hold information about
90 * folders as we scan them.
98 int others
; /* others == 1 if other files in folder */
99 int error
; /* error == 1 for unreadable folder */
103 * Dynamically allocated space to hold
104 * all the folder information.
106 static struct FolderInfo
*fi
;
107 static int maxFolderInfo
;
112 static int get_folder_info (char *, char *);
113 static crawl_callback_t get_folder_info_callback
;
114 static void print_folders (void);
115 static int sfold (struct msgs
*, char *);
116 static void readonly_folders (void);
119 * Function for printing error message if folder does not exist with
123 nonexistent_folder (int status
)
126 die("folder %s does not exist", folder
);
131 main (int argc
, char **argv
)
137 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
138 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
140 if (nmh_init(argv
[0], true, true)) { return 1; }
143 * If program was invoked with name ending
144 * in `s', then add switch `-all'.
146 all
= has_suffix_c(argv
[0], 's');
148 arguments
= getarguments (invo_name
, argc
, argv
, 1);
151 while ((cp
= *argp
++)) {
153 switch (smatch (++cp
, switches
)) {
155 ambigsw (cp
, switches
);
158 die("-%s unknown", cp
);
161 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
163 print_help (buf
, switches
, 1);
166 print_version(invo_name
);
252 if (*cp
== '+' || *cp
== '@') {
254 die("only one folder at a time!");
255 argfolder
= pluspath (cp
);
258 die("only one (current) message at a time!");
263 if (!context_find ("path"))
264 free (path ("./", TFOLDER
));
265 nmhdir
= concat (m_maildir (""), "/", NULL
);
268 * If not directed via -print/-noprint, we print folder summary
269 * info unless if we're working with the folder stack (i.e.,
270 * -push, -pop, or -list).
273 printsw
= !(pushsw
|| popsw
|| listsw
);
276 /* Pushing a folder onto the folder stack */
279 /* If no folder is given, the current folder and */
280 /* the top of the folder stack are swapped. */
281 if ((cp
= context_find (stack
))) {
283 ap
= brkstring (dp
, " ", "\n");
284 argfolder
= getcpy(*ap
++);
286 die("no other folder");
288 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
289 cp
= add (*ap
, add (" ", cp
));
291 context_replace (stack
, cp
); /* update folder stack */
293 /* update folder stack */
294 context_replace (stack
,
295 (cp
= context_find (stack
))
296 ? concat (getfolder (1), " ", cp
, NULL
)
297 : mh_xstrdup(getfolder(1)));
301 /* Popping a folder off of the folder stack */
304 die("sorry, no folders allowed with -pop");
305 if ((cp
= context_find (stack
))) {
307 ap
= brkstring (dp
, " ", "\n");
308 argfolder
= getcpy(*ap
++);
310 die("folder stack empty");
313 /* if there's anything left in the stack */
316 cp
= add (*ap
, add (" ", cp
));
317 context_replace (stack
, cp
); /* update folder stack */
319 context_del (stack
); /* delete folder stack entry from context */
323 if (pushsw
|| popsw
) {
324 cp
= m_maildir(argfolder
);
325 if (access (cp
, F_OK
) == NOTOK
)
326 adios (cp
, "unable to find folder");
327 context_replace (pfolder
, argfolder
); /* update current folder */
328 context_save (); /* save the context file */
332 /* Listing the folder stack */
334 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
335 if ((cp
= context_find (stack
))) {
337 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
347 /* Allocate initial space to record folder information */
348 maxFolderInfo
= CRAWL_NUMFOLDERS
;
349 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
354 /* change directory to base of nmh directory for crawl_folders */
355 if (chdir (nmhdir
) == NOTOK
)
356 adios (nmhdir
, "unable to change directory to");
357 if (all
|| ftotal
> 0) {
359 * If no folder is given, do them all
363 inform("no folder given for message %s, continuing...", msg
);
364 readonly_folders (); /* do any readonly folders */
365 cp
= context_find(pfolder
);
366 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
367 crawl_folders (".", get_folder_info_callback
, NULL
);
369 strncpy (folder
, argfolder
, sizeof(folder
));
370 if (get_folder_info (argfolder
, msg
)) {
371 context_replace (pfolder
, argfolder
);/* update current folder */
372 context_save (); /* save the context file */
375 * Since recurse wasn't done in get_folder_info(),
376 * we still need to list all level-1 sub-folders.
379 crawl_folders (folder
, get_folder_info_callback
, NULL
);
382 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
385 * Check if folder exists. If not, then see if
386 * we should create it, or just exit.
388 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
390 if (get_folder_info (folder
, msg
) && argfolder
) {
391 /* update current folder */
392 context_replace (pfolder
, argfolder
);
397 * Print out folder information
402 context_save (); /* save the context file */
408 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
411 struct msgs
*mp
= NULL
;
416 * if necessary, reallocate the space
417 * for folder information
419 if (total_folders
>= maxFolderInfo
) {
420 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
421 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
432 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
434 * create message structure and get folder info
436 if (!(mp
= folder_read (fold
, fpack
))) {
437 inform("unable to read folder %s, continuing...", fold
);
438 *crawl_children
= false;
442 /* set the current message */
443 if (msg
&& !sfold (mp
, msg
))
447 if (folder_pack (&mp
, fverb
) == -1) {
448 *crawl_children
= false; /* to please clang static analyzer */
451 seq_save (mp
); /* synchronize the sequences */
452 context_save (); /* save the context file */
455 /* record info for this folder */
456 if ((ftotal
> 0) || !fshort
) {
457 fi
[i
].nummsg
= mp
->nummsg
;
458 fi
[i
].curmsg
= mp
->curmsg
;
459 fi
[i
].lowmsg
= mp
->lowmsg
;
460 fi
[i
].hghmsg
= mp
->hghmsg
;
461 fi
[i
].others
= other_files (mp
);
464 folder_free (mp
); /* free folder/message structure */
467 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
468 && (fi
[i
].error
== 0));
473 get_folder_info_callback (char *fold
, void *baton
)
478 get_folder_info_body (fold
, NULL
, &crawl_children
);
480 return crawl_children
;
484 get_folder_info (char *fold
, char *msg
)
489 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
491 if (crawl_children
) {
492 crawl_folders (fold
, get_folder_info_callback
, NULL
);
499 * Print folder information
506 bool hasempty
= false;
508 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
509 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
510 int nummsgdigits
, lowmsgdigits
;
511 int hghmsgdigits
, curmsgdigits
;
512 char tmpname
[BUFSIZ
];
515 * compute a few values needed to for
516 * printing various fields
518 for (i
= 0; i
< total_folders
; i
++) {
519 /* length of folder name */
520 len
= strlen (fi
[i
].name
);
524 /* If folder has error, skip the rest */
528 /* calculate total number of messages */
529 total_msgs
+= fi
[i
].nummsg
;
531 /* maximum number of messages */
532 if (fi
[i
].nummsg
> maxnummsg
)
533 maxnummsg
= fi
[i
].nummsg
;
535 /* maximum low message */
536 if (fi
[i
].lowmsg
> maxlowmsg
)
537 maxlowmsg
= fi
[i
].lowmsg
;
539 /* maximum high message */
540 if (fi
[i
].hghmsg
> maxhghmsg
)
541 maxhghmsg
= fi
[i
].hghmsg
;
543 /* maximum current message */
544 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
545 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
546 fi
[i
].curmsg
> maxcurmsg
)
547 maxcurmsg
= fi
[i
].curmsg
;
549 /* check for empty folders */
550 if (fi
[i
].nummsg
== 0)
553 nummsgdigits
= num_digits (maxnummsg
);
554 lowmsgdigits
= num_digits (maxlowmsg
);
555 hghmsgdigits
= num_digits (maxhghmsg
);
556 curmsgdigits
= num_digits (maxcurmsg
);
558 if (hasempty
&& nummsgdigits
< 2)
564 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
565 printf ("%-*s %*s %-*s; %-*s %*s\n",
567 nummsgdigits
+ 13, "# MESSAGES",
568 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
569 curmsgdigits
+ 4, "CUR",
573 * Print folder information
575 if (all
|| fshort
|| ftotal
< 1) {
576 for (i
= 0; i
< total_folders
; i
++) {
582 /* Add `+' to end of name, if folder is current */
583 if (strcmp (folder
, fi
[i
].name
))
584 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
586 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
589 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
593 printf ("%-*s ", maxlen
+1, tmpname
);
595 curprinted
= false; /* remember if we print cur */
596 if (fi
[i
].nummsg
== 0) {
597 printf ("has %*s messages%*s",
599 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
601 printf ("has %*d message%1s (%*d-%*d)",
602 nummsgdigits
, fi
[i
].nummsg
,
603 PLURALS(fi
[i
].nummsg
),
604 lowmsgdigits
, fi
[i
].lowmsg
,
605 hghmsgdigits
, fi
[i
].hghmsg
);
606 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
608 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
613 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
619 * Print folder/message totals
621 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
624 printf ("TOTAL = %d message%s in %d folder%s.\n",
625 total_msgs
, PLURALS(total_msgs
),
626 total_folders
, PLURALS(total_folders
));
633 * Set the current message and synchronize sequences
637 sfold (struct msgs
*mp
, char *msg
)
639 /* parse the message range/sequence/name and set SELECTED */
640 if (!m_convert (mp
, msg
))
643 if (mp
->numsel
> 1) {
644 inform("only one message at a time!, continuing...");
647 seq_setprev (mp
); /* set the previous-sequence */
648 seq_setcur (mp
, mp
->lowsel
);/* set current message */
649 seq_save (mp
); /* synchronize message sequences */
650 context_save (); /* save the context file */
657 * Do the read only folders
661 readonly_folders (void)
667 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
668 atrlen
= strlen (atrcur
);
670 for (np
= m_defs
; np
; np
= np
->n_next
)
671 if (ssequal (atrcur
, np
->n_name
)
672 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
673 get_folder_info (np
->n_name
+ atrlen
, NULL
);