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