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)
197 /* skip any initial control characters or spaces */
198 while ((c = (unsigned char) *s) && (iscntrl(c) || isspace(c)))
201 /* compact repeated control characters and spaces into a single space */
202 while((c = (unsigned char) *s++) && *start < end)
203 if (!iscntrl(c) && !isspace(c))
206 while ((c = (unsigned char) *s) &&
207 (iscntrl(c) || isspace(c)))
213 static char *lmonth[] = {
214 "January", "February", "March", "April",
215 "May", "June", "July", "August",
216 "September", "October", "November", "December"
220 get_x400_friendly(char *mbox, char *buffer, int buffer_len)
222 char given[BUFSIZ], surname[BUFSIZ];
231 if (get_x400_comp(mbox, "/PN=", buffer, buffer_len)) {
232 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
238 if (!get_x400_comp(mbox, "/S=", surname, sizeof(surname)))
241 if (get_x400_comp(mbox, "/G=", given, sizeof(given)))
242 snprintf(buffer, buffer_len, "%s %s", given, surname);
244 snprintf(buffer, buffer_len, "%s", surname);
250 get_x400_comp(char *mbox, char *key, char *buffer, int buffer_len)
255 if ((idx = stringdex(key, mbox)) < 0
256 || !(cp = strchr(mbox += idx + strlen(key), '/')))
259 snprintf(buffer, buffer_len, "%*.*s", (int)(cp - mbox), (int)(cp - mbox), mbox);
264 fmt_trim(char *str, int width)
270 strncpy(buffer, str, sizeof(buffer));
271 buffer[sizeof(buffer)-1] = '\0';
273 while (isspace(*str)) {
277 if ((i = width) < 0) {
282 if (!ljust && i > 0 && (int)strlen(str) > i) {
286 xp += strlen(str) - 1;
287 while (xp > str && isspace(*xp)) {
290 if (ljust && i > 0 && (int)strlen(str) > i) {
291 str += strlen(str) - i;
297 fmt_scan(struct format *format, char *scanl, int width, int *dat)
301 char *savestr = NULL;
302 unsigned char *str = NULL;
303 char buffer[BUFSIZ], buffer2[BUFSIZ];
313 ep = scanl + width - 1;
315 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
316 switch (fmt->f_type) {
319 fmt->f_comp->c_flags &= ~CF_PARSED;
326 switch (fmt->f_type) {
329 cpstripped(&cp, ep, fmt->f_comp->c_text);
332 cptrimmed(&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
337 while( (c = *sp++) && cp < ep)
346 ljust++; /* XXX should do something with this */
348 while( (c = *sp++) && --i >= 0 && cp < ep)
350 while( --i >= 0 && cp < ep)
355 cpstripped(&cp, ep, str);
358 cptrimmed(&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
361 adios(EX_SOFTWARE, NULL, "internal error (FT_STRFW)");
364 n = snprintf(cp, ep - cp + 1, "%d", value);
373 cpnumber(&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
384 if (!(value = (str && *str))) {
391 if (!(value = (str == NULL || *str == 0))) {
398 if (value != fmt->f_value) {
405 if (value == fmt->f_value) {
412 if (value <= fmt->f_value) {
419 if (!(value = (str && match(str, fmt->f_text)))) {
427 value = match(str, fmt->f_text);
433 if (!(value = (str && uprf(str, fmt->f_text)))) {
440 value = uprf(str, fmt->f_text);
444 value = (str != NULL && *str != 0);
448 value = (str == NULL || *str == 0);
452 value = (fmt->f_value == value);
456 value = (fmt->f_value != value);
460 value = (fmt->f_value > value);
471 str = fmt->f_comp->c_text;
477 if (!(str = getenv(fmt->f_text)))
481 if (!(str = context_find(fmt->f_text)))
485 case FT_LS_DECODECOMP:
486 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
489 str = fmt->f_comp->c_text;
493 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
499 str = fmt_trim(str, fmt->f_width);
504 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
507 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
510 value = fmt->f_value;
513 value = dat[fmt->f_value];
521 case FT_LV_CHAR_LEFT:
522 value = width - (cp - scanl);
525 value += fmt->f_value;
528 value = fmt->f_value - value;
532 value = value / fmt->f_value;
538 value = value % fmt->f_value;
547 value = fmt->f_comp->c_tws->tw_sec;
550 value = fmt->f_comp->c_tws->tw_min;
553 value = fmt->f_comp->c_tws->tw_hour;
556 value = fmt->f_comp->c_tws->tw_mday;
559 value = fmt->f_comp->c_tws->tw_mon + 1;
562 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
565 str = lmonth[fmt->f_comp->c_tws->tw_mon];
568 str = dtwszone(fmt->f_comp->c_tws);
571 value = fmt->f_comp->c_tws->tw_year;
574 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
576 value = tws->tw_wday;
579 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
581 str = tw_dotw[tws->tw_wday];
584 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
586 str = tw_ldotw[tws->tw_wday];
589 value = fmt->f_comp->c_tws->tw_yday;
592 value = fmt->f_comp->c_tws->tw_zone;
595 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
596 value = dmktime(fmt->f_comp->c_tws);
599 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
600 value = dmktime(fmt->f_comp->c_tws);
601 value = time((time_t *) 0) - value;
604 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
606 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
615 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
621 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
625 str = dasctime(fmt->f_comp->c_tws);
629 str = fmt->f_comp->c_mn->m_pers;
632 str = fmt->f_comp->c_mn->m_mbox;
635 str = fmt->f_comp->c_mn->m_host;
638 str = fmt->f_comp->c_mn->m_path;
641 str = fmt->f_comp->c_mn->m_gname;
644 str = fmt->f_comp->c_mn->m_note;
647 str = adrformat( fmt->f_comp->c_mn );
650 value = fmt->f_comp->c_mn->m_type;
653 value = fmt->f_comp->c_mn->m_ingrp;
656 value = fmt->f_comp->c_mn->m_nohost;
660 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
661 str = fmt->f_comp->c_text;
664 if (fmt->f_type == FT_LS_ADDR)
666 if ((str = mn->m_pers) == NULL) {
667 if ((str = mn->m_note)) {
668 strncpy(buffer, str, sizeof(buffer));
669 buffer[sizeof(buffer)-1] = '\0';
673 sp = str + strlen(str) - 1;
682 } else if (!(str = get_x400_friendly(mn->m_mbox,
683 buffer, sizeof(buffer)))) {
685 switch (mn->m_type) {
691 snprintf(buffer, sizeof(buffer), "%s@%s", mn->m_mbox, mn->m_host);
702 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
705 strncpy(buffer, str, sizeof(buffer));
706 /* strncpy doesn't NUL-terminate if it fills the buffer */
707 buffer[sizeof(buffer)-1] = '\0';
708 unquote_string(buffer, buffer2);
715 if ((t = comp->c_tws->tw_clock) == 0)
716 t = dmktime(comp->c_tws);
717 tws = dlocaltime(&t);
723 if ((t = comp->c_tws->tw_clock) == 0)
724 t = dmktime(comp->c_tws);
731 if (comp->c_flags & CF_PARSED)
733 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
735 comp->c_flags &= ~CF_TRUE;
736 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
737 memset((char *) comp->c_tws, 0, sizeof *comp->c_tws);
738 comp->c_flags = CF_TRUE;
740 comp->c_flags |= CF_PARSED;
745 ** hook for custom address list formatting
748 str = formataddr(savestr, str);
753 ** output the str register as an address component,
754 ** splitting it into multiple lines if necessary. The
755 ** value reg. contains the max line length. The lit.
756 ** field may contain a string to prepend to the result
762 int indent, wid, len;
771 adios(EX_SOFTWARE, NULL, "putaddr -- num register (%d) "
772 "must be greater than label "
773 "width (%d)", value, indent);
775 while( (c = *sp++) && cp < ep)
779 ** try to break at a comma; failing that,
782 lastb = 0; sp = lp + wid;
783 while (sp > lp && (c = *--sp) != ',') {
784 if (! lastb && isspace(c))
788 if (! (sp = lastb)) {
790 while (*sp && *sp != ',' && !isspace(*sp))
797 while (cp < ep && lp <= sp)
804 for (i=indent; cp < ep && i > 0; i--)
808 cpstripped(&cp, ep, lp);
814 if (comp->c_flags & CF_PARSED)
816 if (comp->c_mn != &fmt_mnull)
818 if ((sp = comp->c_text) && (sp = getname(sp)) &&
819 (mn = getm(sp, NULL, 0, fmt_norm, NULL))) {
823 comp->c_flags |= CF_PARSED;
825 while (getname("")) /* XXX */
827 comp->c_mn = &fmt_mnull;
833 ** if there's no component, we say true. Otherwise we
834 ** say "true" only if we can parse the address and it
835 ** matches one of our addresses.
838 if (comp->c_mn != &fmt_mnull)
840 if ((sp = comp->c_text) && (sp = getname(sp)) &&
841 (mn = getm(sp, NULL, 0, AD_NAME, NULL))) {
844 comp->c_flags |= CF_TRUE;
846 comp->c_flags &= ~CF_TRUE;
847 while ((sp = getname(sp)))
848 if ((comp->c_flags & CF_TRUE) == 0 &&
849 (mn = getm(sp, NULL, 0, AD_NAME, NULL)))
851 comp->c_flags |= CF_TRUE;
853 while (getname("")) /* XXX */
855 if (comp->c_text == 0)
856 comp->c_flags |= CF_TRUE;
858 comp->c_flags &= ~CF_TRUE;
859 comp->c_mn = &fmt_mnull;
868 if (*str == '<' && str[strlen(str)-1] == '>') {
870 str[strlen(str)-1] = '\0';
872 if (strncmp("mailto:", str, 7)==0) {
884 return ((struct format *)0);