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/folder_read.h"
12 #include "sbr/folder_pack.h"
13 #include "sbr/folder_free.h"
14 #include "sbr/context_save.h"
15 #include "sbr/context_replace.h"
16 #include "sbr/context_del.h"
17 #include "sbr/context_find.h"
18 #include "sbr/brkstring.h"
19 #include "sbr/ambigsw.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/error.h"
24 #include "h/crawl_folders.h"
27 #include "sbr/m_maildir.h"
29 #define FOLDER_SWITCHES \
31 X("noall", 0, NALLSW) \
32 X("create", 0, CREATSW) \
33 X("nocreate", 0, NCREATSW) \
34 X("fast", 0, FASTSW) \
35 X("nofast", 0, NFASTSW) \
36 X("header", 0, HDRSW) \
37 X("noheader", 0, NHDRSW) \
38 X("pack", 0, PACKSW) \
39 X("nopack", 0, NPACKSW) \
40 X("verbose", 0, VERBSW) \
41 X("noverbose", 0, NVERBSW) \
42 X("recurse", 0, RECURSW) \
43 X("norecurse", 0, NRECRSW) \
44 X("total", 0, TOTALSW) \
45 X("nototal", 0, NTOTLSW) \
46 X("list", 0, LISTSW) \
47 X("nolist", 0, NLISTSW) \
48 X("print", 0, PRNTSW) \
49 X("noprint", 0, NPRNTSW) \
50 X("push", 0, PUSHSW) \
52 X("version", 0, VERSIONSW) \
53 X("help", 0, HELPSW) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(FOLDER
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
63 static bool fshort
; /* output only folder names */
64 static int fcreat
= 0; /* should we ask to create new folders? */
65 static bool fpack
; /* are we packing the folder? */
66 static bool fverb
; /* print actions taken while packing folder */
67 static int fheader
= 0; /* should we output a header? */
68 static bool frecurse
; /* recurse through subfolders */
69 static int ftotal
= 0; /* should we output the totals? */
70 static bool all
; /* should we output all folders */
72 static int total_folders
= 0; /* total number of folders */
75 static char *stack
= "Folder-Stack";
76 static char folder
[BUFSIZ
];
79 * Structure to hold information about
80 * folders as we scan them.
88 int others
; /* others == 1 if other files in folder */
89 int error
; /* error == 1 for unreadable folder */
93 * Dynamically allocated space to hold
94 * all the folder information.
96 static struct FolderInfo
*fi
;
97 static int maxFolderInfo
;
102 static int get_folder_info (char *, char *);
103 static crawl_callback_t get_folder_info_callback
;
104 static void print_folders (void);
105 static int sfold (struct msgs
*, char *);
106 static void readonly_folders (void);
109 * Function for printing error message if folder does not exist with
113 nonexistent_folder (int status
)
116 die("folder %s does not exist", folder
);
121 main (int argc
, char **argv
)
123 bool printsw
= false;
127 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
128 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
130 if (nmh_init(argv
[0], true, true)) { return 1; }
133 * If program was invoked with name ending
134 * in `s', then add switch `-all'.
136 all
= has_suffix_c(argv
[0], 's');
138 arguments
= getarguments (invo_name
, argc
, argv
, 1);
141 while ((cp
= *argp
++)) {
143 switch (smatch (++cp
, switches
)) {
145 ambigsw (cp
, switches
);
148 die("-%s unknown", cp
);
151 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
153 print_help (buf
, switches
, 1);
156 print_version(invo_name
);
242 if (*cp
== '+' || *cp
== '@') {
244 die("only one folder at a time!");
245 argfolder
= pluspath (cp
);
248 die("only one (current) message at a time!");
253 if (!context_find ("path"))
254 free (path ("./", TFOLDER
));
255 nmhdir
= concat (m_maildir (""), "/", NULL
);
258 * If we aren't working with the folder stack
259 * (-push, -pop, -list) then the default is to print.
261 if (!pushsw
&& !popsw
&& !listsw
)
264 /* Pushing a folder onto the folder stack */
267 /* If no folder is given, the current folder and */
268 /* the top of the folder stack are swapped. */
269 if ((cp
= context_find (stack
))) {
271 ap
= brkstring (dp
, " ", "\n");
272 argfolder
= getcpy(*ap
++);
274 die("no other folder");
276 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
277 cp
= add (*ap
, add (" ", cp
));
279 context_replace (stack
, cp
); /* update folder stack */
281 /* update folder stack */
282 context_replace (stack
,
283 (cp
= context_find (stack
))
284 ? concat (getfolder (1), " ", cp
, NULL
)
285 : mh_xstrdup(getfolder(1)));
289 /* Popping a folder off of the folder stack */
292 die("sorry, no folders allowed with -pop");
293 if ((cp
= context_find (stack
))) {
295 ap
= brkstring (dp
, " ", "\n");
296 argfolder
= getcpy(*ap
++);
298 die("folder stack empty");
301 /* if there's anything left in the stack */
304 cp
= add (*ap
, add (" ", cp
));
305 context_replace (stack
, cp
); /* update folder stack */
307 context_del (stack
); /* delete folder stack entry from context */
311 if (pushsw
|| popsw
) {
312 cp
= m_maildir(argfolder
);
313 if (access (cp
, F_OK
) == NOTOK
)
314 adios (cp
, "unable to find folder");
315 context_replace (pfolder
, argfolder
); /* update current folder */
316 context_save (); /* save the context file */
320 /* Listing the folder stack */
322 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
323 if ((cp
= context_find (stack
))) {
325 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
335 /* Allocate initial space to record folder information */
336 maxFolderInfo
= CRAWL_NUMFOLDERS
;
337 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
342 /* change directory to base of nmh directory for crawl_folders */
343 if (chdir (nmhdir
) == NOTOK
)
344 adios (nmhdir
, "unable to change directory to");
345 if (all
|| ftotal
> 0) {
347 * If no folder is given, do them all
351 inform("no folder given for message %s, continuing...", msg
);
352 readonly_folders (); /* do any readonly folders */
353 cp
= context_find(pfolder
);
354 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
355 crawl_folders (".", get_folder_info_callback
, NULL
);
357 strncpy (folder
, argfolder
, sizeof(folder
));
358 if (get_folder_info (argfolder
, msg
)) {
359 context_replace (pfolder
, argfolder
);/* update current folder */
360 context_save (); /* save the context file */
363 * Since recurse wasn't done in get_folder_info(),
364 * we still need to list all level-1 sub-folders.
367 crawl_folders (folder
, get_folder_info_callback
, NULL
);
370 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
373 * Check if folder exists. If not, then see if
374 * we should create it, or just exit.
376 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
378 if (get_folder_info (folder
, msg
) && argfolder
) {
379 /* update current folder */
380 context_replace (pfolder
, argfolder
);
385 * Print out folder information
389 context_save (); /* save the context file */
395 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
398 struct msgs
*mp
= NULL
;
403 * if necessary, reallocate the space
404 * for folder information
406 if (total_folders
>= maxFolderInfo
) {
407 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
408 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
419 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
421 * create message structure and get folder info
423 if (!(mp
= folder_read (fold
, fpack
))) {
424 inform("unable to read folder %s, continuing...", fold
);
425 *crawl_children
= false;
429 /* set the current message */
430 if (msg
&& !sfold (mp
, msg
))
434 if (folder_pack (&mp
, fverb
) == -1) {
435 *crawl_children
= false; /* to please clang static analyzer */
438 seq_save (mp
); /* synchronize the sequences */
439 context_save (); /* save the context file */
442 /* record info for this folder */
443 if ((ftotal
> 0) || !fshort
) {
444 fi
[i
].nummsg
= mp
->nummsg
;
445 fi
[i
].curmsg
= mp
->curmsg
;
446 fi
[i
].lowmsg
= mp
->lowmsg
;
447 fi
[i
].hghmsg
= mp
->hghmsg
;
448 fi
[i
].others
= other_files (mp
);
451 folder_free (mp
); /* free folder/message structure */
454 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
455 && (fi
[i
].error
== 0));
460 get_folder_info_callback (char *fold
, void *baton
)
465 get_folder_info_body (fold
, NULL
, &crawl_children
);
467 return crawl_children
;
471 get_folder_info (char *fold
, char *msg
)
476 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
478 if (crawl_children
) {
479 crawl_folders (fold
, get_folder_info_callback
, NULL
);
486 * Print folder information
493 bool hasempty
= false;
495 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
496 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
497 int nummsgdigits
, lowmsgdigits
;
498 int hghmsgdigits
, curmsgdigits
;
499 char tmpname
[BUFSIZ
];
502 * compute a few values needed to for
503 * printing various fields
505 for (i
= 0; i
< total_folders
; i
++) {
506 /* length of folder name */
507 len
= strlen (fi
[i
].name
);
511 /* If folder has error, skip the rest */
515 /* calculate total number of messages */
516 total_msgs
+= fi
[i
].nummsg
;
518 /* maximum number of messages */
519 if (fi
[i
].nummsg
> maxnummsg
)
520 maxnummsg
= fi
[i
].nummsg
;
522 /* maximum low message */
523 if (fi
[i
].lowmsg
> maxlowmsg
)
524 maxlowmsg
= fi
[i
].lowmsg
;
526 /* maximum high message */
527 if (fi
[i
].hghmsg
> maxhghmsg
)
528 maxhghmsg
= fi
[i
].hghmsg
;
530 /* maximum current message */
531 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
532 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
533 fi
[i
].curmsg
> maxcurmsg
)
534 maxcurmsg
= fi
[i
].curmsg
;
536 /* check for empty folders */
537 if (fi
[i
].nummsg
== 0)
540 nummsgdigits
= num_digits (maxnummsg
);
541 lowmsgdigits
= num_digits (maxlowmsg
);
542 hghmsgdigits
= num_digits (maxhghmsg
);
543 curmsgdigits
= num_digits (maxcurmsg
);
545 if (hasempty
&& nummsgdigits
< 2)
551 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
552 printf ("%-*s %*s %-*s; %-*s %*s\n",
554 nummsgdigits
+ 13, "# MESSAGES",
555 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
556 curmsgdigits
+ 4, "CUR",
560 * Print folder information
562 if (all
|| fshort
|| ftotal
< 1) {
563 for (i
= 0; i
< total_folders
; i
++) {
569 /* Add `+' to end of name, if folder is current */
570 if (strcmp (folder
, fi
[i
].name
))
571 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
573 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
576 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
580 printf ("%-*s ", maxlen
+1, tmpname
);
582 curprinted
= false; /* remember if we print cur */
583 if (fi
[i
].nummsg
== 0) {
584 printf ("has %*s messages%*s",
586 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
588 printf ("has %*d message%1s (%*d-%*d)",
589 nummsgdigits
, fi
[i
].nummsg
,
590 PLURALS(fi
[i
].nummsg
),
591 lowmsgdigits
, fi
[i
].lowmsg
,
592 hghmsgdigits
, fi
[i
].hghmsg
);
593 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
595 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
600 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
606 * Print folder/message totals
608 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
611 printf ("TOTAL = %d message%s in %d folder%s.\n",
612 total_msgs
, PLURALS(total_msgs
),
613 total_folders
, PLURALS(total_folders
));
620 * Set the current message and synchronize sequences
624 sfold (struct msgs
*mp
, char *msg
)
626 /* parse the message range/sequence/name and set SELECTED */
627 if (!m_convert (mp
, msg
))
630 if (mp
->numsel
> 1) {
631 inform("only one message at a time!, continuing...");
634 seq_setprev (mp
); /* set the previous-sequence */
635 seq_setcur (mp
, mp
->lowsel
);/* set current message */
636 seq_save (mp
); /* synchronize message sequences */
637 context_save (); /* save the context file */
644 * Do the read only folders
648 readonly_folders (void)
654 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
655 atrlen
= strlen (atrcur
);
657 for (np
= m_defs
; np
; np
= np
->n_next
)
658 if (ssequal (atrcur
, np
->n_name
)
659 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
660 get_folder_info (np
->n_name
+ atrlen
, NULL
);