]>
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.
9 #include "sbr/folder_read.h"
10 #include "sbr/folder_free.h"
11 #include "sbr/context_save.h"
12 #include "sbr/context_replace.h"
13 #include "sbr/context_find.h"
14 #include "sbr/brkstring.h"
15 #include "sbr/ambigsw.h"
17 #include "sbr/print_version.h"
18 #include "sbr/print_help.h"
19 #include "sbr/seq_getnum.h"
20 #include "sbr/error.h"
21 #include "h/fmt_scan.h"
22 #include "h/scansbr.h"
27 #include "sbr/m_maildir.h"
28 #include "sbr/terminal.h"
30 #define SCAN_SWITCHES \
31 X("clear", 0, CLRSW) \
32 X("noclear", 0, NCLRSW) \
33 X("form formatfile", 0, FORMSW) \
34 X("format string", 5, FMTSW) \
35 X("header", 0, HEADSW) \
36 X("noheader", 0, NHEADSW) \
37 X("width columns", 0, WIDTHSW) \
38 X("reverse", 0, REVSW) \
39 X("noreverse", 0, NREVSW) \
40 X("file file", 4, FILESW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(SCAN
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
54 main (int argc
, char **argv
)
56 bool clearflag
= false;
62 ivector_t seqnum
= ivector_create (0);
64 int num_unseen_seq
= 0;
65 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
66 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
67 char **argp
, *nfs
, **arguments
;
68 struct msgs_array msgs
= { 0, 0, NULL
};
72 if (nmh_init(argv
[0], true, true)) { return 1; }
75 arguments
= getarguments (invo_name
, argc
, argv
, 1);
81 while ((cp
= *argp
++)) {
83 switch (smatch (++cp
, switches
)) {
85 ambigsw (cp
, switches
);
88 die("-%s unknown", cp
);
91 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
93 print_help (buf
, switches
, 1);
96 print_version(invo_name
);
107 if (!(form
= *argp
++) || *form
== '-')
108 die("missing argument to %s", argp
[-2]);
112 if (!(format
= *argp
++) || *format
== '-')
113 die("missing argument to %s", argp
[-2]);
125 if (!(cp
= *argp
++) || *cp
== '-')
126 die("missing argument to %s", argp
[-2]);
137 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
138 die("missing argument to %s", argp
[-2]);
139 if (strcmp (file
= cp
, "-"))
140 file
= path (cp
, TFILE
);
144 if (*cp
== '+' || *cp
== '@') {
146 die("only one folder at a time!");
147 folder
= pluspath (cp
);
149 app_msgarg(&msgs
, cp
);
152 if (!context_find ("path"))
153 free (path ("./", TFOLDER
));
156 * Get new format string. Must be before chdir().
158 nfs
= new_fs (form
, format
, FORMAT
);
161 * We are scanning a maildrop file
165 die("\"msgs\" not allowed with -file");
167 die("\"+folder\" not allowed with -file");
169 /* check if "file" is really stdin */
170 if (strcmp (file
, "-") == 0) {
174 if ((in
= fopen (file
, "r")) == NULL
)
175 adios (file
, "unable to open");
179 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
182 scan_detect_mbox_style (in
);
183 for (msgnum
= 1; ; ++msgnum
) {
184 charstring_t scanl
= NULL
;
186 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
187 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
188 charstring_free (scanl
);
189 if (state
!= SCNMSG
&& state
!= SCNENC
)
198 * We are scanning a folder
202 app_msgarg(&msgs
, "all");
204 folder
= getfolder (1);
205 maildir
= m_maildir (folder
);
207 if (chdir (maildir
) == NOTOK
)
208 adios (maildir
, "unable to change directory to");
210 /* read folder and create message structure */
211 if (!(mp
= folder_read (folder
, 1)))
212 die("unable to read folder %s", folder
);
214 /* check for empty folder */
216 die("no messages in %s", folder
);
218 /* parse all the message ranges/sequences and set SELECTED */
219 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
220 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
222 seq_setprev (mp
); /* set the Previous-Sequence */
224 context_replace (pfolder
, folder
); /* update current folder */
225 seq_save (mp
); /* synchronize message sequences */
226 context_save (); /* save the context file */
229 * Get the sequence number for each sequence
230 * specified by Unseen-Sequence
232 if ((cp
= context_find (usequence
)) && *cp
) {
236 ap
= brkstring (dp
, " ", "\n");
237 for (i
= 0; ap
&& *ap
; i
++, ap
++)
238 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
244 ontty
= isatty (fileno (stdout
));
246 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
247 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
248 msgnum
+= (revflag
? -1 : 1)) {
249 if (is_selected(mp
, msgnum
)) {
250 charstring_t scanl
= NULL
;
252 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
253 admonish (cp
, "unable to open message");
258 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
262 * Check if message is in any sequence given
263 * by Unseen-Sequence profile entry.
266 for (i
= 0; i
< num_unseen_seq
; i
++) {
267 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
273 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
274 msgnum
== mp
->curmsg
, unseen
,
275 folder
, 0L, 1, &scanl
)) {
282 die("scan() botch (%d)", state
);
285 inform("message %d: empty", msgnum
);
288 charstring_free (scanl
);
297 ivector_free (seqnum
);
298 folder_free (mp
); /* free folder/message structure */