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 },
57 * global for sbr/formatsbr.c - yech!
60 extern struct msgs *fmt_current_folder;
66 void clear_screen(void); /* from termsbr.c */
70 main (int argc, char **argv)
72 int clearflag = 0, hdrflag = 0, ontty;
73 int width = 0, revflag = 0;
74 int i, state, msgnum, nummsgs, maxmsgs;
75 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
76 char *cp, *maildir, *file = NULL, *folder = NULL;
77 char *form = NULL, *format = NULL, buf[BUFSIZ];
78 char **argp, *nfs, **arguments, **msgs;
83 setlocale(LC_ALL, "");
85 invo_name = r1bindex (argv[0], '/');
87 /* read user profile/context */
91 arguments = getarguments (invo_name, argc, argv, 1);
95 * Allocate the initial space to record message
96 * names, ranges, and sequences.
100 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
101 adios (NULL, "unable to allocate storage");
106 while ((cp = *argp++)) {
108 switch (smatch (++cp, switches)) {
110 ambigsw (cp, switches);
113 adios (NULL, "-%s unknown", cp);
116 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
118 print_help (buf, switches, 1);
121 print_version(invo_name);
132 if (!(form = *argp++) || *form == '-')
133 adios (NULL, "missing argument to %s", argp[-2]);
137 if (!(format = *argp++) || *format == '-')
138 adios (NULL, "missing argument to %s", argp[-2]);
150 if (!(cp = *argp++) || *cp == '-')
151 adios (NULL, "missing argument to %s", argp[-2]);
162 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
163 adios (NULL, "missing argument to %s", argp[-2]);
164 if (strcmp (file = cp, "-"))
165 file = path (cp, TFILE);
169 if (*cp == '+' || *cp == '@') {
171 adios (NULL, "only one folder at a time!");
173 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
176 * Check if we need to allocate more space
177 * for message names/ranges/sequences.
179 if (nummsgs >= maxmsgs) {
181 if (!(msgs = (char **) realloc (msgs,
182 (size_t) (maxmsgs * sizeof(*msgs)))))
183 adios (NULL, "unable to reallocate msgs storage");
185 msgs[nummsgs++] = cp;
189 if (!context_find ("path"))
190 free (path ("./", TFOLDER));
193 * Get new format string. Must be before chdir().
195 nfs = new_fs (form, format, FORMAT);
198 * We are scanning a maildrop file
202 adios (NULL, "\"msgs\" not allowed with -file");
204 adios (NULL, "\"+folder\" not allowed with -file");
206 /* check if "file" is really stdin */
207 if (strcmp (file, "-") == 0) {
211 if ((in = fopen (file, "r")) == NULL)
212 adios (file, "unable to open");
217 printf ("FOLDER %s\t%s\n", file, dtimenow (1));
222 for (msgnum = 1; ; ++msgnum) {
223 state = scan (in, msgnum, -1, nfs, width, 0, 0,
224 hdrflag ? file : NULL, 0L, 1);
225 if (state != SCNMSG && state != SCNENC)
233 * We are scanning a folder
237 msgs[nummsgs++] = "all";
239 folder = getfolder (1);
240 maildir = m_maildir (folder);
242 if (chdir (maildir) == NOTOK)
243 adios (maildir, "unable to change directory to");
245 /* read folder and create message structure */
246 if (!(mp = folder_read (folder)))
247 adios (NULL, "unable to read folder %s", folder);
249 /* check for empty folder */
251 adios (NULL, "no messages in %s", folder);
253 /* parse all the message ranges/sequences and set SELECTED */
254 for (msgnum = 0; msgnum < nummsgs; msgnum++)
255 if (!m_convert (mp, msgs[msgnum]))
257 seq_setprev (mp); /* set the Previous-Sequence */
259 context_replace (pfolder, folder); /* update current folder */
260 seq_save (mp); /* synchronize message sequences */
261 context_save (); /* save the context file */
264 * Get the sequence number for each sequence
265 * specified by Unseen-Sequence
267 if ((cp = context_find (usequence)) && *cp) {
271 ap = brkstring (dp, " ", "\n");
272 for (i = 0; ap && *ap; i++, ap++)
273 seqnum[i] = seq_getnum (mp, *ap);
280 ontty = isatty (fileno (stdout));
284 fmt_current_folder = mp;
287 for (msgnum = revflag ? mp->hghsel : mp->lowsel;
288 (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
289 msgnum += (revflag ? -1 : 1)) {
290 if (is_selected(mp, msgnum)) {
291 if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
295 admonish (cp, "unable to open message");
298 printf ("%*d unreadable\n", DMAXFOLDER, msgnum);
305 printf ("FOLDER %s\t%s\n", folder, dtimenow(1));
310 * Check if message is in any sequence given
311 * by Unseen-Sequence profile entry.
314 for (i = 0; i < num_unseen_seq; i++) {
315 if (in_sequence(mp, seqnum[i], msgnum)) {
321 switch (state = scan (in, msgnum, 0, nfs, width,
322 msgnum == mp->curmsg, unseen,
323 hdrflag ? folder : NULL, 0L, 1)) {
330 adios (NULL, "scan() botch (%d)", state);
334 printf ("%*d empty\n", DMAXFOLDER, msgnum);
336 advise (NULL, "message %d: empty", msgnum);
348 seq_save (mp); /* because formatsbr might have made changes */
351 folder_free (mp); /* free folder/message structure */