]>
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.
10 #include "sbr/m_name.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_save.h"
14 #include "sbr/smatch.h"
15 #include "sbr/m_convert.h"
16 #include "sbr/getfolder.h"
17 #include "sbr/folder_read.h"
18 #include "sbr/folder_free.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_replace.h"
21 #include "sbr/context_find.h"
22 #include "sbr/brkstring.h"
23 #include "sbr/ambigsw.h"
25 #include "sbr/print_version.h"
26 #include "sbr/print_help.h"
27 #include "sbr/seq_getnum.h"
28 #include "sbr/error.h"
29 #include "h/fmt_scan.h"
34 #include "sbr/m_maildir.h"
35 #include "sbr/terminal.h"
37 #define SCAN_SWITCHES \
38 X("clear", 0, CLRSW) \
39 X("noclear", 0, NCLRSW) \
40 X("form formatfile", 0, FORMSW) \
41 X("format string", 5, FMTSW) \
42 X("header", 0, HEADSW) \
43 X("noheader", 0, NHEADSW) \
44 X("width columns", 0, WIDTHSW) \
45 X("reverse", 0, REVSW) \
46 X("noreverse", 0, NREVSW) \
47 X("file file", 4, FILESW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(SCAN
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
61 main (int argc
, char **argv
)
63 bool clearflag
= false;
69 ivector_t seqnum
= ivector_create (0);
71 int 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
;
75 struct msgs_array msgs
= { 0, 0, NULL
};
79 if (nmh_init(argv
[0], true, true)) { return 1; }
82 arguments
= getarguments (invo_name
, argc
, argv
, 1);
88 while ((cp
= *argp
++)) {
90 switch (smatch (++cp
, switches
)) {
92 ambigsw (cp
, switches
);
95 die("-%s unknown", cp
);
98 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
100 print_help (buf
, switches
, 1);
103 print_version(invo_name
);
114 if (!(form
= *argp
++) || *form
== '-')
115 die("missing argument to %s", argp
[-2]);
119 if (!(format
= *argp
++) || *format
== '-')
120 die("missing argument to %s", argp
[-2]);
132 if (!(cp
= *argp
++) || *cp
== '-')
133 die("missing argument to %s", argp
[-2]);
144 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
145 die("missing argument to %s", argp
[-2]);
146 if (strcmp (file
= cp
, "-"))
147 file
= path (cp
, TFILE
);
151 if (*cp
== '+' || *cp
== '@') {
153 die("only one folder at a time!");
154 folder
= pluspath (cp
);
156 app_msgarg(&msgs
, cp
);
159 if (!context_find ("path"))
160 free (path ("./", TFOLDER
));
163 * Get new format string. Must be before chdir().
165 nfs
= new_fs (form
, format
, FORMAT
);
168 * We are scanning a maildrop file
172 die("\"msgs\" not allowed with -file");
174 die("\"+folder\" not allowed with -file");
176 /* check if "file" is really stdin */
177 if (strcmp (file
, "-") == 0) {
181 if ((in
= fopen (file
, "r")) == NULL
)
182 adios (file
, "unable to open");
186 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
189 scan_detect_mbox_style (in
);
190 for (msgnum
= 1; ; ++msgnum
) {
191 charstring_t scanl
= NULL
;
193 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
194 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
195 charstring_free (scanl
);
196 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
, 1)))
219 die("unable to read folder %s", folder
);
221 /* check for empty folder */
223 die("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 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
251 ontty
= isatty (fileno (stdout
));
253 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
254 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
255 msgnum
+= (revflag
? -1 : 1)) {
256 if (is_selected(mp
, msgnum
)) {
257 charstring_t scanl
= NULL
;
259 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
260 admonish (cp
, "unable to open message");
265 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
269 * Check if message is in any sequence given
270 * by Unseen-Sequence profile entry.
273 for (i
= 0; i
< num_unseen_seq
; i
++) {
274 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
280 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
281 msgnum
== mp
->curmsg
, unseen
,
282 folder
, 0L, 1, &scanl
)) {
289 die("scan() botch (%d)", state
);
292 inform("message %d: empty", msgnum
);
295 charstring_free (scanl
);
304 ivector_free (seqnum
);
305 folder_free (mp
); /* free folder/message structure */