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