]> diplodocus.org Git - nmh/blob - uip/scan.c
add rmf(1) and folder(1) to one another's SEE ALSO sections
[nmh] / uip / scan.c
1
2 /*
3 * scan.c -- display a one-line "scan" listing of folder or messages
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <h/fmt_scan.h>
12 #include <h/scansbr.h>
13 #include <h/tws.h>
14 #include <h/mts.h>
15 #include <h/utils.h>
16
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) \
30
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(SCAN);
33 #undef X
34
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(SCAN, switches);
37 #undef X
38
39
40 int
41 main (int argc, char **argv)
42 {
43 int clearflag = 0, hdrflag = 0, ontty;
44 int width = 0, revflag = 0;
45 int i, state, msgnum;
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 };
52 struct msgs *mp;
53 FILE *in;
54
55 if (nmh_init(argv[0], 1)) { return 1; }
56
57 mts_init (invo_name);
58 arguments = getarguments (invo_name, argc, argv, 1);
59 argp = arguments;
60
61 /*
62 * Parse arguments
63 */
64 while ((cp = *argp++)) {
65 if (*cp == '-') {
66 switch (smatch (++cp, switches)) {
67 case AMBIGSW:
68 ambigsw (cp, switches);
69 done (1);
70 case UNKWNSW:
71 adios (NULL, "-%s unknown", cp);
72
73 case HELPSW:
74 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
75 invo_name);
76 print_help (buf, switches, 1);
77 done (0);
78 case VERSIONSW:
79 print_version(invo_name);
80 done (0);
81
82 case CLRSW:
83 clearflag++;
84 continue;
85 case NCLRSW:
86 clearflag = 0;
87 continue;
88
89 case FORMSW:
90 if (!(form = *argp++) || *form == '-')
91 adios (NULL, "missing argument to %s", argp[-2]);
92 format = NULL;
93 continue;
94 case FMTSW:
95 if (!(format = *argp++) || *format == '-')
96 adios (NULL, "missing argument to %s", argp[-2]);
97 form = NULL;
98 continue;
99
100 case HEADSW:
101 hdrflag++;
102 continue;
103 case NHEADSW:
104 hdrflag = 0;
105 continue;
106
107 case WIDTHSW:
108 if (!(cp = *argp++) || *cp == '-')
109 adios (NULL, "missing argument to %s", argp[-2]);
110 width = atoi (cp);
111 continue;
112 case REVSW:
113 revflag++;
114 continue;
115 case NREVSW:
116 revflag = 0;
117 continue;
118
119 case FILESW:
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);
124 continue;
125 }
126 }
127 if (*cp == '+' || *cp == '@') {
128 if (folder)
129 adios (NULL, "only one folder at a time!");
130 else
131 folder = pluspath (cp);
132 } else
133 app_msgarg(&msgs, cp);
134 }
135
136 if (!context_find ("path"))
137 free (path ("./", TFOLDER));
138
139 /*
140 * Get new format string. Must be before chdir().
141 */
142 nfs = new_fs (form, format, FORMAT);
143
144 /*
145 * We are scanning a maildrop file
146 */
147 if (file) {
148 if (msgs.size)
149 adios (NULL, "\"msgs\" not allowed with -file");
150 if (folder)
151 adios (NULL, "\"+folder\" not allowed with -file");
152
153 /* check if "file" is really stdin */
154 if (strcmp (file, "-") == 0) {
155 in = stdin;
156 file = "stdin";
157 } else {
158 if ((in = fopen (file, "r")) == NULL)
159 adios (file, "unable to open");
160 }
161
162 if (hdrflag) {
163 printf ("FOLDER %s\t%s\n", file, dtimenow (1));
164 }
165
166 scan_detect_mbox_style (in);
167 for (msgnum = 1; ; ++msgnum) {
168 state = scan (in, msgnum, -1, nfs, width, 0, 0,
169 hdrflag ? file : NULL, 0L, 1);
170 if (state != SCNMSG && state != SCNENC)
171 break;
172 }
173 scan_finished ();
174 fclose (in);
175 done (0);
176 }
177
178 /*
179 * We are scanning a folder
180 */
181
182 if (!msgs.size)
183 app_msgarg(&msgs, "all");
184 if (!folder)
185 folder = getfolder (1);
186 maildir = m_maildir (folder);
187
188 if (chdir (maildir) == NOTOK)
189 adios (maildir, "unable to change directory to");
190
191 /* read folder and create message structure */
192 if (!(mp = folder_read (folder, 1)))
193 adios (NULL, "unable to read folder %s", folder);
194
195 /* check for empty folder */
196 if (mp->nummsg == 0)
197 adios (NULL, "no messages in %s", folder);
198
199 /* parse all the message ranges/sequences and set SELECTED */
200 for (msgnum = 0; msgnum < msgs.size; msgnum++)
201 if (!m_convert (mp, msgs.msgs[msgnum]))
202 done(1);
203 seq_setprev (mp); /* set the Previous-Sequence */
204
205 context_replace (pfolder, folder); /* update current folder */
206 seq_save (mp); /* synchronize message sequences */
207 context_save (); /* save the context file */
208
209 /*
210 * Get the sequence number for each sequence
211 * specified by Unseen-Sequence
212 */
213 if ((cp = context_find (usequence)) && *cp) {
214 char **ap, *dp;
215
216 dp = getcpy(cp);
217 ap = brkstring (dp, " ", "\n");
218 for (i = 0; ap && *ap; i++, ap++)
219 ivector_push_back (seqnum, seq_getnum (mp, *ap));
220
221 num_unseen_seq = i;
222 if (dp)
223 free(dp);
224 }
225
226 ontty = isatty (fileno (stdout));
227
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 if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
233 admonish (cp, "unable to open message");
234 continue;
235 }
236
237 if (hdrflag) {
238 printf ("FOLDER %s\t%s\n", folder, dtimenow(1));
239 }
240
241 /*
242 * Check if message is in any sequence given
243 * by Unseen-Sequence profile entry.
244 */
245 unseen = 0;
246 for (i = 0; i < num_unseen_seq; i++) {
247 if (in_sequence(mp, ivector_at (seqnum, i), msgnum)) {
248 unseen = 1;
249 break;
250 }
251 }
252
253 switch (state = scan (in, msgnum, 0, nfs, width,
254 msgnum == mp->curmsg, unseen,
255 folder, 0L, 1)) {
256 case SCNMSG:
257 case SCNENC:
258 case SCNERR:
259 break;
260
261 default:
262 adios (NULL, "scan() botch (%d)", state);
263
264 case SCNEOF:
265 advise (NULL, "message %d: empty", msgnum);
266 break;
267 }
268 scan_finished ();
269 hdrflag = 0;
270 fclose (in);
271 if (ontty)
272 fflush (stdout);
273 }
274 }
275
276 ivector_free (seqnum);
277 folder_free (mp); /* free folder/message structure */
278 if (clearflag)
279 nmh_clear_screen ();
280
281 done (0);
282 return 1;
283 }