]>
diplodocus.org Git - nmh/blob - uip/scan.c
3 * scan.c -- display a one-line "scan" listing of folder or messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/fmt_scan.h>
12 #include <h/scansbr.h>
17 #define SCAN_SWITCHES \
18 X("clear", 0, CLRSW) \
19 X("noclear", 0, NCLRSW) \
20 X("form formatfile", 0, FORMSW) \
21 X("format string", 5, FMTSW) \
22 X("header", 0, HEADSW) \
23 X("noheader", 0, NHEADSW) \
24 X("width columns", 0, WIDTHSW) \
25 X("reverse", 0, REVSW) \
26 X("noreverse", 0, NREVSW) \
27 X("file file", 4, FILESW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(SCAN
);
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(SCAN
, switches
);
41 main (int argc
, char **argv
)
43 int clearflag
= 0, hdrflag
= 0, ontty
;
44 int width
= -1, revflag
= 0;
46 ivector_t seqnum
= ivector_create (0);
47 int unseen
, num_unseen_seq
= 0;
48 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
49 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
50 char **argp
, *nfs
, **arguments
;
51 struct msgs_array msgs
= { 0, 0, NULL
};
55 if (nmh_init(argv
[0], 1)) { return 1; }
58 arguments
= getarguments (invo_name
, argc
, argv
, 1);
64 while ((cp
= *argp
++)) {
66 switch (smatch (++cp
, switches
)) {
68 ambigsw (cp
, switches
);
71 adios (NULL
, "-%s unknown", cp
);
74 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
76 print_help (buf
, switches
, 1);
79 print_version(invo_name
);
90 if (!(form
= *argp
++) || *form
== '-')
91 adios (NULL
, "missing argument to %s", argp
[-2]);
95 if (!(format
= *argp
++) || *format
== '-')
96 adios (NULL
, "missing argument to %s", argp
[-2]);
108 if (!(cp
= *argp
++) || *cp
== '-')
109 adios (NULL
, "missing argument to %s", argp
[-2]);
120 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
121 adios (NULL
, "missing argument to %s", argp
[-2]);
122 if (strcmp (file
= cp
, "-"))
123 file
= path (cp
, TFILE
);
127 if (*cp
== '+' || *cp
== '@') {
129 adios (NULL
, "only one folder at a time!");
131 folder
= pluspath (cp
);
133 app_msgarg(&msgs
, cp
);
136 if (!context_find ("path"))
137 free (path ("./", TFOLDER
));
140 * Get new format string. Must be before chdir().
142 nfs
= new_fs (form
, format
, FORMAT
);
145 * We are scanning a maildrop file
149 adios (NULL
, "\"msgs\" not allowed with -file");
151 adios (NULL
, "\"+folder\" not allowed with -file");
153 /* check if "file" is really stdin */
154 if (strcmp (file
, "-") == 0) {
158 if ((in
= fopen (file
, "r")) == NULL
)
159 adios (file
, "unable to open");
163 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
166 scan_detect_mbox_style (in
);
167 for (msgnum
= 1; ; ++msgnum
) {
168 charstring_t scanl
= NULL
;
170 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
171 hdrflag
? file
: NULL
, 0L, 1, &scanl
);
172 charstring_free (scanl
);
173 if (state
!= SCNMSG
&& state
!= SCNENC
)
182 * We are scanning a folder
186 app_msgarg(&msgs
, "all");
188 folder
= getfolder (1);
189 maildir
= m_maildir (folder
);
191 if (chdir (maildir
) == NOTOK
)
192 adios (maildir
, "unable to change directory to");
194 /* read folder and create message structure */
195 if (!(mp
= folder_read (folder
, 1)))
196 adios (NULL
, "unable to read folder %s", folder
);
198 /* check for empty folder */
200 adios (NULL
, "no messages in %s", folder
);
202 /* parse all the message ranges/sequences and set SELECTED */
203 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
204 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
206 seq_setprev (mp
); /* set the Previous-Sequence */
208 context_replace (pfolder
, folder
); /* update current folder */
209 seq_save (mp
); /* synchronize message sequences */
210 context_save (); /* save the context file */
213 * Get the sequence number for each sequence
214 * specified by Unseen-Sequence
216 if ((cp
= context_find (usequence
)) && *cp
) {
220 ap
= brkstring (dp
, " ", "\n");
221 for (i
= 0; ap
&& *ap
; i
++, ap
++)
222 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
229 ontty
= isatty (fileno (stdout
));
231 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
232 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
233 msgnum
+= (revflag
? -1 : 1)) {
234 if (is_selected(mp
, msgnum
)) {
235 charstring_t scanl
= NULL
;
237 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
238 admonish (cp
, "unable to open message");
243 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
247 * Check if message is in any sequence given
248 * by Unseen-Sequence profile entry.
251 for (i
= 0; i
< num_unseen_seq
; i
++) {
252 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
258 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
259 msgnum
== mp
->curmsg
, unseen
,
260 folder
, 0L, 1, &scanl
)) {
267 adios (NULL
, "scan() botch (%d)", state
);
270 advise (NULL
, "message %d: empty", msgnum
);
273 charstring_free (scanl
);
282 ivector_free (seqnum
);
283 folder_free (mp
); /* free folder/message structure */