print_file_contents: define parameter struct, use strncpy()

The list of parameters was getting too lengthy.
This commit is contained in:
Michael Stapelberg 2021-11-02 19:40:40 +01:00
parent 8598a76681
commit 11d5c9863e
3 changed files with 45 additions and 14 deletions

View File

@ -779,7 +779,17 @@ int main(int argc, char *argv[]) {
CASE_SEC_TITLE("read_file") {
SEC_OPEN_MAP("read_file");
print_file_contents(json_gen, buffer, title, cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_bad"), cfg_getint(sec, "max_characters"));
file_contents_ctx_t ctx = {
.json_gen = json_gen,
.buf = buffer,
.buflen = sizeof(buffer),
.title = title,
.path = cfg_getstr(sec, "path"),
.format = cfg_getstr(sec, "format"),
.format_bad = cfg_getstr(sec, "format_bad"),
.max_chars = cfg_getint(sec, "max_characters"),
};
print_file_contents(&ctx);
SEC_CLOSE_MAP;
}
}

View File

@ -175,6 +175,13 @@ extern char *pct_mark;
} \
} while (0)
#define OUTPUT_FORMATTED \
do { \
const size_t remaining = ctx->buflen - (outwalk - ctx->buf); \
strncpy(outwalk, formatted, remaining); \
outwalk += strlen(formatted); \
} while (0)
/*
* The "min_width" module option may either be defined as a string or a number.
*/
@ -246,7 +253,19 @@ bool process_runs(const char *path);
int volume_pulseaudio(uint32_t sink_idx, const char *sink_name);
bool description_pulseaudio(uint32_t sink_idx, const char *sink_name, char buffer[MAX_SINK_DESCRIPTION_LEN]);
bool pulse_initialize(void);
void print_file_contents(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_bad, const int max_chars);
typedef struct {
yajl_gen json_gen;
char *buf;
size_t buflen;
const char *title;
const char *path;
const char *format;
const char *format_bad;
const int max_chars;
} file_contents_ctx_t;
void print_file_contents(file_contents_ctx_t *ctx);
/* socket file descriptor for general purposes */
extern int general_socket;

View File

@ -14,27 +14,28 @@
#define STRING_SIZE 10
void print_file_contents(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format, const char *format_bad, const int max_chars) {
const char *walk = format;
char *outwalk = buffer;
char *buf = scalloc(max_chars * sizeof(char) + 1);
void print_file_contents(file_contents_ctx_t *ctx) {
const char *walk = ctx->format;
char *outwalk = ctx->buf;
char *buf = scalloc(ctx->max_chars * sizeof(char) + 1);
char *abs_path = resolve_tilde(path);
char *abs_path = resolve_tilde(ctx->path);
int fd = open(abs_path, O_RDONLY);
free(abs_path);
INSTANCE(path);
#define json_gen ctx->json_gen
INSTANCE(ctx->path);
int n = -1;
if (fd > -1) {
n = read(fd, buf, max_chars);
n = read(fd, buf, ctx->max_chars);
if (n != -1) {
buf[n] = '\0';
}
(void)close(fd);
START_COLOR("color_good");
} else if (errno != 0) {
walk = format_bad;
walk = ctx->format_bad;
START_COLOR("color_bad");
}
@ -53,18 +54,19 @@ void print_file_contents(yajl_gen json_gen, char *buffer, const char *title, con
sprintf(string_errno, "%d", errno);
placeholder_t placeholders[] = {
{.name = "%title", .value = title},
{.name = "%title", .value = ctx->title},
{.name = "%content", .value = buf},
{.name = "%errno", .value = string_errno},
{.name = "%error", .value = strerror(errno)}};
const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
char *formatted = format_placeholders(walk, &placeholders[0], num);
strcpy(outwalk, formatted);
outwalk += strlen(formatted);
OUTPUT_FORMATTED;
free(formatted);
free(buf);
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
OUTPUT_FULL_TEXT(ctx->buf);
}