]> diplodocus.org Git - nmh/blob - uip/scan.c
Removed temporary probes added in commit
[nmh] / uip / scan.c
1
2 /*
3 * scan.c -- display a one-line "scan" listing of folder or messages
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/fmt_scan.h>
12 #include <h/scansbr.h>
13 #include <h/tws.h>
14 #include <h/mts.h>
15 #include <h/utils.h>
16
17 #define SCAN_SWITCHES \
18 X("clear", 0, CLRSW) \
19 X("noclear", 0, NCLRSW) \
20 X("form formatfile", 0, FORMSW) \
21 X("format string", 5, FMTSW) \
22 X("header", 0, HEADSW) \
23 X("noheader", 0, NHEADSW) \
24 X("width columns", 0, WIDTHSW) \
25 X("reverse", 0, REVSW) \
26 X("noreverse", 0, NREVSW) \
27 X("file file", 4, FILESW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
30
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(SCAN);
33 #undef X
34
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(SCAN, switches);
37 #undef X
38
39
40 /*
41 * prototypes
42 */
43 void clear_screen(void); /* from termsbr.c */
44
45
46 int
47 main (int argc, char **argv)
48 {
49 int clearflag = 0, hdrflag = 0, ontty;
50 int width = 0, revflag = 0;
51 int i, state, msgnum;
52 ivector_t seqnum = ivector_create (0);
53 int unseen, num_unseen_seq = 0;
54 char *cp, *maildir, *file = NULL, *folder = NULL;
55 char *form = NULL, *format = NULL, buf[BUFSIZ];
56 char **argp, *nfs, **arguments;
57 struct msgs_array msgs = { 0, 0, NULL };
58 struct msgs *mp;
59 FILE *in;
60
61 #ifdef LOCALE
62 setlocale(LC_ALL, "");
63 #endif
64 invo_name = r1bindex (argv[0], '/');
65
66 /* read user profile/context */
67 context_read();
68
69 mts_init (invo_name);
70 arguments = getarguments (invo_name, argc, argv, 1);
71 argp = arguments;
72
73 /*
74 * Parse arguments
75 */
76 while ((cp = *argp++)) {
77 if (*cp == '-') {
78 switch (smatch (++cp, switches)) {
79 case AMBIGSW:
80 ambigsw (cp, switches);
81 done (1);
82 case UNKWNSW:
83 adios (NULL, "-%s unknown", cp);
84
85 case HELPSW:
86 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
87 invo_name);
88 print_help (buf, switches, 1);
89 done (0);
90 case VERSIONSW:
91 print_version(invo_name);
92 done (0);
93
94 case CLRSW:
95 clearflag++;
96 continue;
97 case NCLRSW:
98 clearflag = 0;
99 continue;
100
101 case FORMSW:
102 if (!(form = *argp++) || *form == '-')
103 adios (NULL, "missing argument to %s", argp[-2]);
104 format = NULL;
105 continue;
106 case FMTSW:
107 if (!(format = *argp++) || *format == '-')
108 adios (NULL, "missing argument to %s", argp[-2]);
109 form = NULL;
110 continue;
111
112 case HEADSW:
113 hdrflag++;
114 continue;
115 case NHEADSW:
116 hdrflag = 0;
117 continue;
118
119 case WIDTHSW:
120 if (!(cp = *argp++) || *cp == '-')
121 adios (NULL, "missing argument to %s", argp[-2]);
122 width = atoi (cp);
123 continue;
124 case REVSW:
125 revflag++;
126 continue;
127 case NREVSW:
128 revflag = 0;
129 continue;
130
131 case FILESW:
132 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
133 adios (NULL, "missing argument to %s", argp[-2]);
134 if (strcmp (file = cp, "-"))
135 file = path (cp, TFILE);
136 continue;
137 }
138 }
139 if (*cp == '+' || *cp == '@') {
140 if (folder)
141 adios (NULL, "only one folder at a time!");
142 else
143 folder = pluspath (cp);
144 } else
145 app_msgarg(&msgs, cp);
146 }
147
148 if (!context_find ("path"))
149 free (path ("./", TFOLDER));
150
151 /*
152 * Get new format string. Must be before chdir().
153 */
154 nfs = new_fs (form, format, FORMAT);
155
156 /*
157 * We are scanning a maildrop file
158 */
159 if (file) {
160 if (msgs.size)
161 adios (NULL, "\"msgs\" not allowed with -file");
162 if (folder)
163 adios (NULL, "\"+folder\" not allowed with -file");
164
165 /* check if "file" is really stdin */
166 if (strcmp (file, "-") == 0) {
167 in = stdin;
168 file = "stdin";
169 } else {
170 if ((in = fopen (file, "r")) == NULL)
171 adios (file, "unable to open");
172 }
173
174 if (hdrflag) {
175 printf ("FOLDER %s\t%s\n", file, dtimenow (1));
176 }
177
178 scan_detect_mbox_style (in);
179 for (msgnum = 1; ; ++msgnum) {
180 state = scan (in, msgnum, -1, nfs, width, 0, 0,
181 hdrflag ? file : NULL, 0L, 1);
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 adios (NULL, "unable to read folder %s", folder);
206
207 /* check for empty folder */
208 if (mp->nummsg == 0)
209 adios (NULL, "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 = getcpy(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 if (dp)
235 free(dp);
236 }
237
238 ontty = isatty (fileno (stdout));
239
240 for (msgnum = revflag ? mp->hghsel : mp->lowsel;
241 (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
242 msgnum += (revflag ? -1 : 1)) {
243 if (is_selected(mp, msgnum)) {
244 if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
245 admonish (cp, "unable to open message");
246 continue;
247 }
248
249 if (hdrflag) {
250 printf ("FOLDER %s\t%s\n", folder, dtimenow(1));
251 }
252
253 /*
254 * Check if message is in any sequence given
255 * by Unseen-Sequence profile entry.
256 */
257 unseen = 0;
258 for (i = 0; i < num_unseen_seq; i++) {
259 if (in_sequence(mp, ivector_at (seqnum, i), msgnum)) {
260 unseen = 1;
261 break;
262 }
263 }
264
265 switch (state = scan (in, msgnum, 0, nfs, width,
266 msgnum == mp->curmsg, unseen,
267 folder, 0L, 1)) {
268 case SCNMSG:
269 case SCNENC:
270 case SCNERR:
271 break;
272
273 default:
274 adios (NULL, "scan() botch (%d)", state);
275
276 case SCNEOF:
277 advise (NULL, "message %d: empty", msgnum);
278 break;
279 }
280 scan_finished ();
281 hdrflag = 0;
282 fclose (in);
283 if (ontty)
284 fflush (stdout);
285 }
286 }
287
288 ivector_free (seqnum);
289 folder_free (mp); /* free folder/message structure */
290 if (clearflag)
291 clear_screen ();
292
293 done (0);
294 return 1;
295 }