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