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