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>
19 #ifdef TIME_WITH_SYS_TIME
20 # include <sys/time.h>
23 # ifdef TM_IN_SYS_TIME
24 # 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 * macros to format data
92 #define PUTDF(cp, num, wid, fill)\
100 *--sp = (i % 10) + '0';\
102 } while (i > 0 && sp > cp);\
105 else if ((num) < 0 && sp > cp)\
112 char * PUTSF(char *cp, char *str, unsigned int wid, char fill) {
115 unsigned int char_len;
116 unsigned int term_len = 0;
119 for (i = 0 ; i < wid && i < strlen(str) && term_len < wid; ) {
120 char_len = mblen(str + i, strlen(str + i));
124 mbtowc(&wide_char, str + i, strlen(str + i));
125 term_len += wcwidth(wide_char);
127 for (j = 0 ; j < char_len ; j++) {
128 *(cp + i) = *(str + i);
133 if (term_len < wid) {
134 for (j = term_len ; j <= wid ; j++) {
143 #define PUTS(cp, str) {\
145 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
147 while((c = (unsigned char) *sp++) && cp < ep)\
148 if (!iscntrl(c) && !isspace(c)) \
151 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
158 static char *lmonth[] = { "January", "February","March", "April",
159 "May", "June", "July", "August",
160 "September","October", "November","December" };
163 get_x400_friendly (char *mbox, char *buffer, int buffer_len)
165 char given[BUFSIZ], surname[BUFSIZ];
174 if (get_x400_comp (mbox, "/PN=", buffer, buffer_len)) {
175 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
181 if (!get_x400_comp (mbox, "/S=", surname, sizeof(surname)))
184 if (get_x400_comp (mbox, "/G=", given, sizeof(given)))
185 snprintf (buffer, buffer_len, "%s %s", given, surname);
187 snprintf (buffer, buffer_len, "%s", surname);
193 get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len)
198 if ((idx = stringdex (key, mbox)) < 0
199 || !(cp = strchr(mbox += idx + strlen (key), '/')))
202 snprintf (buffer, buffer_len, "%*.*s", cp - mbox, cp - mbox, mbox);
207 fmt_scan (struct format *format, char *scanl, int width, int *dat)
210 char *savestr, *str = NULL;
211 char buffer[BUFSIZ], buffer2[BUFSIZ];
221 ep = scanl + width - 1;
223 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
224 switch (fmt->f_type) {
227 fmt->f_comp->c_flags &= ~CF_PARSED;
234 switch (fmt->f_type) {
237 PUTS (cp, fmt->f_comp->c_text);
240 cp = PUTSF(cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill);
245 while( (c = *sp++) && cp < ep)
254 ljust++; /* XXX should do something with this */
256 while( (c = *sp++) && --i >= 0 && cp < ep)
258 while( --i >= 0 && cp < ep)
266 cp =PUTSF (cp, str, fmt->f_width, fmt->f_fill);
269 adios (NULL, "internal error (FT_STRFW)");
272 n = snprintf(cp, ep - cp, "%d", value);
276 PUTDF (cp, value, fmt->f_width, fmt->f_fill);
287 if (!(value = (str && *str))) {
294 if (!(value = (str == NULL || *str == 0))) {
301 if (value != fmt->f_value) {
308 if (value == fmt->f_value) {
315 if (value <= fmt->f_value) {
322 if (!(value = (str && match (str, fmt->f_text)))) {
330 value = match (str, fmt->f_text);
336 if (!(value = (str && uprf (str, fmt->f_text)))) {
343 value = uprf (str, fmt->f_text);
347 value = (str != NULL && *str != 0);
351 value = (str == NULL || *str == 0);
355 value = (fmt->f_value == value);
359 value = (fmt->f_value != value);
363 value = (fmt->f_value > value);
374 str = fmt->f_comp->c_text;
380 if (!(str = getenv (fmt->f_text)))
384 if (!(str = context_find (fmt->f_text)))
388 case FT_LS_DECODECOMP:
389 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
392 str = fmt->f_comp->c_text;
396 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
404 strncpy(buffer, str, sizeof(buffer));
405 buffer[sizeof(buffer)-1] = '\0';
407 while (isspace(*str))
410 if ((i = fmt->f_width) < 0) {
415 if (!ljust && i > 0 && strlen(str) > i)
418 xp += strlen(str) - 1;
419 while (xp > str && isspace(*xp))
421 if (ljust && i > 0 && strlen(str) > i)
422 str += strlen(str) - i;
427 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
430 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
433 value = fmt->f_value;
436 value = dat[fmt->f_value];
444 case FT_LV_CHAR_LEFT:
445 value = width - (cp - scanl);
448 value += fmt->f_value;
451 value = fmt->f_value - value;
455 value = value / fmt->f_value;
461 value = value % fmt->f_value;
470 value = fmt->f_comp->c_tws->tw_sec;
473 value = fmt->f_comp->c_tws->tw_min;
476 value = fmt->f_comp->c_tws->tw_hour;
479 value = fmt->f_comp->c_tws->tw_mday;
482 value = fmt->f_comp->c_tws->tw_mon + 1;
485 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
488 str = lmonth[fmt->f_comp->c_tws->tw_mon];
491 str = dtwszone (fmt->f_comp->c_tws);
494 value = fmt->f_comp->c_tws->tw_year;
497 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
499 value = tws->tw_wday;
502 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
504 str = tw_dotw[tws->tw_wday];
507 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
509 str = tw_ldotw[tws->tw_wday];
512 value = fmt->f_comp->c_tws->tw_yday;
515 value = fmt->f_comp->c_tws->tw_zone;
518 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
519 value = dmktime(fmt->f_comp->c_tws);
522 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
523 value = dmktime(fmt->f_comp->c_tws);
524 value = time((time_t *) 0) - value;
527 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
529 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
538 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
544 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
547 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
550 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
554 str = fmt->f_comp->c_mn->m_pers;
557 str = fmt->f_comp->c_mn->m_mbox;
560 str = fmt->f_comp->c_mn->m_host;
563 str = fmt->f_comp->c_mn->m_path;
566 str = fmt->f_comp->c_mn->m_gname;
569 str = fmt->f_comp->c_mn->m_note;
572 str = adrformat( fmt->f_comp->c_mn );
575 value = fmt->f_comp->c_mn->m_type;
578 value = fmt->f_comp->c_mn->m_ingrp;
581 value = fmt->f_comp->c_mn->m_nohost;
585 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
586 str = fmt->f_comp->c_text;
589 if (fmt->f_type == FT_LS_ADDR)
591 if ((str = mn->m_pers) == NULL) {
592 if ((str = mn->m_note)) {
593 strncpy (buffer, str, sizeof(buffer));
594 buffer[sizeof(buffer)-1] = '\0';
598 sp = str + strlen(str) - 1;
607 } else if (!(str = get_x400_friendly (mn->m_mbox,
608 buffer, sizeof(buffer)))) {
610 switch (mn->m_type) {
615 snprintf (buffer, sizeof(buffer), "%s!%s",
616 mn->m_host, mn->m_mbox);
621 snprintf (buffer, sizeof(buffer), "%s@%s",
622 mn->m_mbox, mn->m_host);
634 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
638 strncpy(buffer, str, sizeof(buffer));
639 /* strncpy doesn't NUL-terminate if it fills the buffer */
640 buffer[sizeof(buffer)-1] = '\0';
643 /* we will parse from buffer to buffer2 */
644 n = 0; /* n is the input position in str */
645 m = 0; /* m is the ouput position in buffer2 */
647 while ( str[n] != '\0') {
652 buffer2[m++] = str[n++];
658 buffer2[m++] = str[n++];
669 if ((t = comp->c_tws->tw_clock) == 0)
670 t = dmktime(comp->c_tws);
671 tws = dlocaltime(&t);
677 if ((t = comp->c_tws->tw_clock) == 0)
678 t = dmktime(comp->c_tws);
685 if (comp->c_flags & CF_PARSED)
687 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
689 comp->c_flags &= ~CF_TRUE;
690 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
691 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
692 comp->c_flags = CF_TRUE;
694 comp->c_flags |= CF_PARSED;
698 /* hook for custom address list formatting (see replsbr.c) */
699 str = formataddr (savestr, str);
703 /* output the str register as an address component,
704 * splitting it into multiple lines if necessary. The
705 * value reg. contains the max line length. The lit.
706 * field may contain a string to prepend to the result
711 int indent, wid, len;
717 indent = strlen (sp);
719 while( (c = *sp++) && cp < ep)
722 /* try to break at a comma; failing that, break at a
725 lastb = 0; sp = lp + wid;
726 while (sp > lp && (c = *--sp) != ',') {
727 if (! lastb && isspace(c))
731 if (! (sp = lastb)) {
733 while (*sp && *sp != ',' && !isspace(*sp))
740 while (cp < ep && lp <= sp)
747 for (i=indent; cp < ep && i > 0; i--)
757 if (comp->c_flags & CF_PARSED)
759 if (comp->c_mn != &fmt_mnull)
761 if ((sp = comp->c_text) && (sp = getname(sp)) &&
762 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
766 comp->c_flags |= CF_PARSED;
768 while (getname("")) /* XXX */
770 comp->c_mn = &fmt_mnull;
776 * if there's no component, we say true. Otherwise we
777 * say "true" only if we can parse the address and it
778 * matches one of our addresses.
781 if (comp->c_mn != &fmt_mnull)
783 if ((sp = comp->c_text) && (sp = getname(sp)) &&
784 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
787 comp->c_flags |= CF_TRUE;
789 comp->c_flags &= ~CF_TRUE;
790 while ((sp = getname(sp)))
791 if ((comp->c_flags & CF_TRUE) == 0 &&
792 (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
794 comp->c_flags |= CF_TRUE;
796 while (getname("")) /* XXX */
798 if (comp->c_text == 0)
799 comp->c_flags |= CF_TRUE;
801 comp->c_flags &= ~CF_TRUE;
802 comp->c_mn = &fmt_mnull;
808 /* If we're working on a folder (as opposed to a file), add the
809 * current msg to sequence given in literal field. Don't
810 * disturb string or value registers.
812 if (fmt_current_folder)
813 seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
824 return ((struct format *)0);
828 while (fmt->f_type != FT_DONE)
833 return (fmt->f_value ? ++fmt : (struct format *) 0);