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_save.h"
12 #include "sbr/smatch.h"
13 #include "sbr/ssequal.h"
14 #include "sbr/getcpy.h"
15 #include "sbr/m_convert.h"
16 #include "sbr/getfolder.h"
17 #include "sbr/folder_read.h"
18 #include "sbr/folder_pack.h"
19 #include "sbr/folder_free.h"
20 #include "sbr/context_save.h"
21 #include "sbr/context_replace.h"
22 #include "sbr/context_del.h"
23 #include "sbr/context_find.h"
24 #include "sbr/brkstring.h"
25 #include "sbr/ambigsw.h"
27 #include "sbr/print_version.h"
28 #include "sbr/print_help.h"
29 #include "sbr/error.h"
30 #include "h/crawl_folders.h"
33 #include "sbr/m_maildir.h"
35 #define FOLDER_SWITCHES \
37 X("noall", 0, NALLSW) \
38 X("create", 0, CREATSW) \
39 X("nocreate", 0, NCREATSW) \
40 X("fast", 0, FASTSW) \
41 X("nofast", 0, NFASTSW) \
42 X("header", 0, HDRSW) \
43 X("noheader", 0, NHDRSW) \
44 X("pack", 0, PACKSW) \
45 X("nopack", 0, NPACKSW) \
46 X("verbose", 0, VERBSW) \
47 X("noverbose", 0, NVERBSW) \
48 X("recurse", 0, RECURSW) \
49 X("norecurse", 0, NRECRSW) \
50 X("total", 0, TOTALSW) \
51 X("nototal", 0, NTOTLSW) \
52 X("list", 0, LISTSW) \
53 X("nolist", 0, NLISTSW) \
54 X("print", 0, PRNTSW) \
55 X("noprint", 0, NPRNTSW) \
56 X("push", 0, PUSHSW) \
58 X("version", 0, VERSIONSW) \
59 X("help", 0, HELPSW) \
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(FOLDER
);
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
69 static bool fshort
; /* output only folder names */
70 static int fcreat
= 0; /* should we ask to create new folders? */
71 static bool fpack
; /* are we packing the folder? */
72 static bool fverb
; /* print actions taken while packing folder */
73 static int fheader
= 0; /* should we output a header? */
74 static bool frecurse
; /* recurse through subfolders */
75 static int ftotal
= 0; /* should we output the totals? */
76 static bool all
; /* should we output all folders */
78 static int total_folders
= 0; /* total number of folders */
81 static char *stack
= "Folder-Stack";
82 static char folder
[BUFSIZ
];
85 * Structure to hold information about
86 * folders as we scan them.
94 int others
; /* others == 1 if other files in folder */
95 int error
; /* error == 1 for unreadable folder */
99 * Dynamically allocated space to hold
100 * all the folder information.
102 static struct FolderInfo
*fi
;
103 static int maxFolderInfo
;
108 static int get_folder_info (char *, char *);
109 static crawl_callback_t get_folder_info_callback
;
110 static void print_folders (void);
111 static int sfold (struct msgs
*, char *);
112 static void readonly_folders (void);
115 * Function for printing error message if folder does not exist with
119 nonexistent_folder (int status
)
122 die("folder %s does not exist", folder
);
127 main (int argc
, char **argv
)
129 bool printsw
= false;
133 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
134 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
136 if (nmh_init(argv
[0], true, true)) { return 1; }
139 * If program was invoked with name ending
140 * in `s', then add switch `-all'.
142 all
= has_suffix_c(argv
[0], 's');
144 arguments
= getarguments (invo_name
, argc
, argv
, 1);
147 while ((cp
= *argp
++)) {
149 switch (smatch (++cp
, switches
)) {
151 ambigsw (cp
, switches
);
154 die("-%s unknown", cp
);
157 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
159 print_help (buf
, switches
, 1);
162 print_version(invo_name
);
248 if (*cp
== '+' || *cp
== '@') {
250 die("only one folder at a time!");
251 argfolder
= pluspath (cp
);
254 die("only one (current) message at a time!");
259 if (!context_find ("path"))
260 free (path ("./", TFOLDER
));
261 nmhdir
= concat (m_maildir (""), "/", NULL
);
264 * If we aren't working with the folder stack
265 * (-push, -pop, -list) then the default is to print.
267 if (!pushsw
&& !popsw
&& !listsw
)
270 /* Pushing a folder onto the folder stack */
273 /* If no folder is given, the current folder and */
274 /* the top of the folder stack are swapped. */
275 if ((cp
= context_find (stack
))) {
277 ap
= brkstring (dp
, " ", "\n");
278 argfolder
= getcpy(*ap
++);
280 die("no other folder");
282 for (cp
= mh_xstrdup(getfolder(1)); *ap
; ap
++)
283 cp
= add (*ap
, add (" ", cp
));
285 context_replace (stack
, cp
); /* update folder stack */
287 /* update folder stack */
288 context_replace (stack
,
289 (cp
= context_find (stack
))
290 ? concat (getfolder (1), " ", cp
, NULL
)
291 : mh_xstrdup(getfolder(1)));
295 /* Popping a folder off of the folder stack */
298 die("sorry, no folders allowed with -pop");
299 if ((cp
= context_find (stack
))) {
301 ap
= brkstring (dp
, " ", "\n");
302 argfolder
= getcpy(*ap
++);
304 die("folder stack empty");
307 /* if there's anything left in the stack */
310 cp
= add (*ap
, add (" ", cp
));
311 context_replace (stack
, cp
); /* update folder stack */
313 context_del (stack
); /* delete folder stack entry from context */
317 if (pushsw
|| popsw
) {
318 cp
= m_maildir(argfolder
);
319 if (access (cp
, F_OK
) == NOTOK
)
320 adios (cp
, "unable to find folder");
321 context_replace (pfolder
, argfolder
); /* update current folder */
322 context_save (); /* save the context file */
326 /* Listing the folder stack */
328 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
329 if ((cp
= context_find (stack
))) {
331 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
341 /* Allocate initial space to record folder information */
342 maxFolderInfo
= CRAWL_NUMFOLDERS
;
343 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
348 /* change directory to base of nmh directory for crawl_folders */
349 if (chdir (nmhdir
) == NOTOK
)
350 adios (nmhdir
, "unable to change directory to");
351 if (all
|| ftotal
> 0) {
353 * If no folder is given, do them all
357 inform("no folder given for message %s, continuing...", msg
);
358 readonly_folders (); /* do any readonly folders */
359 cp
= context_find(pfolder
);
360 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
361 crawl_folders (".", get_folder_info_callback
, NULL
);
363 strncpy (folder
, argfolder
, sizeof(folder
));
364 if (get_folder_info (argfolder
, msg
)) {
365 context_replace (pfolder
, argfolder
);/* update current folder */
366 context_save (); /* save the context file */
369 * Since recurse wasn't done in get_folder_info(),
370 * we still need to list all level-1 sub-folders.
373 crawl_folders (folder
, get_folder_info_callback
, NULL
);
376 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
379 * Check if folder exists. If not, then see if
380 * we should create it, or just exit.
382 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
384 if (get_folder_info (folder
, msg
) && argfolder
) {
385 /* update current folder */
386 context_replace (pfolder
, argfolder
);
391 * Print out folder information
395 context_save (); /* save the context file */
401 get_folder_info_body (char *fold
, char *msg
, bool *crawl_children
)
404 struct msgs
*mp
= NULL
;
409 * if necessary, reallocate the space
410 * for folder information
412 if (total_folders
>= maxFolderInfo
) {
413 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
414 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
425 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
427 * create message structure and get folder info
429 if (!(mp
= folder_read (fold
, fpack
))) {
430 inform("unable to read folder %s, continuing...", fold
);
431 *crawl_children
= false;
435 /* set the current message */
436 if (msg
&& !sfold (mp
, msg
))
440 if (folder_pack (&mp
, fverb
) == -1) {
441 *crawl_children
= false; /* to please clang static analyzer */
444 seq_save (mp
); /* synchronize the sequences */
445 context_save (); /* save the context file */
448 /* record info for this folder */
449 if ((ftotal
> 0) || !fshort
) {
450 fi
[i
].nummsg
= mp
->nummsg
;
451 fi
[i
].curmsg
= mp
->curmsg
;
452 fi
[i
].lowmsg
= mp
->lowmsg
;
453 fi
[i
].hghmsg
= mp
->hghmsg
;
454 fi
[i
].others
= other_files (mp
);
457 folder_free (mp
); /* free folder/message structure */
460 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
461 && (fi
[i
].error
== 0));
466 get_folder_info_callback (char *fold
, void *baton
)
471 get_folder_info_body (fold
, NULL
, &crawl_children
);
473 return crawl_children
;
477 get_folder_info (char *fold
, char *msg
)
482 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
484 if (crawl_children
) {
485 crawl_folders (fold
, get_folder_info_callback
, NULL
);
492 * Print folder information
499 bool hasempty
= false;
501 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
502 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
503 int nummsgdigits
, lowmsgdigits
;
504 int hghmsgdigits
, curmsgdigits
;
505 char tmpname
[BUFSIZ
];
508 * compute a few values needed to for
509 * printing various fields
511 for (i
= 0; i
< total_folders
; i
++) {
512 /* length of folder name */
513 len
= strlen (fi
[i
].name
);
517 /* If folder has error, skip the rest */
521 /* calculate total number of messages */
522 total_msgs
+= fi
[i
].nummsg
;
524 /* maximum number of messages */
525 if (fi
[i
].nummsg
> maxnummsg
)
526 maxnummsg
= fi
[i
].nummsg
;
528 /* maximum low message */
529 if (fi
[i
].lowmsg
> maxlowmsg
)
530 maxlowmsg
= fi
[i
].lowmsg
;
532 /* maximum high message */
533 if (fi
[i
].hghmsg
> maxhghmsg
)
534 maxhghmsg
= fi
[i
].hghmsg
;
536 /* maximum current message */
537 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
538 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
539 fi
[i
].curmsg
> maxcurmsg
)
540 maxcurmsg
= fi
[i
].curmsg
;
542 /* check for empty folders */
543 if (fi
[i
].nummsg
== 0)
546 nummsgdigits
= num_digits (maxnummsg
);
547 lowmsgdigits
= num_digits (maxlowmsg
);
548 hghmsgdigits
= num_digits (maxhghmsg
);
549 curmsgdigits
= num_digits (maxcurmsg
);
551 if (hasempty
&& nummsgdigits
< 2)
557 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
558 printf ("%-*s %*s %-*s; %-*s %*s\n",
560 nummsgdigits
+ 13, "# MESSAGES",
561 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
562 curmsgdigits
+ 4, "CUR",
566 * Print folder information
568 if (all
|| fshort
|| ftotal
< 1) {
569 for (i
= 0; i
< total_folders
; i
++) {
575 /* Add `+' to end of name, if folder is current */
576 if (strcmp (folder
, fi
[i
].name
))
577 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
579 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
582 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
586 printf ("%-*s ", maxlen
+1, tmpname
);
588 curprinted
= false; /* remember if we print cur */
589 if (fi
[i
].nummsg
== 0) {
590 printf ("has %*s messages%*s",
592 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
594 printf ("has %*d message%1s (%*d-%*d)",
595 nummsgdigits
, fi
[i
].nummsg
,
596 PLURALS(fi
[i
].nummsg
),
597 lowmsgdigits
, fi
[i
].lowmsg
,
598 hghmsgdigits
, fi
[i
].hghmsg
);
599 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
601 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
606 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
612 * Print folder/message totals
614 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
617 printf ("TOTAL = %d message%s in %d folder%s.\n",
618 total_msgs
, PLURALS(total_msgs
),
619 total_folders
, PLURALS(total_folders
));
626 * Set the current message and synchronize sequences
630 sfold (struct msgs
*mp
, char *msg
)
632 /* parse the message range/sequence/name and set SELECTED */
633 if (!m_convert (mp
, msg
))
636 if (mp
->numsel
> 1) {
637 inform("only one message at a time!, continuing...");
640 seq_setprev (mp
); /* set the previous-sequence */
641 seq_setcur (mp
, mp
->lowsel
);/* set current message */
642 seq_save (mp
); /* synchronize message sequences */
643 context_save (); /* save the context file */
650 * Do the read only folders
654 readonly_folders (void)
660 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
661 atrlen
= strlen (atrcur
);
663 for (np
= m_defs
; np
; np
= np
->n_next
)
664 if (ssequal (atrcur
, np
->n_name
)
665 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
666 get_folder_info (np
->n_name
+ atrlen
, NULL
);