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