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>
14 #define FOLDER_SWITCHES \
16 X("noall", 0, NALLSW) \
17 X("create", 0, CREATSW) \
18 X("nocreate", 0, NCREATSW) \
19 X("fast", 0, FASTSW) \
20 X("nofast", 0, NFASTSW) \
21 X("header", 0, HDRSW) \
22 X("noheader", 0, NHDRSW) \
23 X("pack", 0, PACKSW) \
24 X("nopack", 0, NPACKSW) \
25 X("verbose", 0, VERBSW) \
26 X("noverbose", 0, NVERBSW) \
27 X("recurse", 0, RECURSW) \
28 X("norecurse", 0, NRECRSW) \
29 X("total", 0, TOTALSW) \
30 X("nototal", 0, NTOTLSW) \
31 X("list", 0, LISTSW) \
32 X("nolist", 0, NLISTSW) \
33 X("print", 0, PRNTSW) \
34 X("noprint", 0, NPRNTSW) \
35 X("push", 0, PUSHSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
40 #define X(sw, minchars, id) id,
41 DEFINE_SWITCH_ENUM(FOLDER
);
44 #define X(sw, minchars, id) { sw, minchars, id },
45 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
48 static int fshort
= 0; /* output only folder names */
49 static int fcreat
= 0; /* should we ask to create new folders? */
50 static int fpack
= 0; /* are we packing the folder? */
51 static int fverb
= 0; /* print actions taken while packing folder */
52 static int fheader
= 0; /* should we output a header? */
53 static int frecurse
= 0; /* recurse through subfolders */
54 static int ftotal
= 0; /* should we output the totals? */
55 static int all
= 0; /* should we output all folders */
57 static int total_folders
= 0; /* total number of folders */
60 static char *stack
= "Folder-Stack";
61 static char folder
[BUFSIZ
];
64 * Structure to hold information about
65 * folders as we scan them.
73 int others
; /* others == 1 if other files in folder */
74 int error
; /* error == 1 for unreadable folder */
78 * Dynamically allocated space to hold
79 * all the folder information.
81 static struct FolderInfo
*fi
;
82 static int maxFolderInfo
;
87 static int get_folder_info (char *, char *);
88 static crawl_callback_t get_folder_info_callback
;
89 static void print_folders (void);
90 static int sfold (struct msgs
*, char *);
91 static void readonly_folders (void);
94 * Function for printing error message if folder does not exist with
99 nonexistent_folder (int status
) {
101 adios (NULL
, "folder %s does not exist", folder
);
106 main (int argc
, char **argv
)
108 int printsw
= 0, listsw
= 0;
109 int pushsw
= 0, popsw
= 0;
110 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
111 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
113 if (nmh_init(argv
[0], 1)) { return 1; }
116 * If program was invoked with name ending
117 * in `s', then add switch `-all'.
119 all
= has_suffix_c(argv
[0], 's');
121 arguments
= getarguments (invo_name
, argc
, argv
, 1);
124 while ((cp
= *argp
++)) {
126 switch (smatch (++cp
, switches
)) {
128 ambigsw (cp
, switches
);
131 adios (NULL
, "-%s unknown", cp
);
134 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
136 print_help (buf
, switches
, 1);
139 print_version(invo_name
);
225 if (*cp
== '+' || *cp
== '@') {
227 adios (NULL
, "only one folder at a time!");
229 argfolder
= pluspath (cp
);
232 adios (NULL
, "only one (current) message at a time!");
238 if (!context_find ("path"))
239 free (path ("./", TFOLDER
));
240 nmhdir
= concat (m_maildir (""), "/", NULL
);
243 * If we aren't working with the folder stack
244 * (-push, -pop, -list) then the default is to print.
246 if (pushsw
== 0 && popsw
== 0 && listsw
== 0)
249 /* Pushing a folder onto the folder stack */
252 /* If no folder is given, the current folder and */
253 /* the top of the folder stack are swapped. */
254 if ((cp
= context_find (stack
))) {
256 ap
= brkstring (dp
, " ", "\n");
257 argfolder
= getcpy(*ap
++);
259 adios (NULL
, "no other folder");
261 for (cp
= getcpy (getfolder (1)); *ap
; ap
++)
262 cp
= add (*ap
, add (" ", cp
));
264 context_replace (stack
, cp
); /* update folder stack */
266 /* update folder stack */
267 context_replace (stack
,
268 (cp
= context_find (stack
))
269 ? concat (getfolder (1), " ", cp
, NULL
)
270 : getcpy (getfolder (1)));
274 /* Popping a folder off of the folder stack */
277 adios (NULL
, "sorry, no folders allowed with -pop");
278 if ((cp
= context_find (stack
))) {
280 ap
= brkstring (dp
, " ", "\n");
281 argfolder
= getcpy(*ap
++);
283 adios (NULL
, "folder stack empty");
286 /* if there's anything left in the stack */
289 cp
= add (*ap
, add (" ", cp
));
290 context_replace (stack
, cp
); /* update folder stack */
292 context_del (stack
); /* delete folder stack entry from context */
296 if (pushsw
|| popsw
) {
297 cp
= m_maildir(argfolder
);
298 if (access (cp
, F_OK
) == NOTOK
)
299 adios (cp
, "unable to find folder");
300 context_replace (pfolder
, argfolder
); /* update current folder */
301 context_save (); /* save the context file */
305 /* Listing the folder stack */
307 fputs(argfolder
? argfolder
: getfolder (1), stdout
);
308 if ((cp
= context_find (stack
))) {
310 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
320 /* Allocate initial space to record folder information */
321 maxFolderInfo
= CRAWL_NUMFOLDERS
;
322 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
327 /* change directory to base of nmh directory for crawl_folders */
328 if (chdir (nmhdir
) == NOTOK
)
329 adios (nmhdir
, "unable to change directory to");
330 if (all
|| ftotal
> 0) {
332 * If no folder is given, do them all
336 inform("no folder given for message %s, continuing...", msg
);
337 readonly_folders (); /* do any readonly folders */
338 cp
= context_find(pfolder
);
339 strncpy (folder
, FENDNULL(cp
), sizeof(folder
));
340 crawl_folders (".", get_folder_info_callback
, NULL
);
342 strncpy (folder
, argfolder
, sizeof(folder
));
343 if (get_folder_info (argfolder
, msg
)) {
344 context_replace (pfolder
, argfolder
);/* update current folder */
345 context_save (); /* save the context file */
348 * Since recurse wasn't done in get_folder_info(),
349 * we still need to list all level-1 sub-folders.
352 crawl_folders (folder
, get_folder_info_callback
, NULL
);
355 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
358 * Check if folder exists. If not, then see if
359 * we should create it, or just exit.
361 create_folder (m_maildir (folder
), fcreat
, nonexistent_folder
);
363 if (get_folder_info (folder
, msg
) && argfolder
) {
364 /* update current folder */
365 context_replace (pfolder
, argfolder
);
370 * Print out folder information
374 context_save (); /* save the context file */
380 get_folder_info_body (char *fold
, char *msg
, boolean
*crawl_children
)
383 struct msgs
*mp
= NULL
;
388 * if necessary, reallocate the space
389 * for folder information
391 if (total_folders
>= maxFolderInfo
) {
392 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
393 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
404 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
406 * create message structure and get folder info
408 if (!(mp
= folder_read (fold
, fpack
))) {
409 inform("unable to read folder %s, continuing...", fold
);
410 *crawl_children
= FALSE
;
414 /* set the current message */
415 if (msg
&& !sfold (mp
, msg
))
419 if (folder_pack (&mp
, fverb
) == -1) {
420 *crawl_children
= FALSE
; /* to please clang static analyzer */
423 seq_save (mp
); /* synchronize the sequences */
424 context_save (); /* save the context file */
427 /* record info for this folder */
428 if ((ftotal
> 0) || !fshort
) {
429 fi
[i
].nummsg
= mp
->nummsg
;
430 fi
[i
].curmsg
= mp
->curmsg
;
431 fi
[i
].lowmsg
= mp
->lowmsg
;
432 fi
[i
].hghmsg
= mp
->hghmsg
;
433 fi
[i
].others
= other_files (mp
);
436 folder_free (mp
); /* free folder/message structure */
439 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
440 && (fi
[i
].error
== 0));
445 get_folder_info_callback (char *fold
, void *baton
)
447 boolean crawl_children
;
450 get_folder_info_body (fold
, NULL
, &crawl_children
);
452 return crawl_children
;
456 get_folder_info (char *fold
, char *msg
)
458 boolean crawl_children
;
461 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
463 if (crawl_children
) {
464 crawl_folders (fold
, get_folder_info_callback
, NULL
);
471 * Print folder information
477 int i
, len
, hasempty
= 0, curprinted
;
478 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
479 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
480 int nummsgdigits
, lowmsgdigits
;
481 int hghmsgdigits
, curmsgdigits
;
482 char tmpname
[BUFSIZ
];
485 * compute a few values needed to for
486 * printing various fields
488 for (i
= 0; i
< total_folders
; i
++) {
489 /* length of folder name */
490 len
= strlen (fi
[i
].name
);
494 /* If folder has error, skip the rest */
498 /* calculate total number of messages */
499 total_msgs
+= fi
[i
].nummsg
;
501 /* maximum number of messages */
502 if (fi
[i
].nummsg
> maxnummsg
)
503 maxnummsg
= fi
[i
].nummsg
;
505 /* maximum low message */
506 if (fi
[i
].lowmsg
> maxlowmsg
)
507 maxlowmsg
= fi
[i
].lowmsg
;
509 /* maximum high message */
510 if (fi
[i
].hghmsg
> maxhghmsg
)
511 maxhghmsg
= fi
[i
].hghmsg
;
513 /* maximum current message */
514 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
515 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
516 fi
[i
].curmsg
> maxcurmsg
)
517 maxcurmsg
= fi
[i
].curmsg
;
519 /* check for empty folders */
520 if (fi
[i
].nummsg
== 0)
523 nummsgdigits
= num_digits (maxnummsg
);
524 lowmsgdigits
= num_digits (maxlowmsg
);
525 hghmsgdigits
= num_digits (maxhghmsg
);
526 curmsgdigits
= num_digits (maxcurmsg
);
528 if (hasempty
&& nummsgdigits
< 2)
534 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
535 printf ("%-*s %*s %-*s; %-*s %*s\n",
537 nummsgdigits
+ 13, "# MESSAGES",
538 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
539 curmsgdigits
+ 4, "CUR",
543 * Print folder information
545 if (all
|| fshort
|| ftotal
< 1) {
546 for (i
= 0; i
< total_folders
; i
++) {
552 /* Add `+' to end of name, if folder is current */
553 if (strcmp (folder
, fi
[i
].name
))
554 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
556 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
559 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
563 printf ("%-*s ", maxlen
+1, tmpname
);
565 curprinted
= 0; /* remember if we print cur */
566 if (fi
[i
].nummsg
== 0) {
567 printf ("has %*s messages%*s",
569 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
571 printf ("has %*d message%1s (%*d-%*d)",
572 nummsgdigits
, fi
[i
].nummsg
,
573 PLURALS(fi
[i
].nummsg
),
574 lowmsgdigits
, fi
[i
].lowmsg
,
575 hghmsgdigits
, fi
[i
].hghmsg
);
576 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
578 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
583 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
589 * Print folder/message totals
591 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
594 printf ("TOTAL = %d message%s in %d folder%s.\n",
595 total_msgs
, PLURALS(total_msgs
),
596 total_folders
, PLURALS(total_folders
));
603 * Set the current message and synchronize sequences
607 sfold (struct msgs
*mp
, char *msg
)
609 /* parse the message range/sequence/name and set SELECTED */
610 if (!m_convert (mp
, msg
))
613 if (mp
->numsel
> 1) {
614 inform("only one message at a time!, continuing...");
617 seq_setprev (mp
); /* set the previous-sequence */
618 seq_setcur (mp
, mp
->lowsel
);/* set current message */
619 seq_save (mp
); /* synchronize message sequences */
620 context_save (); /* save the context file */
627 * Do the read only folders
631 readonly_folders (void)
637 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
638 atrlen
= strlen (atrcur
);
640 for (np
= m_defs
; np
; np
= np
->n_next
)
641 if (ssequal (atrcur
, np
->n_name
)
642 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
643 get_folder_info (np
->n_name
+ atrlen
, NULL
);