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)\
111 char * PUTSF(char *cp, char *str, unsigned int wid, char fill) {
114 unsigned int char_len;
115 unsigned int term_len = 0;
118 for (i = 0 ; i < wid && i < strlen(str) && term_len < wid; ) {
119 char_len = mblen(str + i, strlen(str + i));
123 mbtowc(&wide_char, str + i, strlen(str + i));
124 term_len += wcwidth(wide_char);
126 for (j = 0 ; j < char_len ; j++) {
127 *(cp + i) = *(str + i);
132 if (term_len < wid) {
133 for (j = term_len ; j <= wid ; j++) {
141 #define PUTS(cp, str) {\
143 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
145 while((c = (unsigned char) *sp++) && cp < ep)\
146 if (!iscntrl(c) && !isspace(c)) \
149 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
156 static char *lmonth[] = { "January", "February","March", "April",
157 "May", "June", "July", "August",
158 "September","October", "November","December" };
161 get_x400_friendly (char *mbox, char *buffer, int buffer_len)
163 char given[BUFSIZ], surname[BUFSIZ];
172 if (get_x400_comp (mbox, "/PN=", buffer, buffer_len)) {
173 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
179 if (!get_x400_comp (mbox, "/S=", surname, sizeof(surname)))
182 if (get_x400_comp (mbox, "/G=", given, sizeof(given)))
183 snprintf (buffer, buffer_len, "%s %s", given, surname);
185 snprintf (buffer, buffer_len, "%s", surname);
191 get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len)
196 if ((idx = stringdex (key, mbox)) < 0
197 || !(cp = strchr(mbox += idx + strlen (key), '/')))
200 snprintf (buffer, buffer_len, "%*.*s", cp - mbox, cp - mbox, mbox);
205 fmt_scan (struct format *format, char *scanl, int width, int *dat)
208 char *savestr, *str = NULL;
209 char buffer[BUFSIZ], buffer2[BUFSIZ];
219 ep = scanl + width - 1;
221 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
222 switch (fmt->f_type) {
225 fmt->f_comp->c_flags &= ~CF_PARSED;
232 switch (fmt->f_type) {
235 PUTS (cp, fmt->f_comp->c_text);
238 cp = PUTSF(cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill);
243 while( (c = *sp++) && cp < ep)
252 ljust++; /* XXX should do something with this */
254 while( (c = *sp++) && --i >= 0 && cp < ep)
256 while( --i >= 0 && cp < ep)
264 cp =PUTSF (cp, str, fmt->f_width, fmt->f_fill);
267 adios (NULL, "internal error (FT_STRFW)");
270 n = snprintf(cp, ep - cp, "%d", value);
274 PUTDF (cp, value, fmt->f_width, fmt->f_fill);
285 if (!(value = (str && *str))) {
292 if (!(value = (str == NULL || *str == 0))) {
299 if (value != fmt->f_value) {
306 if (value == fmt->f_value) {
313 if (value <= fmt->f_value) {
320 if (!(value = (str && match (str, fmt->f_text)))) {
328 value = match (str, fmt->f_text);
334 if (!(value = (str && uprf (str, fmt->f_text)))) {
341 value = uprf (str, fmt->f_text);
345 value = (str != NULL && *str != 0);
349 value = (str == NULL || *str == 0);
353 value = (fmt->f_value == value);
357 value = (fmt->f_value != value);
361 value = (fmt->f_value > value);
372 str = fmt->f_comp->c_text;
378 if (!(str = getenv (fmt->f_text)))
382 if (!(str = context_find (fmt->f_text)))
386 case FT_LS_DECODECOMP:
387 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
390 str = fmt->f_comp->c_text;
394 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
402 strncpy(buffer, str, sizeof(buffer));
403 buffer[sizeof(buffer)-1] = '\0';
405 while (isspace(*str))
408 if ((i = fmt->f_width) < 0) {
413 if (!ljust && i > 0 && strlen(str) > i)
416 xp += strlen(str) - 1;
417 while (xp > str && isspace(*xp))
419 if (ljust && i > 0 && strlen(str) > i)
420 str += strlen(str) - i;
425 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
428 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
431 value = fmt->f_value;
434 value = dat[fmt->f_value];
442 case FT_LV_CHAR_LEFT:
443 value = width - (cp - scanl);
446 value += fmt->f_value;
449 value = fmt->f_value - value;
453 value = value / fmt->f_value;
459 value = value % fmt->f_value;
468 value = fmt->f_comp->c_tws->tw_sec;
471 value = fmt->f_comp->c_tws->tw_min;
474 value = fmt->f_comp->c_tws->tw_hour;
477 value = fmt->f_comp->c_tws->tw_mday;
480 value = fmt->f_comp->c_tws->tw_mon + 1;
483 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
486 str = lmonth[fmt->f_comp->c_tws->tw_mon];
489 str = dtwszone (fmt->f_comp->c_tws);
492 value = fmt->f_comp->c_tws->tw_year;
495 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
497 value = tws->tw_wday;
500 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
502 str = tw_dotw[tws->tw_wday];
505 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
507 str = tw_ldotw[tws->tw_wday];
510 value = fmt->f_comp->c_tws->tw_yday;
513 value = fmt->f_comp->c_tws->tw_zone;
516 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
517 value = dmktime(fmt->f_comp->c_tws);
520 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
521 value = dmktime(fmt->f_comp->c_tws);
522 value = time((time_t *) 0) - value;
525 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
527 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
536 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
542 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
545 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
548 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
552 str = fmt->f_comp->c_mn->m_pers;
555 str = fmt->f_comp->c_mn->m_mbox;
558 str = fmt->f_comp->c_mn->m_host;
561 str = fmt->f_comp->c_mn->m_path;
564 str = fmt->f_comp->c_mn->m_gname;
567 str = fmt->f_comp->c_mn->m_note;
570 str = adrformat( fmt->f_comp->c_mn );
573 value = fmt->f_comp->c_mn->m_type;
576 value = fmt->f_comp->c_mn->m_ingrp;
579 value = fmt->f_comp->c_mn->m_nohost;
583 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
584 str = fmt->f_comp->c_text;
587 if (fmt->f_type == FT_LS_ADDR)
589 if ((str = mn->m_pers) == NULL) {
590 if ((str = mn->m_note)) {
591 strncpy (buffer, str, sizeof(buffer));
592 buffer[sizeof(buffer)-1] = '\0';
596 sp = str + strlen(str) - 1;
605 } else if (!(str = get_x400_friendly (mn->m_mbox,
606 buffer, sizeof(buffer)))) {
608 switch (mn->m_type) {
613 snprintf (buffer, sizeof(buffer), "%s!%s",
614 mn->m_host, mn->m_mbox);
619 snprintf (buffer, sizeof(buffer), "%s@%s",
620 mn->m_mbox, mn->m_host);
632 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
636 strncpy(buffer, str, sizeof(buffer));
637 /* strncpy doesn't NUL-terminate if it fills the buffer */
638 buffer[sizeof(buffer)-1] = '\0';
641 /* we will parse from buffer to buffer2 */
642 n = 0; /* n is the input position in str */
643 m = 0; /* m is the ouput position in buffer2 */
645 while ( str[n] != '\0') {
650 buffer2[m++] = str[n++];
656 buffer2[m++] = str[n++];
667 if ((t = comp->c_tws->tw_clock) == 0)
668 t = dmktime(comp->c_tws);
669 tws = dlocaltime(&t);
675 if ((t = comp->c_tws->tw_clock) == 0)
676 t = dmktime(comp->c_tws);
683 if (comp->c_flags & CF_PARSED)
685 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
687 comp->c_flags &= ~CF_TRUE;
688 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
689 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
690 comp->c_flags = CF_TRUE;
692 comp->c_flags |= CF_PARSED;
696 /* hook for custom address list formatting (see replsbr.c) */
697 str = formataddr (savestr, str);
701 /* output the str register as an address component,
702 * splitting it into multiple lines if necessary. The
703 * value reg. contains the max line length. The lit.
704 * field may contain a string to prepend to the result
709 int indent, wid, len;
715 indent = strlen (sp);
717 while( (c = *sp++) && cp < ep)
720 /* try to break at a comma; failing that, break at a
723 lastb = 0; sp = lp + wid;
724 while (sp > lp && (c = *--sp) != ',') {
725 if (! lastb && isspace(c))
729 if (! (sp = lastb)) {
731 while (*sp && *sp != ',' && !isspace(*sp))
738 while (cp < ep && lp <= sp)
745 for (i=indent; cp < ep && i > 0; i--)
755 if (comp->c_flags & CF_PARSED)
757 if (comp->c_mn != &fmt_mnull)
759 if ((sp = comp->c_text) && (sp = getname(sp)) &&
760 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
764 comp->c_flags |= CF_PARSED;
766 while (getname("")) /* XXX */
768 comp->c_mn = &fmt_mnull;
774 * if there's no component, we say true. Otherwise we
775 * say "true" only if we can parse the address and it
776 * matches one of our addresses.
779 if (comp->c_mn != &fmt_mnull)
781 if ((sp = comp->c_text) && (sp = getname(sp)) &&
782 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
785 comp->c_flags |= CF_TRUE;
787 comp->c_flags &= ~CF_TRUE;
788 while ((sp = getname(sp)))
789 if ((comp->c_flags & CF_TRUE) == 0 &&
790 (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
792 comp->c_flags |= CF_TRUE;
794 while (getname("")) /* XXX */
796 if (comp->c_text == 0)
797 comp->c_flags |= CF_TRUE;
799 comp->c_flags &= ~CF_TRUE;
800 comp->c_mn = &fmt_mnull;
806 /* If we're working on a folder (as opposed to a file), add the
807 * current msg to sequence given in literal field. Don't
808 * disturb string or value registers.
810 if (fmt_current_folder)
811 seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
822 return ((struct format *)0);
826 while (fmt->f_type != FT_DONE)
831 return (fmt->f_value ? ++fmt : (struct format *) 0);