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