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/ssequal.h"
12 #include "sbr/getcpy.h"
13 #include "sbr/m_convert.h"
14 #include "sbr/getfolder.h"
15 #include "sbr/folder_read.h"
16 #include "sbr/folder_pack.h"
17 #include "sbr/folder_free.h"
18 #include "sbr/context_save.h"
19 #include "sbr/context_replace.h"
20 #include "sbr/context_del.h"
21 #include "sbr/context_find.h"
22 #include "sbr/brkstring.h"
23 #include "sbr/ambigsw.h"
25 #include "sbr/print_version.h"
26 #include "sbr/print_help.h"
27 #include "sbr/error.h"
28 #include "h/crawl_folders.h"
31 #include "sbr/m_maildir.h"
33 #define FOLDER_SWITCHES \
35 X("noall", 0, NALLSW) \
36 X("create", 0, CREATSW) \
37 X("nocreate", 0, NCREATSW) \
38 X("fast", 0, FASTSW) \
39 X("nofast", 0, NFASTSW) \
40 X("header", 0, HDRSW) \
41 X("noheader", 0, NHDRSW) \
42 X("pack", 0, PACKSW) \
43 X("nopack", 0, NPACKSW) \
44 X("verbose", 0, VERBSW) \
45 X("noverbose", 0, NVERBSW) \
46 X("recurse", 0, RECURSW) \
47 X("norecurse", 0, NRECRSW) \
48 X("total", 0, TOTALSW) \
49 X("nototal", 0, NTOTLSW) \
50 X("list", 0, LISTSW) \
51 X("nolist", 0, NLISTSW) \
52 X("print", 0, PRNTSW) \
53 X("noprint", 0, NPRNTSW) \
54 X("push", 0, PUSHSW) \
56 X("version", 0, VERSIONSW) \
57 X("help", 0, HELPSW) \
59 #define X(sw, minchars, id) id,
60 DEFINE_SWITCH_ENUM(FOLDER
);
63 #define X(sw, minchars, id) { sw, minchars, id },
64 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
67 static bool fshort
; /* output only folder names */
68 static int fcreat
= 0; /* should we ask to create new folders? */
69 static bool fpack
; /* are we packing the folder? */
70 static bool fverb
; /* print actions taken while packing folder */
71 static int fheader
= 0; /* should we output a header? */
72 static bool frecurse
; /* recurse through subfolders */
73 static int ftotal
= 0; /* should we output the totals? */
74 static bool all
; /* should we output all folders */
76 static int total_folders
= 0; /* total number of folders */
79 static char *stack
= "Folder-Stack";
80 static char folder
[BUFSIZ
];
83 * Structure to hold information about
84 * folders as we scan them.
92 int others
; /* others == 1 if other files in folder */
93 int error
; /* error == 1 for unreadable folder */
97 * Dynamically allocated space to hold
98 * all the folder information.
100 static struct FolderInfo
*fi
;
101 static int maxFolderInfo
;
106 static int get_folder_info (char *, char *);
107 static crawl_callback_t get_folder_info_callback
;
108 static void print_folders (void);
109 static int sfold (struct msgs
*, char *);
110 static void readonly_folders (void);
113 * Function for printing error message if folder does not exist with
117 nonexistent_folder (int status
)
120 die("folder %s does not exist", folder
);
125 main (int argc
, char **argv
)
127 bool printsw
= false;
131 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
132 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
134 if (nmh_init(argv
[0], true, true)) { return 1; }
137 * If program was invoked with name ending
138 * in `s', then add switch `-all'.
140 all
= has_suffix_c(argv
[0], 's');
142 arguments
= getarguments (invo_name
, argc
, argv
, 1);
145 while ((cp
= *argp
++)) {
147 switch (smatch (++cp
, switches
)) {
149 ambigsw (cp
, switches
);
152 die("-%s unknown", cp
);
155 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
157 print_help (buf
, switches
, 1);
160 print_version(invo_name
);
246 if (*cp
== '+' || *cp
== '@') {
248 die("only one folder at a time!");
249 argfolder
= pluspath (cp
);
252 die("only one (current) message at a time!");
257 if (!context_find ("path"))
258 free (path ("./", TFOLDER
));
259 nmhdir
= concat (m_maildir (""), "/", NULL
);
262 * If we aren't working with the folder stack
263 * (-push, -pop, -list) then the default is to print.
265 if (!pushsw
&& !popsw
&& !listsw
)
268 /* Pushing a folder onto the folder stack */
271 /* If no folder is given, the current folder and */
272 /* the top of the folder stack are swapped. */
273 if ((cp
= context_find (stack
))) {
275 ap
= brkstring (dp
, " ", "\n");
276 argfolder
= getcpy(*ap
++);
278 die("no other folder");
280 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
281 cp
= add (*ap
, add (" ", cp
));
283 context_replace (stack
, cp
); /* update folder stack */
285 /* update folder stack */
286 context_replace (stack
,
287 (cp
= context_find (stack
))
288 ? concat (getfolder (1), " ", cp
, NULL
)
289 : mh_xstrdup(getfolder(1)));
293 /* Popping a folder off of the folder stack */
296 die("sorry, no folders allowed with -pop");
297 if ((cp
= context_find (stack
))) {
299 ap
= brkstring (dp
, " ", "\n");
300 argfolder
= getcpy(*ap
++);
302 die("folder stack empty");
305 /* if there's anything left in the stack */
308 cp
= add (*ap
, add (" ", cp
));
309 context_replace (stack
, cp
); /* update folder stack */
311 context_del (stack
); /* delete folder stack entry from context */
315 if (pushsw
|| popsw
) {
316 cp
= m_maildir(argfolder
);
317 if (access (cp
, F_OK
) == NOTOK
)
318 adios (cp
, "unable to find folder");
319 context_replace (pfolder
, argfolder
); /* update current folder */
320 context_save (); /* save the context file */
324 /* Listing the folder stack */
326 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
327 if ((cp
= context_find (stack
))) {
329 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
339 /* Allocate initial space to record folder information */
340 maxFolderInfo
= CRAWL_NUMFOLDERS
;
341 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
346 /* change directory to base of nmh directory for crawl_folders */
347 if (chdir (nmhdir
) == NOTOK
)
348 adios (nmhdir
, "unable to change directory to");
349 if (all
|| ftotal
> 0) {
351 * If no folder is given, do them all
355 inform("no folder given for message %s, continuing...", msg
);
356 readonly_folders (); /* do any readonly folders */
357 cp
= context_find(pfolder
);
358 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
359 crawl_folders (".", get_folder_info_callback
, NULL
);
361 strncpy (folder
, argfolder
, sizeof(folder
));
362 if (get_folder_info (argfolder
, msg
)) {
363 context_replace (pfolder
, argfolder
);/* update current folder */
364 context_save (); /* save the context file */
367 * Since recurse wasn't done in get_folder_info(),
368 * we still need to list all level-1 sub-folders.
371 crawl_folders (folder
, get_folder_info_callback
, NULL
);
374 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
377 * Check if folder exists. If not, then see if
378 * we should create it, or just exit.
380 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
382 if (get_folder_info (folder
, msg
) && argfolder
) {
383 /* update current folder */
384 context_replace (pfolder
, argfolder
);
389 * Print out folder information
393 context_save (); /* save the context file */
399 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
402 struct msgs
*mp
= NULL
;
407 * if necessary, reallocate the space
408 * for folder information
410 if (total_folders
>= maxFolderInfo
) {
411 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
412 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
423 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
425 * create message structure and get folder info
427 if (!(mp
= folder_read (fold
, fpack
))) {
428 inform("unable to read folder %s, continuing...", fold
);
429 *crawl_children
= false;
433 /* set the current message */
434 if (msg
&& !sfold (mp
, msg
))
438 if (folder_pack (&mp
, fverb
) == -1) {
439 *crawl_children
= false; /* to please clang static analyzer */
442 seq_save (mp
); /* synchronize the sequences */
443 context_save (); /* save the context file */
446 /* record info for this folder */
447 if ((ftotal
> 0) || !fshort
) {
448 fi
[i
].nummsg
= mp
->nummsg
;
449 fi
[i
].curmsg
= mp
->curmsg
;
450 fi
[i
].lowmsg
= mp
->lowmsg
;
451 fi
[i
].hghmsg
= mp
->hghmsg
;
452 fi
[i
].others
= other_files (mp
);
455 folder_free (mp
); /* free folder/message structure */
458 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
459 && (fi
[i
].error
== 0));
464 get_folder_info_callback (char *fold
, void *baton
)
469 get_folder_info_body (fold
, NULL
, &crawl_children
);
471 return crawl_children
;
475 get_folder_info (char *fold
, char *msg
)
480 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
482 if (crawl_children
) {
483 crawl_folders (fold
, get_folder_info_callback
, NULL
);
490 * Print folder information
497 bool hasempty
= false;
499 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
500 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
501 int nummsgdigits
, lowmsgdigits
;
502 int hghmsgdigits
, curmsgdigits
;
503 char tmpname
[BUFSIZ
];
506 * compute a few values needed to for
507 * printing various fields
509 for (i
= 0; i
< total_folders
; i
++) {
510 /* length of folder name */
511 len
= strlen (fi
[i
].name
);
515 /* If folder has error, skip the rest */
519 /* calculate total number of messages */
520 total_msgs
+= fi
[i
].nummsg
;
522 /* maximum number of messages */
523 if (fi
[i
].nummsg
> maxnummsg
)
524 maxnummsg
= fi
[i
].nummsg
;
526 /* maximum low message */
527 if (fi
[i
].lowmsg
> maxlowmsg
)
528 maxlowmsg
= fi
[i
].lowmsg
;
530 /* maximum high message */
531 if (fi
[i
].hghmsg
> maxhghmsg
)
532 maxhghmsg
= fi
[i
].hghmsg
;
534 /* maximum current message */
535 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
536 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
537 fi
[i
].curmsg
> maxcurmsg
)
538 maxcurmsg
= fi
[i
].curmsg
;
540 /* check for empty folders */
541 if (fi
[i
].nummsg
== 0)
544 nummsgdigits
= num_digits (maxnummsg
);
545 lowmsgdigits
= num_digits (maxlowmsg
);
546 hghmsgdigits
= num_digits (maxhghmsg
);
547 curmsgdigits
= num_digits (maxcurmsg
);
549 if (hasempty
&& nummsgdigits
< 2)
555 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
556 printf ("%-*s %*s %-*s; %-*s %*s\n",
558 nummsgdigits
+ 13, "# MESSAGES",
559 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
560 curmsgdigits
+ 4, "CUR",
564 * Print folder information
566 if (all
|| fshort
|| ftotal
< 1) {
567 for (i
= 0; i
< total_folders
; i
++) {
573 /* Add `+' to end of name, if folder is current */
574 if (strcmp (folder
, fi
[i
].name
))
575 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
577 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
580 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
584 printf ("%-*s ", maxlen
+1, tmpname
);
586 curprinted
= false; /* remember if we print cur */
587 if (fi
[i
].nummsg
== 0) {
588 printf ("has %*s messages%*s",
590 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
592 printf ("has %*d message%1s (%*d-%*d)",
593 nummsgdigits
, fi
[i
].nummsg
,
594 PLURALS(fi
[i
].nummsg
),
595 lowmsgdigits
, fi
[i
].lowmsg
,
596 hghmsgdigits
, fi
[i
].hghmsg
);
597 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
599 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
604 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
610 * Print folder/message totals
612 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
615 printf ("TOTAL = %d message%s in %d folder%s.\n",
616 total_msgs
, PLURALS(total_msgs
),
617 total_folders
, PLURALS(total_folders
));
624 * Set the current message and synchronize sequences
628 sfold (struct msgs
*mp
, char *msg
)
630 /* parse the message range/sequence/name and set SELECTED */
631 if (!m_convert (mp
, msg
))
634 if (mp
->numsel
> 1) {
635 inform("only one message at a time!, continuing...");
638 seq_setprev (mp
); /* set the previous-sequence */
639 seq_setcur (mp
, mp
->lowsel
);/* set current message */
640 seq_save (mp
); /* synchronize message sequences */
641 context_save (); /* save the context file */
648 * Do the read only folders
652 readonly_folders (void)
658 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
659 atrlen
= strlen (atrcur
);
661 for (np
= m_defs
; np
; np
= np
->n_next
)
662 if (ssequal (atrcur
, np
->n_name
)
663 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
664 get_folder_info (np
->n_name
+ atrlen
, NULL
);