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