]> diplodocus.org Git - nmh/blob - uip/folder.c
h/prototypes.h: Remove scan_reset_m_getfld_state() prototype.
[nmh] / uip / folder.c
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
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <h/crawl_folders.h>
12 #include <h/utils.h>
13
14 #define FOLDER_SWITCHES \
15 X("all", 0, ALLSW) \
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) \
36 X("pop", 0, POPSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
39
40 #define X(sw, minchars, id) id,
41 DEFINE_SWITCH_ENUM(FOLDER);
42 #undef X
43
44 #define X(sw, minchars, id) { sw, minchars, id },
45 DEFINE_SWITCH_ARRAY(FOLDER, switches);
46 #undef X
47
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 */
56
57 static int total_folders = 0; /* total number of folders */
58
59 static char *nmhdir;
60 static char *stack = "Folder-Stack";
61 static char folder[BUFSIZ];
62
63 /*
64 * Structure to hold information about
65 * folders as we scan them.
66 */
67 struct FolderInfo {
68 char *name;
69 int nummsg;
70 int curmsg;
71 int lowmsg;
72 int hghmsg;
73 int others; /* others == 1 if other files in folder */
74 int error; /* error == 1 for unreadable folder */
75 };
76
77 /*
78 * Dynamically allocated space to hold
79 * all the folder information.
80 */
81 static struct FolderInfo *fi;
82 static int maxFolderInfo;
83
84 /*
85 * static prototypes
86 */
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);
92
93 /*
94 * Function for printing error message if folder does not exist with
95 * -nocreate.
96 */
97 static
98 void
99 nonexistent_folder (int status) {
100 NMH_UNUSED (status);
101 adios (NULL, "folder %s does not exist", folder);
102 }
103
104
105 int
106 main (int argc, char **argv)
107 {
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;
112
113 if (nmh_init(argv[0], 1)) { return 1; }
114
115 /*
116 * If program was invoked with name ending
117 * in `s', then add switch `-all'.
118 */
119 all = has_suffix_c(argv[0], 's');
120
121 arguments = getarguments (invo_name, argc, argv, 1);
122 argp = arguments;
123
124 while ((cp = *argp++)) {
125 if (*cp == '-') {
126 switch (smatch (++cp, switches)) {
127 case AMBIGSW:
128 ambigsw (cp, switches);
129 done (1);
130 case UNKWNSW:
131 adios (NULL, "-%s unknown", cp);
132
133 case HELPSW:
134 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
135 invo_name);
136 print_help (buf, switches, 1);
137 done (0);
138 case VERSIONSW:
139 print_version(invo_name);
140 done (0);
141
142 case ALLSW:
143 all = 1;
144 continue;
145
146 case NALLSW:
147 all = 0;
148 continue;
149
150 case CREATSW:
151 fcreat = 1;
152 continue;
153 case NCREATSW:
154 fcreat = -1;
155 continue;
156
157 case FASTSW:
158 fshort++;
159 continue;
160 case NFASTSW:
161 fshort = 0;
162 continue;
163
164 case HDRSW:
165 fheader = 1;
166 continue;
167 case NHDRSW:
168 fheader = -1;
169 continue;
170
171 case PACKSW:
172 fpack++;
173 continue;
174 case NPACKSW:
175 fpack = 0;
176 continue;
177
178 case VERBSW:
179 fverb++;
180 continue;
181 case NVERBSW:
182 fverb = 0;
183 continue;
184
185 case RECURSW:
186 frecurse++;
187 continue;
188 case NRECRSW:
189 frecurse = 0;
190 continue;
191
192 case TOTALSW:
193 ftotal = 1;
194 continue;
195 case NTOTLSW:
196 ftotal = -1;
197 continue;
198
199 case PRNTSW:
200 printsw = 1;
201 continue;
202 case NPRNTSW:
203 printsw = 0;
204 continue;
205
206 case LISTSW:
207 listsw = 1;
208 continue;
209 case NLISTSW:
210 listsw = 0;
211 continue;
212
213 case PUSHSW:
214 pushsw = 1;
215 listsw = 1;
216 popsw = 0;
217 continue;
218 case POPSW:
219 popsw = 1;
220 listsw = 1;
221 pushsw = 0;
222 continue;
223 }
224 }
225 if (*cp == '+' || *cp == '@') {
226 if (argfolder)
227 adios (NULL, "only one folder at a time!");
228 else
229 argfolder = pluspath (cp);
230 } else {
231 if (msg)
232 adios (NULL, "only one (current) message at a time!");
233 else
234 msg = cp;
235 }
236 }
237
238 if (!context_find ("path"))
239 free (path ("./", TFOLDER));
240 nmhdir = concat (m_maildir (""), "/", NULL);
241
242 /*
243 * If we aren't working with the folder stack
244 * (-push, -pop, -list) then the default is to print.
245 */
246 if (pushsw == 0 && popsw == 0 && listsw == 0)
247 printsw++;
248
249 /* Pushing a folder onto the folder stack */
250 if (pushsw) {
251 if (!argfolder) {
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))) {
255 dp = mh_xstrdup(cp);
256 ap = brkstring (dp, " ", "\n");
257 argfolder = getcpy(*ap++);
258 } else {
259 adios (NULL, "no other folder");
260 }
261 for (cp = getcpy (getfolder (1)); *ap; ap++)
262 cp = add (*ap, add (" ", cp));
263 free (dp);
264 context_replace (stack, cp); /* update folder stack */
265 } else {
266 /* update folder stack */
267 context_replace (stack,
268 (cp = context_find (stack))
269 ? concat (getfolder (1), " ", cp, NULL)
270 : getcpy (getfolder (1)));
271 }
272 }
273
274 /* Popping a folder off of the folder stack */
275 if (popsw) {
276 if (argfolder)
277 adios (NULL, "sorry, no folders allowed with -pop");
278 if ((cp = context_find (stack))) {
279 dp = mh_xstrdup(cp);
280 ap = brkstring (dp, " ", "\n");
281 argfolder = getcpy(*ap++);
282 } else {
283 adios (NULL, "folder stack empty");
284 }
285 if (*ap) {
286 /* if there's anything left in the stack */
287 cp = getcpy (*ap++);
288 for (; *ap; ap++)
289 cp = add (*ap, add (" ", cp));
290 context_replace (stack, cp); /* update folder stack */
291 } else {
292 context_del (stack); /* delete folder stack entry from context */
293 }
294 free (dp);
295 }
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 */
302 argfolder = NULL;
303 }
304
305 /* Listing the folder stack */
306 if (listsw) {
307 fputs(argfolder ? argfolder : getfolder (1), stdout);
308 if ((cp = context_find (stack))) {
309 dp = mh_xstrdup(cp);
310 for (ap = brkstring (dp, " ", "\n"); *ap; ap++)
311 printf (" %s", *ap);
312 free (dp);
313 }
314 putchar('\n');
315
316 if (!printsw)
317 done (0);
318 }
319
320 /* Allocate initial space to record folder information */
321 maxFolderInfo = CRAWL_NUMFOLDERS;
322 fi = mh_xmalloc (maxFolderInfo * sizeof(*fi));
323
324 /*
325 * Scan the folders
326 */
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) {
331 /*
332 * If no folder is given, do them all
333 */
334 if (!argfolder) {
335 if (msg)
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);
341 } else {
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 */
346 }
347 /*
348 * Since recurse wasn't done in get_folder_info(),
349 * we still need to list all level-1 sub-folders.
350 */
351 if (!frecurse)
352 crawl_folders (folder, get_folder_info_callback, NULL);
353 }
354 } else {
355 strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
356
357 /*
358 * Check if folder exists. If not, then see if
359 * we should create it, or just exit.
360 */
361 create_folder (m_maildir (folder), fcreat, nonexistent_folder);
362
363 if (get_folder_info (folder, msg) && argfolder) {
364 /* update current folder */
365 context_replace (pfolder, argfolder);
366 }
367 }
368
369 /*
370 * Print out folder information
371 */
372 print_folders();
373
374 context_save (); /* save the context file */
375 done (0);
376 return 1;
377 }
378
379 static int
380 get_folder_info_body (char *fold, char *msg, boolean *crawl_children)
381 {
382 int i, retval = 1;
383 struct msgs *mp = NULL;
384
385 i = total_folders++;
386
387 /*
388 * if necessary, reallocate the space
389 * for folder information
390 */
391 if (total_folders >= maxFolderInfo) {
392 maxFolderInfo += CRAWL_NUMFOLDERS;
393 fi = mh_xrealloc (fi, maxFolderInfo * sizeof(*fi));
394 }
395
396 fi[i].name = fold;
397 fi[i].nummsg = 0;
398 fi[i].curmsg = 0;
399 fi[i].lowmsg = 0;
400 fi[i].hghmsg = 0;
401 fi[i].others = 0;
402 fi[i].error = 0;
403
404 if ((ftotal > 0) || !fshort || msg || fpack) {
405 /*
406 * create message structure and get folder info
407 */
408 if (!(mp = folder_read (fold, fpack))) {
409 inform("unable to read folder %s, continuing...", fold);
410 *crawl_children = FALSE;
411 return 0;
412 }
413
414 /* set the current message */
415 if (msg && !sfold (mp, msg))
416 retval = 0;
417
418 if (fpack) {
419 if (folder_pack (&mp, fverb) == -1) {
420 *crawl_children = FALSE; /* to please clang static analyzer */
421 done (1);
422 }
423 seq_save (mp); /* synchronize the sequences */
424 context_save (); /* save the context file */
425 }
426
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);
434 }
435
436 folder_free (mp); /* free folder/message structure */
437 }
438
439 *crawl_children = (frecurse && (fshort || fi[i].others)
440 && (fi[i].error == 0));
441 return retval;
442 }
443
444 static boolean
445 get_folder_info_callback (char *fold, void *baton)
446 {
447 boolean crawl_children;
448 NMH_UNUSED (baton);
449
450 get_folder_info_body (fold, NULL, &crawl_children);
451 fflush (stdout);
452 return crawl_children;
453 }
454
455 static int
456 get_folder_info (char *fold, char *msg)
457 {
458 boolean crawl_children;
459 int retval;
460
461 retval = get_folder_info_body (fold, msg, &crawl_children);
462
463 if (crawl_children) {
464 crawl_folders (fold, get_folder_info_callback, NULL);
465 }
466
467 return retval;
468 }
469
470 /*
471 * Print folder information
472 */
473
474 static void
475 print_folders (void)
476 {
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];
483
484 /*
485 * compute a few values needed to for
486 * printing various fields
487 */
488 for (i = 0; i < total_folders; i++) {
489 /* length of folder name */
490 len = strlen (fi[i].name);
491 if (len > maxlen)
492 maxlen = len;
493
494 /* If folder has error, skip the rest */
495 if (fi[i].error)
496 continue;
497
498 /* calculate total number of messages */
499 total_msgs += fi[i].nummsg;
500
501 /* maximum number of messages */
502 if (fi[i].nummsg > maxnummsg)
503 maxnummsg = fi[i].nummsg;
504
505 /* maximum low message */
506 if (fi[i].lowmsg > maxlowmsg)
507 maxlowmsg = fi[i].lowmsg;
508
509 /* maximum high message */
510 if (fi[i].hghmsg > maxhghmsg)
511 maxhghmsg = fi[i].hghmsg;
512
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;
518
519 /* check for empty folders */
520 if (fi[i].nummsg == 0)
521 hasempty = 1;
522 }
523 nummsgdigits = num_digits (maxnummsg);
524 lowmsgdigits = num_digits (maxlowmsg);
525 hghmsgdigits = num_digits (maxhghmsg);
526 curmsgdigits = num_digits (maxcurmsg);
527
528 if (hasempty && nummsgdigits < 2)
529 nummsgdigits = 2;
530
531 /*
532 * Print the header
533 */
534 if (fheader > 0 || (all && !fshort && fheader >= 0))
535 printf ("%-*s %*s %-*s; %-*s %*s\n",
536 maxlen+1, "FOLDER",
537 nummsgdigits + 13, "# MESSAGES",
538 lowmsgdigits + hghmsgdigits + 4, " RANGE",
539 curmsgdigits + 4, "CUR",
540 9, "(OTHERS)");
541
542 /*
543 * Print folder information
544 */
545 if (all || fshort || ftotal < 1) {
546 for (i = 0; i < total_folders; i++) {
547 if (fshort) {
548 puts(fi[i].name);
549 continue;
550 }
551
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);
555 else
556 snprintf (tmpname, sizeof(tmpname), "%s+", fi[i].name);
557
558 if (fi[i].error) {
559 printf ("%-*s is unreadable\n", maxlen+1, tmpname);
560 continue;
561 }
562
563 printf ("%-*s ", maxlen+1, tmpname);
564
565 curprinted = 0; /* remember if we print cur */
566 if (fi[i].nummsg == 0) {
567 printf ("has %*s messages%*s",
568 nummsgdigits, "no",
569 fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
570 } else {
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) {
577 curprinted = 1;
578 printf ("; cur=%*d", curmsgdigits, fi[i].curmsg);
579 }
580 }
581
582 if (fi[i].others)
583 printf (";%*s (others)", curprinted ? 0 : curmsgdigits + 6, "");
584 puts(".");
585 }
586 }
587
588 /*
589 * Print folder/message totals
590 */
591 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
592 if (all)
593 putchar('\n');
594 printf ("TOTAL = %d message%s in %d folder%s.\n",
595 total_msgs, PLURALS(total_msgs),
596 total_folders, PLURALS(total_folders));
597 }
598
599 fflush (stdout);
600 }
601
602 /*
603 * Set the current message and synchronize sequences
604 */
605
606 static int
607 sfold (struct msgs *mp, char *msg)
608 {
609 /* parse the message range/sequence/name and set SELECTED */
610 if (!m_convert (mp, msg))
611 return 0;
612
613 if (mp->numsel > 1) {
614 inform("only one message at a time!, continuing...");
615 return 0;
616 }
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 */
621
622 return 1;
623 }
624
625
626 /*
627 * Do the read only folders
628 */
629
630 static void
631 readonly_folders (void)
632 {
633 int atrlen;
634 char atrcur[BUFSIZ];
635 struct node *np;
636
637 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
638 atrlen = strlen (atrcur);
639
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);
644 }