osc: allocate data buffer dynamically

This commit is contained in:
Daniel Eklöf 2019-07-19 08:59:35 +02:00
parent 95ff37afd7
commit 153628a217
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
5 changed files with 30 additions and 4 deletions

1
main.c
View file

@ -818,6 +818,7 @@ out:
if (term.kbd.xkb != NULL)
xkb_context_unref(term.kbd.xkb);
free(term.vt.osc.data);
for (int row = 0; row < term.normal.num_rows; row++)
grid_row_free(term.normal.rows[row]);
free(term.normal.rows);

20
osc.c
View file

@ -83,3 +83,23 @@ osc_dispatch(struct terminal *term)
break;
}
}
bool
osc_ensure_size(struct terminal *term, size_t required_size)
{
if (required_size <= term->vt.osc.size)
return true;
size_t new_size = (required_size + 127) / 128 * 128;
assert(new_size > 0);
uint8_t *new_data = realloc(term->vt.osc.data, new_size);
if (new_data == NULL) {
LOG_ERRNO("failed to increase size of OSC buffer");
return false;
}
term->vt.osc.data = new_data;
term->vt.osc.size = new_size;
return true;
}

1
osc.h
View file

@ -3,4 +3,5 @@
#include <stdbool.h>
#include "terminal.h"
bool osc_ensure_size(struct terminal *term, size_t size);
void osc_dispatch(struct terminal *term);

View file

@ -125,7 +125,8 @@ struct vt {
} params;
char private;
struct {
uint8_t data[1024];
uint8_t *data;
size_t size;
size_t idx;
} osc;
struct {

9
vt.c
View file

@ -909,12 +909,15 @@ action(struct terminal *term, enum action _action, uint8_t c)
break;
case ACTION_OSC_PUT:
if (term->vt.osc.idx < (int)sizeof(term->vt.osc.data) - 1)
term->vt.osc.data[term->vt.osc.idx++] = c;
if (!osc_ensure_size(term, term->vt.osc.idx + 1))
break;
term->vt.osc.data[term->vt.osc.idx++] = c;
break;
case ACTION_OSC_END:
assert(term->vt.osc.idx < sizeof(term->vt.osc.data));
if (!osc_ensure_size(term, term->vt.osc.idx + 1))
break;
term->vt.osc.data[term->vt.osc.idx] = '\0';
osc_dispatch(term);
break;