]>
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
= 0, 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
};
56 setlocale(LC_ALL
, "");
58 invo_name
= r1bindex (argv
[0], '/');
60 /* read user profile/context */
64 arguments
= getarguments (invo_name
, argc
, argv
, 1);
70 while ((cp
= *argp
++)) {
72 switch (smatch (++cp
, switches
)) {
74 ambigsw (cp
, switches
);
77 adios (NULL
, "-%s unknown", cp
);
80 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
82 print_help (buf
, switches
, 1);
85 print_version(invo_name
);
96 if (!(form
= *argp
++) || *form
== '-')
97 adios (NULL
, "missing argument to %s", argp
[-2]);
101 if (!(format
= *argp
++) || *format
== '-')
102 adios (NULL
, "missing argument to %s", argp
[-2]);
114 if (!(cp
= *argp
++) || *cp
== '-')
115 adios (NULL
, "missing argument to %s", argp
[-2]);
126 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
127 adios (NULL
, "missing argument to %s", argp
[-2]);
128 if (strcmp (file
= cp
, "-"))
129 file
= path (cp
, TFILE
);
133 if (*cp
== '+' || *cp
== '@') {
135 adios (NULL
, "only one folder at a time!");
137 folder
= pluspath (cp
);
139 app_msgarg(&msgs
, cp
);
142 if (!context_find ("path"))
143 free (path ("./", TFOLDER
));
146 * Get new format string. Must be before chdir().
148 nfs
= new_fs (form
, format
, FORMAT
);
151 * We are scanning a maildrop file
155 adios (NULL
, "\"msgs\" not allowed with -file");
157 adios (NULL
, "\"+folder\" not allowed with -file");
159 /* check if "file" is really stdin */
160 if (strcmp (file
, "-") == 0) {
164 if ((in
= fopen (file
, "r")) == NULL
)
165 adios (file
, "unable to open");
169 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
172 scan_detect_mbox_style (in
);
173 for (msgnum
= 1; ; ++msgnum
) {
174 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
175 hdrflag
? file
: NULL
, 0L, 1);
176 if (state
!= SCNMSG
&& state
!= SCNENC
)
185 * We are scanning a folder
189 app_msgarg(&msgs
, "all");
191 folder
= getfolder (1);
192 maildir
= m_maildir (folder
);
194 if (chdir (maildir
) == NOTOK
)
195 adios (maildir
, "unable to change directory to");
197 /* read folder and create message structure */
198 if (!(mp
= folder_read (folder
, 1)))
199 adios (NULL
, "unable to read folder %s", folder
);
201 /* check for empty folder */
203 adios (NULL
, "no messages in %s", folder
);
205 /* parse all the message ranges/sequences and set SELECTED */
206 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
207 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
209 seq_setprev (mp
); /* set the Previous-Sequence */
211 context_replace (pfolder
, folder
); /* update current folder */
212 seq_save (mp
); /* synchronize message sequences */
213 context_save (); /* save the context file */
216 * Get the sequence number for each sequence
217 * specified by Unseen-Sequence
219 if ((cp
= context_find (usequence
)) && *cp
) {
223 ap
= brkstring (dp
, " ", "\n");
224 for (i
= 0; ap
&& *ap
; i
++, ap
++)
225 ivector_push_back (seqnum
, seq_getnum (mp
, *ap
));
232 ontty
= isatty (fileno (stdout
));
234 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
235 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
236 msgnum
+= (revflag
? -1 : 1)) {
237 if (is_selected(mp
, msgnum
)) {
238 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
239 admonish (cp
, "unable to open message");
244 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
248 * Check if message is in any sequence given
249 * by Unseen-Sequence profile entry.
252 for (i
= 0; i
< num_unseen_seq
; i
++) {
253 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
259 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
260 msgnum
== mp
->curmsg
, unseen
,
268 adios (NULL
, "scan() botch (%d)", state
);
271 advise (NULL
, "message %d: empty", msgnum
);
282 ivector_free (seqnum
);
283 folder_free (mp
); /* free folder/message structure */