move vterm to heap to avoid stack overflow
This commit is contained in:
33
ansi2html.h
33
ansi2html.h
@@ -82,7 +82,7 @@ static int color_css(char* buf, size_t size, int32_t color, int is_bg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static char* vterm_render(const vterm_t* vt) {
|
static char* vterm_render(const vterm_t* vt) {
|
||||||
const size_t cap =
|
const size_t cap = 1024
|
||||||
+ (size_t)(vt->last_row + 1) * 4
|
+ (size_t)(vt->last_row + 1) * 4
|
||||||
+ (size_t)(vt->last_row + 1) * (vt->last_col + 1) *
|
+ (size_t)(vt->last_row + 1) * (vt->last_col + 1) *
|
||||||
(sizeof("<span class=\" b i u s d k\" style=\"color:#xxxxxx;background:#xxxxxx;\">&</span>") - 1);
|
(sizeof("<span class=\" b i u s d k\" style=\"color:#xxxxxx;background:#xxxxxx;\">&</span>") - 1);
|
||||||
@@ -288,13 +288,14 @@ static int utf8_len(const unsigned char c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static char* ansi2html(const char* ansislop) {
|
static char* ansi2html(const char* ansislop) {
|
||||||
vterm_t vt = {0};
|
vterm_t* vt = malloc(sizeof(vterm_t));
|
||||||
|
memset(vt, 0, sizeof(vterm_t));
|
||||||
|
|
||||||
vt.slop.fg = vt.slop.bg = -1;
|
vt->slop.fg = vt->slop.bg = -1;
|
||||||
for (int r = 0; r < VTERM_ROWS; r++) {
|
for (int r = 0; r < VTERM_ROWS; r++) {
|
||||||
for (int c = 0; c < VTERM_COLS; c++) {
|
for (int c = 0; c < VTERM_COLS; c++) {
|
||||||
vt.cells[r][c].slop.fg = vt.cells[r][c].slop.bg = -1;
|
vt->cells[r][c].slop.fg = vt->cells[r][c].slop.bg = -1;
|
||||||
vt.cells[r][c].utf8[0] = ' ';
|
vt->cells[r][c].utf8[0] = ' ';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,7 +303,7 @@ static char* ansi2html(const char* ansislop) {
|
|||||||
if (*p == '\e') {
|
if (*p == '\e') {
|
||||||
p++;
|
p++;
|
||||||
if (*p == '[') {
|
if (*p == '[') {
|
||||||
p = vterm_csi(&vt, p + 1);
|
p = vterm_csi(vt, p + 1);
|
||||||
}
|
}
|
||||||
else if (*p == '(') {
|
else if (*p == '(') {
|
||||||
p += 2;
|
p += 2;
|
||||||
@@ -313,19 +314,19 @@ static char* ansi2html(const char* ansislop) {
|
|||||||
}
|
}
|
||||||
else if (*p == '\r') {
|
else if (*p == '\r') {
|
||||||
p++;
|
p++;
|
||||||
vt.col = 0;
|
vt->col = 0;
|
||||||
}
|
}
|
||||||
else if (*p == '\n') {
|
else if (*p == '\n') {
|
||||||
p++;
|
p++;
|
||||||
vt.col = 0;
|
vt->col = 0;
|
||||||
vt.row++;
|
vt->row++;
|
||||||
}
|
}
|
||||||
else if (*p == '\t') {
|
else if (*p == '\t') {
|
||||||
p++;
|
p++;
|
||||||
vt.col = (vt.col + 8) & ~7;
|
vt->col = (vt->col + 8) & ~7;
|
||||||
if (vt.col >= VTERM_COLS) {
|
if (vt->col >= VTERM_COLS) {
|
||||||
vt.col = 0;
|
vt->col = 0;
|
||||||
vt.row++;
|
vt->row++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ((uint8_t)*p < 0x20) {
|
else if ((uint8_t)*p < 0x20) {
|
||||||
@@ -333,9 +334,11 @@ static char* ansi2html(const char* ansislop) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const int l = utf8_len((unsigned char)*p);
|
const int l = utf8_len((unsigned char)*p);
|
||||||
vterm_put(&vt, p, l);
|
vterm_put(vt, p, l);
|
||||||
p += l;
|
p += l;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return vterm_render(&vt);
|
char* html = vterm_render(vt);
|
||||||
|
free(vt);
|
||||||
|
return html;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user