3 * scan.c -- display a one-line "scan" listing of folder or messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/fmt_scan.h>
14 #include <h/scansbr.h>
20 * We allocate space for message names (msgs array)
21 * this number of elements at a time.
26 static struct swit switches[] = {
32 { "form formatfile", 0 },
34 { "format string", 5 },
40 { "width columns", 0 },
56 * global for sbr/formatsbr.c - yech!
59 extern struct msgs *fmt_current_folder;
65 void clear_screen(void); /* from termsbr.c */
69 main (int argc, char **argv)
71 int clearflag = 0, hdrflag = 0, ontty;
72 int width = 0, revflag = 0;
73 int i, state, msgnum, nummsgs, maxmsgs;
74 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
75 char *cp, *maildir, *file = NULL, *folder = NULL;
76 char *form = NULL, *format = NULL, buf[BUFSIZ];
77 char **argp, *nfs, **arguments, **msgs;
82 setlocale(LC_ALL, "");
84 invo_name = r1bindex (argv[0], '/');
86 /* read user profile/context */
90 arguments = getarguments (invo_name, argc, argv, 1);
94 * Allocate the initial space to record message
95 * names, ranges, and sequences.
99 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
100 adios (NULL, "unable to allocate storage");
105 while ((cp = *argp++)) {
107 switch (smatch (++cp, switches)) {
109 ambigsw (cp, switches);
112 adios (NULL, "-%s unknown", cp);
115 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
117 print_help (buf, switches, 1);
120 print_version(invo_name);
131 if (!(form = *argp++) || *form == '-')
132 adios (NULL, "missing argument to %s", argp[-2]);
136 if (!(format = *argp++) || *format == '-')
137 adios (NULL, "missing argument to %s", argp[-2]);
149 if (!(cp = *argp++) || *cp == '-')
150 adios (NULL, "missing argument to %s", argp[-2]);
161 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
162 adios (NULL, "missing argument to %s", argp[-2]);
163 if (strcmp (file = cp, "-"))
164 file = path (cp, TFILE);
168 if (*cp == '+' || *cp == '@') {
170 adios (NULL, "only one folder at a time!");
172 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
175 * Check if we need to allocate more space
176 * for message names/ranges/sequences.
178 if (nummsgs >= maxmsgs) {
180 if (!(msgs = (char **) realloc (msgs,
181 (size_t) (maxmsgs * sizeof(*msgs)))))
182 adios (NULL, "unable to reallocate msgs storage");
184 msgs[nummsgs++] = cp;
188 if (!context_find ("path"))
189 free (path ("./", TFOLDER));
192 * Get new format string. Must be before chdir().
194 nfs = new_fs (form, format, FORMAT);
197 * We are scanning a maildrop file
201 adios (NULL, "\"msgs\" not allowed with -file");
203 adios (NULL, "\"+folder\" not allowed with -file");
205 /* check if "file" is really stdin */
206 if (strcmp (file, "-") == 0) {
210 if ((in = fopen (file, "r")) == NULL)
211 adios (file, "unable to open");
216 printf ("FOLDER %s\t%s\n", file, dtimenow (1));
221 for (msgnum = 1; ; ++msgnum) {
222 state = scan (in, msgnum, -1, nfs, width, 0, 0,
223 hdrflag ? file : NULL, 0L, 1);
224 if (state != SCNMSG && state != SCNENC)
232 * We are scanning a folder
236 msgs[nummsgs++] = "all";
238 folder = getfolder (1);
239 maildir = m_maildir (folder);
241 if (chdir (maildir) == NOTOK)
242 adios (maildir, "unable to change directory to");
244 /* read folder and create message structure */
245 if (!(mp = folder_read (folder)))
246 adios (NULL, "unable to read folder %s", folder);
248 /* check for empty folder */
250 adios (NULL, "no messages in %s", folder);
252 /* parse all the message ranges/sequences and set SELECTED */
253 for (msgnum = 0; msgnum < nummsgs; msgnum++)
254 if (!m_convert (mp, msgs[msgnum]))
256 seq_setprev (mp); /* set the Previous-Sequence */
258 context_replace (pfolder, folder); /* update current folder */
259 seq_save (mp); /* synchronize message sequences */
260 context_save (); /* save the context file */
263 * Get the sequence number for each sequence
264 * specified by Unseen-Sequence
266 if ((cp = context_find (usequence)) && *cp) {
270 ap = brkstring (dp, " ", "\n");
271 for (i = 0; ap && *ap; i++, ap++)
272 seqnum[i] = seq_getnum (mp, *ap);
279 ontty = isatty (fileno (stdout));
283 fmt_current_folder = mp;
286 for (msgnum = revflag ? mp->hghsel : mp->lowsel;
287 (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
288 msgnum += (revflag ? -1 : 1)) {
289 if (is_selected(mp, msgnum)) {
290 if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
294 admonish (cp, "unable to open message");
297 printf ("%*d unreadable\n", DMAXFOLDER, msgnum);
304 printf ("FOLDER %s\t%s\n", folder, dtimenow(1));
309 * Check if message is in any sequence given
310 * by Unseen-Sequence profile entry.
313 for (i = 0; i < num_unseen_seq; i++) {
314 if (in_sequence(mp, seqnum[i], msgnum)) {
320 switch (state = scan (in, msgnum, 0, nfs, width,
321 msgnum == mp->curmsg, unseen,
329 adios (NULL, "scan() botch (%d)", state);
333 printf ("%*d empty\n", DMAXFOLDER, msgnum);
335 advise (NULL, "message %d: empty", msgnum);
347 seq_save (mp); /* because formatsbr might have made changes */
350 folder_free (mp); /* free folder/message structure */