X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=sbr%2Ffmt_scan.c;h=4365e350c39f8b1aae6bd457ebb089e0379cdcf7;hp=0af69aba2533b64127c4f529be202202e2e40b75;hb=b3f36786e62ff7b708849e05d0cac2f55f37e6a5;hpb=3c9700d8d045f3ff26ce5dd2a174454dafc14822 diff --git a/sbr/fmt_scan.c b/sbr/fmt_scan.c index 0af69ab..4365e35 100644 --- a/sbr/fmt_scan.c +++ b/sbr/fmt_scan.c @@ -3,14 +3,29 @@ * fmt_scan.c -- format string interpretation * * $Id$ + * + * This code is Copyright (c) 2002, by the authors of nmh. See the + * COPYRIGHT file in the root directory of the nmh distribution for + * complete copyright information. */ #include #include #include -#include +#include #include +#ifdef TIME_WITH_SYS_TIME +# include +# include +#else +# ifdef TM_IN_SYS_TIME +# include +# else +# include +# endif +#endif + #define NFMTS MAXARGS extern char *formataddr (); /* hook for custom address formatting */ @@ -74,46 +89,6 @@ match (char *str, char *sub) * macros to format data */ -#define PUTDF(cp, num, wid, fill)\ - if (cp + wid < ep) {\ - if ((i = (num)) < 0)\ - i = -(num);\ - if ((c = (wid)) < 0)\ - c = -c;\ - sp = cp + c;\ - do {\ - *--sp = (i % 10) + '0';\ - i /= 10;\ - } while (i > 0 && sp > cp);\ - if (i > 0)\ - *sp = '?';\ - else if ((num) < 0 && sp > cp)\ - *--sp = '-';\ - while (sp > cp)\ - *--sp = fill;\ - cp += c;\ - } - -#define PUTD(cp, num)\ - if (cp < ep) {\ - if ((i = (num)) == 0)\ - *cp++ = '0';\ - else {\ - if ((i = (num)) < 0) {\ - *cp++ = '-';\ - i = -(num);\ - }\ - c = 10;\ - while (c <= i) \ - c *= 10;\ - while (cp < ep && c > 1) {\ - c /= 10;\ - *cp++ = (i / c) + '0';\ - i %= c;\ - }\ - }\ - } - #ifdef LOCALE #define PUTSF(cp, str, wid, fill) {\ ljust = 0;\ @@ -271,7 +246,7 @@ fmt_scan (struct format *format, char *scanl, int width, int *dat) char *cp, *ep, *sp; char *savestr, *str = NULL; char buffer[BUFSIZ], buffer2[BUFSIZ]; - int i, c, ljust; + int i, c, ljust, n; int value = 0; time_t t; struct format *fmt; @@ -281,6 +256,15 @@ fmt_scan (struct format *format, char *scanl, int width, int *dat) cp = scanl; ep = scanl + width - 1; + + for (fmt = format; fmt->f_type != FT_DONE; fmt++) + switch (fmt->f_type) { + case FT_PARSEADDR: + case FT_PARSEDATE: + fmt->f_comp->c_flags &= ~CF_PARSED; + break; + } + fmt = format; while (cp < ep) { @@ -322,10 +306,16 @@ fmt_scan (struct format *format, char *scanl, int width, int *dat) adios (NULL, "internal error (FT_STRFW)"); case FT_NUM: - PUTD (cp, value); + n = snprintf(cp, ep - cp, "%d", value); + if (n >= 0) cp += n; break; case FT_NUMF: - PUTDF (cp, value, fmt->f_width, fmt->f_fill); + n = snprintf(cp, ep - cp, "%d", value); + if (n >= 0) { + cp += n; + } else n = 0; + c = abs(fmt->f_width) - n; + for (; (c > 0) && (cp < ep); c--) *cp++ = fmt->f_fill; break; case FT_CHAR: @@ -475,7 +465,7 @@ fmt_scan (struct format *format, char *scanl, int width, int *dat) break; case FT_LV_COMPFLAG: - value = fmt->f_comp->c_flags; + value = (fmt->f_comp->c_flags & CF_TRUE) != 0; break; case FT_LV_COMP: value = (comp = fmt->f_comp)->c_text ? atoi(comp->c_text) : 0; @@ -487,7 +477,10 @@ fmt_scan (struct format *format, char *scanl, int width, int *dat) value = dat[fmt->f_value]; break; case FT_LV_STRLEN: - value = strlen(str); + if (str != NULL) + value = strlen(str); + else + value = 0; break; case FT_LV_CHAR_LEFT: value = width - (cp - scanl); @@ -695,13 +688,16 @@ fmt_scan (struct format *format, char *scanl, int width, int *dat) case FT_PARSEDATE: comp = fmt->f_comp; + if (comp->c_flags & CF_PARSED) + break; if ((sp = comp->c_text) && (tws = dparsetime(sp))) { *comp->c_tws = *tws; - comp->c_flags = 0; - } else if (comp->c_flags >= 0) { + comp->c_flags &= ~CF_TRUE; + } else if ((comp->c_flags & CF_DATEFAB) == 0) { memset ((char *) comp->c_tws, 0, sizeof *comp->c_tws); - comp->c_flags = 1; + comp->c_flags = CF_TRUE; } + comp->c_flags |= CF_PARSED; break; case FT_FORMATADDR: @@ -749,9 +745,6 @@ fmt_scan (struct format *format, char *scanl, int width, int *dat) len -= sp - lp + 1; while (cp < ep && lp <= sp) *cp++ = *lp++; - *cp++ = '\n'; - for (i=indent; cp < ep && i > 0; i--) - *cp++ = ' '; while (isspace(*lp)) lp++, len--; if (*lp) { @@ -767,6 +760,8 @@ fmt_scan (struct format *format, char *scanl, int width, int *dat) case FT_PARSEADDR: comp = fmt->f_comp; + if (comp->c_flags & CF_PARSED) + break; if (comp->c_mn != &fmt_mnull) mnfree (comp->c_mn); if ((sp = comp->c_text) && (sp = getname(sp)) && @@ -774,6 +769,7 @@ fmt_scan (struct format *format, char *scanl, int width, int *dat) comp->c_mn = mn; while (getname("")) ; + comp->c_flags |= CF_PARSED; } else { while (getname("")) /* XXX */ ; @@ -793,15 +789,22 @@ fmt_scan (struct format *format, char *scanl, int width, int *dat) if ((sp = comp->c_text) && (sp = getname(sp)) && (mn = getm (sp, NULL, 0, AD_NAME, NULL))) { comp->c_mn = mn; - comp->c_flags = ismymbox(mn); + if (ismymbox(mn)) + comp->c_flags |= CF_TRUE; + else + comp->c_flags &= ~CF_TRUE; while ((sp = getname(sp))) - if (comp->c_flags == 0 && + if ((comp->c_flags & CF_TRUE) == 0 && (mn = getm (sp, NULL, 0, AD_NAME, NULL))) - comp->c_flags |= ismymbox(mn); + if (ismymbox(mn)) + comp->c_flags |= CF_TRUE; } else { while (getname("")) /* XXX */ ; - comp->c_flags = (comp->c_text == 0); + if (comp->c_text == 0) + comp->c_flags |= CF_TRUE; + else + comp->c_flags &= ~CF_TRUE; comp->c_mn = &fmt_mnull; } break;