]> git.mdlowis.com Git - projs/tide.git/commitdiff
refactored fetch to have telemetry and added read functions
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 1 Nov 2019 02:24:07 +0000 (22:24 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 1 Nov 2019 02:24:07 +0000 (22:24 -0400)
inc/io.h
src/fetch.c
src/lib/gapbuf.c
src/lib/readfd.c [new file with mode: 0644]
src/lib/readfile.c [new file with mode: 0644]

index 82875e1b44d4b973ff2838eba125f42aeaca0a7a..46e631fab6ec394a2320be20b32badb31db514f7 100644 (file)
--- a/inc/io.h
+++ b/inc/io.h
@@ -3,3 +3,5 @@
 */
 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);
index 15676aaed979cb0a849b2f641248ec52ae52c6d6..6bb2c7af8c2930dc6ce3b2afb400d919fd5ca3f4 100644 (file)
@@ -13,7 +13,6 @@ typedef struct {
     char* arg2;
 } Rule;
 
-char* ARGV0;
 char* Matches[10];
 
 #define FETCH_RULES
@@ -152,13 +151,13 @@ bool exec(char* cmd)
     {
         if (pid == 0)
         {
-        runcmd(cmd);
+            runcmd(cmd);
         }
         else
         {
-        waitpid(pid, &status, 0);
+            waitpid(pid, &status, 0);
             ret = (status == 0);
-    }
+        }
     }
     return ret;
 }
@@ -228,32 +227,29 @@ char* type2str(int type)
 
 int main(int argc, char** argv)
 {
-    int debug = 0;
-    OPTBEGIN { case 'd': debug = 1; } OPTEND;
-
+    ARGV0 = *argv;
     if (!argc) usage(ARGV0);
     setenv("data", *argv, 1);
 
-    if (debug)
-        printf("Data: '%s'\n", *argv);
+    telem_send("FETCH(%s)\n", *argv);
     for (unsigned int i = 0; i < nelem(BuiltinRules); i++)
     {
         Rule* rule = BuiltinRules[i];
-        if (debug)
-            printf("\nRuleset %d:\n", i);
+        telem_send("RULESET(%d)\n", i);
+
         for (; rule->type != COMPLETE; rule++)
         {
-            if (debug)
-                printf("    %s '%s' '%s'\n", type2str(rule->type), rule->arg1, rule->arg2);
+            telem_send("  RULE(%s '%s' '%s')\n", type2str(rule->type), rule->arg1, rule->arg2);
             if (!apply_rule(rule))
             {
-                if (debug)
-                    printf("    FAIL\n");
                 break;
             }
         }
         if (rule->type == COMPLETE)
+        {
+            telem_send("  ACCEPT\n");
             exit(0);
+        }
     }
     return 1;
 }
index d1d046bfb8e7cec62d75c366f50b00ac60f7cb05..a5b915cce63344014c4f7114b637dac7baa54118 100644 (file)
@@ -116,7 +116,7 @@ void gapbuf_load(GapBuf* buf, char* path)
     require(buf != NULL);
     require(path != NULL);
     /* load the contents from the file */
-    int fd, nread;
+    int fd;
     struct stat sb = {0};
     if (((fd = open(path, O_RDONLY, 0)) >= 0) && (fstat(fd, &sb) >= 0) && (sb.st_size > 0))
     {
@@ -125,12 +125,9 @@ void gapbuf_load(GapBuf* buf, char* path)
         buf->bufsize  = pagealign(sb.st_size);
         buf->bufstart = malloc(buf->bufsize);
         buf->bufend   = buf->bufstart + buf->bufsize;
-        buf->gapstart = buf->bufstart;
+        buf->gapstart = buf->bufstart + sb.st_size;
         buf->gapend   = buf->bufend;
-
-        /* Read the file into the buffer */
-        while (sb.st_size && (nread = read(fd, buf->gapstart, sb.st_size)) > 0)
-            buf->gapstart += nread, sb.st_size -= nread;
+        (void)readfd(fd, buf->bufstart, sb.st_size);
     }
     if (fd > 0)
     {
diff --git a/src/lib/readfd.c b/src/lib/readfd.c
new file mode 100644 (file)
index 0000000..5e4db43
--- /dev/null
@@ -0,0 +1,12 @@
+#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/src/lib/readfile.c b/src/lib/readfile.c
new file mode 100644 (file)
index 0000000..4534f95
--- /dev/null
@@ -0,0 +1,21 @@
+#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;
+}