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_pack.h"
12 #include "sbr/folder_free.h"
13 #include "sbr/context_save.h"
14 #include "sbr/context_replace.h"
15 #include "sbr/context_del.h"
16 #include "sbr/context_find.h"
17 #include "sbr/brkstring.h"
18 #include "sbr/ambigsw.h"
20 #include "sbr/print_version.h"
21 #include "sbr/print_help.h"
22 #include "sbr/error.h"
23 #include "h/crawl_folders.h"
26 #include "sbr/m_maildir.h"
28 #define FOLDER_SWITCHES \
30 X("noall", 0, NALLSW) \
31 X("create", 0, CREATSW) \
32 X("nocreate", 0, NCREATSW) \
33 X("fast", 0, FASTSW) \
34 X("nofast", 0, NFASTSW) \
35 X("header", 0, HDRSW) \
36 X("noheader", 0, NHDRSW) \
37 X("pack", 0, PACKSW) \
38 X("nopack", 0, NPACKSW) \
39 X("verbose", 0, VERBSW) \
40 X("noverbose", 0, NVERBSW) \
41 X("recurse", 0, RECURSW) \
42 X("norecurse", 0, NRECRSW) \
43 X("total", 0, TOTALSW) \
44 X("nototal", 0, NTOTLSW) \
45 X("list", 0, LISTSW) \
46 X("nolist", 0, NLISTSW) \
47 X("print", 0, PRNTSW) \
48 X("noprint", 0, NPRNTSW) \
49 X("push", 0, PUSHSW) \
51 X("version", 0, VERSIONSW) \
52 X("help", 0, HELPSW) \
54 #define X(sw, minchars, id) id,
55 DEFINE_SWITCH_ENUM(FOLDER
);
58 #define X(sw, minchars, id) { sw, minchars, id },
59 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
62 static bool fshort
; /* output only folder names */
63 static int fcreat
= 0; /* should we ask to create new folders? */
64 static bool fpack
; /* are we packing the folder? */
65 static bool fverb
; /* print actions taken while packing folder */
66 static int fheader
= 0; /* should we output a header? */
67 static bool frecurse
; /* recurse through subfolders */
68 static int ftotal
= 0; /* should we output the totals? */
69 static bool all
; /* should we output all folders */
71 static int total_folders
= 0; /* total number of folders */
74 static char *stack
= "Folder-Stack";
75 static char folder
[BUFSIZ
];
78 * Structure to hold information about
79 * folders as we scan them.
87 int others
; /* others == 1 if other files in folder */
88 int error
; /* error == 1 for unreadable folder */
92 * Dynamically allocated space to hold
93 * all the folder information.
95 static struct FolderInfo
*fi
;
96 static int maxFolderInfo
;
101 static int get_folder_info (char *, char *);
102 static crawl_callback_t get_folder_info_callback
;
103 static void print_folders (void);
104 static int sfold (struct msgs
*, char *);
105 static void readonly_folders (void);
108 * Function for printing error message if folder does not exist with
112 nonexistent_folder (int status
)
115 die("folder %s does not exist", folder
);
120 main (int argc
, char **argv
)
122 bool printsw
= false;
126 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
127 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
129 if (nmh_init(argv
[0], true, true)) { return 1; }
132 * If program was invoked with name ending
133 * in `s', then add switch `-all'.
135 all
= has_suffix_c(argv
[0], 's');
137 arguments
= getarguments (invo_name
, argc
, argv
, 1);
140 while ((cp
= *argp
++)) {
142 switch (smatch (++cp
, switches
)) {
144 ambigsw (cp
, switches
);
147 die("-%s unknown", cp
);
150 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
152 print_help (buf
, switches
, 1);
155 print_version(invo_name
);
241 if (*cp
== '+' || *cp
== '@') {
243 die("only one folder at a time!");
244 argfolder
= pluspath (cp
);
247 die("only one (current) message at a time!");
252 if (!context_find ("path"))
253 free (path ("./", TFOLDER
));
254 nmhdir
= concat (m_maildir (""), "/", NULL
);
257 * If we aren't working with the folder stack
258 * (-push, -pop, -list) then the default is to print.
260 if (!pushsw
&& !popsw
&& !listsw
)
263 /* Pushing a folder onto the folder stack */
266 /* If no folder is given, the current folder and */
267 /* the top of the folder stack are swapped. */
268 if ((cp
= context_find (stack
))) {
270 ap
= brkstring (dp
, " ", "\n");
271 argfolder
= getcpy(*ap
++);
273 die("no other folder");
275 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
276 cp
= add (*ap
, add (" ", cp
));
278 context_replace (stack
, cp
); /* update folder stack */
280 /* update folder stack */
281 context_replace (stack
,
282 (cp
= context_find (stack
))
283 ? concat (getfolder (1), " ", cp
, NULL
)
284 : mh_xstrdup(getfolder(1)));
288 /* Popping a folder off of the folder stack */
291 die("sorry, no folders allowed with -pop");
292 if ((cp
= context_find (stack
))) {
294 ap
= brkstring (dp
, " ", "\n");
295 argfolder
= getcpy(*ap
++);
297 die("folder stack empty");
300 /* if there's anything left in the stack */
303 cp
= add (*ap
, add (" ", cp
));
304 context_replace (stack
, cp
); /* update folder stack */
306 context_del (stack
); /* delete folder stack entry from context */
310 if (pushsw
|| popsw
) {
311 cp
= m_maildir(argfolder
);
312 if (access (cp
, F_OK
) == NOTOK
)
313 adios (cp
, "unable to find folder");
314 context_replace (pfolder
, argfolder
); /* update current folder */
315 context_save (); /* save the context file */
319 /* Listing the folder stack */
321 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
322 if ((cp
= context_find (stack
))) {
324 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
334 /* Allocate initial space to record folder information */
335 maxFolderInfo
= CRAWL_NUMFOLDERS
;
336 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
341 /* change directory to base of nmh directory for crawl_folders */
342 if (chdir (nmhdir
) == NOTOK
)
343 adios (nmhdir
, "unable to change directory to");
344 if (all
|| ftotal
> 0) {
346 * If no folder is given, do them all
350 inform("no folder given for message %s, continuing...", msg
);
351 readonly_folders (); /* do any readonly folders */
352 cp
= context_find(pfolder
);
353 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
354 crawl_folders (".", get_folder_info_callback
, NULL
);
356 strncpy (folder
, argfolder
, sizeof(folder
));
357 if (get_folder_info (argfolder
, msg
)) {
358 context_replace (pfolder
, argfolder
);/* update current folder */
359 context_save (); /* save the context file */
362 * Since recurse wasn't done in get_folder_info(),
363 * we still need to list all level-1 sub-folders.
366 crawl_folders (folder
, get_folder_info_callback
, NULL
);
369 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
372 * Check if folder exists. If not, then see if
373 * we should create it, or just exit.
375 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
377 if (get_folder_info (folder
, msg
) && argfolder
) {
378 /* update current folder */
379 context_replace (pfolder
, argfolder
);
384 * Print out folder information
388 context_save (); /* save the context file */
394 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
397 struct msgs
*mp
= NULL
;
402 * if necessary, reallocate the space
403 * for folder information
405 if (total_folders
>= maxFolderInfo
) {
406 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
407 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
418 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
420 * create message structure and get folder info
422 if (!(mp
= folder_read (fold
, fpack
))) {
423 inform("unable to read folder %s, continuing...", fold
);
424 *crawl_children
= false;
428 /* set the current message */
429 if (msg
&& !sfold (mp
, msg
))
433 if (folder_pack (&mp
, fverb
) == -1) {
434 *crawl_children
= false; /* to please clang static analyzer */
437 seq_save (mp
); /* synchronize the sequences */
438 context_save (); /* save the context file */
441 /* record info for this folder */
442 if ((ftotal
> 0) || !fshort
) {
443 fi
[i
].nummsg
= mp
->nummsg
;
444 fi
[i
].curmsg
= mp
->curmsg
;
445 fi
[i
].lowmsg
= mp
->lowmsg
;
446 fi
[i
].hghmsg
= mp
->hghmsg
;
447 fi
[i
].others
= other_files (mp
);
450 folder_free (mp
); /* free folder/message structure */
453 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
454 && (fi
[i
].error
== 0));
459 get_folder_info_callback (char *fold
, void *baton
)
464 get_folder_info_body (fold
, NULL
, &crawl_children
);
466 return crawl_children
;
470 get_folder_info (char *fold
, char *msg
)
475 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
477 if (crawl_children
) {
478 crawl_folders (fold
, get_folder_info_callback
, NULL
);
485 * Print folder information
492 bool hasempty
= false;
494 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
495 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
496 int nummsgdigits
, lowmsgdigits
;
497 int hghmsgdigits
, curmsgdigits
;
498 char tmpname
[BUFSIZ
];
501 * compute a few values needed to for
502 * printing various fields
504 for (i
= 0; i
< total_folders
; i
++) {
505 /* length of folder name */
506 len
= strlen (fi
[i
].name
);
510 /* If folder has error, skip the rest */
514 /* calculate total number of messages */
515 total_msgs
+= fi
[i
].nummsg
;
517 /* maximum number of messages */
518 if (fi
[i
].nummsg
> maxnummsg
)
519 maxnummsg
= fi
[i
].nummsg
;
521 /* maximum low message */
522 if (fi
[i
].lowmsg
> maxlowmsg
)
523 maxlowmsg
= fi
[i
].lowmsg
;
525 /* maximum high message */
526 if (fi
[i
].hghmsg
> maxhghmsg
)
527 maxhghmsg
= fi
[i
].hghmsg
;
529 /* maximum current message */
530 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
531 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
532 fi
[i
].curmsg
> maxcurmsg
)
533 maxcurmsg
= fi
[i
].curmsg
;
535 /* check for empty folders */
536 if (fi
[i
].nummsg
== 0)
539 nummsgdigits
= num_digits (maxnummsg
);
540 lowmsgdigits
= num_digits (maxlowmsg
);
541 hghmsgdigits
= num_digits (maxhghmsg
);
542 curmsgdigits
= num_digits (maxcurmsg
);
544 if (hasempty
&& nummsgdigits
< 2)
550 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
551 printf ("%-*s %*s %-*s; %-*s %*s\n",
553 nummsgdigits
+ 13, "# MESSAGES",
554 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
555 curmsgdigits
+ 4, "CUR",
559 * Print folder information
561 if (all
|| fshort
|| ftotal
< 1) {
562 for (i
= 0; i
< total_folders
; i
++) {
568 /* Add `+' to end of name, if folder is current */
569 if (strcmp (folder
, fi
[i
].name
))
570 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
572 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
575 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
579 printf ("%-*s ", maxlen
+1, tmpname
);
581 curprinted
= false; /* remember if we print cur */
582 if (fi
[i
].nummsg
== 0) {
583 printf ("has %*s messages%*s",
585 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
587 printf ("has %*d message%1s (%*d-%*d)",
588 nummsgdigits
, fi
[i
].nummsg
,
589 PLURALS(fi
[i
].nummsg
),
590 lowmsgdigits
, fi
[i
].lowmsg
,
591 hghmsgdigits
, fi
[i
].hghmsg
);
592 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
594 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
599 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
605 * Print folder/message totals
607 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
610 printf ("TOTAL = %d message%s in %d folder%s.\n",
611 total_msgs
, PLURALS(total_msgs
),
612 total_folders
, PLURALS(total_folders
));
619 * Set the current message and synchronize sequences
623 sfold (struct msgs
*mp
, char *msg
)
625 /* parse the message range/sequence/name and set SELECTED */
626 if (!m_convert (mp
, msg
))
629 if (mp
->numsel
> 1) {
630 inform("only one message at a time!, continuing...");
633 seq_setprev (mp
); /* set the previous-sequence */
634 seq_setcur (mp
, mp
->lowsel
);/* set current message */
635 seq_save (mp
); /* synchronize message sequences */
636 context_save (); /* save the context file */
643 * Do the read only folders
647 readonly_folders (void)
653 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
654 atrlen
= strlen (atrcur
);
656 for (np
= m_defs
; np
; np
= np
->n_next
)
657 if (ssequal (atrcur
, np
->n_name
)
658 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
659 get_folder_info (np
->n_name
+ atrlen
, NULL
);