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