]>
diplodocus.org Git - nmh/blob - uip/scan.c
942895f23e9ed6009a8881e05e5cde8edfb3999a
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/getfolder.h"
10 #include "sbr/folder_read.h"
11 #include "sbr/folder_free.h"
12 #include "sbr/context_save.h"
13 #include "sbr/context_replace.h"
14 #include "sbr/context_find.h"
15 #include "sbr/brkstring.h"
16 #include "sbr/ambigsw.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/seq_getnum.h"
21 #include "sbr/error.h"
22 #include "h/fmt_scan.h"
23 #include "h/scansbr.h"
28 #include "sbr/m_maildir.h"
29 #include "sbr/terminal.h"
31 #define SCAN_SWITCHES \
32 X("clear", 0, CLRSW) \
33 X("noclear", 0, NCLRSW) \
34 X("form formatfile", 0, FORMSW) \
35 X("format string", 5, FMTSW) \
36 X("header", 0, HEADSW) \
37 X("noheader", 0, NHEADSW) \
38 X("width columns", 0, WIDTHSW) \
39 X("reverse", 0, REVSW) \
40 X("noreverse", 0, NREVSW) \
41 X("file file", 4, FILESW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(SCAN
);
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
55 main (int argc
, char **argv
)
57 bool clearflag
= false;
63 ivector_t seqnum
= ivector_create (0);
65 int num_unseen_seq
= 0;
66 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
67 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
68 char **argp
, *nfs
, **arguments
;
69 struct msgs_array msgs
= { 0, 0, NULL
};
73 if (nmh_init(argv
[0], true, true)) { return 1; }
76 arguments
= getarguments (invo_name
, argc
, argv
, 1);
82 while ((cp
= *argp
++)) {
84 switch (smatch (++cp
, switches
)) {
86 ambigsw (cp
, switches
);
89 die("-%s unknown", cp
);
92 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
94 print_help (buf
, switches
, 1);
97 print_version(invo_name
);
108 if (!(form
= *argp
++) || *form
== '-')
109 die("missing argument to %s", argp
[-2]);
113 if (!(format
= *argp
++) || *format
== '-')
114 die("missing argument to %s", argp
[-2]);
126 if (!(cp
= *argp
++) || *cp
== '-')
127 die("missing argument to %s", argp
[-2]);
138 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
139 die("missing argument to %s", argp
[-2]);
140 if (strcmp (file
= cp
, "-"))
141 file
= path (cp
, TFILE
);
145 if (*cp
== '+' || *cp
== '@') {
147 die("only one folder at a time!");
148 folder
= pluspath (cp
);
150 app_msgarg(&msgs
, cp
);
153 if (!context_find ("path"))
154 free (path ("./", TFOLDER
));
157 * Get new format string. Must be before chdir().
159 nfs
= new_fs (form
, format
, FORMAT
);
162 * We are scanning a maildrop file
166 die("\"msgs\" not allowed with -file");
168 die("\"+folder\" not allowed with -file");
170 /* check if "file" is really stdin */
171 if (strcmp (file
, "-") == 0) {
175 if ((in
= fopen (file
, "r")) == NULL
)
176 adios (file
, "unable to open");
180 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
183 scan_detect_mbox_style (in
);
184 for (msgnum
= 1; ; ++msgnum
) {
185 charstring_t scanl
= NULL
;
187 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
188 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
189 charstring_free (scanl
);
190 if (state
!= SCNMSG
&& state
!= SCNENC
)
199 * We are scanning a folder
203 app_msgarg(&msgs
, "all");
205 folder
= getfolder (1);
206 maildir
= m_maildir (folder
);
208 if (chdir (maildir
) == NOTOK
)
209 adios (maildir
, "unable to change directory to");
211 /* read folder and create message structure */
212 if (!(mp
= folder_read (folder
, 1)))
213 die("unable to read folder %s", folder
);
215 /* check for empty folder */
217 die("no messages in %s", folder
);
219 /* parse all the message ranges/sequences and set SELECTED */
220 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
221 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
223 seq_setprev (mp
); /* set the Previous-Sequence */
225 context_replace (pfolder
, folder
); /* update current folder */
226 seq_save (mp
); /* synchronize message sequences */
227 context_save (); /* save the context file */
230 * Get the sequence number for each sequence
231 * specified by Unseen-Sequence
233 if ((cp
= context_find (usequence
)) && *cp
) {
237 ap
= brkstring (dp
, " ", "\n");
238 for (i
= 0; ap
&& *ap
; i
++, ap
++)
239 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
245 ontty
= isatty (fileno (stdout
));
247 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
248 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
249 msgnum
+= (revflag
? -1 : 1)) {
250 if (is_selected(mp
, msgnum
)) {
251 charstring_t scanl
= NULL
;
253 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
254 admonish (cp
, "unable to open message");
259 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
263 * Check if message is in any sequence given
264 * by Unseen-Sequence profile entry.
267 for (i
= 0; i
< num_unseen_seq
; i
++) {
268 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
274 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
275 msgnum
== mp
->curmsg
, unseen
,
276 folder
, 0L, 1, &scanl
)) {
283 die("scan() botch (%d)", state
);
286 inform("message %d: empty", msgnum
);
289 charstring_free (scanl
);
298 ivector_free (seqnum
);
299 folder_free (mp
); /* free folder/message structure */