]> diplodocus.org Git - nmh/blob - uip/folder.c
Simplified m_strn() per Ralph's suggestions.
[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 else
230 argfolder = pluspath (cp);
231 } else {
232 if (msg)
233 adios (NULL, "only one (current) message at a time!");
234 else
235 msg = cp;
236 }
237 }
238
239 if (!context_find ("path"))
240 free (path ("./", TFOLDER));
241 nmhdir = concat (m_maildir (""), "/", NULL);
242
243 /*
244 * If we aren't working with the folder stack
245 * (-push, -pop, -list) then the default is to print.
246 */
247 if (pushsw == 0 && popsw == 0 && listsw == 0)
248 printsw++;
249
250 /* Pushing a folder onto the folder stack */
251 if (pushsw) {
252 if (!argfolder) {
253 /* If no folder is given, the current folder and */
254 /* the top of the folder stack are swapped. */
255 if ((cp = context_find (stack))) {
256 dp = mh_xstrdup(cp);
257 ap = brkstring (dp, " ", "\n");
258 argfolder = getcpy(*ap++);
259 } else {
260 adios (NULL, "no other folder");
261 }
262 for (cp = getcpy (getfolder (1)); *ap; ap++)
263 cp = add (*ap, add (" ", cp));
264 free (dp);
265 context_replace (stack, cp); /* update folder stack */
266 } else {
267 /* update folder stack */
268 context_replace (stack,
269 (cp = context_find (stack))
270 ? concat (getfolder (1), " ", cp, NULL)
271 : getcpy (getfolder (1)));
272 }
273 }
274
275 /* Popping a folder off of the folder stack */
276 if (popsw) {
277 if (argfolder)
278 adios (NULL, "sorry, no folders allowed with -pop");
279 if ((cp = context_find (stack))) {
280 dp = mh_xstrdup(cp);
281 ap = brkstring (dp, " ", "\n");
282 argfolder = getcpy(*ap++);
283 } else {
284 adios (NULL, "folder stack empty");
285 }
286 if (*ap) {
287 /* if there's anything left in the stack */
288 cp = getcpy (*ap++);
289 for (; *ap; ap++)
290 cp = add (*ap, add (" ", cp));
291 context_replace (stack, cp); /* update folder stack */
292 } else {
293 context_del (stack); /* delete folder stack entry from context */
294 }
295 free (dp);
296 }
297 if (pushsw || popsw) {
298 cp = m_maildir(argfolder);
299 if (access (cp, F_OK) == NOTOK)
300 adios (cp, "unable to find folder");
301 context_replace (pfolder, argfolder); /* update current folder */
302 context_save (); /* save the context file */
303 argfolder = NULL;
304 }
305
306 /* Listing the folder stack */
307 if (listsw) {
308 fputs(argfolder ? argfolder : getfolder (1), stdout);
309 if ((cp = context_find (stack))) {
310 dp = mh_xstrdup(cp);
311 for (ap = brkstring (dp, " ", "\n"); *ap; ap++)
312 printf (" %s", *ap);
313 free (dp);
314 }
315 putchar('\n');
316
317 if (!printsw)
318 done (0);
319 }
320
321 /* Allocate initial space to record folder information */
322 maxFolderInfo = CRAWL_NUMFOLDERS;
323 fi = mh_xmalloc (maxFolderInfo * sizeof(*fi));
324
325 /*
326 * Scan the folders
327 */
328 /* change directory to base of nmh directory for crawl_folders */
329 if (chdir (nmhdir) == NOTOK)
330 adios (nmhdir, "unable to change directory to");
331 if (all || ftotal > 0) {
332 /*
333 * If no folder is given, do them all
334 */
335 if (!argfolder) {
336 if (msg)
337 inform("no folder given for message %s, continuing...", msg);
338 readonly_folders (); /* do any readonly folders */
339 cp = context_find(pfolder);
340 strncpy (folder, FENDNULL(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 inform("unable to read folder %s, continuing...", 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%1s (%*d-%*d)",
573 nummsgdigits, fi[i].nummsg,
574 PLURALS(fi[i].nummsg),
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%s in %d folder%s.\n",
596 total_msgs, PLURALS(total_msgs),
597 total_folders, PLURALS(total_folders));
598 }
599
600 fflush (stdout);
601 }
602
603 /*
604 * Set the current message and synchronize 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 inform("only one message at a time!, continuing...");
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 }