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)
303 strncpy(buffer, str, sizeof(buffer));
304 buffer[sizeof(buffer)-1] = '\0';
306 while (isspace(*str)) {
310 if ((i = width) < 0) {
315 if (!ljust && i > 0 && (int)strlen(str) > i) {
319 xp += strlen(str) - 1;
320 while (xp > str && isspace(*xp)) {
323 if (ljust && i > 0 && (int)strlen(str) > i) {
324 str += strlen(str) - i;
330 fmt_scan(struct format *format, char *scanl, int width, int *dat)
334 char *savestr = NULL;
335 unsigned char *str = NULL;
336 char buffer[BUFSIZ], buffer2[BUFSIZ];
346 ep = scanl + width - 1;
348 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
349 switch (fmt->f_type) {
352 fmt->f_comp->c_flags &= ~CF_PARSED;
359 switch (fmt->f_type) {
362 cpstripped(&cp, ep, fmt->f_comp->c_text);
365 cptrimmed(&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
370 while( (c = *sp++) && cp < ep)
379 ljust++; /* XXX should do something with this */
381 while( (c = *sp++) && --i >= 0 && cp < ep)
383 while( --i >= 0 && cp < ep)
388 cpstripped(&cp, ep, str);
391 cptrimmed(&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
394 adios(EX_SOFTWARE, NULL, "internal error (FT_STRFW)");
397 n = snprintf(cp, ep - cp + 1, "%d", value);
406 cpnumber(&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
417 if (!(value = (str && *str))) {
424 if (!(value = (str == NULL || *str == 0))) {
431 if (value != fmt->f_value) {
438 if (value == fmt->f_value) {
445 if (value <= fmt->f_value) {
452 if (!(value = (str && match(str, fmt->f_text)))) {
460 value = match(str, fmt->f_text);
466 if (!(value = (str && uprf(str, fmt->f_text)))) {
473 value = uprf(str, fmt->f_text);
477 value = (str != NULL && *str != 0);
481 value = (str == NULL || *str == 0);
485 value = (fmt->f_value == value);
489 value = (fmt->f_value != value);
493 value = (fmt->f_value > value);
504 str = fmt->f_comp->c_text;
510 if (!(str = getenv(fmt->f_text)))
514 if (!(str = context_find(fmt->f_text)))
518 case FT_LS_DECODECOMP:
519 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
522 str = fmt->f_comp->c_text;
526 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
532 str = fmt_trim(str, fmt->f_width);
537 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
540 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
543 value = fmt->f_value;
546 value = dat[fmt->f_value];
554 case FT_LV_CHAR_LEFT:
555 value = width - (cp - scanl);
558 value += fmt->f_value;
561 value = fmt->f_value - value;
565 value = value / fmt->f_value;
571 value = value % fmt->f_value;
580 value = fmt->f_comp->c_tws->tw_sec;
583 value = fmt->f_comp->c_tws->tw_min;
586 value = fmt->f_comp->c_tws->tw_hour;
589 value = fmt->f_comp->c_tws->tw_mday;
592 value = fmt->f_comp->c_tws->tw_mon + 1;
595 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
598 str = lmonth[fmt->f_comp->c_tws->tw_mon];
601 str = dtwszone(fmt->f_comp->c_tws);
604 value = fmt->f_comp->c_tws->tw_year;
607 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
609 value = tws->tw_wday;
612 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
614 str = tw_dotw[tws->tw_wday];
617 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
619 str = tw_ldotw[tws->tw_wday];
622 value = fmt->f_comp->c_tws->tw_yday;
625 value = fmt->f_comp->c_tws->tw_zone;
628 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
629 value = dmktime(fmt->f_comp->c_tws);
632 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
633 value = dmktime(fmt->f_comp->c_tws);
634 value = time((time_t *) 0) - value;
637 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
639 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
648 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
654 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
658 str = dasctime(fmt->f_comp->c_tws);
662 str = fmt->f_comp->c_mn->m_pers;
665 str = fmt->f_comp->c_mn->m_mbox;
668 str = fmt->f_comp->c_mn->m_host;
671 str = fmt->f_comp->c_mn->m_path;
674 str = fmt->f_comp->c_mn->m_gname;
677 str = fmt->f_comp->c_mn->m_note;
680 str = adrformat( fmt->f_comp->c_mn );
683 value = fmt->f_comp->c_mn->m_type;
686 value = fmt->f_comp->c_mn->m_ingrp;
689 value = fmt->f_comp->c_mn->m_nohost;
693 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
694 str = fmt->f_comp->c_text;
697 if (fmt->f_type == FT_LS_ADDR)
699 if ((str = mn->m_pers) == NULL) {
700 if ((str = mn->m_note)) {
701 strncpy(buffer, str, sizeof(buffer));
702 buffer[sizeof(buffer)-1] = '\0';
706 sp = str + strlen(str) - 1;
715 } else if (!(str = get_x400_friendly(mn->m_mbox,
716 buffer, sizeof(buffer)))) {
718 switch (mn->m_type) {
724 snprintf(buffer, sizeof(buffer), "%s@%s", mn->m_mbox, mn->m_host);
735 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
738 strncpy(buffer, str, sizeof(buffer));
739 /* strncpy doesn't NUL-terminate if it fills the buffer */
740 buffer[sizeof(buffer)-1] = '\0';
741 unquote_string(buffer, buffer2);
748 if ((t = comp->c_tws->tw_clock) == 0)
749 t = dmktime(comp->c_tws);
750 tws = dlocaltime(&t);
756 if ((t = comp->c_tws->tw_clock) == 0)
757 t = dmktime(comp->c_tws);
764 if (comp->c_flags & CF_PARSED)
766 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
768 comp->c_flags &= ~CF_TRUE;
769 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
770 memset((char *) comp->c_tws, 0, sizeof *comp->c_tws);
771 comp->c_flags = CF_TRUE;
773 comp->c_flags |= CF_PARSED;
778 ** hook for custom address list formatting
781 str = formataddr(savestr, str);
786 ** output the str register as an address component,
787 ** splitting it into multiple lines if necessary. The
788 ** value reg. contains the max line length. The lit.
789 ** field may contain a string to prepend to the result
795 int indent, wid, len;
804 adios(EX_SOFTWARE, NULL, "putaddr -- num register (%d) "
805 "must be greater than label "
806 "width (%d)", value, indent);
808 while( (c = *sp++) && cp < ep)
812 ** try to break at a comma; failing that,
815 lastb = 0; sp = lp + wid;
816 while (sp > lp && (c = *--sp) != ',') {
817 if (! lastb && isspace(c))
821 if (! (sp = lastb)) {
823 while (*sp && *sp != ',' && !isspace(*sp))
830 while (cp < ep && lp <= sp)
837 for (i=indent; cp < ep && i > 0; i--)
841 cpstripped(&cp, ep, lp);
847 if (comp->c_flags & CF_PARSED)
849 if (comp->c_mn != &fmt_mnull)
851 if ((sp = comp->c_text) && (sp = getname(sp)) &&
852 (mn = getm(sp, NULL, 0, fmt_norm, NULL))) {
856 comp->c_flags |= CF_PARSED;
858 while (getname("")) /* XXX */
860 comp->c_mn = &fmt_mnull;
866 ** if there's no component, we say true. Otherwise we
867 ** say "true" only if we can parse the address and it
868 ** matches one of our addresses.
871 if (comp->c_mn != &fmt_mnull)
873 if ((sp = comp->c_text) && (sp = getname(sp)) &&
874 (mn = getm(sp, NULL, 0, AD_NAME, NULL))) {
877 comp->c_flags |= CF_TRUE;
879 comp->c_flags &= ~CF_TRUE;
880 while ((sp = getname(sp)))
881 if ((comp->c_flags & CF_TRUE) == 0 &&
882 (mn = getm(sp, NULL, 0, AD_NAME, NULL)))
884 comp->c_flags |= CF_TRUE;
886 while (getname("")) /* XXX */
888 if (comp->c_text == 0)
889 comp->c_flags |= CF_TRUE;
891 comp->c_flags &= ~CF_TRUE;
892 comp->c_mn = &fmt_mnull;
901 if (*str == '<' && str[strlen(str)-1] == '>') {
903 str[strlen(str)-1] = '\0';
905 if (strncmp("mailto:", str, 7)==0) {
917 return ((struct format *)0);