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");
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 /* read folder and create message structure */
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]))
162 seq_setprev(mp); /* set the Previous-Sequence */
164 context_replace(curfolder, folder); /* update current folder */
165 seq_save(mp); /* synchronize message sequences */
166 context_save(); /* save the context file */
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)) {
190 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
191 admonish(cp, "unable to open message");
196 ** Check if message is in any sequence given
197 ** by Unseen-Sequence profile entry.
200 for (i = 0; i < num_unseen_seq; i++) {
201 if (in_sequence(mp, seqnum[i], msgnum)) {
207 switch (state = scan(in, msgnum, SCN_FOLD, fmtstr,
208 width, msgnum==mp->curmsg, unseen)) {
214 adios(EX_SOFTWARE, NULL, "scan() botch(%d)", state);
217 advise(NULL, "message %d: empty", msgnum);
224 folder_free(mp); /* free folder/message structure */