2 ** scan.c -- display a one-line "scan" listing of folder or messages
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 #include <h/fmt_scan.h>
11 #include <h/scansbr.h>
17 static struct swit switches[] = {
23 { "form formatfile", 0 },
25 { "format string", 5 },
31 { "width columns", 0 },
49 void clear_screen(void); /* from termsbr.c */
53 main(int argc, char **argv)
55 int clearflag = 0, hdrflag = 0, ontty;
56 int width = 0, revflag = 0;
58 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
59 char *cp, *maildir, *file = NULL, *folder = NULL;
60 char *form = NULL, *format = NULL, buf[BUFSIZ];
61 char **argp, *nfs, **arguments;
62 struct msgs_array msgs = { 0, 0, NULL };
67 setlocale(LC_ALL, "");
69 invo_name = mhbasename(argv[0]);
71 /* read user profile/context */
75 arguments = getarguments(invo_name, argc, argv, 1);
81 while ((cp = *argp++)) {
83 switch (smatch(++cp, switches)) {
85 ambigsw(cp, switches);
88 adios(NULL, "-%s unknown", cp);
91 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
92 print_help(buf, switches, 1);
95 print_version(invo_name);
106 if (!(form = *argp++) || *form == '-')
107 adios(NULL, "missing argument to %s",
112 if (!(format = *argp++) || *format == '-')
113 adios(NULL, "missing argument to %s",
126 if (!(cp = *argp++) || *cp == '-')
127 adios(NULL, "missing argument to %s",
139 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
140 adios(NULL, "missing argument to %s",
142 if (strcmp(file = cp, "-")!=0)
143 file = getcpy(expanddir(cp));
147 if (*cp == '+' || *cp == '@') {
149 adios(NULL, "only one folder at a time!");
151 folder = getcpy(expandfol(cp));
153 app_msgarg(&msgs, cp);
157 ** Get new format string. Must be before chdir().
159 nfs = new_fs(form, format, FORMAT);
162 ** We are scanning a maildrop file
166 adios(NULL, "\"msgs\" not allowed with -file");
168 adios(NULL, "\"+folder\" not allowed with -file");
170 /* check if "file" is really stdin */
171 if (strcmp(file, "-") == 0) {
174 } else if (!(in = fopen(file, "r"))) {
175 adios(file, "unable to open");
180 printf("FOLDER %s\t%s\n", file, dtimenow(1));
185 for (msgnum = 1; ; ++msgnum) {
186 state = scan(in, msgnum, -1, nfs, width, 0, 0,
187 hdrflag ? file : NULL, 0L, 1);
188 if (state != SCNMSG && state != SCNENC)
196 ** We are scanning a folder
200 app_msgarg(&msgs, seq_all);
202 folder = getcurfol();
203 maildir = toabsdir(folder);
205 if (chdir(maildir) == NOTOK)
206 adios(maildir, "unable to change directory to");
208 /* read folder and create message structure */
209 if (!(mp = folder_read(folder)))
210 adios(NULL, "unable to read folder %s", folder);
212 /* check for empty folder */
214 adios(NULL, "no messages in %s", folder);
216 /* parse all the message ranges/sequences and set SELECTED */
217 for (msgnum = 0; msgnum < msgs.size; msgnum++)
218 if (!m_convert(mp, msgs.msgs[msgnum]))
220 seq_setprev(mp); /* set the Previous-Sequence */
222 context_replace(curfolder, folder); /* update current folder */
223 seq_save(mp); /* synchronize message sequences */
224 context_save(); /* save the context file */
227 ** Get the sequence number for each `unseen' sequence
229 if (!(cp = context_find(usequence))) {
230 cp = seq_unseen; /* use default, if not set */
236 ap = brkstring(dp, " ", "\n");
237 for (i = 0; ap && *ap; i++, ap++) {
238 seqnum[i] = seq_getnum(mp, *ap);
246 ontty = isatty(fileno(stdout));
248 for (msgnum = revflag ? mp->hghsel : mp->lowsel;
249 (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
250 msgnum += (revflag ? -1 : 1)) {
251 if (is_selected(mp, msgnum)) {
252 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
253 admonish(cp, "unable to open message");
259 printf("FOLDER %s\t%s\n", folder, dtimenow(1));
264 ** Check if message is in any sequence given
265 ** by Unseen-Sequence profile entry.
268 for (i = 0; i < num_unseen_seq; i++) {
269 if (in_sequence(mp, seqnum[i], msgnum)) {
275 switch (state = scan(in, msgnum, 0, nfs, width,
276 msgnum == mp->curmsg, unseen,
284 adios(NULL, "scan() botch(%d)", state);
287 advise(NULL, "message %d: empty", msgnum);
297 folder_free(mp); /* free folder/message structure */