]> diplodocus.org Git - nmh/blob - uip/folder.c
Escape literal leading full stop in man/new.man.
[nmh] / uip / folder.c
1
2 /*
3 * folder(s).c -- set/list the current message and/or folder
4 * -- push/pop a folder onto/from the folder stack
5 * -- list the folder stack
6 *
7 * This code is Copyright (c) 2002, 2008, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/crawl_folders.h>
14 #include <h/utils.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 int fshort = 0; /* output only folder names */
51 static int fcreat = 0; /* should we ask to create new folders? */
52 static int fpack = 0; /* are we packing the folder? */
53 static int fverb = 0; /* print actions taken while packing folder */
54 static int fheader = 0; /* should we output a header? */
55 static int frecurse = 0; /* recurse through subfolders */
56 static int ftotal = 0; /* should we output the totals? */
57 static int all = 0; /* 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
100 void
101 nonexistent_folder (int status) {
102 NMH_UNUSED (status);
103 adios (NULL, "folder %s does not exist", folder);
104 }
105
106
107 int
108 main (int argc, char **argv)
109 {
110 int printsw = 0, listsw = 0;
111 int pushsw = 0, popsw = 0;
112 char *cp, *dp, *msg = NULL, *argfolder = NULL;
113 char **ap, **argp, buf[BUFSIZ], **arguments;
114
115 if (nmh_init(argv[0], 1)) { return 1; }
116
117 /*
118 * If program was invoked with name ending
119 * in `s', then add switch `-all'.
120 */
121 all = has_suffix_c(argv[0], 's');
122
123 arguments = getarguments (invo_name, argc, argv, 1);
124 argp = arguments;
125
126 while ((cp = *argp++)) {
127 if (*cp == '-') {
128 switch (smatch (++cp, switches)) {
129 case AMBIGSW:
130 ambigsw (cp, switches);
131 done (1);
132 case UNKWNSW:
133 adios (NULL, "-%s unknown", cp);
134
135 case HELPSW:
136 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
137 invo_name);
138 print_help (buf, switches, 1);
139 done (0);
140 case VERSIONSW:
141 print_version(invo_name);
142 done (0);
143
144 case ALLSW:
145 all = 1;
146 continue;
147
148 case NALLSW:
149 all = 0;
150 continue;
151
152 case CREATSW:
153 fcreat = 1;
154 continue;
155 case NCREATSW:
156 fcreat = -1;
157 continue;
158
159 case FASTSW:
160 fshort++;
161 continue;
162 case NFASTSW:
163 fshort = 0;
164 continue;
165
166 case HDRSW:
167 fheader = 1;
168 continue;
169 case NHDRSW:
170 fheader = -1;
171 continue;
172
173 case PACKSW:
174 fpack++;
175 continue;
176 case NPACKSW:
177 fpack = 0;
178 continue;
179
180 case VERBSW:
181 fverb++;
182 continue;
183 case NVERBSW:
184 fverb = 0;
185 continue;
186
187 case RECURSW:
188 frecurse++;
189 continue;
190 case NRECRSW:
191 frecurse = 0;
192 continue;
193
194 case TOTALSW:
195 ftotal = 1;
196 continue;
197 case NTOTLSW:
198 ftotal = -1;
199 continue;
200
201 case PRNTSW:
202 printsw = 1;
203 continue;
204 case NPRNTSW:
205 printsw = 0;
206 continue;
207
208 case LISTSW:
209 listsw = 1;
210 continue;
211 case NLISTSW:
212 listsw = 0;
213 continue;
214
215 case PUSHSW:
216 pushsw = 1;
217 listsw = 1;
218 popsw = 0;
219 continue;
220 case POPSW:
221 popsw = 1;
222 listsw = 1;
223 pushsw = 0;
224 continue;
225 }
226 }
227 if (*cp == '+' || *cp == '@') {
228 if (argfolder)
229 adios (NULL, "only one folder at a time!");
230 else
231 argfolder = pluspath (cp);
232 } else {
233 if (msg)
234 adios (NULL, "only one (current) message at a time!");
235 else
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 == 0 && popsw == 0 && listsw == 0)
249 printsw++;
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 adios (NULL, "no other folder");
262 }
263 for (cp = getcpy (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 : getcpy (getfolder (1)));
273 }
274 }
275
276 /* Popping a folder off of the folder stack */
277 if (popsw) {
278 if (argfolder)
279 adios (NULL, "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 adios (NULL, "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 admonish (NULL, "no folder given for message %s", msg);
339 readonly_folders (); /* do any readonly folders */
340 strncpy (folder, (cp = context_find (pfolder)) ? cp : "", sizeof(folder));
341 crawl_folders (".", get_folder_info_callback, NULL);
342 } else {
343 strncpy (folder, argfolder, sizeof(folder));
344 if (get_folder_info (argfolder, msg)) {
345 context_replace (pfolder, argfolder);/* update current folder */
346 context_save (); /* save the context file */
347 }
348 /*
349 * Since recurse wasn't done in get_folder_info(),
350 * we still need to list all level-1 sub-folders.
351 */
352 if (!frecurse)
353 crawl_folders (folder, get_folder_info_callback, NULL);
354 }
355 } else {
356 strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
357
358 /*
359 * Check if folder exists. If not, then see if
360 * we should create it, or just exit.
361 */
362 create_folder (m_maildir (folder), fcreat, nonexistent_folder);
363
364 if (get_folder_info (folder, msg) && argfolder) {
365 /* update current folder */
366 context_replace (pfolder, argfolder);
367 }
368 }
369
370 /*
371 * Print out folder information
372 */
373 print_folders();
374
375 context_save (); /* save the context file */
376 done (0);
377 return 1;
378 }
379
380 static int
381 get_folder_info_body (char *fold, char *msg, boolean *crawl_children)
382 {
383 int i, retval = 1;
384 struct msgs *mp = NULL;
385
386 i = total_folders++;
387
388 /*
389 * if necessary, reallocate the space
390 * for folder information
391 */
392 if (total_folders >= maxFolderInfo) {
393 maxFolderInfo += CRAWL_NUMFOLDERS;
394 fi = mh_xrealloc (fi, maxFolderInfo * sizeof(*fi));
395 }
396
397 fi[i].name = fold;
398 fi[i].nummsg = 0;
399 fi[i].curmsg = 0;
400 fi[i].lowmsg = 0;
401 fi[i].hghmsg = 0;
402 fi[i].others = 0;
403 fi[i].error = 0;
404
405 if ((ftotal > 0) || !fshort || msg || fpack) {
406 /*
407 * create message structure and get folder info
408 */
409 if (!(mp = folder_read (fold, fpack))) {
410 admonish (NULL, "unable to read folder %s", fold);
411 *crawl_children = FALSE;
412 return 0;
413 }
414
415 /* set the current message */
416 if (msg && !sfold (mp, msg))
417 retval = 0;
418
419 if (fpack) {
420 if (folder_pack (&mp, fverb) == -1) {
421 *crawl_children = FALSE; /* to please clang static analyzer */
422 done (1);
423 }
424 seq_save (mp); /* synchronize the sequences */
425 context_save (); /* save the context file */
426 }
427
428 /* record info for this folder */
429 if ((ftotal > 0) || !fshort) {
430 fi[i].nummsg = mp->nummsg;
431 fi[i].curmsg = mp->curmsg;
432 fi[i].lowmsg = mp->lowmsg;
433 fi[i].hghmsg = mp->hghmsg;
434 fi[i].others = other_files (mp);
435 }
436
437 folder_free (mp); /* free folder/message structure */
438 }
439
440 *crawl_children = (frecurse && (fshort || fi[i].others)
441 && (fi[i].error == 0));
442 return retval;
443 }
444
445 static boolean
446 get_folder_info_callback (char *fold, void *baton)
447 {
448 boolean crawl_children;
449 NMH_UNUSED (baton);
450
451 get_folder_info_body (fold, NULL, &crawl_children);
452 fflush (stdout);
453 return crawl_children;
454 }
455
456 static int
457 get_folder_info (char *fold, char *msg)
458 {
459 boolean crawl_children;
460 int retval;
461
462 retval = get_folder_info_body (fold, msg, &crawl_children);
463
464 if (crawl_children) {
465 crawl_folders (fold, get_folder_info_callback, NULL);
466 }
467
468 return retval;
469 }
470
471 /*
472 * Print folder information
473 */
474
475 static void
476 print_folders (void)
477 {
478 int i, len, hasempty = 0, curprinted;
479 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
480 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
481 int nummsgdigits, lowmsgdigits;
482 int hghmsgdigits, curmsgdigits;
483 char tmpname[BUFSIZ];
484
485 /*
486 * compute a few values needed to for
487 * printing various fields
488 */
489 for (i = 0; i < total_folders; i++) {
490 /* length of folder name */
491 len = strlen (fi[i].name);
492 if (len > maxlen)
493 maxlen = len;
494
495 /* If folder has error, skip the rest */
496 if (fi[i].error)
497 continue;
498
499 /* calculate total number of messages */
500 total_msgs += fi[i].nummsg;
501
502 /* maximum number of messages */
503 if (fi[i].nummsg > maxnummsg)
504 maxnummsg = fi[i].nummsg;
505
506 /* maximum low message */
507 if (fi[i].lowmsg > maxlowmsg)
508 maxlowmsg = fi[i].lowmsg;
509
510 /* maximum high message */
511 if (fi[i].hghmsg > maxhghmsg)
512 maxhghmsg = fi[i].hghmsg;
513
514 /* maximum current message */
515 if (fi[i].curmsg >= fi[i].lowmsg &&
516 fi[i].curmsg <= fi[i].hghmsg &&
517 fi[i].curmsg > maxcurmsg)
518 maxcurmsg = fi[i].curmsg;
519
520 /* check for empty folders */
521 if (fi[i].nummsg == 0)
522 hasempty = 1;
523 }
524 nummsgdigits = num_digits (maxnummsg);
525 lowmsgdigits = num_digits (maxlowmsg);
526 hghmsgdigits = num_digits (maxhghmsg);
527 curmsgdigits = num_digits (maxcurmsg);
528
529 if (hasempty && nummsgdigits < 2)
530 nummsgdigits = 2;
531
532 /*
533 * Print the header
534 */
535 if (fheader > 0 || (all && !fshort && fheader >= 0))
536 printf ("%-*s %*s %-*s; %-*s %*s\n",
537 maxlen+1, "FOLDER",
538 nummsgdigits + 13, "# MESSAGES",
539 lowmsgdigits + hghmsgdigits + 4, " RANGE",
540 curmsgdigits + 4, "CUR",
541 9, "(OTHERS)");
542
543 /*
544 * Print folder information
545 */
546 if (all || fshort || ftotal < 1) {
547 for (i = 0; i < total_folders; i++) {
548 if (fshort) {
549 puts(fi[i].name);
550 continue;
551 }
552
553 /* Add `+' to end of name, if folder is current */
554 if (strcmp (folder, fi[i].name))
555 snprintf (tmpname, sizeof(tmpname), "%s", fi[i].name);
556 else
557 snprintf (tmpname, sizeof(tmpname), "%s+", fi[i].name);
558
559 if (fi[i].error) {
560 printf ("%-*s is unreadable\n", maxlen+1, tmpname);
561 continue;
562 }
563
564 printf ("%-*s ", maxlen+1, tmpname);
565
566 curprinted = 0; /* remember if we print cur */
567 if (fi[i].nummsg == 0) {
568 printf ("has %*s messages%*s",
569 nummsgdigits, "no",
570 fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
571 } else {
572 printf ("has %*d message%s (%*d-%*d)",
573 nummsgdigits, fi[i].nummsg,
574 (fi[i].nummsg == 1) ? " " : "s",
575 lowmsgdigits, fi[i].lowmsg,
576 hghmsgdigits, fi[i].hghmsg);
577 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
578 curprinted = 1;
579 printf ("; cur=%*d", curmsgdigits, fi[i].curmsg);
580 }
581 }
582
583 if (fi[i].others)
584 printf (";%*s (others)", curprinted ? 0 : curmsgdigits + 6, "");
585 puts(".");
586 }
587 }
588
589 /*
590 * Print folder/message totals
591 */
592 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
593 if (all)
594 putchar('\n');
595 printf ("TOTAL = %d message%c in %d folder%s.\n",
596 total_msgs, total_msgs != 1 ? 's' : ' ',
597 total_folders, total_folders != 1 ? "s" : "");
598 }
599
600 fflush (stdout);
601 }
602
603 /*
604 * Set the current message and sychronize sequences
605 */
606
607 static int
608 sfold (struct msgs *mp, char *msg)
609 {
610 /* parse the message range/sequence/name and set SELECTED */
611 if (!m_convert (mp, msg))
612 return 0;
613
614 if (mp->numsel > 1) {
615 admonish (NULL, "only one message at a time!");
616 return 0;
617 }
618 seq_setprev (mp); /* set the previous-sequence */
619 seq_setcur (mp, mp->lowsel);/* set current message */
620 seq_save (mp); /* synchronize message sequences */
621 context_save (); /* save the context file */
622
623 return 1;
624 }
625
626
627 /*
628 * Do the read only folders
629 */
630
631 static void
632 readonly_folders (void)
633 {
634 int atrlen;
635 char atrcur[BUFSIZ];
636 struct node *np;
637
638 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
639 atrlen = strlen (atrcur);
640
641 for (np = m_defs; np; np = np->n_next)
642 if (ssequal (atrcur, np->n_name)
643 && !ssequal (nmhdir, np->n_name + atrlen))
644 get_folder_info (np->n_name + atrlen, NULL);
645 }