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