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/concat.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_setcur.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/smatch.h"
16 #include "sbr/ssequal.h"
17 #include "sbr/getcpy.h"
18 #include "sbr/m_convert.h"
19 #include "sbr/getfolder.h"
20 #include "sbr/folder_read.h"
21 #include "sbr/folder_pack.h"
22 #include "sbr/folder_free.h"
23 #include "sbr/context_save.h"
24 #include "sbr/context_replace.h"
25 #include "sbr/context_del.h"
26 #include "sbr/context_find.h"
27 #include "sbr/brkstring.h"
28 #include "sbr/ambigsw.h"
30 #include "sbr/print_version.h"
31 #include "sbr/print_help.h"
32 #include "sbr/error.h"
33 #include "h/crawl_folders.h"
36 #include "sbr/m_maildir.h"
38 #define FOLDER_SWITCHES \
40 X("noall", 0, NALLSW) \
41 X("create", 0, CREATSW) \
42 X("nocreate", 0, NCREATSW) \
43 X("fast", 0, FASTSW) \
44 X("nofast", 0, NFASTSW) \
45 X("header", 0, HDRSW) \
46 X("noheader", 0, NHDRSW) \
47 X("pack", 0, PACKSW) \
48 X("nopack", 0, NPACKSW) \
49 X("verbose", 0, VERBSW) \
50 X("noverbose", 0, NVERBSW) \
51 X("recurse", 0, RECURSW) \
52 X("norecurse", 0, NRECRSW) \
53 X("total", 0, TOTALSW) \
54 X("nototal", 0, NTOTLSW) \
55 X("list", 0, LISTSW) \
56 X("nolist", 0, NLISTSW) \
57 X("print", 0, PRNTSW) \
58 X("noprint", 0, NPRNTSW) \
59 X("push", 0, PUSHSW) \
61 X("version", 0, VERSIONSW) \
62 X("help", 0, HELPSW) \
64 #define X(sw, minchars, id) id,
65 DEFINE_SWITCH_ENUM(FOLDER
);
68 #define X(sw, minchars, id) { sw, minchars, id },
69 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
72 static bool fshort
; /* output only folder names */
73 static int fcreat
= 0; /* should we ask to create new folders? */
74 static bool fpack
; /* are we packing the folder? */
75 static bool fverb
; /* print actions taken while packing folder */
76 static int fheader
= 0; /* should we output a header? */
77 static bool frecurse
; /* recurse through subfolders */
78 static int ftotal
= 0; /* should we output the totals? */
79 static bool all
; /* should we output all folders */
81 static int total_folders
= 0; /* total number of folders */
84 static char *stack
= "Folder-Stack";
85 static char folder
[BUFSIZ
];
88 * Structure to hold information about
89 * folders as we scan them.
97 int others
; /* others == 1 if other files in folder */
98 int error
; /* error == 1 for unreadable folder */
102 * Dynamically allocated space to hold
103 * all the folder information.
105 static struct FolderInfo
*fi
;
106 static int maxFolderInfo
;
111 static int get_folder_info (char *, char *);
112 static crawl_callback_t get_folder_info_callback
;
113 static void print_folders (void);
114 static int sfold (struct msgs
*, char *);
115 static void readonly_folders (void);
118 * Function for printing error message if folder does not exist with
122 nonexistent_folder (int status
)
125 die("folder %s does not exist", folder
);
130 main (int argc
, char **argv
)
132 bool printsw
= false;
136 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
137 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
139 if (nmh_init(argv
[0], true, true)) { return 1; }
142 * If program was invoked with name ending
143 * in `s', then add switch `-all'.
145 all
= has_suffix_c(argv
[0], 's');
147 arguments
= getarguments (invo_name
, argc
, argv
, 1);
150 while ((cp
= *argp
++)) {
152 switch (smatch (++cp
, switches
)) {
154 ambigsw (cp
, switches
);
157 die("-%s unknown", cp
);
160 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
162 print_help (buf
, switches
, 1);
165 print_version(invo_name
);
251 if (*cp
== '+' || *cp
== '@') {
253 die("only one folder at a time!");
254 argfolder
= pluspath (cp
);
257 die("only one (current) message at a time!");
262 if (!context_find ("path"))
263 free (path ("./", TFOLDER
));
264 nmhdir
= concat (m_maildir (""), "/", NULL
);
267 * If we aren't working with the folder stack
268 * (-push, -pop, -list) then the default is to print.
270 if (!pushsw
&& !popsw
&& !listsw
)
273 /* Pushing a folder onto the folder stack */
276 /* If no folder is given, the current folder and */
277 /* the top of the folder stack are swapped. */
278 if ((cp
= context_find (stack
))) {
280 ap
= brkstring (dp
, " ", "\n");
281 argfolder
= getcpy(*ap
++);
283 die("no other folder");
285 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
286 cp
= add (*ap
, add (" ", cp
));
288 context_replace (stack
, cp
); /* update folder stack */
290 /* update folder stack */
291 context_replace (stack
,
292 (cp
= context_find (stack
))
293 ? concat (getfolder (1), " ", cp
, NULL
)
294 : mh_xstrdup(getfolder(1)));
298 /* Popping a folder off of the folder stack */
301 die("sorry, no folders allowed with -pop");
302 if ((cp
= context_find (stack
))) {
304 ap
= brkstring (dp
, " ", "\n");
305 argfolder
= getcpy(*ap
++);
307 die("folder stack empty");
310 /* if there's anything left in the stack */
313 cp
= add (*ap
, add (" ", cp
));
314 context_replace (stack
, cp
); /* update folder stack */
316 context_del (stack
); /* delete folder stack entry from context */
320 if (pushsw
|| popsw
) {
321 cp
= m_maildir(argfolder
);
322 if (access (cp
, F_OK
) == NOTOK
)
323 adios (cp
, "unable to find folder");
324 context_replace (pfolder
, argfolder
); /* update current folder */
325 context_save (); /* save the context file */
329 /* Listing the folder stack */
331 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
332 if ((cp
= context_find (stack
))) {
334 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
344 /* Allocate initial space to record folder information */
345 maxFolderInfo
= CRAWL_NUMFOLDERS
;
346 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
351 /* change directory to base of nmh directory for crawl_folders */
352 if (chdir (nmhdir
) == NOTOK
)
353 adios (nmhdir
, "unable to change directory to");
354 if (all
|| ftotal
> 0) {
356 * If no folder is given, do them all
360 inform("no folder given for message %s, continuing...", msg
);
361 readonly_folders (); /* do any readonly folders */
362 cp
= context_find(pfolder
);
363 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
364 crawl_folders (".", get_folder_info_callback
, NULL
);
366 strncpy (folder
, argfolder
, sizeof(folder
));
367 if (get_folder_info (argfolder
, msg
)) {
368 context_replace (pfolder
, argfolder
);/* update current folder */
369 context_save (); /* save the context file */
372 * Since recurse wasn't done in get_folder_info(),
373 * we still need to list all level-1 sub-folders.
376 crawl_folders (folder
, get_folder_info_callback
, NULL
);
379 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
382 * Check if folder exists. If not, then see if
383 * we should create it, or just exit.
385 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
387 if (get_folder_info (folder
, msg
) && argfolder
) {
388 /* update current folder */
389 context_replace (pfolder
, argfolder
);
394 * Print out folder information
398 context_save (); /* save the context file */
404 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
407 struct msgs
*mp
= NULL
;
412 * if necessary, reallocate the space
413 * for folder information
415 if (total_folders
>= maxFolderInfo
) {
416 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
417 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
428 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
430 * create message structure and get folder info
432 if (!(mp
= folder_read (fold
, fpack
))) {
433 inform("unable to read folder %s, continuing...", fold
);
434 *crawl_children
= false;
438 /* set the current message */
439 if (msg
&& !sfold (mp
, msg
))
443 if (folder_pack (&mp
, fverb
) == -1) {
444 *crawl_children
= false; /* to please clang static analyzer */
447 seq_save (mp
); /* synchronize the sequences */
448 context_save (); /* save the context file */
451 /* record info for this folder */
452 if ((ftotal
> 0) || !fshort
) {
453 fi
[i
].nummsg
= mp
->nummsg
;
454 fi
[i
].curmsg
= mp
->curmsg
;
455 fi
[i
].lowmsg
= mp
->lowmsg
;
456 fi
[i
].hghmsg
= mp
->hghmsg
;
457 fi
[i
].others
= other_files (mp
);
460 folder_free (mp
); /* free folder/message structure */
463 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
464 && (fi
[i
].error
== 0));
469 get_folder_info_callback (char *fold
, void *baton
)
474 get_folder_info_body (fold
, NULL
, &crawl_children
);
476 return crawl_children
;
480 get_folder_info (char *fold
, char *msg
)
485 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
487 if (crawl_children
) {
488 crawl_folders (fold
, get_folder_info_callback
, NULL
);
495 * Print folder information
502 bool hasempty
= false;
504 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
505 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
506 int nummsgdigits
, lowmsgdigits
;
507 int hghmsgdigits
, curmsgdigits
;
508 char tmpname
[BUFSIZ
];
511 * compute a few values needed to for
512 * printing various fields
514 for (i
= 0; i
< total_folders
; i
++) {
515 /* length of folder name */
516 len
= strlen (fi
[i
].name
);
520 /* If folder has error, skip the rest */
524 /* calculate total number of messages */
525 total_msgs
+= fi
[i
].nummsg
;
527 /* maximum number of messages */
528 if (fi
[i
].nummsg
> maxnummsg
)
529 maxnummsg
= fi
[i
].nummsg
;
531 /* maximum low message */
532 if (fi
[i
].lowmsg
> maxlowmsg
)
533 maxlowmsg
= fi
[i
].lowmsg
;
535 /* maximum high message */
536 if (fi
[i
].hghmsg
> maxhghmsg
)
537 maxhghmsg
= fi
[i
].hghmsg
;
539 /* maximum current message */
540 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
541 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
542 fi
[i
].curmsg
> maxcurmsg
)
543 maxcurmsg
= fi
[i
].curmsg
;
545 /* check for empty folders */
546 if (fi
[i
].nummsg
== 0)
549 nummsgdigits
= num_digits (maxnummsg
);
550 lowmsgdigits
= num_digits (maxlowmsg
);
551 hghmsgdigits
= num_digits (maxhghmsg
);
552 curmsgdigits
= num_digits (maxcurmsg
);
554 if (hasempty
&& nummsgdigits
< 2)
560 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
561 printf ("%-*s %*s %-*s; %-*s %*s\n",
563 nummsgdigits
+ 13, "# MESSAGES",
564 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
565 curmsgdigits
+ 4, "CUR",
569 * Print folder information
571 if (all
|| fshort
|| ftotal
< 1) {
572 for (i
= 0; i
< total_folders
; i
++) {
578 /* Add `+' to end of name, if folder is current */
579 if (strcmp (folder
, fi
[i
].name
))
580 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
582 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
585 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
589 printf ("%-*s ", maxlen
+1, tmpname
);
591 curprinted
= false; /* remember if we print cur */
592 if (fi
[i
].nummsg
== 0) {
593 printf ("has %*s messages%*s",
595 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
597 printf ("has %*d message%1s (%*d-%*d)",
598 nummsgdigits
, fi
[i
].nummsg
,
599 PLURALS(fi
[i
].nummsg
),
600 lowmsgdigits
, fi
[i
].lowmsg
,
601 hghmsgdigits
, fi
[i
].hghmsg
);
602 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
604 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
609 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
615 * Print folder/message totals
617 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
620 printf ("TOTAL = %d message%s in %d folder%s.\n",
621 total_msgs
, PLURALS(total_msgs
),
622 total_folders
, PLURALS(total_folders
));
629 * Set the current message and synchronize sequences
633 sfold (struct msgs
*mp
, char *msg
)
635 /* parse the message range/sequence/name and set SELECTED */
636 if (!m_convert (mp
, msg
))
639 if (mp
->numsel
> 1) {
640 inform("only one message at a time!, continuing...");
643 seq_setprev (mp
); /* set the previous-sequence */
644 seq_setcur (mp
, mp
->lowsel
);/* set current message */
645 seq_save (mp
); /* synchronize message sequences */
646 context_save (); /* save the context file */
653 * Do the read only folders
657 readonly_folders (void)
663 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
664 atrlen
= strlen (atrcur
);
666 for (np
= m_defs
; np
; np
= np
->n_next
)
667 if (ssequal (atrcur
, np
->n_name
)
668 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
669 get_folder_info (np
->n_name
+ atrlen
, NULL
);