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