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