]> diplodocus.org Git - nmh/blob - uip/scan.c
Changed how build_nmh deals with older mhparam that doesn't
[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 #ifdef LOCALE
56 setlocale(LC_ALL, "");
57 #endif
58 invo_name = r1bindex (argv[0], '/');
59
60 /* read user profile/context */
61 context_read();
62
63 mts_init (invo_name);
64 arguments = getarguments (invo_name, argc, argv, 1);
65 argp = arguments;
66
67 /*
68 * Parse arguments
69 */
70 while ((cp = *argp++)) {
71 if (*cp == '-') {
72 switch (smatch (++cp, switches)) {
73 case AMBIGSW:
74 ambigsw (cp, switches);
75 done (1);
76 case UNKWNSW:
77 adios (NULL, "-%s unknown", cp);
78
79 case HELPSW:
80 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
81 invo_name);
82 print_help (buf, switches, 1);
83 done (0);
84 case VERSIONSW:
85 print_version(invo_name);
86 done (0);
87
88 case CLRSW:
89 clearflag++;
90 continue;
91 case NCLRSW:
92 clearflag = 0;
93 continue;
94
95 case FORMSW:
96 if (!(form = *argp++) || *form == '-')
97 adios (NULL, "missing argument to %s", argp[-2]);
98 format = NULL;
99 continue;
100 case FMTSW:
101 if (!(format = *argp++) || *format == '-')
102 adios (NULL, "missing argument to %s", argp[-2]);
103 form = NULL;
104 continue;
105
106 case HEADSW:
107 hdrflag++;
108 continue;
109 case NHEADSW:
110 hdrflag = 0;
111 continue;
112
113 case WIDTHSW:
114 if (!(cp = *argp++) || *cp == '-')
115 adios (NULL, "missing argument to %s", argp[-2]);
116 width = atoi (cp);
117 continue;
118 case REVSW:
119 revflag++;
120 continue;
121 case NREVSW:
122 revflag = 0;
123 continue;
124
125 case FILESW:
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);
130 continue;
131 }
132 }
133 if (*cp == '+' || *cp == '@') {
134 if (folder)
135 adios (NULL, "only one folder at a time!");
136 else
137 folder = pluspath (cp);
138 } else
139 app_msgarg(&msgs, cp);
140 }
141
142 if (!context_find ("path"))
143 free (path ("./", TFOLDER));
144
145 /*
146 * Get new format string. Must be before chdir().
147 */
148 nfs = new_fs (form, format, FORMAT);
149
150 /*
151 * We are scanning a maildrop file
152 */
153 if (file) {
154 if (msgs.size)
155 adios (NULL, "\"msgs\" not allowed with -file");
156 if (folder)
157 adios (NULL, "\"+folder\" not allowed with -file");
158
159 /* check if "file" is really stdin */
160 if (strcmp (file, "-") == 0) {
161 in = stdin;
162 file = "stdin";
163 } else {
164 if ((in = fopen (file, "r")) == NULL)
165 adios (file, "unable to open");
166 }
167
168 if (hdrflag) {
169 printf ("FOLDER %s\t%s\n", file, dtimenow (1));
170 }
171
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)
177 break;
178 }
179 scan_finished ();
180 fclose (in);
181 done (0);
182 }
183
184 /*
185 * We are scanning a folder
186 */
187
188 if (!msgs.size)
189 app_msgarg(&msgs, "all");
190 if (!folder)
191 folder = getfolder (1);
192 maildir = m_maildir (folder);
193
194 if (chdir (maildir) == NOTOK)
195 adios (maildir, "unable to change directory to");
196
197 /* read folder and create message structure */
198 if (!(mp = folder_read (folder, 1)))
199 adios (NULL, "unable to read folder %s", folder);
200
201 /* check for empty folder */
202 if (mp->nummsg == 0)
203 adios (NULL, "no messages in %s", folder);
204
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]))
208 done(1);
209 seq_setprev (mp); /* set the Previous-Sequence */
210
211 context_replace (pfolder, folder); /* update current folder */
212 seq_save (mp); /* synchronize message sequences */
213 context_save (); /* save the context file */
214
215 /*
216 * Get the sequence number for each sequence
217 * specified by Unseen-Sequence
218 */
219 if ((cp = context_find (usequence)) && *cp) {
220 char **ap, *dp;
221
222 dp = getcpy(cp);
223 ap = brkstring (dp, " ", "\n");
224 for (i = 0; ap && *ap; i++, ap++)
225 ivector_push_back (seqnum, seq_getnum (mp, *ap));
226
227 num_unseen_seq = i;
228 if (dp)
229 free(dp);
230 }
231
232 ontty = isatty (fileno (stdout));
233
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");
240 continue;
241 }
242
243 if (hdrflag) {
244 printf ("FOLDER %s\t%s\n", folder, dtimenow(1));
245 }
246
247 /*
248 * Check if message is in any sequence given
249 * by Unseen-Sequence profile entry.
250 */
251 unseen = 0;
252 for (i = 0; i < num_unseen_seq; i++) {
253 if (in_sequence(mp, ivector_at (seqnum, i), msgnum)) {
254 unseen = 1;
255 break;
256 }
257 }
258
259 switch (state = scan (in, msgnum, 0, nfs, width,
260 msgnum == mp->curmsg, unseen,
261 folder, 0L, 1)) {
262 case SCNMSG:
263 case SCNENC:
264 case SCNERR:
265 break;
266
267 default:
268 adios (NULL, "scan() botch (%d)", state);
269
270 case SCNEOF:
271 advise (NULL, "message %d: empty", msgnum);
272 break;
273 }
274 scan_finished ();
275 hdrflag = 0;
276 fclose (in);
277 if (ontty)
278 fflush (stdout);
279 }
280 }
281
282 ivector_free (seqnum);
283 folder_free (mp); /* free folder/message structure */
284 if (clearflag)
285 nmh_clear_screen ();
286
287 done (0);
288 return 1;
289 }