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