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