3 * scan.c -- display a one-line "scan" listing of folder or messages
9 #include <h/fmt_scan.h>
10 #include <h/scansbr.h>
11 #include <zotnet/tws/tws.h>
15 * We allocate space for message names (msgs array)
16 * this number of elements at a time.
21 static struct swit switches[] = {
27 { "form formatfile", 0 },
29 { "format string", 5 },
35 { "width columns", 0 },
52 * global for sbr/formatsbr.c - yech!
55 extern struct msgs *fmt_current_folder;
61 void clear_screen(void); /* from termsbr.c */
65 main (int argc, char **argv)
67 int clearflag = 0, hdrflag = 0, ontty;
68 int width = 0, revflag = 0;
69 int i, state, msgnum, nummsgs, maxmsgs;
70 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
71 char *cp, *maildir, *file = NULL, *folder = NULL;
72 char *form = NULL, *format = NULL, buf[BUFSIZ];
73 char **argp, *nfs, **arguments, **msgs;
78 setlocale(LC_ALL, "");
80 invo_name = r1bindex (argv[0], '/');
82 /* read user profile/context */
86 arguments = getarguments (invo_name, argc, argv, 1);
90 * Allocate the initial space to record message
91 * names, ranges, and sequences.
95 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
96 adios (NULL, "unable to allocate storage");
101 while ((cp = *argp++)) {
103 switch (smatch (++cp, switches)) {
105 ambigsw (cp, switches);
108 adios (NULL, "-%s unknown", cp);
111 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
113 print_help (buf, switches, 1);
116 print_version(invo_name);
127 if (!(form = *argp++) || *form == '-')
128 adios (NULL, "missing argument to %s", argp[-2]);
132 if (!(format = *argp++) || *format == '-')
133 adios (NULL, "missing argument to %s", argp[-2]);
145 if (!(cp = *argp++) || *cp == '-')
146 adios (NULL, "missing argument to %s", argp[-2]);
157 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
158 adios (NULL, "missing argument to %s", argp[-2]);
159 if (strcmp (file = cp, "-"))
160 file = path (cp, TFILE);
164 if (*cp == '+' || *cp == '@') {
166 adios (NULL, "only one folder at a time!");
168 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
171 * Check if we need to allocate more space
172 * for message names/ranges/sequences.
174 if (nummsgs >= maxmsgs) {
176 if (!(msgs = (char **) realloc (msgs,
177 (size_t) (maxmsgs * sizeof(*msgs)))))
178 adios (NULL, "unable to reallocate msgs storage");
180 msgs[nummsgs++] = cp;
184 if (!context_find ("path"))
185 free (path ("./", TFOLDER));
188 * Get new format string. Must be before chdir().
190 nfs = new_fs (form, format, FORMAT);
193 * We are scanning a maildrop file
197 adios (NULL, "\"msgs\" not allowed with -file");
199 adios (NULL, "\"+folder\" not allowed with -file");
201 /* check if "file" is really stdin */
202 if (strcmp (file, "-") == 0) {
206 if ((in = fopen (file, "r")) == NULL)
207 adios (file, "unable to open");
212 printf ("FOLDER %s\t%s\n", file, dtimenow (1));
217 for (msgnum = 1; ; ++msgnum) {
218 state = scan (in, msgnum, -1, nfs, width, 0, 0,
219 hdrflag ? file : NULL, 0L, 1);
220 if (state != SCNMSG && state != SCNENC)
228 * We are scanning a folder
232 msgs[nummsgs++] = "all";
234 folder = getfolder (1);
235 maildir = m_maildir (folder);
237 if (chdir (maildir) == NOTOK)
238 adios (maildir, "unable to change directory to");
240 /* read folder and create message structure */
241 if (!(mp = folder_read (folder)))
242 adios (NULL, "unable to read folder %s", folder);
244 /* check for empty folder */
246 adios (NULL, "no messages in %s", folder);
248 /* parse all the message ranges/sequences and set SELECTED */
249 for (msgnum = 0; msgnum < nummsgs; msgnum++)
250 if (!m_convert (mp, msgs[msgnum]))
252 seq_setprev (mp); /* set the Previous-Sequence */
254 context_replace (pfolder, folder); /* update current folder */
255 seq_save (mp); /* synchronize message sequences */
256 context_save (); /* save the context file */
259 * Get the sequence number for each sequence
260 * specified by Unseen-Sequence
262 if ((cp = context_find (usequence)) && *cp) {
266 ap = brkstring (dp, " ", "\n");
267 for (i = 0; ap && *ap; i++, ap++)
268 seqnum[i] = seq_getnum (mp, *ap);
275 ontty = isatty (fileno (stdout));
279 fmt_current_folder = mp;
282 for (msgnum = revflag ? mp->hghsel : mp->lowsel;
283 (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
284 msgnum += (revflag ? -1 : 1)) {
285 if (is_selected(mp, msgnum)) {
286 if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
290 admonish (cp, "unable to open message");
293 printf ("%*d unreadable\n", DMAXFOLDER, msgnum);
300 printf ("FOLDER %s\t%s\n", folder, dtimenow(1));
305 * Check if message is in any sequence given
306 * by Unseen-Sequence profile entry.
309 for (i = 0; i < num_unseen_seq; i++) {
310 if (in_sequence(mp, seqnum[i], msgnum)) {
316 switch (state = scan (in, msgnum, 0, nfs, width,
317 msgnum == mp->curmsg, unseen,
318 hdrflag ? folder : NULL, 0L, 1)) {
325 adios (NULL, "scan() botch (%d)", state);
329 printf ("%*d empty\n", DMAXFOLDER, msgnum);
331 advise (NULL, "message %d: empty", msgnum);
343 seq_save (mp); /* because formatsbr might have made changes */
346 folder_free (mp); /* free folder/message structure */