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.
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
15 #include "h/crawl_folders.h"
18 #include "sbr/m_maildir.h"
20 #define FOLDER_SWITCHES \
22 X("noall", 0, NALLSW) \
23 X("create", 0, CREATSW) \
24 X("nocreate", 0, NCREATSW) \
25 X("fast", 0, FASTSW) \
26 X("nofast", 0, NFASTSW) \
27 X("header", 0, HDRSW) \
28 X("noheader", 0, NHDRSW) \
29 X("pack", 0, PACKSW) \
30 X("nopack", 0, NPACKSW) \
31 X("verbose", 0, VERBSW) \
32 X("noverbose", 0, NVERBSW) \
33 X("recurse", 0, RECURSW) \
34 X("norecurse", 0, NRECRSW) \
35 X("total", 0, TOTALSW) \
36 X("nototal", 0, NTOTLSW) \
37 X("list", 0, LISTSW) \
38 X("nolist", 0, NLISTSW) \
39 X("print", 0, PRNTSW) \
40 X("noprint", 0, NPRNTSW) \
41 X("push", 0, PUSHSW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
46 #define X(sw, minchars, id) id,
47 DEFINE_SWITCH_ENUM(FOLDER
);
50 #define X(sw, minchars, id) { sw, minchars, id },
51 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
54 static bool fshort
; /* output only folder names */
55 static int fcreat
= 0; /* should we ask to create new folders? */
56 static bool fpack
; /* are we packing the folder? */
57 static bool fverb
; /* print actions taken while packing folder */
58 static int fheader
= 0; /* should we output a header? */
59 static bool frecurse
; /* recurse through subfolders */
60 static int ftotal
= 0; /* should we output the totals? */
61 static bool all
; /* should we output all folders */
63 static int total_folders
= 0; /* total number of folders */
66 static char *stack
= "Folder-Stack";
67 static char folder
[BUFSIZ
];
70 * Structure to hold information about
71 * folders as we scan them.
79 int others
; /* others == 1 if other files in folder */
80 int error
; /* error == 1 for unreadable folder */
84 * Dynamically allocated space to hold
85 * all the folder information.
87 static struct FolderInfo
*fi
;
88 static int maxFolderInfo
;
93 static int get_folder_info (char *, char *);
94 static crawl_callback_t get_folder_info_callback
;
95 static void print_folders (void);
96 static int sfold (struct msgs
*, char *);
97 static void readonly_folders (void);
100 * Function for printing error message if folder does not exist with
104 nonexistent_folder (int status
)
107 die("folder %s does not exist", folder
);
112 main (int argc
, char **argv
)
114 bool printsw
= false;
118 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
119 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
121 if (nmh_init(argv
[0], true, true)) { return 1; }
124 * If program was invoked with name ending
125 * in `s', then add switch `-all'.
127 all
= has_suffix_c(argv
[0], 's');
129 arguments
= getarguments (invo_name
, argc
, argv
, 1);
132 while ((cp
= *argp
++)) {
134 switch (smatch (++cp
, switches
)) {
136 ambigsw (cp
, switches
);
139 die("-%s unknown", cp
);
142 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
144 print_help (buf
, switches
, 1);
147 print_version(invo_name
);
233 if (*cp
== '+' || *cp
== '@') {
235 die("only one folder at a time!");
236 argfolder
= pluspath (cp
);
239 die("only one (current) message at a time!");
244 if (!context_find ("path"))
245 free (path ("./", TFOLDER
));
246 nmhdir
= concat (m_maildir (""), "/", NULL
);
249 * If we aren't working with the folder stack
250 * (-push, -pop, -list) then the default is to print.
252 if (!pushsw
&& !popsw
&& !listsw
)
255 /* Pushing a folder onto the folder stack */
258 /* If no folder is given, the current folder and */
259 /* the top of the folder stack are swapped. */
260 if ((cp
= context_find (stack
))) {
262 ap
= brkstring (dp
, " ", "\n");
263 argfolder
= getcpy(*ap
++);
265 die("no other folder");
267 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
268 cp
= add (*ap
, add (" ", cp
));
270 context_replace (stack
, cp
); /* update folder stack */
272 /* update folder stack */
273 context_replace (stack
,
274 (cp
= context_find (stack
))
275 ? concat (getfolder (1), " ", cp
, NULL
)
276 : mh_xstrdup(getfolder(1)));
280 /* Popping a folder off of the folder stack */
283 die("sorry, no folders allowed with -pop");
284 if ((cp
= context_find (stack
))) {
286 ap
= brkstring (dp
, " ", "\n");
287 argfolder
= getcpy(*ap
++);
289 die("folder stack empty");
292 /* if there's anything left in the stack */
295 cp
= add (*ap
, add (" ", cp
));
296 context_replace (stack
, cp
); /* update folder stack */
298 context_del (stack
); /* delete folder stack entry from context */
302 if (pushsw
|| popsw
) {
303 cp
= m_maildir(argfolder
);
304 if (access (cp
, F_OK
) == NOTOK
)
305 adios (cp
, "unable to find folder");
306 context_replace (pfolder
, argfolder
); /* update current folder */
307 context_save (); /* save the context file */
311 /* Listing the folder stack */
313 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
314 if ((cp
= context_find (stack
))) {
316 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
326 /* Allocate initial space to record folder information */
327 maxFolderInfo
= CRAWL_NUMFOLDERS
;
328 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
333 /* change directory to base of nmh directory for crawl_folders */
334 if (chdir (nmhdir
) == NOTOK
)
335 adios (nmhdir
, "unable to change directory to");
336 if (all
|| ftotal
> 0) {
338 * If no folder is given, do them all
342 inform("no folder given for message %s, continuing...", msg
);
343 readonly_folders (); /* do any readonly folders */
344 cp
= context_find(pfolder
);
345 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
346 crawl_folders (".", get_folder_info_callback
, NULL
);
348 strncpy (folder
, argfolder
, sizeof(folder
));
349 if (get_folder_info (argfolder
, msg
)) {
350 context_replace (pfolder
, argfolder
);/* update current folder */
351 context_save (); /* save the context file */
354 * Since recurse wasn't done in get_folder_info(),
355 * we still need to list all level-1 sub-folders.
358 crawl_folders (folder
, get_folder_info_callback
, NULL
);
361 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
364 * Check if folder exists. If not, then see if
365 * we should create it, or just exit.
367 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
369 if (get_folder_info (folder
, msg
) && argfolder
) {
370 /* update current folder */
371 context_replace (pfolder
, argfolder
);
376 * Print out folder information
380 context_save (); /* save the context file */
386 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
389 struct msgs
*mp
= NULL
;
394 * if necessary, reallocate the space
395 * for folder information
397 if (total_folders
>= maxFolderInfo
) {
398 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
399 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
410 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
412 * create message structure and get folder info
414 if (!(mp
= folder_read (fold
, fpack
))) {
415 inform("unable to read folder %s, continuing...", fold
);
416 *crawl_children
= false;
420 /* set the current message */
421 if (msg
&& !sfold (mp
, msg
))
425 if (folder_pack (&mp
, fverb
) == -1) {
426 *crawl_children
= false; /* to please clang static analyzer */
429 seq_save (mp
); /* synchronize the sequences */
430 context_save (); /* save the context file */
433 /* record info for this folder */
434 if ((ftotal
> 0) || !fshort
) {
435 fi
[i
].nummsg
= mp
->nummsg
;
436 fi
[i
].curmsg
= mp
->curmsg
;
437 fi
[i
].lowmsg
= mp
->lowmsg
;
438 fi
[i
].hghmsg
= mp
->hghmsg
;
439 fi
[i
].others
= other_files (mp
);
442 folder_free (mp
); /* free folder/message structure */
445 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
446 && (fi
[i
].error
== 0));
451 get_folder_info_callback (char *fold
, void *baton
)
456 get_folder_info_body (fold
, NULL
, &crawl_children
);
458 return crawl_children
;
462 get_folder_info (char *fold
, char *msg
)
467 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
469 if (crawl_children
) {
470 crawl_folders (fold
, get_folder_info_callback
, NULL
);
477 * Print folder information
484 bool hasempty
= false;
486 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
487 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
488 int nummsgdigits
, lowmsgdigits
;
489 int hghmsgdigits
, curmsgdigits
;
490 char tmpname
[BUFSIZ
];
493 * compute a few values needed to for
494 * printing various fields
496 for (i
= 0; i
< total_folders
; i
++) {
497 /* length of folder name */
498 len
= strlen (fi
[i
].name
);
502 /* If folder has error, skip the rest */
506 /* calculate total number of messages */
507 total_msgs
+= fi
[i
].nummsg
;
509 /* maximum number of messages */
510 if (fi
[i
].nummsg
> maxnummsg
)
511 maxnummsg
= fi
[i
].nummsg
;
513 /* maximum low message */
514 if (fi
[i
].lowmsg
> maxlowmsg
)
515 maxlowmsg
= fi
[i
].lowmsg
;
517 /* maximum high message */
518 if (fi
[i
].hghmsg
> maxhghmsg
)
519 maxhghmsg
= fi
[i
].hghmsg
;
521 /* maximum current message */
522 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
523 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
524 fi
[i
].curmsg
> maxcurmsg
)
525 maxcurmsg
= fi
[i
].curmsg
;
527 /* check for empty folders */
528 if (fi
[i
].nummsg
== 0)
531 nummsgdigits
= num_digits (maxnummsg
);
532 lowmsgdigits
= num_digits (maxlowmsg
);
533 hghmsgdigits
= num_digits (maxhghmsg
);
534 curmsgdigits
= num_digits (maxcurmsg
);
536 if (hasempty
&& nummsgdigits
< 2)
542 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
543 printf ("%-*s %*s %-*s; %-*s %*s\n",
545 nummsgdigits
+ 13, "# MESSAGES",
546 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
547 curmsgdigits
+ 4, "CUR",
551 * Print folder information
553 if (all
|| fshort
|| ftotal
< 1) {
554 for (i
= 0; i
< total_folders
; i
++) {
560 /* Add `+' to end of name, if folder is current */
561 if (strcmp (folder
, fi
[i
].name
))
562 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
564 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
567 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
571 printf ("%-*s ", maxlen
+1, tmpname
);
573 curprinted
= false; /* remember if we print cur */
574 if (fi
[i
].nummsg
== 0) {
575 printf ("has %*s messages%*s",
577 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
579 printf ("has %*d message%1s (%*d-%*d)",
580 nummsgdigits
, fi
[i
].nummsg
,
581 PLURALS(fi
[i
].nummsg
),
582 lowmsgdigits
, fi
[i
].lowmsg
,
583 hghmsgdigits
, fi
[i
].hghmsg
);
584 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
586 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
591 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
597 * Print folder/message totals
599 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
602 printf ("TOTAL = %d message%s in %d folder%s.\n",
603 total_msgs
, PLURALS(total_msgs
),
604 total_folders
, PLURALS(total_folders
));
611 * Set the current message and synchronize sequences
615 sfold (struct msgs
*mp
, char *msg
)
617 /* parse the message range/sequence/name and set SELECTED */
618 if (!m_convert (mp
, msg
))
621 if (mp
->numsel
> 1) {
622 inform("only one message at a time!, continuing...");
625 seq_setprev (mp
); /* set the previous-sequence */
626 seq_setcur (mp
, mp
->lowsel
);/* set current message */
627 seq_save (mp
); /* synchronize message sequences */
628 context_save (); /* save the context file */
635 * Do the read only folders
639 readonly_folders (void)
645 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
646 atrlen
= strlen (atrcur
);
648 for (np
= m_defs
; np
; np
= np
->n_next
)
649 if (ssequal (atrcur
, np
->n_name
)
650 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
651 get_folder_info (np
->n_name
+ atrlen
, NULL
);