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_setcur.h"
12 #include "sbr/seq_save.h"
13 #include "sbr/smatch.h"
14 #include "sbr/ssequal.h"
15 #include "sbr/getcpy.h"
16 #include "sbr/m_convert.h"
17 #include "sbr/getfolder.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/folder_pack.h"
20 #include "sbr/folder_free.h"
21 #include "sbr/context_save.h"
22 #include "sbr/context_replace.h"
23 #include "sbr/context_del.h"
24 #include "sbr/context_find.h"
25 #include "sbr/brkstring.h"
26 #include "sbr/ambigsw.h"
28 #include "sbr/print_version.h"
29 #include "sbr/print_help.h"
30 #include "sbr/error.h"
31 #include "h/crawl_folders.h"
34 #include "sbr/m_maildir.h"
36 #define FOLDER_SWITCHES \
38 X("noall", 0, NALLSW) \
39 X("create", 0, CREATSW) \
40 X("nocreate", 0, NCREATSW) \
41 X("fast", 0, FASTSW) \
42 X("nofast", 0, NFASTSW) \
43 X("header", 0, HDRSW) \
44 X("noheader", 0, NHDRSW) \
45 X("pack", 0, PACKSW) \
46 X("nopack", 0, NPACKSW) \
47 X("verbose", 0, VERBSW) \
48 X("noverbose", 0, NVERBSW) \
49 X("recurse", 0, RECURSW) \
50 X("norecurse", 0, NRECRSW) \
51 X("total", 0, TOTALSW) \
52 X("nototal", 0, NTOTLSW) \
53 X("list", 0, LISTSW) \
54 X("nolist", 0, NLISTSW) \
55 X("print", 0, PRNTSW) \
56 X("noprint", 0, NPRNTSW) \
57 X("push", 0, PUSHSW) \
59 X("version", 0, VERSIONSW) \
60 X("help", 0, HELPSW) \
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(FOLDER
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
70 static bool fshort
; /* output only folder names */
71 static int fcreat
= 0; /* should we ask to create new folders? */
72 static bool fpack
; /* are we packing the folder? */
73 static bool fverb
; /* print actions taken while packing folder */
74 static int fheader
= 0; /* should we output a header? */
75 static bool frecurse
; /* recurse through subfolders */
76 static int ftotal
= 0; /* should we output the totals? */
77 static bool all
; /* should we output all folders */
79 static int total_folders
= 0; /* total number of folders */
82 static char *stack
= "Folder-Stack";
83 static char folder
[BUFSIZ
];
86 * Structure to hold information about
87 * folders as we scan them.
95 int others
; /* others == 1 if other files in folder */
96 int error
; /* error == 1 for unreadable folder */
100 * Dynamically allocated space to hold
101 * all the folder information.
103 static struct FolderInfo
*fi
;
104 static int maxFolderInfo
;
109 static int get_folder_info (char *, char *);
110 static crawl_callback_t get_folder_info_callback
;
111 static void print_folders (void);
112 static int sfold (struct msgs
*, char *);
113 static void readonly_folders (void);
116 * Function for printing error message if folder does not exist with
120 nonexistent_folder (int status
)
123 die("folder %s does not exist", folder
);
128 main (int argc
, char **argv
)
130 bool printsw
= false;
134 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
135 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
137 if (nmh_init(argv
[0], true, true)) { return 1; }
140 * If program was invoked with name ending
141 * in `s', then add switch `-all'.
143 all
= has_suffix_c(argv
[0], 's');
145 arguments
= getarguments (invo_name
, argc
, argv
, 1);
148 while ((cp
= *argp
++)) {
150 switch (smatch (++cp
, switches
)) {
152 ambigsw (cp
, switches
);
155 die("-%s unknown", cp
);
158 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
160 print_help (buf
, switches
, 1);
163 print_version(invo_name
);
249 if (*cp
== '+' || *cp
== '@') {
251 die("only one folder at a time!");
252 argfolder
= pluspath (cp
);
255 die("only one (current) message at a time!");
260 if (!context_find ("path"))
261 free (path ("./", TFOLDER
));
262 nmhdir
= concat (m_maildir (""), "/", NULL
);
265 * If we aren't working with the folder stack
266 * (-push, -pop, -list) then the default is to print.
268 if (!pushsw
&& !popsw
&& !listsw
)
271 /* Pushing a folder onto the folder stack */
274 /* If no folder is given, the current folder and */
275 /* the top of the folder stack are swapped. */
276 if ((cp
= context_find (stack
))) {
278 ap
= brkstring (dp
, " ", "\n");
279 argfolder
= getcpy(*ap
++);
281 die("no other folder");
283 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
284 cp
= add (*ap
, add (" ", cp
));
286 context_replace (stack
, cp
); /* update folder stack */
288 /* update folder stack */
289 context_replace (stack
,
290 (cp
= context_find (stack
))
291 ? concat (getfolder (1), " ", cp
, NULL
)
292 : mh_xstrdup(getfolder(1)));
296 /* Popping a folder off of the folder stack */
299 die("sorry, no folders allowed with -pop");
300 if ((cp
= context_find (stack
))) {
302 ap
= brkstring (dp
, " ", "\n");
303 argfolder
= getcpy(*ap
++);
305 die("folder stack empty");
308 /* if there's anything left in the stack */
311 cp
= add (*ap
, add (" ", cp
));
312 context_replace (stack
, cp
); /* update folder stack */
314 context_del (stack
); /* delete folder stack entry from context */
318 if (pushsw
|| popsw
) {
319 cp
= m_maildir(argfolder
);
320 if (access (cp
, F_OK
) == NOTOK
)
321 adios (cp
, "unable to find folder");
322 context_replace (pfolder
, argfolder
); /* update current folder */
323 context_save (); /* save the context file */
327 /* Listing the folder stack */
329 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
330 if ((cp
= context_find (stack
))) {
332 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
342 /* Allocate initial space to record folder information */
343 maxFolderInfo
= CRAWL_NUMFOLDERS
;
344 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
349 /* change directory to base of nmh directory for crawl_folders */
350 if (chdir (nmhdir
) == NOTOK
)
351 adios (nmhdir
, "unable to change directory to");
352 if (all
|| ftotal
> 0) {
354 * If no folder is given, do them all
358 inform("no folder given for message %s, continuing...", msg
);
359 readonly_folders (); /* do any readonly folders */
360 cp
= context_find(pfolder
);
361 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
362 crawl_folders (".", get_folder_info_callback
, NULL
);
364 strncpy (folder
, argfolder
, sizeof(folder
));
365 if (get_folder_info (argfolder
, msg
)) {
366 context_replace (pfolder
, argfolder
);/* update current folder */
367 context_save (); /* save the context file */
370 * Since recurse wasn't done in get_folder_info(),
371 * we still need to list all level-1 sub-folders.
374 crawl_folders (folder
, get_folder_info_callback
, NULL
);
377 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
380 * Check if folder exists. If not, then see if
381 * we should create it, or just exit.
383 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
385 if (get_folder_info (folder
, msg
) && argfolder
) {
386 /* update current folder */
387 context_replace (pfolder
, argfolder
);
392 * Print out folder information
396 context_save (); /* save the context file */
402 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
405 struct msgs
*mp
= NULL
;
410 * if necessary, reallocate the space
411 * for folder information
413 if (total_folders
>= maxFolderInfo
) {
414 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
415 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
426 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
428 * create message structure and get folder info
430 if (!(mp
= folder_read (fold
, fpack
))) {
431 inform("unable to read folder %s, continuing...", fold
);
432 *crawl_children
= false;
436 /* set the current message */
437 if (msg
&& !sfold (mp
, msg
))
441 if (folder_pack (&mp
, fverb
) == -1) {
442 *crawl_children
= false; /* to please clang static analyzer */
445 seq_save (mp
); /* synchronize the sequences */
446 context_save (); /* save the context file */
449 /* record info for this folder */
450 if ((ftotal
> 0) || !fshort
) {
451 fi
[i
].nummsg
= mp
->nummsg
;
452 fi
[i
].curmsg
= mp
->curmsg
;
453 fi
[i
].lowmsg
= mp
->lowmsg
;
454 fi
[i
].hghmsg
= mp
->hghmsg
;
455 fi
[i
].others
= other_files (mp
);
458 folder_free (mp
); /* free folder/message structure */
461 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
462 && (fi
[i
].error
== 0));
467 get_folder_info_callback (char *fold
, void *baton
)
472 get_folder_info_body (fold
, NULL
, &crawl_children
);
474 return crawl_children
;
478 get_folder_info (char *fold
, char *msg
)
483 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
485 if (crawl_children
) {
486 crawl_folders (fold
, get_folder_info_callback
, NULL
);
493 * Print folder information
500 bool hasempty
= false;
502 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
503 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
504 int nummsgdigits
, lowmsgdigits
;
505 int hghmsgdigits
, curmsgdigits
;
506 char tmpname
[BUFSIZ
];
509 * compute a few values needed to for
510 * printing various fields
512 for (i
= 0; i
< total_folders
; i
++) {
513 /* length of folder name */
514 len
= strlen (fi
[i
].name
);
518 /* If folder has error, skip the rest */
522 /* calculate total number of messages */
523 total_msgs
+= fi
[i
].nummsg
;
525 /* maximum number of messages */
526 if (fi
[i
].nummsg
> maxnummsg
)
527 maxnummsg
= fi
[i
].nummsg
;
529 /* maximum low message */
530 if (fi
[i
].lowmsg
> maxlowmsg
)
531 maxlowmsg
= fi
[i
].lowmsg
;
533 /* maximum high message */
534 if (fi
[i
].hghmsg
> maxhghmsg
)
535 maxhghmsg
= fi
[i
].hghmsg
;
537 /* maximum current message */
538 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
539 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
540 fi
[i
].curmsg
> maxcurmsg
)
541 maxcurmsg
= fi
[i
].curmsg
;
543 /* check for empty folders */
544 if (fi
[i
].nummsg
== 0)
547 nummsgdigits
= num_digits (maxnummsg
);
548 lowmsgdigits
= num_digits (maxlowmsg
);
549 hghmsgdigits
= num_digits (maxhghmsg
);
550 curmsgdigits
= num_digits (maxcurmsg
);
552 if (hasempty
&& nummsgdigits
< 2)
558 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
559 printf ("%-*s %*s %-*s; %-*s %*s\n",
561 nummsgdigits
+ 13, "# MESSAGES",
562 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
563 curmsgdigits
+ 4, "CUR",
567 * Print folder information
569 if (all
|| fshort
|| ftotal
< 1) {
570 for (i
= 0; i
< total_folders
; i
++) {
576 /* Add `+' to end of name, if folder is current */
577 if (strcmp (folder
, fi
[i
].name
))
578 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
580 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
583 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
587 printf ("%-*s ", maxlen
+1, tmpname
);
589 curprinted
= false; /* remember if we print cur */
590 if (fi
[i
].nummsg
== 0) {
591 printf ("has %*s messages%*s",
593 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
595 printf ("has %*d message%1s (%*d-%*d)",
596 nummsgdigits
, fi
[i
].nummsg
,
597 PLURALS(fi
[i
].nummsg
),
598 lowmsgdigits
, fi
[i
].lowmsg
,
599 hghmsgdigits
, fi
[i
].hghmsg
);
600 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
602 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
607 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
613 * Print folder/message totals
615 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
618 printf ("TOTAL = %d message%s in %d folder%s.\n",
619 total_msgs
, PLURALS(total_msgs
),
620 total_folders
, PLURALS(total_folders
));
627 * Set the current message and synchronize sequences
631 sfold (struct msgs
*mp
, char *msg
)
633 /* parse the message range/sequence/name and set SELECTED */
634 if (!m_convert (mp
, msg
))
637 if (mp
->numsel
> 1) {
638 inform("only one message at a time!, continuing...");
641 seq_setprev (mp
); /* set the previous-sequence */
642 seq_setcur (mp
, mp
->lowsel
);/* set current message */
643 seq_save (mp
); /* synchronize message sequences */
644 context_save (); /* save the context file */
651 * Do the read only folders
655 readonly_folders (void)
661 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
662 atrlen
= strlen (atrcur
);
664 for (np
= m_defs
; np
; np
= np
->n_next
)
665 if (ssequal (atrcur
, np
->n_name
)
666 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
667 get_folder_info (np
->n_name
+ atrlen
, NULL
);