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/m_convert.h"
12 #include "sbr/getfolder.h"
13 #include "sbr/folder_read.h"
14 #include "sbr/folder_pack.h"
15 #include "sbr/folder_free.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_del.h"
19 #include "sbr/context_find.h"
20 #include "sbr/brkstring.h"
21 #include "sbr/ambigsw.h"
23 #include "sbr/print_version.h"
24 #include "sbr/print_help.h"
25 #include "sbr/error.h"
26 #include "h/crawl_folders.h"
29 #include "sbr/m_maildir.h"
31 #define FOLDER_SWITCHES \
33 X("noall", 0, NALLSW) \
34 X("create", 0, CREATSW) \
35 X("nocreate", 0, NCREATSW) \
36 X("fast", 0, FASTSW) \
37 X("nofast", 0, NFASTSW) \
38 X("header", 0, HDRSW) \
39 X("noheader", 0, NHDRSW) \
40 X("pack", 0, PACKSW) \
41 X("nopack", 0, NPACKSW) \
42 X("verbose", 0, VERBSW) \
43 X("noverbose", 0, NVERBSW) \
44 X("recurse", 0, RECURSW) \
45 X("norecurse", 0, NRECRSW) \
46 X("total", 0, TOTALSW) \
47 X("nototal", 0, NTOTLSW) \
48 X("list", 0, LISTSW) \
49 X("nolist", 0, NLISTSW) \
50 X("print", 0, PRNTSW) \
51 X("noprint", 0, NPRNTSW) \
52 X("push", 0, PUSHSW) \
54 X("version", 0, VERSIONSW) \
55 X("help", 0, HELPSW) \
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(FOLDER
);
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
65 static bool fshort
; /* output only folder names */
66 static int fcreat
= 0; /* should we ask to create new folders? */
67 static bool fpack
; /* are we packing the folder? */
68 static bool fverb
; /* print actions taken while packing folder */
69 static int fheader
= 0; /* should we output a header? */
70 static bool frecurse
; /* recurse through subfolders */
71 static int ftotal
= 0; /* should we output the totals? */
72 static bool all
; /* should we output all folders */
74 static int total_folders
= 0; /* total number of folders */
77 static char *stack
= "Folder-Stack";
78 static char folder
[BUFSIZ
];
81 * Structure to hold information about
82 * folders as we scan them.
90 int others
; /* others == 1 if other files in folder */
91 int error
; /* error == 1 for unreadable folder */
95 * Dynamically allocated space to hold
96 * all the folder information.
98 static struct FolderInfo
*fi
;
99 static int maxFolderInfo
;
104 static int get_folder_info (char *, char *);
105 static crawl_callback_t get_folder_info_callback
;
106 static void print_folders (void);
107 static int sfold (struct msgs
*, char *);
108 static void readonly_folders (void);
111 * Function for printing error message if folder does not exist with
115 nonexistent_folder (int status
)
118 die("folder %s does not exist", folder
);
123 main (int argc
, char **argv
)
125 bool printsw
= false;
129 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
130 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
132 if (nmh_init(argv
[0], true, true)) { return 1; }
135 * If program was invoked with name ending
136 * in `s', then add switch `-all'.
138 all
= has_suffix_c(argv
[0], 's');
140 arguments
= getarguments (invo_name
, argc
, argv
, 1);
143 while ((cp
= *argp
++)) {
145 switch (smatch (++cp
, switches
)) {
147 ambigsw (cp
, switches
);
150 die("-%s unknown", cp
);
153 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
155 print_help (buf
, switches
, 1);
158 print_version(invo_name
);
244 if (*cp
== '+' || *cp
== '@') {
246 die("only one folder at a time!");
247 argfolder
= pluspath (cp
);
250 die("only one (current) message at a time!");
255 if (!context_find ("path"))
256 free (path ("./", TFOLDER
));
257 nmhdir
= concat (m_maildir (""), "/", NULL
);
260 * If we aren't working with the folder stack
261 * (-push, -pop, -list) then the default is to print.
263 if (!pushsw
&& !popsw
&& !listsw
)
266 /* Pushing a folder onto the folder stack */
269 /* If no folder is given, the current folder and */
270 /* the top of the folder stack are swapped. */
271 if ((cp
= context_find (stack
))) {
273 ap
= brkstring (dp
, " ", "\n");
274 argfolder
= getcpy(*ap
++);
276 die("no other folder");
278 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
279 cp
= add (*ap
, add (" ", cp
));
281 context_replace (stack
, cp
); /* update folder stack */
283 /* update folder stack */
284 context_replace (stack
,
285 (cp
= context_find (stack
))
286 ? concat (getfolder (1), " ", cp
, NULL
)
287 : mh_xstrdup(getfolder(1)));
291 /* Popping a folder off of the folder stack */
294 die("sorry, no folders allowed with -pop");
295 if ((cp
= context_find (stack
))) {
297 ap
= brkstring (dp
, " ", "\n");
298 argfolder
= getcpy(*ap
++);
300 die("folder stack empty");
303 /* if there's anything left in the stack */
306 cp
= add (*ap
, add (" ", cp
));
307 context_replace (stack
, cp
); /* update folder stack */
309 context_del (stack
); /* 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 context_replace (pfolder
, argfolder
); /* update current folder */
318 context_save (); /* save the context file */
322 /* Listing the folder stack */
324 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
325 if ((cp
= context_find (stack
))) {
327 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
337 /* Allocate initial space to record folder information */
338 maxFolderInfo
= CRAWL_NUMFOLDERS
;
339 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
344 /* change directory to base of nmh directory for crawl_folders */
345 if (chdir (nmhdir
) == NOTOK
)
346 adios (nmhdir
, "unable to change directory to");
347 if (all
|| ftotal
> 0) {
349 * If no folder is given, do them all
353 inform("no folder given for message %s, continuing...", msg
);
354 readonly_folders (); /* do any readonly folders */
355 cp
= context_find(pfolder
);
356 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
357 crawl_folders (".", get_folder_info_callback
, NULL
);
359 strncpy (folder
, argfolder
, sizeof(folder
));
360 if (get_folder_info (argfolder
, msg
)) {
361 context_replace (pfolder
, argfolder
);/* update current folder */
362 context_save (); /* save the context file */
365 * Since recurse wasn't done in get_folder_info(),
366 * we still need to list all level-1 sub-folders.
369 crawl_folders (folder
, get_folder_info_callback
, NULL
);
372 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
375 * Check if folder exists. If not, then see if
376 * we should create it, or just exit.
378 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
380 if (get_folder_info (folder
, msg
) && argfolder
) {
381 /* update current folder */
382 context_replace (pfolder
, argfolder
);
387 * Print out folder information
391 context_save (); /* save the context file */
397 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
400 struct msgs
*mp
= NULL
;
405 * if necessary, reallocate the space
406 * for folder information
408 if (total_folders
>= maxFolderInfo
) {
409 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
410 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
421 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
423 * create message structure and get folder info
425 if (!(mp
= folder_read (fold
, fpack
))) {
426 inform("unable to read folder %s, continuing...", fold
);
427 *crawl_children
= false;
431 /* set the current message */
432 if (msg
&& !sfold (mp
, msg
))
436 if (folder_pack (&mp
, fverb
) == -1) {
437 *crawl_children
= false; /* to please clang static analyzer */
440 seq_save (mp
); /* synchronize the sequences */
441 context_save (); /* save the context file */
444 /* record info for this folder */
445 if ((ftotal
> 0) || !fshort
) {
446 fi
[i
].nummsg
= mp
->nummsg
;
447 fi
[i
].curmsg
= mp
->curmsg
;
448 fi
[i
].lowmsg
= mp
->lowmsg
;
449 fi
[i
].hghmsg
= mp
->hghmsg
;
450 fi
[i
].others
= other_files (mp
);
453 folder_free (mp
); /* free folder/message structure */
456 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
457 && (fi
[i
].error
== 0));
462 get_folder_info_callback (char *fold
, void *baton
)
467 get_folder_info_body (fold
, NULL
, &crawl_children
);
469 return crawl_children
;
473 get_folder_info (char *fold
, char *msg
)
478 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
480 if (crawl_children
) {
481 crawl_folders (fold
, get_folder_info_callback
, NULL
);
488 * Print folder information
495 bool hasempty
= false;
497 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
498 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
499 int nummsgdigits
, lowmsgdigits
;
500 int hghmsgdigits
, curmsgdigits
;
501 char tmpname
[BUFSIZ
];
504 * compute a few values needed to for
505 * printing various fields
507 for (i
= 0; i
< total_folders
; i
++) {
508 /* length of folder name */
509 len
= strlen (fi
[i
].name
);
513 /* If folder has error, skip the rest */
517 /* calculate total number of messages */
518 total_msgs
+= fi
[i
].nummsg
;
520 /* maximum number of messages */
521 if (fi
[i
].nummsg
> maxnummsg
)
522 maxnummsg
= fi
[i
].nummsg
;
524 /* maximum low message */
525 if (fi
[i
].lowmsg
> maxlowmsg
)
526 maxlowmsg
= fi
[i
].lowmsg
;
528 /* maximum high message */
529 if (fi
[i
].hghmsg
> maxhghmsg
)
530 maxhghmsg
= fi
[i
].hghmsg
;
532 /* maximum current message */
533 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
534 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
535 fi
[i
].curmsg
> maxcurmsg
)
536 maxcurmsg
= fi
[i
].curmsg
;
538 /* check for empty folders */
539 if (fi
[i
].nummsg
== 0)
542 nummsgdigits
= num_digits (maxnummsg
);
543 lowmsgdigits
= num_digits (maxlowmsg
);
544 hghmsgdigits
= num_digits (maxhghmsg
);
545 curmsgdigits
= num_digits (maxcurmsg
);
547 if (hasempty
&& nummsgdigits
< 2)
553 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
554 printf ("%-*s %*s %-*s; %-*s %*s\n",
556 nummsgdigits
+ 13, "# MESSAGES",
557 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
558 curmsgdigits
+ 4, "CUR",
562 * Print folder information
564 if (all
|| fshort
|| ftotal
< 1) {
565 for (i
= 0; i
< total_folders
; i
++) {
571 /* Add `+' to end of name, if folder is current */
572 if (strcmp (folder
, fi
[i
].name
))
573 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
575 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
578 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
582 printf ("%-*s ", maxlen
+1, tmpname
);
584 curprinted
= false; /* remember if we print cur */
585 if (fi
[i
].nummsg
== 0) {
586 printf ("has %*s messages%*s",
588 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
590 printf ("has %*d message%1s (%*d-%*d)",
591 nummsgdigits
, fi
[i
].nummsg
,
592 PLURALS(fi
[i
].nummsg
),
593 lowmsgdigits
, fi
[i
].lowmsg
,
594 hghmsgdigits
, fi
[i
].hghmsg
);
595 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
597 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
602 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
608 * Print folder/message totals
610 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
613 printf ("TOTAL = %d message%s in %d folder%s.\n",
614 total_msgs
, PLURALS(total_msgs
),
615 total_folders
, PLURALS(total_folders
));
622 * Set the current message and synchronize sequences
626 sfold (struct msgs
*mp
, char *msg
)
628 /* parse the message range/sequence/name and set SELECTED */
629 if (!m_convert (mp
, msg
))
632 if (mp
->numsel
> 1) {
633 inform("only one message at a time!, continuing...");
636 seq_setprev (mp
); /* set the previous-sequence */
637 seq_setcur (mp
, mp
->lowsel
);/* set current message */
638 seq_save (mp
); /* synchronize message sequences */
639 context_save (); /* save the context file */
646 * Do the read only folders
650 readonly_folders (void)
656 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
657 atrlen
= strlen (atrcur
);
659 for (np
= m_defs
; np
; np
= np
->n_next
)
660 if (ssequal (atrcur
, np
->n_name
)
661 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
662 get_folder_info (np
->n_name
+ atrlen
, NULL
);