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