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