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
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 #define PUTD(cp, num)\
114 if ((i = (num)) == 0)\
117 if ((i = (num)) < 0) {\
124 while (cp < ep && c > 1) {\
126 *cp++ = (i / c) + '0';\
133 #define PUTSF(cp, str, wid, fill) {\
135 if ((i = (wid)) < 0) {\
145 while( --i >= c && cp < ep)\
150 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
153 while ((c = (unsigned char) *sp++) && --i >= 0 && cp < ep)\
157 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
163 while( --i >= 0 && cp < ep)\
167 #define PUTS(cp, str) {\
169 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
171 while((c = (unsigned char) *sp++) && cp < ep)\
175 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
183 #define PUTSF(cp, str, wid, fill) {\
185 if ((i = (wid)) < 0) {\
195 while( --i >= c && cp < ep)\
200 while ((c = *sp) && c <= 32)\
203 while ((c = *sp++) && --i >= 0 && cp < ep)\
207 while ((c = *sp) && c <= 32)\
213 while( --i >= 0 && cp < ep)\
217 #define PUTS(cp, str) {\
219 while ((c = *sp) && c <= 32)\
221 while( (c = *sp++) && cp < ep)\
225 while ( (c = *sp) && c <= 32 )\
235 static char *lmonth[] = { "January", "February","March", "April",
236 "May", "June", "July", "August",
237 "September","October", "November","December" };
240 get_x400_friendly (char *mbox, char *buffer, int buffer_len)
242 char given[BUFSIZ], surname[BUFSIZ];
251 if (get_x400_comp (mbox, "/PN=", buffer, buffer_len)) {
252 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
258 if (!get_x400_comp (mbox, "/S=", surname, sizeof(surname)))
261 if (get_x400_comp (mbox, "/G=", given, sizeof(given)))
262 snprintf (buffer, buffer_len, "%s %s", given, surname);
264 snprintf (buffer, buffer_len, "%s", surname);
270 get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len)
275 if ((idx = stringdex (key, mbox)) < 0
276 || !(cp = strchr(mbox += idx + strlen (key), '/')))
279 snprintf (buffer, buffer_len, "%*.*s", cp - mbox, cp - mbox, mbox);
284 fmt_scan (struct format *format, char *scanl, int width, int *dat)
287 char *savestr, *str = NULL;
288 char buffer[BUFSIZ], buffer2[BUFSIZ];
298 ep = scanl + width - 1;
302 switch (fmt->f_type) {
305 PUTS (cp, fmt->f_comp->c_text);
308 PUTSF (cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill);
313 while( (c = *sp++) && cp < ep)
322 ljust++; /* XXX should do something with this */
324 while( (c = *sp++) && --i >= 0 && cp < ep)
326 while( --i >= 0 && cp < ep)
334 PUTSF (cp, str, fmt->f_width, fmt->f_fill);
337 adios (NULL, "internal error (FT_STRFW)");
343 PUTDF (cp, value, fmt->f_width, fmt->f_fill);
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))
459 str = fmt->f_comp->c_text;
463 if (str && decode_rfc2047(str, buffer2))
471 strncpy(buffer, str, sizeof(buffer));
473 while (isspace(*str))
476 if ((i = fmt->f_width) < 0) {
481 if (!ljust && i > 0 && strlen(str) > i)
484 xp += strlen(str) - 1;
485 while (xp > str && isspace(*xp))
487 if (ljust && i > 0 && strlen(str) > i)
488 str += strlen(str) - i;
493 value = fmt->f_comp->c_flags;
496 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
499 value = fmt->f_value;
502 value = dat[fmt->f_value];
507 case FT_LV_CHAR_LEFT:
508 value = width - (cp - scanl);
511 value += fmt->f_value;
514 value = fmt->f_value - value;
518 value = value / fmt->f_value;
524 value = value % fmt->f_value;
533 value = fmt->f_comp->c_tws->tw_sec;
536 value = fmt->f_comp->c_tws->tw_min;
539 value = fmt->f_comp->c_tws->tw_hour;
542 value = fmt->f_comp->c_tws->tw_mday;
545 value = fmt->f_comp->c_tws->tw_mon + 1;
548 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
551 str = lmonth[fmt->f_comp->c_tws->tw_mon];
554 str = dtwszone (fmt->f_comp->c_tws);
557 value = fmt->f_comp->c_tws->tw_year;
560 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
562 value = tws->tw_wday;
565 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
567 str = tw_dotw[tws->tw_wday];
570 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
572 str = tw_ldotw[tws->tw_wday];
575 value = fmt->f_comp->c_tws->tw_yday;
578 value = fmt->f_comp->c_tws->tw_zone;
581 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
582 value = dmktime(fmt->f_comp->c_tws);
585 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
586 value = dmktime(fmt->f_comp->c_tws);
587 value = time((time_t *) 0) - value;
590 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
592 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
601 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
607 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
610 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
613 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
617 str = fmt->f_comp->c_mn->m_pers;
620 str = fmt->f_comp->c_mn->m_mbox;
623 str = fmt->f_comp->c_mn->m_host;
626 str = fmt->f_comp->c_mn->m_path;
629 str = fmt->f_comp->c_mn->m_gname;
632 str = fmt->f_comp->c_mn->m_note;
635 str = adrformat( fmt->f_comp->c_mn );
638 value = fmt->f_comp->c_mn->m_type;
641 value = fmt->f_comp->c_mn->m_ingrp;
644 value = fmt->f_comp->c_mn->m_nohost;
648 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
649 str = fmt->f_comp->c_text;
652 if (fmt->f_type == FT_LS_ADDR)
654 if ((str = mn->m_pers) == NULL) {
655 if ((str = mn->m_note)) {
656 strncpy (buffer, str, sizeof(buffer));
660 sp = str + strlen(str) - 1;
669 } else if (!(str = get_x400_friendly (mn->m_mbox,
670 buffer, sizeof(buffer)))) {
672 switch (mn->m_type) {
677 snprintf (buffer, sizeof(buffer), "%s!%s",
678 mn->m_host, mn->m_mbox);
683 snprintf (buffer, sizeof(buffer), "%s@%s",
684 mn->m_mbox, mn->m_host);
697 if ((t = comp->c_tws->tw_clock) == 0)
698 t = dmktime(comp->c_tws);
699 tws = dlocaltime(&t);
705 if ((t = comp->c_tws->tw_clock) == 0)
706 t = dmktime(comp->c_tws);
713 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
716 } else if (comp->c_flags >= 0) {
717 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
723 /* hook for custom address list formatting (see replsbr.c) */
724 str = formataddr (savestr, str);
728 /* output the str register as an address component,
729 * splitting it into multiple lines if necessary. The
730 * value reg. contains the max line length. The lit.
731 * field may contain a string to prepend to the result
736 int indent, wid, len;
742 indent = strlen (sp);
744 while( (c = *sp++) && cp < ep)
747 /* try to break at a comma; failing that, break at a
750 lastb = 0; sp = lp + wid;
751 while (sp > lp && (c = *--sp) != ',') {
752 if (! lastb && isspace(c))
756 if (! (sp = lastb)) {
758 while (*sp && *sp != ',' && !isspace(*sp))
765 while (cp < ep && lp <= sp)
772 for (i=indent; cp < ep && i > 0; i--)
782 if (comp->c_mn != &fmt_mnull)
784 if ((sp = comp->c_text) && (sp = getname(sp)) &&
785 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
790 while (getname("")) /* XXX */
792 comp->c_mn = &fmt_mnull;
798 * if there's no component, we say true. Otherwise we
799 * say "true" only if we can parse the address and it
800 * matches one of our addresses.
803 if (comp->c_mn != &fmt_mnull)
805 if ((sp = comp->c_text) && (sp = getname(sp)) &&
806 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
808 comp->c_flags = ismymbox(mn);
809 while ((sp = getname(sp)))
810 if (comp->c_flags == 0 &&
811 (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
812 comp->c_flags |= ismymbox(mn);
814 while (getname("")) /* XXX */
816 comp->c_flags = (comp->c_text == 0);
817 comp->c_mn = &fmt_mnull;
823 /* If we're working on a folder (as opposed to a file), add the
824 * current msg to sequence given in literal field. Don't
825 * disturb string or value registers.
827 if (fmt_current_folder)
828 seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
839 return ((struct format *)0);
843 while (fmt->f_type != FT_DONE)
848 return (fmt->f_value ? ++fmt : (struct format *) 0);