ref: 0f5e8eb6cd16ae6e1a54bf769826bd7df5cd321d
parent: 645dac214007d2edd60ea495a1e073cccdf482fc
author: Julien Blanchard <[email protected]>
date: Fri Nov 27 12:31:20 EST 2020
Append protocol if needed
--- a/castor.c
+++ b/castor.c
@@ -118,6 +118,30 @@
return u;
}
+int
+starts_with(char *pre, char *str)
+{
+ int lenpre = strlen(pre),
+ lenstr = strlen(str);
+ return lenstr < lenpre ? 1 : memcmp(pre, str, lenpre);
+}
+
+char *
+protocol(char *link)
+{
+ if(strstr(link, "http://") != nil) {
+ return " [WWW]";
+ } else if(strstr(link, "https://") != nil) {
+ return " [WWW]";
+ } else if(strstr(link, "gopher://") != nil) {
+ return " [GOPHER]";
+ } else if(strstr(link, "finger://") != nil) {
+ return " [FINGER]";
+ } else {
+ return "";
+ }
+}
+
void
gemini_get(Url *url)
{
@@ -163,8 +187,8 @@
{
if (strstr(line, "=>") == nil) {
/* Not a link so wrapping text */
- char *base,*right_margin;
- int length,width;
+ char *base, *right_margin;
+ int length, width;
length = strlen(strdup(line));
base = strdup(line);
@@ -181,12 +205,12 @@
while(!isspace(*right_margin))
{
right_margin--;
- if( right_margin == base)
+ if(right_margin == base)
{
right_margin += width;
while(!isspace(*right_margin))
{
- if( *right_margin == '\0')
+ if(*right_margin == '\0')
break;
right_margin++;
}
@@ -194,8 +218,8 @@
}
*right_margin = '\0';
plrtstr(&m->text, 1000000, 8, 0, font, strdup(cleanup(base)), 0, 0);
- length -= right_margin-base+1; /* +1 for the space */
- base = right_margin+1;
+ length -= right_margin - base + 1; /* +1 for the space */
+ base = right_margin + 1;
}
} else {
/* a link */
@@ -203,7 +227,11 @@
strtok(copy, " ");
char *link = strtok(NULL, " ");
char *rest = strtok(NULL, "\0");
- char *label = smprint("→ %s", rest);
+ while(isspace(*rest))
+ rest++;
+
+ char *label = smprint("→ %s%s", rest, protocol(link));
+
plrtstr(&m->text, 1000000, 8, 0, font, strdup(label), PL_HOT, strdup(link));
}
free(line);
@@ -278,8 +306,10 @@
char *link = rt->user;
copy = link;
- if ((strpbrk(link, " :/") == nil) || link[0] == '/') {
+ int len = strlen(copy);
+ if ((strpbrk(link, " :/") == nil) || link[0] == '/' || link[len-1] == '/') {
/* assuming relative URL */
+ message(copy);
if (*copy == '/') {
next_url = smprint("gemini://%s:%s/%s", current_url->server, current_url->port, copy+1);
} else {