]>
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/m_convert.h"
10 #include "sbr/getfolder.h"
11 #include "sbr/folder_read.h"
12 #include "sbr/folder_free.h"
13 #include "sbr/context_save.h"
14 #include "sbr/context_replace.h"
15 #include "sbr/context_find.h"
16 #include "sbr/brkstring.h"
17 #include "sbr/ambigsw.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/seq_getnum.h"
22 #include "sbr/error.h"
23 #include "h/fmt_scan.h"
24 #include "h/scansbr.h"
29 #include "sbr/m_maildir.h"
30 #include "sbr/terminal.h"
32 #define SCAN_SWITCHES \
33 X("clear", 0, CLRSW) \
34 X("noclear", 0, NCLRSW) \
35 X("form formatfile", 0, FORMSW) \
36 X("format string", 5, FMTSW) \
37 X("header", 0, HEADSW) \
38 X("noheader", 0, NHEADSW) \
39 X("width columns", 0, WIDTHSW) \
40 X("reverse", 0, REVSW) \
41 X("noreverse", 0, NREVSW) \
42 X("file file", 4, FILESW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
46 #define X(sw, minchars, id) id,
47 DEFINE_SWITCH_ENUM(SCAN
);
50 #define X(sw, minchars, id) { sw, minchars, id },
51 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
56 main (int argc
, char **argv
)
58 bool clearflag
= false;
64 ivector_t seqnum
= ivector_create (0);
66 int num_unseen_seq
= 0;
67 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
68 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
69 char **argp
, *nfs
, **arguments
;
70 struct msgs_array msgs
= { 0, 0, NULL
};
74 if (nmh_init(argv
[0], true, true)) { return 1; }
77 arguments
= getarguments (invo_name
, argc
, argv
, 1);
83 while ((cp
= *argp
++)) {
85 switch (smatch (++cp
, switches
)) {
87 ambigsw (cp
, switches
);
90 die("-%s unknown", cp
);
93 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
95 print_help (buf
, switches
, 1);
98 print_version(invo_name
);
109 if (!(form
= *argp
++) || *form
== '-')
110 die("missing argument to %s", argp
[-2]);
114 if (!(format
= *argp
++) || *format
== '-')
115 die("missing argument to %s", argp
[-2]);
127 if (!(cp
= *argp
++) || *cp
== '-')
128 die("missing argument to %s", argp
[-2]);
139 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
140 die("missing argument to %s", argp
[-2]);
141 if (strcmp (file
= cp
, "-"))
142 file
= path (cp
, TFILE
);
146 if (*cp
== '+' || *cp
== '@') {
148 die("only one folder at a time!");
149 folder
= pluspath (cp
);
151 app_msgarg(&msgs
, cp
);
154 if (!context_find ("path"))
155 free (path ("./", TFOLDER
));
158 * Get new format string. Must be before chdir().
160 nfs
= new_fs (form
, format
, FORMAT
);
163 * We are scanning a maildrop file
167 die("\"msgs\" not allowed with -file");
169 die("\"+folder\" not allowed with -file");
171 /* check if "file" is really stdin */
172 if (strcmp (file
, "-") == 0) {
176 if ((in
= fopen (file
, "r")) == NULL
)
177 adios (file
, "unable to open");
181 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
184 scan_detect_mbox_style (in
);
185 for (msgnum
= 1; ; ++msgnum
) {
186 charstring_t scanl
= NULL
;
188 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
189 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
190 charstring_free (scanl
);
191 if (state
!= SCNMSG
&& state
!= SCNENC
)
200 * We are scanning a folder
204 app_msgarg(&msgs
, "all");
206 folder
= getfolder (1);
207 maildir
= m_maildir (folder
);
209 if (chdir (maildir
) == NOTOK
)
210 adios (maildir
, "unable to change directory to");
212 /* read folder and create message structure */
213 if (!(mp
= folder_read (folder
, 1)))
214 die("unable to read folder %s", folder
);
216 /* check for empty folder */
218 die("no messages in %s", folder
);
220 /* parse all the message ranges/sequences and set SELECTED */
221 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
222 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
224 seq_setprev (mp
); /* set the Previous-Sequence */
226 context_replace (pfolder
, folder
); /* update current folder */
227 seq_save (mp
); /* synchronize message sequences */
228 context_save (); /* save the context file */
231 * Get the sequence number for each sequence
232 * specified by Unseen-Sequence
234 if ((cp
= context_find (usequence
)) && *cp
) {
238 ap
= brkstring (dp
, " ", "\n");
239 for (i
= 0; ap
&& *ap
; i
++, ap
++)
240 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
246 ontty
= isatty (fileno (stdout
));
248 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
249 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
250 msgnum
+= (revflag
? -1 : 1)) {
251 if (is_selected(mp
, msgnum
)) {
252 charstring_t scanl
= NULL
;
254 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
255 admonish (cp
, "unable to open message");
260 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
264 * Check if message is in any sequence given
265 * by Unseen-Sequence profile entry.
268 for (i
= 0; i
< num_unseen_seq
; i
++) {
269 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
275 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
276 msgnum
== mp
->curmsg
, unseen
,
277 folder
, 0L, 1, &scanl
)) {
284 die("scan() botch (%d)", state
);
287 inform("message %d: empty", msgnum
);
290 charstring_free (scanl
);
299 ivector_free (seqnum
);
300 folder_free (mp
); /* free folder/message structure */