format.c revision 1.19 1 1.19 rillig /* $NetBSD: format.c,v 1.19 2024/10/06 19:31:26 rillig Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Anon Ymous.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.1 christos
32 1.1 christos #include <sys/cdefs.h>
33 1.1 christos #ifndef __lint__
34 1.19 rillig __RCSID("$NetBSD: format.c,v 1.19 2024/10/06 19:31:26 rillig Exp $");
35 1.1 christos #endif /* not __lint__ */
36 1.1 christos
37 1.1 christos #include <time.h>
38 1.1 christos #include <stdio.h>
39 1.1 christos #include <util.h>
40 1.1 christos
41 1.1 christos #include "def.h"
42 1.1 christos #include "extern.h"
43 1.1 christos #include "format.h"
44 1.1 christos #include "glob.h"
45 1.2 christos #include "thread.h"
46 1.1 christos
47 1.11 christos #undef DEBUG
48 1.11 christos #ifdef DEBUG
49 1.11 christos #define DPRINTF(a) printf a
50 1.11 christos #else
51 1.11 christos #define DPRINTF(a)
52 1.11 christos #endif
53 1.1 christos
54 1.1 christos static void
55 1.1 christos check_bufsize(char **buf, size_t *bufsize, char **p, size_t cnt)
56 1.1 christos {
57 1.18 shm size_t offset = (size_t)(*p - *buf);
58 1.18 shm
59 1.18 shm /* enough buffer allocated already */
60 1.18 shm if (cnt < *bufsize - offset)
61 1.1 christos return;
62 1.18 shm
63 1.18 shm /* expand buffer till it's sufficient to handle the data */
64 1.18 shm while (cnt >= *bufsize - offset) {
65 1.18 shm if (*bufsize > SIZE_MAX/2)
66 1.18 shm errx(1, "out of memory");
67 1.18 shm *bufsize *= 2;
68 1.18 shm }
69 1.18 shm
70 1.18 shm *buf = erealloc(*buf, *bufsize);
71 1.18 shm *p = *buf + offset;
72 1.1 christos }
73 1.1 christos
74 1.1 christos static const char *
75 1.1 christos sfmtoff(const char **fmtbeg, const char *fmtch, off_t off)
76 1.1 christos {
77 1.1 christos char *newfmt; /* pointer to new format string */
78 1.1 christos size_t len; /* space for "lld" including '\0' */
79 1.2 christos char *p;
80 1.2 christos
81 1.1 christos len = fmtch - *fmtbeg + sizeof(PRId64);
82 1.1 christos newfmt = salloc(len);
83 1.12 christos (void)strlcpy(newfmt, *fmtbeg, len - sizeof(PRId64) + 1);
84 1.1 christos (void)strlcat(newfmt, PRId64, len);
85 1.1 christos *fmtbeg = fmtch + 1;
86 1.2 christos (void)sasprintf(&p, newfmt, off);
87 1.2 christos return p;
88 1.1 christos }
89 1.1 christos
90 1.1 christos static const char *
91 1.1 christos sfmtint(const char **fmtbeg, const char *fmtch, int num)
92 1.1 christos {
93 1.1 christos char *newfmt;
94 1.1 christos size_t len;
95 1.2 christos char *p;
96 1.1 christos
97 1.1 christos len = fmtch - *fmtbeg + 2; /* space for 'd' and '\0' */
98 1.1 christos newfmt = salloc(len);
99 1.1 christos (void)strlcpy(newfmt, *fmtbeg, len);
100 1.1 christos newfmt[len-2] = 'd'; /* convert to printf format */
101 1.1 christos *fmtbeg = fmtch + 1;
102 1.2 christos (void)sasprintf(&p, newfmt, num);
103 1.2 christos return p;
104 1.1 christos }
105 1.1 christos
106 1.1 christos static const char *
107 1.1 christos sfmtstr(const char **fmtbeg, const char *fmtch, const char *str)
108 1.1 christos {
109 1.1 christos char *newfmt;
110 1.1 christos size_t len;
111 1.2 christos char *p;
112 1.1 christos
113 1.1 christos len = fmtch - *fmtbeg + 2; /* space for 's' and '\0' */
114 1.1 christos newfmt = salloc(len);
115 1.1 christos (void)strlcpy(newfmt, *fmtbeg, len);
116 1.1 christos newfmt[len-2] = 's'; /* convert to printf format */
117 1.1 christos *fmtbeg = fmtch + 1;
118 1.2 christos (void)sasprintf(&p, newfmt, str ? str : "");
119 1.2 christos return p;
120 1.2 christos }
121 1.2 christos
122 1.2 christos #ifdef THREAD_SUPPORT
123 1.2 christos static char*
124 1.2 christos sfmtdepth(char *str, int depth)
125 1.2 christos {
126 1.2 christos char *p;
127 1.2 christos if (*str == '\0') {
128 1.2 christos (void)sasprintf(&p, "%d", depth);
129 1.2 christos return p;
130 1.2 christos }
131 1.2 christos p = __UNCONST("");
132 1.2 christos for (/*EMPTY*/; depth > 0; depth--)
133 1.2 christos (void)sasprintf(&p, "%s%s", p, str);
134 1.2 christos return p;
135 1.1 christos }
136 1.2 christos #endif
137 1.1 christos
138 1.1 christos static const char *
139 1.1 christos sfmtfield(const char **fmtbeg, const char *fmtch, struct message *mp)
140 1.1 christos {
141 1.19 rillig const char *q;
142 1.1 christos q = strchr(fmtch + 1, '?');
143 1.1 christos if (q) {
144 1.1 christos size_t len;
145 1.1 christos char *p;
146 1.1 christos const char *str;
147 1.1 christos int skin_it;
148 1.2 christos #ifdef THREAD_SUPPORT
149 1.2 christos int depth;
150 1.2 christos #endif
151 1.2 christos if (mp == NULL) {
152 1.2 christos *fmtbeg = q + 1;
153 1.2 christos return NULL;
154 1.2 christos }
155 1.2 christos #ifdef THREAD_SUPPORT
156 1.2 christos depth = mp->m_depth;
157 1.2 christos #endif
158 1.2 christos skin_it = 0;
159 1.2 christos switch (fmtch[1]) { /* check the '?' modifier */
160 1.2 christos #ifdef THREAD_SUPPORT
161 1.2 christos case '&': /* use the relative depth */
162 1.2 christos depth -= thread_depth();
163 1.2 christos /* FALLTHROUGH */
164 1.2 christos case '*': /* use the absolute depth */
165 1.2 christos len = q - fmtch - 1;
166 1.2 christos p = salloc(len);
167 1.2 christos (void)strlcpy(p, fmtch + 2, len);
168 1.2 christos p = sfmtdepth(p, depth);
169 1.2 christos break;
170 1.11 christos #endif
171 1.2 christos case '-':
172 1.2 christos skin_it = 1;
173 1.2 christos /* FALLTHROUGH */
174 1.2 christos default:
175 1.2 christos len = q - fmtch - skin_it;
176 1.2 christos p = salloc(len);
177 1.2 christos (void)strlcpy(p, fmtch + skin_it + 1, len);
178 1.2 christos p = hfield(p, mp);
179 1.2 christos if (skin_it)
180 1.2 christos p = skin(p);
181 1.2 christos break;
182 1.2 christos }
183 1.2 christos str = sfmtstr(fmtbeg, fmtch, p);
184 1.1 christos *fmtbeg = q + 1;
185 1.1 christos return str;
186 1.1 christos }
187 1.1 christos return NULL;
188 1.1 christos }
189 1.1 christos
190 1.2 christos struct flags_s {
191 1.2 christos int f_and;
192 1.2 christos int f_or;
193 1.2 christos int f_new; /* some message in the thread is new */
194 1.2 christos int f_unread; /* some message in the thread is unread */
195 1.2 christos };
196 1.2 christos
197 1.2 christos static void
198 1.2 christos get_and_or_flags(struct message *mp, struct flags_s *flags)
199 1.2 christos {
200 1.2 christos for (/*EMPTY*/; mp; mp = mp->m_flink) {
201 1.2 christos flags->f_and &= mp->m_flag;
202 1.2 christos flags->f_or |= mp->m_flag;
203 1.2 christos flags->f_new |= (mp->m_flag & (MREAD|MNEW)) == MNEW;
204 1.2 christos flags->f_unread |= (mp->m_flag & (MREAD|MNEW)) == 0;
205 1.2 christos get_and_or_flags(mp->m_clink, flags);
206 1.2 christos }
207 1.2 christos }
208 1.2 christos
209 1.1 christos static const char *
210 1.2 christos sfmtflag(const char **fmtbeg, const char *fmtch, struct message *mp)
211 1.1 christos {
212 1.1 christos char disp[2];
213 1.2 christos struct flags_s flags;
214 1.2 christos int is_thread;
215 1.2 christos
216 1.2 christos if (mp == NULL)
217 1.2 christos return NULL;
218 1.11 christos
219 1.2 christos is_thread = mp->m_clink != NULL;
220 1.2 christos disp[0] = is_thread ? '+' : ' ';
221 1.1 christos disp[1] = '\0';
222 1.2 christos
223 1.2 christos flags.f_and = mp->m_flag;
224 1.2 christos flags.f_or = mp->m_flag;
225 1.2 christos flags.f_new = 0;
226 1.2 christos flags.f_unread = 0;
227 1.2 christos #ifdef THREAD_SUPPORT
228 1.2 christos if (thread_hidden())
229 1.2 christos get_and_or_flags(mp->m_clink, &flags);
230 1.2 christos #endif
231 1.2 christos
232 1.2 christos if (flags.f_or & MTAGGED)
233 1.2 christos disp[0] = 't';
234 1.2 christos if (flags.f_and & MTAGGED)
235 1.2 christos disp[0] = 'T';
236 1.2 christos
237 1.2 christos if (flags.f_or & MMODIFY)
238 1.2 christos disp[0] = 'e';
239 1.2 christos if (flags.f_and & MMODIFY)
240 1.2 christos disp[0] = 'E';
241 1.2 christos
242 1.2 christos if (flags.f_or & MSAVED)
243 1.2 christos disp[0] = '&';
244 1.2 christos if (flags.f_and & MSAVED)
245 1.1 christos disp[0] = '*';
246 1.2 christos
247 1.2 christos if (flags.f_or & MPRESERVE)
248 1.2 christos disp[0] = 'p';
249 1.2 christos if (flags.f_and & MPRESERVE)
250 1.1 christos disp[0] = 'P';
251 1.2 christos
252 1.2 christos if (flags.f_unread)
253 1.2 christos disp[0] = 'u';
254 1.2 christos if ((flags.f_or & (MREAD|MNEW)) == 0)
255 1.2 christos disp[0] = 'U';
256 1.2 christos
257 1.2 christos if (flags.f_new)
258 1.2 christos disp[0] = 'n';
259 1.2 christos if ((flags.f_and & (MREAD|MNEW)) == MNEW)
260 1.1 christos disp[0] = 'N';
261 1.2 christos
262 1.2 christos if (flags.f_or & MBOX)
263 1.2 christos disp[0] = 'm';
264 1.2 christos if (flags.f_and & MBOX)
265 1.1 christos disp[0] = 'M';
266 1.2 christos
267 1.1 christos return sfmtstr(fmtbeg, fmtch, disp);
268 1.1 christos }
269 1.1 christos
270 1.1 christos static const char *
271 1.1 christos login_name(const char *addr)
272 1.1 christos {
273 1.19 rillig const char *p;
274 1.1 christos p = strchr(addr, '@');
275 1.1 christos if (p) {
276 1.1 christos char *q;
277 1.1 christos size_t len;
278 1.1 christos len = p - addr + 1;
279 1.1 christos q = salloc(len);
280 1.1 christos (void)strlcpy(q, addr, len);
281 1.1 christos return q;
282 1.1 christos }
283 1.1 christos return addr;
284 1.1 christos }
285 1.1 christos
286 1.2 christos /*
287 1.2 christos * A simple routine to get around a lint warning.
288 1.2 christos */
289 1.2 christos static inline const char *
290 1.2 christos skip_fmt(const char **src, const char *p)
291 1.2 christos {
292 1.2 christos *src = p;
293 1.2 christos return NULL;
294 1.2 christos }
295 1.2 christos
296 1.1 christos static const char *
297 1.1 christos subformat(const char **src, struct message *mp, const char *addr,
298 1.11 christos const char *user, const char *subj, int tm_isdst)
299 1.1 christos {
300 1.2 christos #if 0
301 1.2 christos /* XXX - lint doesn't like this, hence skip_fmt(). */
302 1.2 christos #define MP(a) mp ? a : (*src = (p + 1), NULL)
303 1.2 christos #else
304 1.2 christos #define MP(a) mp ? a : skip_fmt(src, p + 1);
305 1.2 christos #endif
306 1.1 christos const char *p;
307 1.1 christos
308 1.1 christos p = *src;
309 1.1 christos if (p[1] == '%') {
310 1.1 christos *src += 2;
311 1.1 christos return "%%";
312 1.1 christos }
313 1.1 christos for (p = *src; *p && !isalpha((unsigned char)*p) && *p != '?'; p++)
314 1.1 christos continue;
315 1.1 christos
316 1.1 christos switch (*p) {
317 1.11 christos /*
318 1.11 christos * Our format extensions to strftime(3)
319 1.11 christos */
320 1.1 christos case '?':
321 1.2 christos return sfmtfield(src, p, mp);
322 1.1 christos case 'J':
323 1.2 christos return MP(sfmtint(src, p, (int)(mp->m_lines - mp->m_blines)));
324 1.1 christos case 'K':
325 1.1 christos return MP(sfmtint(src, p, (int)mp->m_blines));
326 1.1 christos case 'L':
327 1.1 christos return MP(sfmtint(src, p, (int)mp->m_lines));
328 1.1 christos case 'N':
329 1.1 christos return sfmtstr(src, p, user);
330 1.1 christos case 'O':
331 1.1 christos return MP(sfmtoff(src, p, mp->m_size));
332 1.1 christos case 'P':
333 1.1 christos return MP(sfmtstr(src, p, mp == dot ? ">" : " "));
334 1.1 christos case 'Q':
335 1.2 christos return MP(sfmtflag(src, p, mp));
336 1.1 christos case 'f':
337 1.1 christos return sfmtstr(src, p, addr);
338 1.1 christos case 'i':
339 1.2 christos return sfmtint(src, p, get_msgnum(mp)); /* '0' if mp == NULL */
340 1.1 christos case 'n':
341 1.1 christos return sfmtstr(src, p, login_name(addr));
342 1.1 christos case 'q':
343 1.1 christos return sfmtstr(src, p, subj);
344 1.1 christos case 't':
345 1.2 christos return sfmtint(src, p, get_msgCount());
346 1.11 christos
347 1.11 christos /*
348 1.11 christos * strftime(3) special cases:
349 1.11 christos *
350 1.11 christos * When 'tm_isdst' was not determined (i.e., < 0), a C99
351 1.11 christos * compliant strftime(3) will output an empty string for the
352 1.11 christos * "%Z" and "%z" formats. This messes up alignment so we
353 1.11 christos * handle these ourselves.
354 1.11 christos */
355 1.11 christos case 'Z':
356 1.11 christos if (tm_isdst < 0) {
357 1.11 christos *src = p + 1;
358 1.11 christos return "???"; /* XXX - not ideal */
359 1.11 christos }
360 1.11 christos return NULL;
361 1.1 christos case 'z':
362 1.11 christos if (tm_isdst < 0) {
363 1.11 christos *src = p + 1;
364 1.11 christos return "-0000"; /* consistent with RFC 2822 */
365 1.11 christos }
366 1.11 christos return NULL;
367 1.11 christos
368 1.11 christos /* everything else is handled by strftime(3) */
369 1.1 christos default:
370 1.1 christos return NULL;
371 1.1 christos }
372 1.1 christos #undef MP
373 1.1 christos }
374 1.1 christos
375 1.1 christos static const char *
376 1.1 christos snarf_comment(char **buf, char *bufend, const char *string)
377 1.1 christos {
378 1.1 christos const char *p;
379 1.1 christos char *q;
380 1.1 christos char *qend;
381 1.1 christos int clevel;
382 1.1 christos
383 1.1 christos q = buf ? *buf : NULL;
384 1.1 christos qend = buf ? bufend : NULL;
385 1.1 christos
386 1.1 christos clevel = 1;
387 1.1 christos for (p = string + 1; *p != '\0'; p++) {
388 1.11 christos DPRINTF(("snarf_comment: %s\n", p));
389 1.1 christos if (*p == '(') {
390 1.1 christos clevel++;
391 1.1 christos continue;
392 1.1 christos }
393 1.1 christos if (*p == ')') {
394 1.1 christos if (--clevel == 0)
395 1.1 christos break;
396 1.1 christos continue;
397 1.1 christos }
398 1.1 christos if (*p == '\\' && p[1] != 0)
399 1.1 christos p++;
400 1.1 christos
401 1.1 christos if (q < qend)
402 1.1 christos *q++ = *p;
403 1.1 christos }
404 1.1 christos if (buf) {
405 1.1 christos *q = '\0';
406 1.11 christos DPRINTF(("snarf_comment: terminating: %s\n", *buf));
407 1.1 christos *buf = q;
408 1.1 christos }
409 1.1 christos if (*p == '\0')
410 1.1 christos p--;
411 1.1 christos return p;
412 1.1 christos }
413 1.1 christos
414 1.1 christos static const char *
415 1.1 christos snarf_quote(char **buf, char *bufend, const char *string)
416 1.1 christos {
417 1.1 christos const char *p;
418 1.1 christos char *q;
419 1.1 christos char *qend;
420 1.1 christos
421 1.1 christos q = buf ? *buf : NULL;
422 1.1 christos qend = buf ? bufend : NULL;
423 1.1 christos
424 1.1 christos for (p = string + 1; *p != '\0' && *p != '"'; p++) {
425 1.11 christos DPRINTF(("snarf_quote: %s\n", p));
426 1.1 christos if (*p == '\\' && p[1] != '\0')
427 1.1 christos p++;
428 1.1 christos
429 1.1 christos if (q < qend)
430 1.1 christos *q++ = *p;
431 1.1 christos }
432 1.1 christos if (buf) {
433 1.1 christos *q = '\0';
434 1.11 christos DPRINTF(("snarf_quote: terminating: %s\n", *buf));
435 1.1 christos *buf = q;
436 1.1 christos }
437 1.1 christos if (*p == '\0')
438 1.1 christos p--;
439 1.1 christos return p;
440 1.1 christos }
441 1.1 christos
442 1.1 christos /*
443 1.1 christos * Grab the comments, separating each by a space.
444 1.1 christos */
445 1.1 christos static char *
446 1.1 christos get_comments(char *name)
447 1.1 christos {
448 1.2 christos char nbuf[LINESIZE];
449 1.1 christos const char *p;
450 1.1 christos char *qend;
451 1.1 christos char *q;
452 1.1 christos char *lastq;
453 1.1 christos
454 1.1 christos if (name == NULL)
455 1.2 christos return NULL;
456 1.1 christos
457 1.1 christos p = name;
458 1.1 christos q = nbuf;
459 1.1 christos lastq = nbuf;
460 1.1 christos qend = nbuf + sizeof(nbuf) - 1;
461 1.11 christos for (p = skip_WSP(name); *p != '\0'; p++) {
462 1.11 christos DPRINTF(("get_comments: %s\n", p));
463 1.1 christos switch (*p) {
464 1.1 christos case '"': /* quoted-string ... skip it! */
465 1.1 christos p = snarf_quote(NULL, NULL, p);
466 1.1 christos break;
467 1.1 christos
468 1.1 christos case '(':
469 1.1 christos p = snarf_comment(&q, qend, p);
470 1.1 christos lastq = q;
471 1.1 christos if (q < qend) /* separate comments by space */
472 1.11 christos *q++ = ' ';
473 1.1 christos break;
474 1.1 christos
475 1.1 christos default:
476 1.1 christos break;
477 1.1 christos }
478 1.1 christos }
479 1.1 christos *lastq = '\0';
480 1.1 christos return savestr(nbuf);
481 1.1 christos }
482 1.1 christos
483 1.8 christos /*
484 1.8 christos * Convert a possible obs_zone (see RFC 2822, sec 4.3) to a valid
485 1.8 christos * gmtoff string.
486 1.8 christos */
487 1.8 christos static const char *
488 1.8 christos convert_obs_zone(const char *obs_zone)
489 1.8 christos {
490 1.8 christos static const struct obs_zone_tbl_s {
491 1.8 christos const char *zone;
492 1.8 christos const char *gmtoff;
493 1.8 christos } obs_zone_tbl[] = {
494 1.8 christos {"UT", "+0000"},
495 1.8 christos {"GMT", "+0000"},
496 1.8 christos {"EST", "-0500"},
497 1.8 christos {"EDT", "-0400"},
498 1.8 christos {"CST", "-0600"},
499 1.8 christos {"CDT", "-0500"},
500 1.8 christos {"MST", "-0700"},
501 1.8 christos {"MDT", "-0600"},
502 1.8 christos {"PST", "-0800"},
503 1.8 christos {"PDT", "-0700"},
504 1.8 christos {NULL, NULL},
505 1.8 christos };
506 1.8 christos const struct obs_zone_tbl_s *zp;
507 1.8 christos
508 1.8 christos if (obs_zone[0] == '+' || obs_zone[0] == '-')
509 1.8 christos return obs_zone;
510 1.8 christos
511 1.8 christos if (obs_zone[1] == 0) { /* possible military zones */
512 1.11 christos /* be explicit here - avoid C extensions and ctype(3) */
513 1.8 christos switch((unsigned char)obs_zone[0]) {
514 1.11 christos case 'A': case 'B': case 'C': case 'D': case 'E':
515 1.11 christos case 'F': case 'G': case 'H': case 'I':
516 1.11 christos case 'K': case 'L': case 'M': case 'N': case 'O':
517 1.11 christos case 'P': case 'Q': case 'R': case 'S': case 'T':
518 1.11 christos case 'U': case 'V': case 'W': case 'X': case 'Y':
519 1.11 christos case 'Z':
520 1.11 christos case 'a': case 'b': case 'c': case 'd': case 'e':
521 1.11 christos case 'f': case 'g': case 'h': case 'i':
522 1.11 christos case 'k': case 'l': case 'm': case 'n': case 'o':
523 1.11 christos case 'p': case 'q': case 'r': case 's': case 't':
524 1.11 christos case 'u': case 'v': case 'w': case 'x': case 'y':
525 1.11 christos case 'z':
526 1.11 christos return "-0000"; /* See RFC 2822, sec 4.3 */
527 1.8 christos default:
528 1.8 christos return obs_zone;
529 1.8 christos }
530 1.8 christos }
531 1.11 christos for (zp = obs_zone_tbl; zp->zone; zp++) {
532 1.8 christos if (strcmp(obs_zone, zp->zone) == 0)
533 1.8 christos return zp->gmtoff;
534 1.8 christos }
535 1.8 christos return obs_zone;
536 1.8 christos }
537 1.8 christos
538 1.1 christos /*
539 1.11 christos * Parse the 'Date:" field into a tm structure and return the gmtoff
540 1.11 christos * string or NULL on error.
541 1.11 christos */
542 1.11 christos static const char *
543 1.11 christos date_to_tm(char *date, struct tm *tm)
544 1.11 christos {
545 1.11 christos /****************************************************************
546 1.11 christos * The header 'date-time' syntax - From RFC 2822 sec 3.3 and 4.3:
547 1.11 christos *
548 1.11 christos * date-time = [ day-of-week "," ] date FWS time [CFWS]
549 1.11 christos * day-of-week = ([FWS] day-name) / obs-day-of-week
550 1.11 christos * day-name = "Mon" / "Tue" / "Wed" / "Thu" /
551 1.11 christos * "Fri" / "Sat" / "Sun"
552 1.11 christos * date = day month year
553 1.11 christos * year = 4*DIGIT / obs-year
554 1.11 christos * month = (FWS month-name FWS) / obs-month
555 1.11 christos * month-name = "Jan" / "Feb" / "Mar" / "Apr" /
556 1.11 christos * "May" / "Jun" / "Jul" / "Aug" /
557 1.11 christos * "Sep" / "Oct" / "Nov" / "Dec"
558 1.11 christos * day = ([FWS] 1*2DIGIT) / obs-day
559 1.11 christos * time = time-of-day FWS zone
560 1.11 christos * time-of-day = hour ":" minute [ ":" second ]
561 1.11 christos * hour = 2DIGIT / obs-hour
562 1.11 christos * minute = 2DIGIT / obs-minute
563 1.11 christos * second = 2DIGIT / obs-second
564 1.11 christos * zone = (( "+" / "-" ) 4DIGIT) / obs-zone
565 1.11 christos *
566 1.11 christos * obs-day-of-week = [CFWS] day-name [CFWS]
567 1.11 christos * obs-year = [CFWS] 2*DIGIT [CFWS]
568 1.11 christos * obs-month = CFWS month-name CFWS
569 1.11 christos * obs-day = [CFWS] 1*2DIGIT [CFWS]
570 1.11 christos * obs-hour = [CFWS] 2DIGIT [CFWS]
571 1.11 christos * obs-minute = [CFWS] 2DIGIT [CFWS]
572 1.11 christos * obs-second = [CFWS] 2DIGIT [CFWS]
573 1.11 christos ****************************************************************/
574 1.11 christos /*
575 1.11 christos * For example, a typical date might look like:
576 1.11 christos *
577 1.11 christos * Date: Mon, 1 Oct 2007 05:38:10 +0000 (UTC)
578 1.11 christos */
579 1.11 christos char *tail;
580 1.11 christos char *p;
581 1.11 christos struct tm tmp_tm;
582 1.11 christos /*
583 1.11 christos * NOTE: Rather than depend on strptime(3) modifying only
584 1.11 christos * those fields specified in its format string, we use tmp_tm
585 1.11 christos * and copy the appropriate result to tm. This is not
586 1.11 christos * required with the NetBSD strptime(3) implementation.
587 1.11 christos */
588 1.11 christos
589 1.11 christos /* Check for an optional 'day-of-week' */
590 1.16 christos if ((tail = strptime(date, " %a,", &tmp_tm)) == NULL)
591 1.11 christos tail = date;
592 1.16 christos else
593 1.11 christos tm->tm_wday = tmp_tm.tm_wday;
594 1.11 christos
595 1.11 christos /* Get the required 'day' and 'month' */
596 1.11 christos if ((tail = strptime(tail, " %d %b", &tmp_tm)) == NULL)
597 1.11 christos return NULL;
598 1.11 christos
599 1.11 christos tm->tm_mday = tmp_tm.tm_mday;
600 1.11 christos tm->tm_mon = tmp_tm.tm_mon;
601 1.11 christos
602 1.11 christos /* Check for 'obs-year' (2 digits) before 'year' (4 digits) */
603 1.11 christos /* XXX - Portable? This depends on strptime not scanning off
604 1.11 christos * trailing whitespace unless specified in the format string.
605 1.11 christos */
606 1.11 christos if ((p = strptime(tail, " %y", &tmp_tm)) != NULL && is_WSP(*p))
607 1.11 christos tail = p;
608 1.11 christos else if ((tail = strptime(tail, " %Y", &tmp_tm)) == NULL)
609 1.11 christos return NULL;
610 1.11 christos
611 1.11 christos tm->tm_year = tmp_tm.tm_year;
612 1.11 christos
613 1.11 christos /* Get the required 'hour' and 'minute' */
614 1.11 christos if ((tail = strptime(tail, " %H:%M", &tmp_tm)) == NULL)
615 1.11 christos return NULL;
616 1.11 christos
617 1.11 christos tm->tm_hour = tmp_tm.tm_hour;
618 1.11 christos tm->tm_min = tmp_tm.tm_min;
619 1.11 christos
620 1.11 christos /* Check for an optional 'seconds' field */
621 1.11 christos if ((p = strptime(tail, ":%S", &tmp_tm)) != NULL) {
622 1.11 christos tail = p;
623 1.11 christos tm->tm_sec = tmp_tm.tm_sec;
624 1.11 christos }
625 1.11 christos
626 1.11 christos tail = skip_WSP(tail);
627 1.11 christos
628 1.11 christos /*
629 1.11 christos * The timezone name is frequently in a comment following the
630 1.11 christos * zone offset.
631 1.11 christos *
632 1.11 christos * XXX - this will get overwritten later by timegm(3).
633 1.11 christos */
634 1.11 christos if ((p = strchr(tail, '(')) != NULL)
635 1.11 christos tm->tm_zone = get_comments(p);
636 1.11 christos else
637 1.11 christos tm->tm_zone = NULL;
638 1.11 christos
639 1.11 christos /* what remains should be the gmtoff string */
640 1.11 christos tail = skin(tail);
641 1.11 christos return convert_obs_zone(tail);
642 1.11 christos }
643 1.11 christos
644 1.11 christos /*
645 1.11 christos * Parse the headline string into a tm structure. Returns a pointer
646 1.11 christos * to first non-whitespace after the date or NULL on error.
647 1.11 christos *
648 1.11 christos * XXX - This needs to be consistent with isdate().
649 1.11 christos */
650 1.11 christos static char *
651 1.11 christos hl_date_to_tm(const char *buf, struct tm *tm)
652 1.11 christos {
653 1.11 christos /****************************************************************
654 1.11 christos * The BSD and System V headline date formats differ
655 1.11 christos * and each have an optional timezone field between
656 1.11 christos * the time and date (see head.c). Unfortunately,
657 1.11 christos * strptime(3) doesn't know about timezone fields, so
658 1.11 christos * we have to handle it ourselves.
659 1.11 christos *
660 1.11 christos * char ctype[] = "Aaa Aaa O0 00:00:00 0000";
661 1.11 christos * char tmztype[] = "Aaa Aaa O0 00:00:00 AAA 0000";
662 1.11 christos * char SysV_ctype[] = "Aaa Aaa O0 00:00 0000";
663 1.11 christos * char SysV_tmztype[] = "Aaa Aaa O0 00:00 AAA 0000";
664 1.11 christos ****************************************************************/
665 1.11 christos char *tail;
666 1.11 christos char *p;
667 1.11 christos char zone[4];
668 1.11 christos struct tm tmp_tm; /* see comment in date_to_tm() */
669 1.11 christos int len;
670 1.11 christos
671 1.11 christos zone[0] = '\0';
672 1.11 christos if ((tail = strptime(buf, " %a %b %d %H:%M", &tmp_tm)) == NULL)
673 1.11 christos return NULL;
674 1.11 christos
675 1.11 christos tm->tm_wday = tmp_tm.tm_wday;
676 1.11 christos tm->tm_mday = tmp_tm.tm_mday;
677 1.11 christos tm->tm_mon = tmp_tm.tm_mon;
678 1.11 christos tm->tm_hour = tmp_tm.tm_hour;
679 1.11 christos tm->tm_min = tmp_tm.tm_min;
680 1.11 christos
681 1.11 christos /* Check for an optional 'seconds' field */
682 1.11 christos if ((p = strptime(tail, ":%S", &tmp_tm)) != NULL) {
683 1.11 christos tail = p;
684 1.11 christos tm->tm_sec = tmp_tm.tm_sec;
685 1.11 christos }
686 1.11 christos
687 1.11 christos /* Grab an optional timezone name */
688 1.11 christos /*
689 1.11 christos * XXX - Is the zone name always 3 characters as in isdate()?
690 1.11 christos */
691 1.11 christos if (sscanf(tail, " %3[A-Z] %n", zone, &len) == 1) {
692 1.11 christos if (zone[0])
693 1.11 christos tm->tm_zone = savestr(zone);
694 1.11 christos tail += len;
695 1.11 christos }
696 1.11 christos
697 1.11 christos /* Grab the required year field */
698 1.11 christos tail = strptime(tail, " %Y ", &tmp_tm);
699 1.11 christos tm->tm_year = tmp_tm.tm_year; /* save this even if it failed */
700 1.11 christos
701 1.11 christos return tail;
702 1.11 christos }
703 1.11 christos
704 1.11 christos /*
705 1.1 christos * Get the date and time info from the "Date:" line, parse it into a
706 1.1 christos * tm structure as much as possible.
707 1.1 christos *
708 1.1 christos * Note: We return the gmtoff as a string as "-0000" has special
709 1.1 christos * meaning. See RFC 2822, sec 3.3.
710 1.1 christos */
711 1.11 christos PUBLIC void
712 1.1 christos dateof(struct tm *tm, struct message *mp, int use_hl_date)
713 1.1 christos {
714 1.3 christos static int tzinit = 0;
715 1.11 christos char *date = NULL;
716 1.8 christos const char *gmtoff;
717 1.1 christos
718 1.1 christos (void)memset(tm, 0, sizeof(*tm));
719 1.1 christos
720 1.3 christos /* Make sure the time zone info is initialized. */
721 1.3 christos if (!tzinit) {
722 1.3 christos tzinit = 1;
723 1.3 christos tzset();
724 1.3 christos }
725 1.1 christos if (mp == NULL) { /* use local time */
726 1.1 christos time_t now;
727 1.1 christos (void)time(&now);
728 1.1 christos (void)localtime_r(&now, tm);
729 1.11 christos return;
730 1.1 christos }
731 1.11 christos
732 1.1 christos /*
733 1.1 christos * See RFC 2822 sec 3.3 for date-time format used in
734 1.1 christos * the "Date:" field.
735 1.1 christos *
736 1.11 christos * NOTE: The range for the time is 00:00 to 23:60 (to allow
737 1.17 andvar * for a leap second), but I have seen this violated making
738 1.11 christos * strptime() fail, e.g.,
739 1.1 christos *
740 1.11 christos * Date: Tue, 24 Oct 2006 24:07:58 +0400
741 1.1 christos *
742 1.11 christos * In this case we (silently) fall back to the headline time
743 1.11 christos * which was written locally when the message was received.
744 1.11 christos * Of course, this is not the same time as in the Date field.
745 1.1 christos */
746 1.1 christos if (use_hl_date == 0 &&
747 1.1 christos (date = hfield("date", mp)) != NULL &&
748 1.11 christos (gmtoff = date_to_tm(date, tm)) != NULL) {
749 1.2 christos int hour;
750 1.2 christos int min;
751 1.4 christos char sign[2];
752 1.11 christos struct tm save_tm;
753 1.2 christos
754 1.2 christos /*
755 1.2 christos * Scan the gmtoff and use it to convert the time to a
756 1.2 christos * local time.
757 1.2 christos *
758 1.3 christos * Note: "-0000" means no valid zone info. See
759 1.3 christos * RFC 2822, sec 3.3.
760 1.3 christos *
761 1.2 christos * XXX - This is painful! Is there a better way?
762 1.2 christos */
763 1.11 christos
764 1.11 christos tm->tm_isdst = -1; /* let timegm(3) determine tm_isdst */
765 1.11 christos save_tm = *tm; /* use this if we fail */
766 1.11 christos
767 1.3 christos if (strcmp(gmtoff, "-0000") != 0 &&
768 1.4 christos sscanf(gmtoff, " %1[+-]%2d%2d ", sign, &hour, &min) == 3) {
769 1.2 christos time_t otime;
770 1.11 christos
771 1.4 christos if (sign[0] == '-') {
772 1.2 christos tm->tm_hour += hour;
773 1.2 christos tm->tm_min += min;
774 1.2 christos }
775 1.2 christos else {
776 1.2 christos tm->tm_hour -= hour;
777 1.2 christos tm->tm_min -= min;
778 1.2 christos }
779 1.9 christos if ((otime = timegm(tm)) == (time_t)-1 ||
780 1.9 christos localtime_r(&otime, tm) == NULL) {
781 1.11 christos if (debug)
782 1.11 christos warnx("cannot convert date: \"%s\"", date);
783 1.11 christos *tm = save_tm;
784 1.9 christos }
785 1.2 christos }
786 1.11 christos else { /* Unable to do the conversion to local time. */
787 1.11 christos *tm = save_tm;
788 1.11 christos /* tm->tm_isdst = -1; */ /* Set above */
789 1.2 christos tm->tm_gmtoff = 0;
790 1.11 christos tm->tm_zone = NULL;
791 1.11 christos }
792 1.1 christos }
793 1.1 christos else {
794 1.1 christos struct headline hl;
795 1.1 christos char headline[LINESIZE];
796 1.2 christos char pbuf[LINESIZE];
797 1.11 christos
798 1.11 christos if (debug && use_hl_date == 0)
799 1.11 christos warnx("invalid date: \"%s\"", date ? date : "<null>");
800 1.11 christos
801 1.11 christos /*
802 1.11 christos * The headline is written locally so failures here
803 1.11 christos * should be seen (i.e., not conditional on 'debug').
804 1.11 christos */
805 1.11 christos tm->tm_isdst = -1; /* let mktime(3) determine tm_isdst */
806 1.1 christos headline[0] = '\0';
807 1.14 christos (void)readline(setinput(mp), headline, (int)sizeof(headline), 0);
808 1.1 christos parse(headline, &hl, pbuf);
809 1.11 christos if (hl.l_date == NULL)
810 1.11 christos warnx("invalid headline: `%s'", headline);
811 1.3 christos
812 1.11 christos else if (hl_date_to_tm(hl.l_date, tm) == NULL ||
813 1.11 christos mktime(tm) == -1)
814 1.11 christos warnx("invalid headline date: `%s'", hl.l_date);
815 1.1 christos }
816 1.1 christos }
817 1.1 christos
818 1.1 christos /*
819 1.1 christos * Get the sender's address for display. Let nameof() do this.
820 1.1 christos */
821 1.1 christos static const char *
822 1.1 christos addrof(struct message *mp)
823 1.1 christos {
824 1.1 christos if (mp == NULL)
825 1.1 christos return NULL;
826 1.1 christos
827 1.1 christos return nameof(mp, 0);
828 1.1 christos }
829 1.1 christos
830 1.1 christos /************************************************************************
831 1.11 christos * The 'address' syntax - from RFC 2822:
832 1.1 christos *
833 1.1 christos * specials = "(" / ")" / ; Special characters used in
834 1.1 christos * "<" / ">" / ; other parts of the syntax
835 1.1 christos * "[" / "]" /
836 1.1 christos * ":" / ";" /
837 1.1 christos * "@" / "\" /
838 1.1 christos * "," / "." /
839 1.1 christos * DQUOTE
840 1.1 christos * qtext = NO-WS-CTL / ; Non white space controls
841 1.1 christos * %d33 / ; The rest of the US-ASCII
842 1.1 christos * %d35-91 / ; characters not including "\"
843 1.1 christos * %d93-126 ; or the quote character
844 1.1 christos * qcontent = qtext / quoted-pair
845 1.1 christos * quoted-string = [CFWS]
846 1.1 christos * DQUOTE *([FWS] qcontent) [FWS] DQUOTE
847 1.1 christos * [CFWS]
848 1.1 christos * atext = ALPHA / DIGIT / ; Any character except controls,
849 1.1 christos * "!" / "#" / ; SP, and specials.
850 1.1 christos * "$" / "%" / ; Used for atoms
851 1.1 christos * "&" / "'" /
852 1.1 christos * "*" / "+" /
853 1.1 christos * "-" / "/" /
854 1.1 christos * "=" / "?" /
855 1.1 christos * "^" / "_" /
856 1.1 christos * "`" / "{" /
857 1.1 christos * "|" / "}" /
858 1.1 christos * "~"
859 1.1 christos * atom = [CFWS] 1*atext [CFWS]
860 1.1 christos * word = atom / quoted-string
861 1.1 christos * phrase = 1*word / obs-phrase
862 1.1 christos * display-name = phrase
863 1.1 christos * dtext = NO-WS-CTL / ; Non white space controls
864 1.1 christos * %d33-90 / ; The rest of the US-ASCII
865 1.1 christos * %d94-126 ; characters not including "[",
866 1.1 christos * ; "]", or "\"
867 1.1 christos * dcontent = dtext / quoted-pair
868 1.1 christos * domain-literal = [CFWS] "[" *([FWS] dcontent) [FWS] "]" [CFWS]
869 1.1 christos * domain = dot-atom / domain-literal / obs-domain
870 1.1 christos * local-part = dot-atom / quoted-string / obs-local-part
871 1.1 christos * addr-spec = local-part "@" domain
872 1.1 christos * angle-addr = [CFWS] "<" addr-spec ">" [CFWS] / obs-angle-addr
873 1.1 christos * name-addr = [display-name] angle-addr
874 1.1 christos * mailbox = name-addr / addr-spec
875 1.1 christos * mailbox-list = (mailbox *("," mailbox)) / obs-mbox-list
876 1.1 christos * group = display-name ":" [mailbox-list / CFWS] ";"
877 1.1 christos * [CFWS]
878 1.1 christos * address = mailbox / group
879 1.1 christos ************************************************************************/
880 1.1 christos static char *
881 1.1 christos get_display_name(char *name)
882 1.1 christos {
883 1.2 christos char nbuf[LINESIZE];
884 1.1 christos const char *p;
885 1.1 christos char *q;
886 1.1 christos char *qend;
887 1.1 christos char *lastq;
888 1.1 christos int quoted;
889 1.1 christos
890 1.1 christos if (name == NULL)
891 1.2 christos return NULL;
892 1.1 christos
893 1.1 christos q = nbuf;
894 1.1 christos lastq = nbuf;
895 1.1 christos qend = nbuf + sizeof(nbuf) - 1; /* reserve space for '\0' */
896 1.1 christos quoted = 0;
897 1.11 christos for (p = skip_WSP(name); *p != '\0'; p++) {
898 1.11 christos DPRINTF(("get_display_name: %s\n", p));
899 1.1 christos switch (*p) {
900 1.1 christos case '"': /* quoted-string */
901 1.1 christos q = nbuf;
902 1.1 christos p = snarf_quote(&q, qend, p);
903 1.1 christos if (!quoted)
904 1.1 christos lastq = q;
905 1.1 christos quoted = 1;
906 1.1 christos break;
907 1.1 christos
908 1.1 christos case ':': /* group */
909 1.1 christos case '<': /* angle-address */
910 1.1 christos if (lastq == nbuf)
911 1.1 christos return NULL;
912 1.1 christos *lastq = '\0'; /* NULL termination */
913 1.2 christos return savestr(nbuf);
914 1.1 christos
915 1.1 christos case '(': /* comment - skip it! */
916 1.1 christos p = snarf_comment(NULL, NULL, p);
917 1.1 christos break;
918 1.1 christos
919 1.1 christos default:
920 1.1 christos if (!quoted && q < qend) {
921 1.1 christos *q++ = *p;
922 1.11 christos if (!is_WSP(*p)
923 1.15 christos /* && !is_specials((unsigned char)*p) */)
924 1.1 christos lastq = q;
925 1.1 christos }
926 1.1 christos break;
927 1.1 christos }
928 1.1 christos }
929 1.1 christos return NULL; /* no group or angle-address */
930 1.1 christos }
931 1.1 christos
932 1.1 christos /*
933 1.1 christos * See RFC 2822 sec 3.4 and 3.6.2.
934 1.1 christos */
935 1.1 christos static const char *
936 1.1 christos userof(struct message *mp)
937 1.1 christos {
938 1.1 christos char *sender;
939 1.1 christos char *dispname;
940 1.1 christos
941 1.1 christos if (mp == NULL)
942 1.1 christos return NULL;
943 1.1 christos
944 1.1 christos if ((sender = hfield("from", mp)) != NULL ||
945 1.1 christos (sender = hfield("sender", mp)) != NULL)
946 1.1 christos /*
947 1.1 christos * Try to get the display-name. If one doesn't exist,
948 1.1 christos * then the best we can hope for is that the user's
949 1.1 christos * name is in the comments.
950 1.1 christos */
951 1.1 christos if ((dispname = get_display_name(sender)) != NULL ||
952 1.1 christos (dispname = get_comments(sender)) != NULL)
953 1.1 christos return dispname;
954 1.1 christos return NULL;
955 1.1 christos }
956 1.1 christos
957 1.1 christos /*
958 1.1 christos * Grab the subject line.
959 1.1 christos */
960 1.1 christos static const char *
961 1.1 christos subjof(struct message *mp)
962 1.1 christos {
963 1.1 christos const char *subj;
964 1.1 christos
965 1.1 christos if (mp == NULL)
966 1.1 christos return NULL;
967 1.1 christos
968 1.1 christos if ((subj = hfield("subject", mp)) == NULL)
969 1.1 christos subj = hfield("subj", mp);
970 1.1 christos return subj;
971 1.1 christos }
972 1.1 christos
973 1.5 christos /*
974 1.5 christos * Protect a string against strftime() conversion.
975 1.5 christos */
976 1.5 christos static const char*
977 1.5 christos protect(const char *str)
978 1.5 christos {
979 1.5 christos char *p, *q;
980 1.5 christos size_t size;
981 1.11 christos
982 1.6 christos if (str == NULL || (size = strlen(str)) == 0)
983 1.5 christos return str;
984 1.11 christos
985 1.7 christos p = salloc(2 * size + 1);
986 1.5 christos for (q = p; *str; str++) {
987 1.5 christos *q = *str;
988 1.5 christos if (*q++ == '%')
989 1.5 christos *q++ = '%';
990 1.5 christos }
991 1.5 christos *q = '\0';
992 1.5 christos return p;
993 1.5 christos }
994 1.5 christos
995 1.1 christos static char *
996 1.1 christos preformat(struct tm *tm, const char *oldfmt, struct message *mp, int use_hl_date)
997 1.1 christos {
998 1.1 christos const char *subj;
999 1.1 christos const char *addr;
1000 1.1 christos const char *user;
1001 1.1 christos const char *p;
1002 1.1 christos char *q;
1003 1.1 christos char *newfmt;
1004 1.1 christos size_t fmtsize;
1005 1.1 christos
1006 1.1 christos if (mp != NULL && (mp->m_flag & MDELETED) != 0)
1007 1.1 christos mp = NULL; /* deleted mail shouldn't show up! */
1008 1.1 christos
1009 1.5 christos subj = protect(subjof(mp));
1010 1.5 christos addr = protect(addrof(mp));
1011 1.5 christos user = protect(userof(mp));
1012 1.11 christos dateof(tm, mp, use_hl_date);
1013 1.1 christos fmtsize = LINESIZE;
1014 1.12 christos newfmt = ecalloc(1, fmtsize); /* so we can realloc() in check_bufsize() */
1015 1.1 christos q = newfmt;
1016 1.1 christos p = oldfmt;
1017 1.1 christos while (*p) {
1018 1.1 christos if (*p == '%') {
1019 1.1 christos const char *fp;
1020 1.11 christos fp = subformat(&p, mp, addr, user, subj, tm->tm_isdst);
1021 1.1 christos if (fp) {
1022 1.1 christos size_t len;
1023 1.1 christos len = strlen(fp);
1024 1.1 christos check_bufsize(&newfmt, &fmtsize, &q, len);
1025 1.1 christos (void)strcpy(q, fp);
1026 1.1 christos q += len;
1027 1.1 christos continue;
1028 1.1 christos }
1029 1.1 christos }
1030 1.1 christos check_bufsize(&newfmt, &fmtsize, &q, 1);
1031 1.1 christos *q++ = *p++;
1032 1.1 christos }
1033 1.1 christos *q = '\0';
1034 1.1 christos
1035 1.1 christos return newfmt;
1036 1.1 christos }
1037 1.1 christos
1038 1.1 christos /*
1039 1.1 christos * If a format string begins with the USE_HL_DATE string, smsgprintf
1040 1.1 christos * will use the headerline date rather than trying to extract the date
1041 1.1 christos * from the Date field.
1042 1.1 christos *
1043 1.1 christos * Note: If a 'valid' date cannot be extracted from the Date field,
1044 1.1 christos * then the headline date is used.
1045 1.1 christos */
1046 1.1 christos #define USE_HL_DATE "%??"
1047 1.1 christos
1048 1.1 christos PUBLIC char *
1049 1.1 christos smsgprintf(const char *fmtstr, struct message *mp)
1050 1.1 christos {
1051 1.1 christos struct tm tm;
1052 1.1 christos int use_hl_date;
1053 1.1 christos char *newfmt;
1054 1.1 christos char *buf;
1055 1.1 christos size_t bufsize;
1056 1.1 christos
1057 1.1 christos if (strncmp(fmtstr, USE_HL_DATE, sizeof(USE_HL_DATE) - 1) != 0)
1058 1.1 christos use_hl_date = 0;
1059 1.1 christos else {
1060 1.1 christos use_hl_date = 1;
1061 1.1 christos fmtstr += sizeof(USE_HL_DATE) - 1;
1062 1.1 christos }
1063 1.1 christos bufsize = LINESIZE;
1064 1.1 christos buf = salloc(bufsize);
1065 1.1 christos newfmt = preformat(&tm, fmtstr, mp, use_hl_date);
1066 1.1 christos (void)strftime(buf, bufsize, newfmt, &tm);
1067 1.1 christos free(newfmt); /* preformat() uses malloc()/realloc() */
1068 1.1 christos return buf;
1069 1.1 christos }
1070 1.1 christos
1071 1.1 christos
1072 1.1 christos PUBLIC void
1073 1.1 christos fmsgprintf(FILE *fo, const char *fmtstr, struct message *mp)
1074 1.1 christos {
1075 1.1 christos char *buf;
1076 1.1 christos
1077 1.1 christos buf = smsgprintf(fmtstr, mp);
1078 1.1 christos (void)fprintf(fo, "%s\n", buf); /* XXX - add the newline here? */
1079 1.1 christos }
1080