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>
27 struct msgs *fmt_current_folder; /* current folder (set by main program) */
30 extern int fmt_norm; /* defined in sbr/fmt_def.c = AD_NAME */
31 struct mailname fmt_mnull = { NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0,
37 static int match (char *, char *);
38 static char *get_x400_friendly (char *, char *, int);
39 static int get_x400_comp (char *, char *, char *, int);
43 * test if string "sub" appears anywhere in
44 * string "str" (case insensitive).
48 match (char *str, char *sub)
55 c1 = (isalpha(c1) && isupper(c1)) ? tolower(c1) : c1;
56 while ((c2 = *str++) && c1 != ((isalpha(c2) && isupper(c2)) ? tolower(c2) : c2))
60 s1 = sub + 1; s2 = str;
61 while ((c1 = *s1++) && ((isalpha(c1) && isupper(c1)) ? tolower(c1) : c1) == ((isalpha(c2 =*s2++) && isupper(c2)) ? tolower(c2) : c2))
68 while ((c2 = *str++) && (c1 | 040) != (c2 | 040))
72 s1 = sub + 1; s2 = str;
73 while ((c1 = *s1++) && (c1 | 040) == (*s2++ | 040))
83 * copy a number to the destination subject to a maximum width
86 cpnumber(char **dest, int num, unsigned int wid, char fill, size_t n) {
99 *--sp = (i % 10) + '0';
101 } while (i > 0 && sp > cp);
104 else if ((num) < 0 && sp > cp)
114 * copy string from str to dest padding with the fill character to a size
115 * of wid characters. if wid is negative, the string is right aligned
116 * no more than n bytes are copied
119 cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
120 int remaining; /* remaining output width available */
122 int end; /* number of input bytes remaining in str */
123 int char_len; /* bytes in current character */
126 char *sp; /* current position in source string */
127 char *cp = *dest; /* current position in destination string */
128 char *ep = cp + n; /* end of destination buffer */
133 if ((remaining = (wid)) < 0) {
134 remaining = -remaining;
138 mbtowc(NULL, NULL, 0); /* reset shift state */
140 while (*sp && remaining > 0 && end > 0) {
141 char_len = mbtowc(&wide_char, sp, end);
142 if (char_len <= 0 || (cp + char_len > ep))
147 if (iswcntrl(wide_char) || iswspace(wide_char)) {
159 w = wcwidth(wide_char);
160 if (w >= 0 && remaining >= w) {
161 strncpy(cp, sp, char_len);
170 if (cp + remaining > ep)
174 /* copy string to the right */
175 while (--cp >= *dest)
176 *(cp + remaining) = *cp;
177 /* add padding at the beginning */
179 for (c=remaining; c>0; c--)
184 /* pad remaining space */
185 while (remaining-- > 0 && cp < ep)
192 cpstripped (char **start, char *end, char *str)
200 /* skip any initial control characters or spaces */
201 while ((c = (unsigned char) *s) &&
203 (iscntrl(c) || isspace(c)))
209 /* compact repeated control characters and spaces into a single space */
210 while((c = (unsigned char) *s++) && *start < end)
211 if (!iscntrl(c) && !isspace(c))
214 while ((c = (unsigned char) *s) &&
216 (iscntrl(c) || isspace(c)))
225 static char *lmonth[] = { "January", "February","March", "April",
226 "May", "June", "July", "August",
227 "September","October", "November","December" };
230 get_x400_friendly (char *mbox, char *buffer, int buffer_len)
232 char given[BUFSIZ], surname[BUFSIZ];
241 if (get_x400_comp (mbox, "/PN=", buffer, buffer_len)) {
242 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
248 if (!get_x400_comp (mbox, "/S=", surname, sizeof(surname)))
251 if (get_x400_comp (mbox, "/G=", given, sizeof(given)))
252 snprintf (buffer, buffer_len, "%s %s", given, surname);
254 snprintf (buffer, buffer_len, "%s", surname);
260 get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len)
265 if ((idx = stringdex (key, mbox)) < 0
266 || !(cp = strchr(mbox += idx + strlen (key), '/')))
269 snprintf (buffer, buffer_len, "%*.*s", (int)(cp - mbox), (int)(cp - mbox), mbox);
274 fmt_scan (struct format *format, char *scanl, int width, int *dat)
278 char *savestr = NULL;
279 unsigned char *str = NULL;
280 char buffer[BUFSIZ], buffer2[BUFSIZ];
290 ep = scanl + width - 1;
292 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
293 switch (fmt->f_type) {
296 fmt->f_comp->c_flags &= ~CF_PARSED;
303 switch (fmt->f_type) {
306 cpstripped (&cp, ep, fmt->f_comp->c_text);
309 cptrimmed (&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
314 while( (c = *sp++) && cp < ep)
323 ljust++; /* XXX should do something with this */
325 while( (c = *sp++) && --i >= 0 && cp < ep)
327 while( --i >= 0 && cp < ep)
332 cpstripped (&cp, ep, str);
335 cptrimmed (&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
339 while ((c = *sp++) && cp < ep)
343 adios (NULL, "internal error (FT_STRFW)");
346 n = snprintf(cp, ep - cp + 1, "%d", value);
355 cpnumber (&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
366 if (!(value = (str && *str))) {
373 if (!(value = (str == NULL || *str == 0))) {
380 if (value != fmt->f_value) {
387 if (value == fmt->f_value) {
394 if (value <= fmt->f_value) {
401 if (!(value = (str && match (str, fmt->f_text)))) {
409 value = match (str, fmt->f_text);
415 if (!(value = (str && uprf (str, fmt->f_text)))) {
422 value = uprf (str, fmt->f_text);
426 value = (str != NULL && *str != 0);
430 value = (str == NULL || *str == 0);
434 value = (fmt->f_value == value);
438 value = (fmt->f_value != value);
442 value = (fmt->f_value > value);
453 str = fmt->f_comp->c_text;
459 if (!(str = getenv (fmt->f_text)))
463 if (!(str = context_find (fmt->f_text)))
467 case FT_LS_DECODECOMP:
468 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
471 str = fmt->f_comp->c_text;
475 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
483 strncpy(buffer, str, sizeof(buffer));
484 buffer[sizeof(buffer)-1] = '\0';
486 while (isspace(*str))
489 if ((i = fmt->f_width) < 0) {
494 if (!ljust && i > 0 && (int) strlen(str) > i)
497 xp += strlen(str) - 1;
498 while (xp > str && isspace(*xp))
500 if (ljust && i > 0 && (int) strlen(str) > i)
501 str += strlen(str) - i;
506 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
509 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
512 value = fmt->f_value;
515 value = dat[fmt->f_value];
523 case FT_LV_CHAR_LEFT:
524 value = width - (cp - scanl);
527 value += fmt->f_value;
530 value = fmt->f_value - value;
534 value = value / fmt->f_value;
540 value = value % fmt->f_value;
549 value = fmt->f_comp->c_tws->tw_sec;
552 value = fmt->f_comp->c_tws->tw_min;
555 value = fmt->f_comp->c_tws->tw_hour;
558 value = fmt->f_comp->c_tws->tw_mday;
561 value = fmt->f_comp->c_tws->tw_mon + 1;
564 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
567 str = lmonth[fmt->f_comp->c_tws->tw_mon];
570 str = dtwszone (fmt->f_comp->c_tws);
573 value = fmt->f_comp->c_tws->tw_year;
576 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
578 value = tws->tw_wday;
581 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
583 str = tw_dotw[tws->tw_wday];
586 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
588 str = tw_ldotw[tws->tw_wday];
591 value = fmt->f_comp->c_tws->tw_yday;
594 value = fmt->f_comp->c_tws->tw_zone;
597 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
598 value = dmktime(fmt->f_comp->c_tws);
601 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
602 value = dmktime(fmt->f_comp->c_tws);
603 value = time((time_t *) 0) - value;
606 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
608 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
617 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
623 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
626 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
629 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
633 str = fmt->f_comp->c_mn->m_pers;
636 str = fmt->f_comp->c_mn->m_mbox;
639 str = fmt->f_comp->c_mn->m_host;
642 str = fmt->f_comp->c_mn->m_path;
645 str = fmt->f_comp->c_mn->m_gname;
648 str = fmt->f_comp->c_mn->m_note;
651 str = adrformat( fmt->f_comp->c_mn );
654 value = fmt->f_comp->c_mn->m_type;
657 value = fmt->f_comp->c_mn->m_ingrp;
660 value = fmt->f_comp->c_mn->m_nohost;
664 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
665 str = fmt->f_comp->c_text;
668 if (fmt->f_type == FT_LS_ADDR)
670 if ((str = mn->m_pers) == NULL) {
671 if ((str = mn->m_note)) {
672 strncpy (buffer, str, sizeof(buffer));
673 buffer[sizeof(buffer)-1] = '\0';
677 sp = str + strlen(str) - 1;
686 } else if (!(str = get_x400_friendly (mn->m_mbox,
687 buffer, sizeof(buffer)))) {
689 switch (mn->m_type) {
694 snprintf (buffer, sizeof(buffer), "%s!%s",
695 mn->m_host, mn->m_mbox);
700 snprintf (buffer, sizeof(buffer), "%s@%s",
701 mn->m_mbox, mn->m_host);
713 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
717 strncpy(buffer, str, sizeof(buffer));
718 /* strncpy doesn't NUL-terminate if it fills the buffer */
719 buffer[sizeof(buffer)-1] = '\0';
722 /* we will parse from buffer to buffer2 */
723 n = 0; /* n is the input position in str */
724 m = 0; /* m is the ouput position in buffer2 */
726 while ( str[n] != '\0') {
731 buffer2[m++] = str[n++];
737 buffer2[m++] = str[n++];
748 if ((t = comp->c_tws->tw_clock) == 0)
749 t = dmktime(comp->c_tws);
750 tws = dlocaltime(&t);
756 if ((t = comp->c_tws->tw_clock) == 0)
757 t = dmktime(comp->c_tws);
764 if (comp->c_flags & CF_PARSED)
766 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
768 comp->c_flags &= ~CF_TRUE;
769 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
770 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
771 comp->c_flags = CF_TRUE;
773 comp->c_flags |= CF_PARSED;
777 /* hook for custom address list formatting (see replsbr.c) */
778 str = formataddr (savestr, str);
782 /* The same as formataddr, but doesn't do duplicate suppression */
783 str = concataddr (savestr, str);
787 /* output the str register as an address component,
788 * splitting it into multiple lines if necessary. The
789 * value reg. contains the max line length. The lit.
790 * field may contain a string to prepend to the result
796 int indent, wid, len;
802 indent = strlen (sp);
805 adios(NULL, "putaddr -- num register (%d) must be greater "
806 "than label width (%d)", value, indent);
808 while( (c = *sp++) && cp < ep)
811 /* try to break at a comma; failing that, break at a
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;
897 /* If we're working on a folder (as opposed to a file), add the
898 * current msg to sequence given in literal field. Don't
899 * disturb string or value registers.
901 if (fmt_current_folder)
902 seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
913 return ((struct format *)0);
917 while (fmt->f_type != FT_DONE)
922 return (fmt->f_value ? ++fmt : (struct format *) 0);