* man/mhbuild.man: wrapped one appearance of "Content-Disposition"
[mmh] / uip / scan.c
1
2 /*
3  * scan.c -- display a one-line "scan" listing of folder or messages
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2002, 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/fmt_scan.h>
14 #include <h/scansbr.h>
15 #include <h/tws.h>
16 #include <h/mts.h>
17 #include <h/utils.h>
18 #include <errno.h>
19
20 /*
21  * We allocate space for message names (msgs array)
22  * this number of elements at a time.
23  */
24 #define MAXMSGS  256
25
26
27 static struct swit switches[] = {
28 #define CLRSW   0
29     { "clear", 0 },
30 #define NCLRSW  1
31     { "noclear", 0 },
32 #define FORMSW  2
33     { "form formatfile", 0 },
34 #define FMTSW   3
35     { "format string", 5 },
36 #define HEADSW  4
37     { "header", 0 },
38 #define NHEADSW 5
39     { "noheader", 0 },
40 #define WIDTHSW 6
41     { "width columns", 0 },
42 #define REVSW   7
43     { "reverse", 0 },
44 #define NREVSW  8
45     { "noreverse", 0 },
46 #define FILESW  9
47     { "file file", 4 },
48 #define VERSIONSW 10
49     { "version", 0 },
50 #define HELPSW  11
51     { "help", 0 },
52     { NULL, 0 }
53 };
54
55
56 /*
57  * global for sbr/formatsbr.c - yech!
58  */
59 #ifdef LBL
60 extern struct msgs *fmt_current_folder; 
61 #endif
62
63 /*
64  * prototypes
65  */
66 void clear_screen(void);  /* from termsbr.c */
67
68
69 int
70 main (int argc, char **argv)
71 {
72     int clearflag = 0, hdrflag = 0, ontty;
73     int width = 0, revflag = 0;
74     int i, state, msgnum, nummsgs, maxmsgs;
75     int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
76     char *cp, *maildir, *file = NULL, *folder = NULL;
77     char *form = NULL, *format = NULL, buf[BUFSIZ];
78     char **argp, *nfs, **arguments, **msgs;
79     struct msgs *mp;
80     FILE *in;
81
82 #ifdef LOCALE
83     setlocale(LC_ALL, "");
84 #endif
85     invo_name = r1bindex (argv[0], '/');
86
87     /* read user profile/context */
88     context_read();
89
90     mts_init (invo_name);
91     arguments = getarguments (invo_name, argc, argv, 1);
92     argp = arguments;
93
94     /*
95      * Allocate the initial space to record message
96      * names, ranges, and sequences.
97      */
98     nummsgs = 0;
99     maxmsgs = MAXMSGS;
100     msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
101
102     /*
103      * Parse arguments
104      */
105     while ((cp = *argp++)) {
106         if (*cp == '-') {
107             switch (smatch (++cp, switches)) {
108                 case AMBIGSW: 
109                     ambigsw (cp, switches);
110                     done (1);
111                 case UNKWNSW: 
112                     adios (NULL, "-%s unknown", cp);
113
114                 case HELPSW: 
115                     snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
116                         invo_name);
117                     print_help (buf, switches, 1);
118                     done (1);
119                 case VERSIONSW:
120                     print_version(invo_name);
121                     done (1);
122
123                 case CLRSW: 
124                     clearflag++;
125                     continue;
126                 case NCLRSW: 
127                     clearflag = 0;
128                     continue;
129
130                 case FORMSW: 
131                     if (!(form = *argp++) || *form == '-')
132                         adios (NULL, "missing argument to %s", argp[-2]);
133                     format = NULL;
134                     continue;
135                 case FMTSW: 
136                     if (!(format = *argp++) || *format == '-')
137                         adios (NULL, "missing argument to %s", argp[-2]);
138                     form = NULL;
139                     continue;
140
141                 case HEADSW: 
142                     hdrflag++;
143                     continue;
144                 case NHEADSW: 
145                     hdrflag = 0;
146                     continue;
147
148                 case WIDTHSW: 
149                     if (!(cp = *argp++) || *cp == '-')
150                         adios (NULL, "missing argument to %s", argp[-2]);
151                     width = atoi (cp);
152                     continue;
153                 case REVSW:
154                     revflag++;
155                     continue;
156                 case NREVSW:
157                     revflag = 0;
158                     continue;
159
160                 case FILESW:
161                     if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
162                         adios (NULL, "missing argument to %s", argp[-2]);
163                     if (strcmp (file = cp, "-"))
164                         file = path (cp, TFILE);
165                     continue;
166             }
167         }
168         if (*cp == '+' || *cp == '@') {
169             if (folder)
170                 adios (NULL, "only one folder at a time!");
171             else
172                 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
173         } else {
174             /*
175              * Check if we need to allocate more space
176              * for message names/ranges/sequences.
177              */
178             if (nummsgs >= maxmsgs) {
179                 maxmsgs += MAXMSGS;
180                 msgs = (char **) mh_xrealloc (msgs,
181                     (size_t) (maxmsgs * sizeof(*msgs)));
182             }
183             msgs[nummsgs++] = cp;
184         }
185     }
186
187     if (!context_find ("path"))
188         free (path ("./", TFOLDER));
189
190     /*
191      * Get new format string.  Must be before chdir().
192      */
193     nfs = new_fs (form, format, FORMAT);
194
195     /*
196      * We are scanning a maildrop file
197      */
198     if (file) {
199         if (nummsgs)
200             adios (NULL, "\"msgs\" not allowed with -file");
201         if (folder)
202             adios (NULL, "\"+folder\" not allowed with -file");
203
204         /* check if "file" is really stdin */
205         if (strcmp (file, "-") == 0) {
206             in = stdin;
207             file = "stdin";
208         } else {
209             if ((in = fopen (file, "r")) == NULL)
210                 adios (file, "unable to open");
211         }
212
213 #ifndef JLR
214         if (hdrflag) {
215             printf ("FOLDER %s\t%s\n", file, dtimenow (1));
216         }
217 #endif /* JLR */
218
219         m_unknown (in);
220         for (msgnum = 1; ; ++msgnum) {
221             state = scan (in, msgnum, -1, nfs, width, 0, 0,
222                     hdrflag ? file : NULL, 0L, 1);
223             if (state != SCNMSG && state != SCNENC)
224                 break;
225         }
226         fclose (in);
227         done (0);
228     }
229
230     /*
231      * We are scanning a folder
232      */
233
234     if (!nummsgs)
235         msgs[nummsgs++] = "all";
236     if (!folder)
237         folder = getfolder (1);
238     maildir = m_maildir (folder);
239
240     if (chdir (maildir) == NOTOK)
241         adios (maildir, "unable to change directory to");
242
243     /* read folder and create message structure */
244     if (!(mp = folder_read (folder)))
245         adios (NULL, "unable to read folder %s", folder);
246
247     /* check for empty folder */
248     if (mp->nummsg == 0)
249         adios (NULL, "no messages in %s", folder);
250
251     /* parse all the message ranges/sequences and set SELECTED */
252     for (msgnum = 0; msgnum < nummsgs; msgnum++)
253         if (!m_convert (mp, msgs[msgnum]))
254             done(1);
255     seq_setprev (mp);                   /* set the Previous-Sequence */
256
257     context_replace (pfolder, folder);  /* update current folder         */
258     seq_save (mp);                      /* synchronize message sequences */
259     context_save ();                    /* save the context file         */
260
261     /*
262      * Get the sequence number for each sequence
263      * specified by Unseen-Sequence
264      */
265     if ((cp = context_find (usequence)) && *cp) {
266         char **ap, *dp;
267
268         dp = getcpy(cp);
269         ap = brkstring (dp, " ", "\n");
270         for (i = 0; ap && *ap; i++, ap++)
271             seqnum[i] = seq_getnum (mp, *ap);
272
273         num_unseen_seq = i;
274         if (dp)
275             free(dp);
276     }
277
278     ontty = isatty (fileno (stdout));
279
280 #ifdef LBL
281     else
282         fmt_current_folder = mp;
283 #endif
284
285     for (msgnum = revflag ? mp->hghsel : mp->lowsel;
286          (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
287          msgnum += (revflag ? -1 : 1)) {
288         if (is_selected(mp, msgnum)) {
289             if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
290 #if 0
291                 if (errno != EACCES)
292 #endif
293                     admonish (cp, "unable to open message");
294 #if 0
295                 else
296                     printf ("%*d  unreadable\n", DMAXFOLDER, msgnum);
297 #endif
298                 continue;
299             }
300
301 #ifndef JLR
302             if (hdrflag) {
303                 printf ("FOLDER %s\t%s\n", folder, dtimenow(1));
304             }
305 #endif /* JLR */
306
307             /*
308              * Check if message is in any sequence given
309              * by Unseen-Sequence profile entry.
310              */
311             unseen = 0;
312             for (i = 0; i < num_unseen_seq; i++) {
313                 if (in_sequence(mp, seqnum[i], msgnum)) {
314                     unseen = 1;
315                     break;
316                 }
317             }
318
319             switch (state = scan (in, msgnum, 0, nfs, width,
320                         msgnum == mp->curmsg, unseen,
321                         folder, 0L, 1)) {
322                 case SCNMSG: 
323                 case SCNENC: 
324                 case SCNERR: 
325                     break;
326
327                 default: 
328                     adios (NULL, "scan() botch (%d)", state);
329
330                 case SCNEOF: 
331 #if 0
332                     printf ("%*d  empty\n", DMAXFOLDER, msgnum);
333 #else
334                     advise (NULL, "message %d: empty", msgnum);
335 #endif
336                     break;
337             }
338             hdrflag = 0;
339             fclose (in);
340             if (ontty)
341                 fflush (stdout);
342         }
343     }
344
345 #ifdef LBL
346     seq_save (mp);      /* because formatsbr might have made changes */
347 #endif
348
349     folder_free (mp);   /* free folder/message structure */
350     if (clearflag)
351         clear_screen ();
352
353     return done (0);
354 }