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