]>
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
);
43 void clear_screen(void); /* from termsbr.c */
47 main (int argc
, char **argv
)
49 int clearflag
= 0, hdrflag
= 0, ontty
;
50 int width
= 0, revflag
= 0;
52 ivector_t seqnum
= ivector_create (0);
53 int unseen
, 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
};
62 setlocale(LC_ALL
, "");
64 invo_name
= r1bindex (argv
[0], '/');
66 /* read user profile/context */
70 arguments
= getarguments (invo_name
, argc
, argv
, 1);
76 while ((cp
= *argp
++)) {
78 switch (smatch (++cp
, switches
)) {
80 ambigsw (cp
, switches
);
83 adios (NULL
, "-%s unknown", cp
);
86 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
88 print_help (buf
, switches
, 1);
91 print_version(invo_name
);
102 if (!(form
= *argp
++) || *form
== '-')
103 adios (NULL
, "missing argument to %s", argp
[-2]);
107 if (!(format
= *argp
++) || *format
== '-')
108 adios (NULL
, "missing argument to %s", argp
[-2]);
120 if (!(cp
= *argp
++) || *cp
== '-')
121 adios (NULL
, "missing argument to %s", argp
[-2]);
132 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
133 adios (NULL
, "missing argument to %s", argp
[-2]);
134 if (strcmp (file
= cp
, "-"))
135 file
= path (cp
, TFILE
);
139 if (*cp
== '+' || *cp
== '@') {
141 adios (NULL
, "only one folder at a time!");
143 folder
= pluspath (cp
);
145 app_msgarg(&msgs
, cp
);
148 if (!context_find ("path"))
149 free (path ("./", TFOLDER
));
152 * Get new format string. Must be before chdir().
154 nfs
= new_fs (form
, format
, FORMAT
);
157 * We are scanning a maildrop file
161 adios (NULL
, "\"msgs\" not allowed with -file");
163 adios (NULL
, "\"+folder\" not allowed with -file");
165 /* check if "file" is really stdin */
166 if (strcmp (file
, "-") == 0) {
170 if ((in
= fopen (file
, "r")) == NULL
)
171 adios (file
, "unable to open");
175 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
178 scan_detect_mbox_style (in
);
179 for (msgnum
= 1; ; ++msgnum
) {
180 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
181 hdrflag
? file
: NULL
, 0L, 1);
182 if (state
!= SCNMSG
&& state
!= SCNENC
)
191 * We are scanning a folder
195 app_msgarg(&msgs
, "all");
197 folder
= getfolder (1);
198 maildir
= m_maildir (folder
);
200 if (chdir (maildir
) == NOTOK
)
201 adios (maildir
, "unable to change directory to");
203 /* read folder and create message structure */
204 if (!(mp
= folder_read (folder
, 1)))
205 adios (NULL
, "unable to read folder %s", folder
);
207 /* check for empty folder */
209 adios (NULL
, "no messages in %s", folder
);
211 /* parse all the message ranges/sequences and set SELECTED */
212 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
213 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
215 seq_setprev (mp
); /* set the Previous-Sequence */
217 context_replace (pfolder
, folder
); /* update current folder */
218 seq_save (mp
); /* synchronize message sequences */
219 context_save (); /* save the context file */
222 * Get the sequence number for each sequence
223 * specified by Unseen-Sequence
225 if ((cp
= context_find (usequence
)) && *cp
) {
229 ap
= brkstring (dp
, " ", "\n");
230 for (i
= 0; ap
&& *ap
; i
++, ap
++)
231 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
238 ontty
= isatty (fileno (stdout
));
240 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
241 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
242 msgnum
+= (revflag
? -1 : 1)) {
243 if (is_selected(mp
, msgnum
)) {
244 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
245 admonish (cp
, "unable to open message");
250 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
254 * Check if message is in any sequence given
255 * by Unseen-Sequence profile entry.
258 for (i
= 0; i
< num_unseen_seq
; i
++) {
259 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
265 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
266 msgnum
== mp
->curmsg
, unseen
,
274 adios (NULL
, "scan() botch (%d)", state
);
277 advise (NULL
, "message %d: empty", msgnum
);
288 ivector_free (seqnum
);
289 folder_free (mp
); /* free folder/message structure */