Skip to content

Commit

Permalink
Make xbuf public so it can be used in main code, not just lesskey_parse.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed May 25, 2021
1 parent 2c1174a commit 5e3b29e
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Makefile.aut
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SRC = \
help.c ifile.c input.c jump.c line.c linenum.c \
lsystem.c mark.c optfunc.c option.c opttbl.c os.c \
output.c pattern.c position.c prompt.c search.c signal.c \
tags.c ttyin.c version.c
tags.c ttyin.c version.c xbuf.c
DISTFILES_W = \
defines.ds Makefile.dsb Makefile.dsg Makefile.dsu \
defines.o2 Makefile.o2e \
Expand Down
6 changes: 3 additions & 3 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ OBJ = \
line.${O} linenum.${O} \
lsystem.${O} mark.${O} optfunc.${O} option.${O} opttbl.${O} os.${O} \
output.${O} pattern.${O} position.${O} prompt.${O} search.${O} signal.${O} \
tags.${O} ttyin.${O} version.${O} @REGEX_O@
tags.${O} ttyin.${O} version.${O} xbuf.${O} @REGEX_O@

ifneq (@SECURE_COMPILE@,1)
OBJ += lesskey_parse.${O}
Expand All @@ -61,8 +61,8 @@ all: less$(EXEEXT) lesskey$(EXEEXT) lessecho$(EXEEXT)
less$(EXEEXT): ${OBJ}
${CC} ${LDFLAGS} -o $@ ${OBJ} ${LIBS}

lesskey$(EXEEXT): lesskey.${O} lesskey_parse.${O} version.${O}
${CC} ${LDFLAGS} -o $@ lesskey.${O} lesskey_parse.${O} version.${O}
lesskey$(EXEEXT): lesskey.${O} lesskey_parse.${O} xbuf.${O} version.${O}
${CC} ${LDFLAGS} -o $@ lesskey.${O} lesskey_parse.${O} xbuf.${O} version.${O}

lessecho$(EXEEXT): lessecho.${O} version.${O}
${CC} ${LDFLAGS} -o $@ lessecho.${O} version.${O}
Expand Down
1 change: 1 addition & 0 deletions less.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ struct loption;
struct hilite_tree;
struct ansi_state;
#include "pattern.h"
#include "xbuf.h"
#include "funcs.h"

/* Functions not included in funcs.h */
Expand Down
8 changes: 1 addition & 7 deletions lesskey.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*@@copyright@@*/

#include "xbuf.h"

/*
* Format of a lesskey file:
Expand Down Expand Up @@ -31,13 +32,6 @@
/* */
#define KRADIX 64

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

struct lesskey_cmdname
{
char *cn_name;
Expand Down
33 changes: 1 addition & 32 deletions lesskey_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdlib.h>
#include "lesskey.h"
#include "cmd.h"
#include "xbuf.h"
#include "defines.h"

#define CONTROL(c) ((c)&037)
Expand Down Expand Up @@ -124,38 +125,6 @@ parse_error(s1, s2)
lesskey_parse_error(buf);
}

/*
* Initialize an expandable text buffer.
*/
static void
xbuf_init(xbuf)
struct xbuffer *xbuf;
{
xbuf->size = 16;
xbuf->data = ecalloc(xbuf->size, sizeof(char));
xbuf->end = 0;
}

/*
* Add a char to an expandable text buffer.
*/
static void
xbuf_add(xbuf, ch)
struct xbuffer *xbuf;
char ch;
{
if (xbuf->end >= xbuf->size)
{
char *data;
xbuf->size = xbuf->size * 2;
data = ecalloc(xbuf->size, sizeof(char));
memcpy(data, xbuf->data, xbuf->end);
free(xbuf->data);
xbuf->data = data;
}
xbuf->data[xbuf->end++] = ch;
}

/*
* Initialize lesskey_tables.
*/
Expand Down
45 changes: 45 additions & 0 deletions xbuf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "less.h"
#include "xbuf.h"

/*
* Initialize an expandable text buffer.
*/
public void
xbuf_init(xbuf)
struct xbuffer *xbuf;
{
xbuf->data = NULL;
xbuf->size = xbuf->end = 0;
}

public void
xbuf_reset(xbuf)
struct xbuffer *xbuf;
{
if (xbuf->data != NULL)
free(xbuf->data);
xbuf_init(xbuf);
}

/*
* Add a char to an expandable text buffer.
*/
public void
xbuf_add(xbuf, ch)
struct xbuffer *xbuf;
char ch;
{
if (xbuf->end >= xbuf->size)
{
char *data;
xbuf->size = (xbuf->size == 0) ? 16 : xbuf->size * 2;
data = ecalloc(xbuf->size, sizeof(char));
if (xbuf->data != NULL)
{
memcpy(data, xbuf->data, xbuf->end);
free(xbuf->data);
}
xbuf->data = data;
}
xbuf->data[xbuf->end++] = ch;
}
15 changes: 15 additions & 0 deletions xbuf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef XBUF_H_
#define XBUF_H_

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

void xbuf_init(struct xbuffer *xbuf);
void xbuf_reset(struct xbuffer *xbuf);
void xbuf_add(struct xbuffer *xbuf, char ch);

#endif

0 comments on commit 5e3b29e

Please sign in to comment.