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