]>
diplodocus.org Git - nmh/blob - uip/scan.c
52343f75ad526b30e997f6fe5418526ac01bdfab
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/folder_free.h"
10 #include "sbr/context_save.h"
11 #include "sbr/context_replace.h"
12 #include "sbr/context_find.h"
13 #include "sbr/brkstring.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/seq_getnum.h"
19 #include "sbr/error.h"
20 #include "h/fmt_scan.h"
21 #include "h/scansbr.h"
26 #include "sbr/m_maildir.h"
27 #include "sbr/terminal.h"
29 #define SCAN_SWITCHES \
30 X("clear", 0, CLRSW) \
31 X("noclear", 0, NCLRSW) \
32 X("form formatfile", 0, FORMSW) \
33 X("format string", 5, FMTSW) \
34 X("header", 0, HEADSW) \
35 X("noheader", 0, NHEADSW) \
36 X("width columns", 0, WIDTHSW) \
37 X("reverse", 0, REVSW) \
38 X("noreverse", 0, NREVSW) \
39 X("file file", 4, FILESW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(SCAN
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
53 main (int argc
, char **argv
)
55 bool clearflag
= false;
61 ivector_t seqnum
= ivector_create (0);
63 int num_unseen_seq
= 0;
64 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
65 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
66 char **argp
, *nfs
, **arguments
;
67 struct msgs_array msgs
= { 0, 0, NULL
};
71 if (nmh_init(argv
[0], true, true)) { return 1; }
74 arguments
= getarguments (invo_name
, argc
, argv
, 1);
80 while ((cp
= *argp
++)) {
82 switch (smatch (++cp
, switches
)) {
84 ambigsw (cp
, switches
);
87 die("-%s unknown", cp
);
90 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
92 print_help (buf
, switches
, 1);
95 print_version(invo_name
);
106 if (!(form
= *argp
++) || *form
== '-')
107 die("missing argument to %s", argp
[-2]);
111 if (!(format
= *argp
++) || *format
== '-')
112 die("missing argument to %s", argp
[-2]);
124 if (!(cp
= *argp
++) || *cp
== '-')
125 die("missing argument to %s", argp
[-2]);
136 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
137 die("missing argument to %s", argp
[-2]);
138 if (strcmp (file
= cp
, "-"))
139 file
= path (cp
, TFILE
);
143 if (*cp
== '+' || *cp
== '@') {
145 die("only one folder at a time!");
146 folder
= pluspath (cp
);
148 app_msgarg(&msgs
, cp
);
151 if (!context_find ("path"))
152 free (path ("./", TFOLDER
));
155 * Get new format string. Must be before chdir().
157 nfs
= new_fs (form
, format
, FORMAT
);
160 * We are scanning a maildrop file
164 die("\"msgs\" not allowed with -file");
166 die("\"+folder\" not allowed with -file");
168 /* check if "file" is really stdin */
169 if (strcmp (file
, "-") == 0) {
173 if ((in
= fopen (file
, "r")) == NULL
)
174 adios (file
, "unable to open");
178 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
181 scan_detect_mbox_style (in
);
182 for (msgnum
= 1; ; ++msgnum
) {
183 charstring_t scanl
= NULL
;
185 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
186 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
187 charstring_free (scanl
);
188 if (state
!= SCNMSG
&& state
!= SCNENC
)
197 * We are scanning a folder
201 app_msgarg(&msgs
, "all");
203 folder
= getfolder (1);
204 maildir
= m_maildir (folder
);
206 if (chdir (maildir
) == NOTOK
)
207 adios (maildir
, "unable to change directory to");
209 /* read folder and create message structure */
210 if (!(mp
= folder_read (folder
, 1)))
211 die("unable to read folder %s", folder
);
213 /* check for empty folder */
215 die("no messages in %s", folder
);
217 /* parse all the message ranges/sequences and set SELECTED */
218 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
219 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
221 seq_setprev (mp
); /* set the Previous-Sequence */
223 context_replace (pfolder
, folder
); /* update current folder */
224 seq_save (mp
); /* synchronize message sequences */
225 context_save (); /* save the context file */
228 * Get the sequence number for each sequence
229 * specified by Unseen-Sequence
231 if ((cp
= context_find (usequence
)) && *cp
) {
235 ap
= brkstring (dp
, " ", "\n");
236 for (i
= 0; ap
&& *ap
; i
++, ap
++)
237 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
243 ontty
= isatty (fileno (stdout
));
245 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
246 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
247 msgnum
+= (revflag
? -1 : 1)) {
248 if (is_selected(mp
, msgnum
)) {
249 charstring_t scanl
= NULL
;
251 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
252 admonish (cp
, "unable to open message");
257 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
261 * Check if message is in any sequence given
262 * by Unseen-Sequence profile entry.
265 for (i
= 0; i
< num_unseen_seq
; i
++) {
266 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
272 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
273 msgnum
== mp
->curmsg
, unseen
,
274 folder
, 0L, 1, &scanl
)) {
281 die("scan() botch (%d)", state
);
284 inform("message %d: empty", msgnum
);
287 charstring_free (scanl
);
296 ivector_free (seqnum
);
297 folder_free (mp
); /* free folder/message structure */