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>
31 extern char *formataddr (); /* hook for custom address formatting */
34 struct msgs *fmt_current_folder; /* current folder (set by main program) */
37 extern int fmt_norm; /* defined in sbr/fmt_def.c = AD_NAME */
38 struct mailname fmt_mnull;
43 static int match (char *, char *);
44 static char *get_x400_friendly (char *, char *, int);
45 static int get_x400_comp (char *, char *, char *, int);
49 * test if string "sub" appears anywhere in
50 * string "str" (case insensitive).
54 match (char *str, char *sub)
61 c1 = (isalpha(c1) && isupper(c1)) ? tolower(c1) : c1;
62 while ((c2 = *str++) && c1 != ((isalpha(c2) && isupper(c2)) ? tolower(c2) : c2))
66 s1 = sub + 1; s2 = str;
67 while ((c1 = *s1++) && ((isalpha(c1) && isupper(c1)) ? tolower(c1) : c1) == ((isalpha(c2 =*s2++) && isupper(c2)) ? tolower(c2) : c2))
74 while ((c2 = *str++) && (c1 | 040) != (c2 | 040))
78 s1 = sub + 1; s2 = str;
79 while ((c1 = *s1++) && (c1 | 040) == (*s2++ | 040))
89 * macros to format data
93 #define PUTSF(cp, str, wid, fill) {\
95 if ((i = (wid)) < 0) {\
105 while( --i >= c && cp < ep)\
110 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
113 while ((c = (unsigned char) *sp++) && --i >= 0 && cp < ep)\
117 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
123 while( --i >= 0 && cp < ep)\
127 #define PUTS(cp, str) {\
129 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
131 while((c = (unsigned char) *sp++) && cp < ep)\
135 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
143 #define PUTSF(cp, str, wid, fill) {\
145 if ((i = (wid)) < 0) {\
155 while( --i >= c && cp < ep)\
160 while ((c = *sp) && c <= 32)\
163 while ((c = *sp++) && --i >= 0 && cp < ep)\
167 while ((c = *sp) && c <= 32)\
173 while( --i >= 0 && cp < ep)\
177 #define PUTS(cp, str) {\
179 while ((c = *sp) && c <= 32)\
181 while( (c = *sp++) && cp < ep)\
185 while ( (c = *sp) && c <= 32 )\
195 static char *lmonth[] = { "January", "February","March", "April",
196 "May", "June", "July", "August",
197 "September","October", "November","December" };
200 get_x400_friendly (char *mbox, char *buffer, int buffer_len)
202 char given[BUFSIZ], surname[BUFSIZ];
211 if (get_x400_comp (mbox, "/PN=", buffer, buffer_len)) {
212 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
218 if (!get_x400_comp (mbox, "/S=", surname, sizeof(surname)))
221 if (get_x400_comp (mbox, "/G=", given, sizeof(given)))
222 snprintf (buffer, buffer_len, "%s %s", given, surname);
224 snprintf (buffer, buffer_len, "%s", surname);
230 get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len)
235 if ((idx = stringdex (key, mbox)) < 0
236 || !(cp = strchr(mbox += idx + strlen (key), '/')))
239 snprintf (buffer, buffer_len, "%*.*s", cp - mbox, cp - mbox, mbox);
244 fmt_scan (struct format *format, char *scanl, int width, int *dat)
247 char *savestr, *str = NULL;
248 char buffer[BUFSIZ], buffer2[BUFSIZ];
258 ep = scanl + width - 1;
260 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
261 switch (fmt->f_type) {
264 fmt->f_comp->c_flags &= ~CF_PARSED;
271 switch (fmt->f_type) {
274 PUTS (cp, fmt->f_comp->c_text);
277 PUTSF (cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill);
282 while( (c = *sp++) && cp < ep)
291 ljust++; /* XXX should do something with this */
293 while( (c = *sp++) && --i >= 0 && cp < ep)
295 while( --i >= 0 && cp < ep)
303 PUTSF (cp, str, fmt->f_width, fmt->f_fill);
306 adios (NULL, "internal error (FT_STRFW)");
309 n = snprintf(cp, ep - cp, "%d", value);
313 n = snprintf(cp, ep - cp, "%d", value);
317 c = abs(fmt->f_width) - n;
318 for (; (c > 0) && (cp < ep); c--) *cp++ = fmt->f_fill;
329 if (!(value = (str && *str))) {
336 if (!(value = (str == NULL || *str == 0))) {
343 if (value != fmt->f_value) {
350 if (value == fmt->f_value) {
357 if (value <= fmt->f_value) {
364 if (!(value = (str && match (str, fmt->f_text)))) {
372 value = match (str, fmt->f_text);
378 if (!(value = (str && uprf (str, fmt->f_text)))) {
385 value = uprf (str, fmt->f_text);
389 value = (str != NULL && *str != 0);
393 value = (str == NULL || *str == 0);
397 value = (fmt->f_value == value);
401 value = (fmt->f_value != value);
405 value = (fmt->f_value > value);
416 str = fmt->f_comp->c_text;
422 if (!(str = getenv (fmt->f_text)))
426 if (!(str = context_find (fmt->f_text)))
430 case FT_LS_DECODECOMP:
431 if (decode_rfc2047(fmt->f_comp->c_text, buffer2))
434 str = fmt->f_comp->c_text;
438 if (str && decode_rfc2047(str, buffer2))
446 strncpy(buffer, str, sizeof(buffer));
448 while (isspace(*str))
451 if ((i = fmt->f_width) < 0) {
456 if (!ljust && i > 0 && strlen(str) > i)
459 xp += strlen(str) - 1;
460 while (xp > str && isspace(*xp))
462 if (ljust && i > 0 && strlen(str) > i)
463 str += strlen(str) - i;
468 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
471 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
474 value = fmt->f_value;
477 value = dat[fmt->f_value];
485 case FT_LV_CHAR_LEFT:
486 value = width - (cp - scanl);
489 value += fmt->f_value;
492 value = fmt->f_value - value;
496 value = value / fmt->f_value;
502 value = value % fmt->f_value;
511 value = fmt->f_comp->c_tws->tw_sec;
514 value = fmt->f_comp->c_tws->tw_min;
517 value = fmt->f_comp->c_tws->tw_hour;
520 value = fmt->f_comp->c_tws->tw_mday;
523 value = fmt->f_comp->c_tws->tw_mon + 1;
526 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
529 str = lmonth[fmt->f_comp->c_tws->tw_mon];
532 str = dtwszone (fmt->f_comp->c_tws);
535 value = fmt->f_comp->c_tws->tw_year;
538 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
540 value = tws->tw_wday;
543 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
545 str = tw_dotw[tws->tw_wday];
548 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
550 str = tw_ldotw[tws->tw_wday];
553 value = fmt->f_comp->c_tws->tw_yday;
556 value = fmt->f_comp->c_tws->tw_zone;
559 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
560 value = dmktime(fmt->f_comp->c_tws);
563 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
564 value = dmktime(fmt->f_comp->c_tws);
565 value = time((time_t *) 0) - value;
568 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
570 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
579 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
585 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
588 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
591 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
595 str = fmt->f_comp->c_mn->m_pers;
598 str = fmt->f_comp->c_mn->m_mbox;
601 str = fmt->f_comp->c_mn->m_host;
604 str = fmt->f_comp->c_mn->m_path;
607 str = fmt->f_comp->c_mn->m_gname;
610 str = fmt->f_comp->c_mn->m_note;
613 str = adrformat( fmt->f_comp->c_mn );
616 value = fmt->f_comp->c_mn->m_type;
619 value = fmt->f_comp->c_mn->m_ingrp;
622 value = fmt->f_comp->c_mn->m_nohost;
626 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
627 str = fmt->f_comp->c_text;
630 if (fmt->f_type == FT_LS_ADDR)
632 if ((str = mn->m_pers) == NULL) {
633 if ((str = mn->m_note)) {
634 strncpy (buffer, str, sizeof(buffer));
638 sp = str + strlen(str) - 1;
647 } else if (!(str = get_x400_friendly (mn->m_mbox,
648 buffer, sizeof(buffer)))) {
650 switch (mn->m_type) {
655 snprintf (buffer, sizeof(buffer), "%s!%s",
656 mn->m_host, mn->m_mbox);
661 snprintf (buffer, sizeof(buffer), "%s@%s",
662 mn->m_mbox, mn->m_host);
675 if ((t = comp->c_tws->tw_clock) == 0)
676 t = dmktime(comp->c_tws);
677 tws = dlocaltime(&t);
683 if ((t = comp->c_tws->tw_clock) == 0)
684 t = dmktime(comp->c_tws);
691 if (comp->c_flags & CF_PARSED)
693 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
695 comp->c_flags &= ~CF_TRUE;
696 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
697 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
698 comp->c_flags = CF_TRUE;
700 comp->c_flags |= CF_PARSED;
704 /* hook for custom address list formatting (see replsbr.c) */
705 str = formataddr (savestr, str);
709 /* output the str register as an address component,
710 * splitting it into multiple lines if necessary. The
711 * value reg. contains the max line length. The lit.
712 * field may contain a string to prepend to the result
717 int indent, wid, len;
723 indent = strlen (sp);
725 while( (c = *sp++) && cp < ep)
728 /* try to break at a comma; failing that, break at a
731 lastb = 0; sp = lp + wid;
732 while (sp > lp && (c = *--sp) != ',') {
733 if (! lastb && isspace(c))
737 if (! (sp = lastb)) {
739 while (*sp && *sp != ',' && !isspace(*sp))
746 while (cp < ep && lp <= sp)
753 for (i=indent; cp < ep && i > 0; i--)
763 if (comp->c_flags & CF_PARSED)
765 if (comp->c_mn != &fmt_mnull)
767 if ((sp = comp->c_text) && (sp = getname(sp)) &&
768 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
772 comp->c_flags |= CF_PARSED;
774 while (getname("")) /* XXX */
776 comp->c_mn = &fmt_mnull;
782 * if there's no component, we say true. Otherwise we
783 * say "true" only if we can parse the address and it
784 * matches one of our addresses.
787 if (comp->c_mn != &fmt_mnull)
789 if ((sp = comp->c_text) && (sp = getname(sp)) &&
790 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
793 comp->c_flags |= CF_TRUE;
795 comp->c_flags &= ~CF_TRUE;
796 while ((sp = getname(sp)))
797 if ((comp->c_flags & CF_TRUE) == 0 &&
798 (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
800 comp->c_flags |= CF_TRUE;
802 while (getname("")) /* XXX */
804 if (comp->c_text == 0)
805 comp->c_flags |= CF_TRUE;
807 comp->c_flags &= ~CF_TRUE;
808 comp->c_mn = &fmt_mnull;
814 /* If we're working on a folder (as opposed to a file), add the
815 * current msg to sequence given in literal field. Don't
816 * disturb string or value registers.
818 if (fmt_current_folder)
819 seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
830 return ((struct format *)0);
834 while (fmt->f_type != FT_DONE)
839 return (fmt->f_value ? ++fmt : (struct format *) 0);