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