]>
diplodocus.org Git - nmh/blob - uip/scan.c
a47b8685f46b4795499723f2f8f168320f2b6240
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/context_save.h"
10 #include "sbr/context_replace.h"
11 #include "sbr/context_find.h"
12 #include "sbr/brkstring.h"
13 #include "sbr/ambigsw.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/seq_getnum.h"
18 #include "sbr/error.h"
19 #include "h/fmt_scan.h"
20 #include "h/scansbr.h"
25 #include "sbr/m_maildir.h"
26 #include "sbr/terminal.h"
28 #define SCAN_SWITCHES \
29 X("clear", 0, CLRSW) \
30 X("noclear", 0, NCLRSW) \
31 X("form formatfile", 0, FORMSW) \
32 X("format string", 5, FMTSW) \
33 X("header", 0, HEADSW) \
34 X("noheader", 0, NHEADSW) \
35 X("width columns", 0, WIDTHSW) \
36 X("reverse", 0, REVSW) \
37 X("noreverse", 0, NREVSW) \
38 X("file file", 4, FILESW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(SCAN
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
52 main (int argc
, char **argv
)
54 bool clearflag
= false;
60 ivector_t seqnum
= ivector_create (0);
62 int num_unseen_seq
= 0;
63 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
64 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
65 char **argp
, *nfs
, **arguments
;
66 struct msgs_array msgs
= { 0, 0, NULL
};
70 if (nmh_init(argv
[0], true, true)) { return 1; }
73 arguments
= getarguments (invo_name
, argc
, argv
, 1);
79 while ((cp
= *argp
++)) {
81 switch (smatch (++cp
, switches
)) {
83 ambigsw (cp
, switches
);
86 die("-%s unknown", cp
);
89 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
91 print_help (buf
, switches
, 1);
94 print_version(invo_name
);
105 if (!(form
= *argp
++) || *form
== '-')
106 die("missing argument to %s", argp
[-2]);
110 if (!(format
= *argp
++) || *format
== '-')
111 die("missing argument to %s", argp
[-2]);
123 if (!(cp
= *argp
++) || *cp
== '-')
124 die("missing argument to %s", argp
[-2]);
135 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
136 die("missing argument to %s", argp
[-2]);
137 if (strcmp (file
= cp
, "-"))
138 file
= path (cp
, TFILE
);
142 if (*cp
== '+' || *cp
== '@') {
144 die("only one folder at a time!");
145 folder
= pluspath (cp
);
147 app_msgarg(&msgs
, cp
);
150 if (!context_find ("path"))
151 free (path ("./", TFOLDER
));
154 * Get new format string. Must be before chdir().
156 nfs
= new_fs (form
, format
, FORMAT
);
159 * We are scanning a maildrop file
163 die("\"msgs\" not allowed with -file");
165 die("\"+folder\" not allowed with -file");
167 /* check if "file" is really stdin */
168 if (strcmp (file
, "-") == 0) {
172 if ((in
= fopen (file
, "r")) == NULL
)
173 adios (file
, "unable to open");
177 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
180 scan_detect_mbox_style (in
);
181 for (msgnum
= 1; ; ++msgnum
) {
182 charstring_t scanl
= NULL
;
184 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
185 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
186 charstring_free (scanl
);
187 if (state
!= SCNMSG
&& state
!= SCNENC
)
196 * We are scanning a folder
200 app_msgarg(&msgs
, "all");
202 folder
= getfolder (1);
203 maildir
= m_maildir (folder
);
205 if (chdir (maildir
) == NOTOK
)
206 adios (maildir
, "unable to change directory to");
208 /* read folder and create message structure */
209 if (!(mp
= folder_read (folder
, 1)))
210 die("unable to read folder %s", folder
);
212 /* check for empty folder */
214 die("no messages in %s", folder
);
216 /* parse all the message ranges/sequences and set SELECTED */
217 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
218 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
220 seq_setprev (mp
); /* set the Previous-Sequence */
222 context_replace (pfolder
, folder
); /* update current folder */
223 seq_save (mp
); /* synchronize message sequences */
224 context_save (); /* save the context file */
227 * Get the sequence number for each sequence
228 * specified by Unseen-Sequence
230 if ((cp
= context_find (usequence
)) && *cp
) {
234 ap
= brkstring (dp
, " ", "\n");
235 for (i
= 0; ap
&& *ap
; i
++, ap
++)
236 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
242 ontty
= isatty (fileno (stdout
));
244 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
245 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
246 msgnum
+= (revflag
? -1 : 1)) {
247 if (is_selected(mp
, msgnum
)) {
248 charstring_t scanl
= NULL
;
250 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
251 admonish (cp
, "unable to open message");
256 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
260 * Check if message is in any sequence given
261 * by Unseen-Sequence profile entry.
264 for (i
= 0; i
< num_unseen_seq
; i
++) {
265 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
271 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
272 msgnum
== mp
->curmsg
, unseen
,
273 folder
, 0L, 1, &scanl
)) {
280 die("scan() botch (%d)", state
);
283 inform("message %d: empty", msgnum
);
286 charstring_free (scanl
);
295 ivector_free (seqnum
);
296 folder_free (mp
); /* free folder/message structure */