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>
19 static struct swit switches[] = {
21 { "form formatfile", 0 },
23 { "width columns", 0 },
35 main(int argc, char **argv)
39 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
40 char *cp, *maildir, *file = NULL, *folder = NULL;
41 char *form = NULL, buf[BUFSIZ];
42 char **argp, *fmtstr, **arguments;
43 struct msgs_array msgs = { 0, 0, NULL };
47 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(EX_USAGE, NULL, "-%s unknown", cp);
69 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
70 print_help(buf, switches, 1);
71 exit(argc == 2 ? EX_OK : EX_USAGE);
73 print_version(invo_name);
74 exit(argc == 2 ? EX_OK : EX_USAGE);
77 if (!(form = *argp++) || *form == '-')
78 adios(EX_USAGE, NULL, "missing argument to %s",
83 if (!(cp = *argp++) || *cp == '-')
84 adios(EX_USAGE, NULL, "missing argument to %s",
90 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
91 adios(EX_USAGE, NULL, "missing argument to %s",
93 if (strcmp(file = cp, "-")!=0)
94 file = getcpy(expanddir(cp));
98 if (*cp == '+' || *cp == '@') {
100 adios(EX_USAGE, 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 fmtstr = new_fs(form, FORMAT);
113 ** We are scanning a maildrop file
117 adios(EX_USAGE, NULL, "\"msgs\" not allowed with -file");
119 adios(EX_USAGE, 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(EX_IOERR, 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(EX_OSERR, maildir, "unable to change directory to");
151 if (!(mp = folder_read(folder)))
152 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
154 /* check for empty folder */
156 adios(EX_DATAERR, NULL, "no messages in %s", folder);
158 /* parse all the message ranges/sequences and set SELECTED */
159 for (msgnum = 0; msgnum < msgs.size; msgnum++)
160 if (!m_convert(mp, msgs.msgs[msgnum]))
164 context_replace(curfolder, folder);
169 ** Get the sequence number for each `unseen' sequence
171 if (!(cp = context_find(usequence))) {
172 cp = seq_unseen; /* use default, if not set */
178 ap = brkstring(dp, " ", "\n");
179 for (i = 0; ap && *ap; i++, ap++) {
180 seqnum[i] = seq_getnum(mp, *ap);
188 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
189 if (!is_selected(mp, msgnum)) {
193 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
194 admonish(cp, "unable to open message");
199 ** Check if message is in any sequence given
200 ** by Unseen-Sequence profile entry.
203 for (i = 0; i < num_unseen_seq; i++) {
204 if (in_sequence(mp, seqnum[i], msgnum)) {
210 switch (state = scan(in, msgnum, SCN_FOLD, fmtstr,
211 width, msgnum==mp->curmsg, unseen)) {
217 adios(EX_SOFTWARE, NULL, "scan() botch(%d)", state);
220 advise(NULL, "message %d: empty", msgnum);