]>
diplodocus.org Git - nmh/blob - uip/scan.c
3 * scan.c -- display a one-line "scan" listing of folder or messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/fmt_scan.h>
14 #include <h/scansbr.h>
20 static struct swit switches
[] = {
26 { "form formatfile", 0 },
28 { "format string", 5 },
34 { "width columns", 0 },
50 * global for sbr/formatsbr.c - yech!
53 extern struct msgs
*fmt_current_folder
;
59 void clear_screen(void); /* from termsbr.c */
63 main (int argc
, char **argv
)
65 int clearflag
= 0, hdrflag
= 0, ontty
;
66 int width
= 0, revflag
= 0;
68 int seqnum
[NUMATTRS
], unseen
, num_unseen_seq
= 0;
69 char *cp
, *maildir
, *file
= NULL
, *folder
= NULL
;
70 char *form
= NULL
, *format
= NULL
, buf
[BUFSIZ
];
71 char **argp
, *nfs
, **arguments
;
72 struct msgs_array msgs
= { 0, 0, NULL
};
77 setlocale(LC_ALL
, "");
79 invo_name
= r1bindex (argv
[0], '/');
81 /* read user profile/context */
85 arguments
= getarguments (invo_name
, argc
, argv
, 1);
91 while ((cp
= *argp
++)) {
93 switch (smatch (++cp
, switches
)) {
95 ambigsw (cp
, switches
);
98 adios (NULL
, "-%s unknown", cp
);
101 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
103 print_help (buf
, switches
, 1);
106 print_version(invo_name
);
117 if (!(form
= *argp
++) || *form
== '-')
118 adios (NULL
, "missing argument to %s", argp
[-2]);
122 if (!(format
= *argp
++) || *format
== '-')
123 adios (NULL
, "missing argument to %s", argp
[-2]);
135 if (!(cp
= *argp
++) || *cp
== '-')
136 adios (NULL
, "missing argument to %s", argp
[-2]);
147 if (!(cp
= *argp
++) || (cp
[0] == '-' && cp
[1]))
148 adios (NULL
, "missing argument to %s", argp
[-2]);
149 if (strcmp (file
= cp
, "-"))
150 file
= path (cp
, TFILE
);
154 if (*cp
== '+' || *cp
== '@') {
156 adios (NULL
, "only one folder at a time!");
158 folder
= pluspath (cp
);
160 app_msgarg(&msgs
, cp
);
163 if (!context_find ("path"))
164 free (path ("./", TFOLDER
));
167 * Get new format string. Must be before chdir().
169 nfs
= new_fs (form
, format
, FORMAT
);
172 * We are scanning a maildrop file
176 adios (NULL
, "\"msgs\" not allowed with -file");
178 adios (NULL
, "\"+folder\" not allowed with -file");
180 /* check if "file" is really stdin */
181 if (strcmp (file
, "-") == 0) {
185 if ((in
= fopen (file
, "r")) == NULL
)
186 adios (file
, "unable to open");
191 printf ("FOLDER %s\t%s\n", file
, dtimenow (1));
196 for (msgnum
= 1; ; ++msgnum
) {
197 state
= scan (in
, msgnum
, -1, nfs
, width
, 0, 0,
198 hdrflag
? file
: NULL
, 0L, 1);
199 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
)))
221 adios (NULL
, "unable to read folder %s", folder
);
223 /* check for empty folder */
225 adios (NULL
, "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 seqnum
[i
] = seq_getnum (mp
, *ap
);
254 ontty
= isatty (fileno (stdout
));
258 fmt_current_folder
= mp
;
261 for (msgnum
= revflag
? mp
->hghsel
: mp
->lowsel
;
262 (revflag
? msgnum
>= mp
->lowsel
: msgnum
<= mp
->hghsel
);
263 msgnum
+= (revflag
? -1 : 1)) {
264 if (is_selected(mp
, msgnum
)) {
265 if ((in
= fopen (cp
= m_name (msgnum
), "r")) == NULL
) {
269 admonish (cp
, "unable to open message");
272 printf ("%*d unreadable\n", DMAXFOLDER
, msgnum
);
279 printf ("FOLDER %s\t%s\n", folder
, dtimenow(1));
284 * Check if message is in any sequence given
285 * by Unseen-Sequence profile entry.
288 for (i
= 0; i
< num_unseen_seq
; i
++) {
289 if (in_sequence(mp
, seqnum
[i
], msgnum
)) {
295 switch (state
= scan (in
, msgnum
, 0, nfs
, width
,
296 msgnum
== mp
->curmsg
, unseen
,
304 adios (NULL
, "scan() botch (%d)", state
);
308 printf ("%*d empty\n", DMAXFOLDER
, msgnum
);
310 advise (NULL
, "message %d: empty", msgnum
);
322 seq_save (mp
); /* because formatsbr might have made changes */
325 folder_free (mp
); /* free folder/message structure */