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>
21 * We allocate space for message names (msgs array)
22 * this number of elements at a time.
27 static struct swit switches[] = {
33 { "form formatfile", 0 },
35 { "format string", 5 },
41 { "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 msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs)));
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 msgs = (char **) mh_xrealloc (msgs,
181 (size_t) (maxmsgs * sizeof(*msgs)));
183 msgs[nummsgs++] = cp;
187 if (!context_find ("path"))
188 free (path ("./", TFOLDER));
191 * Get new format string. Must be before chdir().
193 nfs = new_fs (form, format, FORMAT);
196 * We are scanning a maildrop file
200 adios (NULL, "\"msgs\" not allowed with -file");
202 adios (NULL, "\"+folder\" not allowed with -file");
204 /* check if "file" is really stdin */
205 if (strcmp (file, "-") == 0) {
209 if ((in = fopen (file, "r")) == NULL)
210 adios (file, "unable to open");
215 printf ("FOLDER %s\t%s\n", file, dtimenow (1));
220 for (msgnum = 1; ; ++msgnum) {
221 state = scan (in, msgnum, -1, nfs, width, 0, 0,
222 hdrflag ? file : NULL, 0L, 1);
223 if (state != SCNMSG && state != SCNENC)
231 * We are scanning a folder
235 msgs[nummsgs++] = "all";
237 folder = getfolder (1);
238 maildir = m_maildir (folder);
240 if (chdir (maildir) == NOTOK)
241 adios (maildir, "unable to change directory to");
243 /* read folder and create message structure */
244 if (!(mp = folder_read (folder)))
245 adios (NULL, "unable to read folder %s", folder);
247 /* check for empty folder */
249 adios (NULL, "no messages in %s", folder);
251 /* parse all the message ranges/sequences and set SELECTED */
252 for (msgnum = 0; msgnum < nummsgs; msgnum++)
253 if (!m_convert (mp, msgs[msgnum]))
255 seq_setprev (mp); /* set the Previous-Sequence */
257 context_replace (pfolder, folder); /* update current folder */
258 seq_save (mp); /* synchronize message sequences */
259 context_save (); /* save the context file */
262 * Get the sequence number for each sequence
263 * specified by Unseen-Sequence
265 if ((cp = context_find (usequence)) && *cp) {
269 ap = brkstring (dp, " ", "\n");
270 for (i = 0; ap && *ap; i++, ap++)
271 seqnum[i] = seq_getnum (mp, *ap);
278 ontty = isatty (fileno (stdout));
282 fmt_current_folder = mp;
285 for (msgnum = revflag ? mp->hghsel : mp->lowsel;
286 (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
287 msgnum += (revflag ? -1 : 1)) {
288 if (is_selected(mp, msgnum)) {
289 if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
293 admonish (cp, "unable to open message");
296 printf ("%*d unreadable\n", DMAXFOLDER, msgnum);
303 printf ("FOLDER %s\t%s\n", folder, dtimenow(1));
308 * Check if message is in any sequence given
309 * by Unseen-Sequence profile entry.
312 for (i = 0; i < num_unseen_seq; i++) {
313 if (in_sequence(mp, seqnum[i], msgnum)) {
319 switch (state = scan (in, msgnum, 0, nfs, width,
320 msgnum == mp->curmsg, unseen,
328 adios (NULL, "scan() botch (%d)", state);
332 printf ("%*d empty\n", DMAXFOLDER, msgnum);
334 advise (NULL, "message %d: empty", msgnum);
346 seq_save (mp); /* because formatsbr might have made changes */
349 folder_free (mp); /* free folder/message structure */