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/getfolder.h"
12 #include "sbr/folder_read.h"
13 #include "sbr/folder_pack.h"
14 #include "sbr/folder_free.h"
15 #include "sbr/context_save.h"
16 #include "sbr/context_replace.h"
17 #include "sbr/context_del.h"
18 #include "sbr/context_find.h"
19 #include "sbr/brkstring.h"
20 #include "sbr/ambigsw.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/error.h"
25 #include "h/crawl_folders.h"
28 #include "sbr/m_maildir.h"
30 #define FOLDER_SWITCHES \
32 X("noall", 0, NALLSW) \
33 X("create", 0, CREATSW) \
34 X("nocreate", 0, NCREATSW) \
35 X("fast", 0, FASTSW) \
36 X("nofast", 0, NFASTSW) \
37 X("header", 0, HDRSW) \
38 X("noheader", 0, NHDRSW) \
39 X("pack", 0, PACKSW) \
40 X("nopack", 0, NPACKSW) \
41 X("verbose", 0, VERBSW) \
42 X("noverbose", 0, NVERBSW) \
43 X("recurse", 0, RECURSW) \
44 X("norecurse", 0, NRECRSW) \
45 X("total", 0, TOTALSW) \
46 X("nototal", 0, NTOTLSW) \
47 X("list", 0, LISTSW) \
48 X("nolist", 0, NLISTSW) \
49 X("print", 0, PRNTSW) \
50 X("noprint", 0, NPRNTSW) \
51 X("push", 0, PUSHSW) \
53 X("version", 0, VERSIONSW) \
54 X("help", 0, HELPSW) \
56 #define X(sw, minchars, id) id,
57 DEFINE_SWITCH_ENUM(FOLDER
);
60 #define X(sw, minchars, id) { sw, minchars, id },
61 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
64 static bool fshort
; /* output only folder names */
65 static int fcreat
= 0; /* should we ask to create new folders? */
66 static bool fpack
; /* are we packing the folder? */
67 static bool fverb
; /* print actions taken while packing folder */
68 static int fheader
= 0; /* should we output a header? */
69 static bool frecurse
; /* recurse through subfolders */
70 static int ftotal
= 0; /* should we output the totals? */
71 static bool all
; /* should we output all folders */
73 static int total_folders
= 0; /* total number of folders */
76 static char *stack
= "Folder-Stack";
77 static char folder
[BUFSIZ
];
80 * Structure to hold information about
81 * folders as we scan them.
89 int others
; /* others == 1 if other files in folder */
90 int error
; /* error == 1 for unreadable folder */
94 * Dynamically allocated space to hold
95 * all the folder information.
97 static struct FolderInfo
*fi
;
98 static int maxFolderInfo
;
103 static int get_folder_info (char *, char *);
104 static crawl_callback_t get_folder_info_callback
;
105 static void print_folders (void);
106 static int sfold (struct msgs
*, char *);
107 static void readonly_folders (void);
110 * Function for printing error message if folder does not exist with
114 nonexistent_folder (int status
)
117 die("folder %s does not exist", folder
);
122 main (int argc
, char **argv
)
124 bool printsw
= false;
128 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
129 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
131 if (nmh_init(argv
[0], true, true)) { return 1; }
134 * If program was invoked with name ending
135 * in `s', then add switch `-all'.
137 all
= has_suffix_c(argv
[0], 's');
139 arguments
= getarguments (invo_name
, argc
, argv
, 1);
142 while ((cp
= *argp
++)) {
144 switch (smatch (++cp
, switches
)) {
146 ambigsw (cp
, switches
);
149 die("-%s unknown", cp
);
152 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
154 print_help (buf
, switches
, 1);
157 print_version(invo_name
);
243 if (*cp
== '+' || *cp
== '@') {
245 die("only one folder at a time!");
246 argfolder
= pluspath (cp
);
249 die("only one (current) message at a time!");
254 if (!context_find ("path"))
255 free (path ("./", TFOLDER
));
256 nmhdir
= concat (m_maildir (""), "/", NULL
);
259 * If we aren't working with the folder stack
260 * (-push, -pop, -list) then the default is to print.
262 if (!pushsw
&& !popsw
&& !listsw
)
265 /* Pushing a folder onto the folder stack */
268 /* If no folder is given, the current folder and */
269 /* the top of the folder stack are swapped. */
270 if ((cp
= context_find (stack
))) {
272 ap
= brkstring (dp
, " ", "\n");
273 argfolder
= getcpy(*ap
++);
275 die("no other folder");
277 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
278 cp
= add (*ap
, add (" ", cp
));
280 context_replace (stack
, cp
); /* update folder stack */
282 /* update folder stack */
283 context_replace (stack
,
284 (cp
= context_find (stack
))
285 ? concat (getfolder (1), " ", cp
, NULL
)
286 : mh_xstrdup(getfolder(1)));
290 /* Popping a folder off of the folder stack */
293 die("sorry, no folders allowed with -pop");
294 if ((cp
= context_find (stack
))) {
296 ap
= brkstring (dp
, " ", "\n");
297 argfolder
= getcpy(*ap
++);
299 die("folder stack empty");
302 /* if there's anything left in the stack */
305 cp
= add (*ap
, add (" ", cp
));
306 context_replace (stack
, cp
); /* update folder stack */
308 context_del (stack
); /* delete folder stack entry from context */
312 if (pushsw
|| popsw
) {
313 cp
= m_maildir(argfolder
);
314 if (access (cp
, F_OK
) == NOTOK
)
315 adios (cp
, "unable to find folder");
316 context_replace (pfolder
, argfolder
); /* update current folder */
317 context_save (); /* save the context file */
321 /* Listing the folder stack */
323 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
324 if ((cp
= context_find (stack
))) {
326 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
336 /* Allocate initial space to record folder information */
337 maxFolderInfo
= CRAWL_NUMFOLDERS
;
338 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
343 /* change directory to base of nmh directory for crawl_folders */
344 if (chdir (nmhdir
) == NOTOK
)
345 adios (nmhdir
, "unable to change directory to");
346 if (all
|| ftotal
> 0) {
348 * If no folder is given, do them all
352 inform("no folder given for message %s, continuing...", msg
);
353 readonly_folders (); /* do any readonly folders */
354 cp
= context_find(pfolder
);
355 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
356 crawl_folders (".", get_folder_info_callback
, NULL
);
358 strncpy (folder
, argfolder
, sizeof(folder
));
359 if (get_folder_info (argfolder
, msg
)) {
360 context_replace (pfolder
, argfolder
);/* update current folder */
361 context_save (); /* save the context file */
364 * Since recurse wasn't done in get_folder_info(),
365 * we still need to list all level-1 sub-folders.
368 crawl_folders (folder
, get_folder_info_callback
, NULL
);
371 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
374 * Check if folder exists. If not, then see if
375 * we should create it, or just exit.
377 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
379 if (get_folder_info (folder
, msg
) && argfolder
) {
380 /* update current folder */
381 context_replace (pfolder
, argfolder
);
386 * Print out folder information
390 context_save (); /* save the context file */
396 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
399 struct msgs
*mp
= NULL
;
404 * if necessary, reallocate the space
405 * for folder information
407 if (total_folders
>= maxFolderInfo
) {
408 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
409 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
420 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
422 * create message structure and get folder info
424 if (!(mp
= folder_read (fold
, fpack
))) {
425 inform("unable to read folder %s, continuing...", fold
);
426 *crawl_children
= false;
430 /* set the current message */
431 if (msg
&& !sfold (mp
, msg
))
435 if (folder_pack (&mp
, fverb
) == -1) {
436 *crawl_children
= false; /* to please clang static analyzer */
439 seq_save (mp
); /* synchronize the sequences */
440 context_save (); /* save the context file */
443 /* record info for this folder */
444 if ((ftotal
> 0) || !fshort
) {
445 fi
[i
].nummsg
= mp
->nummsg
;
446 fi
[i
].curmsg
= mp
->curmsg
;
447 fi
[i
].lowmsg
= mp
->lowmsg
;
448 fi
[i
].hghmsg
= mp
->hghmsg
;
449 fi
[i
].others
= other_files (mp
);
452 folder_free (mp
); /* free folder/message structure */
455 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
456 && (fi
[i
].error
== 0));
461 get_folder_info_callback (char *fold
, void *baton
)
466 get_folder_info_body (fold
, NULL
, &crawl_children
);
468 return crawl_children
;
472 get_folder_info (char *fold
, char *msg
)
477 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
479 if (crawl_children
) {
480 crawl_folders (fold
, get_folder_info_callback
, NULL
);
487 * Print folder information
494 bool hasempty
= false;
496 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
497 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
498 int nummsgdigits
, lowmsgdigits
;
499 int hghmsgdigits
, curmsgdigits
;
500 char tmpname
[BUFSIZ
];
503 * compute a few values needed to for
504 * printing various fields
506 for (i
= 0; i
< total_folders
; i
++) {
507 /* length of folder name */
508 len
= strlen (fi
[i
].name
);
512 /* If folder has error, skip the rest */
516 /* calculate total number of messages */
517 total_msgs
+= fi
[i
].nummsg
;
519 /* maximum number of messages */
520 if (fi
[i
].nummsg
> maxnummsg
)
521 maxnummsg
= fi
[i
].nummsg
;
523 /* maximum low message */
524 if (fi
[i
].lowmsg
> maxlowmsg
)
525 maxlowmsg
= fi
[i
].lowmsg
;
527 /* maximum high message */
528 if (fi
[i
].hghmsg
> maxhghmsg
)
529 maxhghmsg
= fi
[i
].hghmsg
;
531 /* maximum current message */
532 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
533 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
534 fi
[i
].curmsg
> maxcurmsg
)
535 maxcurmsg
= fi
[i
].curmsg
;
537 /* check for empty folders */
538 if (fi
[i
].nummsg
== 0)
541 nummsgdigits
= num_digits (maxnummsg
);
542 lowmsgdigits
= num_digits (maxlowmsg
);
543 hghmsgdigits
= num_digits (maxhghmsg
);
544 curmsgdigits
= num_digits (maxcurmsg
);
546 if (hasempty
&& nummsgdigits
< 2)
552 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
553 printf ("%-*s %*s %-*s; %-*s %*s\n",
555 nummsgdigits
+ 13, "# MESSAGES",
556 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
557 curmsgdigits
+ 4, "CUR",
561 * Print folder information
563 if (all
|| fshort
|| ftotal
< 1) {
564 for (i
= 0; i
< total_folders
; i
++) {
570 /* Add `+' to end of name, if folder is current */
571 if (strcmp (folder
, fi
[i
].name
))
572 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
574 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
577 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
581 printf ("%-*s ", maxlen
+1, tmpname
);
583 curprinted
= false; /* remember if we print cur */
584 if (fi
[i
].nummsg
== 0) {
585 printf ("has %*s messages%*s",
587 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
589 printf ("has %*d message%1s (%*d-%*d)",
590 nummsgdigits
, fi
[i
].nummsg
,
591 PLURALS(fi
[i
].nummsg
),
592 lowmsgdigits
, fi
[i
].lowmsg
,
593 hghmsgdigits
, fi
[i
].hghmsg
);
594 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
596 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
601 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
607 * Print folder/message totals
609 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
612 printf ("TOTAL = %d message%s in %d folder%s.\n",
613 total_msgs
, PLURALS(total_msgs
),
614 total_folders
, PLURALS(total_folders
));
621 * Set the current message and synchronize sequences
625 sfold (struct msgs
*mp
, char *msg
)
627 /* parse the message range/sequence/name and set SELECTED */
628 if (!m_convert (mp
, msg
))
631 if (mp
->numsel
> 1) {
632 inform("only one message at a time!, continuing...");
635 seq_setprev (mp
); /* set the previous-sequence */
636 seq_setcur (mp
, mp
->lowsel
);/* set current message */
637 seq_save (mp
); /* synchronize message sequences */
638 context_save (); /* save the context file */
645 * Do the read only folders
649 readonly_folders (void)
655 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
656 atrlen
= strlen (atrcur
);
658 for (np
= m_defs
; np
; np
= np
->n_next
)
659 if (ssequal (atrcur
, np
->n_name
)
660 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
661 get_folder_info (np
->n_name
+ atrlen
, NULL
);