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_replace.h"
12 #include "sbr/context_del.h"
13 #include "sbr/context_find.h"
14 #include "sbr/brkstring.h"
15 #include "sbr/ambigsw.h"
17 #include "sbr/print_version.h"
18 #include "sbr/print_help.h"
19 #include "sbr/error.h"
20 #include "h/crawl_folders.h"
23 #include "sbr/m_maildir.h"
25 #define FOLDER_SWITCHES \
27 X("noall", 0, NALLSW) \
28 X("create", 0, CREATSW) \
29 X("nocreate", 0, NCREATSW) \
30 X("fast", 0, FASTSW) \
31 X("nofast", 0, NFASTSW) \
32 X("header", 0, HDRSW) \
33 X("noheader", 0, NHDRSW) \
34 X("pack", 0, PACKSW) \
35 X("nopack", 0, NPACKSW) \
36 X("verbose", 0, VERBSW) \
37 X("noverbose", 0, NVERBSW) \
38 X("recurse", 0, RECURSW) \
39 X("norecurse", 0, NRECRSW) \
40 X("total", 0, TOTALSW) \
41 X("nototal", 0, NTOTLSW) \
42 X("list", 0, LISTSW) \
43 X("nolist", 0, NLISTSW) \
44 X("print", 0, PRNTSW) \
45 X("noprint", 0, NPRNTSW) \
46 X("push", 0, PUSHSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(FOLDER
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
59 static bool fshort
; /* output only folder names */
60 static int fcreat
= 0; /* should we ask to create new folders? */
61 static bool fpack
; /* are we packing the folder? */
62 static bool fverb
; /* print actions taken while packing folder */
63 static int fheader
= 0; /* should we output a header? */
64 static bool frecurse
; /* recurse through subfolders */
65 static int ftotal
= 0; /* should we output the totals? */
66 static bool all
; /* should we output all folders */
68 static int total_folders
= 0; /* total number of folders */
71 static char *stack
= "Folder-Stack";
72 static char folder
[BUFSIZ
];
75 * Structure to hold information about
76 * folders as we scan them.
84 int others
; /* others == 1 if other files in folder */
85 int error
; /* error == 1 for unreadable folder */
89 * Dynamically allocated space to hold
90 * all the folder information.
92 static struct FolderInfo
*fi
;
93 static int maxFolderInfo
;
98 static int get_folder_info (char *, char *);
99 static crawl_callback_t get_folder_info_callback
;
100 static void print_folders (void);
101 static int sfold (struct msgs
*, char *);
102 static void readonly_folders (void);
105 * Function for printing error message if folder does not exist with
109 nonexistent_folder (int status
)
112 die("folder %s does not exist", folder
);
117 main (int argc
, char **argv
)
119 bool printsw
= false;
123 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
124 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
126 if (nmh_init(argv
[0], true, true)) { return 1; }
129 * If program was invoked with name ending
130 * in `s', then add switch `-all'.
132 all
= has_suffix_c(argv
[0], 's');
134 arguments
= getarguments (invo_name
, argc
, argv
, 1);
137 while ((cp
= *argp
++)) {
139 switch (smatch (++cp
, switches
)) {
141 ambigsw (cp
, switches
);
144 die("-%s unknown", cp
);
147 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
149 print_help (buf
, switches
, 1);
152 print_version(invo_name
);
238 if (*cp
== '+' || *cp
== '@') {
240 die("only one folder at a time!");
241 argfolder
= pluspath (cp
);
244 die("only one (current) message at a time!");
249 if (!context_find ("path"))
250 free (path ("./", TFOLDER
));
251 nmhdir
= concat (m_maildir (""), "/", NULL
);
254 * If we aren't working with the folder stack
255 * (-push, -pop, -list) then the default is to print.
257 if (!pushsw
&& !popsw
&& !listsw
)
260 /* Pushing a folder onto the folder stack */
263 /* If no folder is given, the current folder and */
264 /* the top of the folder stack are swapped. */
265 if ((cp
= context_find (stack
))) {
267 ap
= brkstring (dp
, " ", "\n");
268 argfolder
= getcpy(*ap
++);
270 die("no other folder");
272 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
273 cp
= add (*ap
, add (" ", cp
));
275 context_replace (stack
, cp
); /* update folder stack */
277 /* update folder stack */
278 context_replace (stack
,
279 (cp
= context_find (stack
))
280 ? concat (getfolder (1), " ", cp
, NULL
)
281 : mh_xstrdup(getfolder(1)));
285 /* Popping a folder off of the folder stack */
288 die("sorry, no folders allowed with -pop");
289 if ((cp
= context_find (stack
))) {
291 ap
= brkstring (dp
, " ", "\n");
292 argfolder
= getcpy(*ap
++);
294 die("folder stack empty");
297 /* if there's anything left in the stack */
300 cp
= add (*ap
, add (" ", cp
));
301 context_replace (stack
, cp
); /* update folder stack */
303 context_del (stack
); /* delete folder stack entry from context */
307 if (pushsw
|| popsw
) {
308 cp
= m_maildir(argfolder
);
309 if (access (cp
, F_OK
) == NOTOK
)
310 adios (cp
, "unable to find folder");
311 context_replace (pfolder
, argfolder
); /* update current folder */
312 context_save (); /* save the context file */
316 /* Listing the folder stack */
318 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
319 if ((cp
= context_find (stack
))) {
321 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
331 /* Allocate initial space to record folder information */
332 maxFolderInfo
= CRAWL_NUMFOLDERS
;
333 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
338 /* change directory to base of nmh directory for crawl_folders */
339 if (chdir (nmhdir
) == NOTOK
)
340 adios (nmhdir
, "unable to change directory to");
341 if (all
|| ftotal
> 0) {
343 * If no folder is given, do them all
347 inform("no folder given for message %s, continuing...", msg
);
348 readonly_folders (); /* do any readonly folders */
349 cp
= context_find(pfolder
);
350 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
351 crawl_folders (".", get_folder_info_callback
, NULL
);
353 strncpy (folder
, argfolder
, sizeof(folder
));
354 if (get_folder_info (argfolder
, msg
)) {
355 context_replace (pfolder
, argfolder
);/* update current folder */
356 context_save (); /* save the context file */
359 * Since recurse wasn't done in get_folder_info(),
360 * we still need to list all level-1 sub-folders.
363 crawl_folders (folder
, get_folder_info_callback
, NULL
);
366 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
369 * Check if folder exists. If not, then see if
370 * we should create it, or just exit.
372 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
374 if (get_folder_info (folder
, msg
) && argfolder
) {
375 /* update current folder */
376 context_replace (pfolder
, argfolder
);
381 * Print out folder information
385 context_save (); /* save the context file */
391 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
394 struct msgs
*mp
= NULL
;
399 * if necessary, reallocate the space
400 * for folder information
402 if (total_folders
>= maxFolderInfo
) {
403 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
404 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
415 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
417 * create message structure and get folder info
419 if (!(mp
= folder_read (fold
, fpack
))) {
420 inform("unable to read folder %s, continuing...", fold
);
421 *crawl_children
= false;
425 /* set the current message */
426 if (msg
&& !sfold (mp
, msg
))
430 if (folder_pack (&mp
, fverb
) == -1) {
431 *crawl_children
= false; /* to please clang static analyzer */
434 seq_save (mp
); /* synchronize the sequences */
435 context_save (); /* save the context file */
438 /* record info for this folder */
439 if ((ftotal
> 0) || !fshort
) {
440 fi
[i
].nummsg
= mp
->nummsg
;
441 fi
[i
].curmsg
= mp
->curmsg
;
442 fi
[i
].lowmsg
= mp
->lowmsg
;
443 fi
[i
].hghmsg
= mp
->hghmsg
;
444 fi
[i
].others
= other_files (mp
);
447 folder_free (mp
); /* free folder/message structure */
450 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
451 && (fi
[i
].error
== 0));
456 get_folder_info_callback (char *fold
, void *baton
)
461 get_folder_info_body (fold
, NULL
, &crawl_children
);
463 return crawl_children
;
467 get_folder_info (char *fold
, char *msg
)
472 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
474 if (crawl_children
) {
475 crawl_folders (fold
, get_folder_info_callback
, NULL
);
482 * Print folder information
489 bool hasempty
= false;
491 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
492 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
493 int nummsgdigits
, lowmsgdigits
;
494 int hghmsgdigits
, curmsgdigits
;
495 char tmpname
[BUFSIZ
];
498 * compute a few values needed to for
499 * printing various fields
501 for (i
= 0; i
< total_folders
; i
++) {
502 /* length of folder name */
503 len
= strlen (fi
[i
].name
);
507 /* If folder has error, skip the rest */
511 /* calculate total number of messages */
512 total_msgs
+= fi
[i
].nummsg
;
514 /* maximum number of messages */
515 if (fi
[i
].nummsg
> maxnummsg
)
516 maxnummsg
= fi
[i
].nummsg
;
518 /* maximum low message */
519 if (fi
[i
].lowmsg
> maxlowmsg
)
520 maxlowmsg
= fi
[i
].lowmsg
;
522 /* maximum high message */
523 if (fi
[i
].hghmsg
> maxhghmsg
)
524 maxhghmsg
= fi
[i
].hghmsg
;
526 /* maximum current message */
527 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
528 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
529 fi
[i
].curmsg
> maxcurmsg
)
530 maxcurmsg
= fi
[i
].curmsg
;
532 /* check for empty folders */
533 if (fi
[i
].nummsg
== 0)
536 nummsgdigits
= num_digits (maxnummsg
);
537 lowmsgdigits
= num_digits (maxlowmsg
);
538 hghmsgdigits
= num_digits (maxhghmsg
);
539 curmsgdigits
= num_digits (maxcurmsg
);
541 if (hasempty
&& nummsgdigits
< 2)
547 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
548 printf ("%-*s %*s %-*s; %-*s %*s\n",
550 nummsgdigits
+ 13, "# MESSAGES",
551 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
552 curmsgdigits
+ 4, "CUR",
556 * Print folder information
558 if (all
|| fshort
|| ftotal
< 1) {
559 for (i
= 0; i
< total_folders
; i
++) {
565 /* Add `+' to end of name, if folder is current */
566 if (strcmp (folder
, fi
[i
].name
))
567 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
569 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
572 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
576 printf ("%-*s ", maxlen
+1, tmpname
);
578 curprinted
= false; /* remember if we print cur */
579 if (fi
[i
].nummsg
== 0) {
580 printf ("has %*s messages%*s",
582 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
584 printf ("has %*d message%1s (%*d-%*d)",
585 nummsgdigits
, fi
[i
].nummsg
,
586 PLURALS(fi
[i
].nummsg
),
587 lowmsgdigits
, fi
[i
].lowmsg
,
588 hghmsgdigits
, fi
[i
].hghmsg
);
589 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
591 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
596 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
602 * Print folder/message totals
604 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
607 printf ("TOTAL = %d message%s in %d folder%s.\n",
608 total_msgs
, PLURALS(total_msgs
),
609 total_folders
, PLURALS(total_folders
));
616 * Set the current message and synchronize sequences
620 sfold (struct msgs
*mp
, char *msg
)
622 /* parse the message range/sequence/name and set SELECTED */
623 if (!m_convert (mp
, msg
))
626 if (mp
->numsel
> 1) {
627 inform("only one message at a time!, continuing...");
630 seq_setprev (mp
); /* set the previous-sequence */
631 seq_setcur (mp
, mp
->lowsel
);/* set current message */
632 seq_save (mp
); /* synchronize message sequences */
633 context_save (); /* save the context file */
640 * Do the read only folders
644 readonly_folders (void)
650 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
651 atrlen
= strlen (atrcur
);
653 for (np
= m_defs
; np
; np
= np
->n_next
)
654 if (ssequal (atrcur
, np
->n_name
)
655 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
656 get_folder_info (np
->n_name
+ atrlen
, NULL
);