3 * fmt_scan.c -- format string interpretation
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/addrsbr.h>
14 #include <h/fmt_scan.h>
16 #include <h/fmt_compile.h>
18 #ifdef TIME_WITH_SYS_TIME
19 # include <sys/time.h>
22 # ifdef TM_IN_SYS_TIME
23 # include <sys/time.h>
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)
62 c1 = (isalpha(c1) && isupper(c1)) ? tolower(c1) : c1;
63 while ((c2 = *str++) && c1 != ((isalpha(c2) && isupper(c2)) ? tolower(c2) : c2))
67 s1 = sub + 1; s2 = str;
68 while ((c1 = *s1++) && ((isalpha(c1) && isupper(c1)) ? tolower(c1) : c1) == ((isalpha(c2 =*s2++) && isupper(c2)) ? tolower(c2) : c2))
75 while ((c2 = *str++) && (c1 | 040) != (c2 | 040))
79 s1 = sub + 1; s2 = str;
80 while ((c1 = *s1++) && (c1 | 040) == (*s2++ | 040))
90 * copy a number to the destination subject to a maximum width
93 cpnumber(char **dest, int num, unsigned int wid, char fill, size_t n) {
106 *--sp = (i % 10) + '0';
108 } while (i > 0 && sp > cp);
111 else if ((num) < 0 && sp > cp)
121 * copy string from str to dest padding with the fill character to a size
122 * of wid characters. if wid is negative, the string is right aligned
123 * no more than n bytes are copied
126 cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
127 int remaining; /* remaining output width available */
129 int end; /* number of input bytes remaining in str */
130 int char_len; /* bytes in current character */
132 char *sp; /* current position in source string */
133 char *cp = *dest; /* current position in destination string */
134 char *ep = cp + n; /* end of destination buffer */
139 if ((remaining = (wid)) < 0) {
140 remaining = -remaining;
144 mbtowc(NULL, NULL, 0); /* reset shift state */
146 while (*sp && remaining > 0 && end > 0) {
147 char_len = mbtowc(&wide_char, sp, end);
148 if (char_len <= 0 || (cp + char_len > ep))
153 if (iswcntrl(wide_char) || iswspace(wide_char)) {
165 w = wcwidth(wide_char);
166 if (w >= 0 && remaining >= w) {
167 strncpy(cp, sp, char_len);
176 if (cp + remaining > ep)
180 /* copy string to the right */
181 while (--cp >= *dest)
182 *(cp + remaining) = *cp;
183 /* add padding at the beginning */
185 for (c=remaining; c>0; c--)
190 /* pad remaining space */
191 while (remaining-- > 0 && cp < ep)
198 cpstripped (char **start, char *end, char *str)
206 /* skip any initial control characters or spaces */
207 while ((c = (unsigned char) *s) &&
209 (iscntrl(c) || isspace(c)))
215 /* compact repeated control characters and spaces into a single space */
216 while((c = (unsigned char) *s++) && *start < end)
217 if (!iscntrl(c) && !isspace(c))
220 while ((c = (unsigned char) *s) &&
222 (iscntrl(c) || isspace(c)))
231 static char *lmonth[] = { "January", "February","March", "April",
232 "May", "June", "July", "August",
233 "September","October", "November","December" };
236 get_x400_friendly (char *mbox, char *buffer, int buffer_len)
238 char given[BUFSIZ], surname[BUFSIZ];
247 if (get_x400_comp (mbox, "/PN=", buffer, buffer_len)) {
248 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
254 if (!get_x400_comp (mbox, "/S=", surname, sizeof(surname)))
257 if (get_x400_comp (mbox, "/G=", given, sizeof(given)))
258 snprintf (buffer, buffer_len, "%s %s", given, surname);
260 snprintf (buffer, buffer_len, "%s", surname);
266 get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len)
271 if ((idx = stringdex (key, mbox)) < 0
272 || !(cp = strchr(mbox += idx + strlen (key), '/')))
275 snprintf (buffer, buffer_len, "%*.*s", cp - mbox, cp - mbox, mbox);
280 fmt_scan (struct format *format, char *scanl, int width, int *dat)
283 char *savestr, *str = NULL;
284 char buffer[BUFSIZ], buffer2[BUFSIZ];
294 ep = scanl + width - 1;
296 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
297 switch (fmt->f_type) {
300 fmt->f_comp->c_flags &= ~CF_PARSED;
307 switch (fmt->f_type) {
310 cpstripped (&cp, ep, fmt->f_comp->c_text);
313 cptrimmed (&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
318 while( (c = *sp++) && cp < ep)
327 ljust++; /* XXX should do something with this */
329 while( (c = *sp++) && --i >= 0 && cp < ep)
331 while( --i >= 0 && cp < ep)
336 cpstripped (&cp, ep, str);
339 cptrimmed (&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
342 adios (NULL, "internal error (FT_STRFW)");
345 n = snprintf(cp, ep - cp, "%d", value);
349 cpnumber (&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
360 if (!(value = (str && *str))) {
367 if (!(value = (str == NULL || *str == 0))) {
374 if (value != fmt->f_value) {
381 if (value == fmt->f_value) {
388 if (value <= fmt->f_value) {
395 if (!(value = (str && match (str, fmt->f_text)))) {
403 value = match (str, fmt->f_text);
409 if (!(value = (str && uprf (str, fmt->f_text)))) {
416 value = uprf (str, fmt->f_text);
420 value = (str != NULL && *str != 0);
424 value = (str == NULL || *str == 0);
428 value = (fmt->f_value == value);
432 value = (fmt->f_value != value);
436 value = (fmt->f_value > value);
447 str = fmt->f_comp->c_text;
453 if (!(str = getenv (fmt->f_text)))
457 if (!(str = context_find (fmt->f_text)))
461 case FT_LS_DECODECOMP:
462 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
465 str = fmt->f_comp->c_text;
469 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
477 strncpy(buffer, str, sizeof(buffer));
478 buffer[sizeof(buffer)-1] = '\0';
480 while (isspace(*str))
483 if ((i = fmt->f_width) < 0) {
488 if (!ljust && i > 0 && strlen(str) > i)
491 xp += strlen(str) - 1;
492 while (xp > str && isspace(*xp))
494 if (ljust && i > 0 && strlen(str) > i)
495 str += strlen(str) - i;
500 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
503 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
506 value = fmt->f_value;
509 value = dat[fmt->f_value];
517 case FT_LV_CHAR_LEFT:
518 value = width - (cp - scanl);
521 value += fmt->f_value;
524 value = fmt->f_value - value;
528 value = value / fmt->f_value;
534 value = value % fmt->f_value;
543 value = fmt->f_comp->c_tws->tw_sec;
546 value = fmt->f_comp->c_tws->tw_min;
549 value = fmt->f_comp->c_tws->tw_hour;
552 value = fmt->f_comp->c_tws->tw_mday;
555 value = fmt->f_comp->c_tws->tw_mon + 1;
558 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
561 str = lmonth[fmt->f_comp->c_tws->tw_mon];
564 str = dtwszone (fmt->f_comp->c_tws);
567 value = fmt->f_comp->c_tws->tw_year;
570 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
572 value = tws->tw_wday;
575 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
577 str = tw_dotw[tws->tw_wday];
580 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
582 str = tw_ldotw[tws->tw_wday];
585 value = fmt->f_comp->c_tws->tw_yday;
588 value = fmt->f_comp->c_tws->tw_zone;
591 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
592 value = dmktime(fmt->f_comp->c_tws);
595 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
596 value = dmktime(fmt->f_comp->c_tws);
597 value = time((time_t *) 0) - value;
600 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
602 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
611 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
617 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
620 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
623 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
627 str = fmt->f_comp->c_mn->m_pers;
630 str = fmt->f_comp->c_mn->m_mbox;
633 str = fmt->f_comp->c_mn->m_host;
636 str = fmt->f_comp->c_mn->m_path;
639 str = fmt->f_comp->c_mn->m_gname;
642 str = fmt->f_comp->c_mn->m_note;
645 str = adrformat( fmt->f_comp->c_mn );
648 value = fmt->f_comp->c_mn->m_type;
651 value = fmt->f_comp->c_mn->m_ingrp;
654 value = fmt->f_comp->c_mn->m_nohost;
658 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
659 str = fmt->f_comp->c_text;
662 if (fmt->f_type == FT_LS_ADDR)
664 if ((str = mn->m_pers) == NULL) {
665 if ((str = mn->m_note)) {
666 strncpy (buffer, str, sizeof(buffer));
667 buffer[sizeof(buffer)-1] = '\0';
671 sp = str + strlen(str) - 1;
680 } else if (!(str = get_x400_friendly (mn->m_mbox,
681 buffer, sizeof(buffer)))) {
683 switch (mn->m_type) {
688 snprintf (buffer, sizeof(buffer), "%s!%s",
689 mn->m_host, mn->m_mbox);
694 snprintf (buffer, sizeof(buffer), "%s@%s",
695 mn->m_mbox, mn->m_host);
707 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
711 strncpy(buffer, str, sizeof(buffer));
712 /* strncpy doesn't NUL-terminate if it fills the buffer */
713 buffer[sizeof(buffer)-1] = '\0';
716 /* we will parse from buffer to buffer2 */
717 n = 0; /* n is the input position in str */
718 m = 0; /* m is the ouput position in buffer2 */
720 while ( str[n] != '\0') {
725 buffer2[m++] = str[n++];
731 buffer2[m++] = str[n++];
742 if ((t = comp->c_tws->tw_clock) == 0)
743 t = dmktime(comp->c_tws);
744 tws = dlocaltime(&t);
750 if ((t = comp->c_tws->tw_clock) == 0)
751 t = dmktime(comp->c_tws);
758 if (comp->c_flags & CF_PARSED)
760 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
762 comp->c_flags &= ~CF_TRUE;
763 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
764 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
765 comp->c_flags = CF_TRUE;
767 comp->c_flags |= CF_PARSED;
771 /* hook for custom address list formatting (see replsbr.c) */
772 str = formataddr (savestr, str);
776 /* output the str register as an address component,
777 * splitting it into multiple lines if necessary. The
778 * value reg. contains the max line length. The lit.
779 * field may contain a string to prepend to the result
784 int indent, wid, len;
790 indent = strlen (sp);
792 while( (c = *sp++) && cp < ep)
795 /* try to break at a comma; failing that, break at a
798 lastb = 0; sp = lp + wid;
799 while (sp > lp && (c = *--sp) != ',') {
800 if (! lastb && isspace(c))
804 if (! (sp = lastb)) {
806 while (*sp && *sp != ',' && !isspace(*sp))
813 while (cp < ep && lp <= sp)
820 for (i=indent; cp < ep && i > 0; i--)
824 cpstripped (&cp, ep, lp);
830 if (comp->c_flags & CF_PARSED)
832 if (comp->c_mn != &fmt_mnull)
834 if ((sp = comp->c_text) && (sp = getname(sp)) &&
835 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
839 comp->c_flags |= CF_PARSED;
841 while (getname("")) /* XXX */
843 comp->c_mn = &fmt_mnull;
849 * if there's no component, we say true. Otherwise we
850 * say "true" only if we can parse the address and it
851 * matches one of our addresses.
854 if (comp->c_mn != &fmt_mnull)
856 if ((sp = comp->c_text) && (sp = getname(sp)) &&
857 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
860 comp->c_flags |= CF_TRUE;
862 comp->c_flags &= ~CF_TRUE;
863 while ((sp = getname(sp)))
864 if ((comp->c_flags & CF_TRUE) == 0 &&
865 (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
867 comp->c_flags |= CF_TRUE;
869 while (getname("")) /* XXX */
871 if (comp->c_text == 0)
872 comp->c_flags |= CF_TRUE;
874 comp->c_flags &= ~CF_TRUE;
875 comp->c_mn = &fmt_mnull;
881 /* If we're working on a folder (as opposed to a file), add the
882 * current msg to sequence given in literal field. Don't
883 * disturb string or value registers.
885 if (fmt_current_folder)
886 seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
897 return ((struct format *)0);
901 while (fmt->f_type != FT_DONE)
906 return (fmt->f_value ? ++fmt : (struct format *) 0);