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 = mh_xstrdup(expanddir(cp));
98 if (*cp == '+' || *cp == '@') {
100 adios(EX_USAGE, NULL, "only one folder at a time!");
102 folder = mh_xstrdup(expandfol(cp));
104 app_msgarg(&msgs, cp);
107 /* Set format string. Must be before chdir(). */
108 fmtstr = new_fs(form, scanformat);
111 ** We are scanning a maildrop file
115 adios(EX_USAGE, NULL, "\"msgs\" not allowed with -file");
117 adios(EX_USAGE, NULL, "\"+folder\" not allowed with -file");
119 /* check if "file" is really stdin */
120 if (strcmp(file, "-") == 0) {
123 } else if (!(in = fopen(file, "r"))) {
124 adios(EX_IOERR, 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(EX_OSERR, maildir, "unable to change directory to");
149 if (!(mp = folder_read(folder)))
150 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
152 /* check for empty folder */
154 adios(EX_DATAERR, NULL, "no messages in %s", folder);
156 /* parse all the message ranges/sequences and set SELECTED */
157 for (msgnum = 0; msgnum < msgs.size; msgnum++)
158 if (!m_convert(mp, msgs.msgs[msgnum]))
162 context_replace(curfolder, folder);
167 ** Get the sequence number for each `unseen' sequence
169 if (!(cp = context_find(usequence))) {
170 cp = seq_unseen; /* use default, if not set */
176 ap = brkstring(dp, " ", "\n");
177 for (i = 0; ap && *ap; i++, ap++) {
178 seqnum[i] = seq_getnum(mp, *ap);
186 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
187 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(EX_SOFTWARE, NULL, "scan() botch(%d)", state);
218 advise(NULL, "message %d: empty", msgnum);