]> diplodocus.org Git - nmh/blob - uip/folder.c
Bring these changes over from the branch.
[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 * $Id$
8 *
9 * This code is Copyright (c) 2002, by the authors of nmh. See the
10 * COPYRIGHT file in the root directory of the nmh distribution for
11 * complete copyright information.
12 */
13
14 #include <h/mh.h>
15 #include <errno.h>
16
17 static struct swit switches[] = {
18 #define ALLSW 0
19 { "all", 0 },
20 #define NALLSW 1
21 { "noall", 0 },
22 #define CREATSW 2
23 { "create", 0 },
24 #define NCREATSW 3
25 { "nocreate", 0 },
26 #define FASTSW 4
27 { "fast", 0 },
28 #define NFASTSW 5
29 { "nofast", 0 },
30 #define HDRSW 6
31 { "header", 0 },
32 #define NHDRSW 7
33 { "noheader", 0 },
34 #define PACKSW 8
35 { "pack", 0 },
36 #define NPACKSW 9
37 { "nopack", 0 },
38 #define VERBSW 10
39 { "verbose", 0 },
40 #define NVERBSW 11
41 { "noverbose", 0 },
42 #define RECURSW 12
43 { "recurse", 0 },
44 #define NRECRSW 13
45 { "norecurse", 0 },
46 #define TOTALSW 14
47 { "total", 0 },
48 #define NTOTLSW 15
49 { "nototal", 0 },
50 #define LISTSW 16
51 { "list", 0 },
52 #define NLISTSW 17
53 { "nolist", 0 },
54 #define PRNTSW 18
55 { "print", 0 },
56 #define NPRNTSW 19
57 { "noprint", -4 },
58 #define PUSHSW 20
59 { "push", 0 },
60 #define POPSW 21
61 { "pop", 0 },
62 #define VERSIONSW 22
63 { "version", 0 },
64 #define HELPSW 23
65 { "help", 0 },
66 { NULL, 0 }
67 };
68
69 extern int errno;
70
71 static int fshort = 0; /* output only folder names */
72 static int fcreat = 0; /* should we ask to create new folders? */
73 static int fpack = 0; /* are we packing the folder? */
74 static int fverb = 0; /* print actions taken while packing folder */
75 static int fheader = 0; /* should we output a header? */
76 static int frecurse = 0; /* recurse through subfolders */
77 static int ftotal = 0; /* should we output the totals? */
78 static int all = 0; /* should we output all folders */
79
80 static int total_folders = 0; /* total number of folders */
81
82 static int start = 0;
83 static int foldp = 0;
84
85 static char *nmhdir;
86 static char *stack = "Folder-Stack";
87 static char folder[BUFSIZ];
88
89 #define NUMFOLDERS 100
90
91 /*
92 * This is how many folders we currently can hold in the array
93 * `folds'. We increase this amount by NUMFOLDERS at a time.
94 */
95 static int maxfolders;
96 static char **folds;
97
98 /*
99 * Structure to hold information about
100 * folders as we scan them.
101 */
102 struct FolderInfo {
103 char *name;
104 int nummsg;
105 int curmsg;
106 int lowmsg;
107 int hghmsg;
108 int others; /* others == 1 if other files in folder */
109 int error; /* error == 1 for unreadable folder */
110 };
111
112 /*
113 * Dynamically allocated space to hold
114 * all the folder information.
115 */
116 static struct FolderInfo *fi;
117 static int maxFolderInfo;
118
119 /*
120 * static prototypes
121 */
122 static void dodir (char *);
123 static int get_folder_info (char *, char *);
124 static void print_folders (void);
125 static int num_digits (int);
126 static int sfold (struct msgs *, char *);
127 static void addir (char *);
128 static void addfold (char *);
129 static int compare (char *, char *);
130 static void readonly_folders (void);
131
132
133 int
134 main (int argc, char **argv)
135 {
136 int printsw = 0, listsw = 0;
137 int pushsw = 0, popsw = 0;
138 char *cp, *dp, *msg = NULL, *argfolder = NULL;
139 char **ap, **argp, buf[BUFSIZ], **arguments;
140 struct stat st;
141
142 #ifdef LOCALE
143 setlocale(LC_ALL, "");
144 #endif
145 invo_name = r1bindex (argv[0], '/');
146
147 /* read user profile/context */
148 context_read();
149
150 /*
151 * If program was invoked with name ending
152 * in `s', then add switch `-all'.
153 */
154 if (argv[0][strlen (argv[0]) - 1] == 's')
155 all = 1;
156
157 arguments = getarguments (invo_name, argc, argv, 1);
158 argp = arguments;
159
160 while ((cp = *argp++)) {
161 if (*cp == '-') {
162 switch (smatch (++cp, switches)) {
163 case AMBIGSW:
164 ambigsw (cp, switches);
165 done (1);
166 case UNKWNSW:
167 adios (NULL, "-%s unknown", cp);
168
169 case HELPSW:
170 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
171 invo_name);
172 print_help (buf, switches, 1);
173 done (1);
174 case VERSIONSW:
175 print_version(invo_name);
176 done (1);
177
178 case ALLSW:
179 all = 1;
180 continue;
181
182 case NALLSW:
183 all = 0;
184 continue;
185
186 case CREATSW:
187 fcreat = 1;
188 continue;
189 case NCREATSW:
190 fcreat = -1;
191 continue;
192
193 case FASTSW:
194 fshort++;
195 continue;
196 case NFASTSW:
197 fshort = 0;
198 continue;
199
200 case HDRSW:
201 fheader = 1;
202 continue;
203 case NHDRSW:
204 fheader = -1;
205 continue;
206
207 case PACKSW:
208 fpack++;
209 continue;
210 case NPACKSW:
211 fpack = 0;
212 continue;
213
214 case VERBSW:
215 fverb++;
216 continue;
217 case NVERBSW:
218 fverb = 0;
219 continue;
220
221 case RECURSW:
222 frecurse++;
223 continue;
224 case NRECRSW:
225 frecurse = 0;
226 continue;
227
228 case TOTALSW:
229 ftotal = 1;
230 continue;
231 case NTOTLSW:
232 ftotal = -1;
233 continue;
234
235 case PRNTSW:
236 printsw = 1;
237 continue;
238 case NPRNTSW:
239 printsw = 0;
240 continue;
241
242 case LISTSW:
243 listsw = 1;
244 continue;
245 case NLISTSW:
246 listsw = 0;
247 continue;
248
249 case PUSHSW:
250 pushsw = 1;
251 listsw = 1;
252 popsw = 0;
253 continue;
254 case POPSW:
255 popsw = 1;
256 listsw = 1;
257 pushsw = 0;
258 continue;
259 }
260 }
261 if (*cp == '+' || *cp == '@') {
262 if (argfolder)
263 adios (NULL, "only one folder at a time!");
264 else
265 argfolder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
266 } else {
267 if (msg)
268 adios (NULL, "only one (current) message at a time!");
269 else
270 msg = cp;
271 }
272 }
273
274 if (!context_find ("path"))
275 free (path ("./", TFOLDER));
276 nmhdir = concat (m_maildir (""), "/", NULL);
277
278 /*
279 * If we aren't working with the folder stack
280 * (-push, -pop, -list) then the default is to print.
281 */
282 if (pushsw == 0 && popsw == 0 && listsw == 0)
283 printsw++;
284
285 /* Pushing a folder onto the folder stack */
286 if (pushsw) {
287 if (!argfolder) {
288 /* If no folder is given, the current folder and */
289 /* the top of the folder stack are swapped. */
290 if ((cp = context_find (stack))) {
291 dp = getcpy (cp);
292 ap = brkstring (dp, " ", "\n");
293 argfolder = getcpy(*ap++);
294 } else {
295 adios (NULL, "no other folder");
296 }
297 for (cp = getcpy (getfolder (1)); *ap; ap++)
298 cp = add (*ap, add (" ", cp));
299 free (dp);
300 context_replace (stack, cp); /* update folder stack */
301 } else {
302 /* update folder stack */
303 context_replace (stack,
304 (cp = context_find (stack))
305 ? concat (getfolder (1), " ", cp, NULL)
306 : getcpy (getfolder (1)));
307 }
308 }
309
310 /* Popping a folder off of the folder stack */
311 if (popsw) {
312 if (argfolder)
313 adios (NULL, "sorry, no folders allowed with -pop");
314 if ((cp = context_find (stack))) {
315 dp = getcpy (cp);
316 ap = brkstring (dp, " ", "\n");
317 argfolder = getcpy(*ap++);
318 } else {
319 adios (NULL, "folder stack empty");
320 }
321 if (*ap) {
322 /* if there's anything left in the stack */
323 cp = getcpy (*ap++);
324 for (; *ap; ap++)
325 cp = add (*ap, add (" ", cp));
326 context_replace (stack, cp); /* update folder stack */
327 } else {
328 context_del (stack); /* delete folder stack entry from context */
329 }
330 free (dp);
331 }
332 if (pushsw || popsw) {
333 cp = m_maildir(argfolder);
334 if (access (cp, F_OK) == NOTOK)
335 adios (cp, "unable to find folder");
336 context_replace (pfolder, argfolder); /* update current folder */
337 context_save (); /* save the context file */
338 argfolder = NULL;
339 }
340
341 /* Listing the folder stack */
342 if (listsw) {
343 printf ("%s", argfolder ? argfolder : getfolder (1));
344 if ((cp = context_find (stack))) {
345 dp = getcpy (cp);
346 for (ap = brkstring (dp, " ", "\n"); *ap; ap++)
347 printf (" %s", *ap);
348 free (dp);
349 }
350 printf ("\n");
351
352 if (!printsw)
353 done (0);
354 }
355
356 /* Allocate initial space to record folder names */
357 maxfolders = NUMFOLDERS;
358 if ((folds = malloc (maxfolders * sizeof(char *))) == NULL)
359 adios (NULL, "unable to allocate storage for folder names");
360
361 /* Allocate initial space to record folder information */
362 maxFolderInfo = NUMFOLDERS;
363 if ((fi = malloc (maxFolderInfo * sizeof(*fi))) == NULL)
364 adios (NULL, "unable to allocate storage for folder info");
365
366 /*
367 * Scan the folders
368 */
369 if (all || ftotal > 0) {
370 /*
371 * If no folder is given, do them all
372 */
373 if (!argfolder) {
374 if (msg)
375 admonish (NULL, "no folder given for message %s", msg);
376 readonly_folders (); /* do any readonly folders */
377 strncpy (folder, (cp = context_find (pfolder)) ? cp : "", sizeof(folder));
378 dodir (".");
379 } else {
380 strncpy (folder, argfolder, sizeof(folder));
381 if (get_folder_info (argfolder, msg)) {
382 context_replace (pfolder, argfolder);/* update current folder */
383 context_save (); /* save the context file */
384 }
385 /*
386 * Since recurse wasn't done in get_folder_info(),
387 * we still need to list all level-1 sub-folders.
388 */
389 if (!frecurse)
390 dodir (folder);
391 }
392 } else {
393 strncpy (folder, argfolder ? argfolder : getfolder (1), sizeof(folder));
394
395 /*
396 * Check if folder exists. If not, then see if
397 * we should create it, or just exit.
398 */
399 if (stat (strncpy (buf, m_maildir (folder), sizeof(buf)), &st) == -1) {
400 if (errno != ENOENT)
401 adios (buf, "error on folder");
402 if (fcreat == 0) {
403 /* ask before creating folder */
404 cp = concat ("Create folder \"", buf, "\"? ", NULL);
405 if (!getanswer (cp))
406 done (1);
407 free (cp);
408 } else if (fcreat == -1) {
409 /* do not create, so exit */
410 done (1);
411 }
412 if (!makedir (buf))
413 adios (NULL, "unable to create folder %s", buf);
414 }
415
416 if (get_folder_info (folder, msg) && argfolder) {
417 /* update current folder */
418 context_replace (pfolder, argfolder);
419 }
420 }
421
422 /*
423 * Print out folder information
424 */
425 print_folders();
426
427 context_save (); /* save the context file */
428 return done (0);
429 }
430
431 /*
432 * Base routine for scanning a folder
433 */
434
435 static void
436 dodir (char *dir)
437 {
438 int i;
439 int os = start;
440 int of = foldp;
441 char buffer[BUFSIZ];
442
443 start = foldp;
444
445 /* change directory to base of nmh directory */
446 if (chdir (nmhdir) == NOTOK)
447 adios (nmhdir, "unable to change directory to");
448
449 addir (strncpy (buffer, dir, sizeof(buffer)));
450
451 for (i = start; i < foldp; i++) {
452 get_folder_info (folds[i], NULL);
453 fflush (stdout);
454 }
455
456 start = os;
457 foldp = of;
458 }
459
460 static int
461 get_folder_info (char *fold, char *msg)
462 {
463 int i, retval = 1;
464 char *mailfile;
465 struct msgs *mp = NULL;
466
467 i = total_folders++;
468
469 /*
470 * if necessary, reallocate the space
471 * for folder information
472 */
473 if (total_folders >= maxFolderInfo) {
474 maxFolderInfo += NUMFOLDERS;
475 if ((fi = realloc (fi, maxFolderInfo * sizeof(*fi))) == NULL)
476 adios (NULL, "unable to re-allocate storage for folder info");
477 }
478
479 fi[i].name = fold;
480 fi[i].nummsg = 0;
481 fi[i].curmsg = 0;
482 fi[i].lowmsg = 0;
483 fi[i].hghmsg = 0;
484 fi[i].others = 0;
485 fi[i].error = 0;
486
487 mailfile = m_maildir (fold);
488
489 if (!chdir (mailfile)) {
490 if ((ftotal > 0) || !fshort || msg || fpack) {
491 /*
492 * create message structure and get folder info
493 */
494 if (!(mp = folder_read (fold))) {
495 admonish (NULL, "unable to read folder %s", fold);
496 return 0;
497 }
498
499 /* set the current message */
500 if (msg && !sfold (mp, msg))
501 retval = 0;
502
503 if (fpack) {
504 if (folder_pack (&mp, fverb) == -1)
505 done (1);
506 seq_save (mp); /* synchronize the sequences */
507 context_save (); /* save the context file */
508 }
509
510 /* record info for this folder */
511 if ((ftotal > 0) || !fshort) {
512 fi[i].nummsg = mp->nummsg;
513 fi[i].curmsg = mp->curmsg;
514 fi[i].lowmsg = mp->lowmsg;
515 fi[i].hghmsg = mp->hghmsg;
516 fi[i].others = other_files (mp);
517 }
518
519 folder_free (mp); /* free folder/message structure */
520 }
521 } else {
522 fi[i].error = 1;
523 }
524
525 if (frecurse && (fshort || fi[i].others) && (fi[i].error == 0))
526 dodir (fold);
527 return retval;
528 }
529
530 /*
531 * Print folder information
532 */
533
534 static void
535 print_folders (void)
536 {
537 int i, len, hasempty = 0, curprinted;
538 int maxlen = 0, maxnummsg = 0, maxlowmsg = 0;
539 int maxhghmsg = 0, maxcurmsg = 0, total_msgs = 0;
540 int nummsgdigits, lowmsgdigits;
541 int hghmsgdigits, curmsgdigits;
542 char tmpname[BUFSIZ];
543
544 /*
545 * compute a few values needed to for
546 * printing various fields
547 */
548 for (i = 0; i < total_folders; i++) {
549 /* length of folder name */
550 len = strlen (fi[i].name);
551 if (len > maxlen)
552 maxlen = len;
553
554 /* If folder has error, skip the rest */
555 if (fi[i].error)
556 continue;
557
558 /* calculate total number of messages */
559 total_msgs += fi[i].nummsg;
560
561 /* maximum number of messages */
562 if (fi[i].nummsg > maxnummsg)
563 maxnummsg = fi[i].nummsg;
564
565 /* maximum low message */
566 if (fi[i].lowmsg > maxlowmsg)
567 maxlowmsg = fi[i].lowmsg;
568
569 /* maximum high message */
570 if (fi[i].hghmsg > maxhghmsg)
571 maxhghmsg = fi[i].hghmsg;
572
573 /* maximum current message */
574 if (fi[i].curmsg >= fi[i].lowmsg &&
575 fi[i].curmsg <= fi[i].hghmsg &&
576 fi[i].curmsg > maxcurmsg)
577 maxcurmsg = fi[i].curmsg;
578
579 /* check for empty folders */
580 if (fi[i].nummsg == 0)
581 hasempty = 1;
582 }
583 nummsgdigits = num_digits (maxnummsg);
584 lowmsgdigits = num_digits (maxlowmsg);
585 hghmsgdigits = num_digits (maxhghmsg);
586 curmsgdigits = num_digits (maxcurmsg);
587
588 if (hasempty && nummsgdigits < 2)
589 nummsgdigits = 2;
590
591 /*
592 * Print the header
593 */
594 if (fheader > 0 || (all && !fshort && fheader >= 0))
595 printf ("%-*s %*s %-*s; %-*s %*s\n",
596 maxlen+1, "FOLDER",
597 nummsgdigits + 13, "# MESSAGES",
598 lowmsgdigits + hghmsgdigits + 4, " RANGE",
599 curmsgdigits + 4, "CUR",
600 9, "(OTHERS)");
601
602 /*
603 * Print folder information
604 */
605 if (all || fshort || ftotal < 1) {
606 for (i = 0; i < total_folders; i++) {
607 if (fshort) {
608 printf ("%s\n", fi[i].name);
609 continue;
610 }
611
612 /* Add `+' to end of name, if folder is current */
613 if (strcmp (folder, fi[i].name))
614 snprintf (tmpname, sizeof(tmpname), "%s", fi[i].name);
615 else
616 snprintf (tmpname, sizeof(tmpname), "%s+", fi[i].name);
617
618 if (fi[i].error) {
619 printf ("%-*s is unreadable\n", maxlen+1, tmpname);
620 continue;
621 }
622
623 printf ("%-*s ", maxlen+1, tmpname);
624
625 curprinted = 0; /* remember if we print cur */
626 if (fi[i].nummsg == 0) {
627 printf ("has %*s messages%*s",
628 nummsgdigits, "no",
629 fi[i].others ? lowmsgdigits + hghmsgdigits + 5 : 0, "");
630 } else {
631 printf ("has %*d message%s (%*d-%*d)",
632 nummsgdigits, fi[i].nummsg,
633 (fi[i].nummsg == 1) ? " " : "s",
634 lowmsgdigits, fi[i].lowmsg,
635 hghmsgdigits, fi[i].hghmsg);
636 if (fi[i].curmsg >= fi[i].lowmsg && fi[i].curmsg <= fi[i].hghmsg) {
637 curprinted = 1;
638 printf ("; cur=%*d", curmsgdigits, fi[i].curmsg);
639 }
640 }
641
642 if (fi[i].others)
643 printf (";%*s (others)", curprinted ? 0 : curmsgdigits + 6, "");
644 printf (".\n");
645 }
646 }
647
648 /*
649 * Print folder/message totals
650 */
651 if (ftotal > 0 || (all && !fshort && ftotal >= 0)) {
652 if (all)
653 printf ("\n");
654 printf ("TOTAL = %d message%c in %d folder%s.\n",
655 total_msgs, total_msgs != 1 ? 's' : ' ',
656 total_folders, total_folders != 1 ? "s" : "");
657 }
658
659 fflush (stdout);
660 }
661
662 /*
663 * Calculate the number of digits in a nonnegative integer
664 */
665 int
666 num_digits (int n)
667 {
668 int ndigits = 0;
669
670 /* Sanity check */
671 if (n < 0)
672 adios (NULL, "oops, num_digits called with negative value");
673
674 if (n == 0)
675 return 1;
676
677 while (n) {
678 n /= 10;
679 ndigits++;
680 }
681
682 return ndigits;
683 }
684
685 /*
686 * Set the current message and sychronize sequences
687 */
688
689 static int
690 sfold (struct msgs *mp, char *msg)
691 {
692 /* parse the message range/sequence/name and set SELECTED */
693 if (!m_convert (mp, msg))
694 return 0;
695
696 if (mp->numsel > 1) {
697 admonish (NULL, "only one message at a time!");
698 return 0;
699 }
700 seq_setprev (mp); /* set the previous-sequence */
701 seq_setcur (mp, mp->lowsel);/* set current message */
702 seq_save (mp); /* synchronize message sequences */
703 context_save (); /* save the context file */
704
705 return 1;
706 }
707
708
709 static void
710 addir (char *name)
711 {
712 int nlink;
713 char *base, *cp;
714 struct stat st;
715 struct dirent *dp;
716 DIR * dd;
717
718 cp = name + strlen (name);
719 *cp++ = '/';
720 *cp = '\0';
721
722 /*
723 * A hack to skip over a leading
724 * "./" in folder names.
725 */
726 base = strcmp (name, "./") ? name : name + 2;
727
728 /* short-cut to see if directory has any sub-directories */
729 if (stat (name, &st) != -1 && st.st_nlink == 2)
730 return;
731
732 if (!(dd = opendir (name))) {
733 admonish (name, "unable to read directory ");
734 return;
735 }
736
737 /*
738 * Keep track of the number of directories we've seen
739 * so we can quit stat'ing early, if we've seen them all.
740 */
741 nlink = st.st_nlink;
742
743 while (nlink && (dp = readdir (dd))) {
744 if (!strcmp (dp->d_name, ".") || !strcmp (dp->d_name, "..")) {
745 nlink--;
746 continue;
747 }
748 if (cp + NLENGTH(dp) + 2 >= name + BUFSIZ)
749 continue;
750 strcpy (cp, dp->d_name);
751 if (stat (name, &st) != -1 && S_ISDIR(st.st_mode)) {
752 /*
753 * Check if this was really a symbolic link pointing at
754 * a directory. If not, then decrement link count.
755 */
756 if (lstat (name, &st) == -1)
757 nlink--;
758 addfold (base);
759 }
760 }
761
762 closedir (dd);
763 *--cp = '\0';
764 }
765
766 /*
767 * Add the folder name into the
768 * list in a sorted fashion.
769 */
770
771 static void
772 addfold (char *fold)
773 {
774 register int i, j;
775 register char *cp;
776
777 /* if necessary, reallocate the space for folder names */
778 if (foldp >= maxfolders) {
779 maxfolders += NUMFOLDERS;
780 if ((folds = realloc (folds, maxfolders * sizeof(char *))) == NULL)
781 adios (NULL, "unable to re-allocate storage for folder names");
782 }
783
784 cp = getcpy (fold);
785 for (i = start; i < foldp; i++)
786 if (compare (cp, folds[i]) < 0) {
787 for (j = foldp - 1; j >= i; j--)
788 folds[j + 1] = folds[j];
789 foldp++;
790 folds[i] = cp;
791 return;
792 }
793
794 folds[foldp++] = cp;
795 }
796
797
798 static int
799 compare (char *s1, char *s2)
800 {
801 register int i;
802
803 while (*s1 || *s2)
804 if ((i = *s1++ - *s2++))
805 return i;
806
807 return 0;
808 }
809
810 /*
811 * Do the read only folders
812 */
813
814 static void
815 readonly_folders (void)
816 {
817 int atrlen;
818 char atrcur[BUFSIZ];
819 register struct node *np;
820
821 /* sanity check - check that context has been read */
822 if (defpath == NULL)
823 adios (NULL, "oops, context hasn't been read yet");
824
825 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
826 atrlen = strlen (atrcur);
827
828 for (np = m_defs; np; np = np->n_next)
829 if (ssequal (atrcur, np->n_name)
830 && !ssequal (nmhdir, np->n_name + atrlen))
831 get_folder_info (np->n_name + atrlen, NULL);
832 }