]>
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 #define SCAN_SWITCHES \
16 X("clear", 0, CLRSW) \
17 X("noclear", 0, NCLRSW) \
18 X("form formatfile", 0, FORMSW) \
19 X("format string", 5, FMTSW) \
20 X("header", 0, HEADSW) \
21 X("noheader", 0, NHEADSW) \
22 X("width columns", 0, WIDTHSW) \
23 X("reverse", 0, REVSW) \
24 X("noreverse", 0, NREVSW) \
25 X("file file", 4, FILESW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(SCAN
);
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
39 main (int argc
, char **argv
)
41 int clearflag
= 0, hdrflag
= 0, ontty
;
42 int width
= -1, revflag
= 0;
44 ivector_t seqnum
= ivector_create (0);
45 int unseen
, num_unseen_seq
= 0;
46 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
47 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
48 char **argp
, *nfs
, **arguments
;
49 struct msgs_array msgs
= { 0, 0, NULL
};
53 if (nmh_init(argv
[0], 1)) { return 1; }
56 arguments
= getarguments (invo_name
, argc
, argv
, 1);
62 while ((cp
= *argp
++)) {
64 switch (smatch (++cp
, switches
)) {
66 ambigsw (cp
, switches
);
69 adios (NULL
, "-%s unknown", cp
);
72 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
74 print_help (buf
, switches
, 1);
77 print_version(invo_name
);
88 if (!(form
= *argp
++) || *form
== '-')
89 adios (NULL
, "missing argument to %s", argp
[-2]);
93 if (!(format
= *argp
++) || *format
== '-')
94 adios (NULL
, "missing argument to %s", argp
[-2]);
106 if (!(cp
= *argp
++) || *cp
== '-')
107 adios (NULL
, "missing argument to %s", argp
[-2]);
118 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
119 adios (NULL
, "missing argument to %s", argp
[-2]);
120 if (strcmp (file
= cp
, "-"))
121 file
= path (cp
, TFILE
);
125 if (*cp
== '+' || *cp
== '@') {
127 adios (NULL
, "only one folder at a time!");
129 folder
= pluspath (cp
);
131 app_msgarg(&msgs
, cp
);
134 if (!context_find ("path"))
135 free (path ("./", TFOLDER
));
138 * Get new format string. Must be before chdir().
140 nfs
= new_fs (form
, format
, FORMAT
);
143 * We are scanning a maildrop file
147 adios (NULL
, "\"msgs\" not allowed with -file");
149 adios (NULL
, "\"+folder\" not allowed with -file");
151 /* check if "file" is really stdin */
152 if (strcmp (file
, "-") == 0) {
156 if ((in
= fopen (file
, "r")) == NULL
)
157 adios (file
, "unable to open");
161 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
164 scan_detect_mbox_style (in
);
165 for (msgnum
= 1; ; ++msgnum
) {
166 charstring_t scanl
= NULL
;
168 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
169 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
170 charstring_free (scanl
);
171 if (state
!= SCNMSG
&& state
!= SCNENC
)
180 * We are scanning a folder
184 app_msgarg(&msgs
, "all");
186 folder
= getfolder (1);
187 maildir
= m_maildir (folder
);
189 if (chdir (maildir
) == NOTOK
)
190 adios (maildir
, "unable to change directory to");
192 /* read folder and create message structure */
193 if (!(mp
= folder_read (folder
, 1)))
194 adios (NULL
, "unable to read folder %s", folder
);
196 /* check for empty folder */
198 adios (NULL
, "no messages in %s", folder
);
200 /* parse all the message ranges/sequences and set SELECTED */
201 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
202 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
204 seq_setprev (mp
); /* set the Previous-Sequence */
206 context_replace (pfolder
, folder
); /* update current folder */
207 seq_save (mp
); /* synchronize message sequences */
208 context_save (); /* save the context file */
211 * Get the sequence number for each sequence
212 * specified by Unseen-Sequence
214 if ((cp
= context_find (usequence
)) && *cp
) {
218 ap
= brkstring (dp
, " ", "\n");
219 for (i
= 0; ap
&& *ap
; i
++, ap
++)
220 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
226 ontty
= isatty (fileno (stdout
));
228 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
229 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
230 msgnum
+= (revflag
? -1 : 1)) {
231 if (is_selected(mp
, msgnum
)) {
232 charstring_t scanl
= NULL
;
234 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
235 admonish (cp
, "unable to open message");
240 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
244 * Check if message is in any sequence given
245 * by Unseen-Sequence profile entry.
248 for (i
= 0; i
< num_unseen_seq
; i
++) {
249 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
255 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
256 msgnum
== mp
->curmsg
, unseen
,
257 folder
, 0L, 1, &scanl
)) {
264 adios (NULL
, "scan() botch (%d)", state
);
267 inform("message %d: empty", msgnum
);
270 charstring_free (scanl
);
279 ivector_free (seqnum
);
280 folder_free (mp
); /* free folder/message structure */