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