ref: eeb8ba921c8be60f7f29caba428e89e65ec66737
parent: 41fb4360a4d6ca93f63e9d98c6c12fef463da090
author: Julien Blanchard <[email protected]>
date: Wed Dec 9 12:10:28 EST 2020
Plumb and page stuff
--- a/castor.c
+++ b/castor.c
@@ -88,7 +88,7 @@
}
void
-plumburl(char *u)
+plumburl(Url *u)
{
int fd;
@@ -95,10 +95,36 @@
fd = plumbopen("send", OWRITE|OCEXEC);
if(fd<0)
return;
- plumbsendtext(fd, "gopher", nil, nil, u);
+ plumbsendtext(fd, "castor9", nil, nil, u->raw);
close(fd);
+ freeurl(u);
}
+void
+page(Url *u)
+{
+ int fd;
+ char tmp[32] = "/tmp/castor9XXXXXXXXXXX", *cmd;
+
+ fd = request(u);
+ if(fd < 0)
+ sysfatal("dial: %r");
+
+ fprint(fd, "%s\r\n", u->raw);
+
+ switch(rfork(RFFDG|RFPROC|RFMEM|RFREND|RFNOWAIT|RFNOTEG)){
+ case -1:
+ fprint(2, "Can't fork!");
+ break;
+ case 0:
+ mktemp(tmp);
+ cmd = smprint("tail -n +2 >%s >[2]/dev/null; page -w %s; rm %s", tmp, tmp, tmp);
+ dup(fd, 0);
+ close(fd);
+ execl("/bin/rc", "rc", "-c", cmd, nil);
+ }
+}
+
char *
protocol(char *link)
{
@@ -110,8 +136,6 @@
return " [GOPHER]";
} else if(strstr(link, "finger://") != nil) {
return " [FINGER]";
- } else if(strstr(link, "mailto:") != nil) {
- return " [MAIL]";
} else {
return "";
}
@@ -138,12 +162,12 @@
{
int code;
char *meta;
- code = atoi(strtok(status, " "));
+ code = atoi(strtok(status, " \t"));
if(code == 0)
message("STATUS: %s\n", status);
meta = strtok(NULL, "\n");
r->status = code;
- r->prompt = cleanup(meta);
+ r->meta = cleanup(meta);
}
void
@@ -241,36 +265,14 @@
return urlparse(nil, base_url);
}
-void
-gemini_get(Url *url)
+int
+request(Url *url)
{
Thumbprint *th;
TLSconn conn;
int fd;
- char *line, *port;
- Biobuf body;
+ char *port;
- Ctx *c;
- c = malloc(sizeof *c);
- if(c==nil)
- sysfatal("malloc: %r");
- c->text = nil;
-
- Response *r;
- r = malloc(sizeof *r);
- if(r == nil)
- sysfatal("malloc: %r");
- r->url = url;
-
- Hist *h;
- h = malloc(sizeof *h);
- if(h == nil)
- sysfatal("malloc: %r");
-
- plrtstr(&c->text, 1000000, 0, 0, font, strdup(" "), 0, 0);
-
- message("loading %s...", url->raw);
-
if(url->port == NULL){
port = "1965";
}else{
@@ -280,7 +282,7 @@
fd = dial(naddr, 0, 0, 0);
if(fd < 0){
message("unable to connect to %s:%s: %r", url->host, url->port);
- return;
+ return -1;
}
conn.serverName = url->host;
@@ -295,6 +297,38 @@
free(conn.cert);
}
+ return fd;
+}
+
+void
+gemini_get(Url *url)
+{
+ int fd;
+ char *line;
+ Biobuf body;
+
+ Ctx *c;
+ c = malloc(sizeof *c);
+ if(c==nil)
+ sysfatal("malloc: %r");
+ c->text = nil;
+
+ Response *r;
+ r = malloc(sizeof *r);
+ if(r == nil)
+ sysfatal("malloc: %r");
+ r->url = url;
+
+ Hist *h;
+ h = malloc(sizeof *h);
+ if(h == nil)
+ sysfatal("malloc: %r");
+
+ plrtstr(&c->text, 1000000, 0, 0, font, strdup(" "), 0, 0);
+
+ message("loading %s...", url->raw);
+
+ fd = request(url);
fprint(fd, "%s\r\n", url->raw);
Binit(&body, fd, OREAD);
@@ -304,30 +338,38 @@
if(r->status == 20){
c->url = url;
set_current_base_url(base_url(url));
-
- while((line = Brdstr(&body, '\n', 0)) != nil)
+
+ if(r->meta != NULL && strncmp(r->meta, "text/", strlen("text/")) != 0)
{
- if (strstr(line, "=>") == NULL) {
- /* Not a link so wrapping text */
- render_text(c, line);
- } else {
- /* a link */
- render_link(c, line);
+ Bflush(&body);
+ close(fd);
+
+ page(url);
+ message("Castor9");
+ }else{
+ while((line = Brdstr(&body, '\n', 0)) != nil)
+ {
+ if(strncmp(line, "=>", strlen("=>")) == 0)
+ {
+ render_link(c, line);
+ }else{
+ render_text(c, line);
+ }
+ free(line);
}
- free(line);
- }
- Bflush(&body);
- close(fd);
+ Bflush(&body);
+ close(fd);
- h->p = hist;
- h->n = nil;
- h->c = c;
- hist = h;
+ h->p = hist;
+ h->n = nil;
+ h->c = c;
+ hist = h;
- show(c);
+ show(c);
+ }
} else if(r->status == 31) {
- Url *redirect = urlparse(nil, r->prompt);
+ Url *redirect = urlparse(nil, r->meta);
gemini_get(redirect);
}
}
@@ -439,11 +481,10 @@
}
if(strcmp(next_url->scheme, "gemini") == 0){
- free(link);
gemini_get(next_url);
} else {
- message("%s protocol not supported yet!", link);
- free(link);
+ message("Plumbing %s", next_url->raw);
+ plumburl(next_url);
}
}
@@ -495,21 +536,6 @@
pldraw(root, screen);
}
-Image*
-loadicon(Rectangle r, uchar *data, int ndata)
-{
- Image *i;
- int n;
-
- i = allocimage(display, r, RGBA32, 0, DNofill);
- if(i==nil)
- sysfatal("allocimage: %r");
- n = loadimage(i, r, data, ndata);
- if(n<0)
- sysfatal("loadimage: %r");
- return i;
-}
-
void scrolltext(int dy, int whence)
{
Scroll s;
@@ -543,7 +569,11 @@
Url *url;
if(argc == 2)
- url = urlparse(nil, argv[1]);
+ if(strcmp(argv[1], "gemini://") != 0){
+ url = urlparse(nil, smprint("gemini://%s", argv[1]));
+ }else{
+ url = urlparse(nil, argv[1]);
+ }
else
url = urlparse(nil, "gemini://gemini.circumlunar.space/capcom/");
@@ -830,6 +860,7 @@
return nil;
}
if(u->port){
+ print("PORT: %s\n", u->port);
/* remove default ports */
switch(atoi(u->port)){
case 21: if(!strcmp(u->scheme, "ftp")) goto Defport; break;
--- a/castor.h
+++ b/castor.h
@@ -1,5 +1,4 @@
typedef struct Ctx Ctx;
-typedef struct Link Link;
typedef struct Hist Hist;
typedef struct Url Url;
typedef struct Response Response;
@@ -26,7 +25,8 @@
struct Response {
Url *url;
char *mime;
- char *prompt;
+ char *meta;
+ Biobuf *body;
int status;
int fd;
};
@@ -63,6 +63,7 @@
void freeurl(Url *u);
char *Upath(Url *u);
+int request(Url *u);
void gemini_get(Url *u);
void texthit(Panel *p, int b, Rtext *t);
void message(char *s, ...);
\ No newline at end of file