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/seq_setprev.h"
12 #include "sbr/seq_setcur.h"
13 #include "sbr/seq_save.h"
14 #include "sbr/smatch.h"
15 #include "sbr/ssequal.h"
16 #include "sbr/getcpy.h"
17 #include "sbr/m_convert.h"
18 #include "sbr/getfolder.h"
19 #include "sbr/folder_read.h"
20 #include "sbr/folder_pack.h"
21 #include "sbr/folder_free.h"
22 #include "sbr/context_save.h"
23 #include "sbr/context_replace.h"
24 #include "sbr/context_del.h"
25 #include "sbr/context_find.h"
26 #include "sbr/brkstring.h"
27 #include "sbr/ambigsw.h"
29 #include "sbr/print_version.h"
30 #include "sbr/print_help.h"
31 #include "sbr/error.h"
32 #include "h/crawl_folders.h"
35 #include "sbr/m_maildir.h"
37 #define FOLDER_SWITCHES \
39 X("noall", 0, NALLSW) \
40 X("create", 0, CREATSW) \
41 X("nocreate", 0, NCREATSW) \
42 X("fast", 0, FASTSW) \
43 X("nofast", 0, NFASTSW) \
44 X("header", 0, HDRSW) \
45 X("noheader", 0, NHDRSW) \
46 X("pack", 0, PACKSW) \
47 X("nopack", 0, NPACKSW) \
48 X("verbose", 0, VERBSW) \
49 X("noverbose", 0, NVERBSW) \
50 X("recurse", 0, RECURSW) \
51 X("norecurse", 0, NRECRSW) \
52 X("total", 0, TOTALSW) \
53 X("nototal", 0, NTOTLSW) \
54 X("list", 0, LISTSW) \
55 X("nolist", 0, NLISTSW) \
56 X("print", 0, PRNTSW) \
57 X("noprint", 0, NPRNTSW) \
58 X("push", 0, PUSHSW) \
60 X("version", 0, VERSIONSW) \
61 X("help", 0, HELPSW) \
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(FOLDER
);
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
71 static bool fshort
; /* output only folder names */
72 static int fcreat
= 0; /* should we ask to create new folders? */
73 static bool fpack
; /* are we packing the folder? */
74 static bool fverb
; /* print actions taken while packing folder */
75 static int fheader
= 0; /* should we output a header? */
76 static bool frecurse
; /* recurse through subfolders */
77 static int ftotal
= 0; /* should we output the totals? */
78 static bool all
; /* should we output all folders */
80 static int total_folders
= 0; /* total number of folders */
83 static char *stack
= "Folder-Stack";
84 static char folder
[BUFSIZ
];
87 * Structure to hold information about
88 * folders as we scan them.
96 int others
; /* others == 1 if other files in folder */
97 int error
; /* error == 1 for unreadable folder */
101 * Dynamically allocated space to hold
102 * all the folder information.
104 static struct FolderInfo
*fi
;
105 static int maxFolderInfo
;
110 static int get_folder_info (char *, char *);
111 static crawl_callback_t get_folder_info_callback
;
112 static void print_folders (void);
113 static int sfold (struct msgs
*, char *);
114 static void readonly_folders (void);
117 * Function for printing error message if folder does not exist with
121 nonexistent_folder (int status
)
124 die("folder %s does not exist", folder
);
129 main (int argc
, char **argv
)
131 bool printsw
= false;
135 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
136 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
138 if (nmh_init(argv
[0], true, true)) { return 1; }
141 * If program was invoked with name ending
142 * in `s', then add switch `-all'.
144 all
= has_suffix_c(argv
[0], 's');
146 arguments
= getarguments (invo_name
, argc
, argv
, 1);
149 while ((cp
= *argp
++)) {
151 switch (smatch (++cp
, switches
)) {
153 ambigsw (cp
, switches
);
156 die("-%s unknown", cp
);
159 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
161 print_help (buf
, switches
, 1);
164 print_version(invo_name
);
250 if (*cp
== '+' || *cp
== '@') {
252 die("only one folder at a time!");
253 argfolder
= pluspath (cp
);
256 die("only one (current) message at a time!");
261 if (!context_find ("path"))
262 free (path ("./", TFOLDER
));
263 nmhdir
= concat (m_maildir (""), "/", NULL
);
266 * If we aren't working with the folder stack
267 * (-push, -pop, -list) then the default is to print.
269 if (!pushsw
&& !popsw
&& !listsw
)
272 /* Pushing a folder onto the folder stack */
275 /* If no folder is given, the current folder and */
276 /* the top of the folder stack are swapped. */
277 if ((cp
= context_find (stack
))) {
279 ap
= brkstring (dp
, " ", "\n");
280 argfolder
= getcpy(*ap
++);
282 die("no other folder");
284 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
285 cp
= add (*ap
, add (" ", cp
));
287 context_replace (stack
, cp
); /* update folder stack */
289 /* update folder stack */
290 context_replace (stack
,
291 (cp
= context_find (stack
))
292 ? concat (getfolder (1), " ", cp
, NULL
)
293 : mh_xstrdup(getfolder(1)));
297 /* Popping a folder off of the folder stack */
300 die("sorry, no folders allowed with -pop");
301 if ((cp
= context_find (stack
))) {
303 ap
= brkstring (dp
, " ", "\n");
304 argfolder
= getcpy(*ap
++);
306 die("folder stack empty");
309 /* if there's anything left in the stack */
312 cp
= add (*ap
, add (" ", cp
));
313 context_replace (stack
, cp
); /* update folder stack */
315 context_del (stack
); /* delete folder stack entry from context */
319 if (pushsw
|| popsw
) {
320 cp
= m_maildir(argfolder
);
321 if (access (cp
, F_OK
) == NOTOK
)
322 adios (cp
, "unable to find folder");
323 context_replace (pfolder
, argfolder
); /* update current folder */
324 context_save (); /* save the context file */
328 /* Listing the folder stack */
330 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
331 if ((cp
= context_find (stack
))) {
333 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
343 /* Allocate initial space to record folder information */
344 maxFolderInfo
= CRAWL_NUMFOLDERS
;
345 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
350 /* change directory to base of nmh directory for crawl_folders */
351 if (chdir (nmhdir
) == NOTOK
)
352 adios (nmhdir
, "unable to change directory to");
353 if (all
|| ftotal
> 0) {
355 * If no folder is given, do them all
359 inform("no folder given for message %s, continuing...", msg
);
360 readonly_folders (); /* do any readonly folders */
361 cp
= context_find(pfolder
);
362 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
363 crawl_folders (".", get_folder_info_callback
, NULL
);
365 strncpy (folder
, argfolder
, sizeof(folder
));
366 if (get_folder_info (argfolder
, msg
)) {
367 context_replace (pfolder
, argfolder
);/* update current folder */
368 context_save (); /* save the context file */
371 * Since recurse wasn't done in get_folder_info(),
372 * we still need to list all level-1 sub-folders.
375 crawl_folders (folder
, get_folder_info_callback
, NULL
);
378 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
381 * Check if folder exists. If not, then see if
382 * we should create it, or just exit.
384 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
386 if (get_folder_info (folder
, msg
) && argfolder
) {
387 /* update current folder */
388 context_replace (pfolder
, argfolder
);
393 * Print out folder information
397 context_save (); /* save the context file */
403 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
406 struct msgs
*mp
= NULL
;
411 * if necessary, reallocate the space
412 * for folder information
414 if (total_folders
>= maxFolderInfo
) {
415 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
416 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
427 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
429 * create message structure and get folder info
431 if (!(mp
= folder_read (fold
, fpack
))) {
432 inform("unable to read folder %s, continuing...", fold
);
433 *crawl_children
= false;
437 /* set the current message */
438 if (msg
&& !sfold (mp
, msg
))
442 if (folder_pack (&mp
, fverb
) == -1) {
443 *crawl_children
= false; /* to please clang static analyzer */
446 seq_save (mp
); /* synchronize the sequences */
447 context_save (); /* save the context file */
450 /* record info for this folder */
451 if ((ftotal
> 0) || !fshort
) {
452 fi
[i
].nummsg
= mp
->nummsg
;
453 fi
[i
].curmsg
= mp
->curmsg
;
454 fi
[i
].lowmsg
= mp
->lowmsg
;
455 fi
[i
].hghmsg
= mp
->hghmsg
;
456 fi
[i
].others
= other_files (mp
);
459 folder_free (mp
); /* free folder/message structure */
462 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
463 && (fi
[i
].error
== 0));
468 get_folder_info_callback (char *fold
, void *baton
)
473 get_folder_info_body (fold
, NULL
, &crawl_children
);
475 return crawl_children
;
479 get_folder_info (char *fold
, char *msg
)
484 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
486 if (crawl_children
) {
487 crawl_folders (fold
, get_folder_info_callback
, NULL
);
494 * Print folder information
501 bool hasempty
= false;
503 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
504 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
505 int nummsgdigits
, lowmsgdigits
;
506 int hghmsgdigits
, curmsgdigits
;
507 char tmpname
[BUFSIZ
];
510 * compute a few values needed to for
511 * printing various fields
513 for (i
= 0; i
< total_folders
; i
++) {
514 /* length of folder name */
515 len
= strlen (fi
[i
].name
);
519 /* If folder has error, skip the rest */
523 /* calculate total number of messages */
524 total_msgs
+= fi
[i
].nummsg
;
526 /* maximum number of messages */
527 if (fi
[i
].nummsg
> maxnummsg
)
528 maxnummsg
= fi
[i
].nummsg
;
530 /* maximum low message */
531 if (fi
[i
].lowmsg
> maxlowmsg
)
532 maxlowmsg
= fi
[i
].lowmsg
;
534 /* maximum high message */
535 if (fi
[i
].hghmsg
> maxhghmsg
)
536 maxhghmsg
= fi
[i
].hghmsg
;
538 /* maximum current message */
539 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
540 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
541 fi
[i
].curmsg
> maxcurmsg
)
542 maxcurmsg
= fi
[i
].curmsg
;
544 /* check for empty folders */
545 if (fi
[i
].nummsg
== 0)
548 nummsgdigits
= num_digits (maxnummsg
);
549 lowmsgdigits
= num_digits (maxlowmsg
);
550 hghmsgdigits
= num_digits (maxhghmsg
);
551 curmsgdigits
= num_digits (maxcurmsg
);
553 if (hasempty
&& nummsgdigits
< 2)
559 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
560 printf ("%-*s %*s %-*s; %-*s %*s\n",
562 nummsgdigits
+ 13, "# MESSAGES",
563 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
564 curmsgdigits
+ 4, "CUR",
568 * Print folder information
570 if (all
|| fshort
|| ftotal
< 1) {
571 for (i
= 0; i
< total_folders
; i
++) {
577 /* Add `+' to end of name, if folder is current */
578 if (strcmp (folder
, fi
[i
].name
))
579 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
581 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
584 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
588 printf ("%-*s ", maxlen
+1, tmpname
);
590 curprinted
= false; /* remember if we print cur */
591 if (fi
[i
].nummsg
== 0) {
592 printf ("has %*s messages%*s",
594 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
596 printf ("has %*d message%1s (%*d-%*d)",
597 nummsgdigits
, fi
[i
].nummsg
,
598 PLURALS(fi
[i
].nummsg
),
599 lowmsgdigits
, fi
[i
].lowmsg
,
600 hghmsgdigits
, fi
[i
].hghmsg
);
601 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
603 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
608 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
614 * Print folder/message totals
616 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
619 printf ("TOTAL = %d message%s in %d folder%s.\n",
620 total_msgs
, PLURALS(total_msgs
),
621 total_folders
, PLURALS(total_folders
));
628 * Set the current message and synchronize sequences
632 sfold (struct msgs
*mp
, char *msg
)
634 /* parse the message range/sequence/name and set SELECTED */
635 if (!m_convert (mp
, msg
))
638 if (mp
->numsel
> 1) {
639 inform("only one message at a time!, continuing...");
642 seq_setprev (mp
); /* set the previous-sequence */
643 seq_setcur (mp
, mp
->lowsel
);/* set current message */
644 seq_save (mp
); /* synchronize message sequences */
645 context_save (); /* save the context file */
652 * Do the read only folders
656 readonly_folders (void)
662 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
663 atrlen
= strlen (atrcur
);
665 for (np
= m_defs
; np
; np
= np
->n_next
)
666 if (ssequal (atrcur
, np
->n_name
)
667 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
668 get_folder_info (np
->n_name
+ atrlen
, NULL
);