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))
448 str = fmt->f_comp->c_text;
452 if (str && decode_rfc2047(str, buffer2))
460 strncpy(buffer, str, sizeof(buffer));
462 while (isspace(*str))
465 if ((i = fmt->f_width) < 0) {
470 if (!ljust && i > 0 && strlen(str) > i)
473 xp += strlen(str) - 1;
474 while (xp > str && isspace(*xp))
476 if (ljust && i > 0 && strlen(str) > i)
477 str += strlen(str) - i;
482 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
485 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
488 value = fmt->f_value;
491 value = dat[fmt->f_value];
499 case FT_LV_CHAR_LEFT:
500 value = width - (cp - scanl);
503 value += fmt->f_value;
506 value = fmt->f_value - value;
510 value = value / fmt->f_value;
516 value = value % fmt->f_value;
525 value = fmt->f_comp->c_tws->tw_sec;
528 value = fmt->f_comp->c_tws->tw_min;
531 value = fmt->f_comp->c_tws->tw_hour;
534 value = fmt->f_comp->c_tws->tw_mday;
537 value = fmt->f_comp->c_tws->tw_mon + 1;
540 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
543 str = lmonth[fmt->f_comp->c_tws->tw_mon];
546 str = dtwszone (fmt->f_comp->c_tws);
549 value = fmt->f_comp->c_tws->tw_year;
552 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
554 value = tws->tw_wday;
557 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
559 str = tw_dotw[tws->tw_wday];
562 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
564 str = tw_ldotw[tws->tw_wday];
567 value = fmt->f_comp->c_tws->tw_yday;
570 value = fmt->f_comp->c_tws->tw_zone;
573 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
574 value = dmktime(fmt->f_comp->c_tws);
577 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
578 value = dmktime(fmt->f_comp->c_tws);
579 value = time((time_t *) 0) - value;
582 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
584 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
593 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
599 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
602 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
605 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
609 str = fmt->f_comp->c_mn->m_pers;
612 str = fmt->f_comp->c_mn->m_mbox;
615 str = fmt->f_comp->c_mn->m_host;
618 str = fmt->f_comp->c_mn->m_path;
621 str = fmt->f_comp->c_mn->m_gname;
624 str = fmt->f_comp->c_mn->m_note;
627 str = adrformat( fmt->f_comp->c_mn );
630 value = fmt->f_comp->c_mn->m_type;
633 value = fmt->f_comp->c_mn->m_ingrp;
636 value = fmt->f_comp->c_mn->m_nohost;
640 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
641 str = fmt->f_comp->c_text;
644 if (fmt->f_type == FT_LS_ADDR)
646 if ((str = mn->m_pers) == NULL) {
647 if ((str = mn->m_note)) {
648 strncpy (buffer, str, sizeof(buffer));
652 sp = str + strlen(str) - 1;
661 } else if (!(str = get_x400_friendly (mn->m_mbox,
662 buffer, sizeof(buffer)))) {
664 switch (mn->m_type) {
669 snprintf (buffer, sizeof(buffer), "%s!%s",
670 mn->m_host, mn->m_mbox);
675 snprintf (buffer, sizeof(buffer), "%s@%s",
676 mn->m_mbox, mn->m_host);
688 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
692 strncpy(buffer, str, sizeof(buffer));
695 /* we will parse from buffer to buffer2 */
696 n = 0; /* n is the input position in str */
697 m = 0; /* m is the ouput position in buffer2 */
699 while ( str[n] != '\0') {
704 buffer2[m++] = str[n++];
710 buffer2[m++] = str[n++];
721 if ((t = comp->c_tws->tw_clock) == 0)
722 t = dmktime(comp->c_tws);
723 tws = dlocaltime(&t);
729 if ((t = comp->c_tws->tw_clock) == 0)
730 t = dmktime(comp->c_tws);
737 if (comp->c_flags & CF_PARSED)
739 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
741 comp->c_flags &= ~CF_TRUE;
742 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
743 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
744 comp->c_flags = CF_TRUE;
746 comp->c_flags |= CF_PARSED;
750 /* hook for custom address list formatting (see replsbr.c) */
751 str = formataddr (savestr, str);
755 /* output the str register as an address component,
756 * splitting it into multiple lines if necessary. The
757 * value reg. contains the max line length. The lit.
758 * field may contain a string to prepend to the result
763 int indent, wid, len;
769 indent = strlen (sp);
771 while( (c = *sp++) && cp < ep)
774 /* try to break at a comma; failing that, break at a
777 lastb = 0; sp = lp + wid;
778 while (sp > lp && (c = *--sp) != ',') {
779 if (! lastb && isspace(c))
783 if (! (sp = lastb)) {
785 while (*sp && *sp != ',' && !isspace(*sp))
792 while (cp < ep && lp <= sp)
799 for (i=indent; cp < ep && i > 0; i--)
809 if (comp->c_flags & CF_PARSED)
811 if (comp->c_mn != &fmt_mnull)
813 if ((sp = comp->c_text) && (sp = getname(sp)) &&
814 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
818 comp->c_flags |= CF_PARSED;
820 while (getname("")) /* XXX */
822 comp->c_mn = &fmt_mnull;
828 * if there's no component, we say true. Otherwise we
829 * say "true" only if we can parse the address and it
830 * matches one of our addresses.
833 if (comp->c_mn != &fmt_mnull)
835 if ((sp = comp->c_text) && (sp = getname(sp)) &&
836 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
839 comp->c_flags |= CF_TRUE;
841 comp->c_flags &= ~CF_TRUE;
842 while ((sp = getname(sp)))
843 if ((comp->c_flags & CF_TRUE) == 0 &&
844 (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
846 comp->c_flags |= CF_TRUE;
848 while (getname("")) /* XXX */
850 if (comp->c_text == 0)
851 comp->c_flags |= CF_TRUE;
853 comp->c_flags &= ~CF_TRUE;
854 comp->c_mn = &fmt_mnull;
860 /* If we're working on a folder (as opposed to a file), add the
861 * current msg to sequence given in literal field. Don't
862 * disturb string or value registers.
864 if (fmt_current_folder)
865 seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
876 return ((struct format *)0);
880 while (fmt->f_type != FT_DONE)
885 return (fmt->f_value ? ++fmt : (struct format *) 0);