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_save.h"
12 #include "sbr/context_replace.h"
13 #include "sbr/context_del.h"
14 #include "sbr/context_find.h"
15 #include "sbr/brkstring.h"
16 #include "sbr/ambigsw.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/error.h"
21 #include "h/crawl_folders.h"
24 #include "sbr/m_maildir.h"
26 #define FOLDER_SWITCHES \
28 X("noall", 0, NALLSW) \
29 X("create", 0, CREATSW) \
30 X("nocreate", 0, NCREATSW) \
31 X("fast", 0, FASTSW) \
32 X("nofast", 0, NFASTSW) \
33 X("header", 0, HDRSW) \
34 X("noheader", 0, NHDRSW) \
35 X("pack", 0, PACKSW) \
36 X("nopack", 0, NPACKSW) \
37 X("verbose", 0, VERBSW) \
38 X("noverbose", 0, NVERBSW) \
39 X("recurse", 0, RECURSW) \
40 X("norecurse", 0, NRECRSW) \
41 X("total", 0, TOTALSW) \
42 X("nototal", 0, NTOTLSW) \
43 X("list", 0, LISTSW) \
44 X("nolist", 0, NLISTSW) \
45 X("print", 0, PRNTSW) \
46 X("noprint", 0, NPRNTSW) \
47 X("push", 0, PUSHSW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(FOLDER
);
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
60 static bool fshort
; /* output only folder names */
61 static int fcreat
= 0; /* should we ask to create new folders? */
62 static bool fpack
; /* are we packing the folder? */
63 static bool fverb
; /* print actions taken while packing folder */
64 static int fheader
= 0; /* should we output a header? */
65 static bool frecurse
; /* recurse through subfolders */
66 static int ftotal
= 0; /* should we output the totals? */
67 static bool all
; /* should we output all folders */
69 static int total_folders
= 0; /* total number of folders */
72 static char *stack
= "Folder-Stack";
73 static char folder
[BUFSIZ
];
76 * Structure to hold information about
77 * folders as we scan them.
85 int others
; /* others == 1 if other files in folder */
86 int error
; /* error == 1 for unreadable folder */
90 * Dynamically allocated space to hold
91 * all the folder information.
93 static struct FolderInfo
*fi
;
94 static int maxFolderInfo
;
99 static int get_folder_info (char *, char *);
100 static crawl_callback_t get_folder_info_callback
;
101 static void print_folders (void);
102 static int sfold (struct msgs
*, char *);
103 static void readonly_folders (void);
106 * Function for printing error message if folder does not exist with
110 nonexistent_folder (int status
)
113 die("folder %s does not exist", folder
);
118 main (int argc
, char **argv
)
120 bool printsw
= false;
124 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
125 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
127 if (nmh_init(argv
[0], true, true)) { return 1; }
130 * If program was invoked with name ending
131 * in `s', then add switch `-all'.
133 all
= has_suffix_c(argv
[0], 's');
135 arguments
= getarguments (invo_name
, argc
, argv
, 1);
138 while ((cp
= *argp
++)) {
140 switch (smatch (++cp
, switches
)) {
142 ambigsw (cp
, switches
);
145 die("-%s unknown", cp
);
148 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
150 print_help (buf
, switches
, 1);
153 print_version(invo_name
);
239 if (*cp
== '+' || *cp
== '@') {
241 die("only one folder at a time!");
242 argfolder
= pluspath (cp
);
245 die("only one (current) message at a time!");
250 if (!context_find ("path"))
251 free (path ("./", TFOLDER
));
252 nmhdir
= concat (m_maildir (""), "/", NULL
);
255 * If we aren't working with the folder stack
256 * (-push, -pop, -list) then the default is to print.
258 if (!pushsw
&& !popsw
&& !listsw
)
261 /* Pushing a folder onto the folder stack */
264 /* If no folder is given, the current folder and */
265 /* the top of the folder stack are swapped. */
266 if ((cp
= context_find (stack
))) {
268 ap
= brkstring (dp
, " ", "\n");
269 argfolder
= getcpy(*ap
++);
271 die("no other folder");
273 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
274 cp
= add (*ap
, add (" ", cp
));
276 context_replace (stack
, cp
); /* update folder stack */
278 /* update folder stack */
279 context_replace (stack
,
280 (cp
= context_find (stack
))
281 ? concat (getfolder (1), " ", cp
, NULL
)
282 : mh_xstrdup(getfolder(1)));
286 /* Popping a folder off of the folder stack */
289 die("sorry, no folders allowed with -pop");
290 if ((cp
= context_find (stack
))) {
292 ap
= brkstring (dp
, " ", "\n");
293 argfolder
= getcpy(*ap
++);
295 die("folder stack empty");
298 /* if there's anything left in the stack */
301 cp
= add (*ap
, add (" ", cp
));
302 context_replace (stack
, cp
); /* update folder stack */
304 context_del (stack
); /* delete folder stack entry from context */
308 if (pushsw
|| popsw
) {
309 cp
= m_maildir(argfolder
);
310 if (access (cp
, F_OK
) == NOTOK
)
311 adios (cp
, "unable to find folder");
312 context_replace (pfolder
, argfolder
); /* update current folder */
313 context_save (); /* save the context file */
317 /* Listing the folder stack */
319 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
320 if ((cp
= context_find (stack
))) {
322 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
332 /* Allocate initial space to record folder information */
333 maxFolderInfo
= CRAWL_NUMFOLDERS
;
334 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
339 /* change directory to base of nmh directory for crawl_folders */
340 if (chdir (nmhdir
) == NOTOK
)
341 adios (nmhdir
, "unable to change directory to");
342 if (all
|| ftotal
> 0) {
344 * If no folder is given, do them all
348 inform("no folder given for message %s, continuing...", msg
);
349 readonly_folders (); /* do any readonly folders */
350 cp
= context_find(pfolder
);
351 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
352 crawl_folders (".", get_folder_info_callback
, NULL
);
354 strncpy (folder
, argfolder
, sizeof(folder
));
355 if (get_folder_info (argfolder
, msg
)) {
356 context_replace (pfolder
, argfolder
);/* update current folder */
357 context_save (); /* save the context file */
360 * Since recurse wasn't done in get_folder_info(),
361 * we still need to list all level-1 sub-folders.
364 crawl_folders (folder
, get_folder_info_callback
, NULL
);
367 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
370 * Check if folder exists. If not, then see if
371 * we should create it, or just exit.
373 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
375 if (get_folder_info (folder
, msg
) && argfolder
) {
376 /* update current folder */
377 context_replace (pfolder
, argfolder
);
382 * Print out folder information
386 context_save (); /* save the context file */
392 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
395 struct msgs
*mp
= NULL
;
400 * if necessary, reallocate the space
401 * for folder information
403 if (total_folders
>= maxFolderInfo
) {
404 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
405 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
416 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
418 * create message structure and get folder info
420 if (!(mp
= folder_read (fold
, fpack
))) {
421 inform("unable to read folder %s, continuing...", fold
);
422 *crawl_children
= false;
426 /* set the current message */
427 if (msg
&& !sfold (mp
, msg
))
431 if (folder_pack (&mp
, fverb
) == -1) {
432 *crawl_children
= false; /* to please clang static analyzer */
435 seq_save (mp
); /* synchronize the sequences */
436 context_save (); /* save the context file */
439 /* record info for this folder */
440 if ((ftotal
> 0) || !fshort
) {
441 fi
[i
].nummsg
= mp
->nummsg
;
442 fi
[i
].curmsg
= mp
->curmsg
;
443 fi
[i
].lowmsg
= mp
->lowmsg
;
444 fi
[i
].hghmsg
= mp
->hghmsg
;
445 fi
[i
].others
= other_files (mp
);
448 folder_free (mp
); /* free folder/message structure */
451 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
452 && (fi
[i
].error
== 0));
457 get_folder_info_callback (char *fold
, void *baton
)
462 get_folder_info_body (fold
, NULL
, &crawl_children
);
464 return crawl_children
;
468 get_folder_info (char *fold
, char *msg
)
473 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
475 if (crawl_children
) {
476 crawl_folders (fold
, get_folder_info_callback
, NULL
);
483 * Print folder information
490 bool hasempty
= false;
492 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
493 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
494 int nummsgdigits
, lowmsgdigits
;
495 int hghmsgdigits
, curmsgdigits
;
496 char tmpname
[BUFSIZ
];
499 * compute a few values needed to for
500 * printing various fields
502 for (i
= 0; i
< total_folders
; i
++) {
503 /* length of folder name */
504 len
= strlen (fi
[i
].name
);
508 /* If folder has error, skip the rest */
512 /* calculate total number of messages */
513 total_msgs
+= fi
[i
].nummsg
;
515 /* maximum number of messages */
516 if (fi
[i
].nummsg
> maxnummsg
)
517 maxnummsg
= fi
[i
].nummsg
;
519 /* maximum low message */
520 if (fi
[i
].lowmsg
> maxlowmsg
)
521 maxlowmsg
= fi
[i
].lowmsg
;
523 /* maximum high message */
524 if (fi
[i
].hghmsg
> maxhghmsg
)
525 maxhghmsg
= fi
[i
].hghmsg
;
527 /* maximum current message */
528 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
529 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
530 fi
[i
].curmsg
> maxcurmsg
)
531 maxcurmsg
= fi
[i
].curmsg
;
533 /* check for empty folders */
534 if (fi
[i
].nummsg
== 0)
537 nummsgdigits
= num_digits (maxnummsg
);
538 lowmsgdigits
= num_digits (maxlowmsg
);
539 hghmsgdigits
= num_digits (maxhghmsg
);
540 curmsgdigits
= num_digits (maxcurmsg
);
542 if (hasempty
&& nummsgdigits
< 2)
548 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
549 printf ("%-*s %*s %-*s; %-*s %*s\n",
551 nummsgdigits
+ 13, "# MESSAGES",
552 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
553 curmsgdigits
+ 4, "CUR",
557 * Print folder information
559 if (all
|| fshort
|| ftotal
< 1) {
560 for (i
= 0; i
< total_folders
; i
++) {
566 /* Add `+' to end of name, if folder is current */
567 if (strcmp (folder
, fi
[i
].name
))
568 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
570 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
573 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
577 printf ("%-*s ", maxlen
+1, tmpname
);
579 curprinted
= false; /* remember if we print cur */
580 if (fi
[i
].nummsg
== 0) {
581 printf ("has %*s messages%*s",
583 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
585 printf ("has %*d message%1s (%*d-%*d)",
586 nummsgdigits
, fi
[i
].nummsg
,
587 PLURALS(fi
[i
].nummsg
),
588 lowmsgdigits
, fi
[i
].lowmsg
,
589 hghmsgdigits
, fi
[i
].hghmsg
);
590 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
592 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
597 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
603 * Print folder/message totals
605 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
608 printf ("TOTAL = %d message%s in %d folder%s.\n",
609 total_msgs
, PLURALS(total_msgs
),
610 total_folders
, PLURALS(total_folders
));
617 * Set the current message and synchronize sequences
621 sfold (struct msgs
*mp
, char *msg
)
623 /* parse the message range/sequence/name and set SELECTED */
624 if (!m_convert (mp
, msg
))
627 if (mp
->numsel
> 1) {
628 inform("only one message at a time!, continuing...");
631 seq_setprev (mp
); /* set the previous-sequence */
632 seq_setcur (mp
, mp
->lowsel
);/* set current message */
633 seq_save (mp
); /* synchronize message sequences */
634 context_save (); /* save the context file */
641 * Do the read only folders
645 readonly_folders (void)
651 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
652 atrlen
= strlen (atrcur
);
654 for (np
= m_defs
; np
; np
= np
->n_next
)
655 if (ssequal (atrcur
, np
->n_name
)
656 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
657 get_folder_info (np
->n_name
+ atrlen
, NULL
);