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