From 5fcce3d8b711e6f1fae75b9c9f59f2cbe40e5ccf Mon Sep 17 00:00:00 2001 From: nick black Date: Sun, 13 Jun 2021 14:57:47 -0400 Subject: [PATCH] init vtmachine: extract numeric arguments for sixel #1469 --- src/lib/input.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/lib/input.c b/src/lib/input.c index 86df7de7c..f7df3504e 100644 --- a/src/lib/input.c +++ b/src/lib/input.c @@ -641,9 +641,24 @@ typedef struct init_state { STATE_SIXEL_CREGS, // reading max color registers until 'S' STATE_XTSMGRAPHICS_DRAIN, // drain out XTSMGRAPHICS to 'S' } state; + int numeric; // currently-lexed numeric bool xtgettcap_good; // high when we've received DCS 1 } init_state; +static int +ruts_numeric(int* numeric, unsigned char c){ + if(!isdigit(c)){ + return -1; + } + int digit = c - '0'; + if(INT_MAX / 10 - digit < *numeric){ // would overflow + return -1; + } + *numeric *= 10; + *numeric += digit; + return 0; +} + // FIXME ought implement the full Williams automaton // FIXME doesn't handle 8-bit controls (would need convert UTF-8) // returns 1 after handling the Device Attributes response, 0 if more input @@ -663,6 +678,7 @@ pump_control_read(init_state* inits, unsigned char c){ // not an escape -- throw into user queue break; case STATE_ESC: + inits->numeric = 0; if(c == '['){ inits->state = STATE_CSI; }else if(c == 'P'){ @@ -790,8 +806,11 @@ pump_control_read(init_state* inits, unsigned char c){ break; case STATE_SIXEL_CREGS: if(c == 'S'){ - // FIXME extract color register count +fprintf(stderr, "%d color registers!\n", inits->numeric); + // FIXME set color register count inits->state = STATE_NULL; + }else if(ruts_numeric(&inits->numeric, c)){ + return -1; } break; case STATE_DA_6: @@ -832,14 +851,21 @@ pump_control_read(init_state* inits, unsigned char c){ break; case STATE_SIXEL_WIDTH: if(c == ';'){ - // FIXME extract width +fprintf(stderr, "%d max sixel width!\n", inits->numeric); + // FIXME set width inits->state = STATE_SIXEL_HEIGHT; + inits->numeric = 0; + }else if(ruts_numeric(&inits->numeric, c)){ + return -1; } break; case STATE_SIXEL_HEIGHT: if(c == 'S'){ - // FIXME extract height +fprintf(stderr, "%d max sixel height!\n", inits->numeric); + // FIXME set height inits->state = STATE_NULL; + }else if(ruts_numeric(&inits->numeric, c)){ + return -1; } break; case STATE_XTSMGRAPHICS_DRAIN: