]> git.mdlowis.com Git - proto/aos.git/commitdiff
implemented standard file io module and leap functionality in editor
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 2 May 2023 01:13:49 +0000 (21:13 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 2 May 2023 01:13:49 +0000 (21:13 -0400)
14 files changed:
bin/editor/gapbuf.c
bin/editor/io.h
bin/editor/job.c
bin/editor/leap.c
bin/editor/readfd.c [deleted file]
bin/editor/readfile.c [deleted file]
bin/editor/telem.c
bin/editor/writefd.c [deleted file]
bin/editor/x11.c
bin/editor/xpty.c
bin/mbusd.c
inc/liba.h
lib/a/File.c [new file with mode: 0644]
lib/a/Thread.c

index 076ea99263503537416e29471165bc416aaf3262..86df69801f82c94ce2167b41d3dc80d16283d154 100644 (file)
@@ -1,11 +1,13 @@
+#define _XOPEN_SOURCE 700
+#include <liba.h>
+
 #include <stdc.h>
 #include "dbc.h"
-#include "utf.h"
-#include "io.h"
-#include <ctype.h>
+//#include "utf.h"
+//#include <ctype.h>
 #include <unistd.h>
-#include <fcntl.h>
-#include <sys/stat.h>
+//#include <fcntl.h>
+//#include <sys/stat.h>
 #include <dirent.h>
 #include <vec.h>
 
@@ -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)
index d5fe389f2b5f61ef165f580c0035167a1c0f198a..860b7bbbcc8db2c4145264a607be11b1495480e3 100644 (file)
@@ -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);
index 151273298d9c24194c0c9e58121eba6675e74f05..84fc6dddae083917d736ce302f31228a8e9cb123 100644 (file)
@@ -1,5 +1,7 @@
+#define _XOPEN_SOURCE 700
+#include <liba.h>
+
 #include <stdc.h>
-#include "utf.h"
 #include "io.h"
 #include <unistd.h>
 #include <sys/wait.h>
@@ -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;
index 2ecf7cf9970249af9636fb17aa4ccdd6b79114ed..6105580ed6ad7da602ca0102befedffb12051319 100644 (file)
@@ -1,7 +1,6 @@
 #include <stdc.h>
 #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 (file)
index 8223a38..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-#include <stdc.h>
-#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 (file)
index a690a4c..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-#include <stdc.h>
-#include "io.h"
-#include <fcntl.h>
-#include <sys/stat.h>
-
-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;
-}
index 913688d4408189d2efd98cff8578733b966b0a7e..36c82e29fc93542fcf2c9409e45b0962b68826c4 100644 (file)
@@ -1,9 +1,11 @@
+#include <liba.h>
+
 #include <stdc.h>
 #include <time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <pwd.h>
-#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 (file)
index c5c2eac..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-#include <stdc.h>
-#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;
-}
index 73b56eb7420ed26d88a92d78933d385130bb9e25..c1172a5b244d8042004478d3ce3cf5203c84dfcd 100644 (file)
@@ -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;
 }
 
index 5a791cd480610a4d390cbe37025909bd04ace355..9c146da9ed1843473747da067d7a10b2ef920617 100644 (file)
@@ -1,5 +1,6 @@
+#define _XOPEN_SOURCE 700
+#include <liba.h>
 #include <stdc.h>
-#include "utf.h"
 #include "io.h"
 #include <sys/socket.h>
 #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));
 }
index 84d7369df018cbb9a09c130b41a04d331a62508e..641d528e39c1457c0608bb5e3b8d3805b2d937cf 100644 (file)
@@ -78,7 +78,7 @@ size_t PollClients(void)
     return fd_count;
 }
 
-Int ProcessMessages(void* arg)
+int ProcessMessages(void* arg)
 {
     (void)arg;
     for (;;)
index 8ac5e66e63909784a353963132dc8e59978495f6..597d5d0c0922b77f3675c977b9b7b4f6478bca38 100644 (file)
@@ -3,8 +3,8 @@
 #include <errno.h>
 
 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 (file)
index 0000000..9fb4836
--- /dev/null
@@ -0,0 +1,77 @@
+#define _XOPEN_SOURCE 700
+#include <liba.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+
+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;
+}
+
index 6521313e98812562110823422bc71cfc66694d90..ded7cbdd79707e2f3b64278621d612f1783483bc 100644 (file)
@@ -1,9 +1,9 @@
 #include <liba.h>
 #include <threads.h>
 
-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);
 }