3 * fmt_scan.c -- format string interpretation
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
14 #include <h/fmt_compile.h>
16 #ifdef HAVE_SYS_TIME_H
17 # include <sys/time.h>
20 #ifdef MULTIBYTE_SUPPORT
25 extern char *formataddr (); /* hook for custom address formatting */
26 extern char *concataddr (); /* address formatting but allowing duplicates */
29 struct msgs *fmt_current_folder; /* current folder (set by main program) */
32 extern int fmt_norm; /* defined in sbr/fmt_def.c = AD_NAME */
33 struct mailname fmt_mnull;
38 static int match (char *, char *);
39 static char *get_x400_friendly (char *, char *, int);
40 static int get_x400_comp (char *, char *, char *, int);
44 * test if string "sub" appears anywhere in
45 * string "str" (case insensitive).
49 match (char *str, char *sub)
56 c1 = (isalpha(c1) && isupper(c1)) ? tolower(c1) : c1;
57 while ((c2 = *str++) && c1 != ((isalpha(c2) && isupper(c2)) ? tolower(c2) : c2))
61 s1 = sub + 1; s2 = str;
62 while ((c1 = *s1++) && ((isalpha(c1) && isupper(c1)) ? tolower(c1) : c1) == ((isalpha(c2 =*s2++) && isupper(c2)) ? tolower(c2) : c2))
69 while ((c2 = *str++) && (c1 | 040) != (c2 | 040))
73 s1 = sub + 1; s2 = str;
74 while ((c1 = *s1++) && (c1 | 040) == (*s2++ | 040))
84 * copy a number to the destination subject to a maximum width
87 cpnumber(char **dest, int num, unsigned int wid, char fill, size_t n) {
100 *--sp = (i % 10) + '0';
102 } while (i > 0 && sp > cp);
105 else if ((num) < 0 && sp > cp)
115 * copy string from str to dest padding with the fill character to a size
116 * of wid characters. if wid is negative, the string is right aligned
117 * no more than n bytes are copied
120 cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
121 int remaining; /* remaining output width available */
123 int end; /* number of input bytes remaining in str */
124 #ifdef MULTIBYTE_SUPPORT
125 int char_len; /* bytes in current character */
129 char *sp; /* current position in source string */
130 char *cp = *dest; /* current position in destination string */
131 char *ep = cp + n; /* end of destination buffer */
136 if ((remaining = (wid)) < 0) {
137 remaining = -remaining;
141 mbtowc(NULL, NULL, 0); /* reset shift state */
143 while (*sp && remaining > 0 && end > 0) {
144 #ifdef MULTIBYTE_SUPPORT
145 char_len = mbtowc(&wide_char, sp, end);
146 if (char_len <= 0 || (cp + char_len > ep))
151 if (iswcntrl(wide_char) || iswspace(wide_char)) {
155 if (iscntrl(*sp) || isspace(*sp)) {
168 #ifdef MULTIBYTE_SUPPORT
169 w = wcwidth(wide_char);
170 if (w >= 0 && remaining >= w) {
171 strncpy(cp, sp, char_len);
184 if (cp + remaining > ep)
188 /* copy string to the right */
189 while (--cp >= *dest)
190 *(cp + remaining) = *cp;
191 /* add padding at the beginning */
193 for (c=remaining; c>0; c--)
198 /* pad remaining space */
199 while (remaining-- > 0 && cp < ep)
206 cpstripped (char **start, char *end, char *str)
214 /* skip any initial control characters or spaces */
215 while ((c = (unsigned char) *s) &&
217 (iscntrl(c) || isspace(c)))
223 /* compact repeated control characters and spaces into a single space */
224 while((c = (unsigned char) *s++) && *start < end)
225 if (!iscntrl(c) && !isspace(c))
228 while ((c = (unsigned char) *s) &&
230 (iscntrl(c) || isspace(c)))
239 static char *lmonth[] = { "January", "February","March", "April",
240 "May", "June", "July", "August",
241 "September","October", "November","December" };
244 get_x400_friendly (char *mbox, char *buffer, int buffer_len)
246 char given[BUFSIZ], surname[BUFSIZ];
255 if (get_x400_comp (mbox, "/PN=", buffer, buffer_len)) {
256 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
262 if (!get_x400_comp (mbox, "/S=", surname, sizeof(surname)))
265 if (get_x400_comp (mbox, "/G=", given, sizeof(given)))
266 snprintf (buffer, buffer_len, "%s %s", given, surname);
268 snprintf (buffer, buffer_len, "%s", surname);
274 get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len)
279 if ((idx = stringdex (key, mbox)) < 0
280 || !(cp = strchr(mbox += idx + strlen (key), '/')))
283 snprintf (buffer, buffer_len, "%*.*s", (int)(cp - mbox), (int)(cp - mbox), mbox);
288 fmt_scan (struct format *format, char *scanl, int width, int *dat)
292 char *savestr = NULL;
293 unsigned char *str = NULL;
294 char buffer[BUFSIZ], buffer2[BUFSIZ];
304 ep = scanl + width - 1;
306 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
307 switch (fmt->f_type) {
310 fmt->f_comp->c_flags &= ~CF_PARSED;
317 switch (fmt->f_type) {
320 cpstripped (&cp, ep, fmt->f_comp->c_text);
323 cptrimmed (&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
328 while( (c = *sp++) && cp < ep)
337 ljust++; /* XXX should do something with this */
339 while( (c = *sp++) && --i >= 0 && cp < ep)
341 while( --i >= 0 && cp < ep)
346 cpstripped (&cp, ep, str);
349 cptrimmed (&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
353 while ((c = *sp++) && cp < ep)
357 adios (NULL, "internal error (FT_STRFW)");
360 n = snprintf(cp, ep - cp + 1, "%d", value);
369 cpnumber (&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
380 if (!(value = (str && *str))) {
387 if (!(value = (str == NULL || *str == 0))) {
394 if (value != fmt->f_value) {
401 if (value == fmt->f_value) {
408 if (value <= fmt->f_value) {
415 if (!(value = (str && match (str, fmt->f_text)))) {
423 value = match (str, fmt->f_text);
429 if (!(value = (str && uprf (str, fmt->f_text)))) {
436 value = uprf (str, fmt->f_text);
440 value = (str != NULL && *str != 0);
444 value = (str == NULL || *str == 0);
448 value = (fmt->f_value == value);
452 value = (fmt->f_value != value);
456 value = (fmt->f_value > value);
467 str = fmt->f_comp->c_text;
473 if (!(str = getenv (fmt->f_text)))
477 if (!(str = context_find (fmt->f_text)))
481 case FT_LS_DECODECOMP:
482 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
485 str = fmt->f_comp->c_text;
489 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
497 strncpy(buffer, str, sizeof(buffer));
498 buffer[sizeof(buffer)-1] = '\0';
500 while (isspace(*str))
503 if ((i = fmt->f_width) < 0) {
508 if (!ljust && i > 0 && strlen(str) > i)
511 xp += strlen(str) - 1;
512 while (xp > str && isspace(*xp))
514 if (ljust && i > 0 && strlen(str) > i)
515 str += strlen(str) - i;
520 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
523 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
526 value = fmt->f_value;
529 value = dat[fmt->f_value];
537 case FT_LV_CHAR_LEFT:
538 value = width - (cp - scanl);
541 value += fmt->f_value;
544 value = fmt->f_value - value;
548 value = value / fmt->f_value;
554 value = value % fmt->f_value;
563 value = fmt->f_comp->c_tws->tw_sec;
566 value = fmt->f_comp->c_tws->tw_min;
569 value = fmt->f_comp->c_tws->tw_hour;
572 value = fmt->f_comp->c_tws->tw_mday;
575 value = fmt->f_comp->c_tws->tw_mon + 1;
578 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
581 str = lmonth[fmt->f_comp->c_tws->tw_mon];
584 str = dtwszone (fmt->f_comp->c_tws);
587 value = fmt->f_comp->c_tws->tw_year;
590 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
592 value = tws->tw_wday;
595 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
597 str = tw_dotw[tws->tw_wday];
600 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
602 str = tw_ldotw[tws->tw_wday];
605 value = fmt->f_comp->c_tws->tw_yday;
608 value = fmt->f_comp->c_tws->tw_zone;
611 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
612 value = dmktime(fmt->f_comp->c_tws);
615 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
616 value = dmktime(fmt->f_comp->c_tws);
617 value = time((time_t *) 0) - value;
620 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
622 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
631 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
637 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
640 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
643 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
647 str = fmt->f_comp->c_mn->m_pers;
650 str = fmt->f_comp->c_mn->m_mbox;
653 str = fmt->f_comp->c_mn->m_host;
656 str = fmt->f_comp->c_mn->m_path;
659 str = fmt->f_comp->c_mn->m_gname;
662 str = fmt->f_comp->c_mn->m_note;
665 str = adrformat( fmt->f_comp->c_mn );
668 value = fmt->f_comp->c_mn->m_type;
671 value = fmt->f_comp->c_mn->m_ingrp;
674 value = fmt->f_comp->c_mn->m_nohost;
678 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
679 str = fmt->f_comp->c_text;
682 if (fmt->f_type == FT_LS_ADDR)
684 if ((str = mn->m_pers) == NULL) {
685 if ((str = mn->m_note)) {
686 strncpy (buffer, str, sizeof(buffer));
687 buffer[sizeof(buffer)-1] = '\0';
691 sp = str + strlen(str) - 1;
700 } else if (!(str = get_x400_friendly (mn->m_mbox,
701 buffer, sizeof(buffer)))) {
703 switch (mn->m_type) {
708 snprintf (buffer, sizeof(buffer), "%s!%s",
709 mn->m_host, mn->m_mbox);
714 snprintf (buffer, sizeof(buffer), "%s@%s",
715 mn->m_mbox, mn->m_host);
727 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
731 strncpy(buffer, str, sizeof(buffer));
732 /* strncpy doesn't NUL-terminate if it fills the buffer */
733 buffer[sizeof(buffer)-1] = '\0';
736 /* we will parse from buffer to buffer2 */
737 n = 0; /* n is the input position in str */
738 m = 0; /* m is the ouput position in buffer2 */
740 while ( str[n] != '\0') {
745 buffer2[m++] = str[n++];
751 buffer2[m++] = str[n++];
762 if ((t = comp->c_tws->tw_clock) == 0)
763 t = dmktime(comp->c_tws);
764 tws = dlocaltime(&t);
770 if ((t = comp->c_tws->tw_clock) == 0)
771 t = dmktime(comp->c_tws);
778 if (comp->c_flags & CF_PARSED)
780 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
782 comp->c_flags &= ~CF_TRUE;
783 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
784 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
785 comp->c_flags = CF_TRUE;
787 comp->c_flags |= CF_PARSED;
791 /* hook for custom address list formatting (see replsbr.c) */
792 str = formataddr (savestr, str);
796 /* The same as formataddr, but doesn't do duplicate suppression */
797 str = concataddr (savestr, str);
801 /* output the str register as an address component,
802 * splitting it into multiple lines if necessary. The
803 * value reg. contains the max line length. The lit.
804 * field may contain a string to prepend to the result
810 int indent, wid, len;
816 indent = strlen (sp);
818 while( (c = *sp++) && cp < ep)
821 /* try to break at a comma; failing that, break at a
824 lastb = 0; sp = lp + wid;
825 while (sp > lp && (c = *--sp) != ',') {
826 if (! lastb && isspace(c))
830 if (! (sp = lastb)) {
832 while (*sp && *sp != ',' && !isspace(*sp))
839 while (cp < ep && lp <= sp)
846 for (i=indent; cp < ep && i > 0; i--)
850 cpstripped (&cp, ep, lp);
856 if (comp->c_flags & CF_PARSED)
858 if (comp->c_mn != &fmt_mnull)
860 if ((sp = comp->c_text) && (sp = getname(sp)) &&
861 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
865 comp->c_flags |= CF_PARSED;
867 while (getname("")) /* XXX */
869 comp->c_mn = &fmt_mnull;
875 * if there's no component, we say true. Otherwise we
876 * say "true" only if we can parse the address and it
877 * matches one of our addresses.
880 if (comp->c_mn != &fmt_mnull)
882 if ((sp = comp->c_text) && (sp = getname(sp)) &&
883 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
886 comp->c_flags |= CF_TRUE;
888 comp->c_flags &= ~CF_TRUE;
889 while ((sp = getname(sp)))
890 if ((comp->c_flags & CF_TRUE) == 0 &&
891 (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
893 comp->c_flags |= CF_TRUE;
895 while (getname("")) /* XXX */
897 if (comp->c_text == 0)
898 comp->c_flags |= CF_TRUE;
900 comp->c_flags &= ~CF_TRUE;
901 comp->c_mn = &fmt_mnull;
907 /* If we're working on a folder (as opposed to a file), add the
908 * current msg to sequence given in literal field. Don't
909 * disturb string or value registers.
911 if (fmt_current_folder)
912 seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
923 return ((struct format *)0);
927 while (fmt->f_type != FT_DONE)
932 return (fmt->f_value ? ++fmt : (struct format *) 0);