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)))
245 static char *lmonth[] = {
246 "January", "February", "March", "April",
247 "May", "June", "July", "August",
248 "September", "October", "November", "December"
252 get_x400_friendly(char *mbox, char *buffer, int buffer_len)
254 char given[BUFSIZ], surname[BUFSIZ];
263 if (get_x400_comp(mbox, "/PN=", buffer, buffer_len)) {
264 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
270 if (!get_x400_comp(mbox, "/S=", surname, sizeof(surname)))
273 if (get_x400_comp(mbox, "/G=", given, sizeof(given)))
274 snprintf(buffer, buffer_len, "%s %s", given, surname);
276 snprintf(buffer, buffer_len, "%s", surname);
282 get_x400_comp(char *mbox, char *key, char *buffer, int buffer_len)
287 if ((idx = stringdex(key, mbox)) < 0
288 || !(cp = strchr(mbox += idx + strlen(key), '/')))
291 snprintf(buffer, buffer_len, "%*.*s", (int)(cp - mbox), (int)(cp - mbox), mbox);
296 fmt_trim(char *str, int width)
302 strncpy(buffer, str, sizeof(buffer));
303 buffer[sizeof(buffer)-1] = '\0';
305 while (isspace(*str)) {
309 if ((i = width) < 0) {
314 if (!ljust && i > 0 && (int)strlen(str) > i) {
318 xp += strlen(str) - 1;
319 while (xp > str && isspace(*xp)) {
322 if (ljust && i > 0 && (int)strlen(str) > i) {
323 str += strlen(str) - i;
329 fmt_scan(struct format *format, char *scanl, int width, int *dat)
333 char *savestr = NULL;
334 unsigned char *str = NULL;
335 char buffer[BUFSIZ], buffer2[BUFSIZ];
345 ep = scanl + width - 1;
347 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
348 switch (fmt->f_type) {
351 fmt->f_comp->c_flags &= ~CF_PARSED;
358 switch (fmt->f_type) {
361 cpstripped(&cp, ep, fmt->f_comp->c_text);
364 cptrimmed(&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
369 while( (c = *sp++) && cp < ep)
378 ljust++; /* XXX should do something with this */
380 while( (c = *sp++) && --i >= 0 && cp < ep)
382 while( --i >= 0 && cp < ep)
387 cpstripped(&cp, ep, str);
390 cptrimmed(&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
393 adios(EX_SOFTWARE, NULL, "internal error (FT_STRFW)");
396 n = snprintf(cp, ep - cp + 1, "%d", value);
405 cpnumber(&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
416 if (!(value = (str && *str))) {
423 if (!(value = (str == NULL || *str == 0))) {
430 if (value != fmt->f_value) {
437 if (value == fmt->f_value) {
444 if (value <= fmt->f_value) {
451 if (!(value = (str && match(str, fmt->f_text)))) {
459 value = match(str, fmt->f_text);
465 if (!(value = (str && uprf(str, fmt->f_text)))) {
472 value = uprf(str, fmt->f_text);
476 value = (str != NULL && *str != 0);
480 value = (str == NULL || *str == 0);
484 value = (fmt->f_value == value);
488 value = (fmt->f_value != value);
492 value = (fmt->f_value > value);
503 str = fmt->f_comp->c_text;
509 if (!(str = getenv(fmt->f_text)))
513 if (!(str = context_find(fmt->f_text)))
517 case FT_LS_DECODECOMP:
518 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
521 str = fmt->f_comp->c_text;
525 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
531 str = fmt_trim(str, fmt->f_width);
536 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
539 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
542 value = fmt->f_value;
545 value = dat[fmt->f_value];
553 case FT_LV_CHAR_LEFT:
554 value = width - (cp - scanl);
557 value += fmt->f_value;
560 value = fmt->f_value - value;
564 value = value / fmt->f_value;
570 value = value % fmt->f_value;
579 value = fmt->f_comp->c_tws->tw_sec;
582 value = fmt->f_comp->c_tws->tw_min;
585 value = fmt->f_comp->c_tws->tw_hour;
588 value = fmt->f_comp->c_tws->tw_mday;
591 value = fmt->f_comp->c_tws->tw_mon + 1;
594 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
597 str = lmonth[fmt->f_comp->c_tws->tw_mon];
600 str = dtwszone(fmt->f_comp->c_tws);
603 value = fmt->f_comp->c_tws->tw_year;
606 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
608 value = tws->tw_wday;
611 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
613 str = tw_dotw[tws->tw_wday];
616 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
618 str = tw_ldotw[tws->tw_wday];
621 value = fmt->f_comp->c_tws->tw_yday;
624 value = fmt->f_comp->c_tws->tw_zone;
627 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
628 value = dmktime(fmt->f_comp->c_tws);
631 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
632 value = dmktime(fmt->f_comp->c_tws);
633 value = time((time_t *) 0) - value;
636 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
638 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
647 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
653 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
657 str = dasctime(fmt->f_comp->c_tws);
661 str = fmt->f_comp->c_mn->m_pers;
664 str = fmt->f_comp->c_mn->m_mbox;
667 str = fmt->f_comp->c_mn->m_host;
670 str = fmt->f_comp->c_mn->m_path;
673 str = fmt->f_comp->c_mn->m_gname;
676 str = fmt->f_comp->c_mn->m_note;
679 str = adrformat( fmt->f_comp->c_mn );
682 value = fmt->f_comp->c_mn->m_type;
685 value = fmt->f_comp->c_mn->m_ingrp;
688 value = fmt->f_comp->c_mn->m_nohost;
692 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
693 str = fmt->f_comp->c_text;
696 if (fmt->f_type == FT_LS_ADDR)
698 if ((str = mn->m_pers) == NULL) {
699 if ((str = mn->m_note)) {
700 strncpy(buffer, str, sizeof(buffer));
701 buffer[sizeof(buffer)-1] = '\0';
705 sp = str + strlen(str) - 1;
714 } else if (!(str = get_x400_friendly(mn->m_mbox,
715 buffer, sizeof(buffer)))) {
717 switch (mn->m_type) {
723 snprintf(buffer, sizeof(buffer), "%s@%s", mn->m_mbox, mn->m_host);
734 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
737 strncpy(buffer, str, sizeof(buffer));
738 /* strncpy doesn't NUL-terminate if it fills the buffer */
739 buffer[sizeof(buffer)-1] = '\0';
740 unquote_string(buffer, buffer2);
747 if ((t = comp->c_tws->tw_clock) == 0)
748 t = dmktime(comp->c_tws);
749 tws = dlocaltime(&t);
755 if ((t = comp->c_tws->tw_clock) == 0)
756 t = dmktime(comp->c_tws);
763 if (comp->c_flags & CF_PARSED)
765 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
767 comp->c_flags &= ~CF_TRUE;
768 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
769 memset((char *) comp->c_tws, 0, sizeof *comp->c_tws);
770 comp->c_flags = CF_TRUE;
772 comp->c_flags |= CF_PARSED;
777 ** hook for custom address list formatting
780 str = formataddr(savestr, str);
785 ** output the str register as an address component,
786 ** splitting it into multiple lines if necessary. The
787 ** value reg. contains the max line length. The lit.
788 ** field may contain a string to prepend to the result
794 int indent, wid, len;
803 adios(EX_SOFTWARE, NULL, "putaddr -- num register (%d) "
804 "must be greater than label "
805 "width (%d)", value, indent);
807 while( (c = *sp++) && cp < ep)
811 ** try to break at a comma; failing that,
814 lastb = 0; sp = lp + wid;
815 while (sp > lp && (c = *--sp) != ',') {
816 if (! lastb && isspace(c))
820 if (! (sp = lastb)) {
822 while (*sp && *sp != ',' && !isspace(*sp))
829 while (cp < ep && lp <= sp)
836 for (i=indent; cp < ep && i > 0; i--)
840 cpstripped(&cp, ep, lp);
846 if (comp->c_flags & CF_PARSED)
848 if (comp->c_mn != &fmt_mnull)
850 if ((sp = comp->c_text) && (sp = getname(sp)) &&
851 (mn = getm(sp, NULL, 0, fmt_norm, NULL))) {
855 comp->c_flags |= CF_PARSED;
857 while (getname("")) /* XXX */
859 comp->c_mn = &fmt_mnull;
865 ** if there's no component, we say true. Otherwise we
866 ** say "true" only if we can parse the address and it
867 ** matches one of our addresses.
870 if (comp->c_mn != &fmt_mnull)
872 if ((sp = comp->c_text) && (sp = getname(sp)) &&
873 (mn = getm(sp, NULL, 0, AD_NAME, NULL))) {
876 comp->c_flags |= CF_TRUE;
878 comp->c_flags &= ~CF_TRUE;
879 while ((sp = getname(sp)))
880 if ((comp->c_flags & CF_TRUE) == 0 &&
881 (mn = getm(sp, NULL, 0, AD_NAME, NULL)))
883 comp->c_flags |= CF_TRUE;
885 while (getname("")) /* XXX */
887 if (comp->c_text == 0)
888 comp->c_flags |= CF_TRUE;
890 comp->c_flags &= ~CF_TRUE;
891 comp->c_mn = &fmt_mnull;
900 if (*str == '<' && str[strlen(str)-1] == '>') {
902 str[strlen(str)-1] = '\0';
904 if (strncmp("mailto:", str, 7)==0) {
916 return ((struct format *)0);