From: Michael D. Lowis Date: Mon, 24 Feb 2020 17:51:59 +0000 (-0500) Subject: added ability to read directories on file load. This replaces the hack for send ls... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=54ad50da80dbe031609410cb67960274d979eb4e;p=projs%2Ftide.git added ability to read directories on file load. This replaces the hack for send ls results to tide via standard input --- diff --git a/src/fetch.c b/src/fetch.c index 12285b7..3f09d0a 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -56,9 +56,9 @@ static Rule*** BuiltinRules = (Rule**[]){ &(Rule){ LAUNCH, "man $M2 \"$M1\" | col -b | tide -", NULL }, &(Rule){ COMPLETE, NULL, NULL } }, - (Rule*[]){ /* If it's an existing directory, open it with system default */ + (Rule*[]){ /* If it's an existing directory, open it tide */ &(Rule){ ISDIR, "$data", NULL }, - &(Rule){ LAUNCH, "cd $data && ls -ap | tide -", NULL }, + &(Rule){ LAUNCH, "edit \"$data\"", NULL }, &(Rule){ COMPLETE, NULL, NULL } }, (Rule*[]){ /* look for files in /usr/include */ diff --git a/src/lib/buf.c b/src/lib/buf.c index 252dfa0..3bae4fb 100644 --- a/src/lib/buf.c +++ b/src/lib/buf.c @@ -140,11 +140,6 @@ void buf_load(Buf* buf, char* path) require(buf != NULL); if (path) { - /* process the file path and address */ - if (path[0] == '.' && path[1] == '/') - { - path += 2; - } buf->path = strdup(path); gapbuf_load(&buf->contents, buf->path); diff --git a/src/lib/gapbuf.c b/src/lib/gapbuf.c index a5b915c..e66b0ff 100644 --- a/src/lib/gapbuf.c +++ b/src/lib/gapbuf.c @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include "config.h" size_t gapbuf_end(GapBuf* buf) @@ -80,6 +82,44 @@ static void syncgap(GapBuf* buf, size_t off) } } +static int cmpnames(const void* a, const void* b) +{ + char *sa = *((char**)a), *sb = *((char**)b); + return strcmp(sa, sb); +} + +static void loaddir(GapBuf* buf, char* path, int fd) +{ + Sel sel = { 0 }; + vec_t entries; + vec_init(&entries, sizeof(char*)); + DIR* dir = fdopendir(fd); + struct dirent* entry = NULL; + struct stat attrs = {0}; + while ( (entry = readdir(dir)) ) + { + char* epath = strmcat(path, "/", entry->d_name, 0); + memset(&attrs, 0, sizeof(attrs)); + stat(epath, &attrs); + char* ename = strmcat(entry->d_name, (S_ISDIR(attrs.st_mode) ? "/" : 0), 0); + vec_push_back(&entries, &ename); + free(epath); + } + vec_sort(&entries, cmpnames); + for (int i = 0; i < vec_size(&entries); i++) + { + char* name = *((char**)vec_at(&entries, i)); + for (int i = 0; name[i]; i++) + { + gapbuf_putb(buf, name[i], &sel); + } + gapbuf_putb(buf, '\n', &sel); + free(name); + } + free(entries.elem_buffer); + chdir(path); +} + void gapbuf_init(GapBuf* buf) { require(buf != NULL); @@ -125,9 +165,18 @@ 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 + sb.st_size; + buf->gapstart = buf->bufstart; buf->gapend = buf->bufend; - (void)readfd(fd, buf->bufstart, sb.st_size); + + if (S_ISDIR(sb.st_mode)) + { + loaddir(buf, path, fd); + } + else + { + buf->gapstart += sb.st_size; + (void)readfd(fd, buf->bufstart, sb.st_size); + } } if (fd > 0) {