2 ** new.c -- as new, list all folders with unseen messages
3 ** -- as fnext, move to next folder with unseen messages
4 ** -- as fprev, move to previous folder with unseen messages
5 ** -- as unseen, scan all unseen messages
6 ** This code is Copyright (c) 2008, by the authors of nmh. See the
7 ** COPYRIGHT file in the root directory of the nmh distribution for
8 ** complete copyright information.
10 ** Inspired by Luke Mewburn's new: http://www.mewburn.net/luke/src/new
14 #include <h/crawl_folders.h>
16 #include <sys/types.h>
24 static struct swit switches[] = {
36 static enum { NEW, FNEXT, FPREV, UNSEEN } run_mode = NEW;
39 ** check_folders uses this to maintain state with both .folders list of
40 ** folders and with crawl_folders.
43 struct node **first, **cur_node;
50 /* Return the number of messages in a string list of message numbers. */
52 count_messages(char *field)
58 field = getcpy(field);
60 /* copied from seq_read.c:seq_init */
61 for (ap = brkstring(field, " ", "\n"); *ap; ap++) {
62 if ((cp = strchr(*ap, '-')))
64 if ((j = m_atoi(*ap)) > 0) {
65 k = cp ? m_atoi(cp) : j;
76 /* Return TRUE if the sequence 'name' is in 'sequences'. */
78 seq_in_list(char *name, char *sequences[])
82 for (i = 0; sequences[i] != NULL; i++) {
83 if (strcmp(name, sequences[i]) == 0) {
92 ** Return the string list of message numbers from the sequences file,
96 get_msgnums(char *folder, char *sequences[])
98 char *seqfile = concat(toabsdir(folder), "/", mh_seq, (void *)NULL);
99 FILE *fp = fopen(seqfile, "r");
101 char name[NAMESZ], field[BUFSIZ];
103 char *msgnums = NULL, *this_msgnums, *old_msgnums;
105 /* no sequences file -> no messages */
110 /* copied from seq_read.c:seq_public */
111 for (state = FLD;;) {
112 switch (state = m_getfld(state, name, field, sizeof(field),
117 if (state == FLDPLUS) {
119 while (state == FLDPLUS) {
120 state = m_getfld(state, name, field,
126 ** Here's where we differ from
127 ** seq_public: if it's in a
128 ** sequence we want, save the list
131 if (seq_in_list(name, sequences)) {
132 this_msgnums = trimcpy(cp);
133 if (msgnums == NULL) {
134 msgnums = this_msgnums;
136 old_msgnums = msgnums;
137 msgnums = concat(old_msgnums, " ", this_msgnums, (void *)NULL);
145 if (seq_in_list(name, sequences)) {
146 this_msgnums = trimcpy(field);
147 if (msgnums == NULL) {
148 msgnums = this_msgnums;
150 old_msgnums = msgnums;
151 msgnums = concat(old_msgnums, " ", this_msgnums, (void *)NULL);
164 adios(EX_DATAERR, NULL, "no blank lines are permitted in %s",
172 adios(EX_SOFTWARE, NULL, "%s is poorly formatted", seqfile);
174 break; /* break from for loop */
183 ** Check `folder' (of length `len') for interesting messages,
184 ** filling in the list in `b'.
187 check_folder(char *folder, size_t len, struct list_state *b)
189 char *msgnums = get_msgnums(folder, b->sequences);
190 int is_cur = strcmp(folder, b->cur) == 0;
192 if (is_cur || msgnums != NULL) {
193 if (*b->first == NULL) {
194 *b->first = b->node = mh_xmalloc(sizeof(*b->node));
196 b->node->n_next = mh_xmalloc(sizeof(*b->node));
197 b->node = b->node->n_next;
199 b->node->n_name = folder;
200 b->node->n_field = msgnums;
202 if (*b->maxlen < len) {
207 /* Save the node for the current folder, so we can fall back to it. */
209 *b->cur_node = b->node;
214 crawl_callback(char *folder, void *baton)
216 check_folder(folder, strlen(folder), baton);
221 ** Scan folders, returning:
222 ** first -- list of nodes for all folders which have desired messages;
223 ** if the current folder is listed in .folders, it is also in
224 ** the list regardless of whether it has any desired messages
225 ** last -- last node in list
226 ** cur_node -- node of current folder, if listed in .folders
227 ** maxlen -- length of longest folder name
229 ** `cur' points to the name of the current folder, `folders' points to the
230 ** name of a .folder (if NULL, crawl all folders), and `sequences' points to
231 ** the array of sequences for which to look.
233 ** An empty list is returned as first=last=NULL.
236 check_folders(struct node **first, struct node **last,
237 struct node **cur_node, size_t *maxlen,
238 char *cur, char *folders, char *sequences[])
245 *first = *last = *cur_node = NULL;
249 b.cur_node = cur_node;
252 b.sequences = sequences;
254 if (folders == NULL) {
255 chdir(toabsdir("+"));
256 crawl_folders(".", crawl_callback, &b);
258 fp = fopen(folders, "r");
260 adios(EX_IOERR, NULL, "failed to read %s", folders);
262 while (vfgets(fp, &line) == OK) {
263 len = strlen(line) - 1;
265 check_folder(getcpy(line), len, &b);
270 if (*first != NULL) {
271 b.node->n_next = NULL;
276 /* Return a single string of the `sequences' joined by a space (' '). */
278 join_sequences(char *sequences[])
284 for (i = 0; sequences[i] != NULL; i++) {
285 len += strlen(sequences[i]) + 1;
287 result = mh_xmalloc(len + 1);
289 for (i = 0, cp = result; sequences[i] != NULL; i++, cp += len + 1) {
290 len = strlen(sequences[i]);
291 memcpy(cp, sequences[i], len);
294 /* -1 to overwrite the last delimiter */
301 ** Return a struct node for the folder to change to. This is the next
302 ** (previous, if FPREV mode) folder with desired messages, or the current
303 ** folder if no folders have desired. If NEW or UNSEEN mode, print the
304 ** output but don't change folders.
306 ** n_name is the folder to change to, and n_field is the string list of
307 ** desired message numbers.
310 doit(char *cur, char *folders, char *sequences[])
312 struct node *first, *cur_node, *node, *last = NULL, *prev;
314 int count, total = 0;
315 char *sequences_s = NULL;
321 if (cur == NULL || cur[0] == '\0') {
325 check_folders(&first, &last, &cur_node, &folder_len, cur,
328 if (run_mode == FNEXT || run_mode == FPREV) {
330 /* No folders at all... */
332 } else if (first->n_next == NULL) {
334 ** We have only one node; any desired messages in it?
336 if (first->n_field == NULL) {
341 } else if (cur_node == NULL) {
343 ** Current folder is not listed in .folders,
348 } else if (run_mode == UNSEEN) {
349 sequences_s = join_sequences(sequences);
352 for (node = first, prev = NULL;
354 prev = node, node = node->n_next) {
355 if (run_mode == FNEXT) {
357 ** If we have a previous node and it is the current
358 ** folder, return this node.
360 if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
363 } else if (run_mode == FPREV) {
364 if (strcmp(node->n_name, cur) == 0) {
366 ** Found current folder in fprev mode;
367 ** if we have a previous node in the list,
368 ** return it; else return the last node.
375 } else if (run_mode == UNSEEN) {
376 if (node->n_field == NULL) {
380 printf("\n%d %s messages in %s",
381 count_messages(node->n_field),
384 if (strcmp(node->n_name, cur) == 0) {
385 puts(" (*: current folder)");
392 argv[argc++] = "scan";
393 snprintf(buf, sizeof buf, "+%s", node->n_name);
395 for (seqp=sequences; *seqp; seqp++) {
396 argv[argc++] = *seqp;
398 argv[argc] = (char *)NULL;
399 execprog(*argv, argv);
401 if (node->n_field == NULL) {
405 count = count_messages(node->n_field);
408 printf("%-*s %6d.%c %s\n", (int) folder_len,
410 (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
416 ** If we're fnext, we haven't checked the last node yet. If it's the
417 ** current folder, return the first node.
419 if (run_mode == FNEXT && strcmp(last->n_name, cur) == 0) {
423 if (run_mode == NEW) {
424 printf("%-*s %6d.\n", (int) folder_len, " total", total);
431 main(int argc, char **argv)
433 char **ap, *cp, **argp, **arguments;
435 char *folders = NULL;
436 char *sequences[NUMATTRS + 1];
444 setlocale(LC_ALL, "");
445 invo_name = mhbasename(argv[0]);
447 /* read user profile/context */
450 arguments = getarguments(invo_name, argc, argv, 1);
456 while ((cp = *argp++)) {
458 switch (smatch(++cp, switches)) {
460 ambigsw(cp, switches);
463 adios(EX_USAGE, NULL, "-%s unknown", cp);
466 snprintf(help, sizeof(help),
467 "%s [switches] [sequences]",
469 print_help(help, switches, 1);
470 exit(argc == 2 ? EX_OK : EX_USAGE);
472 print_version(invo_name);
473 exit(argc == 2 ? EX_OK : EX_USAGE);
476 if (!(folders = *argp++) || *folders == '-')
477 adios(EX_USAGE, NULL, "missing argument to %s",
481 if (!(invo_name = *argp++) || *invo_name == '-')
482 adios(EX_USAGE, NULL, "missing argument to %s",
484 invo_name = mhbasename(invo_name);
488 /* have a sequence argument */
489 if (!seq_in_list(cp, sequences)) {
495 if (strcmp(invo_name, "fnext") == 0) {
497 } else if (strcmp(invo_name, "fprev") == 0) {
499 } else if (strcmp(invo_name, "unseen") == 0) {
503 if (folders == NULL) {
506 if (folders[0] != '/') {
507 folders = toabsdir(folders);
513 /* no sequence arguments; use unseen */
514 if ((unseen = context_find(usequence))) {
516 adios(EX_CONFIG, NULL, "profile entry %s set, but empty, and no sequences given", usequence);
519 unseen = seq_unseen; /* use default */
522 for (ap = brkstring(dp, " ", "\n"); *ap; ap++) {
523 sequences[i++] = *ap;
528 folder = doit(context_find(curfolder), folders, sequences);
529 if (folder == NULL) {
534 if (run_mode == UNSEEN) {
536 ** All the scan(1)s it runs change the current folder, so we
537 ** need to put it back. Unfortunately, context_replace lamely
538 ** ignores the new value you give it if it is the same one it
539 ** has in memory. So, we'll be lame, too. I'm not sure if i
540 ** should just change context_replace...
542 context_replace(curfolder, "defeat_context_replace_optimization");
545 /* update current folder */
546 context_replace(curfolder, folder->n_name);
548 if (run_mode == FNEXT || run_mode == FPREV) {
549 printf("%s %s\n", folder->n_name, folder->n_field);