Added printc_err function.
This commit is contained in:
parent
93c3cb342b
commit
b6eda4e225
45
output.c
45
output.c
|
@ -21,33 +21,38 @@
|
||||||
#include "opdb.h"
|
#include "opdb.h"
|
||||||
#include "output.h"
|
#include "output.h"
|
||||||
|
|
||||||
static char out_buf[1024];
|
struct outbuf {
|
||||||
static int out_len;
|
char buf[1024];
|
||||||
static int in_code;
|
int len;
|
||||||
|
int in_code;
|
||||||
|
};
|
||||||
|
|
||||||
static int write_text(const char *buf)
|
static struct outbuf stdout_buf;
|
||||||
|
static struct outbuf stderr_buf;
|
||||||
|
|
||||||
|
static int write_text(struct outbuf *out, const char *buf, FILE *fout)
|
||||||
{
|
{
|
||||||
int want_color = opdb_get_boolean("color");
|
int want_color = opdb_get_boolean("color");
|
||||||
int len = 0;
|
int len = 0;
|
||||||
|
|
||||||
while (*buf) {
|
while (*buf) {
|
||||||
if (*buf == 27)
|
if (*buf == 27)
|
||||||
in_code = 1;
|
out->in_code = 1;
|
||||||
|
|
||||||
if (!in_code)
|
if (!out->in_code)
|
||||||
len++;
|
len++;
|
||||||
|
|
||||||
if (*buf == '\n') {
|
if (*buf == '\n') {
|
||||||
out_buf[out_len] = 0;
|
out->buf[out->len] = 0;
|
||||||
puts(out_buf);
|
fprintf(fout, "%s\n", out->buf);
|
||||||
out_len = 0;
|
out->len = 0;
|
||||||
} else if ((want_color || !in_code) &&
|
} else if ((want_color || !out->in_code) &&
|
||||||
out_len + 1 < sizeof(out_buf)) {
|
out->len + 1 < sizeof(out->buf)) {
|
||||||
out_buf[out_len++] = *buf;
|
out->buf[out->len++] = *buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isalpha(*buf))
|
if (isalpha(*buf))
|
||||||
in_code = 0;
|
out->in_code = 0;
|
||||||
|
|
||||||
buf++;
|
buf++;
|
||||||
}
|
}
|
||||||
|
@ -64,5 +69,17 @@ int printc(const char *fmt, ...)
|
||||||
vsnprintf(buf, sizeof(buf), fmt, ap);
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
return write_text(buf);
|
return write_text(&stdout_buf, buf, stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
int printc_err(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
char buf[1024];
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
vsnprintf(buf, sizeof(buf), fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
return write_text(&stderr_buf, buf, stderr);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue