shithub: castor9

Download patch

ref: 6a24e215725d72e1c24d880809f5ea3454d7a6f8
parent: 36ca0d44ee02900ed52ed143c7612341e26555e6
author: Julien Blanchard <[email protected]>
date: Thu Jan 21 09:22:18 EST 2021

Remove url->raw and use fmt, remove undescores from names

--- a/castor.c
+++ b/castor.c
@@ -40,8 +40,8 @@
 };
 
 int request(Url *u);
-void gemini_get(Url *u);
-void gemini_put(Response *r);
+void geminiget(Url *u);
+void geminiput(Response *r);
 void texthit(Panel *p, int b, Rtext *t);
 void entryhit(Panel *p, char *t);
 void addbookmark(void);
@@ -86,24 +86,30 @@
 }
 
 Url *
-current_url(void)
+currenturl(void)
 {
 	return hist->c->url;
 }
 
 Url *
-current_base_url(void)
+currentbaseurl(void)
 {
-	return base_url(current_url());
+	return baseurl(currenturl());
 }
 
 char *
-current_host(void)
+currenthost(void)
 {
-	Url *base = current_base_url();
+	Url *base = currentbaseurl();
 	return base->host;
 }
 
+char *
+urlstr(Url *url)
+{
+	return smprint("%U", url);
+}
+
 char*
 cleanup(char *line)
 {
@@ -118,7 +124,7 @@
     }
     *dst = '\0';
 	
-	replace_char(line, '\t', ' ');
+	replacechar(line, '\t', ' ');
 	return line;
 }
 
@@ -127,7 +133,7 @@
 {
 	plinittextview(textp, PACKE|EXPAND, ZP, c->text, texthit);
 	pldraw(textp, screen);
-	plinitentry(entryp, PACKN|FILLX, 0, c->url->raw, entryhit);
+	plinitentry(entryp, PACKN|FILLX, 0, urlstr(c->url), entryhit);
 	pldraw(entryp, screen);
 	resettitle();
 }
@@ -136,16 +142,16 @@
 plumburl(Url *u)
 {
 	int fd;
+	char *msg;
 
 	fd = plumbopen("send", OWRITE|OCEXEC);
 	if(fd<0)
 		return;
 
-	char *msg;
 	if(strcmp(u->scheme, "mailto") == 0){
 		msg = u->path;
 	}else{
-		msg = u->raw;
+		msg = urlstr(u);
 	}
 	plumbsendtext(fd, "castor9", nil, nil, msg);
 	close(fd);
@@ -162,7 +168,7 @@
 	if(fd < 0)
 		sysfatal("dial: %r");
 
-	fprint(fd, "%s\r\n", u->raw);
+	fprint(fd, "%U\r\n", u);
 	
 	switch(rfork(RFFDG|RFPROC|RFMEM|RFREND|RFNOWAIT|RFNOTEG)){
 	case -1:
@@ -210,7 +216,7 @@
 }
 
 void
-parse_status(char *status, Response *r)
+parsestatus(char *status, Response *r)
 {
 	int code;
 	char *meta, *s;
@@ -232,7 +238,7 @@
 }
 
 void
-render_text(Ctx *c, char *line)
+rendertext(Ctx *c, char *line)
 {
 	char *base, *right_margin;
     int length, width;
@@ -241,8 +247,8 @@
     base = strdup(line);
     width = 80;
 
-	char *preformatted_marker = "```";
-	if(strbeg(line, preformatted_marker) == 0){
+	char *preformattedmarker = "```";
+	if(strbeg(line, preformattedmarker) == 0){
 		if(preformatted==0){
 			preformatted=1;
 		}else{
@@ -288,7 +294,7 @@
 }
 
 void
-render_link(Ctx *c, char *line)
+renderlink(Ctx *c, char *line)
 {
 	char *copy = strdup(cleanup(line + 2)); /* bypass => */
 	char *link = strtok(copy, " ");
@@ -348,7 +354,7 @@
 }
 
 void
-gemini_get(Url *url)
+geminiget(Url *url)
 {
 	int fd;
 	char *line;
@@ -373,18 +379,18 @@
 
 	plrtstr(&c->text, 1000000, 0, 0, font, strdup(" "), 0, 0);
 
-	message("loading %s...", url->raw);
+	message("loading %s...", urlstr(url));
 
 	fd = request(url);
-	fprint(fd, "%s\r\n", url->raw);
+	fprint(fd, "%U\r\n", url);
 	Binit(&body, fd, OREAD);
 
 	char *status = Brdstr(&body, '\n', 0);
-	parse_status(status, r);
+	parsestatus(status, r);
 
 	switch(r->status){
 	case 10:
-		gemini_put(r);
+		geminiput(r);
 		break;
 	case 11:
 		message("Sensitive input! %s", r->meta);
@@ -401,9 +407,9 @@
 		}else{
 			while((line = Brdstr(&body, '\n', 0)) != nil){
 				if(strbeg(line, "=>") == 0){
-					render_link(c, line);					
+					renderlink(c, line);					
 				}else{
-					render_text(c, line);
+					rendertext(c, line);
 				}
 				free(line);
 			}
@@ -416,14 +422,13 @@
 			hist = h;
 
 			show(c);
-
 		}
 		break;
 	case 30:
-		gemini_get(urlparse(url, r->meta));
+		geminiget(urlparse(url, r->meta));	
 		break;
 	case 31:
-		gemini_get(urlparse(url, r->meta));
+		geminiget(urlparse(url, r->meta));
 		break;
 	case 40:
 		message("Temporary failure, please try again later!");
@@ -472,7 +477,7 @@
 }
 
 void
-gemini_put(Response *r)
+geminiput(Response *r)
 {
 	char buf[1024];
 	char *url;
@@ -482,8 +487,8 @@
 	if(eenter(r->meta, buf, sizeof(buf), mouse) <= 0)
 		return;
 	
-	url = smprint("%s?%s", r->url->raw, buf);
-	gemini_get(urlparse(nil, url));
+	url = smprint("%U?%s", r->url, buf);
+	geminiget(urlparse(nil, url));
 }
 
 void
@@ -636,7 +641,7 @@
 	message("loading bookmarks...");
 	
 	while((line = Brdstr(bfile, '\n', 0)) != nil){
-		render_link(c, line);
+		renderlink(c, line);
 		free(line);
 	}
 
@@ -655,7 +660,7 @@
 {
 	int fd;
 	fd = createbookmarks();
-	fprint(fd, "=> %s\n", hist->c->url->raw);
+	fprint(fd, "=> %U\n", hist->c->url);
 	close(fd);
 	message("Bookmark added!");
 }
@@ -670,7 +675,7 @@
 	if(strbeg(t, "gemini://") != 0)
 		t = smprint("gemini://%s", t);
 			
-	gemini_get(urlparse(nil, t));
+	geminiget(urlparse(nil, t));
 }
 
 void
@@ -698,19 +703,19 @@
 		/* assuming relative URL */
 		if(strcmp(link, "/") == 0){
 			/* no slash, must be a hostname */
-			n = smprint("gemini://%s", current_host());
+			n = smprint("gemini://%s", currenthost());
 		}else if(*link == '/'){
 			/* start with a slash so use the base host */
-			n = smprint("gemini://%s%s", current_host(), estrdup(link));
+			n = smprint("gemini://%s%s", currenthost(), estrdup(link));
 		}else{
 			/* make an absolute URL of the link */
-			n = smprint("%s%s", urlparse(current_base_url(), link)->raw, estrdup(link));
+			n = urlstr(urlparse(currentbaseurl(), link));
 		}
 		next_url = urlparse(nil, n);
 	}
 
-	if(strbeg(next_url->raw, "gemini://") == 0){
-		gemini_get(next_url);
+	if(strcmp(next_url->scheme, "gemini") == 0){
+		geminiget(next_url);
 	}else{
 		plumburl(next_url);
 	}
@@ -807,12 +812,19 @@
 		url = urlparse(nil, "gemini://gemini.circumlunar.space/capcom/");
 
 	quotefmtinstall();
+	fmtinstall('U', Ufmt);
+	fmtinstall('N', Nfmt);
+	fmtinstall(']', Mfmt);
+	fmtinstall('E', Efmt);
+	fmtinstall('[', encodefmt);
+	fmtinstall('H', encodefmt);
+
 	if(initdraw(nil, nil, "gemini")<0)
 		sysfatal("initdraw: %r");
 	einit(Emouse|Ekeyboard);
 	plinit(screen->depth);
 	mkpanels();
-	gemini_get(url);
+	geminiget(url);
 	eresized(0);
 	for(;;){
 		switch(event(&e)){
--- a/castor.h
+++ b/castor.h
@@ -2,7 +2,6 @@
 
 struct Url
 {
-	char *raw;
 	char *full;
 	char *scheme;
 	char *user;
@@ -29,7 +28,7 @@
 void *emalloc(int n);
 char *estrdup(char *s);
 int strbeg(char* str, char *prefix);
-char *replace_char(char *s, char f, char r);
+char *replacechar(char *s, char f, char r);
 
 /* url.c */
 int	Efmt(Fmt*);
@@ -37,7 +36,7 @@
 int	Mfmt(Fmt*);
 int Ufmt(Fmt *f);
 Url *urlparse(Url *from, char *s);
-Url *base_url(Url *u);
+Url *baseurl(Url *u);
 int matchurl(Url *u, Url *s);
 void freeurl(Url *u);
 char *Upath(Url *u);
\ No newline at end of file
--- a/url.c
+++ b/url.c
@@ -164,6 +164,7 @@
 	if(dir)
 		*d++ = '/';
 	*d = 0;
+
 	return b;
 }
 
@@ -258,8 +259,6 @@
 	s = p = estrdup(s);
 	u = emalloc(sizeof(*u));
 
-	u->raw = estrdup(s);
-
 	for(; *p; p++){
 		if(*p == ':'){
 			if(p == s)
@@ -380,7 +379,7 @@
 }
 
 Url*
-base_url(Url *url)
+baseurl(Url *url)
 {
 	char *base_url, *path, *ptr;
 	
@@ -436,6 +435,5 @@
 	free(u->path);
 	free(u->query);
 	free(u->fragment);
-	free(u->raw);
 	free(u);
 }
--- a/util.c
+++ b/util.c
@@ -34,7 +34,7 @@
 }
 
 char* 
-replace_char(char* str, char find, char replace)
+replacechar(char* str, char find, char replace)
 {
 	char *current_pos = strchr(str,find);
 	while(current_pos){