3 * folder(s).c -- set/list the current message and/or folder
4 * -- push/pop a folder onto/from the folder stack
5 * -- list the folder stack
7 * This code is Copyright (c) 2002, 2008, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/crawl_folders.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", -4, 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 int fshort
= 0; /* output only folder names */
52 static int fcreat
= 0; /* should we ask to create new folders? */
53 static int fpack
= 0; /* are we packing the folder? */
54 static int fverb
= 0; /* print actions taken while packing folder */
55 static int fheader
= 0; /* should we output a header? */
56 static int frecurse
= 0; /* recurse through subfolders */
57 static int ftotal
= 0; /* should we output the totals? */
58 static int all
= 0; /* 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);
98 main (int argc
, char **argv
)
100 int printsw
= 0, listsw
= 0;
101 int pushsw
= 0, popsw
= 0;
102 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
103 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
106 setlocale(LC_ALL
, "");
108 invo_name
= r1bindex (argv
[0], '/');
110 /* read user profile/context */
114 * If program was invoked with name ending
115 * in `s', then add switch `-all'.
117 if (argv
[0][strlen (argv
[0]) - 1] == 's')
120 arguments
= getarguments (invo_name
, argc
, argv
, 1);
123 while ((cp
= *argp
++)) {
125 switch (smatch (++cp
, switches
)) {
127 ambigsw (cp
, switches
);
130 adios (NULL
, "-%s unknown", cp
);
133 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
135 print_help (buf
, switches
, 1);
138 print_version(invo_name
);
224 if (*cp
== '+' || *cp
== '@') {
226 adios (NULL
, "only one folder at a time!");
228 argfolder
= pluspath (cp
);
231 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 printf ("%s", argfolder
? argfolder
: getfolder (1));
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 admonish (NULL
, "no folder given for message %s", msg
);
336 readonly_folders (); /* do any readonly folders */
337 strncpy (folder
, (cp
= context_find (pfolder
)) ? cp
: "", sizeof(folder
));
338 crawl_folders (".", get_folder_info_callback
, NULL
);
340 strncpy (folder
, argfolder
, sizeof(folder
));
341 if (get_folder_info (argfolder
, msg
)) {
342 context_replace (pfolder
, argfolder
);/* update current folder */
343 context_save (); /* save the context file */
346 * Since recurse wasn't done in get_folder_info(),
347 * we still need to list all level-1 sub-folders.
350 crawl_folders (folder
, get_folder_info_callback
, NULL
);
353 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
356 * Check if folder exists. If not, then see if
357 * we should create it, or just exit.
359 create_folder (m_maildir (folder
), fcreat
, done
);
361 if (get_folder_info (folder
, msg
) && argfolder
) {
362 /* update current folder */
363 context_replace (pfolder
, argfolder
);
368 * Print out folder information
372 context_save (); /* save the context file */
378 get_folder_info_body (char *fold
, char *msg
, boolean
*crawl_children
)
381 struct msgs
*mp
= NULL
;
386 * if necessary, reallocate the space
387 * for folder information
389 if (total_folders
>= maxFolderInfo
) {
390 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
391 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
402 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
404 * create message structure and get folder info
406 if (!(mp
= folder_read (fold
))) {
407 admonish (NULL
, "unable to read folder %s", fold
);
411 /* set the current message */
412 if (msg
&& !sfold (mp
, msg
))
416 if (folder_pack (&mp
, fverb
) == -1)
418 seq_save (mp
); /* synchronize the sequences */
419 context_save (); /* save the context file */
422 /* record info for this folder */
423 if ((ftotal
> 0) || !fshort
) {
424 fi
[i
].nummsg
= mp
->nummsg
;
425 fi
[i
].curmsg
= mp
->curmsg
;
426 fi
[i
].lowmsg
= mp
->lowmsg
;
427 fi
[i
].hghmsg
= mp
->hghmsg
;
428 fi
[i
].others
= other_files (mp
);
431 folder_free (mp
); /* free folder/message structure */
434 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
435 && (fi
[i
].error
== 0));
440 get_folder_info_callback (char *fold
, void *baton
)
442 boolean crawl_children
;
445 get_folder_info_body (fold
, NULL
, &crawl_children
);
447 return crawl_children
;
451 get_folder_info (char *fold
, char *msg
)
453 boolean crawl_children
;
456 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
458 if (crawl_children
) {
459 crawl_folders (fold
, get_folder_info_callback
, NULL
);
466 * Print folder information
472 int i
, len
, hasempty
= 0, curprinted
;
473 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
474 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
475 int nummsgdigits
, lowmsgdigits
;
476 int hghmsgdigits
, curmsgdigits
;
477 char tmpname
[BUFSIZ
];
480 * compute a few values needed to for
481 * printing various fields
483 for (i
= 0; i
< total_folders
; i
++) {
484 /* length of folder name */
485 len
= strlen (fi
[i
].name
);
489 /* If folder has error, skip the rest */
493 /* calculate total number of messages */
494 total_msgs
+= fi
[i
].nummsg
;
496 /* maximum number of messages */
497 if (fi
[i
].nummsg
> maxnummsg
)
498 maxnummsg
= fi
[i
].nummsg
;
500 /* maximum low message */
501 if (fi
[i
].lowmsg
> maxlowmsg
)
502 maxlowmsg
= fi
[i
].lowmsg
;
504 /* maximum high message */
505 if (fi
[i
].hghmsg
> maxhghmsg
)
506 maxhghmsg
= fi
[i
].hghmsg
;
508 /* maximum current message */
509 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
510 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
511 fi
[i
].curmsg
> maxcurmsg
)
512 maxcurmsg
= fi
[i
].curmsg
;
514 /* check for empty folders */
515 if (fi
[i
].nummsg
== 0)
518 nummsgdigits
= num_digits (maxnummsg
);
519 lowmsgdigits
= num_digits (maxlowmsg
);
520 hghmsgdigits
= num_digits (maxhghmsg
);
521 curmsgdigits
= num_digits (maxcurmsg
);
523 if (hasempty
&& nummsgdigits
< 2)
529 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
530 printf ("%-*s %*s %-*s; %-*s %*s\n",
532 nummsgdigits
+ 13, "# MESSAGES",
533 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
534 curmsgdigits
+ 4, "CUR",
538 * Print folder information
540 if (all
|| fshort
|| ftotal
< 1) {
541 for (i
= 0; i
< total_folders
; i
++) {
543 printf ("%s\n", fi
[i
].name
);
547 /* Add `+' to end of name, if folder is current */
548 if (strcmp (folder
, fi
[i
].name
))
549 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
551 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
554 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
558 printf ("%-*s ", maxlen
+1, tmpname
);
560 curprinted
= 0; /* remember if we print cur */
561 if (fi
[i
].nummsg
== 0) {
562 printf ("has %*s messages%*s",
564 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
566 printf ("has %*d message%s (%*d-%*d)",
567 nummsgdigits
, fi
[i
].nummsg
,
568 (fi
[i
].nummsg
== 1) ? " " : "s",
569 lowmsgdigits
, fi
[i
].lowmsg
,
570 hghmsgdigits
, fi
[i
].hghmsg
);
571 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
573 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
578 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
584 * Print folder/message totals
586 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
589 printf ("TOTAL = %d message%c in %d folder%s.\n",
590 total_msgs
, total_msgs
!= 1 ? 's' : ' ',
591 total_folders
, total_folders
!= 1 ? "s" : "");
598 * Set the current message and sychronize sequences
602 sfold (struct msgs
*mp
, char *msg
)
604 /* parse the message range/sequence/name and set SELECTED */
605 if (!m_convert (mp
, msg
))
608 if (mp
->numsel
> 1) {
609 admonish (NULL
, "only one message at a time!");
612 seq_setprev (mp
); /* set the previous-sequence */
613 seq_setcur (mp
, mp
->lowsel
);/* set current message */
614 seq_save (mp
); /* synchronize message sequences */
615 context_save (); /* save the context file */
622 * Do the read only folders
626 readonly_folders (void)
630 register struct node
*np
;
632 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
633 atrlen
= strlen (atrcur
);
635 for (np
= m_defs
; np
; np
= np
->n_next
)
636 if (ssequal (atrcur
, np
->n_name
)
637 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
638 get_folder_info (np
->n_name
+ atrlen
, NULL
);