Skip to content

Commit

Permalink
OpenBSD port work
Browse files Browse the repository at this point in the history
  • Loading branch information
abbec committed May 3, 2024
1 parent 5ac71ee commit 3b533d3
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 15 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Makefile for bmake
# Makefile for bmake/openbsd make

default: dged

.PHONY: default clean check run debug debug-tests install format
.OBJDIR: ./build
SYNTAX_ENABLE ?= true

default: dged

build:
mkdir -p build

Expand Down
10 changes: 7 additions & 3 deletions src/dged/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,17 @@ struct display *display_create() {

// save old settings
struct termios orig_term;
tcgetattr(0, &orig_term);
if (tcgetattr(0, &orig_term) < 0) {
return NULL;
}

// set terminal to raw mode
struct termios term = {0};
struct termios term = orig_term;
cfmakeraw(&term);

tcsetattr(0, TCSADRAIN, &term);
if (tcsetattr(0, TCSADRAIN, &term) < 0) {
return NULL;
}

struct display *d = calloc(1, sizeof(struct display));
d->orig_term = orig_term;
Expand Down
109 changes: 102 additions & 7 deletions src/dged/reactor-kqueue.c
Original file line number Diff line number Diff line change
@@ -1,45 +1,140 @@
#include "reactor.h"

#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/event.h>
#include <sys/types.h>
#include <unistd.h>

#include "minibuffer.h"

struct reactor {
int queue;
struct kevent events[10];
uint32_t nevents;

int file_queue;
};

struct reactor *reactor_create() {
struct reactor *reactor = calloc(1, sizeof(struct reactor));
int queue = kqueue();
if (queue < 0) {
return NULL;
}

int file_queue = kqueue();
if (file_queue < 0) {
return NULL;
}

struct reactor *reactor = calloc(1, sizeof(struct reactor));
reactor->queue = queue;
reactor->file_queue = file_queue;
reactor->nevents = 0;
return reactor;
}

void reactor_destroy(struct reactor *reactor) {
close(reactor->queue);
reactor->queue = -1;
free(reactor);
}

void reactor_update(struct reactor *reactor) {
int events = kevent(reactor->queue, NULL, 0, reactor->events, 10, NULL);
if (events == -1) {
// TODO: what to do here?
return;
}

reactor->nevents = events;
}

bool reactor_poll_event(struct reactor *reactor, uint32_t ev_id) {
for (uint32_t ei = 0; ei < reactor->nevents; ++ei) {
struct kevent *ev = &reactor->events[ei];

if (ev->ident == ev_id) {
return true;
}
}

return false;
}

uint32_t reactor_register_interest(struct reactor *reactor, int fd, enum interest interest) {
return -1;
uint32_t reactor_register_interest(struct reactor *reactor, int fd,
enum interest interest) {
struct kevent changes[2] = {0};
uint32_t nchanges = 0;

if ((interest & ReadInterest) != 0) {
EV_SET(&changes[0], fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
++nchanges;
}

if ((interest & WriteInterest) != 0) {
EV_SET(&changes[1], fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
++nchanges;
}

if (kevent(reactor->queue, changes, nchanges, NULL, 0, NULL) < 0) {
return -1;
}

return fd;
}

uint32_t reactor_watch_file(struct reactor *reactor, const char *path, uint32_t mask) {
return -1;
uint32_t reactor_watch_file(struct reactor *reactor, const char *path,
uint32_t mask) {

uint32_t fflags = NOTE_WRITE | NOTE_DELETE | NOTE_RENAME | NOTE_REVOKE;
int fd = open(path, O_RDONLY);
if (fd < 0) {
minibuffer_echo_timeout(4, "failed to watch %s: %s", path, strerror(errno));
return 0;
}

struct kevent new_event;
EV_SET(&new_event, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR | EV_ENABLE, fflags, 0,
NULL);
if (kevent(reactor->file_queue, &new_event, 1, NULL, 0, NULL) < 0) {
return 0;
}

return fd;
}

void reactor_unwatch_file(struct reactor *reactor, uint32_t id) {

// all kevents for this fd are removed automatically when closed
close(id);
}

bool reactor_next_file_event(struct reactor *reactor, struct file_event *out) {
return false;
struct kevent ev;
struct timespec zt = {0};
int nevents = kevent(reactor->file_queue, NULL, 0, &ev, 1, &zt);

// TODO: handle the error here (nevents < 0)
if (nevents <= 0) {
return false;
}

out->mask = FileWritten;
if ((ev->fflags & NOTE_DELETE) || (ev->fflags & NOTE_RENAME) ||
(ev->fflags & NOTE_REVOKE)) {
out->mask |= LastEvent;
}

out->id = ev->ident;
return true;
}

void reactor_unregister_interest(struct reactor *reactor, uint32_t ev_id) {
struct kevent changes[2] = {0};
EV_SET(&changes[0], ev_id, EVFILT_READ, EV_DELETE, 0, 0, NULL);
EV_SET(&changes[1], ev_id, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);

kevent(reactor->queue, changes, 2, NULL, 0, NULL);
}
4 changes: 2 additions & 2 deletions src/dged/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ void text_delete(struct text *text, uint32_t start_line, uint32_t start_col,
// in this case we can "overwrite"
uint32_t dstbytei =
utf8_nbytes(firstline->data, firstline->nbytes, start_col);
memcpy(firstline->data + dstbytei, lastline->data + bytei,
lastline->nbytes - bytei);
memmove(firstline->data + dstbytei, lastline->data + bytei,
lastline->nbytes - bytei);
} else {
// otherwise we actually have to copy from the last line
insert_at(text, start_line, start_col, lastline->data + bytei,
Expand Down
6 changes: 6 additions & 0 deletions src/main/main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <errno.h>
#include <getopt.h>
#include <locale.h>
#include <signal.h>
Expand Down Expand Up @@ -220,6 +221,11 @@ int main(int argc, char *argv[]) {
struct reactor *reactor = reactor_create();

display = display_create();
if (display == NULL) {
fprintf(stderr, "Failed to set up display: %s\n", strerror(errno));
return 9;
}

display_clear(display);
signal(SIGWINCH, resized);

Expand Down

0 comments on commit 3b533d3

Please sign in to comment.