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/getarguments.h"
12 #include "sbr/concat.h"
13 #include "sbr/seq_setprev.h"
14 #include "sbr/seq_setcur.h"
15 #include "sbr/seq_save.h"
16 #include "sbr/smatch.h"
17 #include "sbr/ssequal.h"
18 #include "sbr/getcpy.h"
19 #include "sbr/m_convert.h"
20 #include "sbr/getfolder.h"
21 #include "sbr/folder_read.h"
22 #include "sbr/folder_pack.h"
23 #include "sbr/folder_free.h"
24 #include "sbr/context_save.h"
25 #include "sbr/context_replace.h"
26 #include "sbr/context_del.h"
27 #include "sbr/context_find.h"
28 #include "sbr/brkstring.h"
29 #include "sbr/ambigsw.h"
31 #include "sbr/print_version.h"
32 #include "sbr/print_help.h"
33 #include "sbr/error.h"
34 #include "h/crawl_folders.h"
37 #include "sbr/m_maildir.h"
39 #define FOLDER_SWITCHES \
41 X("noall", 0, NALLSW) \
42 X("create", 0, CREATSW) \
43 X("nocreate", 0, NCREATSW) \
44 X("fast", 0, FASTSW) \
45 X("nofast", 0, NFASTSW) \
46 X("header", 0, HDRSW) \
47 X("noheader", 0, NHDRSW) \
48 X("pack", 0, PACKSW) \
49 X("nopack", 0, NPACKSW) \
50 X("verbose", 0, VERBSW) \
51 X("noverbose", 0, NVERBSW) \
52 X("recurse", 0, RECURSW) \
53 X("norecurse", 0, NRECRSW) \
54 X("total", 0, TOTALSW) \
55 X("nototal", 0, NTOTLSW) \
56 X("list", 0, LISTSW) \
57 X("nolist", 0, NLISTSW) \
58 X("print", 0, PRNTSW) \
59 X("noprint", 0, NPRNTSW) \
60 X("push", 0, PUSHSW) \
62 X("version", 0, VERSIONSW) \
63 X("help", 0, HELPSW) \
65 #define X(sw, minchars, id) id,
66 DEFINE_SWITCH_ENUM(FOLDER
);
69 #define X(sw, minchars, id) { sw, minchars, id },
70 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
73 static bool fshort
; /* output only folder names */
74 static int fcreat
= 0; /* should we ask to create new folders? */
75 static bool fpack
; /* are we packing the folder? */
76 static bool fverb
; /* print actions taken while packing folder */
77 static int fheader
= 0; /* should we output a header? */
78 static bool frecurse
; /* recurse through subfolders */
79 static int ftotal
= 0; /* should we output the totals? */
80 static bool all
; /* should we output all folders */
82 static int total_folders
= 0; /* total number of folders */
85 static char *stack
= "Folder-Stack";
86 static char folder
[BUFSIZ
];
89 * Structure to hold information about
90 * folders as we scan them.
98 int others
; /* others == 1 if other files in folder */
99 int error
; /* error == 1 for unreadable folder */
103 * Dynamically allocated space to hold
104 * all the folder information.
106 static struct FolderInfo
*fi
;
107 static int maxFolderInfo
;
112 static int get_folder_info (char *, char *);
113 static crawl_callback_t get_folder_info_callback
;
114 static void print_folders (void);
115 static int sfold (struct msgs
*, char *);
116 static void readonly_folders (void);
119 * Function for printing error message if folder does not exist with
123 nonexistent_folder (int status
)
126 die("folder %s does not exist", folder
);
131 main (int argc
, char **argv
)
133 bool printsw
= false;
137 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
138 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
140 if (nmh_init(argv
[0], true, true)) { return 1; }
143 * If program was invoked with name ending
144 * in `s', then add switch `-all'.
146 all
= has_suffix_c(argv
[0], 's');
148 arguments
= getarguments (invo_name
, argc
, argv
, 1);
151 while ((cp
= *argp
++)) {
153 switch (smatch (++cp
, switches
)) {
155 ambigsw (cp
, switches
);
158 die("-%s unknown", cp
);
161 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
163 print_help (buf
, switches
, 1);
166 print_version(invo_name
);
252 if (*cp
== '+' || *cp
== '@') {
254 die("only one folder at a time!");
255 argfolder
= pluspath (cp
);
258 die("only one (current) message at a time!");
263 if (!context_find ("path"))
264 free (path ("./", TFOLDER
));
265 nmhdir
= concat (m_maildir (""), "/", NULL
);
268 * If we aren't working with the folder stack
269 * (-push, -pop, -list) then the default is to print.
271 if (!pushsw
&& !popsw
&& !listsw
)
274 /* Pushing a folder onto the folder stack */
277 /* If no folder is given, the current folder and */
278 /* the top of the folder stack are swapped. */
279 if ((cp
= context_find (stack
))) {
281 ap
= brkstring (dp
, " ", "\n");
282 argfolder
= getcpy(*ap
++);
284 die("no other folder");
286 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
287 cp
= add (*ap
, add (" ", cp
));
289 context_replace (stack
, cp
); /* update folder stack */
291 /* update folder stack */
292 context_replace (stack
,
293 (cp
= context_find (stack
))
294 ? concat (getfolder (1), " ", cp
, NULL
)
295 : mh_xstrdup(getfolder(1)));
299 /* Popping a folder off of the folder stack */
302 die("sorry, no folders allowed with -pop");
303 if ((cp
= context_find (stack
))) {
305 ap
= brkstring (dp
, " ", "\n");
306 argfolder
= getcpy(*ap
++);
308 die("folder stack empty");
311 /* if there's anything left in the stack */
314 cp
= add (*ap
, add (" ", cp
));
315 context_replace (stack
, cp
); /* update folder stack */
317 context_del (stack
); /* delete folder stack entry from context */
321 if (pushsw
|| popsw
) {
322 cp
= m_maildir(argfolder
);
323 if (access (cp
, F_OK
) == NOTOK
)
324 adios (cp
, "unable to find folder");
325 context_replace (pfolder
, argfolder
); /* update current folder */
326 context_save (); /* save the context file */
330 /* Listing the folder stack */
332 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
333 if ((cp
= context_find (stack
))) {
335 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
345 /* Allocate initial space to record folder information */
346 maxFolderInfo
= CRAWL_NUMFOLDERS
;
347 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
352 /* change directory to base of nmh directory for crawl_folders */
353 if (chdir (nmhdir
) == NOTOK
)
354 adios (nmhdir
, "unable to change directory to");
355 if (all
|| ftotal
> 0) {
357 * If no folder is given, do them all
361 inform("no folder given for message %s, continuing...", msg
);
362 readonly_folders (); /* do any readonly folders */
363 cp
= context_find(pfolder
);
364 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
365 crawl_folders (".", get_folder_info_callback
, NULL
);
367 strncpy (folder
, argfolder
, sizeof(folder
));
368 if (get_folder_info (argfolder
, msg
)) {
369 context_replace (pfolder
, argfolder
);/* update current folder */
370 context_save (); /* save the context file */
373 * Since recurse wasn't done in get_folder_info(),
374 * we still need to list all level-1 sub-folders.
377 crawl_folders (folder
, get_folder_info_callback
, NULL
);
380 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
383 * Check if folder exists. If not, then see if
384 * we should create it, or just exit.
386 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
388 if (get_folder_info (folder
, msg
) && argfolder
) {
389 /* update current folder */
390 context_replace (pfolder
, argfolder
);
395 * Print out folder information
399 context_save (); /* save the context file */
405 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
408 struct msgs
*mp
= NULL
;
413 * if necessary, reallocate the space
414 * for folder information
416 if (total_folders
>= maxFolderInfo
) {
417 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
418 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
429 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
431 * create message structure and get folder info
433 if (!(mp
= folder_read (fold
, fpack
))) {
434 inform("unable to read folder %s, continuing...", fold
);
435 *crawl_children
= false;
439 /* set the current message */
440 if (msg
&& !sfold (mp
, msg
))
444 if (folder_pack (&mp
, fverb
) == -1) {
445 *crawl_children
= false; /* to please clang static analyzer */
448 seq_save (mp
); /* synchronize the sequences */
449 context_save (); /* save the context file */
452 /* record info for this folder */
453 if ((ftotal
> 0) || !fshort
) {
454 fi
[i
].nummsg
= mp
->nummsg
;
455 fi
[i
].curmsg
= mp
->curmsg
;
456 fi
[i
].lowmsg
= mp
->lowmsg
;
457 fi
[i
].hghmsg
= mp
->hghmsg
;
458 fi
[i
].others
= other_files (mp
);
461 folder_free (mp
); /* free folder/message structure */
464 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
465 && (fi
[i
].error
== 0));
470 get_folder_info_callback (char *fold
, void *baton
)
475 get_folder_info_body (fold
, NULL
, &crawl_children
);
477 return crawl_children
;
481 get_folder_info (char *fold
, char *msg
)
486 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
488 if (crawl_children
) {
489 crawl_folders (fold
, get_folder_info_callback
, NULL
);
496 * Print folder information
503 bool hasempty
= false;
505 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
506 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
507 int nummsgdigits
, lowmsgdigits
;
508 int hghmsgdigits
, curmsgdigits
;
509 char tmpname
[BUFSIZ
];
512 * compute a few values needed to for
513 * printing various fields
515 for (i
= 0; i
< total_folders
; i
++) {
516 /* length of folder name */
517 len
= strlen (fi
[i
].name
);
521 /* If folder has error, skip the rest */
525 /* calculate total number of messages */
526 total_msgs
+= fi
[i
].nummsg
;
528 /* maximum number of messages */
529 if (fi
[i
].nummsg
> maxnummsg
)
530 maxnummsg
= fi
[i
].nummsg
;
532 /* maximum low message */
533 if (fi
[i
].lowmsg
> maxlowmsg
)
534 maxlowmsg
= fi
[i
].lowmsg
;
536 /* maximum high message */
537 if (fi
[i
].hghmsg
> maxhghmsg
)
538 maxhghmsg
= fi
[i
].hghmsg
;
540 /* maximum current message */
541 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
542 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
543 fi
[i
].curmsg
> maxcurmsg
)
544 maxcurmsg
= fi
[i
].curmsg
;
546 /* check for empty folders */
547 if (fi
[i
].nummsg
== 0)
550 nummsgdigits
= num_digits (maxnummsg
);
551 lowmsgdigits
= num_digits (maxlowmsg
);
552 hghmsgdigits
= num_digits (maxhghmsg
);
553 curmsgdigits
= num_digits (maxcurmsg
);
555 if (hasempty
&& nummsgdigits
< 2)
561 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
562 printf ("%-*s %*s %-*s; %-*s %*s\n",
564 nummsgdigits
+ 13, "# MESSAGES",
565 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
566 curmsgdigits
+ 4, "CUR",
570 * Print folder information
572 if (all
|| fshort
|| ftotal
< 1) {
573 for (i
= 0; i
< total_folders
; i
++) {
579 /* Add `+' to end of name, if folder is current */
580 if (strcmp (folder
, fi
[i
].name
))
581 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
583 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
586 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
590 printf ("%-*s ", maxlen
+1, tmpname
);
592 curprinted
= false; /* remember if we print cur */
593 if (fi
[i
].nummsg
== 0) {
594 printf ("has %*s messages%*s",
596 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
598 printf ("has %*d message%1s (%*d-%*d)",
599 nummsgdigits
, fi
[i
].nummsg
,
600 PLURALS(fi
[i
].nummsg
),
601 lowmsgdigits
, fi
[i
].lowmsg
,
602 hghmsgdigits
, fi
[i
].hghmsg
);
603 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
605 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
610 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
616 * Print folder/message totals
618 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
621 printf ("TOTAL = %d message%s in %d folder%s.\n",
622 total_msgs
, PLURALS(total_msgs
),
623 total_folders
, PLURALS(total_folders
));
630 * Set the current message and synchronize sequences
634 sfold (struct msgs
*mp
, char *msg
)
636 /* parse the message range/sequence/name and set SELECTED */
637 if (!m_convert (mp
, msg
))
640 if (mp
->numsel
> 1) {
641 inform("only one message at a time!, continuing...");
644 seq_setprev (mp
); /* set the previous-sequence */
645 seq_setcur (mp
, mp
->lowsel
);/* set current message */
646 seq_save (mp
); /* synchronize message sequences */
647 context_save (); /* save the context file */
654 * Do the read only folders
658 readonly_folders (void)
664 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
665 atrlen
= strlen (atrcur
);
667 for (np
= m_defs
; np
; np
= np
->n_next
)
668 if (ssequal (atrcur
, np
->n_name
)
669 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
670 get_folder_info (np
->n_name
+ atrlen
, NULL
);