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