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>
12 #include <zotnet/mts/mts.h>
16 * We allocate space for message names (msgs array)
17 * this number of elements at a time.
22 static struct swit switches[] = {
28 { "form formatfile", 0 },
30 { "format string", 5 },
36 { "width columns", 0 },
53 * global for sbr/formatsbr.c - yech!
56 extern struct msgs *fmt_current_folder;
62 void clear_screen(void); /* from termsbr.c */
66 main (int argc, char **argv)
68 int clearflag = 0, hdrflag = 0, ontty;
69 int width = 0, revflag = 0;
70 int i, state, msgnum, nummsgs, maxmsgs;
71 int seqnum[NUMATTRS], unseen, num_unseen_seq = 0;
72 char *cp, *maildir, *file = NULL, *folder = NULL;
73 char *form = NULL, *format = NULL, buf[BUFSIZ];
74 char **argp, *nfs, **arguments, **msgs;
79 setlocale(LC_ALL, "");
81 invo_name = r1bindex (argv[0], '/');
83 /* read user profile/context */
87 arguments = getarguments (invo_name, argc, argv, 1);
91 * Allocate the initial space to record message
92 * names, ranges, and sequences.
96 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
97 adios (NULL, "unable to allocate storage");
102 while ((cp = *argp++)) {
104 switch (smatch (++cp, switches)) {
106 ambigsw (cp, switches);
109 adios (NULL, "-%s unknown", cp);
112 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
114 print_help (buf, switches, 1);
117 print_version(invo_name);
128 if (!(form = *argp++) || *form == '-')
129 adios (NULL, "missing argument to %s", argp[-2]);
133 if (!(format = *argp++) || *format == '-')
134 adios (NULL, "missing argument to %s", argp[-2]);
146 if (!(cp = *argp++) || *cp == '-')
147 adios (NULL, "missing argument to %s", argp[-2]);
158 if (!(cp = *argp++) || (cp[0] == '-' && cp[1]))
159 adios (NULL, "missing argument to %s", argp[-2]);
160 if (strcmp (file = cp, "-"))
161 file = path (cp, TFILE);
165 if (*cp == '+' || *cp == '@') {
167 adios (NULL, "only one folder at a time!");
169 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
172 * Check if we need to allocate more space
173 * for message names/ranges/sequences.
175 if (nummsgs >= maxmsgs) {
177 if (!(msgs = (char **) realloc (msgs,
178 (size_t) (maxmsgs * sizeof(*msgs)))))
179 adios (NULL, "unable to reallocate msgs storage");
181 msgs[nummsgs++] = cp;
185 if (!context_find ("path"))
186 free (path ("./", TFOLDER));
189 * Get new format string. Must be before chdir().
191 nfs = new_fs (form, format, FORMAT);
194 * We are scanning a maildrop file
198 adios (NULL, "\"msgs\" not allowed with -file");
200 adios (NULL, "\"+folder\" not allowed with -file");
202 /* check if "file" is really stdin */
203 if (strcmp (file, "-") == 0) {
207 if ((in = fopen (file, "r")) == NULL)
208 adios (file, "unable to open");
213 printf ("FOLDER %s\t%s\n", file, dtimenow (1));
218 for (msgnum = 1; ; ++msgnum) {
219 state = scan (in, msgnum, -1, nfs, width, 0, 0,
220 hdrflag ? file : NULL, 0L, 1);
221 if (state != SCNMSG && state != SCNENC)
229 * We are scanning a folder
233 msgs[nummsgs++] = "all";
235 folder = getfolder (1);
236 maildir = m_maildir (folder);
238 if (chdir (maildir) == NOTOK)
239 adios (maildir, "unable to change directory to");
241 /* read folder and create message structure */
242 if (!(mp = folder_read (folder)))
243 adios (NULL, "unable to read folder %s", folder);
245 /* check for empty folder */
247 adios (NULL, "no messages in %s", folder);
249 /* parse all the message ranges/sequences and set SELECTED */
250 for (msgnum = 0; msgnum < nummsgs; msgnum++)
251 if (!m_convert (mp, msgs[msgnum]))
253 seq_setprev (mp); /* set the Previous-Sequence */
255 context_replace (pfolder, folder); /* update current folder */
256 seq_save (mp); /* synchronize message sequences */
257 context_save (); /* save the context file */
260 * Get the sequence number for each sequence
261 * specified by Unseen-Sequence
263 if ((cp = context_find (usequence)) && *cp) {
267 ap = brkstring (dp, " ", "\n");
268 for (i = 0; ap && *ap; i++, ap++)
269 seqnum[i] = seq_getnum (mp, *ap);
276 ontty = isatty (fileno (stdout));
280 fmt_current_folder = mp;
283 for (msgnum = revflag ? mp->hghsel : mp->lowsel;
284 (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
285 msgnum += (revflag ? -1 : 1)) {
286 if (is_selected(mp, msgnum)) {
287 if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
291 admonish (cp, "unable to open message");
294 printf ("%*d unreadable\n", DMAXFOLDER, msgnum);
301 printf ("FOLDER %s\t%s\n", folder, dtimenow(1));
306 * Check if message is in any sequence given
307 * by Unseen-Sequence profile entry.
310 for (i = 0; i < num_unseen_seq; i++) {
311 if (in_sequence(mp, seqnum[i], msgnum)) {
317 switch (state = scan (in, msgnum, 0, nfs, width,
318 msgnum == mp->curmsg, unseen,
319 hdrflag ? folder : NULL, 0L, 1)) {
326 adios (NULL, "scan() botch (%d)", state);
330 printf ("%*d empty\n", DMAXFOLDER, msgnum);
332 advise (NULL, "message %d: empty", msgnum);
344 seq_save (mp); /* because formatsbr might have made changes */
347 folder_free (mp); /* free folder/message structure */