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++) {
142 #define PUTS(cp, str) {\
144 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
146 while((c = (unsigned char) *sp++) && cp < ep)\
147 if (!iscntrl(c) && !isspace(c)) \
150 while ((c = (unsigned char) *sp) && (iscntrl(c) || isspace(c)))\
157 static char *lmonth[] = { "January", "February","March", "April",
158 "May", "June", "July", "August",
159 "September","October", "November","December" };
162 get_x400_friendly (char *mbox, char *buffer, int buffer_len)
164 char given[BUFSIZ], surname[BUFSIZ];
173 if (get_x400_comp (mbox, "/PN=", buffer, buffer_len)) {
174 for (mbox = buffer; (mbox = strchr(mbox, '.')); )
180 if (!get_x400_comp (mbox, "/S=", surname, sizeof(surname)))
183 if (get_x400_comp (mbox, "/G=", given, sizeof(given)))
184 snprintf (buffer, buffer_len, "%s %s", given, surname);
186 snprintf (buffer, buffer_len, "%s", surname);
192 get_x400_comp (char *mbox, char *key, char *buffer, int buffer_len)
197 if ((idx = stringdex (key, mbox)) < 0
198 || !(cp = strchr(mbox += idx + strlen (key), '/')))
201 snprintf (buffer, buffer_len, "%*.*s", cp - mbox, cp - mbox, mbox);
206 fmt_scan (struct format *format, char *scanl, int width, int *dat)
209 char *savestr, *str = NULL;
210 char buffer[BUFSIZ], buffer2[BUFSIZ];
220 ep = scanl + width - 1;
222 for (fmt = format; fmt->f_type != FT_DONE; fmt++)
223 switch (fmt->f_type) {
226 fmt->f_comp->c_flags &= ~CF_PARSED;
233 switch (fmt->f_type) {
236 PUTS (cp, fmt->f_comp->c_text);
239 cp = PUTSF(cp, fmt->f_comp->c_text, fmt->f_width, fmt->f_fill);
244 while( (c = *sp++) && cp < ep)
253 ljust++; /* XXX should do something with this */
255 while( (c = *sp++) && --i >= 0 && cp < ep)
257 while( --i >= 0 && cp < ep)
265 cp =PUTSF (cp, str, fmt->f_width, fmt->f_fill);
268 adios (NULL, "internal error (FT_STRFW)");
271 n = snprintf(cp, ep - cp, "%d", value);
275 PUTDF (cp, value, fmt->f_width, fmt->f_fill);
286 if (!(value = (str && *str))) {
293 if (!(value = (str == NULL || *str == 0))) {
300 if (value != fmt->f_value) {
307 if (value == fmt->f_value) {
314 if (value <= fmt->f_value) {
321 if (!(value = (str && match (str, fmt->f_text)))) {
329 value = match (str, fmt->f_text);
335 if (!(value = (str && uprf (str, fmt->f_text)))) {
342 value = uprf (str, fmt->f_text);
346 value = (str != NULL && *str != 0);
350 value = (str == NULL || *str == 0);
354 value = (fmt->f_value == value);
358 value = (fmt->f_value != value);
362 value = (fmt->f_value > value);
373 str = fmt->f_comp->c_text;
379 if (!(str = getenv (fmt->f_text)))
383 if (!(str = context_find (fmt->f_text)))
387 case FT_LS_DECODECOMP:
388 if (decode_rfc2047(fmt->f_comp->c_text, buffer2, sizeof(buffer2)))
391 str = fmt->f_comp->c_text;
395 if (str && decode_rfc2047(str, buffer2, sizeof(buffer2)))
403 strncpy(buffer, str, sizeof(buffer));
404 buffer[sizeof(buffer)-1] = '\0';
406 while (isspace(*str))
409 if ((i = fmt->f_width) < 0) {
414 if (!ljust && i > 0 && strlen(str) > i)
417 xp += strlen(str) - 1;
418 while (xp > str && isspace(*xp))
420 if (ljust && i > 0 && strlen(str) > i)
421 str += strlen(str) - i;
426 value = (fmt->f_comp->c_flags & CF_TRUE) != 0;
429 value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0;
432 value = fmt->f_value;
435 value = dat[fmt->f_value];
443 case FT_LV_CHAR_LEFT:
444 value = width - (cp - scanl);
447 value += fmt->f_value;
450 value = fmt->f_value - value;
454 value = value / fmt->f_value;
460 value = value % fmt->f_value;
469 value = fmt->f_comp->c_tws->tw_sec;
472 value = fmt->f_comp->c_tws->tw_min;
475 value = fmt->f_comp->c_tws->tw_hour;
478 value = fmt->f_comp->c_tws->tw_mday;
481 value = fmt->f_comp->c_tws->tw_mon + 1;
484 str = tw_moty[fmt->f_comp->c_tws->tw_mon];
487 str = lmonth[fmt->f_comp->c_tws->tw_mon];
490 str = dtwszone (fmt->f_comp->c_tws);
493 value = fmt->f_comp->c_tws->tw_year;
496 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
498 value = tws->tw_wday;
501 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
503 str = tw_dotw[tws->tw_wday];
506 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
508 str = tw_ldotw[tws->tw_wday];
511 value = fmt->f_comp->c_tws->tw_yday;
514 value = fmt->f_comp->c_tws->tw_zone;
517 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
518 value = dmktime(fmt->f_comp->c_tws);
521 if ((value = fmt->f_comp->c_tws->tw_clock) == 0)
522 value = dmktime(fmt->f_comp->c_tws);
523 value = time((time_t *) 0) - value;
526 if (!(((tws = fmt->f_comp->c_tws)->tw_flags) & (TW_SEXP|TW_SIMP)))
528 switch (fmt->f_comp->c_tws->tw_flags & TW_SDAY) {
537 if ((fmt->f_comp->c_tws->tw_flags & TW_SZONE) == TW_SZEXP)
543 value = fmt->f_comp->c_tws->tw_flags & TW_DST;
546 str = dasctime (fmt->f_comp->c_tws , TW_ZONE);
549 str = dasctime (fmt->f_comp->c_tws, TW_NULL);
553 str = fmt->f_comp->c_mn->m_pers;
556 str = fmt->f_comp->c_mn->m_mbox;
559 str = fmt->f_comp->c_mn->m_host;
562 str = fmt->f_comp->c_mn->m_path;
565 str = fmt->f_comp->c_mn->m_gname;
568 str = fmt->f_comp->c_mn->m_note;
571 str = adrformat( fmt->f_comp->c_mn );
574 value = fmt->f_comp->c_mn->m_type;
577 value = fmt->f_comp->c_mn->m_ingrp;
580 value = fmt->f_comp->c_mn->m_nohost;
584 if ((mn = fmt->f_comp->c_mn) == &fmt_mnull) {
585 str = fmt->f_comp->c_text;
588 if (fmt->f_type == FT_LS_ADDR)
590 if ((str = mn->m_pers) == NULL) {
591 if ((str = mn->m_note)) {
592 strncpy (buffer, str, sizeof(buffer));
593 buffer[sizeof(buffer)-1] = '\0';
597 sp = str + strlen(str) - 1;
606 } else if (!(str = get_x400_friendly (mn->m_mbox,
607 buffer, sizeof(buffer)))) {
609 switch (mn->m_type) {
614 snprintf (buffer, sizeof(buffer), "%s!%s",
615 mn->m_host, mn->m_mbox);
620 snprintf (buffer, sizeof(buffer), "%s@%s",
621 mn->m_mbox, mn->m_host);
633 /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */
637 strncpy(buffer, str, sizeof(buffer));
638 /* strncpy doesn't NUL-terminate if it fills the buffer */
639 buffer[sizeof(buffer)-1] = '\0';
642 /* we will parse from buffer to buffer2 */
643 n = 0; /* n is the input position in str */
644 m = 0; /* m is the ouput position in buffer2 */
646 while ( str[n] != '\0') {
651 buffer2[m++] = str[n++];
657 buffer2[m++] = str[n++];
668 if ((t = comp->c_tws->tw_clock) == 0)
669 t = dmktime(comp->c_tws);
670 tws = dlocaltime(&t);
676 if ((t = comp->c_tws->tw_clock) == 0)
677 t = dmktime(comp->c_tws);
684 if (comp->c_flags & CF_PARSED)
686 if ((sp = comp->c_text) && (tws = dparsetime(sp))) {
688 comp->c_flags &= ~CF_TRUE;
689 } else if ((comp->c_flags & CF_DATEFAB) == 0) {
690 memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws);
691 comp->c_flags = CF_TRUE;
693 comp->c_flags |= CF_PARSED;
697 /* hook for custom address list formatting (see replsbr.c) */
698 str = formataddr (savestr, str);
702 /* output the str register as an address component,
703 * splitting it into multiple lines if necessary. The
704 * value reg. contains the max line length. The lit.
705 * field may contain a string to prepend to the result
710 int indent, wid, len;
716 indent = strlen (sp);
718 while( (c = *sp++) && cp < ep)
721 /* try to break at a comma; failing that, break at a
724 lastb = 0; sp = lp + wid;
725 while (sp > lp && (c = *--sp) != ',') {
726 if (! lastb && isspace(c))
730 if (! (sp = lastb)) {
732 while (*sp && *sp != ',' && !isspace(*sp))
739 while (cp < ep && lp <= sp)
746 for (i=indent; cp < ep && i > 0; i--)
756 if (comp->c_flags & CF_PARSED)
758 if (comp->c_mn != &fmt_mnull)
760 if ((sp = comp->c_text) && (sp = getname(sp)) &&
761 (mn = getm (sp, NULL, 0, fmt_norm, NULL))) {
765 comp->c_flags |= CF_PARSED;
767 while (getname("")) /* XXX */
769 comp->c_mn = &fmt_mnull;
775 * if there's no component, we say true. Otherwise we
776 * say "true" only if we can parse the address and it
777 * matches one of our addresses.
780 if (comp->c_mn != &fmt_mnull)
782 if ((sp = comp->c_text) && (sp = getname(sp)) &&
783 (mn = getm (sp, NULL, 0, AD_NAME, NULL))) {
786 comp->c_flags |= CF_TRUE;
788 comp->c_flags &= ~CF_TRUE;
789 while ((sp = getname(sp)))
790 if ((comp->c_flags & CF_TRUE) == 0 &&
791 (mn = getm (sp, NULL, 0, AD_NAME, NULL)))
793 comp->c_flags |= CF_TRUE;
795 while (getname("")) /* XXX */
797 if (comp->c_text == 0)
798 comp->c_flags |= CF_TRUE;
800 comp->c_flags &= ~CF_TRUE;
801 comp->c_mn = &fmt_mnull;
807 /* If we're working on a folder (as opposed to a file), add the
808 * current msg to sequence given in literal field. Don't
809 * disturb string or value registers.
811 if (fmt_current_folder)
812 seq_addmsg(fmt_current_folder, fmt->f_text, dat[0], -1);
823 return ((struct format *)0);
827 while (fmt->f_type != FT_DONE)
832 return (fmt->f_value ? ++fmt : (struct format *) 0);