Replace a lot of snprintf with a catf implementation

master
C. McEnroe 2020-02-16 19:12:19 -05:00
parent 3885dd5231
commit ba524ed804
3 changed files with 36 additions and 36 deletions

12
chat.h
View File

@ -14,10 +14,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <assert.h>
#include <err.h> #include <err.h>
#include <getopt.h> #include <getopt.h>
#include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <sysexits.h> #include <sysexits.h>
#include <time.h> #include <time.h>
@ -29,6 +32,15 @@
typedef unsigned uint; typedef unsigned uint;
typedef unsigned char byte; typedef unsigned char byte;
static inline void __attribute__((format(printf, 3, 4)))
catf(char *buf, size_t cap, const char *format, ...) {
size_t len = strnlen(buf, cap);
va_list ap;
va_start(ap, format);
assert(0 <= vsnprintf(&buf[len], cap - len, format, ap));
va_end(ap);
}
enum Color { enum Color {
White, Black, Blue, Green, Red, Brown, Magenta, Orange, White, Black, Blue, Green, Red, Brown, Magenta, Orange,
Yellow, LightGreen, Cyan, LightCyan, LightBlue, Pink, Gray, LightGray, Yellow, LightGreen, Cyan, LightCyan, LightBlue, Pink, Gray, LightGray,

View File

@ -353,8 +353,7 @@ static void handleQuit(struct Message *msg) {
static void handleReplyNames(struct Message *msg) { static void handleReplyNames(struct Message *msg) {
require(msg, false, 4); require(msg, false, 4);
uint id = idFor(msg->params[2]); uint id = idFor(msg->params[2]);
char buf[1024]; char buf[1024] = "";
size_t len = 0;
while (msg->params[3]) { while (msg->params[3]) {
char *name = strsep(&msg->params[3], " "); char *name = strsep(&msg->params[3], " ");
char *prefixes = strsep(&name, "!"); char *prefixes = strsep(&name, "!");
@ -363,12 +362,10 @@ static void handleReplyNames(struct Message *msg) {
enum Color color = (user ? hash(user) : Default); enum Color color = (user ? hash(user) : Default);
completeAdd(id, nick, color); completeAdd(id, nick, color);
if (!replies.names) continue; if (!replies.names) continue;
int n = snprintf( catf(
&buf[len], sizeof(buf) - len, buf, sizeof(buf), "%s\3%02d%s\3",
"%s\3%02d%s\3", (len ? ", " : ""), color, prefixes (buf[0] ? ", " : ""), color, prefixes
); );
assert(n > 0 && len + n < sizeof(buf));
len += n;
} }
if (!replies.names) return; if (!replies.names) return;
uiFormat( uiFormat(
@ -515,17 +512,14 @@ static void handleReplyWhoisIdle(struct Message *msg) {
static void handleReplyWhoisChannels(struct Message *msg) { static void handleReplyWhoisChannels(struct Message *msg) {
require(msg, false, 3); require(msg, false, 3);
if (!replies.whois) return; if (!replies.whois) return;
char buf[1024]; char buf[1024] = "";
size_t len = 0;
while (msg->params[2]) { while (msg->params[2]) {
char *channel = strsep(&msg->params[2], " "); char *channel = strsep(&msg->params[2], " ");
char *name = &channel[strspn(channel, network.prefixes)]; char *name = &channel[strspn(channel, network.prefixes)];
int n = snprintf( catf(
&buf[len], sizeof(buf) - len, buf, sizeof(buf), "%s\3%02d%s\3",
"%s\3%02d%s\3", (len ? ", " : ""), hash(name), channel (buf[0] ? ", " : ""), hash(name), channel
); );
assert(n > 0 && len + n < sizeof(buf));
len += n;
} }
uiFormat( uiFormat(
Network, Warm, tagTime(msg), Network, Warm, tagTime(msg),
@ -622,12 +616,10 @@ static const char *colorMentions(uint id, struct Message *msg) {
*split = '\0'; *split = '\0';
static char buf[1024]; static char buf[1024];
FILE *str = fmemopen(buf, sizeof(buf), "w"); buf[0] = '\0';
if (!str) err(EX_OSERR, "fmemopen");
while (*mention) { while (*mention) {
size_t skip = strspn(mention, ",<> "); size_t skip = strspn(mention, ",<> ");
fwrite(mention, skip, 1, str); catf(buf, sizeof(buf), "%.*s", (int)skip, mention);
mention += skip; mention += skip;
size_t len = strcspn(mention, ",<> "); size_t len = strcspn(mention, ",<> ");
@ -635,17 +627,14 @@ static const char *colorMentions(uint id, struct Message *msg) {
mention[len] = '\0'; mention[len] = '\0';
enum Color color = completeColor(id, mention); enum Color color = completeColor(id, mention);
if (color != Default) { if (color != Default) {
fprintf(str, "\3%02d%s\3", color, mention); catf(buf, sizeof(buf), "\3%02d%s\3", color, mention);
} else { } else {
fprintf(str, "%s", mention); catf(buf, sizeof(buf), "%s", mention);
} }
mention[len] = punct; mention[len] = punct;
mention += len; mention += len;
} }
fputc(delimit, str); catf(buf, sizeof(buf), "%c", delimit);
fclose(str);
buf[sizeof(buf) - 1] = '\0';
return buf; return buf;
} }

23
ui.c
View File

@ -428,15 +428,15 @@ static void statusUpdate(void) {
const struct Window *window = windows.ptrs[windows.show]; const struct Window *window = windows.ptrs[windows.show];
snprintf(title, sizeof(title), "%s %s", network.name, idNames[window->id]); snprintf(title, sizeof(title), "%s %s", network.name, idNames[window->id]);
if (window->mark && window->unreadWarm) { if (window->mark && window->unreadWarm) {
snprintf( catf(
&title[strlen(title)], sizeof(title) - strlen(title), title, sizeof(title), " (%d%s)",
" (%d%s)", window->unreadWarm, (window->heat > Warm ? "!" : "") window->unreadWarm, (window->heat > Warm ? "!" : "")
); );
} }
if (otherUnread) { if (otherUnread) {
snprintf( catf(
&title[strlen(title)], sizeof(title) - strlen(title), title, sizeof(title), " (+%d%s)",
" (+%d%s)", otherUnread, (otherHeat > Warm ? "!" : "") otherUnread, (otherHeat > Warm ? "!" : "")
); );
} }
} }
@ -560,14 +560,13 @@ static void notify(uint id, const char *str) {
struct Util util = uiNotifyUtil; struct Util util = uiNotifyUtil;
utilPush(&util, idNames[id]); utilPush(&util, idNames[id]);
size_t len = 0;
char buf[1024] = ""; char buf[1024] = "";
while (*str && len < sizeof(buf)) { while (*str) {
size_t run; size_t len;
struct Style style = Reset; struct Style style = Reset;
styleParse(&style, &str, &run); styleParse(&style, &str, &len);
len += snprintf(&buf[len], sizeof(buf) - len, "%.*s", (int)run, str); catf(buf, sizeof(buf), "%.*s", (int)len, str);
str += run; str += len;
} }
utilPush(&util, buf); utilPush(&util, buf);