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 if (nmh_init(argv
[0], 1)) { return 1; }
107 * If program was invoked with name ending
108 * in `s', then add switch `-all'.
110 if (argv
[0][strlen (argv
[0]) - 1] == 's')
113 arguments
= getarguments (invo_name
, argc
, argv
, 1);
116 while ((cp
= *argp
++)) {
118 switch (smatch (++cp
, switches
)) {
120 ambigsw (cp
, switches
);
123 adios (NULL
, "-%s unknown", cp
);
126 snprintf (buf
, sizeof(buf
), "%s [+folder] [msg] [switches]",
128 print_help (buf
, switches
, 1);
131 print_version(invo_name
);
217 if (*cp
== '+' || *cp
== '@') {
219 adios (NULL
, "only one folder at a time!");
221 argfolder
= pluspath (cp
);
224 adios (NULL
, "only one (current) message at a time!");
230 if (!context_find ("path"))
231 free (path ("./", TFOLDER
));
232 nmhdir
= concat (m_maildir (""), "/", NULL
);
235 * If we aren't working with the folder stack
236 * (-push, -pop, -list) then the default is to print.
238 if (pushsw
== 0 && popsw
== 0 && listsw
== 0)
241 /* Pushing a folder onto the folder stack */
244 /* If no folder is given, the current folder and */
245 /* the top of the folder stack are swapped. */
246 if ((cp
= context_find (stack
))) {
248 ap
= brkstring (dp
, " ", "\n");
249 argfolder
= getcpy(*ap
++);
251 adios (NULL
, "no other folder");
253 for (cp
= getcpy (getfolder (1)); *ap
; ap
++)
254 cp
= add (*ap
, add (" ", cp
));
256 context_replace (stack
, cp
); /* update folder stack */
258 /* update folder stack */
259 context_replace (stack
,
260 (cp
= context_find (stack
))
261 ? concat (getfolder (1), " ", cp
, NULL
)
262 : getcpy (getfolder (1)));
266 /* Popping a folder off of the folder stack */
269 adios (NULL
, "sorry, no folders allowed with -pop");
270 if ((cp
= context_find (stack
))) {
272 ap
= brkstring (dp
, " ", "\n");
273 argfolder
= getcpy(*ap
++);
275 adios (NULL
, "folder stack empty");
278 /* if there's anything left in the stack */
281 cp
= add (*ap
, add (" ", cp
));
282 context_replace (stack
, cp
); /* update folder stack */
284 context_del (stack
); /* delete folder stack entry from context */
288 if (pushsw
|| popsw
) {
289 cp
= m_maildir(argfolder
);
290 if (access (cp
, F_OK
) == NOTOK
)
291 adios (cp
, "unable to find folder");
292 context_replace (pfolder
, argfolder
); /* update current folder */
293 context_save (); /* save the context file */
297 /* Listing the folder stack */
299 printf ("%s", argfolder
? argfolder
: getfolder (1));
300 if ((cp
= context_find (stack
))) {
302 for (ap
= brkstring (dp
, " ", "\n"); *ap
; ap
++)
312 /* Allocate initial space to record folder information */
313 maxFolderInfo
= CRAWL_NUMFOLDERS
;
314 fi
= mh_xmalloc (maxFolderInfo
* sizeof(*fi
));
319 /* change directory to base of nmh directory for crawl_folders */
320 if (chdir (nmhdir
) == NOTOK
)
321 adios (nmhdir
, "unable to change directory to");
322 if (all
|| ftotal
> 0) {
324 * If no folder is given, do them all
328 admonish (NULL
, "no folder given for message %s", msg
);
329 readonly_folders (); /* do any readonly folders */
330 strncpy (folder
, (cp
= context_find (pfolder
)) ? cp
: "", sizeof(folder
));
331 crawl_folders (".", get_folder_info_callback
, NULL
);
333 strncpy (folder
, argfolder
, sizeof(folder
));
334 if (get_folder_info (argfolder
, msg
)) {
335 context_replace (pfolder
, argfolder
);/* update current folder */
336 context_save (); /* save the context file */
339 * Since recurse wasn't done in get_folder_info(),
340 * we still need to list all level-1 sub-folders.
343 crawl_folders (folder
, get_folder_info_callback
, NULL
);
346 strncpy (folder
, argfolder
? argfolder
: getfolder (1), sizeof(folder
));
349 * Check if folder exists. If not, then see if
350 * we should create it, or just exit.
352 create_folder (m_maildir (folder
), fcreat
, done
);
354 if (get_folder_info (folder
, msg
) && argfolder
) {
355 /* update current folder */
356 context_replace (pfolder
, argfolder
);
361 * Print out folder information
365 context_save (); /* save the context file */
371 get_folder_info_body (char *fold
, char *msg
, boolean
*crawl_children
)
374 struct msgs
*mp
= NULL
;
379 * if necessary, reallocate the space
380 * for folder information
382 if (total_folders
>= maxFolderInfo
) {
383 maxFolderInfo
+= CRAWL_NUMFOLDERS
;
384 fi
= mh_xrealloc (fi
, maxFolderInfo
* sizeof(*fi
));
395 if ((ftotal
> 0) || !fshort
|| msg
|| fpack
) {
397 * create message structure and get folder info
399 if (!(mp
= folder_read (fold
, 1))) {
400 admonish (NULL
, "unable to read folder %s", fold
);
404 /* set the current message */
405 if (msg
&& !sfold (mp
, msg
))
409 if (folder_pack (&mp
, fverb
) == -1)
411 seq_save (mp
); /* synchronize the sequences */
412 context_save (); /* save the context file */
415 /* record info for this folder */
416 if ((ftotal
> 0) || !fshort
) {
417 fi
[i
].nummsg
= mp
->nummsg
;
418 fi
[i
].curmsg
= mp
->curmsg
;
419 fi
[i
].lowmsg
= mp
->lowmsg
;
420 fi
[i
].hghmsg
= mp
->hghmsg
;
421 fi
[i
].others
= other_files (mp
);
424 folder_free (mp
); /* free folder/message structure */
427 *crawl_children
= (frecurse
&& (fshort
|| fi
[i
].others
)
428 && (fi
[i
].error
== 0));
433 get_folder_info_callback (char *fold
, void *baton
)
435 boolean crawl_children
;
438 get_folder_info_body (fold
, NULL
, &crawl_children
);
440 return crawl_children
;
444 get_folder_info (char *fold
, char *msg
)
446 boolean crawl_children
;
449 retval
= get_folder_info_body (fold
, msg
, &crawl_children
);
451 if (crawl_children
) {
452 crawl_folders (fold
, get_folder_info_callback
, NULL
);
459 * Print folder information
465 int i
, len
, hasempty
= 0, curprinted
;
466 int maxlen
= 0, maxnummsg
= 0, maxlowmsg
= 0;
467 int maxhghmsg
= 0, maxcurmsg
= 0, total_msgs
= 0;
468 int nummsgdigits
, lowmsgdigits
;
469 int hghmsgdigits
, curmsgdigits
;
470 char tmpname
[BUFSIZ
];
473 * compute a few values needed to for
474 * printing various fields
476 for (i
= 0; i
< total_folders
; i
++) {
477 /* length of folder name */
478 len
= strlen (fi
[i
].name
);
482 /* If folder has error, skip the rest */
486 /* calculate total number of messages */
487 total_msgs
+= fi
[i
].nummsg
;
489 /* maximum number of messages */
490 if (fi
[i
].nummsg
> maxnummsg
)
491 maxnummsg
= fi
[i
].nummsg
;
493 /* maximum low message */
494 if (fi
[i
].lowmsg
> maxlowmsg
)
495 maxlowmsg
= fi
[i
].lowmsg
;
497 /* maximum high message */
498 if (fi
[i
].hghmsg
> maxhghmsg
)
499 maxhghmsg
= fi
[i
].hghmsg
;
501 /* maximum current message */
502 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&&
503 fi
[i
].curmsg
<= fi
[i
].hghmsg
&&
504 fi
[i
].curmsg
> maxcurmsg
)
505 maxcurmsg
= fi
[i
].curmsg
;
507 /* check for empty folders */
508 if (fi
[i
].nummsg
== 0)
511 nummsgdigits
= num_digits (maxnummsg
);
512 lowmsgdigits
= num_digits (maxlowmsg
);
513 hghmsgdigits
= num_digits (maxhghmsg
);
514 curmsgdigits
= num_digits (maxcurmsg
);
516 if (hasempty
&& nummsgdigits
< 2)
522 if (fheader
> 0 || (all
&& !fshort
&& fheader
>= 0))
523 printf ("%-*s %*s %-*s; %-*s %*s\n",
525 nummsgdigits
+ 13, "# MESSAGES",
526 lowmsgdigits
+ hghmsgdigits
+ 4, " RANGE",
527 curmsgdigits
+ 4, "CUR",
531 * Print folder information
533 if (all
|| fshort
|| ftotal
< 1) {
534 for (i
= 0; i
< total_folders
; i
++) {
536 printf ("%s\n", fi
[i
].name
);
540 /* Add `+' to end of name, if folder is current */
541 if (strcmp (folder
, fi
[i
].name
))
542 snprintf (tmpname
, sizeof(tmpname
), "%s", fi
[i
].name
);
544 snprintf (tmpname
, sizeof(tmpname
), "%s+", fi
[i
].name
);
547 printf ("%-*s is unreadable\n", maxlen
+1, tmpname
);
551 printf ("%-*s ", maxlen
+1, tmpname
);
553 curprinted
= 0; /* remember if we print cur */
554 if (fi
[i
].nummsg
== 0) {
555 printf ("has %*s messages%*s",
557 fi
[i
].others
? lowmsgdigits
+ hghmsgdigits
+ 5 : 0, "");
559 printf ("has %*d message%s (%*d-%*d)",
560 nummsgdigits
, fi
[i
].nummsg
,
561 (fi
[i
].nummsg
== 1) ? " " : "s",
562 lowmsgdigits
, fi
[i
].lowmsg
,
563 hghmsgdigits
, fi
[i
].hghmsg
);
564 if (fi
[i
].curmsg
>= fi
[i
].lowmsg
&& fi
[i
].curmsg
<= fi
[i
].hghmsg
) {
566 printf ("; cur=%*d", curmsgdigits
, fi
[i
].curmsg
);
571 printf (";%*s (others)", curprinted
? 0 : curmsgdigits
+ 6, "");
577 * Print folder/message totals
579 if (ftotal
> 0 || (all
&& !fshort
&& ftotal
>= 0)) {
582 printf ("TOTAL = %d message%c in %d folder%s.\n",
583 total_msgs
, total_msgs
!= 1 ? 's' : ' ',
584 total_folders
, total_folders
!= 1 ? "s" : "");
591 * Set the current message and sychronize sequences
595 sfold (struct msgs
*mp
, char *msg
)
597 /* parse the message range/sequence/name and set SELECTED */
598 if (!m_convert (mp
, msg
))
601 if (mp
->numsel
> 1) {
602 admonish (NULL
, "only one message at a time!");
605 seq_setprev (mp
); /* set the previous-sequence */
606 seq_setcur (mp
, mp
->lowsel
);/* set current message */
607 seq_save (mp
); /* synchronize message sequences */
608 context_save (); /* save the context file */
615 * Do the read only folders
619 readonly_folders (void)
623 register struct node
*np
;
625 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
626 atrlen
= strlen (atrcur
);
628 for (np
= m_defs
; np
; np
= np
->n_next
)
629 if (ssequal (atrcur
, np
->n_name
)
630 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
631 get_folder_info (np
->n_name
+ atrlen
, NULL
);