~julienxx/castor9

6a24e215725d72e1c24d880809f5ea3454d7a6f8 — Julien Blanchard 3 years ago 36ca0d4
Remove url->raw and use fmt, remove undescores from names
4 files changed, 56 insertions(+), 47 deletions(-)

M castor.c
M castor.h
M url.c
M util.c
M castor.c => castor.c +51 -39
@@ 40,8 40,8 @@ struct Hist
};

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 @@ resettitle(void)
}

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 @@ cleanup(char *line)
    }
    *dst = '\0';
	
	replace_char(line, '\t', ' ');
	replacechar(line, '\t', ' ');
	return line;
}



@@ 127,7 133,7 @@ show(Ctx *c)
{
	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 @@ void
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 @@ page(Url *u)
	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 @@ symbol(char *link)
}

void
parse_status(char *status, Response *r)
parsestatus(char *status, Response *r)
{
	int code;
	char *meta, *s;


@@ 232,7 238,7 @@ parse_status(char *status, Response *r)
}

void
render_text(Ctx *c, char *line)
rendertext(Ctx *c, char *line)
{
	char *base, *right_margin;
    int length, width;


@@ 241,8 247,8 @@ render_text(Ctx *c, char *line)
    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 @@ render_text(Ctx *c, char *line)
}

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 @@ request(Url *url)
}

void
gemini_get(Url *url)
geminiget(Url *url)
{
	int fd;
	char *line;


@@ 373,18 379,18 @@ gemini_get(Url *url)

	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 @@ gemini_get(Url *url)
		}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 @@ gemini_get(Url *url)
			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 @@ gemini_get(Url *url)
}

void
gemini_put(Response *r)
geminiput(Response *r)
{
	char buf[1024];
	char *url;


@@ 482,8 487,8 @@ gemini_put(Response *r)
	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 @@ showbookmarks(void)
	message("loading bookmarks...");
	
	while((line = Brdstr(bfile, '\n', 0)) != nil){
		render_link(c, line);
		renderlink(c, line);
		free(line);
	}



@@ 655,7 660,7 @@ addbookmark(void)
{
	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 @@ entryhit(Panel *p, char *t)
	if(strbeg(t, "gemini://") != 0)
		t = smprint("gemini://%s", t);
			
	gemini_get(urlparse(nil, t));
	geminiget(urlparse(nil, t));
}

void


@@ 698,19 703,19 @@ texthit(Panel *p, int b, Rtext *rt)
		/* 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 @@ main(int argc, char *argv[])
		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)){

M castor.h => castor.h +2 -3
@@ 2,7 2,6 @@ typedef struct Url Url;

struct Url
{
	char *raw;
	char *full;
	char *scheme;
	char *user;


@@ 29,7 28,7 @@ typedef struct {
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	Nfmt(Fmt*);
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

M url.c => url.c +2 -4
@@ 164,6 164,7 @@ remdot(char *s)
	if(dir)
		*d++ = '/';
	*d = 0;

	return b;
}



@@ 258,8 259,6 @@ urlparse(Url *b, char *s)
	s = p = estrdup(s);
	u = emalloc(sizeof(*u));

	u->raw = estrdup(s);

	for(; *p; p++){
		if(*p == ':'){
			if(p == s)


@@ 380,7 379,7 @@ Out:
}

Url*
base_url(Url *url)
baseurl(Url *url)
{
	char *base_url, *path, *ptr;
	


@@ 436,6 435,5 @@ freeurl(Url *u)
	free(u->path);
	free(u->query);
	free(u->fragment);
	free(u->raw);
	free(u);
}

M util.c => util.c +1 -1
@@ 34,7 34,7 @@ strbeg(char *str, char *prefix)
}

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){