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