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>
16 static struct swit switches[] = {
22 { "form formatfile", 0 },
24 { "format string", 5 },
26 { "width columns", 0 },
40 void clear_screen(void); /* from termsbr.c */
44 main(int argc, char **argv)
46 int clearflag = 0, ontty;
49 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
50 char *cp, *maildir, *file = NULL, *folder = NULL;
51 char *form = NULL, *format = NULL, buf[BUFSIZ];
52 char **argp, *nfs, **arguments;
53 struct msgs_array msgs = { 0, 0, NULL };
58 setlocale(LC_ALL, "");
60 invo_name = mhbasename(argv[0]);
62 /* read user profile/context */
65 arguments = getarguments(invo_name, argc, argv, 1);
71 while ((cp = *argp++)) {
73 switch (smatch(++cp, switches)) {
75 ambigsw(cp, switches);
78 adios(NULL, "-%s unknown", cp);
81 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
82 print_help(buf, switches, 1);
85 print_version(invo_name);
96 if (!(form = *argp++) || *form == '-')
97 adios(NULL, "missing argument to %s",
102 if (!(format = *argp++) || *format == '-')
103 adios(NULL, "missing argument to %s",
109 if (!(cp = *argp++) || *cp == '-')
110 adios(NULL, "missing argument to %s",
116 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
117 adios(NULL, "missing argument to %s",
119 if (strcmp(file = cp, "-")!=0)
120 file = getcpy(expanddir(cp));
124 if (*cp == '+' || *cp == '@') {
126 adios(NULL, "only one folder at a time!");
128 folder = getcpy(expandfol(cp));
130 app_msgarg(&msgs, cp);
134 ** Get new format string. Must be before chdir().
136 nfs = new_fs(form, format, FORMAT);
139 ** We are scanning a maildrop file
143 adios(NULL, "\"msgs\" not allowed with -file");
145 adios(NULL, "\"+folder\" not allowed with -file");
147 /* check if "file" is really stdin */
148 if (strcmp(file, "-") == 0) {
151 } else if (!(in = fopen(file, "r"))) {
152 adios(file, "unable to open");
156 for (msgnum = 1; ; ++msgnum) {
157 state = scan(in, msgnum, -1, nfs, width, 0, 0,
159 if (state != SCNMSG && state != SCNENC)
167 ** We are scanning a folder
171 app_msgarg(&msgs, seq_all);
173 folder = getcurfol();
174 maildir = toabsdir(folder);
176 if (chdir(maildir) == NOTOK)
177 adios(maildir, "unable to change directory to");
179 /* read folder and create message structure */
180 if (!(mp = folder_read(folder)))
181 adios(NULL, "unable to read folder %s", folder);
183 /* check for empty folder */
185 adios(NULL, "no messages in %s", folder);
187 /* parse all the message ranges/sequences and set SELECTED */
188 for (msgnum = 0; msgnum < msgs.size; msgnum++)
189 if (!m_convert(mp, msgs.msgs[msgnum]))
191 seq_setprev(mp); /* set the Previous-Sequence */
193 context_replace(curfolder, folder); /* update current folder */
194 seq_save(mp); /* synchronize message sequences */
195 context_save(); /* save the context file */
198 ** Get the sequence number for each `unseen' sequence
200 if (!(cp = context_find(usequence))) {
201 cp = seq_unseen; /* use default, if not set */
207 ap = brkstring(dp, " ", "\n");
208 for (i = 0; ap && *ap; i++, ap++) {
209 seqnum[i] = seq_getnum(mp, *ap);
217 ontty = isatty(fileno(stdout));
219 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
220 if (is_selected(mp, msgnum)) {
221 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
222 admonish(cp, "unable to open message");
227 ** Check if message is in any sequence given
228 ** by Unseen-Sequence profile entry.
231 for (i = 0; i < num_unseen_seq; i++) {
232 if (in_sequence(mp, seqnum[i], msgnum)) {
238 switch (state = scan(in, msgnum, 0, nfs, width,
239 msgnum == mp->curmsg, unseen,
247 adios(NULL, "scan() botch(%d)", state);
250 advise(NULL, "message %d: empty", msgnum);
259 folder_free(mp); /* free folder/message structure */