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/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/error.h"
14 #include "h/crawl_folders.h"
17 #include "sbr/m_maildir.h"
19 #define FOLDER_SWITCHES \
21 X("noall", 0, NALLSW) \
22 X("create", 0, CREATSW) \
23 X("nocreate", 0, NCREATSW) \
24 X("fast", 0, FASTSW) \
25 X("nofast", 0, NFASTSW) \
26 X("header", 0, HDRSW) \
27 X("noheader", 0, NHDRSW) \
28 X("pack", 0, PACKSW) \
29 X("nopack", 0, NPACKSW) \
30 X("verbose", 0, VERBSW) \
31 X("noverbose", 0, NVERBSW) \
32 X("recurse", 0, RECURSW) \
33 X("norecurse", 0, NRECRSW) \
34 X("total", 0, TOTALSW) \
35 X("nototal", 0, NTOTLSW) \
36 X("list", 0, LISTSW) \
37 X("nolist", 0, NLISTSW) \
38 X("print", 0, PRNTSW) \
39 X("noprint", 0, NPRNTSW) \
40 X("push", 0, PUSHSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(FOLDER
);
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
53 static bool fshort
; /* output only folder names */
54 static int fcreat
= 0; /* should we ask to create new folders? */
55 static bool fpack
; /* are we packing the folder? */
56 static bool fverb
; /* print actions taken while packing folder */
57 static int fheader
= 0; /* should we output a header? */
58 static bool frecurse
; /* recurse through subfolders */
59 static int ftotal
= 0; /* should we output the totals? */
60 static bool all
; /* should we output all folders */
62 static int total_folders
= 0; /* total number of folders */
65 static char *stack
= "Folder-Stack";
66 static char folder
[BUFSIZ
];
69 * Structure to hold information about
70 * folders as we scan them.
78 int others
; /* others == 1 if other files in folder */
79 int error
; /* error == 1 for unreadable folder */
83 * Dynamically allocated space to hold
84 * all the folder information.
86 static struct FolderInfo
*fi
;
87 static int maxFolderInfo
;
92 static int get_folder_info (char *, char *);
93 static crawl_callback_t get_folder_info_callback
;
94 static void print_folders (void);
95 static int sfold (struct msgs
*, char *);
96 static void readonly_folders (void);
99 * Function for printing error message if folder does not exist with
103 nonexistent_folder (int status
)
106 die("folder %s does not exist", folder
);
111 main (int argc
, char **argv
)
113 bool printsw
= false;
117 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
118 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
120 if (nmh_init(argv
[0], true, true)) { return 1; }
123 * If program was invoked with name ending
124 * in `s', then add switch `-all'.
126 all
= has_suffix_c(argv
[0], 's');
128 arguments
= getarguments (invo_name
, argc
, argv
, 1);
131 while ((cp
= *argp
++)) {
133 switch (smatch (++cp
, switches
)) {
135 ambigsw (cp
, switches
);
138 die("-%s unknown", cp
);
141 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
143 print_help (buf
, switches
, 1);
146 print_version(invo_name
);
232 if (*cp
== '+' || *cp
== '@') {
234 die("only one folder at a time!");
235 argfolder
= pluspath (cp
);
238 die("only one (current) message at a time!");
243 if (!context_find ("path"))
244 free (path ("./", TFOLDER
));
245 nmhdir
= concat (m_maildir (""), "/", NULL
);
248 * If we aren't working with the folder stack
249 * (-push, -pop, -list) then the default is to print.
251 if (!pushsw
&& !popsw
&& !listsw
)
254 /* Pushing a folder onto the folder stack */
257 /* If no folder is given, the current folder and */
258 /* the top of the folder stack are swapped. */
259 if ((cp
= context_find (stack
))) {
261 ap
= brkstring (dp
, " ", "\n");
262 argfolder
= getcpy(*ap
++);
264 die("no other folder");
266 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
267 cp
= add (*ap
, add (" ", cp
));
269 context_replace (stack
, cp
); /* update folder stack */
271 /* update folder stack */
272 context_replace (stack
,
273 (cp
= context_find (stack
))
274 ? concat (getfolder (1), " ", cp
, NULL
)
275 : mh_xstrdup(getfolder(1)));
279 /* Popping a folder off of the folder stack */
282 die("sorry, no folders allowed with -pop");
283 if ((cp
= context_find (stack
))) {
285 ap
= brkstring (dp
, " ", "\n");
286 argfolder
= getcpy(*ap
++);
288 die("folder stack empty");
291 /* if there's anything left in the stack */
294 cp
= add (*ap
, add (" ", cp
));
295 context_replace (stack
, cp
); /* update folder stack */
297 context_del (stack
); /* delete folder stack entry from context */
301 if (pushsw
|| popsw
) {
302 cp
= m_maildir(argfolder
);
303 if (access (cp
, F_OK
) == NOTOK
)
304 adios (cp
, "unable to find folder");
305 context_replace (pfolder
, argfolder
); /* update current folder */
306 context_save (); /* save the context file */
310 /* Listing the folder stack */
312 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
313 if ((cp
= context_find (stack
))) {
315 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
325 /* Allocate initial space to record folder information */
326 maxFolderInfo
= CRAWL_NUMFOLDERS
;
327 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
332 /* change directory to base of nmh directory for crawl_folders */
333 if (chdir (nmhdir
) == NOTOK
)
334 adios (nmhdir
, "unable to change directory to");
335 if (all
|| ftotal
> 0) {
337 * If no folder is given, do them all
341 inform("no folder given for message %s, continuing...", msg
);
342 readonly_folders (); /* do any readonly folders */
343 cp
= context_find(pfolder
);
344 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
345 crawl_folders (".", get_folder_info_callback
, NULL
);
347 strncpy (folder
, argfolder
, sizeof(folder
));
348 if (get_folder_info (argfolder
, msg
)) {
349 context_replace (pfolder
, argfolder
);/* update current folder */
350 context_save (); /* save the context file */
353 * Since recurse wasn't done in get_folder_info(),
354 * we still need to list all level-1 sub-folders.
357 crawl_folders (folder
, get_folder_info_callback
, NULL
);
360 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
363 * Check if folder exists. If not, then see if
364 * we should create it, or just exit.
366 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
368 if (get_folder_info (folder
, msg
) && argfolder
) {
369 /* update current folder */
370 context_replace (pfolder
, argfolder
);
375 * Print out folder information
379 context_save (); /* save the context file */
385 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
388 struct msgs
*mp
= NULL
;
393 * if necessary, reallocate the space
394 * for folder information
396 if (total_folders
>= maxFolderInfo
) {
397 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
398 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
409 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
411 * create message structure and get folder info
413 if (!(mp
= folder_read (fold
, fpack
))) {
414 inform("unable to read folder %s, continuing...", fold
);
415 *crawl_children
= false;
419 /* set the current message */
420 if (msg
&& !sfold (mp
, msg
))
424 if (folder_pack (&mp
, fverb
) == -1) {
425 *crawl_children
= false; /* to please clang static analyzer */
428 seq_save (mp
); /* synchronize the sequences */
429 context_save (); /* save the context file */
432 /* record info for this folder */
433 if ((ftotal
> 0) || !fshort
) {
434 fi
[i
].nummsg
= mp
->nummsg
;
435 fi
[i
].curmsg
= mp
->curmsg
;
436 fi
[i
].lowmsg
= mp
->lowmsg
;
437 fi
[i
].hghmsg
= mp
->hghmsg
;
438 fi
[i
].others
= other_files (mp
);
441 folder_free (mp
); /* free folder/message structure */
444 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
445 && (fi
[i
].error
== 0));
450 get_folder_info_callback (char *fold
, void *baton
)
455 get_folder_info_body (fold
, NULL
, &crawl_children
);
457 return crawl_children
;
461 get_folder_info (char *fold
, char *msg
)
466 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
468 if (crawl_children
) {
469 crawl_folders (fold
, get_folder_info_callback
, NULL
);
476 * Print folder information
483 bool hasempty
= false;
485 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
486 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
487 int nummsgdigits
, lowmsgdigits
;
488 int hghmsgdigits
, curmsgdigits
;
489 char tmpname
[BUFSIZ
];
492 * compute a few values needed to for
493 * printing various fields
495 for (i
= 0; i
< total_folders
; i
++) {
496 /* length of folder name */
497 len
= strlen (fi
[i
].name
);
501 /* If folder has error, skip the rest */
505 /* calculate total number of messages */
506 total_msgs
+= fi
[i
].nummsg
;
508 /* maximum number of messages */
509 if (fi
[i
].nummsg
> maxnummsg
)
510 maxnummsg
= fi
[i
].nummsg
;
512 /* maximum low message */
513 if (fi
[i
].lowmsg
> maxlowmsg
)
514 maxlowmsg
= fi
[i
].lowmsg
;
516 /* maximum high message */
517 if (fi
[i
].hghmsg
> maxhghmsg
)
518 maxhghmsg
= fi
[i
].hghmsg
;
520 /* maximum current message */
521 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
522 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
523 fi
[i
].curmsg
> maxcurmsg
)
524 maxcurmsg
= fi
[i
].curmsg
;
526 /* check for empty folders */
527 if (fi
[i
].nummsg
== 0)
530 nummsgdigits
= num_digits (maxnummsg
);
531 lowmsgdigits
= num_digits (maxlowmsg
);
532 hghmsgdigits
= num_digits (maxhghmsg
);
533 curmsgdigits
= num_digits (maxcurmsg
);
535 if (hasempty
&& nummsgdigits
< 2)
541 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
542 printf ("%-*s %*s %-*s; %-*s %*s\n",
544 nummsgdigits
+ 13, "# MESSAGES",
545 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
546 curmsgdigits
+ 4, "CUR",
550 * Print folder information
552 if (all
|| fshort
|| ftotal
< 1) {
553 for (i
= 0; i
< total_folders
; i
++) {
559 /* Add `+' to end of name, if folder is current */
560 if (strcmp (folder
, fi
[i
].name
))
561 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
563 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
566 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
570 printf ("%-*s ", maxlen
+1, tmpname
);
572 curprinted
= false; /* remember if we print cur */
573 if (fi
[i
].nummsg
== 0) {
574 printf ("has %*s messages%*s",
576 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
578 printf ("has %*d message%1s (%*d-%*d)",
579 nummsgdigits
, fi
[i
].nummsg
,
580 PLURALS(fi
[i
].nummsg
),
581 lowmsgdigits
, fi
[i
].lowmsg
,
582 hghmsgdigits
, fi
[i
].hghmsg
);
583 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
585 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
590 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
596 * Print folder/message totals
598 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
601 printf ("TOTAL = %d message%s in %d folder%s.\n",
602 total_msgs
, PLURALS(total_msgs
),
603 total_folders
, PLURALS(total_folders
));
610 * Set the current message and synchronize sequences
614 sfold (struct msgs
*mp
, char *msg
)
616 /* parse the message range/sequence/name and set SELECTED */
617 if (!m_convert (mp
, msg
))
620 if (mp
->numsel
> 1) {
621 inform("only one message at a time!, continuing...");
624 seq_setprev (mp
); /* set the previous-sequence */
625 seq_setcur (mp
, mp
->lowsel
);/* set current message */
626 seq_save (mp
); /* synchronize message sequences */
627 context_save (); /* save the context file */
634 * Do the read only folders
638 readonly_folders (void)
644 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
645 atrlen
= strlen (atrcur
);
647 for (np
= m_defs
; np
; np
= np
->n_next
)
648 if (ssequal (atrcur
, np
->n_name
)
649 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
650 get_folder_info (np
->n_name
+ atrlen
, NULL
);