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