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>
20 #ifdef HAVE_SYS_TIME_H
21 # include <sys/time.h>
25 #ifdef MULTIBYTE_SUPPORT
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);
39 static char *fmt_trim(char *, int);
43 ** test if string "sub" appears anywhere in
44 ** string "str" (case insensitive).
48 match(char *str, char *sub)
54 while ((c2 = *str++) && tolower(c1) != tolower(c2))
58 s1 = sub + 1; s2 = str;
59 while ((c1 = *s1++) && tolower(c1) == tolower(*s2++))
68 ** copy a number to the destination subject to a maximum width
71 cpnumber(char **dest, int num, unsigned int wid, char fill, size_t n) {
84 *--sp = (i % 10) + '0';
86 } while (i > 0 && sp > cp);
89 else if ((num) < 0 && sp > cp)
99 ** copy string from str to dest padding with the fill character to a size
100 ** of wid characters. if wid is negative, the string is right aligned
101 ** no more than n bytes are copied
104 cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
105 int remaining; /* remaining output width available */
107 int end; /* number of input bytes remaining in str */
108 #ifdef MULTIBYTE_SUPPORT
109 int char_len; /* bytes in current character */
112 char *sp; /* current position in source string */
113 char *cp = *dest; /* current position in destination string */
114 char *ep = cp + n; /* end of destination buffer */
119 if ((remaining = (wid)) < 0) {
120 remaining = -remaining;
124 mbtowc(NULL, NULL, 0); /* reset shift state */
126 while (*sp && remaining > 0 && end > 0) {
127 #ifdef MULTIBYTE_SUPPORT
128 char_len = mbtowc(&wide_char, sp, end);
129 if (char_len <= 0 || (cp + char_len > ep))
134 if (iswcntrl(wide_char) || iswspace(wide_char)) {
138 if (iscntrl(*sp) || isspace(*sp)) {
151 #ifdef MULTIBYTE_SUPPORT
152 w = wcwidth(wide_char);
153 if (w >= 0 && remaining >= w) {
154 strncpy(cp, sp, char_len);
167 if (cp + remaining > ep)
171 /* copy string to the right */
172 while (--cp >= *dest)
173 *(cp + remaining) = *cp;
174 /* add padding at the beginning */
176 for (c=remaining; c>0; c--)
181 /* pad remaining space */
182 while (remaining-- > 0 && cp < ep)
189 cpstripped(char **start, char *end, char *str)
191 #ifdef MULTIBYTE_SUPPORT
192 int char_len; /* bytes in current character */
203 /* skip any initial control characters or spaces */
204 #ifdef MULTIBYTE_SUPPORT
205 nbytes = end - *start + 1;
206 mbtowc(NULL, NULL, 0); /* reset shift state */
207 while ((char_len = mbtowc(&wide_char, s, nbytes)) > 0 && (iswcntrl(wide_char) || iswspace(wide_char))) {
212 while ((c = (unsigned char) *s) && (iscntrl(c) || isspace(c)))
216 /* compact repeated control characters and spaces into a single space */
217 #ifdef MULTIBYTE_SUPPORT
218 while ((char_len = mbtowc(&wide_char, s, nbytes)) > 0 && *start < end) {
219 if (!iswcntrl(wide_char) && !iswspace(wide_char)) {
220 strncpy(*start, s, char_len);
225 while ((char_len = mbtowc(&wide_char, s, nbytes)) > 0 && (iswcntrl(wide_char) || iswspace(wide_char))) {
233 while((c = (unsigned char) *s++) && *start < end) {
234 if (!iscntrl(c) && !isspace(c)) {
237 while ((c = (unsigned char) *s) &&
238 (iscntrl(c) || isspace(c)))
246 static char *lmonth[] = {
247 "January", "February", "March", "April",
248 "May", "June", "July", "August",
249 "September", "October", "November", "December"
253 get_x400_friendly(char *mbox, char *buffer, int buffer_len)
255 char given[BUFSIZ], surname[BUFSIZ];
264 if (get_x400_comp(mbox, "/PN=", buffer, buffer_len)) {
265 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
271 if (!get_x400_comp(mbox, "/S=", surname, sizeof(surname)))
274 if (get_x400_comp(mbox, "/G=", given, sizeof(given)))
275 snprintf(buffer, buffer_len, "%s %s", given, surname);
277 snprintf(buffer, buffer_len, "%s", surname);
283 get_x400_comp(char *mbox, char *key, char *buffer, int buffer_len)
288 if ((idx = stringdex(key, mbox)) < 0
289 || !(cp = strchr(mbox += idx + strlen(key), '/')))
292 snprintf(buffer, buffer_len, "%*.*s", (int)(cp - mbox), (int)(cp - mbox), mbox);
297 fmt_trim(char *str, int width)
304 strncpy(buffer, str, sizeof(buffer));
306 dst[sizeof(buffer)-1] = '\0';
307 while (isspace(*dst)) {
311 if ((i = width) < 0) {
316 if (!ljust && i > 0 && (int)strlen(dst) > i) {
320 xp += strlen(dst) - 1;
321 while (xp > dst && isspace(*xp)) {
324 if (ljust && i > 0 && (int)strlen(dst) > i) {
325 dst += strlen(dst) - i;
327 strncpy(str, dst, strlen(str) + 1);
332 fmt_scan(struct format *format, char *scanl, int width, int *dat)
336 char *savestr = NULL;
337 unsigned char *str = NULL;
338 char buffer[BUFSIZ], buffer2[BUFSIZ];
348 ep = scanl + width - 1;
350 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
351 switch (fmt->f_type) {
354 fmt->f_comp->c_flags &= ~CF_PARSED;
361 switch (fmt->f_type) {
364 cpstripped(&cp, ep, fmt->f_comp->c_text);
367 cptrimmed(&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
372 while( (c = *sp++) && cp < ep)
381 ljust++; /* XXX should do something with this */
383 while( (c = *sp++) && --i >= 0 && cp < ep)
385 while( --i >= 0 && cp < ep)
390 cpstripped(&cp, ep, str);
393 cptrimmed(&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
396 adios(EX_SOFTWARE, NULL, "internal error (FT_STRFW)");
399 n = snprintf(cp, ep - cp + 1, "%d", value);
408 cpnumber(&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
419 if (!(value = (str && *str))) {
426 if (!(value = (str == NULL || *str == 0))) {
433 if (value != fmt->f_value) {
440 if (value == fmt->f_value) {
447 if (value <= fmt->f_value) {
454 if (!(value = (str && match(str, fmt->f_text)))) {
462 value = match(str, fmt->f_text);
468 if (!(value = (str && uprf(str, fmt->f_text)))) {
475 value = uprf(str, fmt->f_text);
479 value = (str != NULL && *str != 0);
483 value = (str == NULL || *str == 0);
487 value = (fmt->f_value == value);
491 value = (fmt->f_value != value);
495 value = (fmt->f_value > value);
506 str = fmt->f_comp->c_text;
512 if (!(str = getenv(fmt->f_text)))
516 if (!(str = context_find(fmt->f_text)))
520 case FT_LS_DECODECOMP:
521 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
524 str = fmt->f_comp->c_text;
528 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
534 str = fmt_trim(str, fmt->f_width);
539 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
542 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
545 value = fmt->f_value;
548 value = dat[fmt->f_value];
556 case FT_LV_CHAR_LEFT:
557 value = width - (cp - scanl);
560 value += fmt->f_value;
563 value = fmt->f_value - value;
567 value = value / fmt->f_value;
573 value = value % fmt->f_value;
582 value = fmt->f_comp->c_tws->tw_sec;
585 value = fmt->f_comp->c_tws->tw_min;
588 value = fmt->f_comp->c_tws->tw_hour;
591 value = fmt->f_comp->c_tws->tw_mday;
594 value = fmt->f_comp->c_tws->tw_mon + 1;
597 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
600 str = lmonth[fmt->f_comp->c_tws->tw_mon];
603 str = dtwszone(fmt->f_comp->c_tws);
606 value = fmt->f_comp->c_tws->tw_year;
609 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
611 value = tws->tw_wday;
614 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
616 str = tw_dotw[tws->tw_wday];
619 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
621 str = tw_ldotw[tws->tw_wday];
624 value = fmt->f_comp->c_tws->tw_yday;
627 value = fmt->f_comp->c_tws->tw_zone;
630 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
631 value = dmktime(fmt->f_comp->c_tws);
634 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
635 value = dmktime(fmt->f_comp->c_tws);
636 value = time((time_t *) 0) - value;
639 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
641 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
650 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
656 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
660 str = dasctime(fmt->f_comp->c_tws);
664 str = fmt->f_comp->c_mn->m_pers;
667 str = fmt->f_comp->c_mn->m_mbox;
670 str = fmt->f_comp->c_mn->m_host;
673 str = fmt->f_comp->c_mn->m_path;
676 str = fmt->f_comp->c_mn->m_gname;
679 str = fmt->f_comp->c_mn->m_note;
682 str = adrformat( fmt->f_comp->c_mn );
685 value = fmt->f_comp->c_mn->m_type;
688 value = fmt->f_comp->c_mn->m_ingrp;
691 value = fmt->f_comp->c_mn->m_nohost;
695 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
696 str = fmt->f_comp->c_text;
699 if (fmt->f_type == FT_LS_ADDR)
701 if ((str = mn->m_pers) == NULL) {
702 if ((str = mn->m_note)) {
703 strncpy(buffer, str, sizeof(buffer));
704 buffer[sizeof(buffer)-1] = '\0';
708 sp = str + strlen(str) - 1;
717 } else if (!(str = get_x400_friendly(mn->m_mbox,
718 buffer, sizeof(buffer)))) {
720 switch (mn->m_type) {
726 snprintf(buffer, sizeof(buffer), "%s@%s", mn->m_mbox, mn->m_host);
737 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
740 strncpy(buffer, str, sizeof(buffer));
741 /* strncpy doesn't NUL-terminate if it fills the buffer */
742 buffer[sizeof(buffer)-1] = '\0';
743 unquote_string(buffer, buffer2);
750 if ((t = comp->c_tws->tw_clock) == 0)
751 t = dmktime(comp->c_tws);
752 tws = dlocaltime(&t);
758 if ((t = comp->c_tws->tw_clock) == 0)
759 t = dmktime(comp->c_tws);
766 if (comp->c_flags & CF_PARSED)
768 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
770 comp->c_flags &= ~CF_TRUE;
771 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
772 memset((char *) comp->c_tws, 0, sizeof *comp->c_tws);
773 comp->c_flags = CF_TRUE;
775 comp->c_flags |= CF_PARSED;
780 ** hook for custom address list formatting
783 str = formataddr(savestr, str);
788 ** output the str register as an address component,
789 ** splitting it into multiple lines if necessary. The
790 ** value reg. contains the max line length. The lit.
791 ** field may contain a string to prepend to the result
797 int indent, wid, len;
806 adios(EX_SOFTWARE, NULL, "putaddr -- num register (%d) "
807 "must be greater than label "
808 "width (%d)", value, indent);
810 while( (c = *sp++) && cp < ep)
814 ** try to break at a comma; failing that,
817 lastb = 0; sp = lp + wid;
818 while (sp > lp && (c = *--sp) != ',') {
819 if (! lastb && isspace(c))
823 if (! (sp = lastb)) {
825 while (*sp && *sp != ',' && !isspace(*sp))
832 while (cp < ep && lp <= sp)
839 for (i=indent; cp < ep && i > 0; i--)
843 cpstripped(&cp, ep, lp);
849 if (comp->c_flags & CF_PARSED)
851 if (comp->c_mn != &fmt_mnull)
853 if ((sp = comp->c_text) && (sp = getname(sp)) &&
854 (mn = getm(sp, NULL, 0, fmt_norm, NULL))) {
858 comp->c_flags |= CF_PARSED;
860 while (getname("")) /* XXX */
862 comp->c_mn = &fmt_mnull;
868 ** if there's no component, we say true. Otherwise we
869 ** say "true" only if we can parse the address and it
870 ** matches one of our addresses.
873 if (comp->c_mn != &fmt_mnull)
875 if ((sp = comp->c_text) && (sp = getname(sp)) &&
876 (mn = getm(sp, NULL, 0, AD_NAME, NULL))) {
879 comp->c_flags |= CF_TRUE;
881 comp->c_flags &= ~CF_TRUE;
882 while ((sp = getname(sp)))
883 if ((comp->c_flags & CF_TRUE) == 0 &&
884 (mn = getm(sp, NULL, 0, AD_NAME, NULL)))
886 comp->c_flags |= CF_TRUE;
888 while (getname("")) /* XXX */
890 if (comp->c_text == 0)
891 comp->c_flags |= CF_TRUE;
893 comp->c_flags &= ~CF_TRUE;
894 comp->c_mn = &fmt_mnull;
903 if (*str == '<' && str[strlen(str)-1] == '>') {
905 str[strlen(str)-1] = '\0';
907 if (strncmp("mailto:", str, 7)==0) {
919 return ((struct format *)0);