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 },
33 char *version=VERSION;
36 main(int argc, char **argv)
40 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
41 char *cp, *maildir, *file = NULL, *folder = NULL;
42 char *form = NULL, buf[BUFSIZ];
43 char **argp, *fmtstr, **arguments;
44 struct msgs_array msgs = { 0, 0, NULL };
48 setlocale(LC_ALL, "");
49 invo_name = mhbasename(argv[0]);
51 /* read user profile/context */
54 arguments = getarguments(invo_name, argc, argv, 1);
60 while ((cp = *argp++)) {
62 switch (smatch(++cp, switches)) {
64 ambigsw(cp, switches);
67 adios(EX_USAGE, NULL, "-%s unknown", cp);
70 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
71 print_help(buf, switches, 1);
72 exit(argc == 2 ? EX_OK : EX_USAGE);
74 print_version(invo_name);
75 exit(argc == 2 ? EX_OK : EX_USAGE);
78 if (!(form = *argp++) || *form == '-')
79 adios(EX_USAGE, NULL, "missing argument to %s",
84 if (!(cp = *argp++) || *cp == '-')
85 adios(EX_USAGE, NULL, "missing argument to %s",
91 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
92 adios(EX_USAGE, NULL, "missing argument to %s",
94 if (strcmp(file = cp, "-")!=0)
95 file = mh_xstrdup(expanddir(cp));
99 if (*cp == '+' || *cp == '@') {
101 adios(EX_USAGE, NULL, "only one folder at a time!");
103 folder = mh_xstrdup(expandfol(cp));
105 app_msgarg(&msgs, cp);
108 /* Set format string. Must be before chdir(). */
109 fmtstr = new_fs(form, scanformat);
112 ** We are scanning a maildrop file
116 adios(EX_USAGE, NULL, "\"msgs\" not allowed with -file");
118 adios(EX_USAGE, 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(EX_IOERR, file, "unable to open");
128 for (msgnum = 1; ; ++msgnum) {
129 state = scan(in, msgnum, SCN_MBOX, fmtstr, width, 0, 0);
138 ** We are scanning a folder
142 app_msgarg(&msgs, seq_all);
144 folder = getcurfol();
145 maildir = toabsdir(folder);
147 if (chdir(maildir) == NOTOK)
148 adios(EX_OSERR, maildir, "unable to change directory to");
150 if (!(mp = folder_read(folder)))
151 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
153 /* check for empty folder */
155 adios(EX_DATAERR, 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]))
163 context_replace(curfolder, folder);
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)) {
192 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
193 admonish(cp, "unable to open message");
198 ** Check if message is in any sequence given
199 ** by Unseen-Sequence profile entry.
202 for (i = 0; i < num_unseen_seq; i++) {
203 if (in_sequence(mp, seqnum[i], msgnum)) {
209 switch (state = scan(in, msgnum, SCN_FOLD, fmtstr,
210 width, msgnum==mp->curmsg, unseen)) {
216 adios(EX_SOFTWARE, NULL, "scan() botch(%d)", state);
219 advise(NULL, "message %d: empty", msgnum);