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