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