Skip to content

Commit

Permalink
Initial commit of block device interface and emulated block device
Browse files Browse the repository at this point in the history
  • Loading branch information
geky committed Feb 25, 2017
1 parent b113bba commit 02156cb
Show file tree
Hide file tree
Showing 7 changed files with 471 additions and 43 deletions.
49 changes: 49 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
TARGET = lfs

CC = gcc
AR = ar
SIZE = size

SRC += $(wildcard *.c emubd/*.c)
OBJ := $(SRC:.c=.o)
DEP := $(SRC:.c=.d)
ASM := $(SRC:.c=.s)

ifdef DEBUG
CFLAGS += -O0 -g3
else
CFLAGS += -O2
endif
ifdef WORD
CFLAGS += -m$(WORD)
endif
CFLAGS += -I.
CFLAGS += -std=c99 -Wall -pedantic


all: $(TARGET)

asm: $(ASM)

size: $(OBJ)
$(SIZE) -t $^

-include $(DEP)

$(TARGET): $(OBJ)
$(CC) $(CFLAGS) $^ $(LFLAGS) -o $@

%.a: $(OBJ)
$(AR) rcs $@ $^

%.o: %.c
$(CC) -c -MMD $(CFLAGS) $< -o $@

%.s: %.c
$(CC) -S $(CFLAGS) $< -o $@

clean:
rm -f $(TARGET)
rm -f $(OBJ)
rm -f $(DEP)
rm -f $(ASM)
66 changes: 36 additions & 30 deletions emu/cfg.c → emubd/lfs_cfg.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#include "cfg.h"
/*
* Simple config parser
*
* Copyright (c) 2017 Christopher Haster
* Distributed under the MIT license
*/
#include "emubd/lfs_cfg.h"

#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>


static int cfg_buffer(cfg_t *cfg, char c) {
static int lfs_cfg_buffer(lfs_cfg_t *cfg, char c) {
// Amortize double
if (cfg->blen == cfg->bsize) {
size_t nsize = cfg->bsize * 2;
Expand All @@ -26,16 +32,16 @@ static int cfg_buffer(cfg_t *cfg, char c) {
return 0;
}

static int cfg_attr(cfg_t *cfg, unsigned key, unsigned val) {
static int lfs_cfg_attr(lfs_cfg_t *cfg, unsigned key, unsigned val) {
// Amortize double
if (cfg->len == cfg->size) {
size_t nsize = cfg->size * 2;
struct cfg_attr *nattrs = malloc(nsize*sizeof(struct cfg_attr));
struct lfs_cfg_attr *nattrs = malloc(nsize*sizeof(struct lfs_cfg_attr));
if (!nattrs) {
return -ENOMEM;
}

memcpy(nattrs, cfg->attrs, cfg->size*sizeof(struct cfg_attr));
memcpy(nattrs, cfg->attrs, cfg->size*sizeof(struct lfs_cfg_attr));
free(cfg->attrs);
cfg->attrs = nattrs;
cfg->size = nsize;
Expand All @@ -50,14 +56,14 @@ static int cfg_attr(cfg_t *cfg, unsigned key, unsigned val) {
}

memmove(&cfg->attrs[i+1], &cfg->attrs[i],
(cfg->size - i)*sizeof(struct cfg_attr));
(cfg->size - i)*sizeof(struct lfs_cfg_attr));
cfg->attrs[i].key = key;
cfg->attrs[i].val = val;
cfg->len += 1;
return 0;
}

static bool cfg_match(FILE *f, const char *matches) {
static bool lfs_cfg_match(FILE *f, const char *matches) {
char c = getc(f);
ungetc(c, f);

Expand All @@ -70,11 +76,11 @@ static bool cfg_match(FILE *f, const char *matches) {
return false;
}

int cfg_create(cfg_t *cfg, const char *filename) {
int lfs_cfg_create(lfs_cfg_t *cfg, const char *filename) {
// start with some initial space
cfg->len = 0;
cfg->size = 4;
cfg->attrs = malloc(cfg->size*sizeof(struct cfg_attr));
cfg->attrs = malloc(cfg->size*sizeof(struct lfs_cfg_attr));

cfg->blen = 0;
cfg->bsize = 16;
Expand All @@ -88,50 +94,50 @@ int cfg_create(cfg_t *cfg, const char *filename) {
while (!feof(f)) {
int err;

while (cfg_match(f, " \t\v\f")) {
while (lfs_cfg_match(f, " \t\v\f")) {
fgetc(f);
}

if (!cfg_match(f, "#\r\n")) {
if (!lfs_cfg_match(f, "#\r\n")) {
unsigned key = cfg->blen;
while (!cfg_match(f, " \t\v\f:#") && !feof(f)) {
if ((err = cfg_buffer(cfg, fgetc(f)))) {
while (!lfs_cfg_match(f, " \t\v\f:#") && !feof(f)) {
if ((err = lfs_cfg_buffer(cfg, fgetc(f)))) {
return err;
}
}
if ((err = cfg_buffer(cfg, 0))) {
if ((err = lfs_cfg_buffer(cfg, 0))) {
return err;
}

while (cfg_match(f, " \t\v\f")) {
while (lfs_cfg_match(f, " \t\v\f")) {
fgetc(f);
}

if (cfg_match(f, ":")) {
if (lfs_cfg_match(f, ":")) {
fgetc(f);
while (cfg_match(f, " \t\v\f")) {
while (lfs_cfg_match(f, " \t\v\f")) {
fgetc(f);
}

unsigned val = cfg->blen;
while (!cfg_match(f, " \t\v\f#\r\n") && !feof(f)) {
if ((err = cfg_buffer(cfg, fgetc(f)))) {
while (!lfs_cfg_match(f, " \t\v\f#\r\n") && !feof(f)) {
if ((err = lfs_cfg_buffer(cfg, fgetc(f)))) {
return err;
}
}
if ((err = cfg_buffer(cfg, 0))) {
if ((err = lfs_cfg_buffer(cfg, 0))) {
return err;
}

if ((err = cfg_attr(cfg, key, val))) {
if ((err = lfs_cfg_attr(cfg, key, val))) {
return err;
}
} else {
cfg->blen = key;
}
}

while (!cfg_match(f, "\r\n") && !feof(f)) {
while (!lfs_cfg_match(f, "\r\n") && !feof(f)) {
fgetc(f);
}
fgetc(f);
Expand All @@ -140,15 +146,15 @@ int cfg_create(cfg_t *cfg, const char *filename) {
return 0;
}

void cfg_destroy(cfg_t *cfg) {
void lfs_cfg_destroy(lfs_cfg_t *cfg) {
free(cfg->attrs);
}

bool cfg_has(cfg_t *cfg, const char *key) {
return cfg_get(cfg, key, 0);
bool lfs_cfg_has(lfs_cfg_t *cfg, const char *key) {
return lfs_cfg_get(cfg, key, 0);
}

const char *cfg_get(cfg_t *cfg, const char *key, const char *def) {
const char *lfs_cfg_get(lfs_cfg_t *cfg, const char *key, const char *def) {
// binary search for attribute
int lo = 0;
int hi = cfg->len-1;
Expand All @@ -168,8 +174,8 @@ const char *cfg_get(cfg_t *cfg, const char *key, const char *def) {
return def;
}

ssize_t cfg_geti(cfg_t *cfg, const char *key, ssize_t def) {
const char *val = cfg_get(cfg, key, 0);
ssize_t lfs_cfg_geti(lfs_cfg_t *cfg, const char *key, ssize_t def) {
const char *val = lfs_cfg_get(cfg, key, 0);
if (!val) {
return def;
}
Expand All @@ -179,8 +185,8 @@ ssize_t cfg_geti(cfg_t *cfg, const char *key, ssize_t def) {
return (end == val) ? def : res;
}

size_t cfg_getu(cfg_t *cfg, const char *key, size_t def) {
const char *val = cfg_get(cfg, key, 0);
size_t lfs_cfg_getu(lfs_cfg_t *cfg, const char *key, size_t def) {
const char *val = lfs_cfg_get(cfg, key, 0);
if (!val) {
return def;
}
Expand Down
26 changes: 13 additions & 13 deletions emu/cfg.h → emubd/lfs_cfg.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Simple config parser
*
* Copyright (c) 2016 Christopher Haster
* Copyright (c) 2017 Christopher Haster
* Distributed under the MIT license
*/
#ifndef CFG_H
#define CFG_H
#ifndef LFS_CFG_H
#define LFS_CFG_H

#include <stddef.h>
#include <stdbool.h>
Expand All @@ -26,49 +26,49 @@
// huh: yeah_that's_basically_it # basically it

// Internal config structure
typedef struct cfg {
typedef struct lfs_cfg {
size_t len;
size_t size;

size_t blen;
size_t bsize;
char *buf;

struct cfg_attr {
struct lfs_cfg_attr {
unsigned key;
unsigned val;
} *attrs;
} cfg_t;
} lfs_cfg_t;



// Creates a cfg object and reads in the cfg file from the filename
//
// If the cfg_read fails, returns a negative value from the underlying
// If the lfs_cfg_read fails, returns a negative value from the underlying
// stdio functions
int cfg_create(cfg_t *cfg, const char *filename);
int lfs_cfg_create(lfs_cfg_t *cfg, const char *filename);

// Destroys the cfg object and frees any used memory
void cfg_destroy(cfg_t *cfg);
void lfs_cfg_destroy(lfs_cfg_t *cfg);

// Checks if a cfg attribute exists
bool cfg_has(cfg_t *cfg, const char *key);
bool lfs_cfg_has(lfs_cfg_t *cfg, const char *key);

// Retrieves a cfg attribute as a null-terminated string
//
// If the attribute does not exist, returns the string passed as def
const char *cfg_get(cfg_t *cfg, const char *key, const char *def);
const char *lfs_cfg_get(lfs_cfg_t *cfg, const char *key, const char *def);

// Retrieves a cfg attribute parsed as an int
//
// If the attribute does not exist or can't be parsed, returns the
// integer passed as def
ssize_t cfg_geti(cfg_t *cfg, const char *name, ssize_t def);
ssize_t lfs_cfg_geti(lfs_cfg_t *cfg, const char *name, ssize_t def);

// Retrieves a cfg attribute parsed as an unsigned int
//
// If the attribute does not exist or can't be parsed, returns the
// integer passed as def
size_t cfg_getu(cfg_t *cfg, const char *name, size_t def);
size_t lfs_cfg_getu(lfs_cfg_t *cfg, const char *name, size_t def);

#endif
Loading

0 comments on commit 02156cb

Please sign in to comment.