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.
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
13 #include <h/fmt_compile.h>
15 #ifdef HAVE_SYS_TIME_H
16 # include <sys/time.h>
20 #ifdef MULTIBYTE_SUPPORT
25 extern char *formataddr(); /* hook for custom address formatting */
27 extern int fmt_norm; /* defined in sbr/fmt_def.c = AD_NAME */
28 struct mailname fmt_mnull;
33 static int match(char *, char *);
34 static char *get_x400_friendly(char *, char *, int);
35 static int get_x400_comp(char *, char *, char *, int);
39 ** test if string "sub" appears anywhere in
40 ** string "str" (case insensitive).
44 match(char *str, char *sub)
50 while ((c2 = *str++) && tolower(c1) != tolower(c2))
54 s1 = sub + 1; s2 = str;
55 while ((c1 = *s1++) && tolower(c1) == tolower(*s2++))
64 ** copy a number to the destination subject to a maximum width
67 cpnumber(char **dest, int num, unsigned int wid, char fill, size_t n) {
80 *--sp = (i % 10) + '0';
82 } while (i > 0 && sp > cp);
85 else if ((num) < 0 && sp > cp)
95 ** copy string from str to dest padding with the fill character to a size
96 ** of wid characters. if wid is negative, the string is right aligned
97 ** no more than n bytes are copied
100 cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
101 int remaining; /* remaining output width available */
103 int end; /* number of input bytes remaining in str */
104 #ifdef MULTIBYTE_SUPPORT
105 int char_len; /* bytes in current character */
108 char *sp; /* current position in source string */
109 char *cp = *dest; /* current position in destination string */
110 char *ep = cp + n; /* end of destination buffer */
115 if ((remaining = (wid)) < 0) {
116 remaining = -remaining;
120 mbtowc(NULL, NULL, 0); /* reset shift state */
122 while (*sp && remaining > 0 && end > 0) {
123 #ifdef MULTIBYTE_SUPPORT
124 char_len = mbtowc(&wide_char, sp, end);
125 if (char_len <= 0 || (cp + char_len > ep))
130 if (iswcntrl(wide_char) || iswspace(wide_char)) {
134 if (iscntrl(*sp) || isspace(*sp)) {
147 #ifdef MULTIBYTE_SUPPORT
148 w = wcwidth(wide_char);
149 if (w >= 0 && remaining >= w) {
150 strncpy(cp, sp, char_len);
163 if (cp + remaining > ep)
167 /* copy string to the right */
168 while (--cp >= *dest)
169 *(cp + remaining) = *cp;
170 /* add padding at the beginning */
172 for (c=remaining; c>0; c--)
177 /* pad remaining space */
178 while (remaining-- > 0 && cp < ep)
185 cpstripped(char **start, char *end, char *str)
193 /* skip any initial control characters or spaces */
194 while ((c = (unsigned char) *s) && (iscntrl(c) || isspace(c)))
197 /* compact repeated control characters and spaces into a single space */
198 while((c = (unsigned char) *s++) && *start < end)
199 if (!iscntrl(c) && !isspace(c))
202 while ((c = (unsigned char) *s) &&
203 (iscntrl(c) || isspace(c)))
209 static char *lmonth[] = {
210 "January", "February", "March", "April",
211 "May", "June", "July", "August",
212 "September", "October", "November", "December"
216 get_x400_friendly(char *mbox, char *buffer, int buffer_len)
218 char given[BUFSIZ], surname[BUFSIZ];
227 if (get_x400_comp(mbox, "/PN=", buffer, buffer_len)) {
228 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
234 if (!get_x400_comp(mbox, "/S=", surname, sizeof(surname)))
237 if (get_x400_comp(mbox, "/G=", given, sizeof(given)))
238 snprintf(buffer, buffer_len, "%s %s", given, surname);
240 snprintf(buffer, buffer_len, "%s", surname);
246 get_x400_comp(char *mbox, char *key, char *buffer, int buffer_len)
251 if ((idx = stringdex(key, mbox)) < 0
252 || !(cp = strchr(mbox += idx + strlen(key), '/')))
255 snprintf(buffer, buffer_len, "%*.*s", (int)(cp - mbox), (int)(cp - mbox), mbox);
260 fmt_scan(struct format *format, char *scanl, int width, int *dat)
264 char *savestr = NULL;
265 unsigned char *str = NULL;
266 char buffer[BUFSIZ], buffer2[BUFSIZ];
276 ep = scanl + width - 1;
278 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
279 switch (fmt->f_type) {
282 fmt->f_comp->c_flags &= ~CF_PARSED;
289 switch (fmt->f_type) {
292 cpstripped(&cp, ep, fmt->f_comp->c_text);
295 cptrimmed(&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
300 while( (c = *sp++) && cp < ep)
309 ljust++; /* XXX should do something with this */
311 while( (c = *sp++) && --i >= 0 && cp < ep)
313 while( --i >= 0 && cp < ep)
318 cpstripped(&cp, ep, str);
321 cptrimmed(&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
324 adios(NULL, "internal error (FT_STRFW)");
327 n = snprintf(cp, ep - cp + 1, "%d", value);
336 cpnumber(&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
347 if (!(value = (str && *str))) {
354 if (!(value = (str == NULL || *str == 0))) {
361 if (value != fmt->f_value) {
368 if (value == fmt->f_value) {
375 if (value <= fmt->f_value) {
382 if (!(value = (str && match(str, fmt->f_text)))) {
390 value = match(str, fmt->f_text);
396 if (!(value = (str && uprf(str, fmt->f_text)))) {
403 value = uprf(str, fmt->f_text);
407 value = (str != NULL && *str != 0);
411 value = (str == NULL || *str == 0);
415 value = (fmt->f_value == value);
419 value = (fmt->f_value != value);
423 value = (fmt->f_value > value);
434 str = fmt->f_comp->c_text;
440 if (!(str = getenv(fmt->f_text)))
444 if (!(str = context_find(fmt->f_text)))
448 case FT_LS_DECODECOMP:
449 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
452 str = fmt->f_comp->c_text;
456 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
464 strncpy(buffer, str, sizeof(buffer));
465 buffer[sizeof(buffer)-1] = '\0';
467 while (isspace(*str))
470 if ((i = fmt->f_width) < 0) {
475 if (!ljust && i > 0 && strlen(str) > i)
478 xp += strlen(str) - 1;
479 while (xp > str && isspace(*xp))
481 if (ljust && i > 0 && strlen(str) > i)
482 str += strlen(str) - i;
487 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
490 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
493 value = fmt->f_value;
496 value = dat[fmt->f_value];
504 case FT_LV_CHAR_LEFT:
505 value = width - (cp - scanl);
508 value += fmt->f_value;
511 value = fmt->f_value - value;
515 value = value / fmt->f_value;
521 value = value % fmt->f_value;
530 value = fmt->f_comp->c_tws->tw_sec;
533 value = fmt->f_comp->c_tws->tw_min;
536 value = fmt->f_comp->c_tws->tw_hour;
539 value = fmt->f_comp->c_tws->tw_mday;
542 value = fmt->f_comp->c_tws->tw_mon + 1;
545 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
548 str = lmonth[fmt->f_comp->c_tws->tw_mon];
551 str = dtwszone(fmt->f_comp->c_tws);
554 value = fmt->f_comp->c_tws->tw_year;
557 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
559 value = tws->tw_wday;
562 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
564 str = tw_dotw[tws->tw_wday];
567 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
569 str = tw_ldotw[tws->tw_wday];
572 value = fmt->f_comp->c_tws->tw_yday;
575 value = fmt->f_comp->c_tws->tw_zone;
578 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
579 value = dmktime(fmt->f_comp->c_tws);
582 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
583 value = dmktime(fmt->f_comp->c_tws);
584 value = time((time_t *) 0) - value;
587 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
589 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
598 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
604 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
607 str = dasctime(fmt->f_comp->c_tws , TW_ZONE);
610 str = dasctime(fmt->f_comp->c_tws, TW_NULL);
614 str = fmt->f_comp->c_mn->m_pers;
617 str = fmt->f_comp->c_mn->m_mbox;
620 str = fmt->f_comp->c_mn->m_host;
623 str = fmt->f_comp->c_mn->m_path;
626 str = fmt->f_comp->c_mn->m_gname;
629 str = fmt->f_comp->c_mn->m_note;
632 str = adrformat( fmt->f_comp->c_mn );
635 value = fmt->f_comp->c_mn->m_type;
638 value = fmt->f_comp->c_mn->m_ingrp;
641 value = fmt->f_comp->c_mn->m_nohost;
645 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
646 str = fmt->f_comp->c_text;
649 if (fmt->f_type == FT_LS_ADDR)
651 if ((str = mn->m_pers) == NULL) {
652 if ((str = mn->m_note)) {
653 strncpy(buffer, str, sizeof(buffer));
654 buffer[sizeof(buffer)-1] = '\0';
658 sp = str + strlen(str) - 1;
667 } else if (!(str = get_x400_friendly(mn->m_mbox,
668 buffer, sizeof(buffer)))) {
670 switch (mn->m_type) {
676 snprintf(buffer, sizeof(buffer), "%s@%s", mn->m_mbox, mn->m_host);
687 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
691 strncpy(buffer, str, sizeof(buffer));
692 /* strncpy doesn't NUL-terminate if it fills the buffer */
693 buffer[sizeof(buffer)-1] = '\0';
696 /* we will parse from buffer to buffer2 */
697 n = 0; /* n is the input position in str */
698 m = 0; /* m is the ouput position in buffer2 */
700 while ( str[n] != '\0') {
705 buffer2[m++] = str[n++];
711 buffer2[m++] = str[n++];
722 if ((t = comp->c_tws->tw_clock) == 0)
723 t = dmktime(comp->c_tws);
724 tws = dlocaltime(&t);
730 if ((t = comp->c_tws->tw_clock) == 0)
731 t = dmktime(comp->c_tws);
738 if (comp->c_flags & CF_PARSED)
740 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
742 comp->c_flags &= ~CF_TRUE;
743 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
744 memset((char *) comp->c_tws, 0, sizeof *comp->c_tws);
745 comp->c_flags = CF_TRUE;
747 comp->c_flags |= CF_PARSED;
752 ** hook for custom address list formatting
755 str = formataddr(savestr, str);
760 ** output the str register as an address component,
761 ** splitting it into multiple lines if necessary. The
762 ** value reg. contains the max line length. The lit.
763 ** field may contain a string to prepend to the result
769 int indent, wid, len;
777 while( (c = *sp++) && cp < ep)
780 /* try to break at a comma; failing that,
783 lastb = 0; sp = lp + wid;
784 while (sp > lp && (c = *--sp) != ',') {
785 if (! lastb && isspace(c))
789 if (! (sp = lastb)) {
791 while (*sp && *sp != ',' && !isspace(*sp))
798 while (cp < ep && lp <= sp)
805 for (i=indent; cp < ep && i > 0; i--)
809 cpstripped(&cp, ep, lp);
815 if (comp->c_flags & CF_PARSED)
817 if (comp->c_mn != &fmt_mnull)
819 if ((sp = comp->c_text) && (sp = getname(sp)) &&
820 (mn = getm(sp, NULL, 0, fmt_norm, NULL))) {
824 comp->c_flags |= CF_PARSED;
826 while (getname("")) /* XXX */
828 comp->c_mn = &fmt_mnull;
834 ** if there's no component, we say true. Otherwise we
835 ** say "true" only if we can parse the address and it
836 ** matches one of our addresses.
839 if (comp->c_mn != &fmt_mnull)
841 if ((sp = comp->c_text) && (sp = getname(sp)) &&
842 (mn = getm(sp, NULL, 0, AD_NAME, NULL))) {
845 comp->c_flags |= CF_TRUE;
847 comp->c_flags &= ~CF_TRUE;
848 while ((sp = getname(sp)))
849 if ((comp->c_flags & CF_TRUE) == 0 &&
850 (mn = getm(sp, NULL, 0, AD_NAME, NULL)))
852 comp->c_flags |= CF_TRUE;
854 while (getname("")) /* XXX */
856 if (comp->c_text == 0)
857 comp->c_flags |= CF_TRUE;
859 comp->c_flags &= ~CF_TRUE;
860 comp->c_mn = &fmt_mnull;
871 return ((struct format *)0);
874 /* I'll remove this as soon as I understand what it does. --meillo */
877 while (fmt->f_type != FT_DONE)
882 return (fmt->f_value ? ++fmt : (struct format *) 0);