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 { "width columns", 0 },
38 void clear_screen(void); /* from termsbr.c */
42 main(int argc, char **argv)
44 int clearflag = 0, ontty;
47 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
48 char *cp, *maildir, *file = NULL, *folder = NULL;
49 char *form = NULL, buf[BUFSIZ];
50 char **argp, *nfs, **arguments;
51 struct msgs_array msgs = { 0, 0, NULL };
56 setlocale(LC_ALL, "");
58 invo_name = mhbasename(argv[0]);
60 /* read user profile/context */
63 arguments = getarguments(invo_name, argc, argv, 1);
69 while ((cp = *argp++)) {
71 switch (smatch(++cp, switches)) {
73 ambigsw(cp, switches);
76 adios(NULL, "-%s unknown", cp);
79 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
80 print_help(buf, switches, 1);
83 print_version(invo_name);
94 if (!(form = *argp++) || *form == '-')
95 adios(NULL, "missing argument to %s",
100 if (!(cp = *argp++) || *cp == '-')
101 adios(NULL, "missing argument to %s",
107 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
108 adios(NULL, "missing argument to %s",
110 if (strcmp(file = cp, "-")!=0)
111 file = getcpy(expanddir(cp));
115 if (*cp == '+' || *cp == '@') {
117 adios(NULL, "only one folder at a time!");
119 folder = getcpy(expandfol(cp));
121 app_msgarg(&msgs, cp);
125 ** Get new format string. Must be before chdir().
127 nfs = new_fs(form, FORMAT);
130 ** We are scanning a maildrop file
134 adios(NULL, "\"msgs\" not allowed with -file");
136 adios(NULL, "\"+folder\" not allowed with -file");
138 /* check if "file" is really stdin */
139 if (strcmp(file, "-") == 0) {
142 } else if (!(in = fopen(file, "r"))) {
143 adios(file, "unable to open");
147 for (msgnum = 1; ; ++msgnum) {
148 state = scan(in, msgnum, -1, nfs, width, 0, 0,
150 if (state != SCNMSG && state != SCNENC)
158 ** We are scanning a folder
162 app_msgarg(&msgs, seq_all);
164 folder = getcurfol();
165 maildir = toabsdir(folder);
167 if (chdir(maildir) == NOTOK)
168 adios(maildir, "unable to change directory to");
170 /* read folder and create message structure */
171 if (!(mp = folder_read(folder)))
172 adios(NULL, "unable to read folder %s", folder);
174 /* check for empty folder */
176 adios(NULL, "no messages in %s", folder);
178 /* parse all the message ranges/sequences and set SELECTED */
179 for (msgnum = 0; msgnum < msgs.size; msgnum++)
180 if (!m_convert(mp, msgs.msgs[msgnum]))
182 seq_setprev(mp); /* set the Previous-Sequence */
184 context_replace(curfolder, folder); /* update current folder */
185 seq_save(mp); /* synchronize message sequences */
186 context_save(); /* save the context file */
189 ** Get the sequence number for each `unseen' sequence
191 if (!(cp = context_find(usequence))) {
192 cp = seq_unseen; /* use default, if not set */
198 ap = brkstring(dp, " ", "\n");
199 for (i = 0; ap && *ap; i++, ap++) {
200 seqnum[i] = seq_getnum(mp, *ap);
208 ontty = isatty(fileno(stdout));
210 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
211 if (is_selected(mp, msgnum)) {
212 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
213 admonish(cp, "unable to open message");
218 ** Check if message is in any sequence given
219 ** by Unseen-Sequence profile entry.
222 for (i = 0; i < num_unseen_seq; i++) {
223 if (in_sequence(mp, seqnum[i], msgnum)) {
229 switch (state = scan(in, msgnum, 0, nfs, width,
230 msgnum == mp->curmsg, unseen,
238 adios(NULL, "scan() botch(%d)", state);
241 advise(NULL, "message %d: empty", msgnum);
250 folder_free(mp); /* free folder/message structure */