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 + 1, "%d", value);
353 cpnumber (&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
364 if (!(value = (str && *str))) {
371 if (!(value = (str == NULL || *str == 0))) {
378 if (value != fmt->f_value) {
385 if (value == fmt->f_value) {
392 if (value <= fmt->f_value) {
399 if (!(value = (str && match (str, fmt->f_text)))) {
407 value = match (str, fmt->f_text);
413 if (!(value = (str && uprf (str, fmt->f_text)))) {
420 value = uprf (str, fmt->f_text);
424 value = (str != NULL && *str != 0);
428 value = (str == NULL || *str == 0);
432 value = (fmt->f_value == value);
436 value = (fmt->f_value != value);
440 value = (fmt->f_value > value);
451 str = fmt->f_comp->c_text;
457 if (!(str = getenv (fmt->f_text)))
461 if (!(str = context_find (fmt->f_text)))
465 case FT_LS_DECODECOMP:
466 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
469 str = fmt->f_comp->c_text;
473 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
481 strncpy(buffer, str, sizeof(buffer));
482 buffer[sizeof(buffer)-1] = '\0';
484 while (isspace(*str))
487 if ((i = fmt->f_width) < 0) {
492 if (!ljust && i > 0 && strlen(str) > i)
495 xp += strlen(str) - 1;
496 while (xp > str && isspace(*xp))
498 if (ljust && i > 0 && strlen(str) > i)
499 str += strlen(str) - i;
504 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
507 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
510 value = fmt->f_value;
513 value = dat[fmt->f_value];
521 case FT_LV_CHAR_LEFT:
522 value = width - (cp - scanl);
525 value += fmt->f_value;
528 value = fmt->f_value - value;
532 value = value / fmt->f_value;
538 value = value % fmt->f_value;
547 value = fmt->f_comp->c_tws->tw_sec;
550 value = fmt->f_comp->c_tws->tw_min;
553 value = fmt->f_comp->c_tws->tw_hour;
556 value = fmt->f_comp->c_tws->tw_mday;
559 value = fmt->f_comp->c_tws->tw_mon + 1;
562 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
565 str = lmonth[fmt->f_comp->c_tws->tw_mon];
568 str = dtwszone (fmt->f_comp->c_tws);
571 value = fmt->f_comp->c_tws->tw_year;
574 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
576 value = tws->tw_wday;
579 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
581 str = tw_dotw[tws->tw_wday];
584 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
586 str = tw_ldotw[tws->tw_wday];
589 value = fmt->f_comp->c_tws->tw_yday;
592 value = fmt->f_comp->c_tws->tw_zone;
595 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
596 value = dmktime(fmt->f_comp->c_tws);
599 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
600 value = dmktime(fmt->f_comp->c_tws);
601 value = time((time_t *) 0) - value;
604 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
606 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
615 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
621 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
624 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
627 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
631 str = fmt->f_comp->c_mn->m_pers;
634 str = fmt->f_comp->c_mn->m_mbox;
637 str = fmt->f_comp->c_mn->m_host;
640 str = fmt->f_comp->c_mn->m_path;
643 str = fmt->f_comp->c_mn->m_gname;
646 str = fmt->f_comp->c_mn->m_note;
649 str = adrformat( fmt->f_comp->c_mn );
652 value = fmt->f_comp->c_mn->m_type;
655 value = fmt->f_comp->c_mn->m_ingrp;
658 value = fmt->f_comp->c_mn->m_nohost;
662 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
663 str = fmt->f_comp->c_text;
666 if (fmt->f_type == FT_LS_ADDR)
668 if ((str = mn->m_pers) == NULL) {
669 if ((str = mn->m_note)) {
670 strncpy (buffer, str, sizeof(buffer));
671 buffer[sizeof(buffer)-1] = '\0';
675 sp = str + strlen(str) - 1;
684 } else if (!(str = get_x400_friendly (mn->m_mbox,
685 buffer, sizeof(buffer)))) {
687 switch (mn->m_type) {
692 snprintf (buffer, sizeof(buffer), "%s!%s",
693 mn->m_host, mn->m_mbox);
698 snprintf (buffer, sizeof(buffer), "%s@%s",
699 mn->m_mbox, mn->m_host);
711 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
715 strncpy(buffer, str, sizeof(buffer));
716 /* strncpy doesn't NUL-terminate if it fills the buffer */
717 buffer[sizeof(buffer)-1] = '\0';
720 /* we will parse from buffer to buffer2 */
721 n = 0; /* n is the input position in str */
722 m = 0; /* m is the ouput position in buffer2 */
724 while ( str[n] != '\0') {
729 buffer2[m++] = str[n++];
735 buffer2[m++] = str[n++];
746 if ((t = comp->c_tws->tw_clock) == 0)
747 t = dmktime(comp->c_tws);
748 tws = dlocaltime(&t);
754 if ((t = comp->c_tws->tw_clock) == 0)
755 t = dmktime(comp->c_tws);
762 if (comp->c_flags & CF_PARSED)
764 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
766 comp->c_flags &= ~CF_TRUE;
767 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
768 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
769 comp->c_flags = CF_TRUE;
771 comp->c_flags |= CF_PARSED;
775 /* hook for custom address list formatting (see replsbr.c) */
776 str = formataddr (savestr, str);
780 /* output the str register as an address component,
781 * splitting it into multiple lines if necessary. The
782 * value reg. contains the max line length. The lit.
783 * field may contain a string to prepend to the result
788 int indent, wid, len;
794 indent = strlen (sp);
796 while( (c = *sp++) && cp < ep)
799 /* try to break at a comma; failing that, break at a
802 lastb = 0; sp = lp + wid;
803 while (sp > lp && (c = *--sp) != ',') {
804 if (! lastb && isspace(c))
808 if (! (sp = lastb)) {
810 while (*sp && *sp != ',' && !isspace(*sp))
817 while (cp < ep && lp <= sp)
824 for (i=indent; cp < ep && i > 0; i--)
828 cpstripped (&cp, ep, lp);
834 if (comp->c_flags & CF_PARSED)
836 if (comp->c_mn != &fmt_mnull)
838 if ((sp = comp->c_text) && (sp = getname(sp)) &&
839 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
843 comp->c_flags |= CF_PARSED;
845 while (getname("")) /* XXX */
847 comp->c_mn = &fmt_mnull;
853 * if there's no component, we say true. Otherwise we
854 * say "true" only if we can parse the address and it
855 * matches one of our addresses.
858 if (comp->c_mn != &fmt_mnull)
860 if ((sp = comp->c_text) && (sp = getname(sp)) &&
861 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
864 comp->c_flags |= CF_TRUE;
866 comp->c_flags &= ~CF_TRUE;
867 while ((sp = getname(sp)))
868 if ((comp->c_flags & CF_TRUE) == 0 &&
869 (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
871 comp->c_flags |= CF_TRUE;
873 while (getname("")) /* XXX */
875 if (comp->c_text == 0)
876 comp->c_flags |= CF_TRUE;
878 comp->c_flags &= ~CF_TRUE;
879 comp->c_mn = &fmt_mnull;
885 /* If we're working on a folder (as opposed to a file), add the
886 * current msg to sequence given in literal field. Don't
887 * disturb string or value registers.
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);