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 },
45 void clear_screen(void); /* from termsbr.c */
49 main(int argc, char **argv)
51 int clearflag = 0, ontty;
52 int width = 0, revflag = 0;
54 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
55 char *cp, *maildir, *file = NULL, *folder = NULL;
56 char *form = NULL, *format = NULL, buf[BUFSIZ];
57 char **argp, *nfs, **arguments;
58 struct msgs_array msgs = { 0, 0, NULL };
63 setlocale(LC_ALL, "");
65 invo_name = mhbasename(argv[0]);
67 /* read user profile/context */
71 arguments = getarguments(invo_name, argc, argv, 1);
77 while ((cp = *argp++)) {
79 switch (smatch(++cp, switches)) {
81 ambigsw(cp, switches);
84 adios(NULL, "-%s unknown", cp);
87 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
88 print_help(buf, switches, 1);
91 print_version(invo_name);
102 if (!(form = *argp++) || *form == '-')
103 adios(NULL, "missing argument to %s",
108 if (!(format = *argp++) || *format == '-')
109 adios(NULL, "missing argument to %s",
115 if (!(cp = *argp++) || *cp == '-')
116 adios(NULL, "missing argument to %s",
128 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
129 adios(NULL, "missing argument to %s",
131 if (strcmp(file = cp, "-")!=0)
132 file = getcpy(expanddir(cp));
136 if (*cp == '+' || *cp == '@') {
138 adios(NULL, "only one folder at a time!");
140 folder = getcpy(expandfol(cp));
142 app_msgarg(&msgs, cp);
146 ** Get new format string. Must be before chdir().
148 nfs = new_fs(form, format, FORMAT);
151 ** We are scanning a maildrop file
155 adios(NULL, "\"msgs\" not allowed with -file");
157 adios(NULL, "\"+folder\" not allowed with -file");
159 /* check if "file" is really stdin */
160 if (strcmp(file, "-") == 0) {
163 } else if (!(in = fopen(file, "r"))) {
164 adios(file, "unable to open");
168 for (msgnum = 1; ; ++msgnum) {
169 state = scan(in, msgnum, -1, nfs, width, 0, 0,
171 if (state != SCNMSG && state != SCNENC)
179 ** We are scanning a folder
183 app_msgarg(&msgs, seq_all);
185 folder = getcurfol();
186 maildir = toabsdir(folder);
188 if (chdir(maildir) == NOTOK)
189 adios(maildir, "unable to change directory to");
191 /* read folder and create message structure */
192 if (!(mp = folder_read(folder)))
193 adios(NULL, "unable to read folder %s", folder);
195 /* check for empty folder */
197 adios(NULL, "no messages in %s", folder);
199 /* parse all the message ranges/sequences and set SELECTED */
200 for (msgnum = 0; msgnum < msgs.size; msgnum++)
201 if (!m_convert(mp, msgs.msgs[msgnum]))
203 seq_setprev(mp); /* set the Previous-Sequence */
205 context_replace(curfolder, folder); /* update current folder */
206 seq_save(mp); /* synchronize message sequences */
207 context_save(); /* save the context file */
210 ** Get the sequence number for each `unseen' sequence
212 if (!(cp = context_find(usequence))) {
213 cp = seq_unseen; /* use default, if not set */
219 ap = brkstring(dp, " ", "\n");
220 for (i = 0; ap && *ap; i++, ap++) {
221 seqnum[i] = seq_getnum(mp, *ap);
229 ontty = isatty(fileno(stdout));
231 for (msgnum = revflag ? mp->hghsel : mp->lowsel;
232 (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
233 msgnum += (revflag ? -1 : 1)) {
234 if (is_selected(mp, msgnum)) {
235 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
236 admonish(cp, "unable to open message");
241 ** Check if message is in any sequence given
242 ** by Unseen-Sequence profile entry.
245 for (i = 0; i < num_unseen_seq; i++) {
246 if (in_sequence(mp, seqnum[i], msgnum)) {
252 switch (state = scan(in, msgnum, 0, nfs, width,
253 msgnum == mp->curmsg, unseen,
261 adios(NULL, "scan() botch(%d)", state);
264 advise(NULL, "message %d: empty", msgnum);
273 folder_free(mp); /* free folder/message structure */