splitting things out

This commit is contained in:
Kyle Isom 2020-02-11 09:52:49 -08:00
parent 464de25980
commit fd45ff4019
4 changed files with 94 additions and 78 deletions

View File

@ -1,5 +1,5 @@
BIN := ke
OBJS := main.o
OBJS := main.o abuf.o
LDFLAGS :=
CFLAGS := -pedantic -Wall -Werror -Wextra -O0 -std=c99 -g
@ -10,8 +10,8 @@ all: build
.PHONY: build
build: $(BIN)
$(BIN): main.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ main.c
$(BIN): $(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS)
.PHONY: clean
clean:
@ -30,4 +30,3 @@ install: $(BIN)
cp $(BIN) $(HOME)/bin/
%.o: %.c

27
ke/abuf.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdlib.h>
#include <string.h>
#include "abuf.h"
void
ab_append(struct abuf *buf, const char *s, int len)
{
char *nc = realloc(buf->b, buf->len + len);
if (nc == NULL) {
abort();
}
memcpy(&nc[buf->len], s, len);
buf->b = nc;
buf->len += len; /* DANGER: overflow */
}
void
ab_free(struct abuf *buf)
{
free(buf->b);
buf->b = NULL;
}

17
ke/abuf.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef KE_ABUF_H
#define KE_ABUF_H
struct abuf {
char *b;
int len;
};
#define ABUF_INIT {NULL, 0}
void ab_append(struct abuf *buf, const char *s, int len);
void ab_free(struct abuf *buf);
#endif /* KE_ABUF_H */

View File

@ -5,6 +5,7 @@
* https://viewsourcecode.org/snaptoken/kilo/
*/
#include <sys/ioctl.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
@ -17,6 +18,8 @@
#include <time.h>
#include <unistd.h>
#include "abuf.h"
#define KE_VERSION "0.0.1-pre"
#define ESCSEQ "\x1b["
@ -82,36 +85,6 @@ struct {
} editor;
/* append buffer */
struct abuf {
char *b;
int len;
};
#define ABUF_INIT {NULL, 0}
void
ab_append(struct abuf *buf, const char *s, int len)
{
char *nc = realloc(buf->b, buf->len + len);
if (nc == NULL) {
abort();
}
memcpy(&nc[buf->len], s, len);
buf->b = nc;
buf->len += len; /* DANGER: overflow */
}
void
ab_free(struct abuf *buf)
{
free(buf->b);
buf->b = NULL;
}
void
die(const char *s)