3 * fmt_scan.c -- format string interpretation
10 #include <h/fmt_scan.h>
11 #include <zotnet/tws/tws.h>
12 #include <h/fmt_compile.h>
14 #ifdef TIME_WITH_SYS_TIME
15 # include <sys/time.h>
18 # ifdef TM_IN_SYS_TIME
19 # include <sys/time.h>
27 extern char *formataddr (); /* hook for custom address formatting */
30 struct msgs *fmt_current_folder; /* current folder (set by main program) */
33 extern int fmt_norm; /* defined in sbr/fmt_def.c = AD_NAME */
34 struct mailname fmt_mnull;
39 static int match (char *, char *);
40 static char *get_x400_friendly (char *, char *, int);
41 static int get_x400_comp (char *, char *, char *, int);
45 * test if string "sub" appears anywhere in
46 * string "str" (case insensitive).
50 match (char *str, char *sub)
57 c1 = (isalpha(c1) && isupper(c1)) ? tolower(c1) : c1;
58 while ((c2 = *str++) && c1 != ((isalpha(c2) && isupper(c2)) ? tolower(c2) : c2))
62 s1 = sub + 1; s2 = str;
63 while ((c1 = *s1++) && ((isalpha(c1) && isupper(c1)) ? tolower(c1) : c1) == ((isalpha(c2 =*s2++) && isupper(c2)) ? tolower(c2) : c2))
70 while ((c2 = *str++) && (c1 | 040) != (c2 | 040))
74 s1 = sub + 1; s2 = str;
75 while ((c1 = *s1++) && (c1 | 040) == (*s2++ | 040))
85 * macros to format data
88 #define PUTDF(cp, num, wid, fill)\
96 *--sp = (i % 10) + '0';\
98 } while (i > 0 && sp > cp);\
101 else if ((num) < 0 && sp > cp)\
108 #define PUTD(cp, num)\
110 if ((i = (num)) == 0)\
113 if ((i = (num)) < 0) {\
120 while (cp < ep && c > 1) {\
122 *cp++ = (i / c) + '0';\
129 #define PUTSF(cp, str, wid, fill) {\
131 if ((i = (wid)) < 0) {\
141 while( --i >= c && cp < ep)\
146 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
149 while ((c = (unsigned char) *sp++) && --i >= 0 && cp < ep)\
153 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
159 while( --i >= 0 && cp < ep)\
163 #define PUTS(cp, str) {\
165 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
167 while((c = (unsigned char) *sp++) && cp < ep)\
171 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
179 #define PUTSF(cp, str, wid, fill) {\
181 if ((i = (wid)) < 0) {\
191 while( --i >= c && cp < ep)\
196 while ((c = *sp) && c <= 32)\
199 while ((c = *sp++) && --i >= 0 && cp < ep)\
203 while ((c = *sp) && c <= 32)\
209 while( --i >= 0 && cp < ep)\
213 #define PUTS(cp, str) {\
215 while ((c = *sp) && c <= 32)\
217 while( (c = *sp++) && cp < ep)\
221 while ( (c = *sp) && c <= 32 )\
231 static char *lmonth[] = { "January", "February","March", "April",
232 "May", "June", "July", "August",
233 "September","October", "November","December" };
236 get_x400_friendly (char *mbox, char *buffer, int buffer_len)
238 char given[BUFSIZ], surname[BUFSIZ];
247 if (get_x400_comp (mbox, "/PN=", buffer, buffer_len)) {
248 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
254 if (!get_x400_comp (mbox, "/S=", surname, sizeof(surname)))
257 if (get_x400_comp (mbox, "/G=", given, sizeof(given)))
258 snprintf (buffer, buffer_len, "%s %s", given, surname);
260 snprintf (buffer, buffer_len, "%s", surname);
266 get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len)
271 if ((idx = stringdex (key, mbox)) < 0
272 || !(cp = strchr(mbox += idx + strlen (key), '/')))
275 snprintf (buffer, buffer_len, "%*.*s", cp - mbox, cp - mbox, mbox);
280 fmt_scan (struct format *format, char *scanl, int width, int *dat)
283 char *savestr, *str = NULL;
284 char buffer[BUFSIZ], buffer2[BUFSIZ];
294 ep = scanl + width - 1;
298 switch (fmt->f_type) {
301 PUTS (cp, fmt->f_comp->c_text);
304 PUTSF (cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill);
309 while( (c = *sp++) && cp < ep)
318 ljust++; /* XXX should do something with this */
320 while( (c = *sp++) && --i >= 0 && cp < ep)
322 while( --i >= 0 && cp < ep)
330 PUTSF (cp, str, fmt->f_width, fmt->f_fill);
333 adios (NULL, "internal error (FT_STRFW)");
339 PUTDF (cp, value, fmt->f_width, fmt->f_fill);
350 if (!(value = (str && *str))) {
357 if (!(value = (str == NULL || *str == 0))) {
364 if (value != fmt->f_value) {
371 if (value == fmt->f_value) {
378 if (value <= fmt->f_value) {
385 if (!(value = (str && match (str, fmt->f_text)))) {
393 value = match (str, fmt->f_text);
399 if (!(value = (str && uprf (str, fmt->f_text)))) {
406 value = uprf (str, fmt->f_text);
410 value = (str != NULL && *str != 0);
414 value = (str == NULL || *str == 0);
418 value = (fmt->f_value == value);
422 value = (fmt->f_value != value);
426 value = (fmt->f_value > value);
437 str = fmt->f_comp->c_text;
443 if (!(str = getenv (fmt->f_text)))
447 if (!(str = context_find (fmt->f_text)))
451 case FT_LS_DECODECOMP:
452 if (decode_rfc2047(fmt->f_comp->c_text, buffer2))
455 str = fmt->f_comp->c_text;
459 if (str && decode_rfc2047(str, buffer2))
467 strncpy(buffer, str, sizeof(buffer));
469 while (isspace(*str))
472 if ((i = fmt->f_width) < 0) {
477 if (!ljust && i > 0 && strlen(str) > i)
480 xp += strlen(str) - 1;
481 while (xp > str && isspace(*xp))
483 if (ljust && i > 0 && strlen(str) > i)
484 str += strlen(str) - i;
489 value = fmt->f_comp->c_flags;
492 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
495 value = fmt->f_value;
498 value = dat[fmt->f_value];
503 case FT_LV_CHAR_LEFT:
504 value = width - (cp - scanl);
507 value += fmt->f_value;
510 value = fmt->f_value - value;
514 value = value / fmt->f_value;
520 value = value % fmt->f_value;
529 value = fmt->f_comp->c_tws->tw_sec;
532 value = fmt->f_comp->c_tws->tw_min;
535 value = fmt->f_comp->c_tws->tw_hour;
538 value = fmt->f_comp->c_tws->tw_mday;
541 value = fmt->f_comp->c_tws->tw_mon + 1;
544 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
547 str = lmonth[fmt->f_comp->c_tws->tw_mon];
550 str = dtwszone (fmt->f_comp->c_tws);
553 value = fmt->f_comp->c_tws->tw_year;
556 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
558 value = tws->tw_wday;
561 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
563 str = tw_dotw[tws->tw_wday];
566 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
568 str = tw_ldotw[tws->tw_wday];
571 value = fmt->f_comp->c_tws->tw_yday;
574 value = fmt->f_comp->c_tws->tw_zone;
577 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
578 value = dmktime(fmt->f_comp->c_tws);
581 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
582 value = dmktime(fmt->f_comp->c_tws);
583 value = time((time_t *) 0) - value;
586 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
588 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
597 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
603 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
606 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
609 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
613 str = fmt->f_comp->c_mn->m_pers;
616 str = fmt->f_comp->c_mn->m_mbox;
619 str = fmt->f_comp->c_mn->m_host;
622 str = fmt->f_comp->c_mn->m_path;
625 str = fmt->f_comp->c_mn->m_gname;
628 str = fmt->f_comp->c_mn->m_note;
631 str = adrformat( fmt->f_comp->c_mn );
634 value = fmt->f_comp->c_mn->m_type;
637 value = fmt->f_comp->c_mn->m_ingrp;
640 value = fmt->f_comp->c_mn->m_nohost;
644 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
645 str = fmt->f_comp->c_text;
648 if (fmt->f_type == FT_LS_ADDR)
650 if ((str = mn->m_pers) == NULL) {
651 if ((str = mn->m_note)) {
652 strncpy (buffer, str, sizeof(buffer));
656 sp = str + strlen(str) - 1;
665 } else if (!(str = get_x400_friendly (mn->m_mbox,
666 buffer, sizeof(buffer)))) {
668 switch (mn->m_type) {
673 snprintf (buffer, sizeof(buffer), "%s!%s",
674 mn->m_host, mn->m_mbox);
679 snprintf (buffer, sizeof(buffer), "%s@%s",
680 mn->m_mbox, mn->m_host);
693 if ((t = comp->c_tws->tw_clock) == 0)
694 t = dmktime(comp->c_tws);
695 tws = dlocaltime(&t);
701 if ((t = comp->c_tws->tw_clock) == 0)
702 t = dmktime(comp->c_tws);
709 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
712 } else if (comp->c_flags >= 0) {
713 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
719 /* hook for custom address list formatting (see replsbr.c) */
720 str = formataddr (savestr, str);
724 /* output the str register as an address component,
725 * splitting it into multiple lines if necessary. The
726 * value reg. contains the max line length. The lit.
727 * field may contain a string to prepend to the result
732 int indent, wid, len;
738 indent = strlen (sp);
740 while( (c = *sp++) && cp < ep)
743 /* try to break at a comma; failing that, break at a
746 lastb = 0; sp = lp + wid;
747 while (sp > lp && (c = *--sp) != ',') {
748 if (! lastb && isspace(c))
752 if (! (sp = lastb)) {
754 while (*sp && *sp != ',' && !isspace(*sp))
761 while (cp < ep && lp <= sp)
768 for (i=indent; cp < ep && i > 0; i--)
778 if (comp->c_mn != &fmt_mnull)
780 if ((sp = comp->c_text) && (sp = getname(sp)) &&
781 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
786 while (getname("")) /* XXX */
788 comp->c_mn = &fmt_mnull;
794 * if there's no component, we say true. Otherwise we
795 * say "true" only if we can parse the address and it
796 * matches one of our addresses.
799 if (comp->c_mn != &fmt_mnull)
801 if ((sp = comp->c_text) && (sp = getname(sp)) &&
802 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
804 comp->c_flags = ismymbox(mn);
805 while ((sp = getname(sp)))
806 if (comp->c_flags == 0 &&
807 (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
808 comp->c_flags |= ismymbox(mn);
810 while (getname("")) /* XXX */
812 comp->c_flags = (comp->c_text == 0);
813 comp->c_mn = &fmt_mnull;
819 /* If we're working on a folder (as opposed to a file), add the
820 * current msg to sequence given in literal field. Don't
821 * disturb string or value registers.
823 if (fmt_current_folder)
824 seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
835 return ((struct format *)0);
839 while (fmt->f_type != FT_DONE)
844 return (fmt->f_value ? ++fmt : (struct format *) 0);