]>
diplodocus.org Git - nmh/blob - uip/scan.c
7ad1f7b3ce1d89a7ef4570bf7799c29dfcc86024
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.
10 #include <h/fmt_scan.h>
11 #include <h/scansbr.h>
16 #include "sbr/m_maildir.h"
17 #include "sbr/terminal.h"
19 #define SCAN_SWITCHES \
20 X("clear", 0, CLRSW) \
21 X("noclear", 0, NCLRSW) \
22 X("form formatfile", 0, FORMSW) \
23 X("format string", 5, FMTSW) \
24 X("header", 0, HEADSW) \
25 X("noheader", 0, NHEADSW) \
26 X("width columns", 0, WIDTHSW) \
27 X("reverse", 0, REVSW) \
28 X("noreverse", 0, NREVSW) \
29 X("file file", 4, FILESW) \
30 X("version", 0, VERSIONSW) \
31 X("help", 0, HELPSW) \
33 #define X(sw, minchars, id) id,
34 DEFINE_SWITCH_ENUM(SCAN
);
37 #define X(sw, minchars, id) { sw, minchars, id },
38 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
43 main (int argc
, char **argv
)
45 bool clearflag
= false;
51 ivector_t seqnum
= ivector_create (0);
53 int num_unseen_seq
= 0;
54 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
55 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
56 char **argp
, *nfs
, **arguments
;
57 struct msgs_array msgs
= { 0, 0, NULL
};
61 if (nmh_init(argv
[0], true, true)) { return 1; }
64 arguments
= getarguments (invo_name
, argc
, argv
, 1);
70 while ((cp
= *argp
++)) {
72 switch (smatch (++cp
, switches
)) {
74 ambigsw (cp
, switches
);
77 die("-%s unknown", cp
);
80 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
82 print_help (buf
, switches
, 1);
85 print_version(invo_name
);
96 if (!(form
= *argp
++) || *form
== '-')
97 die("missing argument to %s", argp
[-2]);
101 if (!(format
= *argp
++) || *format
== '-')
102 die("missing argument to %s", argp
[-2]);
114 if (!(cp
= *argp
++) || *cp
== '-')
115 die("missing argument to %s", argp
[-2]);
126 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
127 die("missing argument to %s", argp
[-2]);
128 if (strcmp (file
= cp
, "-"))
129 file
= path (cp
, TFILE
);
133 if (*cp
== '+' || *cp
== '@') {
135 die("only one folder at a time!");
136 folder
= pluspath (cp
);
138 app_msgarg(&msgs
, cp
);
141 if (!context_find ("path"))
142 free (path ("./", TFOLDER
));
145 * Get new format string. Must be before chdir().
147 nfs
= new_fs (form
, format
, FORMAT
);
150 * We are scanning a maildrop file
154 die("\"msgs\" not allowed with -file");
156 die("\"+folder\" not allowed with -file");
158 /* check if "file" is really stdin */
159 if (strcmp (file
, "-") == 0) {
163 if ((in
= fopen (file
, "r")) == NULL
)
164 adios (file
, "unable to open");
168 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
171 scan_detect_mbox_style (in
);
172 for (msgnum
= 1; ; ++msgnum
) {
173 charstring_t scanl
= NULL
;
175 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
176 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
177 charstring_free (scanl
);
178 if (state
!= SCNMSG
&& state
!= SCNENC
)
187 * We are scanning a folder
191 app_msgarg(&msgs
, "all");
193 folder
= getfolder (1);
194 maildir
= m_maildir (folder
);
196 if (chdir (maildir
) == NOTOK
)
197 adios (maildir
, "unable to change directory to");
199 /* read folder and create message structure */
200 if (!(mp
= folder_read (folder
, 1)))
201 die("unable to read folder %s", folder
);
203 /* check for empty folder */
205 die("no messages in %s", folder
);
207 /* parse all the message ranges/sequences and set SELECTED */
208 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
209 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
211 seq_setprev (mp
); /* set the Previous-Sequence */
213 context_replace (pfolder
, folder
); /* update current folder */
214 seq_save (mp
); /* synchronize message sequences */
215 context_save (); /* save the context file */
218 * Get the sequence number for each sequence
219 * specified by Unseen-Sequence
221 if ((cp
= context_find (usequence
)) && *cp
) {
225 ap
= brkstring (dp
, " ", "\n");
226 for (i
= 0; ap
&& *ap
; i
++, ap
++)
227 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
233 ontty
= isatty (fileno (stdout
));
235 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
236 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
237 msgnum
+= (revflag
? -1 : 1)) {
238 if (is_selected(mp
, msgnum
)) {
239 charstring_t scanl
= NULL
;
241 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
242 admonish (cp
, "unable to open message");
247 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
251 * Check if message is in any sequence given
252 * by Unseen-Sequence profile entry.
255 for (i
= 0; i
< num_unseen_seq
; i
++) {
256 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
262 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
263 msgnum
== mp
->curmsg
, unseen
,
264 folder
, 0L, 1, &scanl
)) {
271 die("scan() botch (%d)", state
);
274 inform("message %d: empty", msgnum
);
277 charstring_free (scanl
);
286 ivector_free (seqnum
);
287 folder_free (mp
); /* free folder/message structure */