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