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.
8 ** This is the engine that processes the format instructions created by
9 ** fmt_compile (found in fmt_compile.c).
13 #include <h/addrsbr.h>
14 #include <h/fmt_scan.h>
16 #include <h/fmt_compile.h>
18 #ifdef HAVE_SYS_TIME_H
19 # include <sys/time.h>
23 #ifdef MULTIBYTE_SUPPORT
28 extern int fmt_norm; /* defined in sbr/fmt_def.c = AD_NAME */
29 struct mailname fmt_mnull;
34 static int match(char *, char *);
35 static char *get_x400_friendly(char *, char *, int);
36 static int get_x400_comp(char *, char *, char *, int);
40 ** test if string "sub" appears anywhere in
41 ** string "str" (case insensitive).
45 match(char *str, char *sub)
51 while ((c2 = *str++) && tolower(c1) != tolower(c2))
55 s1 = sub + 1; s2 = str;
56 while ((c1 = *s1++) && tolower(c1) == tolower(*s2++))
65 ** copy a number to the destination subject to a maximum width
68 cpnumber(char **dest, int num, unsigned int wid, char fill, size_t n) {
81 *--sp = (i % 10) + '0';
83 } while (i > 0 && sp > cp);
86 else if ((num) < 0 && sp > cp)
96 ** copy string from str to dest padding with the fill character to a size
97 ** of wid characters. if wid is negative, the string is right aligned
98 ** no more than n bytes are copied
101 cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
102 int remaining; /* remaining output width available */
104 int end; /* number of input bytes remaining in str */
105 #ifdef MULTIBYTE_SUPPORT
106 int char_len; /* bytes in current character */
109 char *sp; /* current position in source string */
110 char *cp = *dest; /* current position in destination string */
111 char *ep = cp + n; /* end of destination buffer */
116 if ((remaining = (wid)) < 0) {
117 remaining = -remaining;
121 mbtowc(NULL, NULL, 0); /* reset shift state */
123 while (*sp && remaining > 0 && end > 0) {
124 #ifdef MULTIBYTE_SUPPORT
125 char_len = mbtowc(&wide_char, sp, end);
126 if (char_len <= 0 || (cp + char_len > ep))
131 if (iswcntrl(wide_char) || iswspace(wide_char)) {
135 if (iscntrl(*sp) || isspace(*sp)) {
148 #ifdef MULTIBYTE_SUPPORT
149 w = wcwidth(wide_char);
150 if (w >= 0 && remaining >= w) {
151 strncpy(cp, sp, char_len);
164 if (cp + remaining > ep)
168 /* copy string to the right */
169 while (--cp >= *dest)
170 *(cp + remaining) = *cp;
171 /* add padding at the beginning */
173 for (c=remaining; c>0; c--)
178 /* pad remaining space */
179 while (remaining-- > 0 && cp < ep)
186 cpstripped(char **start, char *end, char *str)
194 /* skip any initial control characters or spaces */
195 while ((c = (unsigned char) *s) && (iscntrl(c) || isspace(c)))
198 /* compact repeated control characters and spaces into a single space */
199 while((c = (unsigned char) *s++) && *start < end)
200 if (!iscntrl(c) && !isspace(c))
203 while ((c = (unsigned char) *s) &&
204 (iscntrl(c) || isspace(c)))
210 static char *lmonth[] = {
211 "January", "February", "March", "April",
212 "May", "June", "July", "August",
213 "September", "October", "November", "December"
217 get_x400_friendly(char *mbox, char *buffer, int buffer_len)
219 char given[BUFSIZ], surname[BUFSIZ];
228 if (get_x400_comp(mbox, "/PN=", buffer, buffer_len)) {
229 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
235 if (!get_x400_comp(mbox, "/S=", surname, sizeof(surname)))
238 if (get_x400_comp(mbox, "/G=", given, sizeof(given)))
239 snprintf(buffer, buffer_len, "%s %s", given, surname);
241 snprintf(buffer, buffer_len, "%s", surname);
247 get_x400_comp(char *mbox, char *key, char *buffer, int buffer_len)
252 if ((idx = stringdex(key, mbox)) < 0
253 || !(cp = strchr(mbox += idx + strlen(key), '/')))
256 snprintf(buffer, buffer_len, "%*.*s", (int)(cp - mbox), (int)(cp - mbox), mbox);
261 fmt_scan(struct format *format, char *scanl, int width, int *dat)
265 char *savestr = NULL;
266 unsigned char *str = NULL;
267 char buffer[BUFSIZ], buffer2[BUFSIZ];
277 ep = scanl + width - 1;
279 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
280 switch (fmt->f_type) {
283 fmt->f_comp->c_flags &= ~CF_PARSED;
290 switch (fmt->f_type) {
293 cpstripped(&cp, ep, fmt->f_comp->c_text);
296 cptrimmed(&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
301 while( (c = *sp++) && cp < ep)
310 ljust++; /* XXX should do something with this */
312 while( (c = *sp++) && --i >= 0 && cp < ep)
314 while( --i >= 0 && cp < ep)
319 cpstripped(&cp, ep, str);
322 cptrimmed(&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
325 adios(NULL, "internal error (FT_STRFW)");
328 n = snprintf(cp, ep - cp + 1, "%d", value);
337 cpnumber(&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
348 if (!(value = (str && *str))) {
355 if (!(value = (str == NULL || *str == 0))) {
362 if (value != fmt->f_value) {
369 if (value == fmt->f_value) {
376 if (value <= fmt->f_value) {
383 if (!(value = (str && match(str, fmt->f_text)))) {
391 value = match(str, fmt->f_text);
397 if (!(value = (str && uprf(str, fmt->f_text)))) {
404 value = uprf(str, fmt->f_text);
408 value = (str != NULL && *str != 0);
412 value = (str == NULL || *str == 0);
416 value = (fmt->f_value == value);
420 value = (fmt->f_value != value);
424 value = (fmt->f_value > value);
435 str = fmt->f_comp->c_text;
441 if (!(str = getenv(fmt->f_text)))
445 if (!(str = context_find(fmt->f_text)))
449 case FT_LS_DECODECOMP:
450 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
453 str = fmt->f_comp->c_text;
457 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
465 strncpy(buffer, str, sizeof(buffer));
466 buffer[sizeof(buffer)-1] = '\0';
468 while (isspace(*str))
471 if ((i = fmt->f_width) < 0) {
476 if (!ljust && i > 0 && (int)strlen(str) > i)
479 xp += strlen(str) - 1;
480 while (xp > str && isspace(*xp))
482 if (ljust && i > 0 && (int)strlen(str) > i)
483 str += strlen(str) - i;
488 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
491 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
494 value = fmt->f_value;
497 value = dat[fmt->f_value];
505 case FT_LV_CHAR_LEFT:
506 value = width - (cp - scanl);
509 value += fmt->f_value;
512 value = fmt->f_value - value;
516 value = value / fmt->f_value;
522 value = value % fmt->f_value;
531 value = fmt->f_comp->c_tws->tw_sec;
534 value = fmt->f_comp->c_tws->tw_min;
537 value = fmt->f_comp->c_tws->tw_hour;
540 value = fmt->f_comp->c_tws->tw_mday;
543 value = fmt->f_comp->c_tws->tw_mon + 1;
546 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
549 str = lmonth[fmt->f_comp->c_tws->tw_mon];
552 str = dtwszone(fmt->f_comp->c_tws);
555 value = fmt->f_comp->c_tws->tw_year;
558 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
560 value = tws->tw_wday;
563 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
565 str = tw_dotw[tws->tw_wday];
568 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
570 str = tw_ldotw[tws->tw_wday];
573 value = fmt->f_comp->c_tws->tw_yday;
576 value = fmt->f_comp->c_tws->tw_zone;
579 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
580 value = dmktime(fmt->f_comp->c_tws);
583 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
584 value = dmktime(fmt->f_comp->c_tws);
585 value = time((time_t *) 0) - value;
588 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
590 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
599 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
605 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
609 str = dasctime(fmt->f_comp->c_tws);
613 str = fmt->f_comp->c_mn->m_pers;
616 str = fmt->f_comp->c_mn->m_mbox;
619 str = fmt->f_comp->c_mn->m_host;
622 str = fmt->f_comp->c_mn->m_path;
625 str = fmt->f_comp->c_mn->m_gname;
628 str = fmt->f_comp->c_mn->m_note;
631 str = adrformat( fmt->f_comp->c_mn );
634 value = fmt->f_comp->c_mn->m_type;
637 value = fmt->f_comp->c_mn->m_ingrp;
640 value = fmt->f_comp->c_mn->m_nohost;
644 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
645 str = fmt->f_comp->c_text;
648 if (fmt->f_type == FT_LS_ADDR)
650 if ((str = mn->m_pers) == NULL) {
651 if ((str = mn->m_note)) {
652 strncpy(buffer, str, sizeof(buffer));
653 buffer[sizeof(buffer)-1] = '\0';
657 sp = str + strlen(str) - 1;
666 } else if (!(str = get_x400_friendly(mn->m_mbox,
667 buffer, sizeof(buffer)))) {
669 switch (mn->m_type) {
675 snprintf(buffer, sizeof(buffer), "%s@%s", mn->m_mbox, mn->m_host);
686 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
690 strncpy(buffer, str, sizeof(buffer));
691 /* strncpy doesn't NUL-terminate if it fills the buffer */
692 buffer[sizeof(buffer)-1] = '\0';
695 /* we will parse from buffer to buffer2 */
696 n = 0; /* n is the input position in str */
697 m = 0; /* m is the ouput position in buffer2 */
699 while ( str[n] != '\0') {
704 buffer2[m++] = str[n++];
710 buffer2[m++] = str[n++];
721 if ((t = comp->c_tws->tw_clock) == 0)
722 t = dmktime(comp->c_tws);
723 tws = dlocaltime(&t);
729 if ((t = comp->c_tws->tw_clock) == 0)
730 t = dmktime(comp->c_tws);
737 if (comp->c_flags & CF_PARSED)
739 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
741 comp->c_flags &= ~CF_TRUE;
742 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
743 memset((char *) comp->c_tws, 0, sizeof *comp->c_tws);
744 comp->c_flags = CF_TRUE;
746 comp->c_flags |= CF_PARSED;
751 ** hook for custom address list formatting
754 str = formataddr(savestr, str);
759 ** output the str register as an address component,
760 ** splitting it into multiple lines if necessary. The
761 ** value reg. contains the max line length. The lit.
762 ** field may contain a string to prepend to the result
768 int indent, wid, len;
777 adios(NULL, "putaddr -- num register (%d) "
778 "must be greater than label "
779 "width (%d)", value, indent);
781 while( (c = *sp++) && cp < ep)
784 /* try to break at a comma; failing that,
787 lastb = 0; sp = lp + wid;
788 while (sp > lp && (c = *--sp) != ',') {
789 if (! lastb && isspace(c))
793 if (! (sp = lastb)) {
795 while (*sp && *sp != ',' && !isspace(*sp))
802 while (cp < ep && lp <= sp)
809 for (i=indent; cp < ep && i > 0; i--)
813 cpstripped(&cp, ep, lp);
819 if (comp->c_flags & CF_PARSED)
821 if (comp->c_mn != &fmt_mnull)
823 if ((sp = comp->c_text) && (sp = getname(sp)) &&
824 (mn = getm(sp, NULL, 0, fmt_norm, NULL))) {
828 comp->c_flags |= CF_PARSED;
830 while (getname("")) /* XXX */
832 comp->c_mn = &fmt_mnull;
838 ** if there's no component, we say true. Otherwise we
839 ** say "true" only if we can parse the address and it
840 ** matches one of our addresses.
843 if (comp->c_mn != &fmt_mnull)
845 if ((sp = comp->c_text) && (sp = getname(sp)) &&
846 (mn = getm(sp, NULL, 0, AD_NAME, NULL))) {
849 comp->c_flags |= CF_TRUE;
851 comp->c_flags &= ~CF_TRUE;
852 while ((sp = getname(sp)))
853 if ((comp->c_flags & CF_TRUE) == 0 &&
854 (mn = getm(sp, NULL, 0, AD_NAME, NULL)))
856 comp->c_flags |= CF_TRUE;
858 while (getname("")) /* XXX */
860 if (comp->c_text == 0)
861 comp->c_flags |= CF_TRUE;
863 comp->c_flags &= ~CF_TRUE;
864 comp->c_mn = &fmt_mnull;
875 return ((struct format *)0);
878 /* I'll remove this as soon as I understand what it does. --meillo */
881 while (fmt->f_type != FT_DONE)
886 return (fmt->f_value ? ++fmt : (struct format *) 0);