]> diplodocus.org Git - nmh/blob - uip/scan.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / scan.c
1 /* scan.c -- display a one-line "scan" listing of folder or messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/fmt_scan.h>
10 #include <h/scansbr.h>
11 #include <h/tws.h>
12 #include <h/mts.h>
13 #include <h/utils.h>
14 #include "sbr/m_maildir.h"
15 #include "sbr/terminal.h"
16
17 #define SCAN_SWITCHES \
18 X("clear", 0, CLRSW) \
19 X("noclear", 0, NCLRSW) \
20 X("form formatfile", 0, FORMSW) \
21 X("format string", 5, FMTSW) \
22 X("header", 0, HEADSW) \
23 X("noheader", 0, NHEADSW) \
24 X("width columns", 0, WIDTHSW) \
25 X("reverse", 0, REVSW) \
26 X("noreverse", 0, NREVSW) \
27 X("file file", 4, FILESW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
30
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(SCAN);
33 #undef X
34
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(SCAN, switches);
37 #undef X
38
39
40 int
41 main (int argc, char **argv)
42 {
43 int clearflag = 0, hdrflag = 0, ontty;
44 int width = -1, revflag = 0;
45 int i, state, msgnum;
46 ivector_t seqnum = ivector_create (0);
47 int unseen, num_unseen_seq = 0;
48 char *cp, *maildir, *file = NULL, *folder = NULL;
49 char *form = NULL, *format = NULL, buf[BUFSIZ];
50 char **argp, *nfs, **arguments;
51 struct msgs_array msgs = { 0, 0, NULL };
52 struct msgs *mp;
53 FILE *in;
54
55 if (nmh_init(argv[0], 1)) { return 1; }
56
57 mts_init ();
58 arguments = getarguments (invo_name, argc, argv, 1);
59 argp = arguments;
60
61 /*
62 * Parse arguments
63 */
64 while ((cp = *argp++)) {
65 if (*cp == '-') {
66 switch (smatch (++cp, switches)) {
67 case AMBIGSW:
68 ambigsw (cp, switches);
69 done (1);
70 case UNKWNSW:
71 adios (NULL, "-%s unknown", cp);
72
73 case HELPSW:
74 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
75 invo_name);
76 print_help (buf, switches, 1);
77 done (0);
78 case VERSIONSW:
79 print_version(invo_name);
80 done (0);
81
82 case CLRSW:
83 clearflag++;
84 continue;
85 case NCLRSW:
86 clearflag = 0;
87 continue;
88
89 case FORMSW:
90 if (!(form = *argp++) || *form == '-')
91 adios (NULL, "missing argument to %s", argp[-2]);
92 format = NULL;
93 continue;
94 case FMTSW:
95 if (!(format = *argp++) || *format == '-')
96 adios (NULL, "missing argument to %s", argp[-2]);
97 form = NULL;
98 continue;
99
100 case HEADSW:
101 hdrflag++;
102 continue;
103 case NHEADSW:
104 hdrflag = 0;
105 continue;
106
107 case WIDTHSW:
108 if (!(cp = *argp++) || *cp == '-')
109 adios (NULL, "missing argument to %s", argp[-2]);
110 width = atoi (cp);
111 continue;
112 case REVSW:
113 revflag++;
114 continue;
115 case NREVSW:
116 revflag = 0;
117 continue;
118
119 case FILESW:
120 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
121 adios (NULL, "missing argument to %s", argp[-2]);
122 if (strcmp (file = cp, "-"))
123 file = path (cp, TFILE);
124 continue;
125 }
126 }
127 if (*cp == '+' || *cp == '@') {
128 if (folder)
129 adios (NULL, "only one folder at a time!");
130 folder = pluspath (cp);
131 } else
132 app_msgarg(&msgs, cp);
133 }
134
135 if (!context_find ("path"))
136 free (path ("./", TFOLDER));
137
138 /*
139 * Get new format string. Must be before chdir().
140 */
141 nfs = new_fs (form, format, FORMAT);
142
143 /*
144 * We are scanning a maildrop file
145 */
146 if (file) {
147 if (msgs.size)
148 adios (NULL, "\"msgs\" not allowed with -file");
149 if (folder)
150 adios (NULL, "\"+folder\" not allowed with -file");
151
152 /* check if "file" is really stdin */
153 if (strcmp (file, "-") == 0) {
154 in = stdin;
155 file = "stdin";
156 } else {
157 if ((in = fopen (file, "r")) == NULL)
158 adios (file, "unable to open");
159 }
160
161 if (hdrflag) {
162 printf ("FOLDER %s\t%s\n", file, dtimenow (1));
163 }
164
165 scan_detect_mbox_style (in);
166 for (msgnum = 1; ; ++msgnum) {
167 charstring_t scanl = NULL;
168
169 state = scan (in, msgnum, -1, nfs, width, 0, 0,
170 hdrflag ? file : NULL, 0L, 1, &scanl);
171 charstring_free (scanl);
172 if (state != SCNMSG && state != SCNENC)
173 break;
174 }
175 scan_finished ();
176 fclose (in);
177 done (0);
178 }
179
180 /*
181 * We are scanning a folder
182 */
183
184 if (!msgs.size)
185 app_msgarg(&msgs, "all");
186 if (!folder)
187 folder = getfolder (1);
188 maildir = m_maildir (folder);
189
190 if (chdir (maildir) == NOTOK)
191 adios (maildir, "unable to change directory to");
192
193 /* read folder and create message structure */
194 if (!(mp = folder_read (folder, 1)))
195 adios (NULL, "unable to read folder %s", folder);
196
197 /* check for empty folder */
198 if (mp->nummsg == 0)
199 adios (NULL, "no messages in %s", folder);
200
201 /* parse all the message ranges/sequences and set SELECTED */
202 for (msgnum = 0; msgnum < msgs.size; msgnum++)
203 if (!m_convert (mp, msgs.msgs[msgnum]))
204 done(1);
205 seq_setprev (mp); /* set the Previous-Sequence */
206
207 context_replace (pfolder, folder); /* update current folder */
208 seq_save (mp); /* synchronize message sequences */
209 context_save (); /* save the context file */
210
211 /*
212 * Get the sequence number for each sequence
213 * specified by Unseen-Sequence
214 */
215 if ((cp = context_find (usequence)) && *cp) {
216 char **ap, *dp;
217
218 dp = mh_xstrdup(cp);
219 ap = brkstring (dp, " ", "\n");
220 for (i = 0; ap && *ap; i++, ap++)
221 ivector_push_back (seqnum, seq_getnum (mp, *ap));
222
223 num_unseen_seq = i;
224 free(dp);
225 }
226
227 ontty = isatty (fileno (stdout));
228
229 for (msgnum = revflag ? mp->hghsel : mp->lowsel;
230 (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
231 msgnum += (revflag ? -1 : 1)) {
232 if (is_selected(mp, msgnum)) {
233 charstring_t scanl = NULL;
234
235 if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
236 admonish (cp, "unable to open message");
237 continue;
238 }
239
240 if (hdrflag) {
241 printf ("FOLDER %s\t%s\n", folder, dtimenow(1));
242 }
243
244 /*
245 * Check if message is in any sequence given
246 * by Unseen-Sequence profile entry.
247 */
248 unseen = 0;
249 for (i = 0; i < num_unseen_seq; i++) {
250 if (in_sequence(mp, ivector_at (seqnum, i), msgnum)) {
251 unseen = 1;
252 break;
253 }
254 }
255
256 switch (state = scan (in, msgnum, 0, nfs, width,
257 msgnum == mp->curmsg, unseen,
258 folder, 0L, 1, &scanl)) {
259 case SCNMSG:
260 case SCNENC:
261 case SCNERR:
262 break;
263
264 default:
265 adios (NULL, "scan() botch (%d)", state);
266
267 case SCNEOF:
268 inform("message %d: empty", msgnum);
269 break;
270 }
271 charstring_free (scanl);
272 scan_finished ();
273 hdrflag = 0;
274 fclose (in);
275 if (ontty)
276 fflush (stdout);
277 }
278 }
279
280 ivector_free (seqnum);
281 folder_free (mp); /* free folder/message structure */
282 if (clearflag)
283 nmh_clear_screen ();
284
285 done (0);
286 return 1;
287 }