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