* patch #3966: Create a mh_xmalloc function to prevent mistakes when
[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                 if (!(msgs = (char **) realloc (msgs,
181                         (size_t) (maxmsgs * sizeof(*msgs)))))
182                     adios (NULL, "unable to reallocate msgs storage");
183             }
184             msgs[nummsgs++] = cp;
185         }
186     }
187
188     if (!context_find ("path"))
189         free (path ("./", TFOLDER));
190
191     /*
192      * Get new format string.  Must be before chdir().
193      */
194     nfs = new_fs (form, format, FORMAT);
195
196     /*
197      * We are scanning a maildrop file
198      */
199     if (file) {
200         if (nummsgs)
201             adios (NULL, "\"msgs\" not allowed with -file");
202         if (folder)
203             adios (NULL, "\"+folder\" not allowed with -file");
204
205         /* check if "file" is really stdin */
206         if (strcmp (file, "-") == 0) {
207             in = stdin;
208             file = "stdin";
209         } else {
210             if ((in = fopen (file, "r")) == NULL)
211                 adios (file, "unable to open");
212         }
213
214 #ifndef JLR
215         if (hdrflag) {
216             printf ("FOLDER %s\t%s\n", file, dtimenow (1));
217         }
218 #endif /* JLR */
219
220         m_unknown (in);
221         for (msgnum = 1; ; ++msgnum) {
222             state = scan (in, msgnum, -1, nfs, width, 0, 0,
223                     hdrflag ? file : NULL, 0L, 1);
224             if (state != SCNMSG && state != SCNENC)
225                 break;
226         }
227         fclose (in);
228         done (0);
229     }
230
231     /*
232      * We are scanning a folder
233      */
234
235     if (!nummsgs)
236         msgs[nummsgs++] = "all";
237     if (!folder)
238         folder = getfolder (1);
239     maildir = m_maildir (folder);
240
241     if (chdir (maildir) == NOTOK)
242         adios (maildir, "unable to change directory to");
243
244     /* read folder and create message structure */
245     if (!(mp = folder_read (folder)))
246         adios (NULL, "unable to read folder %s", folder);
247
248     /* check for empty folder */
249     if (mp->nummsg == 0)
250         adios (NULL, "no messages in %s", folder);
251
252     /* parse all the message ranges/sequences and set SELECTED */
253     for (msgnum = 0; msgnum < nummsgs; msgnum++)
254         if (!m_convert (mp, msgs[msgnum]))
255             done(1);
256     seq_setprev (mp);                   /* set the Previous-Sequence */
257
258     context_replace (pfolder, folder);  /* update current folder         */
259     seq_save (mp);                      /* synchronize message sequences */
260     context_save ();                    /* save the context file         */
261
262     /*
263      * Get the sequence number for each sequence
264      * specified by Unseen-Sequence
265      */
266     if ((cp = context_find (usequence)) && *cp) {
267         char **ap, *dp;
268
269         dp = getcpy(cp);
270         ap = brkstring (dp, " ", "\n");
271         for (i = 0; ap && *ap; i++, ap++)
272             seqnum[i] = seq_getnum (mp, *ap);
273
274         num_unseen_seq = i;
275         if (dp)
276             free(dp);
277     }
278
279     ontty = isatty (fileno (stdout));
280
281 #ifdef LBL
282     else
283         fmt_current_folder = mp;
284 #endif
285
286     for (msgnum = revflag ? mp->hghsel : mp->lowsel;
287          (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
288          msgnum += (revflag ? -1 : 1)) {
289         if (is_selected(mp, msgnum)) {
290             if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
291 #if 0
292                 if (errno != EACCES)
293 #endif
294                     admonish (cp, "unable to open message");
295 #if 0
296                 else
297                     printf ("%*d  unreadable\n", DMAXFOLDER, msgnum);
298 #endif
299                 continue;
300             }
301
302 #ifndef JLR
303             if (hdrflag) {
304                 printf ("FOLDER %s\t%s\n", folder, dtimenow(1));
305             }
306 #endif /* JLR */
307
308             /*
309              * Check if message is in any sequence given
310              * by Unseen-Sequence profile entry.
311              */
312             unseen = 0;
313             for (i = 0; i < num_unseen_seq; i++) {
314                 if (in_sequence(mp, seqnum[i], msgnum)) {
315                     unseen = 1;
316                     break;
317                 }
318             }
319
320             switch (state = scan (in, msgnum, 0, nfs, width,
321                         msgnum == mp->curmsg, unseen,
322                         folder, 0L, 1)) {
323                 case SCNMSG: 
324                 case SCNENC: 
325                 case SCNERR: 
326                     break;
327
328                 default: 
329                     adios (NULL, "scan() botch (%d)", state);
330
331                 case SCNEOF: 
332 #if 0
333                     printf ("%*d  empty\n", DMAXFOLDER, msgnum);
334 #else
335                     advise (NULL, "message %d: empty", msgnum);
336 #endif
337                     break;
338             }
339             hdrflag = 0;
340             fclose (in);
341             if (ontty)
342                 fflush (stdout);
343         }
344     }
345
346 #ifdef LBL
347     seq_save (mp);      /* because formatsbr might have made changes */
348 #endif
349
350     folder_free (mp);   /* free folder/message structure */
351     if (clearflag)
352         clear_screen ();
353
354     return done (0);
355 }