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