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[] = {
18 { "form formatfile", 0 },
20 { "width columns", 0 },
32 main(int argc, char **argv)
36 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
37 char *cp, *maildir, *file = NULL, *folder = NULL;
38 char *form = NULL, buf[BUFSIZ];
39 char **argp, *fmtstr, **arguments;
40 struct msgs_array msgs = { 0, 0, NULL };
44 setlocale(LC_ALL, "");
45 invo_name = mhbasename(argv[0]);
47 /* read user profile/context */
50 arguments = getarguments(invo_name, argc, argv, 1);
56 while ((cp = *argp++)) {
58 switch (smatch(++cp, switches)) {
60 ambigsw(cp, switches);
63 adios(NULL, "-%s unknown", cp);
66 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
67 print_help(buf, switches, 1);
70 print_version(invo_name);
74 if (!(form = *argp++) || *form == '-')
75 adios(NULL, "missing argument to %s",
80 if (!(cp = *argp++) || *cp == '-')
81 adios(NULL, "missing argument to %s",
87 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
88 adios(NULL, "missing argument to %s",
90 if (strcmp(file = cp, "-")!=0)
91 file = getcpy(expanddir(cp));
95 if (*cp == '+' || *cp == '@') {
97 adios(NULL, "only one folder at a time!");
99 folder = getcpy(expandfol(cp));
101 app_msgarg(&msgs, cp);
105 ** Get new format string. Must be before chdir().
107 fmtstr = new_fs(form, FORMAT);
110 ** We are scanning a maildrop file
114 adios(NULL, "\"msgs\" not allowed with -file");
116 adios(NULL, "\"+folder\" not allowed with -file");
118 /* check if "file" is really stdin */
119 if (strcmp(file, "-") == 0) {
122 } else if (!(in = fopen(file, "r"))) {
123 adios(file, "unable to open");
127 for (msgnum = 1; ; ++msgnum) {
128 state = scan(in, msgnum, SCN_MBOX, fmtstr, width, 0, 0);
137 ** We are scanning a folder
141 app_msgarg(&msgs, seq_all);
143 folder = getcurfol();
144 maildir = toabsdir(folder);
146 if (chdir(maildir) == NOTOK)
147 adios(maildir, "unable to change directory to");
149 /* read folder and create message structure */
150 if (!(mp = folder_read(folder)))
151 adios(NULL, "unable to read folder %s", folder);
153 /* check for empty folder */
155 adios(NULL, "no messages in %s", folder);
157 /* parse all the message ranges/sequences and set SELECTED */
158 for (msgnum = 0; msgnum < msgs.size; msgnum++)
159 if (!m_convert(mp, msgs.msgs[msgnum]))
161 seq_setprev(mp); /* set the Previous-Sequence */
163 context_replace(curfolder, folder); /* update current folder */
164 seq_save(mp); /* synchronize message sequences */
165 context_save(); /* save the context file */
168 ** Get the sequence number for each `unseen' sequence
170 if (!(cp = context_find(usequence))) {
171 cp = seq_unseen; /* use default, if not set */
177 ap = brkstring(dp, " ", "\n");
178 for (i = 0; ap && *ap; i++, ap++) {
179 seqnum[i] = seq_getnum(mp, *ap);
187 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
188 if (is_selected(mp, msgnum)) {
189 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
190 admonish(cp, "unable to open message");
195 ** Check if message is in any sequence given
196 ** by Unseen-Sequence profile entry.
199 for (i = 0; i < num_unseen_seq; i++) {
200 if (in_sequence(mp, seqnum[i], msgnum)) {
206 switch (state = scan(in, msgnum, SCN_FOLD, fmtstr,
207 width, msgnum==mp->curmsg, unseen)) {
213 adios(NULL, "scan() botch(%d)", state);
216 advise(NULL, "message %d: empty", msgnum);
223 folder_free(mp); /* free folder/message structure */