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