From: Pierre Habouzit Date: Sun, 3 Dec 2006 12:59:52 +0000 (+0100) Subject: rework m_strformat so that it takes the cols number to use in case of X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=b23d5033080fe9f9fba038e3cf1e3ec2b0a0d33d;p=apps%2Fmadmutt.git rework m_strformat so that it takes the cols number to use in case of right pad, rather than computing it. that makes the function suitable for lib-lib inclusion real soon :] Signed-off-by: Pierre Habouzit --- diff --git a/alias.c b/alias.c index 18fac2b..fdf74c0 100644 --- a/alias.c +++ b/alias.c @@ -513,10 +513,11 @@ alias_format_str(char *dest, ssize_t destlen, char op, const char *src, return src; } -static void alias_entry (char *s, ssize_t slen, MUTTMENU *m, int num) +static void alias_entry(char *s, ssize_t slen, MUTTMENU *m, int num) { - m_strformat(s, slen, AliasFmt, alias_format_str, ((alias_t **)m->data)[num], - option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0); + m_strformat(s, slen, COLS - SW, AliasFmt, alias_format_str, + ((alias_t **)m->data)[num], + option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0); } static int alias_tag (MUTTMENU * menu, int n, int m) diff --git a/browser.c b/browser.c index b135b33..2cc3341 100644 --- a/browser.c +++ b/browser.c @@ -145,11 +145,11 @@ static int link_is_dir (const char *folder, const char *path) return 0; } -static const char *folder_format_str (char *dest, ssize_t destlen, char op, - const char *src, const char *fmt, - const char *ifstr, - const char *elstr, - anytype data, format_flag flags) +static const char * +folder_format_str(char *dest, ssize_t destlen, char op, + const char *src, const char *fmt, + const char *ifstr, const char *elstr, + anytype data, format_flag flags) { char fn[STRING], tmp[STRING], permission[11], date[16]; const char *t_fmt; @@ -302,18 +302,18 @@ static const char *folder_format_str (char *dest, ssize_t destlen, char op, if (flags & M_FORMAT_OPTIONAL) - m_strformat(dest, destlen, optional ? ifstr : elstr, folder_format_str, - data, 0); + m_strformat(dest, destlen, 0, optional ? ifstr : elstr, + folder_format_str, data, 0); return src; } #ifdef USE_NNTP -static const char *newsgroup_format_str (char *dest, ssize_t destlen, char op, - const char *src, const char *fmt, - const char *ifstr, - const char *elstr, - anytype data, format_flag flags) +static const char * +newsgroup_format_str(char *dest, ssize_t destlen, char op, + const char *src, const char *fmt, + const char *ifstr, const char *elstr, + anytype data, format_flag flags) { char fn[STRING], tmp[STRING]; FOLDER *folder = data.ptr; @@ -349,11 +349,11 @@ static const char *newsgroup_format_str (char *dest, ssize_t destlen, char op, case 's': if (flags & M_FORMAT_OPTIONAL) { if (folder->ff->nd->unread != 0) - m_strformat (dest, destlen, ifstr, newsgroup_format_str, - data, flags); + m_strformat(dest, destlen, 0, ifstr, newsgroup_format_str, + data, flags); else - m_strformat (dest, destlen, elstr, newsgroup_format_str, - data, flags); + m_strformat (dest, destlen, 0, elstr, newsgroup_format_str, + data, flags); } else if (Context && Context->data == folder->ff->nd) { snprintf (tmp, sizeof (tmp), "%%%sd", fmt); @@ -644,11 +644,11 @@ static void folder_entry (char *s, ssize_t slen, MUTTMENU * menu, int num) #ifdef USE_NNTP if (option (OPTNEWS)) - m_strformat(s, slen, GroupFormat, newsgroup_format_str, &folder, + m_strformat(s, slen, COLS - SW, GroupFormat, newsgroup_format_str, &folder, option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0); else #endif - m_strformat(s, slen, FolderFormat, folder_format_str, &folder, + m_strformat(s, slen, COLS - SW, FolderFormat, folder_format_str, &folder, option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0); } diff --git a/cflags.mk b/cflags.mk index 1e4dafa..a734def 100644 --- a/cflags.mk +++ b/cflags.mk @@ -44,6 +44,6 @@ CFLAGS += -Wpointer-arith # warn about multiple declarations CFLAGS += -Wredundant-decls # warn if the format string is not a string literal -CFLAGS += -Wformat-nonliteral +#CFLAGS += -Wformat-nonliteral # missing prototypes CFLAGS += -Wmissing-prototypes diff --git a/compose.c b/compose.c index 52907b6..7e74f61 100644 --- a/compose.c +++ b/compose.c @@ -113,9 +113,7 @@ static struct mapping_t ComposeNewsHelp[] = { static void snd_entry (char *b, ssize_t blen, MUTTMENU * menu, int num) { - int w = MIN(COLS-SW, blen); - - m_strformat(b, w, AttachFormat, mutt_attach_fmt, + m_strformat(b, blen, COLS - SW, AttachFormat, mutt_attach_fmt, ((ATTACHPTR **)menu->data)[num], M_FORMAT_STAT_FILE | (option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0)); } @@ -450,10 +448,8 @@ static const char *compose_format_str (char *buf, ssize_t buflen, char op, break; } - if (optional) - compose_status_line (buf, buflen, menu, ifstr); - else if (flags & M_FORMAT_OPTIONAL) - compose_status_line (buf, buflen, menu, elstr); + if (flags & M_FORMAT_OPTIONAL) + compose_status_line(buf, buflen, menu, optional ? ifstr : elstr); return (src); } @@ -461,8 +457,7 @@ static const char *compose_format_str (char *buf, ssize_t buflen, char op, static void compose_status_line (char *buf, ssize_t buflen, MUTTMENU * menu, const char *p) { - int w = MIN(COLS - SW, buflen); - m_strformat(buf, w, p, compose_format_str, menu, 0); + m_strformat(buf, buflen, COLS - SW, p, compose_format_str, menu, 0); } /* return values: diff --git a/lib-crypt/crypt-gpgme.c b/lib-crypt/crypt-gpgme.c index 906e56e..0f0b00f 100644 --- a/lib-crypt/crypt-gpgme.c +++ b/lib-crypt/crypt-gpgme.c @@ -2309,7 +2309,7 @@ crypt_entry_fmt (char *dest, ssize_t destlen, char op, } if (flags & M_FORMAT_OPTIONAL) - m_strformat(dest, destlen, optional ? ifstr: elstr, + m_strformat(dest, destlen, 0, optional ? ifstr: elstr, mutt_attach_fmt, data, 0); return src; } @@ -2323,7 +2323,7 @@ static void crypt_entry (char *s, ssize_t l, MUTTMENU * menu, int num) entry.key = key_table[num]; entry.num = num + 1; - m_strformat(s, l, PgpEntryFormat, crypt_entry_fmt, &entry, + m_strformat(s, l, COLS - SW, PgpEntryFormat, crypt_entry_fmt, &entry, option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0); } diff --git a/lib-crypt/pgpinvoke.c b/lib-crypt/pgpinvoke.c index 6a6dd02..7b0b95a 100644 --- a/lib-crypt/pgpinvoke.c +++ b/lib-crypt/pgpinvoke.c @@ -108,7 +108,7 @@ _mutt_fmt_pgp_command(char *dest, ssize_t destlen, } if (flags & M_FORMAT_OPTIONAL) - m_strformat(dest, destlen, optional ? ifstr : elstr, + m_strformat(dest, destlen, 0, optional ? ifstr : elstr, _mutt_fmt_pgp_command, data, 0); return src; @@ -118,7 +118,7 @@ static void mutt_pgp_command(char *d, ssize_t dlen, struct pgp_command_context *cctx, const char *fmt) { - m_strformat(d, dlen, fmt, _mutt_fmt_pgp_command, cctx, 0); + m_strformat(d, dlen, 0, fmt, _mutt_fmt_pgp_command, cctx, 0); } /* diff --git a/lib-crypt/pgpkey.c b/lib-crypt/pgpkey.c index 87dee7d..77c5d26 100644 --- a/lib-crypt/pgpkey.c +++ b/lib-crypt/pgpkey.c @@ -236,7 +236,7 @@ pgp_entry_fmt (char *dest, ssize_t destlen, char op, } if (flags & M_FORMAT_OPTIONAL) - m_strformat(dest, destlen, optional ? ifstr : elstr, + m_strformat(dest, destlen, 0, optional ? ifstr : elstr, mutt_attach_fmt, data, 0); return src; } @@ -249,7 +249,7 @@ static void pgp_entry (char *s, ssize_t l, MUTTMENU * menu, int num) entry.uid = KeyTable[num]; entry.num = num + 1; - m_strformat(s, l, PgpEntryFormat, pgp_entry_fmt, &entry, + m_strformat(s, l, COLS - SW, PgpEntryFormat, pgp_entry_fmt, &entry, option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0); } diff --git a/lib-crypt/smime.c b/lib-crypt/smime.c index 33c53b0..7e5690d 100644 --- a/lib-crypt/smime.c +++ b/lib-crypt/smime.c @@ -113,8 +113,8 @@ int smime_valid_passphrase (void) /* This is almost identical to ppgp's invoking interface. */ static const char * -_mutt_fmt_smime_command (char *dest, ssize_t destlen, char op, - const char *src, const char *prefix, +_mutt_fmt_smime_command (char *dest, ssize_t destlen, + char op, const char *src, const char *prefix, const char *ifstr, const char *elstr, anytype data, format_flag flags) { @@ -219,7 +219,7 @@ _mutt_fmt_smime_command (char *dest, ssize_t destlen, char op, } if (flags & M_FORMAT_OPTIONAL) - m_strformat(dest, destlen, optional ? ifstr : elstr, + m_strformat(dest, destlen, 0, optional ? ifstr : elstr, _mutt_fmt_smime_command, data, 0); return src; @@ -231,7 +231,7 @@ static void mutt_smime_command (char *d, ssize_t dlen, struct smime_command_context *cctx, const char *fmt) { - m_strformat(d, dlen, fmt, _mutt_fmt_smime_command, cctx, 0); + m_strformat(d, dlen, 0, fmt, _mutt_fmt_smime_command, cctx, 0); } static pid_t smime_invoke (FILE ** smimein, FILE ** smimeout, diff --git a/lib-mx/compress.c b/lib-mx/compress.c index c92853b..044392e 100644 --- a/lib-mx/compress.c +++ b/lib-mx/compress.c @@ -134,13 +134,13 @@ static int get_size (const char *path) return (sb.st_size); } -static const char *compresshook_format_str (char *dest, ssize_t destlen, - char op, const char *src, - const char *fmt, - const char *ifstr __attribute__ ((unused)), - const char *elstr __attribute__ ((unused)), - anytype data, - format_flag flags __attribute__ ((unused))) +static const char * +compresshook_format_str(char *dest, ssize_t destlen, + char op, const char *src, const char *fmt, + const char *ifstr __attribute__ ((unused)), + const char *elstr __attribute__ ((unused)), + anytype data, + format_flag flags __attribute__ ((unused))) { char tmp[STRING]; @@ -167,10 +167,10 @@ int mutt_test_compress_command (const char *cmd) return (strstr (cmd, "%f") && strstr (cmd, "%t")) ? 0 : -1; } -static char *get_compression_cmd(const char *cmd, const CONTEXT * ctx) +static char *get_compression_cmd(const char *cmd, const CONTEXT *ctx) { char buf[_POSIX_PATH_MAX]; - m_strformat(buf, sizeof(buf), cmd, compresshook_format_str, ctx, 0); + m_strformat(buf, sizeof(buf), 0, cmd, compresshook_format_str, (void*)ctx, 0); return m_strdup(buf); } diff --git a/lib-ui/curses.h b/lib-ui/curses.h index cb2eb56..4e1c183 100644 --- a/lib-ui/curses.h +++ b/lib-ui/curses.h @@ -208,4 +208,6 @@ void _mutt_make_string (char *, ssize_t, const char *, CONTEXT *, #define MAYBE_REDRAW(x) if (option (OPTNEEDREDRAW)) { unset_option (OPTNEEDREDRAW); x = REDRAW_FULL; } +#define SW (option(OPTMBOXPANE)?SidebarWidth:0) + #endif /* !_MUTT_CURSES_H */ diff --git a/lib-ui/hdrline.c b/lib-ui/hdrline.c index 879a67f..92e6fa6 100644 --- a/lib-ui/hdrline.c +++ b/lib-ui/hdrline.c @@ -210,14 +210,11 @@ struct hdr_format_info { HEADER *hdr; }; -static const char *hdr_format_str (char *dest, - ssize_t destlen, - char op, - const char *src, - const char *prefix, - const char *ifstr, - const char *elstr, - anytype data, format_flag flags) +static const char * +hdr_format_str(char *dest, ssize_t destlen, + char op, const char *src, const char *prefix, + const char *ifstr, const char *elstr, + anytype data, format_flag flags) { struct hdr_format_info *hfi = data.ptr; HEADER *hdr, *htmp; @@ -707,7 +704,7 @@ static const char *hdr_format_str (char *dest, } if (flags & M_FORMAT_OPTIONAL) - m_strformat(dest, destlen, optional ? ifstr: elstr, + m_strformat(dest, destlen, 0, optional ? ifstr: elstr, hdr_format_str, data, flags); return src; @@ -724,5 +721,5 @@ _mutt_make_string (char *dest, ssize_t destlen, const char *s, CONTEXT * ctx, hfi.hdr = hdr; hfi.ctx = ctx; - m_strformat(dest, destlen, s, hdr_format_str, &hfi, flags); + m_strformat(dest, destlen, COLS - SW, s, hdr_format_str, &hfi, flags); } diff --git a/lib-ui/sidebar.c b/lib-ui/sidebar.c index 7ae1c32..9f6e9ce 100644 --- a/lib-ui/sidebar.c +++ b/lib-ui/sidebar.c @@ -92,10 +92,12 @@ static char *shortened_hierarchy (char *hbox, int maxlen) return m_strdup(hbox); } -static const char* sidebar_number_format (char* dest, ssize_t destlen, char op, - const char* src, const char* fmt, - const char* ifstr, const char* elstr, - anytype data, format_flag flags) { +static const char * +sidebar_number_format(char* dest, ssize_t destlen, + char op, const char* src, const char* fmt, + const char* ifstr, const char* elstr, + anytype data, format_flag flags) +{ char tmp[STRING]; BUFFY* b = Incoming.arr[data.i]; int opt = flags & M_FORMAT_OPTIONAL; @@ -163,8 +165,8 @@ static const char* sidebar_number_format (char* dest, ssize_t destlen, char op, } if (flags & M_FORMAT_OPTIONAL) - m_strformat(dest, destlen, opt ? ifstr : elstr, sidebar_number_format, - data, flags); + m_strformat(dest, destlen, 0, opt ? ifstr : elstr, + sidebar_number_format, data, flags); return src; } @@ -197,7 +199,7 @@ static int make_sidebar_entry (char* sbox, int idx, ssize_t len) * (i.e. always display the currently opened) */ return 0; - m_strformat(no, len, SidebarNumberFormat, sidebar_number_format, idx, 0); + m_strformat(no, len, SidebarWidth, SidebarNumberFormat, sidebar_number_format, idx, 0); lencnt = m_strlen(no); if (l > 0 && m_strncmp(sbox, ImapHomeNamespace, l) == 0 && diff --git a/lib-ui/status.c b/lib-ui/status.c index 3462ffa..9c4bdf9 100644 --- a/lib-ui/status.c +++ b/lib-ui/status.c @@ -46,11 +46,10 @@ static char *get_sort_str (char *buf, ssize_t buflen, int method) * %t = # of tagged messages [option] * %v = Madmutt version * %V = currently active limit pattern [option] */ -static const char *status_format_str (char *buf, ssize_t buflen, char op, - const char *src, const char *prefix, - const char *ifstr, - const char *elstr, - anytype data, format_flag flags) +static const char * +status_format_str(char *buf, ssize_t buflen, char op, const char *src, + const char *prefix, const char *ifstr, const char *elstr, + anytype data, format_flag flags) { char fmt[STRING], tmp[STRING]; const char *cp, *p; @@ -279,19 +278,13 @@ static const char *status_format_str (char *buf, ssize_t buflen, char op, break; } - if (optional) - menu_status_line (buf, buflen, menu, ifstr); - else if (flags & M_FORMAT_OPTIONAL) - menu_status_line (buf, buflen, menu, elstr); + if (flags & M_FORMAT_OPTIONAL) + menu_status_line(buf, buflen, menu, optional ? ifstr : elstr); return (src); } -void menu_status_line (char* buf, ssize_t len, MUTTMENU* menu, const char* p) { - /* - * if we have enough space for buffer, format lines to $COLS-$SidebarWidth - * only to not wrap past end of screen - */ - int width = MIN(COLS - SW + 1, len); - m_strformat(buf, width, p, status_format_str, menu, 0); +void menu_status_line(char* buf, ssize_t len, MUTTMENU* menu, const char* p) +{ + m_strformat(buf, len, COLS - SW, p, status_format_str, menu, 0); } diff --git a/muttlib.c b/muttlib.c index f63ad45..946a2f5 100644 --- a/muttlib.c +++ b/muttlib.c @@ -29,8 +29,6 @@ #include -#define SW (option(OPTMBOXPANE)?SidebarWidth:0) - /* Modified by blong to accept a "suggestion" for file name. If * that file exists, then construct one with unique name but * keep any extension. This might fail, I guess. @@ -328,14 +326,14 @@ void mutt_safe_path(char *s, ssize_t l, address_t *a) } } -ssize_t m_strformat(char *dst, ssize_t dlen, const char *fmt, +ssize_t m_strformat(char *dst, ssize_t dlen, int width, const char *fmt, format_t *callback, anytype cdata, format_flag flags) { ssize_t pos = flags & M_FORMAT_ARROWCURSOR ? 3 : 0; m_strpad(dst, dlen, '\0', pos + 1); if (!fmt) - return; + return pos; while (*fmt) { int ch; @@ -392,25 +390,24 @@ ssize_t m_strformat(char *dst, ssize_t dlen, const char *fmt, } switch (ch) { - ssize_t col; char lower, nodots, buf[LONG_STRING]; case '>': /* right justify to EOL */ - col = mutt_strwidth(dst); + width -= mutt_strwidth(dst); ch = *fmt++; /* pad char */ - if (COLS - SW > col) { - m_strformat(buf, sizeof(buf), fmt, callback, cdata, flags); - pos += m_strpad(dst + pos, dlen - pos, ch, - COLS - SW - col - mutt_strwidth(buf)); + if (width > 0) { + m_strformat(buf, sizeof(buf), 0, fmt, callback, cdata, flags); + width -= mutt_strwidth(buf); + pos += m_strpad(dst + pos, dlen - pos, ch, width); pos += m_strcpy(dst + pos, dlen - pos, buf); } return pos; /* skip rest of input */ case '|': /* pad to EOL */ - return pos + m_strpad(dst + pos, dlen - pos, *fmt, - COLS - SW - mutt_strwidth(dst)); + width -= mutt_strwidth(dst); + return pos + m_strpad(dst + pos, dlen - pos, *fmt, width); default: lower = nodots = 0; diff --git a/nntp/newsrc.c b/nntp/newsrc.c index 306bf68..6d5fad5 100644 --- a/nntp/newsrc.c +++ b/nntp/newsrc.c @@ -346,7 +346,7 @@ NNTP_SERVER *mutt_select_newsserver (char *server) if (!conn) return NULL; - m_strformat(file, sizeof(file), NewsRc, nntp_format_str, NULL, 0); + m_strformat(file, sizeof(file), 0, NewsRc, nntp_format_str, NULL, 0); mutt_expand_path(file, sizeof(file)); serv = (NNTP_SERVER *) conn->data; diff --git a/protos.h b/protos.h index 2f96bbc..081a937 100644 --- a/protos.h +++ b/protos.h @@ -14,12 +14,12 @@ void mutt_mktemp (char *) __attribute__((deprecated)); #define MoreArgs(p) (*p->dptr && *p->dptr != ';' && *p->dptr != '#') -typedef const char *format_t (char *, ssize_t, char, const char *, - const char *, const char *, const char *, - anytype, format_flag); +typedef const char * +format_t(char *, ssize_t, char, const char *, + const char *, const char *, const char *, anytype, format_flag); -ssize_t m_strformat(char *, ssize_t, const char *, format_t *, - anytype, format_flag); +ssize_t m_strformat(char *, ssize_t, int, const char *, + format_t *, anytype, format_flag); void set_quadoption (int, int); int query_quadoption (int, const char *); diff --git a/recvattach.c b/recvattach.c index 1c21de7..301b12e 100644 --- a/recvattach.c +++ b/recvattach.c @@ -139,13 +139,10 @@ ATTACHPTR **mutt_gen_attach_list (BODY * m, * %s = size * %u = unlink */ -const char *mutt_attach_fmt (char *dest, ssize_t destlen, - char op, - const char *src, - const char *prefix, - const char *ifstr, - const char *elstr, - anytype data, format_flag flags) +const char * +mutt_attach_fmt(char *dest, ssize_t destlen, char op, const char *src, + const char *prefix, const char *ifstr, const char *elstr, + anytype data, format_flag flags) { char fmt[16]; char tmp[STRING]; @@ -312,16 +309,14 @@ const char *mutt_attach_fmt (char *dest, ssize_t destlen, } if (flags & M_FORMAT_OPTIONAL) - m_strformat(dest, destlen, optional ? ifstr : elstr, + m_strformat(dest, destlen, 0, optional ? ifstr : elstr, mutt_attach_fmt, data, 0); return src; } static void attach_entry(char *b, ssize_t blen, MUTTMENU * menu, int num) { - int w = MIN(COLS - SW, blen); - - m_strformat(b, w, AttachFormat, mutt_attach_fmt, + m_strformat(b, blen, COLS - SW, AttachFormat, mutt_attach_fmt, ((ATTACHPTR **) menu->data)[num], option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0); } diff --git a/remailer.c b/remailer.c index a261f1b..f8f3882 100644 --- a/remailer.c +++ b/remailer.c @@ -384,8 +384,8 @@ mix_entry_fmt (char *dest, ssize_t destlen, char op, const char *src, } if (flags & M_FORMAT_OPTIONAL) - m_strformat (dest, destlen, optional ? ifstr : elstr, - mutt_attach_fmt, data, 0); + m_strformat(dest, destlen, 0, optional ? ifstr : elstr, + mutt_attach_fmt, data, 0); return (src); } @@ -395,8 +395,8 @@ static void mix_entry (char *b, ssize_t blen, MUTTMENU * menu, int num) { remailer_t **type2_list = (remailer_t **) menu->data; - int w = MIN(COLS - SW, blen); - m_strformat(b, w, MixEntryFormat, mix_entry_fmt, type2_list[num], + m_strformat(b, blen, COLS - SW, MixEntryFormat, mix_entry_fmt, + type2_list[num], option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0); } diff --git a/sendlib.c b/sendlib.c index 3bf9c91..6ad7ed0 100644 --- a/sendlib.c +++ b/sendlib.c @@ -1840,7 +1840,7 @@ static int mutt_invoke_sendmail (address_t * from, /* the sender */ if (option (OPTNEWSSEND)) { char cmd[LONG_STRING]; - m_strformat(cmd, sizeof(cmd), Inews, nntp_format_str, 0, 0); + m_strformat(cmd, sizeof(cmd), 0, Inews, nntp_format_str, 0, 0); if (m_strisempty(cmd)) { i = nntp_post (msg); unlink (msg);