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