~arivigo/sline

df305e2e4172e17134680785d9dd01646f743cfe — Ariadna Vigo 6 months ago 528b7e9
Merging history.c into sline.c

After making sline_history_get() public, history.c sort of became a
weird module in isolation. After trying to split out other components
of sline.c into their own modules, which didn't work out at all, I
believe sline is a relatively small codebase that can easily be one
single module (this even opens the possibility of trivially embedding
it into your code without even bothering to use any of the libraries!)
strlcpy.c remains a different module because it's code that comes from
elsewhere.
4 files changed, 38 insertions(+), 57 deletions(-)

M Makefile
D history.c
D history.h
M sline.c
M Makefile => Makefile +2 -4
@@ 4,7 4,7 @@

include config.mk

SRC = history.c sline.c strlcpy.c
SRC = sline.c strlcpy.c
OBJ = ${SRC:.c=.o}
MAN = man/sline.3 man/sline_end.3 man/sline_errmsg.3 man/sline_history_get.3 \
      man/sline_setup.3 man/sline_set_prompt.3 man/sline_version.3


@@ 19,9 19,7 @@ options:
	@echo "CC       = ${CC}"
	@echo

history.o: strlcpy.h

sline.o: history.h sline.h strlcpy.h
sline.o: sline.h strlcpy.h

strlcpy.o: strlcpy.h


D history.c => history.c +0 -40
@@ 1,40 0,0 @@
/* See LICENSE for copyright and license details. */

#include <string.h>

#include "history.h"
#include "strlcpy.h"

#define HISTORY_SIZE 50

char *history[HISTORY_SIZE];
int hist_top, hist_pos;
size_t hist_entry_size;

void
history_next(void)
{
	if (strlen(history[hist_top]) == 0)
		return;

	++hist_top;
	if (hist_top >= HISTORY_SIZE)
		history_rotate();
}

void
history_rotate(void)
{
	int i;

	for (i = 1; i < HISTORY_SIZE; ++i)
		strlcpy(history[i - 1], history[i], hist_entry_size);

	--hist_top;
}

void
history_set(int pos, const char *input)
{
	strlcpy(history[pos], input, hist_entry_size);
}

D history.h => history.h +0 -11
@@ 1,11 0,0 @@
/* See LICENSE for copyright and license details. */

#define HISTORY_SIZE 50

void history_next(void);
void history_rotate(void);
void history_set(int pos, const char *input);

extern char *history[HISTORY_SIZE];
extern int hist_top, hist_pos;
extern size_t hist_entry_size;

M sline.c => sline.c +36 -2
@@ 7,11 7,11 @@
#include <termios.h>
#include <unistd.h>

#include "history.h"
#include "sline.h"
#include "strlcpy.h"

#define CURSOR_BUF_SIZE 16 /* Used for cursor movement directives */
#define HISTORY_SIZE 50
#define SLINE_PROMPT_DEFAULT "> " 
#define SLINE_PROMPT_SIZE 32
#define UTF8_BYTES 4


@@ 53,8 53,14 @@ static void chr_delete(char *buf, size_t size, int bsmode);
static void chr_ins(char *buf, size_t size, const char *utf8);
static void chr_return(const char *buf);

static void history_next(void);
static void history_rotate(void);
static void history_set(int pos, const char *input);

static char sline_prompt[SLINE_PROMPT_SIZE];
static size_t pos, buf_i;
static char *history[HISTORY_SIZE];
static size_t hist_entry_size, pos, buf_i;
static int hist_top, hist_pos;
static int sline_history = 1; /* History feature on by default */
static struct termios old, term;



@@ 409,6 415,34 @@ chr_return(const char *buf)
	}
}

static void
history_next(void)
{
	if (strlen(history[hist_top]) == 0)
		return;

	++hist_top;
	if (hist_top >= HISTORY_SIZE)
		history_rotate();
}

static void
history_rotate(void)
{
	int i;

	for (i = 1; i < HISTORY_SIZE; ++i)
		strlcpy(history[i - 1], history[i], hist_entry_size);

	--hist_top;
}

static void
history_set(int pos, const char *input)
{
	strlcpy(history[pos], input, hist_entry_size);
}

/* Public sline API subroutines follow */

int