]>
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.
11 #include "sbr/m_name.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/seq_setprev.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/smatch.h"
16 #include "sbr/m_convert.h"
17 #include "sbr/getfolder.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/folder_free.h"
20 #include "sbr/context_save.h"
21 #include "sbr/context_replace.h"
22 #include "sbr/context_find.h"
23 #include "sbr/brkstring.h"
24 #include "sbr/ambigsw.h"
26 #include "sbr/print_version.h"
27 #include "sbr/print_help.h"
28 #include "sbr/seq_getnum.h"
29 #include "sbr/error.h"
30 #include "h/fmt_scan.h"
35 #include "sbr/m_maildir.h"
36 #include "sbr/terminal.h"
38 #define SCAN_SWITCHES \
39 X("clear", 0, CLRSW) \
40 X("noclear", 0, NCLRSW) \
41 X("form formatfile", 0, FORMSW) \
42 X("format string", 5, FMTSW) \
43 X("header", 0, HEADSW) \
44 X("noheader", 0, NHEADSW) \
45 X("width columns", 0, WIDTHSW) \
46 X("reverse", 0, REVSW) \
47 X("noreverse", 0, NREVSW) \
48 X("file file", 4, FILESW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(SCAN
);
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
62 main (int argc
, char **argv
)
64 bool clearflag
= false;
70 ivector_t seqnum
= ivector_create (0);
72 int num_unseen_seq
= 0;
73 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
74 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
75 char **argp
, *nfs
, **arguments
;
76 struct msgs_array msgs
= { 0, 0, NULL
};
80 if (nmh_init(argv
[0], true, true)) { return 1; }
83 arguments
= getarguments (invo_name
, argc
, argv
, 1);
89 while ((cp
= *argp
++)) {
91 switch (smatch (++cp
, switches
)) {
93 ambigsw (cp
, switches
);
96 die("-%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 die("missing argument to %s", argp
[-2]);
120 if (!(format
= *argp
++) || *format
== '-')
121 die("missing argument to %s", argp
[-2]);
133 if (!(cp
= *argp
++) || *cp
== '-')
134 die("missing argument to %s", argp
[-2]);
145 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
146 die("missing argument to %s", argp
[-2]);
147 if (strcmp (file
= cp
, "-"))
148 file
= path (cp
, TFILE
);
152 if (*cp
== '+' || *cp
== '@') {
154 die("only one folder at a time!");
155 folder
= pluspath (cp
);
157 app_msgarg(&msgs
, cp
);
160 if (!context_find ("path"))
161 free (path ("./", TFOLDER
));
164 * Get new format string. Must be before chdir().
166 nfs
= new_fs (form
, format
, FORMAT
);
169 * We are scanning a maildrop file
173 die("\"msgs\" not allowed with -file");
175 die("\"+folder\" not allowed with -file");
177 /* check if "file" is really stdin */
178 if (strcmp (file
, "-") == 0) {
182 if ((in
= fopen (file
, "r")) == NULL
)
183 adios (file
, "unable to open");
187 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
190 scan_detect_mbox_style (in
);
191 for (msgnum
= 1; ; ++msgnum
) {
192 charstring_t scanl
= NULL
;
194 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
195 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
196 charstring_free (scanl
);
197 if (state
!= SCNMSG
&& state
!= SCNENC
)
206 * We are scanning a folder
210 app_msgarg(&msgs
, "all");
212 folder
= getfolder (1);
213 maildir
= m_maildir (folder
);
215 if (chdir (maildir
) == NOTOK
)
216 adios (maildir
, "unable to change directory to");
218 /* read folder and create message structure */
219 if (!(mp
= folder_read (folder
, 1)))
220 die("unable to read folder %s", folder
);
222 /* check for empty folder */
224 die("no messages in %s", folder
);
226 /* parse all the message ranges/sequences and set SELECTED */
227 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
228 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
230 seq_setprev (mp
); /* set the Previous-Sequence */
232 context_replace (pfolder
, folder
); /* update current folder */
233 seq_save (mp
); /* synchronize message sequences */
234 context_save (); /* save the context file */
237 * Get the sequence number for each sequence
238 * specified by Unseen-Sequence
240 if ((cp
= context_find (usequence
)) && *cp
) {
244 ap
= brkstring (dp
, " ", "\n");
245 for (i
= 0; ap
&& *ap
; i
++, ap
++)
246 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
252 ontty
= isatty (fileno (stdout
));
254 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
255 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
256 msgnum
+= (revflag
? -1 : 1)) {
257 if (is_selected(mp
, msgnum
)) {
258 charstring_t scanl
= NULL
;
260 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
261 admonish (cp
, "unable to open message");
266 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
270 * Check if message is in any sequence given
271 * by Unseen-Sequence profile entry.
274 for (i
= 0; i
< num_unseen_seq
; i
++) {
275 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
281 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
282 msgnum
== mp
->curmsg
, unseen
,
283 folder
, 0L, 1, &scanl
)) {
290 die("scan() botch (%d)", state
);
293 inform("message %d: empty", msgnum
);
296 charstring_free (scanl
);
305 ivector_free (seqnum
);
306 folder_free (mp
); /* free folder/message structure */