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>
16 #define FOLDER_SWITCHES \
18 X("noall", 0, NALLSW) \
19 X("create", 0, CREATSW) \
20 X("nocreate", 0, NCREATSW) \
21 X("fast", 0, FASTSW) \
22 X("nofast", 0, NFASTSW) \
23 X("header", 0, HDRSW) \
24 X("noheader", 0, NHDRSW) \
25 X("pack", 0, PACKSW) \
26 X("nopack", 0, NPACKSW) \
27 X("verbose", 0, VERBSW) \
28 X("noverbose", 0, NVERBSW) \
29 X("recurse", 0, RECURSW) \
30 X("norecurse", 0, NRECRSW) \
31 X("total", 0, TOTALSW) \
32 X("nototal", 0, NTOTLSW) \
33 X("list", 0, LISTSW) \
34 X("nolist", 0, NLISTSW) \
35 X("print", 0, PRNTSW) \
36 X("noprint", -4, NPRNTSW) \
37 X("push", 0, PUSHSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(FOLDER
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(FOLDER
, switches
);
50 static int fshort
= 0; /* output only folder names */
51 static int fcreat
= 0; /* should we ask to create new folders? */
52 static int fpack
= 0; /* are we packing the folder? */
53 static int fverb
= 0; /* print actions taken while packing folder */
54 static int fheader
= 0; /* should we output a header? */
55 static int frecurse
= 0; /* recurse through subfolders */
56 static int ftotal
= 0; /* should we output the totals? */
57 static int all
= 0; /* should we output all folders */
59 static int total_folders
= 0; /* total number of folders */
62 static char *stack
= "Folder-Stack";
63 static char folder
[BUFSIZ
];
66 * Structure to hold information about
67 * folders as we scan them.
75 int others
; /* others == 1 if other files in folder */
76 int error
; /* error == 1 for unreadable folder */
80 * Dynamically allocated space to hold
81 * all the folder information.
83 static struct FolderInfo
*fi
;
84 static int maxFolderInfo
;
89 static int get_folder_info (char *, char *);
90 static crawl_callback_t get_folder_info_callback
;
91 static void print_folders (void);
92 static int sfold (struct msgs
*, char *);
93 static void readonly_folders (void);
97 main (int argc
, char **argv
)
99 int printsw
= 0, listsw
= 0;
100 int pushsw
= 0, popsw
= 0;
101 char *cp
, *dp
, *msg
= NULL
, *argfolder
= NULL
;
102 char **ap
, **argp
, buf
[BUFSIZ
], **arguments
;
104 setlocale(LC_ALL
, "");
105 invo_name
= r1bindex (argv
[0], '/');
107 /* read user profile/context */
111 * If program was invoked with name ending
112 * in `s', then add switch `-all'.
114 if (argv
[0][strlen (argv
[0]) - 1] == 's')
117 arguments
= getarguments (invo_name
, argc
, argv
, 1);
120 while ((cp
= *argp
++)) {
122 switch (smatch (++cp
, switches
)) {
124 ambigsw (cp
, switches
);
127 adios (NULL
, "-%s unknown", cp
);
130 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
132 print_help (buf
, switches
, 1);
135 print_version(invo_name
);
221 if (*cp
== '+' || *cp
== '@') {
223 adios (NULL
, "only one folder at a time!");
225 argfolder
= pluspath (cp
);
228 adios (NULL
, "only one (current) message at a time!");
234 if (!context_find ("path"))
235 free (path ("./", TFOLDER
));
236 nmhdir
= concat (m_maildir (""), "/", NULL
);
239 * If we aren't working with the folder stack
240 * (-push, -pop, -list) then the default is to print.
242 if (pushsw
== 0 && popsw
== 0 && listsw
== 0)
245 /* Pushing a folder onto the folder stack */
248 /* If no folder is given, the current folder and */
249 /* the top of the folder stack are swapped. */
250 if ((cp
= context_find (stack
))) {
252 ap
= brkstring (dp
, " ", "\n");
253 argfolder
= getcpy(*ap
++);
255 adios (NULL
, "no other folder");
257 for (cp
= getcpy (getfolder (1)); *ap
; ap
++)
258 cp
= add (*ap
, add (" ", cp
));
260 context_replace (stack
, cp
); /* update folder stack */
262 /* update folder stack */
263 context_replace (stack
,
264 (cp
= context_find (stack
))
265 ? concat (getfolder (1), " ", cp
, NULL
)
266 : getcpy (getfolder (1)));
270 /* Popping a folder off of the folder stack */
273 adios (NULL
, "sorry, no folders allowed with -pop");
274 if ((cp
= context_find (stack
))) {
276 ap
= brkstring (dp
, " ", "\n");
277 argfolder
= getcpy(*ap
++);
279 adios (NULL
, "folder stack empty");
282 /* if there's anything left in the stack */
285 cp
= add (*ap
, add (" ", cp
));
286 context_replace (stack
, cp
); /* update folder stack */
288 context_del (stack
); /* delete folder stack entry from context */
292 if (pushsw
|| popsw
) {
293 cp
= m_maildir(argfolder
);
294 if (access (cp
, F_OK
) == NOTOK
)
295 adios (cp
, "unable to find folder");
296 context_replace (pfolder
, argfolder
); /* update current folder */
297 context_save (); /* save the context file */
301 /* Listing the folder stack */
303 printf ("%s", argfolder
? argfolder
: getfolder (1));
304 if ((cp
= context_find (stack
))) {
306 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
316 /* Allocate initial space to record folder information */
317 maxFolderInfo
= CRAWL_NUMFOLDERS
;
318 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
323 /* change directory to base of nmh directory for crawl_folders */
324 if (chdir (nmhdir
) == NOTOK
)
325 adios (nmhdir
, "unable to change directory to");
326 if (all
|| ftotal
> 0) {
328 * If no folder is given, do them all
332 admonish (NULL
, "no folder given for message %s", msg
);
333 readonly_folders (); /* do any readonly folders */
334 strncpy (folder
, (cp
= context_find (pfolder
)) ? cp
: "", sizeof(folder
));
335 crawl_folders (".", get_folder_info_callback
, NULL
);
337 strncpy (folder
, argfolder
, sizeof(folder
));
338 if (get_folder_info (argfolder
, msg
)) {
339 context_replace (pfolder
, argfolder
);/* update current folder */
340 context_save (); /* save the context file */
343 * Since recurse wasn't done in get_folder_info(),
344 * we still need to list all level-1 sub-folders.
347 crawl_folders (folder
, get_folder_info_callback
, NULL
);
350 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
353 * Check if folder exists. If not, then see if
354 * we should create it, or just exit.
356 create_folder (m_maildir (folder
), fcreat
, done
);
358 if (get_folder_info (folder
, msg
) && argfolder
) {
359 /* update current folder */
360 context_replace (pfolder
, argfolder
);
365 * Print out folder information
369 context_save (); /* save the context file */
375 get_folder_info_body (char *fold
, char *msg
, boolean
*crawl_children
)
378 struct msgs
*mp
= NULL
;
383 * if necessary, reallocate the space
384 * for folder information
386 if (total_folders
>= maxFolderInfo
) {
387 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
388 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
399 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
401 * create message structure and get folder info
403 if (!(mp
= folder_read (fold
, 1))) {
404 admonish (NULL
, "unable to read folder %s", fold
);
408 /* set the current message */
409 if (msg
&& !sfold (mp
, msg
))
413 if (folder_pack (&mp
, fverb
) == -1)
415 seq_save (mp
); /* synchronize the sequences */
416 context_save (); /* save the context file */
419 /* record info for this folder */
420 if ((ftotal
> 0) || !fshort
) {
421 fi
[i
].nummsg
= mp
->nummsg
;
422 fi
[i
].curmsg
= mp
->curmsg
;
423 fi
[i
].lowmsg
= mp
->lowmsg
;
424 fi
[i
].hghmsg
= mp
->hghmsg
;
425 fi
[i
].others
= other_files (mp
);
428 folder_free (mp
); /* free folder/message structure */
431 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
432 && (fi
[i
].error
== 0));
437 get_folder_info_callback (char *fold
, void *baton
)
439 boolean crawl_children
;
442 get_folder_info_body (fold
, NULL
, &crawl_children
);
444 return crawl_children
;
448 get_folder_info (char *fold
, char *msg
)
450 boolean crawl_children
;
453 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
455 if (crawl_children
) {
456 crawl_folders (fold
, get_folder_info_callback
, NULL
);
463 * Print folder information
469 int i
, len
, hasempty
= 0, curprinted
;
470 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
471 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
472 int nummsgdigits
, lowmsgdigits
;
473 int hghmsgdigits
, curmsgdigits
;
474 char tmpname
[BUFSIZ
];
477 * compute a few values needed to for
478 * printing various fields
480 for (i
= 0; i
< total_folders
; i
++) {
481 /* length of folder name */
482 len
= strlen (fi
[i
].name
);
486 /* If folder has error, skip the rest */
490 /* calculate total number of messages */
491 total_msgs
+= fi
[i
].nummsg
;
493 /* maximum number of messages */
494 if (fi
[i
].nummsg
> maxnummsg
)
495 maxnummsg
= fi
[i
].nummsg
;
497 /* maximum low message */
498 if (fi
[i
].lowmsg
> maxlowmsg
)
499 maxlowmsg
= fi
[i
].lowmsg
;
501 /* maximum high message */
502 if (fi
[i
].hghmsg
> maxhghmsg
)
503 maxhghmsg
= fi
[i
].hghmsg
;
505 /* maximum current message */
506 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
507 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
508 fi
[i
].curmsg
> maxcurmsg
)
509 maxcurmsg
= fi
[i
].curmsg
;
511 /* check for empty folders */
512 if (fi
[i
].nummsg
== 0)
515 nummsgdigits
= num_digits (maxnummsg
);
516 lowmsgdigits
= num_digits (maxlowmsg
);
517 hghmsgdigits
= num_digits (maxhghmsg
);
518 curmsgdigits
= num_digits (maxcurmsg
);
520 if (hasempty
&& nummsgdigits
< 2)
526 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
527 printf ("%-*s %*s %-*s; %-*s %*s\n",
529 nummsgdigits
+ 13, "# MESSAGES",
530 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
531 curmsgdigits
+ 4, "CUR",
535 * Print folder information
537 if (all
|| fshort
|| ftotal
< 1) {
538 for (i
= 0; i
< total_folders
; i
++) {
540 printf ("%s\n", fi
[i
].name
);
544 /* Add `+' to end of name, if folder is current */
545 if (strcmp (folder
, fi
[i
].name
))
546 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
548 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
551 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
555 printf ("%-*s ", maxlen
+1, tmpname
);
557 curprinted
= 0; /* remember if we print cur */
558 if (fi
[i
].nummsg
== 0) {
559 printf ("has %*s messages%*s",
561 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
563 printf ("has %*d message%s (%*d-%*d)",
564 nummsgdigits
, fi
[i
].nummsg
,
565 (fi
[i
].nummsg
== 1) ? " " : "s",
566 lowmsgdigits
, fi
[i
].lowmsg
,
567 hghmsgdigits
, fi
[i
].hghmsg
);
568 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
570 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
575 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
581 * Print folder/message totals
583 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
586 printf ("TOTAL = %d message%c in %d folder%s.\n",
587 total_msgs
, total_msgs
!= 1 ? 's' : ' ',
588 total_folders
, total_folders
!= 1 ? "s" : "");
595 * Set the current message and sychronize sequences
599 sfold (struct msgs
*mp
, char *msg
)
601 /* parse the message range/sequence/name and set SELECTED */
602 if (!m_convert (mp
, msg
))
605 if (mp
->numsel
> 1) {
606 admonish (NULL
, "only one message at a time!");
609 seq_setprev (mp
); /* set the previous-sequence */
610 seq_setcur (mp
, mp
->lowsel
);/* set current message */
611 seq_save (mp
); /* synchronize message sequences */
612 context_save (); /* save the context file */
619 * Do the read only folders
623 readonly_folders (void)
627 register struct node
*np
;
629 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
630 atrlen
= strlen (atrcur
);
632 for (np
= m_defs
; np
; np
= np
->n_next
)
633 if (ssequal (atrcur
, np
->n_name
)
634 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
635 get_folder_info (np
->n_name
+ atrlen
, NULL
);