2 ** fmt_scan.c -- format string interpretation
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
8 ** This is the engine that processes the format instructions created by
9 ** fmt_compile (found in fmt_compile.c).
13 #include <h/addrsbr.h>
14 #include <h/fmt_scan.h>
16 #include <h/fmt_compile.h>
18 #ifdef HAVE_SYS_TIME_H
19 # include <sys/time.h>
23 #ifdef MULTIBYTE_SUPPORT
28 extern char *formataddr(); /* hook for custom address formatting */
30 extern int fmt_norm; /* defined in sbr/fmt_def.c = AD_NAME */
31 struct mailname fmt_mnull;
36 static int match(char *, char *);
37 static char *get_x400_friendly(char *, char *, int);
38 static int get_x400_comp(char *, char *, char *, int);
42 ** test if string "sub" appears anywhere in
43 ** string "str" (case insensitive).
47 match(char *str, char *sub)
53 while ((c2 = *str++) && tolower(c1) != tolower(c2))
57 s1 = sub + 1; s2 = str;
58 while ((c1 = *s1++) && tolower(c1) == tolower(*s2++))
67 ** copy a number to the destination subject to a maximum width
70 cpnumber(char **dest, int num, unsigned int wid, char fill, size_t n) {
83 *--sp = (i % 10) + '0';
85 } while (i > 0 && sp > cp);
88 else if ((num) < 0 && sp > cp)
98 ** copy string from str to dest padding with the fill character to a size
99 ** of wid characters. if wid is negative, the string is right aligned
100 ** no more than n bytes are copied
103 cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
104 int remaining; /* remaining output width available */
106 int end; /* number of input bytes remaining in str */
107 #ifdef MULTIBYTE_SUPPORT
108 int char_len; /* bytes in current character */
111 char *sp; /* current position in source string */
112 char *cp = *dest; /* current position in destination string */
113 char *ep = cp + n; /* end of destination buffer */
118 if ((remaining = (wid)) < 0) {
119 remaining = -remaining;
123 mbtowc(NULL, NULL, 0); /* reset shift state */
125 while (*sp && remaining > 0 && end > 0) {
126 #ifdef MULTIBYTE_SUPPORT
127 char_len = mbtowc(&wide_char, sp, end);
128 if (char_len <= 0 || (cp + char_len > ep))
133 if (iswcntrl(wide_char) || iswspace(wide_char)) {
137 if (iscntrl(*sp) || isspace(*sp)) {
150 #ifdef MULTIBYTE_SUPPORT
151 w = wcwidth(wide_char);
152 if (w >= 0 && remaining >= w) {
153 strncpy(cp, sp, char_len);
166 if (cp + remaining > ep)
170 /* copy string to the right */
171 while (--cp >= *dest)
172 *(cp + remaining) = *cp;
173 /* add padding at the beginning */
175 for (c=remaining; c>0; c--)
180 /* pad remaining space */
181 while (remaining-- > 0 && cp < ep)
188 cpstripped(char **start, char *end, char *str)
196 /* skip any initial control characters or spaces */
197 while ((c = (unsigned char) *s) && (iscntrl(c) || isspace(c)))
200 /* compact repeated control characters and spaces into a single space */
201 while((c = (unsigned char) *s++) && *start < end)
202 if (!iscntrl(c) && !isspace(c))
205 while ((c = (unsigned char) *s) &&
206 (iscntrl(c) || isspace(c)))
212 static char *lmonth[] = {
213 "January", "February", "March", "April",
214 "May", "June", "July", "August",
215 "September", "October", "November", "December"
219 get_x400_friendly(char *mbox, char *buffer, int buffer_len)
221 char given[BUFSIZ], surname[BUFSIZ];
230 if (get_x400_comp(mbox, "/PN=", buffer, buffer_len)) {
231 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
237 if (!get_x400_comp(mbox, "/S=", surname, sizeof(surname)))
240 if (get_x400_comp(mbox, "/G=", given, sizeof(given)))
241 snprintf(buffer, buffer_len, "%s %s", given, surname);
243 snprintf(buffer, buffer_len, "%s", surname);
249 get_x400_comp(char *mbox, char *key, char *buffer, int buffer_len)
254 if ((idx = stringdex(key, mbox)) < 0
255 || !(cp = strchr(mbox += idx + strlen(key), '/')))
258 snprintf(buffer, buffer_len, "%*.*s", (int)(cp - mbox), (int)(cp - mbox), mbox);
263 fmt_scan(struct format *format, char *scanl, int width, int *dat)
267 char *savestr = NULL;
268 unsigned char *str = NULL;
269 char buffer[BUFSIZ], buffer2[BUFSIZ];
279 ep = scanl + width - 1;
281 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
282 switch (fmt->f_type) {
285 fmt->f_comp->c_flags &= ~CF_PARSED;
292 switch (fmt->f_type) {
295 cpstripped(&cp, ep, fmt->f_comp->c_text);
298 cptrimmed(&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
303 while( (c = *sp++) && cp < ep)
312 ljust++; /* XXX should do something with this */
314 while( (c = *sp++) && --i >= 0 && cp < ep)
316 while( --i >= 0 && cp < ep)
321 cpstripped(&cp, ep, str);
324 cptrimmed(&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
327 adios(NULL, "internal error (FT_STRFW)");
330 n = snprintf(cp, ep - cp + 1, "%d", value);
339 cpnumber(&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
350 if (!(value = (str && *str))) {
357 if (!(value = (str == NULL || *str == 0))) {
364 if (value != fmt->f_value) {
371 if (value == fmt->f_value) {
378 if (value <= fmt->f_value) {
385 if (!(value = (str && match(str, fmt->f_text)))) {
393 value = match(str, fmt->f_text);
399 if (!(value = (str && uprf(str, fmt->f_text)))) {
406 value = uprf(str, fmt->f_text);
410 value = (str != NULL && *str != 0);
414 value = (str == NULL || *str == 0);
418 value = (fmt->f_value == value);
422 value = (fmt->f_value != value);
426 value = (fmt->f_value > value);
437 str = fmt->f_comp->c_text;
443 if (!(str = getenv(fmt->f_text)))
447 if (!(str = context_find(fmt->f_text)))
451 case FT_LS_DECODECOMP:
452 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
455 str = fmt->f_comp->c_text;
459 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
467 strncpy(buffer, str, sizeof(buffer));
468 buffer[sizeof(buffer)-1] = '\0';
470 while (isspace(*str))
473 if ((i = fmt->f_width) < 0) {
478 if (!ljust && i > 0 && (int)strlen(str) > i)
481 xp += strlen(str) - 1;
482 while (xp > str && isspace(*xp))
484 if (ljust && i > 0 && (int)strlen(str) > i)
485 str += strlen(str) - i;
490 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
493 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
496 value = fmt->f_value;
499 value = dat[fmt->f_value];
507 case FT_LV_CHAR_LEFT:
508 value = width - (cp - scanl);
511 value += fmt->f_value;
514 value = fmt->f_value - value;
518 value = value / fmt->f_value;
524 value = value % fmt->f_value;
533 value = fmt->f_comp->c_tws->tw_sec;
536 value = fmt->f_comp->c_tws->tw_min;
539 value = fmt->f_comp->c_tws->tw_hour;
542 value = fmt->f_comp->c_tws->tw_mday;
545 value = fmt->f_comp->c_tws->tw_mon + 1;
548 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
551 str = lmonth[fmt->f_comp->c_tws->tw_mon];
554 str = dtwszone(fmt->f_comp->c_tws);
557 value = fmt->f_comp->c_tws->tw_year;
560 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
562 value = tws->tw_wday;
565 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
567 str = tw_dotw[tws->tw_wday];
570 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
572 str = tw_ldotw[tws->tw_wday];
575 value = fmt->f_comp->c_tws->tw_yday;
578 value = fmt->f_comp->c_tws->tw_zone;
581 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
582 value = dmktime(fmt->f_comp->c_tws);
585 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
586 value = dmktime(fmt->f_comp->c_tws);
587 value = time((time_t *) 0) - value;
590 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
592 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
601 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
607 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
611 str = dasctime(fmt->f_comp->c_tws);
615 str = fmt->f_comp->c_mn->m_pers;
618 str = fmt->f_comp->c_mn->m_mbox;
621 str = fmt->f_comp->c_mn->m_host;
624 str = fmt->f_comp->c_mn->m_path;
627 str = fmt->f_comp->c_mn->m_gname;
630 str = fmt->f_comp->c_mn->m_note;
633 str = adrformat( fmt->f_comp->c_mn );
636 value = fmt->f_comp->c_mn->m_type;
639 value = fmt->f_comp->c_mn->m_ingrp;
642 value = fmt->f_comp->c_mn->m_nohost;
646 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
647 str = fmt->f_comp->c_text;
650 if (fmt->f_type == FT_LS_ADDR)
652 if ((str = mn->m_pers) == NULL) {
653 if ((str = mn->m_note)) {
654 strncpy(buffer, str, sizeof(buffer));
655 buffer[sizeof(buffer)-1] = '\0';
659 sp = str + strlen(str) - 1;
668 } else if (!(str = get_x400_friendly(mn->m_mbox,
669 buffer, sizeof(buffer)))) {
671 switch (mn->m_type) {
677 snprintf(buffer, sizeof(buffer), "%s@%s", mn->m_mbox, mn->m_host);
688 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
692 strncpy(buffer, str, sizeof(buffer));
693 /* strncpy doesn't NUL-terminate if it fills the buffer */
694 buffer[sizeof(buffer)-1] = '\0';
697 /* we will parse from buffer to buffer2 */
698 n = 0; /* n is the input position in str */
699 m = 0; /* m is the ouput position in buffer2 */
701 while ( str[n] != '\0') {
706 buffer2[m++] = str[n++];
712 buffer2[m++] = str[n++];
723 if ((t = comp->c_tws->tw_clock) == 0)
724 t = dmktime(comp->c_tws);
725 tws = dlocaltime(&t);
731 if ((t = comp->c_tws->tw_clock) == 0)
732 t = dmktime(comp->c_tws);
739 if (comp->c_flags & CF_PARSED)
741 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
743 comp->c_flags &= ~CF_TRUE;
744 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
745 memset((char *) comp->c_tws, 0, sizeof *comp->c_tws);
746 comp->c_flags = CF_TRUE;
748 comp->c_flags |= CF_PARSED;
753 ** hook for custom address list formatting
756 str = formataddr(savestr, str);
761 ** output the str register as an address component,
762 ** splitting it into multiple lines if necessary. The
763 ** value reg. contains the max line length. The lit.
764 ** field may contain a string to prepend to the result
770 int indent, wid, len;
779 adios(NULL, "putaddr -- num register (%d) "
780 "must be greater than label "
781 "width (%d)", value, indent);
783 while( (c = *sp++) && cp < ep)
786 /* try to break at a comma; failing that,
789 lastb = 0; sp = lp + wid;
790 while (sp > lp && (c = *--sp) != ',') {
791 if (! lastb && isspace(c))
795 if (! (sp = lastb)) {
797 while (*sp && *sp != ',' && !isspace(*sp))
804 while (cp < ep && lp <= sp)
811 for (i=indent; cp < ep && i > 0; i--)
815 cpstripped(&cp, ep, lp);
821 if (comp->c_flags & CF_PARSED)
823 if (comp->c_mn != &fmt_mnull)
825 if ((sp = comp->c_text) && (sp = getname(sp)) &&
826 (mn = getm(sp, NULL, 0, fmt_norm, NULL))) {
830 comp->c_flags |= CF_PARSED;
832 while (getname("")) /* XXX */
834 comp->c_mn = &fmt_mnull;
840 ** if there's no component, we say true. Otherwise we
841 ** say "true" only if we can parse the address and it
842 ** matches one of our addresses.
845 if (comp->c_mn != &fmt_mnull)
847 if ((sp = comp->c_text) && (sp = getname(sp)) &&
848 (mn = getm(sp, NULL, 0, AD_NAME, NULL))) {
851 comp->c_flags |= CF_TRUE;
853 comp->c_flags &= ~CF_TRUE;
854 while ((sp = getname(sp)))
855 if ((comp->c_flags & CF_TRUE) == 0 &&
856 (mn = getm(sp, NULL, 0, AD_NAME, NULL)))
858 comp->c_flags |= CF_TRUE;
860 while (getname("")) /* XXX */
862 if (comp->c_text == 0)
863 comp->c_flags |= CF_TRUE;
865 comp->c_flags &= ~CF_TRUE;
866 comp->c_mn = &fmt_mnull;
877 return ((struct format *)0);
880 /* I'll remove this as soon as I understand what it does. --meillo */
883 while (fmt->f_type != FT_DONE)
888 return (fmt->f_value ? ++fmt : (struct format *) 0);