gophra

gopher client for plan9
git clone git://nsmpr.xyz/gophra.git
Log | Files | Refs

commit 73d9810f8c1ceb9aef46cc5b7fe0dc0f69dbeb8f
parent 281782c21fb8490e654355f063269928b1962691
Author: glenda <glenda@9front.local>
Date:   Sun,  7 Jun 2020 18:59:38 +0000

move threadload to load.{c,h}

Diffstat:
Mgophra.c | 87++++++++++++++-----------------------------------------------------------------
Aload.c | 44++++++++++++++++++++++++++++++++++++++++++++
Aload.h | 16++++++++++++++++
Mmkfile | 2+-
4 files changed, 76 insertions(+), 73 deletions(-)

diff --git a/gophra.c b/gophra.c @@ -8,6 +8,7 @@ #include <frame.h> #include "uri.h" +#include "load.h" enum { DBorder = DBlue, @@ -17,12 +18,6 @@ enum { DStatus = DYellow, }; -typedef struct Text Text; -struct Text { - char *data; - long size; -}; - typedef struct History History; struct History { char addr[255]; @@ -30,14 +25,6 @@ struct History { History *prev; }; -typedef struct Loadctl Loadctl; -struct Loadctl { - char *addr; - char *path; - char *query; - Channel *c; -}; - Channel *lc; History hzero, *hp; Image *Iborder, *Iwbg, *Iwfg, *Iwfgl, *Istatus; @@ -59,10 +46,8 @@ void drawstatus(void); void drawtext(void); void handlelink(char*, char, char*); void loadtext(char*, char*); -void resize(void); void runhold(void*); void runpage(void*); -void threadload(void*); void usage(void); void @@ -113,7 +98,6 @@ threadmain(int argc, char **argv) flushimage(display, 1); if (argc == 1) { - strncpy(hzero.addr, argv[0], 256); runuri(argv[0]); } else if (argc > 0) { usage(); @@ -168,6 +152,7 @@ threadmain(int argc, char **argv) sline = (mv.xy.y - rw.min.y) / font->height + scroll; s = getline(&text, sline); + if (s == nil) break; extracturl(s); snprint(status, 255, "%s/%c%s", addr, type, path); drawstatus(); @@ -189,7 +174,12 @@ threadmain(int argc, char **argv) } break; case 2: /* resize */ - resize(); + if(getwindow(display, Refnone) < 0) sysfatal("resize failed: %r"); + calcrects(); + drawborder(); + drawstatus(); + drawmenu(); + flushimage(display, 1); break; case 3: /* loading */ realloc(text.data, 0); @@ -203,45 +193,6 @@ threadmain(int argc, char **argv) } void -threadload(void *v) -{ - int gfd; - Text text; - long n; - char buf[1024]; - - Loadctl *lctl; - lctl = v; - - // set status to "dialing $addr..." - gfd = dial(lctl->addr, 0, 0, 0); - if (gfd == -1) { - // set status to "failed to dial $addr" - // send nill - return; - } - write(gfd, lctl->path, strlen(path)); - write(gfd, "\n", 1); - // if there're query, send query - text.data = nil; - text.size = 0; - while ((n = read(gfd, buf, 1024)) > 0){ - text.data = realloc(text.data, text.size + n); - memcpy(text.data + text.size, buf, n); - text.size += n; - // set status to "loading - $n bytes" - // border animation - } - // set status to done - send(lctl->c, &text); - - realloc(lctl->path, 0); - realloc(lctl->addr, 0); - realloc(lctl->query, 0); - -} - -void usage(void) { fprint(2, "usage: %s [gopher_uri]\n", argv0); @@ -249,17 +200,6 @@ usage(void) } void -resize(void) -{ - if(getwindow(display, Refnone) < 0) sysfatal("resize failed: %r"); - calcrects(); - drawborder(); - drawstatus(); - drawmenu(); - flushimage(display, 1); -} - -void back(void) { History *oldh; @@ -380,15 +320,16 @@ getline(Text *text, long ln) char *lbuf; char *lstart, *lend; long lcount; + if (text->data == nil) return nil; lstart = text->data; for (lcount = 0; lcount < ln; lstart++) { if (*lstart == '\n') lcount++; - if (lstart > text->data + text->size) return 0; + if (lstart > text->data + text->size) return nil; } lend = lstart; while (*lend != '\n') { lend++; - if (lend > text->data + text->size) return 0; + if (lend > text->data + text->size) return nil; } lbuf = mallocz(sizeof(char) * (lend - lstart + 1), 1); memcpy(lbuf, lstart, lend - lstart); @@ -456,7 +397,6 @@ loadtext(char *addr, char *path) return; } - void runhold(void*) { @@ -469,7 +409,6 @@ runpage(void*) procexecl(nil, "/bin/window", "window", "-m", "page", tmpfile, nil); } - int save(char *addr, char *path) { @@ -515,6 +454,8 @@ runuri(char *s) uri = chewuri(s); if (uri == nil) { snprint(status, 255, "invalid URI: %s", s); + drawstatus(); + flushimage(display, 1); return -1; } if (strcmp("gopher", uri->scheme) == 0){ @@ -527,6 +468,8 @@ runuri(char *s) handlelink(addr, type, path); } else { snprint(status, 255, "Unknown scheme: %s", uri->scheme); + drawstatus(); + flushimage(display, 1); freeuri(uri); free(uri); return -1; diff --git a/load.c b/load.c @@ -0,0 +1,44 @@ +#include <u.h> +#include <libc.h> +#include <thread.h> + +#include "load.h" + +void +threadload(void *v) +{ + int gfd; + Text text; + long n; + char buf[1024]; + + Loadctl *lctl; + lctl = v; + + // set status to "dialing $addr..." + gfd = dial(lctl->addr, 0, 0, 0); + if (gfd == -1) { + // set status to "failed to dial $addr" + // send nill + return; + } + write(gfd, lctl->path, strlen(lctl->path)); + write(gfd, "\n", 1); + // if there're query, send query + text.data = nil; + text.size = 0; + while ((n = read(gfd, buf, 1024)) > 0){ + text.data = realloc(text.data, text.size + n); + memcpy(text.data + text.size, buf, n); + text.size += n; + // set status to "loading - $n bytes" + // border animation + } + // set status to done + send(lctl->c, &text); + + realloc(lctl->path, 0); + realloc(lctl->addr, 0); + realloc(lctl->query, 0); + +} diff --git a/load.h b/load.h @@ -0,0 +1,16 @@ + +typedef struct Text Text; +struct Text { + long size; + char *data; +}; + +typedef struct Loadctl Loadctl; +struct Loadctl { + char *addr; + char *path; + char *query; + Channel *c; +}; + +void threadload(void*); diff --git a/mkfile b/mkfile @@ -5,6 +5,6 @@ BIN=$home/bin/$objtype TARG=gophra -OFILES=gophra.$O uri.$O +OFILES=gophra.$O uri.$O load.$O </sys/src/cmd/mkone \ No newline at end of file