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.
9 * This is the engine that processes the format instructions created by
10 * fmt_compile (found in fmt_compile.c).
14 #include <h/addrsbr.h>
15 #include <h/fmt_scan.h>
17 #include <h/fmt_compile.h>
19 #ifdef HAVE_SYS_TIME_H
20 # include <sys/time.h>
23 #ifdef MULTIBYTE_SUPPORT
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 = { NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0,
39 static int match (char *, char *);
40 static char *get_x400_friendly (char *, char *, int);
41 static int get_x400_comp (char *, char *, char *, int);
45 * test if string "sub" appears anywhere in
46 * string "str" (case insensitive).
50 match (char *str, char *sub)
57 c1 = (isalpha(c1) && isupper(c1)) ? tolower(c1) : c1;
58 while ((c2 = *str++) && c1 != ((isalpha(c2) && isupper(c2)) ? tolower(c2) : c2))
62 s1 = sub + 1; s2 = str;
63 while ((c1 = *s1++) && ((isalpha(c1) && isupper(c1)) ? tolower(c1) : c1) == ((isalpha(c2 =*s2++) && isupper(c2)) ? tolower(c2) : c2))
70 while ((c2 = *str++) && (c1 | 040) != (c2 | 040))
74 s1 = sub + 1; s2 = str;
75 while ((c1 = *s1++) && (c1 | 040) == (*s2++ | 040))
85 * copy a number to the destination subject to a maximum width
88 cpnumber(char **dest, int num, unsigned int wid, char fill, size_t n) {
101 *--sp = (i % 10) + '0';
103 } while (i > 0 && sp > cp);
106 else if ((num) < 0 && sp > cp)
116 * copy string from str to dest padding with the fill character to a size
117 * of wid characters. if wid is negative, the string is right aligned
118 * no more than n bytes are copied
121 cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
122 int remaining; /* remaining output width available */
124 int end; /* number of input bytes remaining in str */
125 #ifdef MULTIBYTE_SUPPORT
126 int char_len; /* bytes in current character */
130 char *sp; /* current position in source string */
131 char *cp = *dest; /* current position in destination string */
132 char *ep = cp + n; /* end of destination buffer */
137 if ((remaining = (wid)) < 0) {
138 remaining = -remaining;
142 mbtowc(NULL, NULL, 0); /* reset shift state */
144 while (*sp && remaining > 0 && end > 0) {
145 #ifdef MULTIBYTE_SUPPORT
146 char_len = mbtowc(&wide_char, sp, end);
147 if (char_len <= 0 || (cp + char_len > ep))
152 if (iswcntrl(wide_char) || iswspace(wide_char)) {
157 /* isnctrl(), etc., take an int argument. Cygwin's ctype.h
158 intentionally warns if they are passed a char. */
160 if (iscntrl(c) || isspace(c)) {
173 #ifdef MULTIBYTE_SUPPORT
174 w = wcwidth(wide_char);
175 if (w >= 0 && remaining >= w) {
176 strncpy(cp, sp, char_len);
189 if (cp + remaining > ep)
193 /* copy string to the right */
194 while (--cp >= *dest)
195 *(cp + remaining) = *cp;
196 /* add padding at the beginning */
198 for (c=remaining; c>0; c--)
203 /* pad remaining space */
204 while (remaining-- > 0 && cp < ep)
211 cpstripped (char **start, char *end, char *str)
219 /* skip any initial control characters or spaces */
220 while ((c = (unsigned char) *s) &&
222 (iscntrl(c) || isspace(c)))
228 /* compact repeated control characters and spaces into a single space */
229 while((c = (unsigned char) *s++) && *start < end)
230 if (!iscntrl(c) && !isspace(c))
233 while ((c = (unsigned char) *s) &&
235 (iscntrl(c) || isspace(c)))
244 static char *lmonth[] = { "January", "February","March", "April",
245 "May", "June", "July", "August",
246 "September","October", "November","December" };
249 get_x400_friendly (char *mbox, char *buffer, int buffer_len)
251 char given[BUFSIZ], surname[BUFSIZ];
260 if (get_x400_comp (mbox, "/PN=", buffer, buffer_len)) {
261 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
267 if (!get_x400_comp (mbox, "/S=", surname, sizeof(surname)))
270 if (get_x400_comp (mbox, "/G=", given, sizeof(given)))
271 snprintf (buffer, buffer_len, "%s %s", given, surname);
273 snprintf (buffer, buffer_len, "%s", surname);
279 get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len)
284 if ((idx = stringdex (key, mbox)) < 0
285 || !(cp = strchr(mbox += idx + strlen (key), '/')))
288 snprintf (buffer, buffer_len, "%*.*s", (int)(cp - mbox), (int)(cp - mbox), mbox);
293 fmt_scan (struct format *format, char *scanl, int width, int *dat)
297 char *savestr = NULL;
298 unsigned char *str = NULL;
299 char buffer[BUFSIZ], buffer2[BUFSIZ];
309 ep = scanl + width - 1;
311 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
312 switch (fmt->f_type) {
315 fmt->f_comp->c_flags &= ~CF_PARSED;
320 case FT_LS_DECODECOMP:
322 * Trim these components of any newlines.
324 * But don't trim the "body" and "text" components.
329 if (! (comp->c_flags & CF_TRIMMED) && comp->c_text) {
330 i = strlen(comp->c_text);
331 if (comp->c_text[i - 1] == '\n' &&
332 strcmp(comp->c_name, "body") != 0 &&
333 strcmp(comp->c_name, "text") != 0)
334 comp->c_text[i - 1] = '\0';
335 comp->c_flags |= CF_TRIMMED;
343 switch (fmt->f_type) {
346 cpstripped (&cp, ep, fmt->f_comp->c_text);
349 cptrimmed (&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
354 while( (c = *sp++) && cp < ep)
363 ljust++; /* XXX should do something with this */
365 while( (c = *sp++) && --i >= 0 && cp < ep)
367 while( --i >= 0 && cp < ep)
372 cpstripped (&cp, ep, str);
375 cptrimmed (&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
379 while ((c = *sp++) && cp < ep)
383 adios (NULL, "internal error (FT_STRFW)");
386 n = snprintf(cp, ep - cp + 1, "%d", value);
395 cpnumber (&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
406 if (!(value = (str && *str))) {
413 if (!(value = (str == NULL || *str == 0))) {
420 if (value != fmt->f_value) {
427 if (value == fmt->f_value) {
434 if (value <= fmt->f_value) {
441 if (!(value = (str && match (str, fmt->f_text)))) {
449 value = match (str, fmt->f_text);
455 if (!(value = (str && uprf (str, fmt->f_text)))) {
462 value = uprf (str, fmt->f_text);
466 value = (str != NULL && *str != 0);
470 value = (str == NULL || *str == 0);
474 value = (fmt->f_value == value);
478 value = (fmt->f_value != value);
482 value = (fmt->f_value > value);
493 str = fmt->f_comp->c_text;
499 if (!(str = getenv (fmt->f_text)))
503 if (!(str = context_find (fmt->f_text)))
507 case FT_LS_DECODECOMP:
508 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
511 str = fmt->f_comp->c_text;
515 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
523 strncpy(buffer, str, sizeof(buffer));
524 buffer[sizeof(buffer)-1] = '\0';
526 while (isspace(*str))
529 if ((i = fmt->f_width) < 0) {
534 if (!ljust && i > 0 && (int) strlen(str) > i)
537 xp += strlen(str) - 1;
538 while (xp > str && isspace(*xp))
540 if (ljust && i > 0 && (int) strlen(str) > i)
541 str += strlen(str) - i;
546 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
549 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
552 value = fmt->f_value;
555 value = dat[fmt->f_value];
563 case FT_LV_CHAR_LEFT:
564 value = width - (cp - scanl);
567 value += fmt->f_value;
570 value = fmt->f_value - value;
574 value = value / fmt->f_value;
580 value = value % fmt->f_value;
589 value = fmt->f_comp->c_tws->tw_sec;
592 value = fmt->f_comp->c_tws->tw_min;
595 value = fmt->f_comp->c_tws->tw_hour;
598 value = fmt->f_comp->c_tws->tw_mday;
601 value = fmt->f_comp->c_tws->tw_mon + 1;
604 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
607 str = lmonth[fmt->f_comp->c_tws->tw_mon];
610 str = dtwszone (fmt->f_comp->c_tws);
613 value = fmt->f_comp->c_tws->tw_year;
616 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
618 value = tws->tw_wday;
621 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
623 str = tw_dotw[tws->tw_wday];
626 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
628 str = tw_ldotw[tws->tw_wday];
631 value = fmt->f_comp->c_tws->tw_yday;
634 value = fmt->f_comp->c_tws->tw_zone;
637 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
638 value = dmktime(fmt->f_comp->c_tws);
641 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
642 value = dmktime(fmt->f_comp->c_tws);
643 value = time((time_t *) 0) - value;
646 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
648 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
657 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
663 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
666 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
669 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
673 str = fmt->f_comp->c_mn->m_pers;
676 str = fmt->f_comp->c_mn->m_mbox;
679 str = fmt->f_comp->c_mn->m_host;
682 str = fmt->f_comp->c_mn->m_path;
685 str = fmt->f_comp->c_mn->m_gname;
688 str = fmt->f_comp->c_mn->m_note;
691 str = adrformat( fmt->f_comp->c_mn );
694 value = fmt->f_comp->c_mn->m_type;
697 value = fmt->f_comp->c_mn->m_ingrp;
700 value = fmt->f_comp->c_mn->m_nohost;
704 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
705 str = fmt->f_comp->c_text;
708 if (fmt->f_type == FT_LS_ADDR)
710 if ((str = mn->m_pers) == NULL) {
711 if ((str = mn->m_note)) {
712 strncpy (buffer, str, sizeof(buffer));
713 buffer[sizeof(buffer)-1] = '\0';
717 sp = str + strlen(str) - 1;
726 } else if (!(str = get_x400_friendly (mn->m_mbox,
727 buffer, sizeof(buffer)))) {
729 switch (mn->m_type) {
734 snprintf (buffer, sizeof(buffer), "%s!%s",
735 mn->m_host, mn->m_mbox);
740 snprintf (buffer, sizeof(buffer), "%s@%s",
741 mn->m_mbox, mn->m_host);
753 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
757 strncpy(buffer, str, sizeof(buffer));
758 /* strncpy doesn't NUL-terminate if it fills the buffer */
759 buffer[sizeof(buffer)-1] = '\0';
762 /* we will parse from buffer to buffer2 */
763 n = 0; /* n is the input position in str */
764 m = 0; /* m is the ouput position in buffer2 */
766 while ( str[n] != '\0') {
771 buffer2[m++] = str[n++];
777 buffer2[m++] = str[n++];
788 if ((t = comp->c_tws->tw_clock) == 0)
789 t = dmktime(comp->c_tws);
790 tws = dlocaltime(&t);
796 if ((t = comp->c_tws->tw_clock) == 0)
797 t = dmktime(comp->c_tws);
804 if (comp->c_flags & CF_PARSED)
806 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
808 comp->c_flags &= ~CF_TRUE;
809 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
810 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
811 comp->c_flags = CF_TRUE;
813 comp->c_flags |= CF_PARSED;
817 /* hook for custom address list formatting (see replsbr.c) */
818 str = formataddr (savestr, str);
822 /* The same as formataddr, but doesn't do duplicate suppression */
823 str = concataddr (savestr, str);
827 /* output the str register as an address component,
828 * splitting it into multiple lines if necessary. The
829 * value reg. contains the max line length. The lit.
830 * field may contain a string to prepend to the result
836 int indent, wid, len;
842 indent = strlen (sp);
845 adios(NULL, "putaddr -- num register (%d) must be greater "
846 "than label width (%d)", value, indent);
848 while( (c = *sp++) && cp < ep)
851 /* try to break at a comma; failing that, break at a
854 lastb = 0; sp = lp + wid;
855 while (sp > lp && (c = *--sp) != ',') {
856 if (! lastb && isspace(c))
860 if (! (sp = lastb)) {
862 while (*sp && *sp != ',' && !isspace(*sp))
869 while (cp < ep && lp <= sp)
876 for (i=indent; cp < ep && i > 0; i--)
880 cpstripped (&cp, ep, lp);
886 if (comp->c_flags & CF_PARSED)
888 if (comp->c_mn != &fmt_mnull)
890 if ((sp = comp->c_text) && (sp = getname(sp)) &&
891 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
895 comp->c_flags |= CF_PARSED;
897 while (getname("")) /* XXX */
899 comp->c_mn = &fmt_mnull;
905 * if there's no component, we say true. Otherwise we
906 * say "true" only if we can parse the address and it
907 * matches one of our addresses.
910 if (comp->c_mn != &fmt_mnull)
912 if ((sp = comp->c_text) && (sp = getname(sp)) &&
913 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
916 comp->c_flags |= CF_TRUE;
918 comp->c_flags &= ~CF_TRUE;
919 while ((sp = getname(sp)))
920 if ((comp->c_flags & CF_TRUE) == 0 &&
921 (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
923 comp->c_flags |= CF_TRUE;
925 while (getname("")) /* XXX */
927 if (comp->c_text == 0)
928 comp->c_flags |= CF_TRUE;
930 comp->c_flags &= ~CF_TRUE;
931 comp->c_mn = &fmt_mnull;
937 /* If we're working on a folder (as opposed to a file), add the
938 * current msg to sequence given in literal field. Don't
939 * disturb string or value registers.
941 if (fmt_current_folder)
942 seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
953 return ((struct format *)0);
957 while (fmt->f_type != FT_DONE)
962 return (fmt->f_value ? ++fmt : (struct format *) 0);