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 },
27 { "width columns", 0 },
41 void clear_screen(void); /* from termsbr.c */
45 main(int argc, char **argv)
47 int clearflag = 0, ontty;
50 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
51 char *cp, *maildir, *file = NULL, *folder = NULL;
52 char *form = NULL, *format = NULL, buf[BUFSIZ];
53 char **argp, *nfs, **arguments;
54 struct msgs_array msgs = { 0, 0, NULL };
59 setlocale(LC_ALL, "");
61 invo_name = mhbasename(argv[0]);
63 /* read user profile/context */
67 arguments = getarguments(invo_name, argc, argv, 1);
73 while ((cp = *argp++)) {
75 switch (smatch(++cp, switches)) {
77 ambigsw(cp, switches);
80 adios(NULL, "-%s unknown", cp);
83 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
84 print_help(buf, switches, 1);
87 print_version(invo_name);
98 if (!(form = *argp++) || *form == '-')
99 adios(NULL, "missing argument to %s",
104 if (!(format = *argp++) || *format == '-')
105 adios(NULL, "missing argument to %s",
111 if (!(cp = *argp++) || *cp == '-')
112 adios(NULL, "missing argument to %s",
118 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
119 adios(NULL, "missing argument to %s",
121 if (strcmp(file = cp, "-")!=0)
122 file = getcpy(expanddir(cp));
126 if (*cp == '+' || *cp == '@') {
128 adios(NULL, "only one folder at a time!");
130 folder = getcpy(expandfol(cp));
132 app_msgarg(&msgs, cp);
136 ** Get new format string. Must be before chdir().
138 nfs = new_fs(form, format, FORMAT);
141 ** We are scanning a maildrop file
145 adios(NULL, "\"msgs\" not allowed with -file");
147 adios(NULL, "\"+folder\" not allowed with -file");
149 /* check if "file" is really stdin */
150 if (strcmp(file, "-") == 0) {
153 } else if (!(in = fopen(file, "r"))) {
154 adios(file, "unable to open");
158 for (msgnum = 1; ; ++msgnum) {
159 state = scan(in, msgnum, -1, nfs, width, 0, 0,
161 if (state != SCNMSG && state != SCNENC)
169 ** We are scanning a folder
173 app_msgarg(&msgs, seq_all);
175 folder = getcurfol();
176 maildir = toabsdir(folder);
178 if (chdir(maildir) == NOTOK)
179 adios(maildir, "unable to change directory to");
181 /* read folder and create message structure */
182 if (!(mp = folder_read(folder)))
183 adios(NULL, "unable to read folder %s", folder);
185 /* check for empty folder */
187 adios(NULL, "no messages in %s", folder);
189 /* parse all the message ranges/sequences and set SELECTED */
190 for (msgnum = 0; msgnum < msgs.size; msgnum++)
191 if (!m_convert(mp, msgs.msgs[msgnum]))
193 seq_setprev(mp); /* set the Previous-Sequence */
195 context_replace(curfolder, folder); /* update current folder */
196 seq_save(mp); /* synchronize message sequences */
197 context_save(); /* save the context file */
200 ** Get the sequence number for each `unseen' sequence
202 if (!(cp = context_find(usequence))) {
203 cp = seq_unseen; /* use default, if not set */
209 ap = brkstring(dp, " ", "\n");
210 for (i = 0; ap && *ap; i++, ap++) {
211 seqnum[i] = seq_getnum(mp, *ap);
219 ontty = isatty(fileno(stdout));
221 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
222 if (is_selected(mp, msgnum)) {
223 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
224 admonish(cp, "unable to open message");
229 ** Check if message is in any sequence given
230 ** by Unseen-Sequence profile entry.
233 for (i = 0; i < num_unseen_seq; i++) {
234 if (in_sequence(mp, seqnum[i], msgnum)) {
240 switch (state = scan(in, msgnum, 0, nfs, width,
241 msgnum == mp->curmsg, unseen,
249 adios(NULL, "scan() botch(%d)", state);
252 advise(NULL, "message %d: empty", msgnum);
261 folder_free(mp); /* free folder/message structure */