]>
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 <h/fmt_scan.h>
10 #include <h/scansbr.h>
15 #include "sbr/m_maildir.h"
16 #include "sbr/terminal.h"
18 #define SCAN_SWITCHES \
19 X("clear", 0, CLRSW) \
20 X("noclear", 0, NCLRSW) \
21 X("form formatfile", 0, FORMSW) \
22 X("format string", 5, FMTSW) \
23 X("header", 0, HEADSW) \
24 X("noheader", 0, NHEADSW) \
25 X("width columns", 0, WIDTHSW) \
26 X("reverse", 0, REVSW) \
27 X("noreverse", 0, NREVSW) \
28 X("file file", 4, FILESW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
32 #define X(sw, minchars, id) id,
33 DEFINE_SWITCH_ENUM(SCAN
);
36 #define X(sw, minchars, id) { sw, minchars, id },
37 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
42 main (int argc
, char **argv
)
44 bool clearflag
= false;
50 ivector_t seqnum
= ivector_create (0);
52 int num_unseen_seq
= 0;
53 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
54 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
55 char **argp
, *nfs
, **arguments
;
56 struct msgs_array msgs
= { 0, 0, NULL
};
60 if (nmh_init(argv
[0], true, true)) { return 1; }
63 arguments
= getarguments (invo_name
, argc
, argv
, 1);
69 while ((cp
= *argp
++)) {
71 switch (smatch (++cp
, switches
)) {
73 ambigsw (cp
, switches
);
76 die("-%s unknown", cp
);
79 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
81 print_help (buf
, switches
, 1);
84 print_version(invo_name
);
95 if (!(form
= *argp
++) || *form
== '-')
96 die("missing argument to %s", argp
[-2]);
100 if (!(format
= *argp
++) || *format
== '-')
101 die("missing argument to %s", argp
[-2]);
113 if (!(cp
= *argp
++) || *cp
== '-')
114 die("missing argument to %s", argp
[-2]);
125 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
126 die("missing argument to %s", argp
[-2]);
127 if (strcmp (file
= cp
, "-"))
128 file
= path (cp
, TFILE
);
132 if (*cp
== '+' || *cp
== '@') {
134 die("only one folder at a time!");
135 folder
= pluspath (cp
);
137 app_msgarg(&msgs
, cp
);
140 if (!context_find ("path"))
141 free (path ("./", TFOLDER
));
144 * Get new format string. Must be before chdir().
146 nfs
= new_fs (form
, format
, FORMAT
);
149 * We are scanning a maildrop file
153 die("\"msgs\" not allowed with -file");
155 die("\"+folder\" not allowed with -file");
157 /* check if "file" is really stdin */
158 if (strcmp (file
, "-") == 0) {
162 if ((in
= fopen (file
, "r")) == NULL
)
163 adios (file
, "unable to open");
167 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
170 scan_detect_mbox_style (in
);
171 for (msgnum
= 1; ; ++msgnum
) {
172 charstring_t scanl
= NULL
;
174 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
175 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
176 charstring_free (scanl
);
177 if (state
!= SCNMSG
&& state
!= SCNENC
)
186 * We are scanning a folder
190 app_msgarg(&msgs
, "all");
192 folder
= getfolder (1);
193 maildir
= m_maildir (folder
);
195 if (chdir (maildir
) == NOTOK
)
196 adios (maildir
, "unable to change directory to");
198 /* read folder and create message structure */
199 if (!(mp
= folder_read (folder
, 1)))
200 die("unable to read folder %s", folder
);
202 /* check for empty folder */
204 die("no messages in %s", folder
);
206 /* parse all the message ranges/sequences and set SELECTED */
207 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
208 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
210 seq_setprev (mp
); /* set the Previous-Sequence */
212 context_replace (pfolder
, folder
); /* update current folder */
213 seq_save (mp
); /* synchronize message sequences */
214 context_save (); /* save the context file */
217 * Get the sequence number for each sequence
218 * specified by Unseen-Sequence
220 if ((cp
= context_find (usequence
)) && *cp
) {
224 ap
= brkstring (dp
, " ", "\n");
225 for (i
= 0; ap
&& *ap
; i
++, ap
++)
226 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
232 ontty
= isatty (fileno (stdout
));
234 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
235 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
236 msgnum
+= (revflag
? -1 : 1)) {
237 if (is_selected(mp
, msgnum
)) {
238 charstring_t scanl
= NULL
;
240 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
241 admonish (cp
, "unable to open message");
246 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
250 * Check if message is in any sequence given
251 * by Unseen-Sequence profile entry.
254 for (i
= 0; i
< num_unseen_seq
; i
++) {
255 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
261 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
262 msgnum
== mp
->curmsg
, unseen
,
263 folder
, 0L, 1, &scanl
)) {
270 die("scan() botch (%d)", state
);
273 inform("message %d: empty", msgnum
);
276 charstring_free (scanl
);
285 ivector_free (seqnum
);
286 folder_free (mp
); /* free folder/message structure */