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");
130 for (msgnum = 1; ; ++msgnum) {
131 state = scan(in, msgnum, SCN_MBOX, fmtstr, width, 0, 0);
140 ** We are scanning a folder
144 app_msgarg(&msgs, seq_all);
146 folder = getcurfol();
147 maildir = toabsdir(folder);
149 if (chdir(maildir) == NOTOK)
150 adios(EX_OSERR, maildir, "unable to change directory to");
152 /* read folder and create message structure */
153 if (!(mp = folder_read(folder)))
154 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
156 /* check for empty folder */
158 adios(EX_DATAERR, NULL, "no messages in %s", folder);
160 /* parse all the message ranges/sequences and set SELECTED */
161 for (msgnum = 0; msgnum < msgs.size; msgnum++)
162 if (!m_convert(mp, msgs.msgs[msgnum]))
164 seq_setprev(mp); /* set the Previous-Sequence */
166 context_replace(curfolder, folder); /* update current folder */
167 seq_save(mp); /* synchronize message sequences */
168 context_save(); /* save the context file */
171 ** Get the sequence number for each `unseen' sequence
173 if (!(cp = context_find(usequence))) {
174 cp = seq_unseen; /* use default, if not set */
180 ap = brkstring(dp, " ", "\n");
181 for (i = 0; ap && *ap; i++, ap++) {
182 seqnum[i] = seq_getnum(mp, *ap);
190 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
191 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);
226 folder_free(mp); /* free folder/message structure */