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 <h/crawl_folders.h>
13 #include "sbr/m_maildir.h"
15 #define FOLDER_SWITCHES \
17 X("noall", 0, NALLSW) \
18 X("create", 0, CREATSW) \
19 X("nocreate", 0, NCREATSW) \
20 X("fast", 0, FASTSW) \
21 X("nofast", 0, NFASTSW) \
22 X("header", 0, HDRSW) \
23 X("noheader", 0, NHDRSW) \
24 X("pack", 0, PACKSW) \
25 X("nopack", 0, NPACKSW) \
26 X("verbose", 0, VERBSW) \
27 X("noverbose", 0, NVERBSW) \
28 X("recurse", 0, RECURSW) \
29 X("norecurse", 0, NRECRSW) \
30 X("total", 0, TOTALSW) \
31 X("nototal", 0, NTOTLSW) \
32 X("list", 0, LISTSW) \
33 X("nolist", 0, NLISTSW) \
34 X("print", 0, PRNTSW) \
35 X("noprint", 0, NPRNTSW) \
36 X("push", 0, PUSHSW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(FOLDER
);
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
49 static int fshort
= 0; /* output only folder names */
50 static int fcreat
= 0; /* should we ask to create new folders? */
51 static int fpack
= 0; /* are we packing the folder? */
52 static int fverb
= 0; /* print actions taken while packing folder */
53 static int fheader
= 0; /* should we output a header? */
54 static int frecurse
= 0; /* recurse through subfolders */
55 static int ftotal
= 0; /* should we output the totals? */
56 static int all
= 0; /* should we output all folders */
58 static int total_folders
= 0; /* total number of folders */
61 static char *stack
= "Folder-Stack";
62 static char folder
[BUFSIZ
];
65 * Structure to hold information about
66 * folders as we scan them.
74 int others
; /* others == 1 if other files in folder */
75 int error
; /* error == 1 for unreadable folder */
79 * Dynamically allocated space to hold
80 * all the folder information.
82 static struct FolderInfo
*fi
;
83 static int maxFolderInfo
;
88 static int get_folder_info (char *, char *);
89 static crawl_callback_t get_folder_info_callback
;
90 static void print_folders (void);
91 static int sfold (struct msgs
*, char *);
92 static void readonly_folders (void);
95 * Function for printing error message if folder does not exist with
100 nonexistent_folder (int status
) {
102 adios (NULL
, "folder %s does not exist", folder
);
107 main (int argc
, char **argv
)
109 int printsw
= 0, listsw
= 0;
110 int pushsw
= 0, popsw
= 0;
111 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
112 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
114 if (nmh_init(argv
[0], 1)) { return 1; }
117 * If program was invoked with name ending
118 * in `s', then add switch `-all'.
120 all
= has_suffix_c(argv
[0], 's');
122 arguments
= getarguments (invo_name
, argc
, argv
, 1);
125 while ((cp
= *argp
++)) {
127 switch (smatch (++cp
, switches
)) {
129 ambigsw (cp
, switches
);
132 adios (NULL
, "-%s unknown", cp
);
135 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
137 print_help (buf
, switches
, 1);
140 print_version(invo_name
);
226 if (*cp
== '+' || *cp
== '@') {
228 adios (NULL
, "only one folder at a time!");
229 argfolder
= pluspath (cp
);
232 adios (NULL
, "only one (current) message at a time!");
237 if (!context_find ("path"))
238 free (path ("./", TFOLDER
));
239 nmhdir
= concat (m_maildir (""), "/", NULL
);
242 * If we aren't working with the folder stack
243 * (-push, -pop, -list) then the default is to print.
245 if (pushsw
== 0 && popsw
== 0 && listsw
== 0)
248 /* Pushing a folder onto the folder stack */
251 /* If no folder is given, the current folder and */
252 /* the top of the folder stack are swapped. */
253 if ((cp
= context_find (stack
))) {
255 ap
= brkstring (dp
, " ", "\n");
256 argfolder
= getcpy(*ap
++);
258 adios (NULL
, "no other folder");
260 for (cp
= getcpy (getfolder (1)); *ap
; ap
++)
261 cp
= add (*ap
, add (" ", cp
));
263 context_replace (stack
, cp
); /* update folder stack */
265 /* update folder stack */
266 context_replace (stack
,
267 (cp
= context_find (stack
))
268 ? concat (getfolder (1), " ", cp
, NULL
)
269 : getcpy (getfolder (1)));
273 /* Popping a folder off of the folder stack */
276 adios (NULL
, "sorry, no folders allowed with -pop");
277 if ((cp
= context_find (stack
))) {
279 ap
= brkstring (dp
, " ", "\n");
280 argfolder
= getcpy(*ap
++);
282 adios (NULL
, "folder stack empty");
285 /* if there's anything left in the stack */
288 cp
= add (*ap
, add (" ", cp
));
289 context_replace (stack
, cp
); /* update folder stack */
291 context_del (stack
); /* delete folder stack entry from context */
295 if (pushsw
|| popsw
) {
296 cp
= m_maildir(argfolder
);
297 if (access (cp
, F_OK
) == NOTOK
)
298 adios (cp
, "unable to find folder");
299 context_replace (pfolder
, argfolder
); /* update current folder */
300 context_save (); /* save the context file */
304 /* Listing the folder stack */
306 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
307 if ((cp
= context_find (stack
))) {
309 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
319 /* Allocate initial space to record folder information */
320 maxFolderInfo
= CRAWL_NUMFOLDERS
;
321 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
326 /* change directory to base of nmh directory for crawl_folders */
327 if (chdir (nmhdir
) == NOTOK
)
328 adios (nmhdir
, "unable to change directory to");
329 if (all
|| ftotal
> 0) {
331 * If no folder is given, do them all
335 inform("no folder given for message %s, continuing...", msg
);
336 readonly_folders (); /* do any readonly folders */
337 cp
= context_find(pfolder
);
338 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
339 crawl_folders (".", get_folder_info_callback
, NULL
);
341 strncpy (folder
, argfolder
, sizeof(folder
));
342 if (get_folder_info (argfolder
, msg
)) {
343 context_replace (pfolder
, argfolder
);/* update current folder */
344 context_save (); /* save the context file */
347 * Since recurse wasn't done in get_folder_info(),
348 * we still need to list all level-1 sub-folders.
351 crawl_folders (folder
, get_folder_info_callback
, NULL
);
354 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
357 * Check if folder exists. If not, then see if
358 * we should create it, or just exit.
360 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
362 if (get_folder_info (folder
, msg
) && argfolder
) {
363 /* update current folder */
364 context_replace (pfolder
, argfolder
);
369 * Print out folder information
373 context_save (); /* save the context file */
379 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
382 struct msgs
*mp
= NULL
;
387 * if necessary, reallocate the space
388 * for folder information
390 if (total_folders
>= maxFolderInfo
) {
391 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
392 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
403 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
405 * create message structure and get folder info
407 if (!(mp
= folder_read (fold
, fpack
))) {
408 inform("unable to read folder %s, continuing...", fold
);
409 *crawl_children
= false;
413 /* set the current message */
414 if (msg
&& !sfold (mp
, msg
))
418 if (folder_pack (&mp
, fverb
) == -1) {
419 *crawl_children
= false; /* to please clang static analyzer */
422 seq_save (mp
); /* synchronize the sequences */
423 context_save (); /* save the context file */
426 /* record info for this folder */
427 if ((ftotal
> 0) || !fshort
) {
428 fi
[i
].nummsg
= mp
->nummsg
;
429 fi
[i
].curmsg
= mp
->curmsg
;
430 fi
[i
].lowmsg
= mp
->lowmsg
;
431 fi
[i
].hghmsg
= mp
->hghmsg
;
432 fi
[i
].others
= other_files (mp
);
435 folder_free (mp
); /* free folder/message structure */
438 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
439 && (fi
[i
].error
== 0));
444 get_folder_info_callback (char *fold
, void *baton
)
449 get_folder_info_body (fold
, NULL
, &crawl_children
);
451 return crawl_children
;
455 get_folder_info (char *fold
, char *msg
)
460 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
462 if (crawl_children
) {
463 crawl_folders (fold
, get_folder_info_callback
, NULL
);
470 * Print folder information
476 int i
, len
, hasempty
= 0, curprinted
;
477 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
478 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
479 int nummsgdigits
, lowmsgdigits
;
480 int hghmsgdigits
, curmsgdigits
;
481 char tmpname
[BUFSIZ
];
484 * compute a few values needed to for
485 * printing various fields
487 for (i
= 0; i
< total_folders
; i
++) {
488 /* length of folder name */
489 len
= strlen (fi
[i
].name
);
493 /* If folder has error, skip the rest */
497 /* calculate total number of messages */
498 total_msgs
+= fi
[i
].nummsg
;
500 /* maximum number of messages */
501 if (fi
[i
].nummsg
> maxnummsg
)
502 maxnummsg
= fi
[i
].nummsg
;
504 /* maximum low message */
505 if (fi
[i
].lowmsg
> maxlowmsg
)
506 maxlowmsg
= fi
[i
].lowmsg
;
508 /* maximum high message */
509 if (fi
[i
].hghmsg
> maxhghmsg
)
510 maxhghmsg
= fi
[i
].hghmsg
;
512 /* maximum current message */
513 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
514 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
515 fi
[i
].curmsg
> maxcurmsg
)
516 maxcurmsg
= fi
[i
].curmsg
;
518 /* check for empty folders */
519 if (fi
[i
].nummsg
== 0)
522 nummsgdigits
= num_digits (maxnummsg
);
523 lowmsgdigits
= num_digits (maxlowmsg
);
524 hghmsgdigits
= num_digits (maxhghmsg
);
525 curmsgdigits
= num_digits (maxcurmsg
);
527 if (hasempty
&& nummsgdigits
< 2)
533 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
534 printf ("%-*s %*s %-*s; %-*s %*s\n",
536 nummsgdigits
+ 13, "# MESSAGES",
537 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
538 curmsgdigits
+ 4, "CUR",
542 * Print folder information
544 if (all
|| fshort
|| ftotal
< 1) {
545 for (i
= 0; i
< total_folders
; i
++) {
551 /* Add `+' to end of name, if folder is current */
552 if (strcmp (folder
, fi
[i
].name
))
553 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
555 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
558 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
562 printf ("%-*s ", maxlen
+1, tmpname
);
564 curprinted
= 0; /* remember if we print cur */
565 if (fi
[i
].nummsg
== 0) {
566 printf ("has %*s messages%*s",
568 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
570 printf ("has %*d message%1s (%*d-%*d)",
571 nummsgdigits
, fi
[i
].nummsg
,
572 PLURALS(fi
[i
].nummsg
),
573 lowmsgdigits
, fi
[i
].lowmsg
,
574 hghmsgdigits
, fi
[i
].hghmsg
);
575 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
577 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
582 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
588 * Print folder/message totals
590 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
593 printf ("TOTAL = %d message%s in %d folder%s.\n",
594 total_msgs
, PLURALS(total_msgs
),
595 total_folders
, PLURALS(total_folders
));
602 * Set the current message and synchronize sequences
606 sfold (struct msgs
*mp
, char *msg
)
608 /* parse the message range/sequence/name and set SELECTED */
609 if (!m_convert (mp
, msg
))
612 if (mp
->numsel
> 1) {
613 inform("only one message at a time!, continuing...");
616 seq_setprev (mp
); /* set the previous-sequence */
617 seq_setcur (mp
, mp
->lowsel
);/* set current message */
618 seq_save (mp
); /* synchronize message sequences */
619 context_save (); /* save the context file */
626 * Do the read only folders
630 readonly_folders (void)
636 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
637 atrlen
= strlen (atrcur
);
639 for (np
= m_defs
; np
; np
= np
->n_next
)
640 if (ssequal (atrcur
, np
->n_name
)
641 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
642 get_folder_info (np
->n_name
+ atrlen
, NULL
);