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");
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)
190 ** We are scanning a folder
194 app_msgarg(&msgs, seq_all);
196 folder = getcurfol();
197 maildir = toabsdir(folder);
199 if (chdir(maildir) == NOTOK)
200 adios(maildir, "unable to change directory to");
202 /* read folder and create message structure */
203 if (!(mp = folder_read(folder)))
204 adios(NULL, "unable to read folder %s", folder);
206 /* check for empty folder */
208 adios(NULL, "no messages in %s", folder);
210 /* parse all the message ranges/sequences and set SELECTED */
211 for (msgnum = 0; msgnum < msgs.size; msgnum++)
212 if (!m_convert(mp, msgs.msgs[msgnum]))
214 seq_setprev(mp); /* set the Previous-Sequence */
216 context_replace(curfolder, folder); /* update current folder */
217 seq_save(mp); /* synchronize message sequences */
218 context_save(); /* save the context file */
221 ** Get the sequence number for each `unseen' sequence
223 if (!(cp = context_find(usequence))) {
224 cp = seq_unseen; /* use default, if not set */
230 ap = brkstring(dp, " ", "\n");
231 for (i = 0; ap && *ap; i++, ap++) {
232 seqnum[i] = seq_getnum(mp, *ap);
240 ontty = isatty(fileno(stdout));
242 for (msgnum = revflag ? mp->hghsel : mp->lowsel;
243 (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
244 msgnum += (revflag ? -1 : 1)) {
245 if (is_selected(mp, msgnum)) {
246 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
247 admonish(cp, "unable to open message");
252 ** Check if message is in any sequence given
253 ** by Unseen-Sequence profile entry.
256 for (i = 0; i < num_unseen_seq; i++) {
257 if (in_sequence(mp, seqnum[i], msgnum)) {
263 switch (state = scan(in, msgnum, 0, nfs, width,
264 msgnum == mp->curmsg, unseen,
272 adios(NULL, "scan() botch(%d)", state);
275 advise(NULL, "message %d: empty", msgnum);
285 folder_free(mp); /* free folder/message structure */