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/context_del.h"
12 #include "sbr/context_find.h"
13 #include "sbr/brkstring.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/error.h"
19 #include "h/crawl_folders.h"
22 #include "sbr/m_maildir.h"
24 #define FOLDER_SWITCHES \
26 X("noall", 0, NALLSW) \
27 X("create", 0, CREATSW) \
28 X("nocreate", 0, NCREATSW) \
29 X("fast", 0, FASTSW) \
30 X("nofast", 0, NFASTSW) \
31 X("header", 0, HDRSW) \
32 X("noheader", 0, NHDRSW) \
33 X("pack", 0, PACKSW) \
34 X("nopack", 0, NPACKSW) \
35 X("verbose", 0, VERBSW) \
36 X("noverbose", 0, NVERBSW) \
37 X("recurse", 0, RECURSW) \
38 X("norecurse", 0, NRECRSW) \
39 X("total", 0, TOTALSW) \
40 X("nototal", 0, NTOTLSW) \
41 X("list", 0, LISTSW) \
42 X("nolist", 0, NLISTSW) \
43 X("print", 0, PRNTSW) \
44 X("noprint", 0, NPRNTSW) \
45 X("push", 0, PUSHSW) \
47 X("version", 0, VERSIONSW) \
48 X("help", 0, HELPSW) \
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(FOLDER
);
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
58 static bool fshort
; /* output only folder names */
59 static int fcreat
= 0; /* should we ask to create new folders? */
60 static bool fpack
; /* are we packing the folder? */
61 static bool fverb
; /* print actions taken while packing folder */
62 static int fheader
= 0; /* should we output a header? */
63 static bool frecurse
; /* recurse through subfolders */
64 static int ftotal
= 0; /* should we output the totals? */
65 static bool all
; /* should we output all folders */
67 static int total_folders
= 0; /* total number of folders */
70 static char *stack
= "Folder-Stack";
71 static char folder
[BUFSIZ
];
74 * Structure to hold information about
75 * folders as we scan them.
83 int others
; /* others == 1 if other files in folder */
84 int error
; /* error == 1 for unreadable folder */
88 * Dynamically allocated space to hold
89 * all the folder information.
91 static struct FolderInfo
*fi
;
92 static int maxFolderInfo
;
97 static int get_folder_info (char *, char *);
98 static crawl_callback_t get_folder_info_callback
;
99 static void print_folders (void);
100 static int sfold (struct msgs
*, char *);
101 static void readonly_folders (void);
104 * Function for printing error message if folder does not exist with
108 nonexistent_folder (int status
)
111 die("folder %s does not exist", folder
);
116 main (int argc
, char **argv
)
118 bool printsw
= false;
122 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
123 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
125 if (nmh_init(argv
[0], true, true)) { return 1; }
128 * If program was invoked with name ending
129 * in `s', then add switch `-all'.
131 all
= has_suffix_c(argv
[0], 's');
133 arguments
= getarguments (invo_name
, argc
, argv
, 1);
136 while ((cp
= *argp
++)) {
138 switch (smatch (++cp
, switches
)) {
140 ambigsw (cp
, switches
);
143 die("-%s unknown", cp
);
146 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
148 print_help (buf
, switches
, 1);
151 print_version(invo_name
);
237 if (*cp
== '+' || *cp
== '@') {
239 die("only one folder at a time!");
240 argfolder
= pluspath (cp
);
243 die("only one (current) message at a time!");
248 if (!context_find ("path"))
249 free (path ("./", TFOLDER
));
250 nmhdir
= concat (m_maildir (""), "/", NULL
);
253 * If we aren't working with the folder stack
254 * (-push, -pop, -list) then the default is to print.
256 if (!pushsw
&& !popsw
&& !listsw
)
259 /* Pushing a folder onto the folder stack */
262 /* If no folder is given, the current folder and */
263 /* the top of the folder stack are swapped. */
264 if ((cp
= context_find (stack
))) {
266 ap
= brkstring (dp
, " ", "\n");
267 argfolder
= getcpy(*ap
++);
269 die("no other folder");
271 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
272 cp
= add (*ap
, add (" ", cp
));
274 context_replace (stack
, cp
); /* update folder stack */
276 /* update folder stack */
277 context_replace (stack
,
278 (cp
= context_find (stack
))
279 ? concat (getfolder (1), " ", cp
, NULL
)
280 : mh_xstrdup(getfolder(1)));
284 /* Popping a folder off of the folder stack */
287 die("sorry, no folders allowed with -pop");
288 if ((cp
= context_find (stack
))) {
290 ap
= brkstring (dp
, " ", "\n");
291 argfolder
= getcpy(*ap
++);
293 die("folder stack empty");
296 /* if there's anything left in the stack */
299 cp
= add (*ap
, add (" ", cp
));
300 context_replace (stack
, cp
); /* update folder stack */
302 context_del (stack
); /* delete folder stack entry from context */
306 if (pushsw
|| popsw
) {
307 cp
= m_maildir(argfolder
);
308 if (access (cp
, F_OK
) == NOTOK
)
309 adios (cp
, "unable to find folder");
310 context_replace (pfolder
, argfolder
); /* update current folder */
311 context_save (); /* save the context file */
315 /* Listing the folder stack */
317 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
318 if ((cp
= context_find (stack
))) {
320 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
330 /* Allocate initial space to record folder information */
331 maxFolderInfo
= CRAWL_NUMFOLDERS
;
332 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
337 /* change directory to base of nmh directory for crawl_folders */
338 if (chdir (nmhdir
) == NOTOK
)
339 adios (nmhdir
, "unable to change directory to");
340 if (all
|| ftotal
> 0) {
342 * If no folder is given, do them all
346 inform("no folder given for message %s, continuing...", msg
);
347 readonly_folders (); /* do any readonly folders */
348 cp
= context_find(pfolder
);
349 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
350 crawl_folders (".", get_folder_info_callback
, NULL
);
352 strncpy (folder
, argfolder
, sizeof(folder
));
353 if (get_folder_info (argfolder
, msg
)) {
354 context_replace (pfolder
, argfolder
);/* update current folder */
355 context_save (); /* save the context file */
358 * Since recurse wasn't done in get_folder_info(),
359 * we still need to list all level-1 sub-folders.
362 crawl_folders (folder
, get_folder_info_callback
, NULL
);
365 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
368 * Check if folder exists. If not, then see if
369 * we should create it, or just exit.
371 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
373 if (get_folder_info (folder
, msg
) && argfolder
) {
374 /* update current folder */
375 context_replace (pfolder
, argfolder
);
380 * Print out folder information
384 context_save (); /* save the context file */
390 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
393 struct msgs
*mp
= NULL
;
398 * if necessary, reallocate the space
399 * for folder information
401 if (total_folders
>= maxFolderInfo
) {
402 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
403 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
414 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
416 * create message structure and get folder info
418 if (!(mp
= folder_read (fold
, fpack
))) {
419 inform("unable to read folder %s, continuing...", fold
);
420 *crawl_children
= false;
424 /* set the current message */
425 if (msg
&& !sfold (mp
, msg
))
429 if (folder_pack (&mp
, fverb
) == -1) {
430 *crawl_children
= false; /* to please clang static analyzer */
433 seq_save (mp
); /* synchronize the sequences */
434 context_save (); /* save the context file */
437 /* record info for this folder */
438 if ((ftotal
> 0) || !fshort
) {
439 fi
[i
].nummsg
= mp
->nummsg
;
440 fi
[i
].curmsg
= mp
->curmsg
;
441 fi
[i
].lowmsg
= mp
->lowmsg
;
442 fi
[i
].hghmsg
= mp
->hghmsg
;
443 fi
[i
].others
= other_files (mp
);
446 folder_free (mp
); /* free folder/message structure */
449 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
450 && (fi
[i
].error
== 0));
455 get_folder_info_callback (char *fold
, void *baton
)
460 get_folder_info_body (fold
, NULL
, &crawl_children
);
462 return crawl_children
;
466 get_folder_info (char *fold
, char *msg
)
471 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
473 if (crawl_children
) {
474 crawl_folders (fold
, get_folder_info_callback
, NULL
);
481 * Print folder information
488 bool hasempty
= false;
490 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
491 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
492 int nummsgdigits
, lowmsgdigits
;
493 int hghmsgdigits
, curmsgdigits
;
494 char tmpname
[BUFSIZ
];
497 * compute a few values needed to for
498 * printing various fields
500 for (i
= 0; i
< total_folders
; i
++) {
501 /* length of folder name */
502 len
= strlen (fi
[i
].name
);
506 /* If folder has error, skip the rest */
510 /* calculate total number of messages */
511 total_msgs
+= fi
[i
].nummsg
;
513 /* maximum number of messages */
514 if (fi
[i
].nummsg
> maxnummsg
)
515 maxnummsg
= fi
[i
].nummsg
;
517 /* maximum low message */
518 if (fi
[i
].lowmsg
> maxlowmsg
)
519 maxlowmsg
= fi
[i
].lowmsg
;
521 /* maximum high message */
522 if (fi
[i
].hghmsg
> maxhghmsg
)
523 maxhghmsg
= fi
[i
].hghmsg
;
525 /* maximum current message */
526 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
527 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
528 fi
[i
].curmsg
> maxcurmsg
)
529 maxcurmsg
= fi
[i
].curmsg
;
531 /* check for empty folders */
532 if (fi
[i
].nummsg
== 0)
535 nummsgdigits
= num_digits (maxnummsg
);
536 lowmsgdigits
= num_digits (maxlowmsg
);
537 hghmsgdigits
= num_digits (maxhghmsg
);
538 curmsgdigits
= num_digits (maxcurmsg
);
540 if (hasempty
&& nummsgdigits
< 2)
546 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
547 printf ("%-*s %*s %-*s; %-*s %*s\n",
549 nummsgdigits
+ 13, "# MESSAGES",
550 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
551 curmsgdigits
+ 4, "CUR",
555 * Print folder information
557 if (all
|| fshort
|| ftotal
< 1) {
558 for (i
= 0; i
< total_folders
; i
++) {
564 /* Add `+' to end of name, if folder is current */
565 if (strcmp (folder
, fi
[i
].name
))
566 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
568 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
571 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
575 printf ("%-*s ", maxlen
+1, tmpname
);
577 curprinted
= false; /* remember if we print cur */
578 if (fi
[i
].nummsg
== 0) {
579 printf ("has %*s messages%*s",
581 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
583 printf ("has %*d message%1s (%*d-%*d)",
584 nummsgdigits
, fi
[i
].nummsg
,
585 PLURALS(fi
[i
].nummsg
),
586 lowmsgdigits
, fi
[i
].lowmsg
,
587 hghmsgdigits
, fi
[i
].hghmsg
);
588 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
590 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
595 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
601 * Print folder/message totals
603 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
606 printf ("TOTAL = %d message%s in %d folder%s.\n",
607 total_msgs
, PLURALS(total_msgs
),
608 total_folders
, PLURALS(total_folders
));
615 * Set the current message and synchronize sequences
619 sfold (struct msgs
*mp
, char *msg
)
621 /* parse the message range/sequence/name and set SELECTED */
622 if (!m_convert (mp
, msg
))
625 if (mp
->numsel
> 1) {
626 inform("only one message at a time!, continuing...");
629 seq_setprev (mp
); /* set the previous-sequence */
630 seq_setcur (mp
, mp
->lowsel
);/* set current message */
631 seq_save (mp
); /* synchronize message sequences */
632 context_save (); /* save the context file */
639 * Do the read only folders
643 readonly_folders (void)
649 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
650 atrlen
= strlen (atrcur
);
652 for (np
= m_defs
; np
; np
= np
->n_next
)
653 if (ssequal (atrcur
, np
->n_name
)
654 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
655 get_folder_info (np
->n_name
+ atrlen
, NULL
);