]>
diplodocus.org Git - nmh/blob - uip/scan.c
3 * scan.c -- display a one-line "scan" listing of folder or messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/fmt_scan.h>
12 #include <h/scansbr.h>
18 static struct swit switches
[] = {
24 { "form formatfile", 0 },
26 { "format string", 5 },
32 { "width columns", 0 },
48 * global for sbr/formatsbr.c - yech!
51 extern struct msgs
*fmt_current_folder
;
57 void clear_screen(void); /* from termsbr.c */
61 main (int argc
, char **argv
)
63 int clearflag
= 0, hdrflag
= 0, ontty
;
64 int width
= 0, revflag
= 0;
66 int seqnum
[NUMATTRS
], unseen
, num_unseen_seq
= 0;
67 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
68 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
69 char **argp
, *nfs
, **arguments
;
70 struct msgs_array msgs
= { 0, 0, NULL
};
75 setlocale(LC_ALL
, "");
77 invo_name
= r1bindex (argv
[0], '/');
79 /* read user profile/context */
83 arguments
= getarguments (invo_name
, argc
, argv
, 1);
89 while ((cp
= *argp
++)) {
91 switch (smatch (++cp
, switches
)) {
93 ambigsw (cp
, switches
);
96 adios (NULL
, "-%s unknown", cp
);
99 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
101 print_help (buf
, switches
, 1);
104 print_version(invo_name
);
115 if (!(form
= *argp
++) || *form
== '-')
116 adios (NULL
, "missing argument to %s", argp
[-2]);
120 if (!(format
= *argp
++) || *format
== '-')
121 adios (NULL
, "missing argument to %s", argp
[-2]);
133 if (!(cp
= *argp
++) || *cp
== '-')
134 adios (NULL
, "missing argument to %s", argp
[-2]);
145 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
146 adios (NULL
, "missing argument to %s", argp
[-2]);
147 if (strcmp (file
= cp
, "-"))
148 file
= path (cp
, TFILE
);
152 if (*cp
== '+' || *cp
== '@') {
154 adios (NULL
, "only one folder at a time!");
156 folder
= pluspath (cp
);
158 app_msgarg(&msgs
, cp
);
161 if (!context_find ("path"))
162 free (path ("./", TFOLDER
));
165 * Get new format string. Must be before chdir().
167 nfs
= new_fs (form
, format
, FORMAT
);
170 * We are scanning a maildrop file
174 adios (NULL
, "\"msgs\" not allowed with -file");
176 adios (NULL
, "\"+folder\" not allowed with -file");
178 /* check if "file" is really stdin */
179 if (strcmp (file
, "-") == 0) {
183 if ((in
= fopen (file
, "r")) == NULL
)
184 adios (file
, "unable to open");
189 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
194 for (msgnum
= 1; ; ++msgnum
) {
195 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
196 hdrflag
? file
: NULL
, 0L, 1);
197 if (state
!= SCNMSG
&& state
!= SCNENC
)
205 * We are scanning a folder
209 app_msgarg(&msgs
, "all");
211 folder
= getfolder (1);
212 maildir
= m_maildir (folder
);
214 if (chdir (maildir
) == NOTOK
)
215 adios (maildir
, "unable to change directory to");
217 /* read folder and create message structure */
218 if (!(mp
= folder_read (folder
)))
219 adios (NULL
, "unable to read folder %s", folder
);
221 /* check for empty folder */
223 adios (NULL
, "no messages in %s", folder
);
225 /* parse all the message ranges/sequences and set SELECTED */
226 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
227 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
229 seq_setprev (mp
); /* set the Previous-Sequence */
231 context_replace (pfolder
, folder
); /* update current folder */
232 seq_save (mp
); /* synchronize message sequences */
233 context_save (); /* save the context file */
236 * Get the sequence number for each sequence
237 * specified by Unseen-Sequence
239 if ((cp
= context_find (usequence
)) && *cp
) {
243 ap
= brkstring (dp
, " ", "\n");
244 for (i
= 0; ap
&& *ap
; i
++, ap
++)
245 seqnum
[i
] = seq_getnum (mp
, *ap
);
252 ontty
= isatty (fileno (stdout
));
256 fmt_current_folder
= mp
;
259 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
260 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
261 msgnum
+= (revflag
? -1 : 1)) {
262 if (is_selected(mp
, msgnum
)) {
263 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
267 admonish (cp
, "unable to open message");
270 printf ("%*d unreadable\n", DMAXFOLDER
, msgnum
);
277 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
282 * Check if message is in any sequence given
283 * by Unseen-Sequence profile entry.
286 for (i
= 0; i
< num_unseen_seq
; i
++) {
287 if (in_sequence(mp
, seqnum
[i
], msgnum
)) {
293 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
294 msgnum
== mp
->curmsg
, unseen
,
302 adios (NULL
, "scan() botch (%d)", state
);
306 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
308 advise (NULL
, "message %d: empty", msgnum
);
320 seq_save (mp
); /* because formatsbr might have made changes */
323 folder_free (mp
); /* free folder/message structure */