]>
diplodocus.org Git - nmh/blob - uip/scan.c
a90b72ed200e13577e366ebbbaa340bd9d105170
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_save.h"
10 #include "sbr/smatch.h"
11 #include "sbr/m_convert.h"
12 #include "sbr/getfolder.h"
13 #include "sbr/folder_read.h"
14 #include "sbr/folder_free.h"
15 #include "sbr/context_save.h"
16 #include "sbr/context_replace.h"
17 #include "sbr/context_find.h"
18 #include "sbr/brkstring.h"
19 #include "sbr/ambigsw.h"
21 #include "sbr/print_version.h"
22 #include "sbr/print_help.h"
23 #include "sbr/seq_getnum.h"
24 #include "sbr/error.h"
25 #include "h/fmt_scan.h"
26 #include "h/scansbr.h"
31 #include "sbr/m_maildir.h"
32 #include "sbr/terminal.h"
34 #define SCAN_SWITCHES \
35 X("clear", 0, CLRSW) \
36 X("noclear", 0, NCLRSW) \
37 X("form formatfile", 0, FORMSW) \
38 X("format string", 5, FMTSW) \
39 X("header", 0, HEADSW) \
40 X("noheader", 0, NHEADSW) \
41 X("width columns", 0, WIDTHSW) \
42 X("reverse", 0, REVSW) \
43 X("noreverse", 0, NREVSW) \
44 X("file file", 4, FILESW) \
45 X("version", 0, VERSIONSW) \
46 X("help", 0, HELPSW) \
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(SCAN
);
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
58 main (int argc
, char **argv
)
60 bool clearflag
= false;
66 ivector_t seqnum
= ivector_create (0);
68 int num_unseen_seq
= 0;
69 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
70 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
71 char **argp
, *nfs
, **arguments
;
72 struct msgs_array msgs
= { 0, 0, NULL
};
76 if (nmh_init(argv
[0], true, true)) { return 1; }
79 arguments
= getarguments (invo_name
, argc
, argv
, 1);
85 while ((cp
= *argp
++)) {
87 switch (smatch (++cp
, switches
)) {
89 ambigsw (cp
, switches
);
92 die("-%s unknown", cp
);
95 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
97 print_help (buf
, switches
, 1);
100 print_version(invo_name
);
111 if (!(form
= *argp
++) || *form
== '-')
112 die("missing argument to %s", argp
[-2]);
116 if (!(format
= *argp
++) || *format
== '-')
117 die("missing argument to %s", argp
[-2]);
129 if (!(cp
= *argp
++) || *cp
== '-')
130 die("missing argument to %s", argp
[-2]);
141 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
142 die("missing argument to %s", argp
[-2]);
143 if (strcmp (file
= cp
, "-"))
144 file
= path (cp
, TFILE
);
148 if (*cp
== '+' || *cp
== '@') {
150 die("only one folder at a time!");
151 folder
= pluspath (cp
);
153 app_msgarg(&msgs
, cp
);
156 if (!context_find ("path"))
157 free (path ("./", TFOLDER
));
160 * Get new format string. Must be before chdir().
162 nfs
= new_fs (form
, format
, FORMAT
);
165 * We are scanning a maildrop file
169 die("\"msgs\" not allowed with -file");
171 die("\"+folder\" not allowed with -file");
173 /* check if "file" is really stdin */
174 if (strcmp (file
, "-") == 0) {
178 if ((in
= fopen (file
, "r")) == NULL
)
179 adios (file
, "unable to open");
183 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
186 scan_detect_mbox_style (in
);
187 for (msgnum
= 1; ; ++msgnum
) {
188 charstring_t scanl
= NULL
;
190 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
191 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
192 charstring_free (scanl
);
193 if (state
!= SCNMSG
&& state
!= SCNENC
)
202 * We are scanning a folder
206 app_msgarg(&msgs
, "all");
208 folder
= getfolder (1);
209 maildir
= m_maildir (folder
);
211 if (chdir (maildir
) == NOTOK
)
212 adios (maildir
, "unable to change directory to");
214 /* read folder and create message structure */
215 if (!(mp
= folder_read (folder
, 1)))
216 die("unable to read folder %s", folder
);
218 /* check for empty folder */
220 die("no messages in %s", folder
);
222 /* parse all the message ranges/sequences and set SELECTED */
223 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
224 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
226 seq_setprev (mp
); /* set the Previous-Sequence */
228 context_replace (pfolder
, folder
); /* update current folder */
229 seq_save (mp
); /* synchronize message sequences */
230 context_save (); /* save the context file */
233 * Get the sequence number for each sequence
234 * specified by Unseen-Sequence
236 if ((cp
= context_find (usequence
)) && *cp
) {
240 ap
= brkstring (dp
, " ", "\n");
241 for (i
= 0; ap
&& *ap
; i
++, ap
++)
242 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
248 ontty
= isatty (fileno (stdout
));
250 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
251 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
252 msgnum
+= (revflag
? -1 : 1)) {
253 if (is_selected(mp
, msgnum
)) {
254 charstring_t scanl
= NULL
;
256 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
257 admonish (cp
, "unable to open message");
262 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
266 * Check if message is in any sequence given
267 * by Unseen-Sequence profile entry.
270 for (i
= 0; i
< num_unseen_seq
; i
++) {
271 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
277 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
278 msgnum
== mp
->curmsg
, unseen
,
279 folder
, 0L, 1, &scanl
)) {
286 die("scan() botch (%d)", state
);
289 inform("message %d: empty", msgnum
);
292 charstring_free (scanl
);
301 ivector_free (seqnum
);
302 folder_free (mp
); /* free folder/message structure */