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
91 #define PUTDF(cp, num, wid, fill)\
99 *--sp = (i % 10) + '0';\
101 } while (i > 0 && sp > cp);\
104 else if ((num) < 0 && sp > cp)\
112 #define PUTSF(cp, str, wid, fill) {\
114 if ((i = (wid)) < 0) {\
124 while( --i >= c && cp < ep)\
129 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
132 while ((c = (unsigned char) *sp++) && --i >= 0 && cp < ep)\
133 if (!iscntrl(c) && !isspace(c)) \
136 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
142 while( --i >= 0 && cp < ep)\
146 #define PUTS(cp, str) {\
148 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
150 while((c = (unsigned char) *sp++) && cp < ep)\
151 if (!iscntrl(c) && !isspace(c)) \
154 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
162 #define PUTSF(cp, str, wid, fill) {\
164 if ((i = (wid)) < 0) {\
174 while( --i >= c && cp < ep)\
179 while ((c = *sp) && c <= 32)\
182 while ((c = *sp++) && --i >= 0 && cp < ep)\
186 while ((c = *sp) && c <= 32)\
192 while( --i >= 0 && cp < ep)\
196 #define PUTS(cp, str) {\
198 while ((c = *sp) && c <= 32)\
200 while( (c = *sp++) && cp < ep)\
204 while ( (c = *sp) && c <= 32 )\
214 static char *lmonth[] = { "January", "February","March", "April",
215 "May", "June", "July", "August",
216 "September","October", "November","December" };
219 get_x400_friendly (char *mbox, char *buffer, int buffer_len)
221 char given[BUFSIZ], surname[BUFSIZ];
230 if (get_x400_comp (mbox, "/PN=", buffer, buffer_len)) {
231 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
237 if (!get_x400_comp (mbox, "/S=", surname, sizeof(surname)))
240 if (get_x400_comp (mbox, "/G=", given, sizeof(given)))
241 snprintf (buffer, buffer_len, "%s %s", given, surname);
243 snprintf (buffer, buffer_len, "%s", surname);
249 get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len)
254 if ((idx = stringdex (key, mbox)) < 0
255 || !(cp = strchr(mbox += idx + strlen (key), '/')))
258 snprintf (buffer, buffer_len, "%*.*s", cp - mbox, cp - mbox, mbox);
263 fmt_scan (struct format *format, char *scanl, int width, int *dat)
266 char *savestr, *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 PUTS (cp, fmt->f_comp->c_text);
296 PUTSF (cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill);
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)
322 PUTSF (cp, str, fmt->f_width, fmt->f_fill);
325 adios (NULL, "internal error (FT_STRFW)");
328 n = snprintf(cp, ep - cp, "%d", value);
332 PUTDF (cp, value, fmt->f_width, fmt->f_fill);
343 if (!(value = (str && *str))) {
350 if (!(value = (str == NULL || *str == 0))) {
357 if (value != fmt->f_value) {
364 if (value == fmt->f_value) {
371 if (value <= fmt->f_value) {
378 if (!(value = (str && match (str, fmt->f_text)))) {
386 value = match (str, fmt->f_text);
392 if (!(value = (str && uprf (str, fmt->f_text)))) {
399 value = uprf (str, fmt->f_text);
403 value = (str != NULL && *str != 0);
407 value = (str == NULL || *str == 0);
411 value = (fmt->f_value == value);
415 value = (fmt->f_value != value);
419 value = (fmt->f_value > value);
430 str = fmt->f_comp->c_text;
436 if (!(str = getenv (fmt->f_text)))
440 if (!(str = context_find (fmt->f_text)))
444 case FT_LS_DECODECOMP:
445 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
448 str = fmt->f_comp->c_text;
452 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
460 strncpy(buffer, str, sizeof(buffer));
461 buffer[sizeof(buffer)-1] = '\0';
463 while (isspace(*str))
466 if ((i = fmt->f_width) < 0) {
471 if (!ljust && i > 0 && strlen(str) > i)
474 xp += strlen(str) - 1;
475 while (xp > str && isspace(*xp))
477 if (ljust && i > 0 && strlen(str) > i)
478 str += strlen(str) - i;
483 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
486 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
489 value = fmt->f_value;
492 value = dat[fmt->f_value];
500 case FT_LV_CHAR_LEFT:
501 value = width - (cp - scanl);
504 value += fmt->f_value;
507 value = fmt->f_value - value;
511 value = value / fmt->f_value;
517 value = value % fmt->f_value;
526 value = fmt->f_comp->c_tws->tw_sec;
529 value = fmt->f_comp->c_tws->tw_min;
532 value = fmt->f_comp->c_tws->tw_hour;
535 value = fmt->f_comp->c_tws->tw_mday;
538 value = fmt->f_comp->c_tws->tw_mon + 1;
541 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
544 str = lmonth[fmt->f_comp->c_tws->tw_mon];
547 str = dtwszone (fmt->f_comp->c_tws);
550 value = fmt->f_comp->c_tws->tw_year;
553 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
555 value = tws->tw_wday;
558 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
560 str = tw_dotw[tws->tw_wday];
563 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
565 str = tw_ldotw[tws->tw_wday];
568 value = fmt->f_comp->c_tws->tw_yday;
571 value = fmt->f_comp->c_tws->tw_zone;
574 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
575 value = dmktime(fmt->f_comp->c_tws);
578 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
579 value = dmktime(fmt->f_comp->c_tws);
580 value = time((time_t *) 0) - value;
583 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
585 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
594 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
600 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
603 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
606 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
610 str = fmt->f_comp->c_mn->m_pers;
613 str = fmt->f_comp->c_mn->m_mbox;
616 str = fmt->f_comp->c_mn->m_host;
619 str = fmt->f_comp->c_mn->m_path;
622 str = fmt->f_comp->c_mn->m_gname;
625 str = fmt->f_comp->c_mn->m_note;
628 str = adrformat( fmt->f_comp->c_mn );
631 value = fmt->f_comp->c_mn->m_type;
634 value = fmt->f_comp->c_mn->m_ingrp;
637 value = fmt->f_comp->c_mn->m_nohost;
641 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
642 str = fmt->f_comp->c_text;
645 if (fmt->f_type == FT_LS_ADDR)
647 if ((str = mn->m_pers) == NULL) {
648 if ((str = mn->m_note)) {
649 strncpy (buffer, str, sizeof(buffer));
650 buffer[sizeof(buffer)-1] = '\0';
654 sp = str + strlen(str) - 1;
663 } else if (!(str = get_x400_friendly (mn->m_mbox,
664 buffer, sizeof(buffer)))) {
666 switch (mn->m_type) {
671 snprintf (buffer, sizeof(buffer), "%s!%s",
672 mn->m_host, mn->m_mbox);
677 snprintf (buffer, sizeof(buffer), "%s@%s",
678 mn->m_mbox, mn->m_host);
690 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
694 strncpy(buffer, str, sizeof(buffer));
695 /* strncpy doesn't NUL-terminate if it fills the buffer */
696 buffer[sizeof(buffer)-1] = '\0';
699 /* we will parse from buffer to buffer2 */
700 n = 0; /* n is the input position in str */
701 m = 0; /* m is the ouput position in buffer2 */
703 while ( str[n] != '\0') {
708 buffer2[m++] = str[n++];
714 buffer2[m++] = str[n++];
725 if ((t = comp->c_tws->tw_clock) == 0)
726 t = dmktime(comp->c_tws);
727 tws = dlocaltime(&t);
733 if ((t = comp->c_tws->tw_clock) == 0)
734 t = dmktime(comp->c_tws);
741 if (comp->c_flags & CF_PARSED)
743 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
745 comp->c_flags &= ~CF_TRUE;
746 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
747 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
748 comp->c_flags = CF_TRUE;
750 comp->c_flags |= CF_PARSED;
754 /* hook for custom address list formatting (see replsbr.c) */
755 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
767 int indent, wid, len;
773 indent = strlen (sp);
775 while( (c = *sp++) && cp < ep)
778 /* try to break at a comma; failing that, break at a
781 lastb = 0; sp = lp + wid;
782 while (sp > lp && (c = *--sp) != ',') {
783 if (! lastb && isspace(c))
787 if (! (sp = lastb)) {
789 while (*sp && *sp != ',' && !isspace(*sp))
796 while (cp < ep && lp <= sp)
803 for (i=indent; cp < ep && i > 0; i--)
813 if (comp->c_flags & CF_PARSED)
815 if (comp->c_mn != &fmt_mnull)
817 if ((sp = comp->c_text) && (sp = getname(sp)) &&
818 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
822 comp->c_flags |= CF_PARSED;
824 while (getname("")) /* XXX */
826 comp->c_mn = &fmt_mnull;
832 * if there's no component, we say true. Otherwise we
833 * say "true" only if we can parse the address and it
834 * matches one of our addresses.
837 if (comp->c_mn != &fmt_mnull)
839 if ((sp = comp->c_text) && (sp = getname(sp)) &&
840 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
843 comp->c_flags |= CF_TRUE;
845 comp->c_flags &= ~CF_TRUE;
846 while ((sp = getname(sp)))
847 if ((comp->c_flags & CF_TRUE) == 0 &&
848 (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
850 comp->c_flags |= CF_TRUE;
852 while (getname("")) /* XXX */
854 if (comp->c_text == 0)
855 comp->c_flags |= CF_TRUE;
857 comp->c_flags &= ~CF_TRUE;
858 comp->c_mn = &fmt_mnull;
864 /* If we're working on a folder (as opposed to a file), add the
865 * current msg to sequence given in literal field. Don't
866 * disturb string or value registers.
868 if (fmt_current_folder)
869 seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
880 return ((struct format *)0);
884 while (fmt->f_type != FT_DONE)
889 return (fmt->f_value ? ++fmt : (struct format *) 0);