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