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.
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
13 #include <h/fmt_compile.h>
15 #ifdef TIME_WITH_SYS_TIME
16 # include <sys/time.h>
19 # ifdef TM_IN_SYS_TIME
20 # include <sys/time.h>
25 #ifdef MULTIBYTE_SUPPORT
32 extern char *formataddr(); /* hook for custom address formatting */
35 struct msgs *fmt_current_folder; /* current folder (set by main program) */
38 extern int fmt_norm; /* defined in sbr/fmt_def.c = AD_NAME */
39 struct mailname fmt_mnull;
44 static int match(char *, char *);
45 static char *get_x400_friendly(char *, char *, int);
46 static int get_x400_comp(char *, char *, char *, int);
50 ** test if string "sub" appears anywhere in
51 ** string "str" (case insensitive).
55 match(char *str, char *sub)
61 while ((c2 = *str++) && tolower(c1) != tolower(c2))
65 s1 = sub + 1; s2 = str;
66 while ((c1 = *s1++) && tolower(c1) == tolower(*s2++))
75 ** copy a number to the destination subject to a maximum width
78 cpnumber(char **dest, int num, unsigned int wid, char fill, size_t n) {
91 *--sp = (i % 10) + '0';
93 } while (i > 0 && sp > cp);
96 else if ((num) < 0 && sp > cp)
106 ** copy string from str to dest padding with the fill character to a size
107 ** of wid characters. if wid is negative, the string is right aligned
108 ** no more than n bytes are copied
111 cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
112 int remaining; /* remaining output width available */
114 int end; /* number of input bytes remaining in str */
115 #ifdef MULTIBYTE_SUPPORT
116 int char_len; /* bytes in current character */
119 char *sp; /* current position in source string */
120 char *cp = *dest; /* current position in destination string */
121 char *ep = cp + n; /* end of destination buffer */
126 if ((remaining = (wid)) < 0) {
127 remaining = -remaining;
131 mbtowc(NULL, NULL, 0); /* reset shift state */
133 while (*sp && remaining > 0 && end > 0) {
134 #ifdef MULTIBYTE_SUPPORT
135 char_len = mbtowc(&wide_char, sp, end);
136 if (char_len <= 0 || (cp + char_len > ep))
141 if (iswcntrl(wide_char) || iswspace(wide_char)) {
145 if (iscntrl(*sp) || isspace(*sp)) {
158 #ifdef MULTIBYTE_SUPPORT
159 w = wcwidth(wide_char);
160 if (w >= 0 && remaining >= w) {
161 strncpy(cp, sp, char_len);
174 if (cp + remaining > ep)
178 /* copy string to the right */
179 while (--cp >= *dest)
180 *(cp + remaining) = *cp;
181 /* add padding at the beginning */
183 for (c=remaining; c>0; c--)
188 /* pad remaining space */
189 while (remaining-- > 0 && cp < ep)
196 cpstripped(char **start, char *end, char *str)
204 /* skip any initial control characters or spaces */
205 while ((c = (unsigned char) *s) && (iscntrl(c) || isspace(c)))
208 /* compact repeated control characters and spaces into a single space */
209 while((c = (unsigned char) *s++) && *start < end)
210 if (!iscntrl(c) && !isspace(c))
213 while ((c = (unsigned char) *s) &&
214 (iscntrl(c) || isspace(c)))
220 static char *lmonth[] = {
221 "January", "February", "March", "April",
222 "May", "June", "July", "August",
223 "September", "October", "November", "December"
227 get_x400_friendly(char *mbox, char *buffer, int buffer_len)
229 char given[BUFSIZ], surname[BUFSIZ];
238 if (get_x400_comp(mbox, "/PN=", buffer, buffer_len)) {
239 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
245 if (!get_x400_comp(mbox, "/S=", surname, sizeof(surname)))
248 if (get_x400_comp(mbox, "/G=", given, sizeof(given)))
249 snprintf(buffer, buffer_len, "%s %s", given, surname);
251 snprintf(buffer, buffer_len, "%s", surname);
257 get_x400_comp(char *mbox, char *key, char *buffer, int buffer_len)
262 if ((idx = stringdex(key, mbox)) < 0
263 || !(cp = strchr(mbox += idx + strlen(key), '/')))
266 snprintf(buffer, buffer_len, "%*.*s", (int)(cp - mbox), (int)(cp - mbox), mbox);
271 fmt_scan(struct format *format, char *scanl, int width, int *dat)
275 char *savestr = NULL;
276 unsigned char *str = NULL;
277 char buffer[BUFSIZ], buffer2[BUFSIZ];
287 ep = scanl + width - 1;
289 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
290 switch (fmt->f_type) {
293 fmt->f_comp->c_flags &= ~CF_PARSED;
300 switch (fmt->f_type) {
303 cpstripped(&cp, ep, fmt->f_comp->c_text);
306 cptrimmed(&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
311 while( (c = *sp++) && cp < ep)
320 ljust++; /* XXX should do something with this */
322 while( (c = *sp++) && --i >= 0 && cp < ep)
324 while( --i >= 0 && cp < ep)
329 cpstripped(&cp, ep, str);
332 cptrimmed(&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
335 adios(NULL, "internal error (FT_STRFW)");
338 n = snprintf(cp, ep - cp + 1, "%d", value);
347 cpnumber(&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
358 if (!(value = (str && *str))) {
365 if (!(value = (str == NULL || *str == 0))) {
372 if (value != fmt->f_value) {
379 if (value == fmt->f_value) {
386 if (value <= fmt->f_value) {
393 if (!(value = (str && match(str, fmt->f_text)))) {
401 value = match(str, fmt->f_text);
407 if (!(value = (str && uprf(str, fmt->f_text)))) {
414 value = uprf(str, fmt->f_text);
418 value = (str != NULL && *str != 0);
422 value = (str == NULL || *str == 0);
426 value = (fmt->f_value == value);
430 value = (fmt->f_value != value);
434 value = (fmt->f_value > value);
445 str = fmt->f_comp->c_text;
451 if (!(str = getenv(fmt->f_text)))
455 if (!(str = context_find(fmt->f_text)))
459 case FT_LS_DECODECOMP:
460 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
463 str = fmt->f_comp->c_text;
467 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
475 strncpy(buffer, str, sizeof(buffer));
476 buffer[sizeof(buffer)-1] = '\0';
478 while (isspace(*str))
481 if ((i = fmt->f_width) < 0) {
486 if (!ljust && i > 0 && strlen(str) > i)
489 xp += strlen(str) - 1;
490 while (xp > str && isspace(*xp))
492 if (ljust && i > 0 && strlen(str) > i)
493 str += strlen(str) - i;
498 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
501 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
504 value = fmt->f_value;
507 value = dat[fmt->f_value];
515 case FT_LV_CHAR_LEFT:
516 value = width - (cp - scanl);
519 value += fmt->f_value;
522 value = fmt->f_value - value;
526 value = value / fmt->f_value;
532 value = value % fmt->f_value;
541 value = fmt->f_comp->c_tws->tw_sec;
544 value = fmt->f_comp->c_tws->tw_min;
547 value = fmt->f_comp->c_tws->tw_hour;
550 value = fmt->f_comp->c_tws->tw_mday;
553 value = fmt->f_comp->c_tws->tw_mon + 1;
556 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
559 str = lmonth[fmt->f_comp->c_tws->tw_mon];
562 str = dtwszone(fmt->f_comp->c_tws);
565 value = fmt->f_comp->c_tws->tw_year;
568 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
570 value = tws->tw_wday;
573 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
575 str = tw_dotw[tws->tw_wday];
578 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
580 str = tw_ldotw[tws->tw_wday];
583 value = fmt->f_comp->c_tws->tw_yday;
586 value = fmt->f_comp->c_tws->tw_zone;
589 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
590 value = dmktime(fmt->f_comp->c_tws);
593 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
594 value = dmktime(fmt->f_comp->c_tws);
595 value = time((time_t *) 0) - value;
598 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
600 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
609 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
615 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
618 str = dasctime(fmt->f_comp->c_tws , TW_ZONE);
621 str = dasctime(fmt->f_comp->c_tws, TW_NULL);
625 str = fmt->f_comp->c_mn->m_pers;
628 str = fmt->f_comp->c_mn->m_mbox;
631 str = fmt->f_comp->c_mn->m_host;
634 str = fmt->f_comp->c_mn->m_path;
637 str = fmt->f_comp->c_mn->m_gname;
640 str = fmt->f_comp->c_mn->m_note;
643 str = adrformat( fmt->f_comp->c_mn );
646 value = fmt->f_comp->c_mn->m_type;
649 value = fmt->f_comp->c_mn->m_ingrp;
652 value = fmt->f_comp->c_mn->m_nohost;
656 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
657 str = fmt->f_comp->c_text;
660 if (fmt->f_type == FT_LS_ADDR)
662 if ((str = mn->m_pers) == NULL) {
663 if ((str = mn->m_note)) {
664 strncpy(buffer, str, sizeof(buffer));
665 buffer[sizeof(buffer)-1] = '\0';
669 sp = str + strlen(str) - 1;
678 } else if (!(str = get_x400_friendly(mn->m_mbox,
679 buffer, sizeof(buffer)))) {
681 switch (mn->m_type) {
686 snprintf(buffer, sizeof(buffer), "%s!%s",
687 mn->m_host, mn->m_mbox);
692 snprintf(buffer, sizeof(buffer), "%s@%s",
693 mn->m_mbox, mn->m_host);
704 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
708 strncpy(buffer, str, sizeof(buffer));
709 /* strncpy doesn't NUL-terminate if it fills the buffer */
710 buffer[sizeof(buffer)-1] = '\0';
713 /* we will parse from buffer to buffer2 */
714 n = 0; /* n is the input position in str */
715 m = 0; /* m is the ouput position in buffer2 */
717 while ( str[n] != '\0') {
722 buffer2[m++] = str[n++];
728 buffer2[m++] = str[n++];
739 if ((t = comp->c_tws->tw_clock) == 0)
740 t = dmktime(comp->c_tws);
741 tws = dlocaltime(&t);
747 if ((t = comp->c_tws->tw_clock) == 0)
748 t = dmktime(comp->c_tws);
755 if (comp->c_flags & CF_PARSED)
757 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
759 comp->c_flags &= ~CF_TRUE;
760 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
761 memset((char *) comp->c_tws, 0, sizeof *comp->c_tws);
762 comp->c_flags = CF_TRUE;
764 comp->c_flags |= CF_PARSED;
769 ** hook for custom address list formatting
772 str = formataddr(savestr, str);
777 ** output the str register as an address component,
778 ** splitting it into multiple lines if necessary. The
779 ** value reg. contains the max line length. The lit.
780 ** field may contain a string to prepend to the result
786 int indent, wid, len;
794 while( (c = *sp++) && cp < ep)
797 /* try to break at a comma; failing that,
800 lastb = 0; sp = lp + wid;
801 while (sp > lp && (c = *--sp) != ',') {
802 if (! lastb && isspace(c))
806 if (! (sp = lastb)) {
808 while (*sp && *sp != ',' && !isspace(*sp))
815 while (cp < ep && lp <= sp)
822 for (i=indent; cp < ep && i > 0; i--)
826 cpstripped(&cp, ep, lp);
832 if (comp->c_flags & CF_PARSED)
834 if (comp->c_mn != &fmt_mnull)
836 if ((sp = comp->c_text) && (sp = getname(sp)) &&
837 (mn = getm(sp, NULL, 0, fmt_norm, NULL))) {
841 comp->c_flags |= CF_PARSED;
843 while (getname("")) /* XXX */
845 comp->c_mn = &fmt_mnull;
851 ** if there's no component, we say true. Otherwise we
852 ** say "true" only if we can parse the address and it
853 ** matches one of our addresses.
856 if (comp->c_mn != &fmt_mnull)
858 if ((sp = comp->c_text) && (sp = getname(sp)) &&
859 (mn = getm(sp, NULL, 0, AD_NAME, NULL))) {
862 comp->c_flags |= CF_TRUE;
864 comp->c_flags &= ~CF_TRUE;
865 while ((sp = getname(sp)))
866 if ((comp->c_flags & CF_TRUE) == 0 &&
867 (mn = getm(sp, NULL, 0, AD_NAME, NULL)))
869 comp->c_flags |= CF_TRUE;
871 while (getname("")) /* XXX */
873 if (comp->c_text == 0)
874 comp->c_flags |= CF_TRUE;
876 comp->c_flags &= ~CF_TRUE;
877 comp->c_mn = &fmt_mnull;
884 ** If we're working on a folder (as opposed to a
885 ** file), add the current msg to sequence given
886 ** in literal field. Don't disturb string or
889 if (fmt_current_folder)
890 seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
901 return ((struct format *)0);
905 while (fmt->f_type != FT_DONE)
910 return (fmt->f_value ? ++fmt : (struct format *) 0);