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/getcpy.h"
12 #include "sbr/m_convert.h"
13 #include "sbr/getfolder.h"
14 #include "sbr/folder_read.h"
15 #include "sbr/folder_pack.h"
16 #include "sbr/folder_free.h"
17 #include "sbr/context_save.h"
18 #include "sbr/context_replace.h"
19 #include "sbr/context_del.h"
20 #include "sbr/context_find.h"
21 #include "sbr/brkstring.h"
22 #include "sbr/ambigsw.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/error.h"
27 #include "h/crawl_folders.h"
30 #include "sbr/m_maildir.h"
32 #define FOLDER_SWITCHES \
34 X("noall", 0, NALLSW) \
35 X("create", 0, CREATSW) \
36 X("nocreate", 0, NCREATSW) \
37 X("fast", 0, FASTSW) \
38 X("nofast", 0, NFASTSW) \
39 X("header", 0, HDRSW) \
40 X("noheader", 0, NHDRSW) \
41 X("pack", 0, PACKSW) \
42 X("nopack", 0, NPACKSW) \
43 X("verbose", 0, VERBSW) \
44 X("noverbose", 0, NVERBSW) \
45 X("recurse", 0, RECURSW) \
46 X("norecurse", 0, NRECRSW) \
47 X("total", 0, TOTALSW) \
48 X("nototal", 0, NTOTLSW) \
49 X("list", 0, LISTSW) \
50 X("nolist", 0, NLISTSW) \
51 X("print", 0, PRNTSW) \
52 X("noprint", 0, NPRNTSW) \
53 X("push", 0, PUSHSW) \
55 X("version", 0, VERSIONSW) \
56 X("help", 0, HELPSW) \
58 #define X(sw, minchars, id) id,
59 DEFINE_SWITCH_ENUM(FOLDER
);
62 #define X(sw, minchars, id) { sw, minchars, id },
63 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
66 static bool fshort
; /* output only folder names */
67 static int fcreat
= 0; /* should we ask to create new folders? */
68 static bool fpack
; /* are we packing the folder? */
69 static bool fverb
; /* print actions taken while packing folder */
70 static int fheader
= 0; /* should we output a header? */
71 static bool frecurse
; /* recurse through subfolders */
72 static int ftotal
= 0; /* should we output the totals? */
73 static bool all
; /* should we output all folders */
75 static int total_folders
= 0; /* total number of folders */
78 static char *stack
= "Folder-Stack";
79 static char folder
[BUFSIZ
];
82 * Structure to hold information about
83 * folders as we scan them.
91 int others
; /* others == 1 if other files in folder */
92 int error
; /* error == 1 for unreadable folder */
96 * Dynamically allocated space to hold
97 * all the folder information.
99 static struct FolderInfo
*fi
;
100 static int maxFolderInfo
;
105 static int get_folder_info (char *, char *);
106 static crawl_callback_t get_folder_info_callback
;
107 static void print_folders (void);
108 static int sfold (struct msgs
*, char *);
109 static void readonly_folders (void);
112 * Function for printing error message if folder does not exist with
116 nonexistent_folder (int status
)
119 die("folder %s does not exist", folder
);
124 main (int argc
, char **argv
)
126 bool printsw
= false;
130 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
131 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
133 if (nmh_init(argv
[0], true, true)) { return 1; }
136 * If program was invoked with name ending
137 * in `s', then add switch `-all'.
139 all
= has_suffix_c(argv
[0], 's');
141 arguments
= getarguments (invo_name
, argc
, argv
, 1);
144 while ((cp
= *argp
++)) {
146 switch (smatch (++cp
, switches
)) {
148 ambigsw (cp
, switches
);
151 die("-%s unknown", cp
);
154 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
156 print_help (buf
, switches
, 1);
159 print_version(invo_name
);
245 if (*cp
== '+' || *cp
== '@') {
247 die("only one folder at a time!");
248 argfolder
= pluspath (cp
);
251 die("only one (current) message at a time!");
256 if (!context_find ("path"))
257 free (path ("./", TFOLDER
));
258 nmhdir
= concat (m_maildir (""), "/", NULL
);
261 * If we aren't working with the folder stack
262 * (-push, -pop, -list) then the default is to print.
264 if (!pushsw
&& !popsw
&& !listsw
)
267 /* Pushing a folder onto the folder stack */
270 /* If no folder is given, the current folder and */
271 /* the top of the folder stack are swapped. */
272 if ((cp
= context_find (stack
))) {
274 ap
= brkstring (dp
, " ", "\n");
275 argfolder
= getcpy(*ap
++);
277 die("no other folder");
279 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
280 cp
= add (*ap
, add (" ", cp
));
282 context_replace (stack
, cp
); /* update folder stack */
284 /* update folder stack */
285 context_replace (stack
,
286 (cp
= context_find (stack
))
287 ? concat (getfolder (1), " ", cp
, NULL
)
288 : mh_xstrdup(getfolder(1)));
292 /* Popping a folder off of the folder stack */
295 die("sorry, no folders allowed with -pop");
296 if ((cp
= context_find (stack
))) {
298 ap
= brkstring (dp
, " ", "\n");
299 argfolder
= getcpy(*ap
++);
301 die("folder stack empty");
304 /* if there's anything left in the stack */
307 cp
= add (*ap
, add (" ", cp
));
308 context_replace (stack
, cp
); /* update folder stack */
310 context_del (stack
); /* delete folder stack entry from context */
314 if (pushsw
|| popsw
) {
315 cp
= m_maildir(argfolder
);
316 if (access (cp
, F_OK
) == NOTOK
)
317 adios (cp
, "unable to find folder");
318 context_replace (pfolder
, argfolder
); /* update current folder */
319 context_save (); /* save the context file */
323 /* Listing the folder stack */
325 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
326 if ((cp
= context_find (stack
))) {
328 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
338 /* Allocate initial space to record folder information */
339 maxFolderInfo
= CRAWL_NUMFOLDERS
;
340 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
345 /* change directory to base of nmh directory for crawl_folders */
346 if (chdir (nmhdir
) == NOTOK
)
347 adios (nmhdir
, "unable to change directory to");
348 if (all
|| ftotal
> 0) {
350 * If no folder is given, do them all
354 inform("no folder given for message %s, continuing...", msg
);
355 readonly_folders (); /* do any readonly folders */
356 cp
= context_find(pfolder
);
357 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
358 crawl_folders (".", get_folder_info_callback
, NULL
);
360 strncpy (folder
, argfolder
, sizeof(folder
));
361 if (get_folder_info (argfolder
, msg
)) {
362 context_replace (pfolder
, argfolder
);/* update current folder */
363 context_save (); /* save the context file */
366 * Since recurse wasn't done in get_folder_info(),
367 * we still need to list all level-1 sub-folders.
370 crawl_folders (folder
, get_folder_info_callback
, NULL
);
373 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
376 * Check if folder exists. If not, then see if
377 * we should create it, or just exit.
379 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
381 if (get_folder_info (folder
, msg
) && argfolder
) {
382 /* update current folder */
383 context_replace (pfolder
, argfolder
);
388 * Print out folder information
392 context_save (); /* save the context file */
398 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
401 struct msgs
*mp
= NULL
;
406 * if necessary, reallocate the space
407 * for folder information
409 if (total_folders
>= maxFolderInfo
) {
410 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
411 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
422 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
424 * create message structure and get folder info
426 if (!(mp
= folder_read (fold
, fpack
))) {
427 inform("unable to read folder %s, continuing...", fold
);
428 *crawl_children
= false;
432 /* set the current message */
433 if (msg
&& !sfold (mp
, msg
))
437 if (folder_pack (&mp
, fverb
) == -1) {
438 *crawl_children
= false; /* to please clang static analyzer */
441 seq_save (mp
); /* synchronize the sequences */
442 context_save (); /* save the context file */
445 /* record info for this folder */
446 if ((ftotal
> 0) || !fshort
) {
447 fi
[i
].nummsg
= mp
->nummsg
;
448 fi
[i
].curmsg
= mp
->curmsg
;
449 fi
[i
].lowmsg
= mp
->lowmsg
;
450 fi
[i
].hghmsg
= mp
->hghmsg
;
451 fi
[i
].others
= other_files (mp
);
454 folder_free (mp
); /* free folder/message structure */
457 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
458 && (fi
[i
].error
== 0));
463 get_folder_info_callback (char *fold
, void *baton
)
468 get_folder_info_body (fold
, NULL
, &crawl_children
);
470 return crawl_children
;
474 get_folder_info (char *fold
, char *msg
)
479 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
481 if (crawl_children
) {
482 crawl_folders (fold
, get_folder_info_callback
, NULL
);
489 * Print folder information
496 bool hasempty
= false;
498 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
499 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
500 int nummsgdigits
, lowmsgdigits
;
501 int hghmsgdigits
, curmsgdigits
;
502 char tmpname
[BUFSIZ
];
505 * compute a few values needed to for
506 * printing various fields
508 for (i
= 0; i
< total_folders
; i
++) {
509 /* length of folder name */
510 len
= strlen (fi
[i
].name
);
514 /* If folder has error, skip the rest */
518 /* calculate total number of messages */
519 total_msgs
+= fi
[i
].nummsg
;
521 /* maximum number of messages */
522 if (fi
[i
].nummsg
> maxnummsg
)
523 maxnummsg
= fi
[i
].nummsg
;
525 /* maximum low message */
526 if (fi
[i
].lowmsg
> maxlowmsg
)
527 maxlowmsg
= fi
[i
].lowmsg
;
529 /* maximum high message */
530 if (fi
[i
].hghmsg
> maxhghmsg
)
531 maxhghmsg
= fi
[i
].hghmsg
;
533 /* maximum current message */
534 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
535 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
536 fi
[i
].curmsg
> maxcurmsg
)
537 maxcurmsg
= fi
[i
].curmsg
;
539 /* check for empty folders */
540 if (fi
[i
].nummsg
== 0)
543 nummsgdigits
= num_digits (maxnummsg
);
544 lowmsgdigits
= num_digits (maxlowmsg
);
545 hghmsgdigits
= num_digits (maxhghmsg
);
546 curmsgdigits
= num_digits (maxcurmsg
);
548 if (hasempty
&& nummsgdigits
< 2)
554 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
555 printf ("%-*s %*s %-*s; %-*s %*s\n",
557 nummsgdigits
+ 13, "# MESSAGES",
558 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
559 curmsgdigits
+ 4, "CUR",
563 * Print folder information
565 if (all
|| fshort
|| ftotal
< 1) {
566 for (i
= 0; i
< total_folders
; i
++) {
572 /* Add `+' to end of name, if folder is current */
573 if (strcmp (folder
, fi
[i
].name
))
574 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
576 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
579 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
583 printf ("%-*s ", maxlen
+1, tmpname
);
585 curprinted
= false; /* remember if we print cur */
586 if (fi
[i
].nummsg
== 0) {
587 printf ("has %*s messages%*s",
589 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
591 printf ("has %*d message%1s (%*d-%*d)",
592 nummsgdigits
, fi
[i
].nummsg
,
593 PLURALS(fi
[i
].nummsg
),
594 lowmsgdigits
, fi
[i
].lowmsg
,
595 hghmsgdigits
, fi
[i
].hghmsg
);
596 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
598 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
603 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
609 * Print folder/message totals
611 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
614 printf ("TOTAL = %d message%s in %d folder%s.\n",
615 total_msgs
, PLURALS(total_msgs
),
616 total_folders
, PLURALS(total_folders
));
623 * Set the current message and synchronize sequences
627 sfold (struct msgs
*mp
, char *msg
)
629 /* parse the message range/sequence/name and set SELECTED */
630 if (!m_convert (mp
, msg
))
633 if (mp
->numsel
> 1) {
634 inform("only one message at a time!, continuing...");
637 seq_setprev (mp
); /* set the previous-sequence */
638 seq_setcur (mp
, mp
->lowsel
);/* set current message */
639 seq_save (mp
); /* synchronize message sequences */
640 context_save (); /* save the context file */
647 * Do the read only folders
651 readonly_folders (void)
657 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
658 atrlen
= strlen (atrcur
);
660 for (np
= m_defs
; np
; np
= np
->n_next
)
661 if (ssequal (atrcur
, np
->n_name
)
662 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
663 get_folder_info (np
->n_name
+ atrlen
, NULL
);