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_help.h"
12 #include "sbr/error.h"
13 #include "h/crawl_folders.h"
16 #include "sbr/m_maildir.h"
18 #define FOLDER_SWITCHES \
20 X("noall", 0, NALLSW) \
21 X("create", 0, CREATSW) \
22 X("nocreate", 0, NCREATSW) \
23 X("fast", 0, FASTSW) \
24 X("nofast", 0, NFASTSW) \
25 X("header", 0, HDRSW) \
26 X("noheader", 0, NHDRSW) \
27 X("pack", 0, PACKSW) \
28 X("nopack", 0, NPACKSW) \
29 X("verbose", 0, VERBSW) \
30 X("noverbose", 0, NVERBSW) \
31 X("recurse", 0, RECURSW) \
32 X("norecurse", 0, NRECRSW) \
33 X("total", 0, TOTALSW) \
34 X("nototal", 0, NTOTLSW) \
35 X("list", 0, LISTSW) \
36 X("nolist", 0, NLISTSW) \
37 X("print", 0, PRNTSW) \
38 X("noprint", 0, NPRNTSW) \
39 X("push", 0, PUSHSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(FOLDER
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
52 static bool fshort
; /* output only folder names */
53 static int fcreat
= 0; /* should we ask to create new folders? */
54 static bool fpack
; /* are we packing the folder? */
55 static bool fverb
; /* print actions taken while packing folder */
56 static int fheader
= 0; /* should we output a header? */
57 static bool frecurse
; /* recurse through subfolders */
58 static int ftotal
= 0; /* should we output the totals? */
59 static bool all
; /* should we output all folders */
61 static int total_folders
= 0; /* total number of folders */
64 static char *stack
= "Folder-Stack";
65 static char folder
[BUFSIZ
];
68 * Structure to hold information about
69 * folders as we scan them.
77 int others
; /* others == 1 if other files in folder */
78 int error
; /* error == 1 for unreadable folder */
82 * Dynamically allocated space to hold
83 * all the folder information.
85 static struct FolderInfo
*fi
;
86 static int maxFolderInfo
;
91 static int get_folder_info (char *, char *);
92 static crawl_callback_t get_folder_info_callback
;
93 static void print_folders (void);
94 static int sfold (struct msgs
*, char *);
95 static void readonly_folders (void);
98 * Function for printing error message if folder does not exist with
102 nonexistent_folder (int status
)
105 die("folder %s does not exist", folder
);
110 main (int argc
, char **argv
)
112 bool printsw
= false;
116 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
117 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
119 if (nmh_init(argv
[0], true, true)) { return 1; }
122 * If program was invoked with name ending
123 * in `s', then add switch `-all'.
125 all
= has_suffix_c(argv
[0], 's');
127 arguments
= getarguments (invo_name
, argc
, argv
, 1);
130 while ((cp
= *argp
++)) {
132 switch (smatch (++cp
, switches
)) {
134 ambigsw (cp
, switches
);
137 die("-%s unknown", cp
);
140 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
142 print_help (buf
, switches
, 1);
145 print_version(invo_name
);
231 if (*cp
== '+' || *cp
== '@') {
233 die("only one folder at a time!");
234 argfolder
= pluspath (cp
);
237 die("only one (current) message at a time!");
242 if (!context_find ("path"))
243 free (path ("./", TFOLDER
));
244 nmhdir
= concat (m_maildir (""), "/", NULL
);
247 * If we aren't working with the folder stack
248 * (-push, -pop, -list) then the default is to print.
250 if (!pushsw
&& !popsw
&& !listsw
)
253 /* Pushing a folder onto the folder stack */
256 /* If no folder is given, the current folder and */
257 /* the top of the folder stack are swapped. */
258 if ((cp
= context_find (stack
))) {
260 ap
= brkstring (dp
, " ", "\n");
261 argfolder
= getcpy(*ap
++);
263 die("no other folder");
265 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
266 cp
= add (*ap
, add (" ", cp
));
268 context_replace (stack
, cp
); /* update folder stack */
270 /* update folder stack */
271 context_replace (stack
,
272 (cp
= context_find (stack
))
273 ? concat (getfolder (1), " ", cp
, NULL
)
274 : mh_xstrdup(getfolder(1)));
278 /* Popping a folder off of the folder stack */
281 die("sorry, no folders allowed with -pop");
282 if ((cp
= context_find (stack
))) {
284 ap
= brkstring (dp
, " ", "\n");
285 argfolder
= getcpy(*ap
++);
287 die("folder stack empty");
290 /* if there's anything left in the stack */
293 cp
= add (*ap
, add (" ", cp
));
294 context_replace (stack
, cp
); /* update folder stack */
296 context_del (stack
); /* delete folder stack entry from context */
300 if (pushsw
|| popsw
) {
301 cp
= m_maildir(argfolder
);
302 if (access (cp
, F_OK
) == NOTOK
)
303 adios (cp
, "unable to find folder");
304 context_replace (pfolder
, argfolder
); /* update current folder */
305 context_save (); /* save the context file */
309 /* Listing the folder stack */
311 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
312 if ((cp
= context_find (stack
))) {
314 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
324 /* Allocate initial space to record folder information */
325 maxFolderInfo
= CRAWL_NUMFOLDERS
;
326 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
331 /* change directory to base of nmh directory for crawl_folders */
332 if (chdir (nmhdir
) == NOTOK
)
333 adios (nmhdir
, "unable to change directory to");
334 if (all
|| ftotal
> 0) {
336 * If no folder is given, do them all
340 inform("no folder given for message %s, continuing...", msg
);
341 readonly_folders (); /* do any readonly folders */
342 cp
= context_find(pfolder
);
343 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
344 crawl_folders (".", get_folder_info_callback
, NULL
);
346 strncpy (folder
, argfolder
, sizeof(folder
));
347 if (get_folder_info (argfolder
, msg
)) {
348 context_replace (pfolder
, argfolder
);/* update current folder */
349 context_save (); /* save the context file */
352 * Since recurse wasn't done in get_folder_info(),
353 * we still need to list all level-1 sub-folders.
356 crawl_folders (folder
, get_folder_info_callback
, NULL
);
359 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
362 * Check if folder exists. If not, then see if
363 * we should create it, or just exit.
365 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
367 if (get_folder_info (folder
, msg
) && argfolder
) {
368 /* update current folder */
369 context_replace (pfolder
, argfolder
);
374 * Print out folder information
378 context_save (); /* save the context file */
384 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
387 struct msgs
*mp
= NULL
;
392 * if necessary, reallocate the space
393 * for folder information
395 if (total_folders
>= maxFolderInfo
) {
396 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
397 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
408 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
410 * create message structure and get folder info
412 if (!(mp
= folder_read (fold
, fpack
))) {
413 inform("unable to read folder %s, continuing...", fold
);
414 *crawl_children
= false;
418 /* set the current message */
419 if (msg
&& !sfold (mp
, msg
))
423 if (folder_pack (&mp
, fverb
) == -1) {
424 *crawl_children
= false; /* to please clang static analyzer */
427 seq_save (mp
); /* synchronize the sequences */
428 context_save (); /* save the context file */
431 /* record info for this folder */
432 if ((ftotal
> 0) || !fshort
) {
433 fi
[i
].nummsg
= mp
->nummsg
;
434 fi
[i
].curmsg
= mp
->curmsg
;
435 fi
[i
].lowmsg
= mp
->lowmsg
;
436 fi
[i
].hghmsg
= mp
->hghmsg
;
437 fi
[i
].others
= other_files (mp
);
440 folder_free (mp
); /* free folder/message structure */
443 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
444 && (fi
[i
].error
== 0));
449 get_folder_info_callback (char *fold
, void *baton
)
454 get_folder_info_body (fold
, NULL
, &crawl_children
);
456 return crawl_children
;
460 get_folder_info (char *fold
, char *msg
)
465 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
467 if (crawl_children
) {
468 crawl_folders (fold
, get_folder_info_callback
, NULL
);
475 * Print folder information
482 bool hasempty
= false;
484 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
485 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
486 int nummsgdigits
, lowmsgdigits
;
487 int hghmsgdigits
, curmsgdigits
;
488 char tmpname
[BUFSIZ
];
491 * compute a few values needed to for
492 * printing various fields
494 for (i
= 0; i
< total_folders
; i
++) {
495 /* length of folder name */
496 len
= strlen (fi
[i
].name
);
500 /* If folder has error, skip the rest */
504 /* calculate total number of messages */
505 total_msgs
+= fi
[i
].nummsg
;
507 /* maximum number of messages */
508 if (fi
[i
].nummsg
> maxnummsg
)
509 maxnummsg
= fi
[i
].nummsg
;
511 /* maximum low message */
512 if (fi
[i
].lowmsg
> maxlowmsg
)
513 maxlowmsg
= fi
[i
].lowmsg
;
515 /* maximum high message */
516 if (fi
[i
].hghmsg
> maxhghmsg
)
517 maxhghmsg
= fi
[i
].hghmsg
;
519 /* maximum current message */
520 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
521 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
522 fi
[i
].curmsg
> maxcurmsg
)
523 maxcurmsg
= fi
[i
].curmsg
;
525 /* check for empty folders */
526 if (fi
[i
].nummsg
== 0)
529 nummsgdigits
= num_digits (maxnummsg
);
530 lowmsgdigits
= num_digits (maxlowmsg
);
531 hghmsgdigits
= num_digits (maxhghmsg
);
532 curmsgdigits
= num_digits (maxcurmsg
);
534 if (hasempty
&& nummsgdigits
< 2)
540 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
541 printf ("%-*s %*s %-*s; %-*s %*s\n",
543 nummsgdigits
+ 13, "# MESSAGES",
544 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
545 curmsgdigits
+ 4, "CUR",
549 * Print folder information
551 if (all
|| fshort
|| ftotal
< 1) {
552 for (i
= 0; i
< total_folders
; i
++) {
558 /* Add `+' to end of name, if folder is current */
559 if (strcmp (folder
, fi
[i
].name
))
560 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
562 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
565 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
569 printf ("%-*s ", maxlen
+1, tmpname
);
571 curprinted
= false; /* remember if we print cur */
572 if (fi
[i
].nummsg
== 0) {
573 printf ("has %*s messages%*s",
575 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
577 printf ("has %*d message%1s (%*d-%*d)",
578 nummsgdigits
, fi
[i
].nummsg
,
579 PLURALS(fi
[i
].nummsg
),
580 lowmsgdigits
, fi
[i
].lowmsg
,
581 hghmsgdigits
, fi
[i
].hghmsg
);
582 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
584 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
589 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
595 * Print folder/message totals
597 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
600 printf ("TOTAL = %d message%s in %d folder%s.\n",
601 total_msgs
, PLURALS(total_msgs
),
602 total_folders
, PLURALS(total_folders
));
609 * Set the current message and synchronize sequences
613 sfold (struct msgs
*mp
, char *msg
)
615 /* parse the message range/sequence/name and set SELECTED */
616 if (!m_convert (mp
, msg
))
619 if (mp
->numsel
> 1) {
620 inform("only one message at a time!, continuing...");
623 seq_setprev (mp
); /* set the previous-sequence */
624 seq_setcur (mp
, mp
->lowsel
);/* set current message */
625 seq_save (mp
); /* synchronize message sequences */
626 context_save (); /* save the context file */
633 * Do the read only folders
637 readonly_folders (void)
643 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
644 atrlen
= strlen (atrcur
);
646 for (np
= m_defs
; np
; np
= np
->n_next
)
647 if (ssequal (atrcur
, np
->n_name
)
648 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
649 get_folder_info (np
->n_name
+ atrlen
, NULL
);