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