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)
37 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
38 char *cp, *maildir, *file = NULL, *folder = NULL;
39 char *form = NULL, buf[BUFSIZ];
40 char **argp, *nfs, **arguments;
41 struct msgs_array msgs = { 0, 0, NULL };
46 setlocale(LC_ALL, "");
48 invo_name = mhbasename(argv[0]);
50 /* read user profile/context */
53 arguments = getarguments(invo_name, argc, argv, 1);
59 while ((cp = *argp++)) {
61 switch (smatch(++cp, switches)) {
63 ambigsw(cp, switches);
66 adios(NULL, "-%s unknown", cp);
69 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
70 print_help(buf, switches, 1);
73 print_version(invo_name);
77 if (!(form = *argp++) || *form == '-')
78 adios(NULL, "missing argument to %s",
83 if (!(cp = *argp++) || *cp == '-')
84 adios(NULL, "missing argument to %s",
90 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
91 adios(NULL, "missing argument to %s",
93 if (strcmp(file = cp, "-")!=0)
94 file = getcpy(expanddir(cp));
98 if (*cp == '+' || *cp == '@') {
100 adios(NULL, "only one folder at a time!");
102 folder = getcpy(expandfol(cp));
104 app_msgarg(&msgs, cp);
108 ** Get new format string. Must be before chdir().
110 nfs = new_fs(form, FORMAT);
113 ** We are scanning a maildrop file
117 adios(NULL, "\"msgs\" not allowed with -file");
119 adios(NULL, "\"+folder\" not allowed with -file");
121 /* check if "file" is really stdin */
122 if (strcmp(file, "-") == 0) {
125 } else if (!(in = fopen(file, "r"))) {
126 adios(file, "unable to open");
130 for (msgnum = 1; ; ++msgnum) {
131 state = scan(in, msgnum, -1, nfs, width, 0, 0,
133 if (state != SCNMSG && state != SCNENC)
141 ** We are scanning a folder
145 app_msgarg(&msgs, seq_all);
147 folder = getcurfol();
148 maildir = toabsdir(folder);
150 if (chdir(maildir) == NOTOK)
151 adios(maildir, "unable to change directory to");
153 /* read folder and create message structure */
154 if (!(mp = folder_read(folder)))
155 adios(NULL, "unable to read folder %s", folder);
157 /* check for empty folder */
159 adios(NULL, "no messages in %s", folder);
161 /* parse all the message ranges/sequences and set SELECTED */
162 for (msgnum = 0; msgnum < msgs.size; msgnum++)
163 if (!m_convert(mp, msgs.msgs[msgnum]))
165 seq_setprev(mp); /* set the Previous-Sequence */
167 context_replace(curfolder, folder); /* update current folder */
168 seq_save(mp); /* synchronize message sequences */
169 context_save(); /* save the context file */
172 ** Get the sequence number for each `unseen' sequence
174 if (!(cp = context_find(usequence))) {
175 cp = seq_unseen; /* use default, if not set */
181 ap = brkstring(dp, " ", "\n");
182 for (i = 0; ap && *ap; i++, ap++) {
183 seqnum[i] = seq_getnum(mp, *ap);
191 ontty = isatty(fileno(stdout));
193 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
194 if (is_selected(mp, msgnum)) {
195 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
196 admonish(cp, "unable to open message");
201 ** Check if message is in any sequence given
202 ** by Unseen-Sequence profile entry.
205 for (i = 0; i < num_unseen_seq; i++) {
206 if (in_sequence(mp, seqnum[i], msgnum)) {
212 switch (state = scan(in, msgnum, 0, nfs, width,
213 msgnum == mp->curmsg, unseen,
221 adios(NULL, "scan() botch(%d)", state);
224 advise(NULL, "message %d: empty", msgnum);
233 folder_free(mp); /* free folder/message structure */