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 TIME_WITH_SYS_TIME
16 # include <sys/time.h>
19 # ifdef TM_IN_SYS_TIME
20 # include <sys/time.h>
25 #ifdef MULTIBYTE_SUPPORT
32 extern char *formataddr(); /* hook for custom address formatting */
34 extern int fmt_norm; /* defined in sbr/fmt_def.c = AD_NAME */
35 struct mailname fmt_mnull;
40 static int match(char *, char *);
41 static char *get_x400_friendly(char *, char *, int);
42 static int get_x400_comp(char *, char *, char *, int);
46 ** test if string "sub" appears anywhere in
47 ** string "str" (case insensitive).
51 match(char *str, char *sub)
57 while ((c2 = *str++) && tolower(c1) != tolower(c2))
61 s1 = sub + 1; s2 = str;
62 while ((c1 = *s1++) && tolower(c1) == tolower(*s2++))
71 ** copy a number to the destination subject to a maximum width
74 cpnumber(char **dest, int num, unsigned int wid, char fill, size_t n) {
87 *--sp = (i % 10) + '0';
89 } while (i > 0 && sp > cp);
92 else if ((num) < 0 && sp > cp)
102 ** copy string from str to dest padding with the fill character to a size
103 ** of wid characters. if wid is negative, the string is right aligned
104 ** no more than n bytes are copied
107 cptrimmed(char **dest, char *str, unsigned int wid, char fill, size_t n) {
108 int remaining; /* remaining output width available */
110 int end; /* number of input bytes remaining in str */
111 #ifdef MULTIBYTE_SUPPORT
112 int char_len; /* bytes in current character */
115 char *sp; /* current position in source string */
116 char *cp = *dest; /* current position in destination string */
117 char *ep = cp + n; /* end of destination buffer */
122 if ((remaining = (wid)) < 0) {
123 remaining = -remaining;
127 mbtowc(NULL, NULL, 0); /* reset shift state */
129 while (*sp && remaining > 0 && end > 0) {
130 #ifdef MULTIBYTE_SUPPORT
131 char_len = mbtowc(&wide_char, sp, end);
132 if (char_len <= 0 || (cp + char_len > ep))
137 if (iswcntrl(wide_char) || iswspace(wide_char)) {
141 if (iscntrl(*sp) || isspace(*sp)) {
154 #ifdef MULTIBYTE_SUPPORT
155 w = wcwidth(wide_char);
156 if (w >= 0 && remaining >= w) {
157 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) && (iscntrl(c) || isspace(c)))
204 /* compact repeated control characters and spaces into a single space */
205 while((c = (unsigned char) *s++) && *start < end)
206 if (!iscntrl(c) && !isspace(c))
209 while ((c = (unsigned char) *s) &&
210 (iscntrl(c) || isspace(c)))
216 static char *lmonth[] = {
217 "January", "February", "March", "April",
218 "May", "June", "July", "August",
219 "September", "October", "November", "December"
223 get_x400_friendly(char *mbox, char *buffer, int buffer_len)
225 char given[BUFSIZ], surname[BUFSIZ];
234 if (get_x400_comp(mbox, "/PN=", buffer, buffer_len)) {
235 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
241 if (!get_x400_comp(mbox, "/S=", surname, sizeof(surname)))
244 if (get_x400_comp(mbox, "/G=", given, sizeof(given)))
245 snprintf(buffer, buffer_len, "%s %s", given, surname);
247 snprintf(buffer, buffer_len, "%s", surname);
253 get_x400_comp(char *mbox, char *key, char *buffer, int buffer_len)
258 if ((idx = stringdex(key, mbox)) < 0
259 || !(cp = strchr(mbox += idx + strlen(key), '/')))
262 snprintf(buffer, buffer_len, "%*.*s", (int)(cp - mbox), (int)(cp - mbox), mbox);
267 fmt_scan(struct format *format, char *scanl, int width, int *dat)
271 char *savestr = NULL;
272 unsigned char *str = NULL;
273 char buffer[BUFSIZ], buffer2[BUFSIZ];
283 ep = scanl + width - 1;
285 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
286 switch (fmt->f_type) {
289 fmt->f_comp->c_flags &= ~CF_PARSED;
296 switch (fmt->f_type) {
299 cpstripped(&cp, ep, fmt->f_comp->c_text);
302 cptrimmed(&cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill, ep - cp);
307 while( (c = *sp++) && cp < ep)
316 ljust++; /* XXX should do something with this */
318 while( (c = *sp++) && --i >= 0 && cp < ep)
320 while( --i >= 0 && cp < ep)
325 cpstripped(&cp, ep, str);
328 cptrimmed(&cp, str, fmt->f_width, fmt->f_fill, ep - cp);
331 adios(NULL, "internal error (FT_STRFW)");
334 n = snprintf(cp, ep - cp + 1, "%d", value);
343 cpnumber(&cp, value, fmt->f_width, fmt->f_fill, ep - cp);
354 if (!(value = (str && *str))) {
361 if (!(value = (str == NULL || *str == 0))) {
368 if (value != fmt->f_value) {
375 if (value == fmt->f_value) {
382 if (value <= fmt->f_value) {
389 if (!(value = (str && match(str, fmt->f_text)))) {
397 value = match(str, fmt->f_text);
403 if (!(value = (str && uprf(str, fmt->f_text)))) {
410 value = uprf(str, fmt->f_text);
414 value = (str != NULL && *str != 0);
418 value = (str == NULL || *str == 0);
422 value = (fmt->f_value == value);
426 value = (fmt->f_value != value);
430 value = (fmt->f_value > value);
441 str = fmt->f_comp->c_text;
447 if (!(str = getenv(fmt->f_text)))
451 if (!(str = context_find(fmt->f_text)))
455 case FT_LS_DECODECOMP:
456 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
459 str = fmt->f_comp->c_text;
463 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
471 strncpy(buffer, str, sizeof(buffer));
472 buffer[sizeof(buffer)-1] = '\0';
474 while (isspace(*str))
477 if ((i = fmt->f_width) < 0) {
482 if (!ljust && i > 0 && strlen(str) > i)
485 xp += strlen(str) - 1;
486 while (xp > str && isspace(*xp))
488 if (ljust && i > 0 && strlen(str) > i)
489 str += strlen(str) - i;
494 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
497 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
500 value = fmt->f_value;
503 value = dat[fmt->f_value];
511 case FT_LV_CHAR_LEFT:
512 value = width - (cp - scanl);
515 value += fmt->f_value;
518 value = fmt->f_value - value;
522 value = value / fmt->f_value;
528 value = value % fmt->f_value;
537 value = fmt->f_comp->c_tws->tw_sec;
540 value = fmt->f_comp->c_tws->tw_min;
543 value = fmt->f_comp->c_tws->tw_hour;
546 value = fmt->f_comp->c_tws->tw_mday;
549 value = fmt->f_comp->c_tws->tw_mon + 1;
552 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
555 str = lmonth[fmt->f_comp->c_tws->tw_mon];
558 str = dtwszone(fmt->f_comp->c_tws);
561 value = fmt->f_comp->c_tws->tw_year;
564 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
566 value = tws->tw_wday;
569 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
571 str = tw_dotw[tws->tw_wday];
574 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
576 str = tw_ldotw[tws->tw_wday];
579 value = fmt->f_comp->c_tws->tw_yday;
582 value = fmt->f_comp->c_tws->tw_zone;
585 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
586 value = dmktime(fmt->f_comp->c_tws);
589 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
590 value = dmktime(fmt->f_comp->c_tws);
591 value = time((time_t *) 0) - value;
594 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
596 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
605 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
611 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
614 str = dasctime(fmt->f_comp->c_tws , TW_ZONE);
617 str = dasctime(fmt->f_comp->c_tws, TW_NULL);
621 str = fmt->f_comp->c_mn->m_pers;
624 str = fmt->f_comp->c_mn->m_mbox;
627 str = fmt->f_comp->c_mn->m_host;
630 str = fmt->f_comp->c_mn->m_path;
633 str = fmt->f_comp->c_mn->m_gname;
636 str = fmt->f_comp->c_mn->m_note;
639 str = adrformat( fmt->f_comp->c_mn );
642 value = fmt->f_comp->c_mn->m_type;
645 value = fmt->f_comp->c_mn->m_ingrp;
648 value = fmt->f_comp->c_mn->m_nohost;
652 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
653 str = fmt->f_comp->c_text;
656 if (fmt->f_type == FT_LS_ADDR)
658 if ((str = mn->m_pers) == NULL) {
659 if ((str = mn->m_note)) {
660 strncpy(buffer, str, sizeof(buffer));
661 buffer[sizeof(buffer)-1] = '\0';
665 sp = str + strlen(str) - 1;
674 } else if (!(str = get_x400_friendly(mn->m_mbox,
675 buffer, sizeof(buffer)))) {
677 switch (mn->m_type) {
682 snprintf(buffer, sizeof(buffer), "%s!%s",
683 mn->m_host, mn->m_mbox);
688 snprintf(buffer, sizeof(buffer), "%s@%s",
689 mn->m_mbox, mn->m_host);
700 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
704 strncpy(buffer, str, sizeof(buffer));
705 /* strncpy doesn't NUL-terminate if it fills the buffer */
706 buffer[sizeof(buffer)-1] = '\0';
709 /* we will parse from buffer to buffer2 */
710 n = 0; /* n is the input position in str */
711 m = 0; /* m is the ouput position in buffer2 */
713 while ( str[n] != '\0') {
718 buffer2[m++] = str[n++];
724 buffer2[m++] = str[n++];
735 if ((t = comp->c_tws->tw_clock) == 0)
736 t = dmktime(comp->c_tws);
737 tws = dlocaltime(&t);
743 if ((t = comp->c_tws->tw_clock) == 0)
744 t = dmktime(comp->c_tws);
751 if (comp->c_flags & CF_PARSED)
753 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
755 comp->c_flags &= ~CF_TRUE;
756 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
757 memset((char *) comp->c_tws, 0, sizeof *comp->c_tws);
758 comp->c_flags = CF_TRUE;
760 comp->c_flags |= CF_PARSED;
765 ** hook for custom address list formatting
768 str = formataddr(savestr, str);
773 ** output the str register as an address component,
774 ** splitting it into multiple lines if necessary. The
775 ** value reg. contains the max line length. The lit.
776 ** field may contain a string to prepend to the result
782 int indent, wid, len;
790 while( (c = *sp++) && cp < ep)
793 /* try to break at a comma; failing that,
796 lastb = 0; sp = lp + wid;
797 while (sp > lp && (c = *--sp) != ',') {
798 if (! lastb && isspace(c))
802 if (! (sp = lastb)) {
804 while (*sp && *sp != ',' && !isspace(*sp))
811 while (cp < ep && lp <= sp)
818 for (i=indent; cp < ep && i > 0; i--)
822 cpstripped(&cp, ep, lp);
828 if (comp->c_flags & CF_PARSED)
830 if (comp->c_mn != &fmt_mnull)
832 if ((sp = comp->c_text) && (sp = getname(sp)) &&
833 (mn = getm(sp, NULL, 0, fmt_norm, NULL))) {
837 comp->c_flags |= CF_PARSED;
839 while (getname("")) /* XXX */
841 comp->c_mn = &fmt_mnull;
847 ** if there's no component, we say true. Otherwise we
848 ** say "true" only if we can parse the address and it
849 ** matches one of our addresses.
852 if (comp->c_mn != &fmt_mnull)
854 if ((sp = comp->c_text) && (sp = getname(sp)) &&
855 (mn = getm(sp, NULL, 0, AD_NAME, NULL))) {
858 comp->c_flags |= CF_TRUE;
860 comp->c_flags &= ~CF_TRUE;
861 while ((sp = getname(sp)))
862 if ((comp->c_flags & CF_TRUE) == 0 &&
863 (mn = getm(sp, NULL, 0, AD_NAME, NULL)))
865 comp->c_flags |= CF_TRUE;
867 while (getname("")) /* XXX */
869 if (comp->c_text == 0)
870 comp->c_flags |= CF_TRUE;
872 comp->c_flags &= ~CF_TRUE;
873 comp->c_mn = &fmt_mnull;
884 return ((struct format *)0);
887 /* I'll remove this as soon as I understand what it does. --meillo */
890 while (fmt->f_type != FT_DONE)
895 return (fmt->f_value ? ++fmt : (struct format *) 0);