Skip to content

Commit

Permalink
Make xbuf hold bytes not chars.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Sep 20, 2022
1 parent 03f011f commit 83c8c81
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
6 changes: 3 additions & 3 deletions decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -916,10 +916,10 @@ lesskey_src(filename, sysvar)
int r = parse_lesskey(filename, &tables);
if (r != 0)
return (r);
add_fcmd_table(tables.cmdtable.buf.data, tables.cmdtable.buf.end);
add_ecmd_table(tables.edittable.buf.data, tables.edittable.buf.end);
add_fcmd_table(xbuf_char_data(&tables.cmdtable.buf), tables.cmdtable.buf.end);
add_ecmd_table(xbuf_char_data(&tables.edittable.buf), tables.edittable.buf.end);
add_var_table(sysvar ? &list_sysvar_tables : &list_var_tables,
tables.vartable.buf.data, tables.vartable.buf.end);
xbuf_char_data(&tables.vartable.buf), tables.vartable.buf.end);
return (0);
}

Expand Down
8 changes: 4 additions & 4 deletions lesskey_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,10 @@ clean_line(s)
*/
static void
add_cmd_char(c, tables)
int c;
char c;
struct lesskey_tables *tables;
{
xbuf_add(&tables->currtable->buf, c);
xbuf_add_byte(&tables->currtable->buf, (unsigned char) c);
}

static void
Expand Down Expand Up @@ -563,14 +563,14 @@ parse_cmdline(p, tables)
p = skipsp(p);
if (*p == '\0')
{
add_cmd_char(action, tables);
add_cmd_char((char) action, tables);
} else
{
/*
* OR the special value A_EXTRA into the action byte.
* Put the extra string after the action byte.
*/
add_cmd_char(action | A_EXTRA, tables);
add_cmd_char((char) (action | A_EXTRA), tables);
while (*p != '\0')
add_cmd_str(tstr(&p, 0), tables);
add_cmd_char('\0', tables);
Expand Down
10 changes: 5 additions & 5 deletions line.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ pshift_all(VOID_PARAM)
int i;
for (i = linebuf.print; i < linebuf.end; i++)
if (linebuf.attr[i] == AT_ANSI)
xbuf_add(&shifted_ansi, linebuf.buf[i]);
xbuf_add_byte(&shifted_ansi, (unsigned char) linebuf.buf[i]);
linebuf.end = linebuf.print;
end_column = linebuf.pfx_end;
}
Expand Down Expand Up @@ -808,7 +808,7 @@ store_char(ch, a, rep, pos)
{
/* We haven't left-shifted enough yet. */
if (a == AT_ANSI)
xbuf_add(&shifted_ansi, ch); /* Save ANSI attributes */
xbuf_add_byte(&shifted_ansi, (unsigned char) ch); /* Save ANSI attributes */
if (linebuf.end > linebuf.print)
{
/* Shift left enough to put last byte of this char at print-1. */
Expand Down Expand Up @@ -1038,21 +1038,21 @@ store_ansi(ch, rep, pos)
STORE_CHAR(ch, AT_ANSI, rep, pos);
if (line_ansi->hlink)
hlink_in_line = 1;
xbuf_add(&last_ansi, ch);
xbuf_add_byte(&last_ansi, (unsigned char) ch);
break;
case ANSI_END:
STORE_CHAR(ch, AT_ANSI, rep, pos);
ansi_done(line_ansi);
line_ansi = NULL;
xbuf_add(&last_ansi, ch);
xbuf_add_byte(&last_ansi, (unsigned char) ch);
xbuf_set(&last_ansis[curr_last_ansi], &last_ansi);
xbuf_reset(&last_ansi);
curr_last_ansi = (curr_last_ansi + 1) % NUM_LAST_ANSIS;
break;
case ANSI_ERR:
{
/* Remove whole unrecognized sequence. */
char *start = (cshift < hshift) ? shifted_ansi.data : linebuf.buf;
char *start = (cshift < hshift) ? xbuf_char_data(&shifted_ansi): linebuf.buf;
int *end = (cshift < hshift) ? &shifted_ansi.end : &linebuf.end;
char *p = start + *end;
LWCHAR bch;
Expand Down
37 changes: 26 additions & 11 deletions xbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,37 @@ xbuf_reset(xbuf)
}

/*
* Add a char to an expandable text buffer.
* Add a byte to an expandable text buffer.
*/
public void
xbuf_add(xbuf, ch)
xbuf_add_byte(xbuf, b)
struct xbuffer *xbuf;
int ch;
unsigned char b;
{
if (xbuf->end >= xbuf->size)
{
char *data;
unsigned char *data;
xbuf->size = (xbuf->size == 0) ? 16 : xbuf->size * 2;
data = (char *) ecalloc(xbuf->size, sizeof(char));
data = (unsigned char *) ecalloc(xbuf->size, sizeof(unsigned char));
if (xbuf->data != NULL)
{
memcpy(data, xbuf->data, xbuf->end);
free(xbuf->data);
}
xbuf->data = data;
}
xbuf->data[xbuf->end++] = ch;
xbuf->data[xbuf->end++] = b;
}

public void
xbuf_add_data(xbuf, data, len)
struct xbuffer *xbuf;
unsigned char *data;
int len;
{
int i;
for (i = 0; i < len; i++)
xbuf_add_byte(xbuf, data[i]);
}

public int
Expand All @@ -57,17 +68,21 @@ xbuf_pop(buf)
{
if (buf->end == 0)
return -1;
return buf->data[--(buf->end)];
return (int) buf->data[--(buf->end)];
}

public void
xbuf_set(dst, src)
struct xbuffer *dst;
struct xbuffer *src;
{
int i;

xbuf_reset(dst);
for (i = 0; i < src->end; i++)
xbuf_add(dst, src->data[i]);
xbuf_add_data(dst, src->data, src->end);
}

public char *
xbuf_char_data(xbuf)
struct xbuffer *xbuf;
{
return (char *)(xbuf->data);
}
8 changes: 5 additions & 3 deletions xbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

struct xbuffer
{
char *data;
unsigned char *data;
int end;
int size;
};

void xbuf_init(struct xbuffer *xbuf);
void xbuf_reset(struct xbuffer *xbuf);
void xbuf_add(struct xbuffer *xbuf, int ch);
void xbuf_add_byte(struct xbuffer *xbuf, unsigned char b);
void xbuf_add_data(struct xbuffer *xbuf, unsigned char *data, int len);
int xbuf_pop(struct xbuffer *xbuf);

char *xbuf_char_data();

#endif

0 comments on commit 83c8c81

Please sign in to comment.