]> diplodocus.org Git - nmh/blob - uip/scan.c
mhshowsbr.c: Delete single-use global int `nolist'.
[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 charstring_t scanl = NULL;
80 FILE *in;
81
82 if (nmh_init(argv[0], true, true)) { return 1; }
83
84 mts_init ();
85 arguments = getarguments (invo_name, argc, argv, 1);
86 argp = arguments;
87
88 /*
89 * Parse arguments
90 */
91 while ((cp = *argp++)) {
92 if (*cp == '-') {
93 switch (smatch (++cp, switches)) {
94 case AMBIGSW:
95 ambigsw (cp, switches);
96 done (1);
97 case UNKWNSW:
98 die("-%s unknown", cp);
99
100 case HELPSW:
101 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
102 invo_name);
103 print_help (buf, switches, 1);
104 done (0);
105 case VERSIONSW:
106 print_version(invo_name);
107 done (0);
108
109 case CLRSW:
110 clearflag = true;
111 continue;
112 case NCLRSW:
113 clearflag = false;
114 continue;
115
116 case FORMSW:
117 if (!(form = *argp++) || *form == '-')
118 die("missing argument to %s", argp[-2]);
119 format = NULL;
120 continue;
121 case FMTSW:
122 if (!(format = *argp++) || *format == '-')
123 die("missing argument to %s", argp[-2]);
124 form = NULL;
125 continue;
126
127 case HEADSW:
128 hdrflag = true;
129 continue;
130 case NHEADSW:
131 hdrflag = false;
132 continue;
133
134 case WIDTHSW:
135 if (!(cp = *argp++) || *cp == '-')
136 die("missing argument to %s", argp[-2]);
137 width = atoi (cp);
138 continue;
139 case REVSW:
140 revflag = true;
141 continue;
142 case NREVSW:
143 revflag = false;
144 continue;
145
146 case FILESW:
147 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
148 die("missing argument to %s", argp[-2]);
149 if (strcmp (file = cp, "-"))
150 file = path (cp, TFILE);
151 continue;
152 }
153 }
154 if (*cp == '+' || *cp == '@') {
155 if (folder)
156 die("only one folder at a time!");
157 folder = pluspath (cp);
158 } else
159 app_msgarg(&msgs, cp);
160 }
161
162 if (!context_find ("path"))
163 free (path ("./", TFOLDER));
164
165 /*
166 * Get new format string. Must be before chdir().
167 */
168 nfs = new_fs (form, format, FORMAT);
169
170 /*
171 * We are scanning a maildrop file
172 */
173 if (file) {
174 if (msgs.size)
175 die("\"msgs\" not allowed with -file");
176 if (folder)
177 die("\"+folder\" not allowed with -file");
178
179 /* check if "file" is really stdin */
180 if (strcmp (file, "-") == 0) {
181 in = stdin;
182 file = "stdin";
183 } else {
184 if ((in = fopen (file, "r")) == NULL)
185 adios (file, "unable to open");
186 }
187
188 if (hdrflag) {
189 printf ("FOLDER %s\t%s\n", file, dtimenow (1));
190 }
191
192 scan_detect_mbox_style (in);
193 for (msgnum = 1; ; ++msgnum) {
194 state = scan (in, msgnum, -1, nfs, width, 0, 0,
195 hdrflag ? file : NULL, 0L, 1, &scanl);
196 if (scanl)
197 charstring_clear(scanl);
198 if (state != SCNMSG && state != SCNENC)
199 break;
200 }
201 charstring_free (scanl);
202 scan_finished ();
203 fclose (in);
204 done (0);
205 }
206
207 /*
208 * We are scanning a folder
209 */
210
211 if (!msgs.size)
212 app_msgarg(&msgs, "all");
213 if (!folder)
214 folder = getfolder (1);
215 maildir = m_maildir (folder);
216
217 if (chdir (maildir) == NOTOK)
218 adios (maildir, "unable to change directory to");
219
220 /* read folder and create message structure */
221 if (!(mp = folder_read (folder, 1)))
222 die("unable to read folder %s", folder);
223
224 /* check for empty folder */
225 if (mp->nummsg == 0)
226 die("no messages in %s", folder);
227
228 /* parse all the message ranges/sequences and set SELECTED */
229 for (msgnum = 0; msgnum < msgs.size; msgnum++)
230 if (!m_convert (mp, msgs.msgs[msgnum]))
231 done(1);
232 seq_setprev (mp); /* set the Previous-Sequence */
233
234 context_replace (pfolder, folder); /* update current folder */
235 seq_save (mp); /* synchronize message sequences */
236 context_save (); /* save the context file */
237
238 /*
239 * Get the sequence number for each sequence
240 * specified by Unseen-Sequence
241 */
242 if ((cp = context_find (usequence)) && *cp) {
243 char **ap, *dp;
244
245 dp = mh_xstrdup(cp);
246 ap = brkstring (dp, " ", "\n");
247 for (i = 0; ap && *ap; i++, ap++)
248 ivector_push_back (seqnum, seq_getnum (mp, *ap));
249
250 num_unseen_seq = i;
251 free(dp);
252 }
253
254 ontty = isatty (fileno (stdout));
255
256 for (msgnum = revflag ? mp->hghsel : mp->lowsel;
257 (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
258 msgnum += (revflag ? -1 : 1)) {
259 if (is_selected(mp, msgnum)) {
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 if (scanl)
298 charstring_clear(scanl);
299 scan_finished ();
300 hdrflag = false;
301 fclose (in);
302 if (ontty)
303 fflush (stdout);
304 }
305 }
306 charstring_free (scanl);
307
308 ivector_free (seqnum);
309 folder_free (mp); /* free folder/message structure */
310 if (clearflag)
311 nmh_clear_screen ();
312
313 done (0);
314 return 1;
315 }