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 */
66 arguments = getarguments(invo_name, argc, argv, 1);
72 while ((cp = *argp++)) {
74 switch (smatch(++cp, switches)) {
76 ambigsw(cp, switches);
79 adios(NULL, "-%s unknown", cp);
82 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
83 print_help(buf, switches, 1);
86 print_version(invo_name);
97 if (!(form = *argp++) || *form == '-')
98 adios(NULL, "missing argument to %s",
103 if (!(format = *argp++) || *format == '-')
104 adios(NULL, "missing argument to %s",
110 if (!(cp = *argp++) || *cp == '-')
111 adios(NULL, "missing argument to %s",
117 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
118 adios(NULL, "missing argument to %s",
120 if (strcmp(file = cp, "-")!=0)
121 file = getcpy(expanddir(cp));
125 if (*cp == '+' || *cp == '@') {
127 adios(NULL, "only one folder at a time!");
129 folder = getcpy(expandfol(cp));
131 app_msgarg(&msgs, cp);
135 ** Get new format string. Must be before chdir().
137 nfs = new_fs(form, format, FORMAT);
140 ** We are scanning a maildrop file
144 adios(NULL, "\"msgs\" not allowed with -file");
146 adios(NULL, "\"+folder\" not allowed with -file");
148 /* check if "file" is really stdin */
149 if (strcmp(file, "-") == 0) {
152 } else if (!(in = fopen(file, "r"))) {
153 adios(file, "unable to open");
157 for (msgnum = 1; ; ++msgnum) {
158 state = scan(in, msgnum, -1, nfs, width, 0, 0,
160 if (state != SCNMSG && state != SCNENC)
168 ** We are scanning a folder
172 app_msgarg(&msgs, seq_all);
174 folder = getcurfol();
175 maildir = toabsdir(folder);
177 if (chdir(maildir) == NOTOK)
178 adios(maildir, "unable to change directory to");
180 /* read folder and create message structure */
181 if (!(mp = folder_read(folder)))
182 adios(NULL, "unable to read folder %s", folder);
184 /* check for empty folder */
186 adios(NULL, "no messages in %s", folder);
188 /* parse all the message ranges/sequences and set SELECTED */
189 for (msgnum = 0; msgnum < msgs.size; msgnum++)
190 if (!m_convert(mp, msgs.msgs[msgnum]))
192 seq_setprev(mp); /* set the Previous-Sequence */
194 context_replace(curfolder, folder); /* update current folder */
195 seq_save(mp); /* synchronize message sequences */
196 context_save(); /* save the context file */
199 ** Get the sequence number for each `unseen' sequence
201 if (!(cp = context_find(usequence))) {
202 cp = seq_unseen; /* use default, if not set */
208 ap = brkstring(dp, " ", "\n");
209 for (i = 0; ap && *ap; i++, ap++) {
210 seqnum[i] = seq_getnum(mp, *ap);
218 ontty = isatty(fileno(stdout));
220 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
221 if (is_selected(mp, msgnum)) {
222 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
223 admonish(cp, "unable to open message");
228 ** Check if message is in any sequence given
229 ** by Unseen-Sequence profile entry.
232 for (i = 0; i < num_unseen_seq; i++) {
233 if (in_sequence(mp, seqnum[i], msgnum)) {
239 switch (state = scan(in, msgnum, 0, nfs, width,
240 msgnum == mp->curmsg, unseen,
248 adios(NULL, "scan() botch(%d)", state);
251 advise(NULL, "message %d: empty", msgnum);
260 folder_free(mp); /* free folder/message structure */