From f204f701413738dac9c6babbe9c56b11cfe4521f Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 1 May 2023 21:13:49 -0400 Subject: [PATCH] implemented standard file io module and leap functionality in editor --- bin/editor/gapbuf.c | 29 ++++++++-------- bin/editor/io.h | 8 +++-- bin/editor/job.c | 6 ++-- bin/editor/leap.c | 1 - bin/editor/readfd.c | 13 -------- bin/editor/readfile.c | 22 ------------- bin/editor/telem.c | 6 ++-- bin/editor/writefd.c | 13 -------- bin/editor/x11.c | 2 -- bin/editor/xpty.c | 13 ++++---- bin/mbusd.c | 2 +- inc/liba.h | 21 ++++++++++-- lib/a/File.c | 77 +++++++++++++++++++++++++++++++++++++++++++ lib/a/Thread.c | 6 ++-- 14 files changed, 135 insertions(+), 84 deletions(-) delete mode 100644 bin/editor/readfd.c delete mode 100644 bin/editor/readfile.c delete mode 100644 bin/editor/writefd.c create mode 100644 lib/a/File.c diff --git a/bin/editor/gapbuf.c b/bin/editor/gapbuf.c index 076ea99..86df698 100644 --- a/bin/editor/gapbuf.c +++ b/bin/editor/gapbuf.c @@ -1,11 +1,13 @@ +#define _XOPEN_SOURCE 700 +#include + #include #include "dbc.h" -#include "utf.h" -#include "io.h" -#include +//#include "utf.h" +//#include #include -#include -#include +//#include +//#include #include #include @@ -135,16 +137,17 @@ void gapbuf_init(GapBuf* buf) long gapbuf_save(GapBuf* buf, char* path) { require(buf != NULL); - long fd; + File fd; long nwrite = 0; - if (path && (fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) >= 0) +// if (path && (fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) >= 0) + if (path && (fd = File_OpenForWrite(path)) >= 0) { - nwrite = writefd(fd, buf->bufstart, (buf->gapstart - buf->bufstart)); + nwrite = File_Write(fd, buf->bufstart, (buf->gapstart - buf->bufstart)); if (nwrite >= 0) { - nwrite = writefd(fd, buf->gapend, (buf->bufend - buf->gapend)); + nwrite = File_Write(fd, buf->gapend, (buf->bufend - buf->gapend)); } - close(fd); + File_Close(fd); } else { @@ -158,9 +161,9 @@ void gapbuf_load(GapBuf* buf, char* path) require(buf != NULL); require(path != NULL); /* load the contents from the file */ - int fd; + File fd; struct stat sb = {0}; - if (((fd = open(path, O_RDONLY, 0)) >= 0) && (fstat(fd, &sb) >= 0) && (sb.st_size > 0)) + if (((fd = File_OpenForRead(path)) >= 0) && (fstat((int)fd, &sb) >= 0) && (sb.st_size > 0)) { /* allocate the buffer in advance */ free(buf->bufstart); @@ -177,7 +180,7 @@ void gapbuf_load(GapBuf* buf, char* path) else { buf->gapstart += sb.st_size; - (void)readfd(fd, buf->bufstart, sb.st_size); + (void)File_Read(fd, buf->bufstart, sb.st_size); } } if (fd > 0) diff --git a/bin/editor/io.h b/bin/editor/io.h index d5fe389..860b7bb 100644 --- a/bin/editor/io.h +++ b/bin/editor/io.h @@ -3,7 +3,9 @@ Helper functions for reading and writing file descriptors as well as outputting telemetry data. */ void telem_send(char* fmt, ...); -long writefd(int fd, char* data, long towrite); -long readfd(int fd, char* data, long toread); -char* readfile(char* path); + +//long writefd(int fd, char* data, long towrite); +//long readfd(int fd, char* data, long toread); +//char* readfile(char* path); + char* abspath(char* path); diff --git a/bin/editor/job.c b/bin/editor/job.c index 1512732..84fc6dd 100644 --- a/bin/editor/job.c +++ b/bin/editor/job.c @@ -1,5 +1,7 @@ +#define _XOPEN_SOURCE 700 +#include + #include -#include "utf.h" #include "io.h" #include #include @@ -50,7 +52,7 @@ static void pipe_write(Job* job) struct PipeData* pipedata = job->data; char* chunk = pipedata->data + pipedata->nwrite; errno = 0; - long nwrite = write(job->fd, chunk, pipedata->ndata); + long nwrite = File_Write(job->fd, chunk, pipedata->ndata); if (nwrite >= 0) { pipedata->ndata -= nwrite; diff --git a/bin/editor/leap.c b/bin/editor/leap.c index 2ecf7cf..6105580 100644 --- a/bin/editor/leap.c +++ b/bin/editor/leap.c @@ -1,7 +1,6 @@ #include #include "utf.h" #include "tide.h" -//#include "x11.h" static void leap_process_keysym(LeapState_T* state, unsigned long keysym) { diff --git a/bin/editor/readfd.c b/bin/editor/readfd.c deleted file mode 100644 index 8223a38..0000000 --- a/bin/editor/readfd.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include "io.h" - -long readfd(int fd, char* data, long toread) -{ - long nread = 0; - while (toread && ((nread = read(fd, data, toread)) >= 0)) - { - data += nread; - toread -= nread; - } - return nread; -} diff --git a/bin/editor/readfile.c b/bin/editor/readfile.c deleted file mode 100644 index a690a4c..0000000 --- a/bin/editor/readfile.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include "io.h" -#include -#include - -char* readfile(char* path) -{ - int fd; - char* data = NULL; - struct stat sb = {0}; - if (((fd = open(path, O_RDONLY, 0)) >= 0) && (fstat(fd, &sb) >= 0) && (sb.st_size > 0)) - { - data = malloc(sb.st_size+1); - (void)readfd(fd, data, sb.st_size); - data[sb.st_size] = '\0'; - } - if (fd > 0) - { - close(fd); - } - return data; -} diff --git a/bin/editor/telem.c b/bin/editor/telem.c index 913688d..36c82e2 100644 --- a/bin/editor/telem.c +++ b/bin/editor/telem.c @@ -1,9 +1,11 @@ +#include + #include #include #include #include #include -#include "io.h" + static int TelemFd = -1; static char TelemBuf[16384]; @@ -85,7 +87,7 @@ void telem_send(char* fmt, ...) str = add_pid(str, &nleft); str = add_message(str, &nleft, fmt, args); va_end(args); - if (writefd(fd, TelemBuf, str - TelemBuf) < 0) + if (File_Write(fd, TelemBuf, str - TelemBuf) < 0) { close_telem(); } diff --git a/bin/editor/writefd.c b/bin/editor/writefd.c deleted file mode 100644 index c5c2eac..0000000 --- a/bin/editor/writefd.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include "io.h" - -long writefd(int fd, char* data, long towrite) -{ - long nwrite = 0; - while (towrite && ((nwrite = write(fd, data, towrite)) > 0)) - { - data += nwrite; - towrite -= nwrite; - } - return nwrite; -} diff --git a/bin/editor/x11.c b/bin/editor/x11.c index 73b56eb..c1172a5 100644 --- a/bin/editor/x11.c +++ b/bin/editor/x11.c @@ -184,8 +184,6 @@ KeySym x11_getkeysym(XConf* x, XEvent* e) char buf[32]; KeySym key = 0; XLookupString(&(e->xkey), buf, sizeof(buf), &key, 0); - printf("%s\n", buf); - return key; } diff --git a/bin/editor/xpty.c b/bin/editor/xpty.c index 5a791cd..9c146da 100644 --- a/bin/editor/xpty.c +++ b/bin/editor/xpty.c @@ -1,5 +1,6 @@ +#define _XOPEN_SOURCE 700 +#include #include -#include "utf.h" #include "io.h" #include #ifdef __linux__ @@ -115,7 +116,7 @@ static void putb(int byte) if ((tio.c_lflag & ICANON) == ICANON) { char b = byte; - write(Pty_Fd, &b, 1); + File_Write(Pty_Fd, &b, 1); if (byte == '\n') { *((char*)(EditView->buffer.contents.gapstart-1)) = ' '; @@ -127,7 +128,7 @@ static void putb(int byte) } else { - read(Pty_Fd, &b, 1); + File_Read(Pty_Fd, &b, 1); } } else if (byte == '\n' && (EditView->buffer.selection.end == EditView->buffer.point.end)) @@ -139,7 +140,7 @@ static void putb(int byte) /* write the data and read back to discard the echoed chars */ // printf("write: '%s'\n", str); - writefd(Pty_Fd, str, slen); + File_Write(Pty_Fd, str, slen); State = READ_ECHO; IgnoreCount = slen+1; InputBuf[(InputPos = 0)] = '\0'; @@ -166,7 +167,7 @@ static void writedata(char* buf, long n) static void xpty_read(Job* job) { - long nread = read(job->fd, ReadBuf, sizeof(ReadBuf)-1); + long nread = File_Read(job->fd, ReadBuf, sizeof(ReadBuf)-1); if (nread <= 0) { job->readfn = NULL; @@ -274,5 +275,5 @@ void xpty_send(char* cmd) void xpty_rawsend(char* str) { - (void)write(Pty_Fd, str, strlen(str)); + (void)File_Write(Pty_Fd, str, strlen(str)); } diff --git a/bin/mbusd.c b/bin/mbusd.c index 84d7369..641d528 100644 --- a/bin/mbusd.c +++ b/bin/mbusd.c @@ -78,7 +78,7 @@ size_t PollClients(void) return fd_count; } -Int ProcessMessages(void* arg) +int ProcessMessages(void* arg) { (void)arg; for (;;) diff --git a/inc/liba.h b/inc/liba.h index 8ac5e66..597d5d0 100644 --- a/inc/liba.h +++ b/inc/liba.h @@ -3,8 +3,8 @@ #include typedef unsigned char Byte; -typedef int Int; -typedef unsigned int Uint; +typedef long Int; +typedef unsigned long Uint; typedef double Real; typedef Int Rune; typedef _Bool Bool; @@ -71,6 +71,21 @@ void Options_PrintHelp(void); Int UTF8_Encode(Byte str[UTF_MAX], Rune rune); Bool UTF8_Decode(Rune* rune, Int* length, Int byte); +/* + File I/O +*/ +typedef Int File; +File File_OpenForRead(char* path); +File File_OpenForWrite(char* path); +File File_OpenForReadWrite(char* path); +File File_OpenForAppend(char* path); +Int File_Close(File f); +//FileStats File_Statistics(File); +Int File_Read(File f, char* buf, Uint bufsz); +Int File_Write(File f, char* buf, Uint bufsz); +char* File_ReadAll(char* path); + + /* Message Bus Interface */ @@ -121,7 +136,7 @@ void Net_Serve(char* dialstr, void (*on_client)(Int cfd)); typedef thrd_t Thread_T; -Int Thread_Create(Thread_T* thread, Int (*func)(void*), void *arg); +Int Thread_Create(Thread_T* thread, int (*func)(void*), void *arg); Thread_T Thread_Current(void); void Thread_Yield(void); _Noreturn void Thread_Exit(Int res); diff --git a/lib/a/File.c b/lib/a/File.c new file mode 100644 index 0000000..9fb4836 --- /dev/null +++ b/lib/a/File.c @@ -0,0 +1,77 @@ +#define _XOPEN_SOURCE 700 +#include +#include +#include +#include +#include + +File File_OpenForRead(char* path) +{ + return (File)open(path, O_RDONLY); +} + +File File_OpenForWrite(char* path) +{ + return (File)open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644); +} + +File File_OpenForReadWrite(char* path) +{ + return (File)open(path, O_RDWR|O_CREAT|O_TRUNC, 0644); +} + +File File_OpenForAppend(char* path) +{ + return (File)open(path, O_WRONLY|O_APPEND); +} + +Int File_Close(File f) +{ + return close((int)f); +} + +//FileStats File_Statistics(File) +//{ +//} + +Int File_Read(File f, char* data, Uint toread) +{ + long nread = 0; + while (toread && ((nread = read((int)f, data, toread)) >= 0)) + { + data += nread; + toread -= nread; + } + return nread; +} + +Int File_Write(File f, char* data, Uint towrite) +{ + long nwrite = 0; + while (towrite && ((nwrite = write(f, data, towrite)) > 0)) + { + data += nwrite; + towrite -= nwrite; + } + return nwrite; + +} + +char* File_ReadAll(char* path) +{ + char* data = NULL; + struct stat sb = {0}; + File f = File_OpenForRead(path); + if ((f >= 0) && (fstat(f, &sb) >= 0) && (sb.st_size > 0)) + { + data = malloc(sb.st_size+1); + (void)File_Read(f, data, sb.st_size); + data[sb.st_size] = '\0'; + } + if (f > 0) + { + File_Close(f); + } + return data; +} + diff --git a/lib/a/Thread.c b/lib/a/Thread.c index 6521313..ded7cbd 100644 --- a/lib/a/Thread.c +++ b/lib/a/Thread.c @@ -1,9 +1,9 @@ #include #include -Int Thread_Create(Thread_T* thread, Int (*func)(void*), void *arg) +Int Thread_Create(Thread_T* thread, int (*func)(void*), void *arg) { - return thrd_create(thread, func, arg); + return thrd_create(thread, (int (*)(void*))func, arg); } Thread_T Thread_Current(void) @@ -28,7 +28,7 @@ Int Thread_Detach(Thread_T thread) Int Thread_Join(Thread_T thread) { - Int res = 0; + int res = 0; Int success = thrd_join(thread, &res); return (success == thrd_success ? res : success); } -- 2.54.0