]> diplodocus.org Git - nmh/blob - uip/scan.c
Fix stupid accidental dependence on a bash quirk in previous
[nmh] / 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
55 /*
56 * global for sbr/formatsbr.c - yech!
57 */
58 #ifdef LBL
59 extern struct msgs *fmt_current_folder;
60 #endif
61
62 /*
63 * prototypes
64 */
65 void clear_screen(void); /* from termsbr.c */
66
67
68 int
69 main (int argc, char **argv)
70 {
71 int clearflag = 0, hdrflag = 0, ontty;
72 int width = 0, revflag = 0;
73 int i, state, msgnum, nummsgs, maxmsgs;
74 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
75 char *cp, *maildir, *file = NULL, *folder = NULL;
76 char *form = NULL, *format = NULL, buf[BUFSIZ];
77 char **argp, *nfs, **arguments, **msgs;
78 struct msgs *mp;
79 FILE *in;
80
81 #ifdef LOCALE
82 setlocale(LC_ALL, "");
83 #endif
84 invo_name = r1bindex (argv[0], '/');
85
86 /* read user profile/context */
87 context_read();
88
89 mts_init (invo_name);
90 arguments = getarguments (invo_name, argc, argv, 1);
91 argp = arguments;
92
93 /*
94 * Allocate the initial space to record message
95 * names, ranges, and sequences.
96 */
97 nummsgs = 0;
98 maxmsgs = MAXMSGS;
99 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
100 adios (NULL, "unable to allocate storage");
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 }