]>
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/fmt_new.h"
10 #include "sbr/dtime.h"
12 #include "sbr/m_name.h"
13 #include "sbr/getarguments.h"
14 #include "sbr/seq_setprev.h"
15 #include "sbr/seq_save.h"
16 #include "sbr/smatch.h"
17 #include "sbr/m_convert.h"
18 #include "sbr/getfolder.h"
19 #include "sbr/folder_read.h"
20 #include "sbr/folder_free.h"
21 #include "sbr/context_save.h"
22 #include "sbr/context_replace.h"
23 #include "sbr/context_find.h"
24 #include "sbr/brkstring.h"
25 #include "sbr/ambigsw.h"
27 #include "sbr/print_version.h"
28 #include "sbr/print_help.h"
29 #include "sbr/seq_getnum.h"
30 #include "sbr/error.h"
31 #include "h/fmt_scan.h"
36 #include "sbr/m_maildir.h"
37 #include "sbr/terminal.h"
39 #define SCAN_SWITCHES \
40 X("clear", 0, CLRSW) \
41 X("noclear", 0, NCLRSW) \
42 X("form formatfile", 0, FORMSW) \
43 X("format string", 5, FMTSW) \
44 X("header", 0, HEADSW) \
45 X("noheader", 0, NHEADSW) \
46 X("width columns", 0, WIDTHSW) \
47 X("reverse", 0, REVSW) \
48 X("noreverse", 0, NREVSW) \
49 X("file file", 4, FILESW) \
50 X("version", 0, VERSIONSW) \
51 X("help", 0, HELPSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(SCAN
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
63 main (int argc
, char **argv
)
65 bool clearflag
= false;
71 ivector_t seqnum
= ivector_create (0);
73 int num_unseen_seq
= 0;
74 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
75 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
76 char **argp
, *nfs
, **arguments
;
77 struct msgs_array msgs
= { 0, 0, NULL
};
81 if (nmh_init(argv
[0], true, true)) { return 1; }
84 arguments
= getarguments (invo_name
, argc
, argv
, 1);
90 while ((cp
= *argp
++)) {
92 switch (smatch (++cp
, switches
)) {
94 ambigsw (cp
, switches
);
97 die("-%s unknown", cp
);
100 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
102 print_help (buf
, switches
, 1);
105 print_version(invo_name
);
116 if (!(form
= *argp
++) || *form
== '-')
117 die("missing argument to %s", argp
[-2]);
121 if (!(format
= *argp
++) || *format
== '-')
122 die("missing argument to %s", argp
[-2]);
134 if (!(cp
= *argp
++) || *cp
== '-')
135 die("missing argument to %s", argp
[-2]);
146 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
147 die("missing argument to %s", argp
[-2]);
148 if (strcmp (file
= cp
, "-"))
149 file
= path (cp
, TFILE
);
153 if (*cp
== '+' || *cp
== '@') {
155 die("only one folder at a time!");
156 folder
= pluspath (cp
);
158 app_msgarg(&msgs
, cp
);
161 if (!context_find ("path"))
162 free (path ("./", TFOLDER
));
165 * Get new format string. Must be before chdir().
167 nfs
= new_fs (form
, format
, FORMAT
);
170 * We are scanning a maildrop file
174 die("\"msgs\" not allowed with -file");
176 die("\"+folder\" not allowed with -file");
178 /* check if "file" is really stdin */
179 if (strcmp (file
, "-") == 0) {
183 if ((in
= fopen (file
, "r")) == NULL
)
184 adios (file
, "unable to open");
188 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
191 scan_detect_mbox_style (in
);
192 for (msgnum
= 1; ; ++msgnum
) {
193 charstring_t scanl
= NULL
;
195 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
196 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
197 charstring_free (scanl
);
198 if (state
!= SCNMSG
&& state
!= SCNENC
)
207 * We are scanning a folder
211 app_msgarg(&msgs
, "all");
213 folder
= getfolder (1);
214 maildir
= m_maildir (folder
);
216 if (chdir (maildir
) == NOTOK
)
217 adios (maildir
, "unable to change directory to");
219 /* read folder and create message structure */
220 if (!(mp
= folder_read (folder
, 1)))
221 die("unable to read folder %s", folder
);
223 /* check for empty folder */
225 die("no messages in %s", folder
);
227 /* parse all the message ranges/sequences and set SELECTED */
228 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
229 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
231 seq_setprev (mp
); /* set the Previous-Sequence */
233 context_replace (pfolder
, folder
); /* update current folder */
234 seq_save (mp
); /* synchronize message sequences */
235 context_save (); /* save the context file */
238 * Get the sequence number for each sequence
239 * specified by Unseen-Sequence
241 if ((cp
= context_find (usequence
)) && *cp
) {
245 ap
= brkstring (dp
, " ", "\n");
246 for (i
= 0; ap
&& *ap
; i
++, ap
++)
247 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
253 ontty
= isatty (fileno (stdout
));
255 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
256 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
257 msgnum
+= (revflag
? -1 : 1)) {
258 if (is_selected(mp
, msgnum
)) {
259 charstring_t scanl
= NULL
;
261 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
262 admonish (cp
, "unable to open message");
267 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
271 * Check if message is in any sequence given
272 * by Unseen-Sequence profile entry.
275 for (i
= 0; i
< num_unseen_seq
; i
++) {
276 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
282 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
283 msgnum
== mp
->curmsg
, unseen
,
284 folder
, 0L, 1, &scanl
)) {
291 die("scan() botch (%d)", state
);
294 inform("message %d: empty", msgnum
);
297 charstring_free (scanl
);
306 ivector_free (seqnum
);
307 folder_free (mp
); /* free folder/message structure */