]>
diplodocus.org Git - nmh/blob - uip/scan.c
1 /* scan.c -- display a one-line "scan" listing of folder or messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/seq_setprev.h"
10 #include "sbr/seq_save.h"
11 #include "sbr/smatch.h"
12 #include "sbr/m_convert.h"
13 #include "sbr/getfolder.h"
14 #include "sbr/folder_read.h"
15 #include "sbr/folder_free.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/brkstring.h"
20 #include "sbr/ambigsw.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/seq_getnum.h"
25 #include "sbr/error.h"
26 #include "h/fmt_scan.h"
27 #include "h/scansbr.h"
32 #include "sbr/m_maildir.h"
33 #include "sbr/terminal.h"
35 #define SCAN_SWITCHES \
36 X("clear", 0, CLRSW) \
37 X("noclear", 0, NCLRSW) \
38 X("form formatfile", 0, FORMSW) \
39 X("format string", 5, FMTSW) \
40 X("header", 0, HEADSW) \
41 X("noheader", 0, NHEADSW) \
42 X("width columns", 0, WIDTHSW) \
43 X("reverse", 0, REVSW) \
44 X("noreverse", 0, NREVSW) \
45 X("file file", 4, FILESW) \
46 X("version", 0, VERSIONSW) \
47 X("help", 0, HELPSW) \
49 #define X(sw, minchars, id) id,
50 DEFINE_SWITCH_ENUM(SCAN
);
53 #define X(sw, minchars, id) { sw, minchars, id },
54 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
59 main (int argc
, char **argv
)
61 bool clearflag
= false;
67 ivector_t seqnum
= ivector_create (0);
69 int num_unseen_seq
= 0;
70 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
71 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
72 char **argp
, *nfs
, **arguments
;
73 struct msgs_array msgs
= { 0, 0, NULL
};
77 if (nmh_init(argv
[0], true, true)) { return 1; }
80 arguments
= getarguments (invo_name
, argc
, argv
, 1);
86 while ((cp
= *argp
++)) {
88 switch (smatch (++cp
, switches
)) {
90 ambigsw (cp
, switches
);
93 die("-%s unknown", cp
);
96 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
98 print_help (buf
, switches
, 1);
101 print_version(invo_name
);
112 if (!(form
= *argp
++) || *form
== '-')
113 die("missing argument to %s", argp
[-2]);
117 if (!(format
= *argp
++) || *format
== '-')
118 die("missing argument to %s", argp
[-2]);
130 if (!(cp
= *argp
++) || *cp
== '-')
131 die("missing argument to %s", argp
[-2]);
142 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
143 die("missing argument to %s", argp
[-2]);
144 if (strcmp (file
= cp
, "-"))
145 file
= path (cp
, TFILE
);
149 if (*cp
== '+' || *cp
== '@') {
151 die("only one folder at a time!");
152 folder
= pluspath (cp
);
154 app_msgarg(&msgs
, cp
);
157 if (!context_find ("path"))
158 free (path ("./", TFOLDER
));
161 * Get new format string. Must be before chdir().
163 nfs
= new_fs (form
, format
, FORMAT
);
166 * We are scanning a maildrop file
170 die("\"msgs\" not allowed with -file");
172 die("\"+folder\" not allowed with -file");
174 /* check if "file" is really stdin */
175 if (strcmp (file
, "-") == 0) {
179 if ((in
= fopen (file
, "r")) == NULL
)
180 adios (file
, "unable to open");
184 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
187 scan_detect_mbox_style (in
);
188 for (msgnum
= 1; ; ++msgnum
) {
189 charstring_t scanl
= NULL
;
191 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
192 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
193 charstring_free (scanl
);
194 if (state
!= SCNMSG
&& state
!= SCNENC
)
203 * We are scanning a folder
207 app_msgarg(&msgs
, "all");
209 folder
= getfolder (1);
210 maildir
= m_maildir (folder
);
212 if (chdir (maildir
) == NOTOK
)
213 adios (maildir
, "unable to change directory to");
215 /* read folder and create message structure */
216 if (!(mp
= folder_read (folder
, 1)))
217 die("unable to read folder %s", folder
);
219 /* check for empty folder */
221 die("no messages in %s", folder
);
223 /* parse all the message ranges/sequences and set SELECTED */
224 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
225 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
227 seq_setprev (mp
); /* set the Previous-Sequence */
229 context_replace (pfolder
, folder
); /* update current folder */
230 seq_save (mp
); /* synchronize message sequences */
231 context_save (); /* save the context file */
234 * Get the sequence number for each sequence
235 * specified by Unseen-Sequence
237 if ((cp
= context_find (usequence
)) && *cp
) {
241 ap
= brkstring (dp
, " ", "\n");
242 for (i
= 0; ap
&& *ap
; i
++, ap
++)
243 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
249 ontty
= isatty (fileno (stdout
));
251 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
252 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
253 msgnum
+= (revflag
? -1 : 1)) {
254 if (is_selected(mp
, msgnum
)) {
255 charstring_t scanl
= NULL
;
257 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
258 admonish (cp
, "unable to open message");
263 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
267 * Check if message is in any sequence given
268 * by Unseen-Sequence profile entry.
271 for (i
= 0; i
< num_unseen_seq
; i
++) {
272 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
278 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
279 msgnum
== mp
->curmsg
, unseen
,
280 folder
, 0L, 1, &scanl
)) {
287 die("scan() botch (%d)", state
);
290 inform("message %d: empty", msgnum
);
293 charstring_free (scanl
);
302 ivector_free (seqnum
);
303 folder_free (mp
); /* free folder/message structure */