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/error.h"
12 #include <h/crawl_folders.h>
15 #include "sbr/m_maildir.h"
17 #define FOLDER_SWITCHES \
19 X("noall", 0, NALLSW) \
20 X("create", 0, CREATSW) \
21 X("nocreate", 0, NCREATSW) \
22 X("fast", 0, FASTSW) \
23 X("nofast", 0, NFASTSW) \
24 X("header", 0, HDRSW) \
25 X("noheader", 0, NHDRSW) \
26 X("pack", 0, PACKSW) \
27 X("nopack", 0, NPACKSW) \
28 X("verbose", 0, VERBSW) \
29 X("noverbose", 0, NVERBSW) \
30 X("recurse", 0, RECURSW) \
31 X("norecurse", 0, NRECRSW) \
32 X("total", 0, TOTALSW) \
33 X("nototal", 0, NTOTLSW) \
34 X("list", 0, LISTSW) \
35 X("nolist", 0, NLISTSW) \
36 X("print", 0, PRNTSW) \
37 X("noprint", 0, NPRNTSW) \
38 X("push", 0, PUSHSW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(FOLDER
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
51 static bool fshort
; /* output only folder names */
52 static int fcreat
= 0; /* should we ask to create new folders? */
53 static bool fpack
; /* are we packing the folder? */
54 static bool fverb
; /* print actions taken while packing folder */
55 static int fheader
= 0; /* should we output a header? */
56 static bool frecurse
; /* recurse through subfolders */
57 static int ftotal
= 0; /* should we output the totals? */
58 static bool all
; /* should we output all folders */
60 static int total_folders
= 0; /* total number of folders */
63 static char *stack
= "Folder-Stack";
64 static char folder
[BUFSIZ
];
67 * Structure to hold information about
68 * folders as we scan them.
76 int others
; /* others == 1 if other files in folder */
77 int error
; /* error == 1 for unreadable folder */
81 * Dynamically allocated space to hold
82 * all the folder information.
84 static struct FolderInfo
*fi
;
85 static int maxFolderInfo
;
90 static int get_folder_info (char *, char *);
91 static crawl_callback_t get_folder_info_callback
;
92 static void print_folders (void);
93 static int sfold (struct msgs
*, char *);
94 static void readonly_folders (void);
97 * Function for printing error message if folder does not exist with
101 nonexistent_folder (int status
)
104 die("folder %s does not exist", folder
);
109 main (int argc
, char **argv
)
111 bool printsw
= false;
115 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
116 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
118 if (nmh_init(argv
[0], true, true)) { return 1; }
121 * If program was invoked with name ending
122 * in `s', then add switch `-all'.
124 all
= has_suffix_c(argv
[0], 's');
126 arguments
= getarguments (invo_name
, argc
, argv
, 1);
129 while ((cp
= *argp
++)) {
131 switch (smatch (++cp
, switches
)) {
133 ambigsw (cp
, switches
);
136 die("-%s unknown", cp
);
139 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
141 print_help (buf
, switches
, 1);
144 print_version(invo_name
);
230 if (*cp
== '+' || *cp
== '@') {
232 die("only one folder at a time!");
233 argfolder
= pluspath (cp
);
236 die("only one (current) message at a time!");
241 if (!context_find ("path"))
242 free (path ("./", TFOLDER
));
243 nmhdir
= concat (m_maildir (""), "/", NULL
);
246 * If we aren't working with the folder stack
247 * (-push, -pop, -list) then the default is to print.
249 if (!pushsw
&& !popsw
&& !listsw
)
252 /* Pushing a folder onto the folder stack */
255 /* If no folder is given, the current folder and */
256 /* the top of the folder stack are swapped. */
257 if ((cp
= context_find (stack
))) {
259 ap
= brkstring (dp
, " ", "\n");
260 argfolder
= getcpy(*ap
++);
262 die("no other folder");
264 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
265 cp
= add (*ap
, add (" ", cp
));
267 context_replace (stack
, cp
); /* update folder stack */
269 /* update folder stack */
270 context_replace (stack
,
271 (cp
= context_find (stack
))
272 ? concat (getfolder (1), " ", cp
, NULL
)
273 : mh_xstrdup(getfolder(1)));
277 /* Popping a folder off of the folder stack */
280 die("sorry, no folders allowed with -pop");
281 if ((cp
= context_find (stack
))) {
283 ap
= brkstring (dp
, " ", "\n");
284 argfolder
= getcpy(*ap
++);
286 die("folder stack empty");
289 /* if there's anything left in the stack */
292 cp
= add (*ap
, add (" ", cp
));
293 context_replace (stack
, cp
); /* update folder stack */
295 context_del (stack
); /* delete folder stack entry from context */
299 if (pushsw
|| popsw
) {
300 cp
= m_maildir(argfolder
);
301 if (access (cp
, F_OK
) == NOTOK
)
302 adios (cp
, "unable to find folder");
303 context_replace (pfolder
, argfolder
); /* update current folder */
304 context_save (); /* save the context file */
308 /* Listing the folder stack */
310 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
311 if ((cp
= context_find (stack
))) {
313 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
323 /* Allocate initial space to record folder information */
324 maxFolderInfo
= CRAWL_NUMFOLDERS
;
325 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
330 /* change directory to base of nmh directory for crawl_folders */
331 if (chdir (nmhdir
) == NOTOK
)
332 adios (nmhdir
, "unable to change directory to");
333 if (all
|| ftotal
> 0) {
335 * If no folder is given, do them all
339 inform("no folder given for message %s, continuing...", msg
);
340 readonly_folders (); /* do any readonly folders */
341 cp
= context_find(pfolder
);
342 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
343 crawl_folders (".", get_folder_info_callback
, NULL
);
345 strncpy (folder
, argfolder
, sizeof(folder
));
346 if (get_folder_info (argfolder
, msg
)) {
347 context_replace (pfolder
, argfolder
);/* update current folder */
348 context_save (); /* save the context file */
351 * Since recurse wasn't done in get_folder_info(),
352 * we still need to list all level-1 sub-folders.
355 crawl_folders (folder
, get_folder_info_callback
, NULL
);
358 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
361 * Check if folder exists. If not, then see if
362 * we should create it, or just exit.
364 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
366 if (get_folder_info (folder
, msg
) && argfolder
) {
367 /* update current folder */
368 context_replace (pfolder
, argfolder
);
373 * Print out folder information
377 context_save (); /* save the context file */
383 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
386 struct msgs
*mp
= NULL
;
391 * if necessary, reallocate the space
392 * for folder information
394 if (total_folders
>= maxFolderInfo
) {
395 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
396 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
407 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
409 * create message structure and get folder info
411 if (!(mp
= folder_read (fold
, fpack
))) {
412 inform("unable to read folder %s, continuing...", fold
);
413 *crawl_children
= false;
417 /* set the current message */
418 if (msg
&& !sfold (mp
, msg
))
422 if (folder_pack (&mp
, fverb
) == -1) {
423 *crawl_children
= false; /* to please clang static analyzer */
426 seq_save (mp
); /* synchronize the sequences */
427 context_save (); /* save the context file */
430 /* record info for this folder */
431 if ((ftotal
> 0) || !fshort
) {
432 fi
[i
].nummsg
= mp
->nummsg
;
433 fi
[i
].curmsg
= mp
->curmsg
;
434 fi
[i
].lowmsg
= mp
->lowmsg
;
435 fi
[i
].hghmsg
= mp
->hghmsg
;
436 fi
[i
].others
= other_files (mp
);
439 folder_free (mp
); /* free folder/message structure */
442 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
443 && (fi
[i
].error
== 0));
448 get_folder_info_callback (char *fold
, void *baton
)
453 get_folder_info_body (fold
, NULL
, &crawl_children
);
455 return crawl_children
;
459 get_folder_info (char *fold
, char *msg
)
464 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
466 if (crawl_children
) {
467 crawl_folders (fold
, get_folder_info_callback
, NULL
);
474 * Print folder information
481 bool hasempty
= false;
483 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
484 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
485 int nummsgdigits
, lowmsgdigits
;
486 int hghmsgdigits
, curmsgdigits
;
487 char tmpname
[BUFSIZ
];
490 * compute a few values needed to for
491 * printing various fields
493 for (i
= 0; i
< total_folders
; i
++) {
494 /* length of folder name */
495 len
= strlen (fi
[i
].name
);
499 /* If folder has error, skip the rest */
503 /* calculate total number of messages */
504 total_msgs
+= fi
[i
].nummsg
;
506 /* maximum number of messages */
507 if (fi
[i
].nummsg
> maxnummsg
)
508 maxnummsg
= fi
[i
].nummsg
;
510 /* maximum low message */
511 if (fi
[i
].lowmsg
> maxlowmsg
)
512 maxlowmsg
= fi
[i
].lowmsg
;
514 /* maximum high message */
515 if (fi
[i
].hghmsg
> maxhghmsg
)
516 maxhghmsg
= fi
[i
].hghmsg
;
518 /* maximum current message */
519 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
520 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
521 fi
[i
].curmsg
> maxcurmsg
)
522 maxcurmsg
= fi
[i
].curmsg
;
524 /* check for empty folders */
525 if (fi
[i
].nummsg
== 0)
528 nummsgdigits
= num_digits (maxnummsg
);
529 lowmsgdigits
= num_digits (maxlowmsg
);
530 hghmsgdigits
= num_digits (maxhghmsg
);
531 curmsgdigits
= num_digits (maxcurmsg
);
533 if (hasempty
&& nummsgdigits
< 2)
539 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
540 printf ("%-*s %*s %-*s; %-*s %*s\n",
542 nummsgdigits
+ 13, "# MESSAGES",
543 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
544 curmsgdigits
+ 4, "CUR",
548 * Print folder information
550 if (all
|| fshort
|| ftotal
< 1) {
551 for (i
= 0; i
< total_folders
; i
++) {
557 /* Add `+' to end of name, if folder is current */
558 if (strcmp (folder
, fi
[i
].name
))
559 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
561 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
564 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
568 printf ("%-*s ", maxlen
+1, tmpname
);
570 curprinted
= false; /* remember if we print cur */
571 if (fi
[i
].nummsg
== 0) {
572 printf ("has %*s messages%*s",
574 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
576 printf ("has %*d message%1s (%*d-%*d)",
577 nummsgdigits
, fi
[i
].nummsg
,
578 PLURALS(fi
[i
].nummsg
),
579 lowmsgdigits
, fi
[i
].lowmsg
,
580 hghmsgdigits
, fi
[i
].hghmsg
);
581 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
583 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
588 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
594 * Print folder/message totals
596 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
599 printf ("TOTAL = %d message%s in %d folder%s.\n",
600 total_msgs
, PLURALS(total_msgs
),
601 total_folders
, PLURALS(total_folders
));
608 * Set the current message and synchronize sequences
612 sfold (struct msgs
*mp
, char *msg
)
614 /* parse the message range/sequence/name and set SELECTED */
615 if (!m_convert (mp
, msg
))
618 if (mp
->numsel
> 1) {
619 inform("only one message at a time!, continuing...");
622 seq_setprev (mp
); /* set the previous-sequence */
623 seq_setcur (mp
, mp
->lowsel
);/* set current message */
624 seq_save (mp
); /* synchronize message sequences */
625 context_save (); /* save the context file */
632 * Do the read only folders
636 readonly_folders (void)
642 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
643 atrlen
= strlen (atrcur
);
645 for (np
= m_defs
; np
; np
= np
->n_next
)
646 if (ssequal (atrcur
, np
->n_name
)
647 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
648 get_folder_info (np
->n_name
+ atrlen
, NULL
);