]>
diplodocus.org Git - nmh/blob - uip/scan.c
77341eb9625caf9dc494eb5448cf51039e113a03
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/brkstring.h"
10 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/seq_getnum.h"
15 #include "sbr/error.h"
16 #include "h/fmt_scan.h"
17 #include "h/scansbr.h"
22 #include "sbr/m_maildir.h"
23 #include "sbr/terminal.h"
25 #define SCAN_SWITCHES \
26 X("clear", 0, CLRSW) \
27 X("noclear", 0, NCLRSW) \
28 X("form formatfile", 0, FORMSW) \
29 X("format string", 5, FMTSW) \
30 X("header", 0, HEADSW) \
31 X("noheader", 0, NHEADSW) \
32 X("width columns", 0, WIDTHSW) \
33 X("reverse", 0, REVSW) \
34 X("noreverse", 0, NREVSW) \
35 X("file file", 4, FILESW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(SCAN
);
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
49 main (int argc
, char **argv
)
51 bool clearflag
= false;
57 ivector_t seqnum
= ivector_create (0);
59 int num_unseen_seq
= 0;
60 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
61 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
62 char **argp
, *nfs
, **arguments
;
63 struct msgs_array msgs
= { 0, 0, NULL
};
67 if (nmh_init(argv
[0], true, true)) { return 1; }
70 arguments
= getarguments (invo_name
, argc
, argv
, 1);
76 while ((cp
= *argp
++)) {
78 switch (smatch (++cp
, switches
)) {
80 ambigsw (cp
, switches
);
83 die("-%s unknown", cp
);
86 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
88 print_help (buf
, switches
, 1);
91 print_version(invo_name
);
102 if (!(form
= *argp
++) || *form
== '-')
103 die("missing argument to %s", argp
[-2]);
107 if (!(format
= *argp
++) || *format
== '-')
108 die("missing argument to %s", argp
[-2]);
120 if (!(cp
= *argp
++) || *cp
== '-')
121 die("missing argument to %s", argp
[-2]);
132 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
133 die("missing argument to %s", argp
[-2]);
134 if (strcmp (file
= cp
, "-"))
135 file
= path (cp
, TFILE
);
139 if (*cp
== '+' || *cp
== '@') {
141 die("only one folder at a time!");
142 folder
= pluspath (cp
);
144 app_msgarg(&msgs
, cp
);
147 if (!context_find ("path"))
148 free (path ("./", TFOLDER
));
151 * Get new format string. Must be before chdir().
153 nfs
= new_fs (form
, format
, FORMAT
);
156 * We are scanning a maildrop file
160 die("\"msgs\" not allowed with -file");
162 die("\"+folder\" not allowed with -file");
164 /* check if "file" is really stdin */
165 if (strcmp (file
, "-") == 0) {
169 if ((in
= fopen (file
, "r")) == NULL
)
170 adios (file
, "unable to open");
174 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
177 scan_detect_mbox_style (in
);
178 for (msgnum
= 1; ; ++msgnum
) {
179 charstring_t scanl
= NULL
;
181 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
182 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
183 charstring_free (scanl
);
184 if (state
!= SCNMSG
&& state
!= SCNENC
)
193 * We are scanning a folder
197 app_msgarg(&msgs
, "all");
199 folder
= getfolder (1);
200 maildir
= m_maildir (folder
);
202 if (chdir (maildir
) == NOTOK
)
203 adios (maildir
, "unable to change directory to");
205 /* read folder and create message structure */
206 if (!(mp
= folder_read (folder
, 1)))
207 die("unable to read folder %s", folder
);
209 /* check for empty folder */
211 die("no messages in %s", folder
);
213 /* parse all the message ranges/sequences and set SELECTED */
214 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
215 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
217 seq_setprev (mp
); /* set the Previous-Sequence */
219 context_replace (pfolder
, folder
); /* update current folder */
220 seq_save (mp
); /* synchronize message sequences */
221 context_save (); /* save the context file */
224 * Get the sequence number for each sequence
225 * specified by Unseen-Sequence
227 if ((cp
= context_find (usequence
)) && *cp
) {
231 ap
= brkstring (dp
, " ", "\n");
232 for (i
= 0; ap
&& *ap
; i
++, ap
++)
233 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
239 ontty
= isatty (fileno (stdout
));
241 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
242 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
243 msgnum
+= (revflag
? -1 : 1)) {
244 if (is_selected(mp
, msgnum
)) {
245 charstring_t scanl
= NULL
;
247 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
248 admonish (cp
, "unable to open message");
253 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
257 * Check if message is in any sequence given
258 * by Unseen-Sequence profile entry.
261 for (i
= 0; i
< num_unseen_seq
; i
++) {
262 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
268 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
269 msgnum
== mp
->curmsg
, unseen
,
270 folder
, 0L, 1, &scanl
)) {
277 die("scan() botch (%d)", state
);
280 inform("message %d: empty", msgnum
);
283 charstring_free (scanl
);
292 ivector_free (seqnum
);
293 folder_free (mp
); /* free folder/message structure */