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 };
45 setlocale(LC_ALL, "");
47 invo_name = mhbasename(argv[0]);
49 /* read user profile/context */
52 arguments = getarguments(invo_name, argc, argv, 1);
58 while ((cp = *argp++)) {
60 switch (smatch(++cp, switches)) {
62 ambigsw(cp, switches);
65 adios(NULL, "-%s unknown", cp);
68 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
69 print_help(buf, switches, 1);
72 print_version(invo_name);
76 if (!(form = *argp++) || *form == '-')
77 adios(NULL, "missing argument to %s",
82 if (!(cp = *argp++) || *cp == '-')
83 adios(NULL, "missing argument to %s",
89 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
90 adios(NULL, "missing argument to %s",
92 if (strcmp(file = cp, "-")!=0)
93 file = getcpy(expanddir(cp));
97 if (*cp == '+' || *cp == '@') {
99 adios(NULL, "only one folder at a time!");
101 folder = getcpy(expandfol(cp));
103 app_msgarg(&msgs, cp);
107 ** Get new format string. Must be before chdir().
109 fmtstr = new_fs(form, FORMAT);
112 ** We are scanning a maildrop file
116 adios(NULL, "\"msgs\" not allowed with -file");
118 adios(NULL, "\"+folder\" not allowed with -file");
120 /* check if "file" is really stdin */
121 if (strcmp(file, "-") == 0) {
124 } else if (!(in = fopen(file, "r"))) {
125 adios(file, "unable to open");
129 for (msgnum = 1; ; ++msgnum) {
130 state = scan(in, msgnum, SCN_MBOX, fmtstr, width, 0, 0);
139 ** We are scanning a folder
143 app_msgarg(&msgs, seq_all);
145 folder = getcurfol();
146 maildir = toabsdir(folder);
148 if (chdir(maildir) == NOTOK)
149 adios(maildir, "unable to change directory to");
151 /* read folder and create message structure */
152 if (!(mp = folder_read(folder)))
153 adios(NULL, "unable to read folder %s", folder);
155 /* check for empty folder */
157 adios(NULL, "no messages in %s", folder);
159 /* parse all the message ranges/sequences and set SELECTED */
160 for (msgnum = 0; msgnum < msgs.size; msgnum++)
161 if (!m_convert(mp, msgs.msgs[msgnum]))
163 seq_setprev(mp); /* set the Previous-Sequence */
165 context_replace(curfolder, folder); /* update current folder */
166 seq_save(mp); /* synchronize message sequences */
167 context_save(); /* save the context file */
170 ** Get the sequence number for each `unseen' sequence
172 if (!(cp = context_find(usequence))) {
173 cp = seq_unseen; /* use default, if not set */
179 ap = brkstring(dp, " ", "\n");
180 for (i = 0; ap && *ap; i++, ap++) {
181 seqnum[i] = seq_getnum(mp, *ap);
189 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
190 if (is_selected(mp, msgnum)) {
191 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
192 admonish(cp, "unable to open message");
197 ** Check if message is in any sequence given
198 ** by Unseen-Sequence profile entry.
201 for (i = 0; i < num_unseen_seq; i++) {
202 if (in_sequence(mp, seqnum[i], msgnum)) {
208 switch (state = scan(in, msgnum, SCN_FOLD, fmtstr,
209 width, msgnum==mp->curmsg, unseen)) {
215 adios(NULL, "scan() botch(%d)", state);
218 advise(NULL, "message %d: empty", msgnum);
225 folder_free(mp); /* free folder/message structure */