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