[dev] [surf] read from stdin

2 views
Skip to first unread message

Avid Seeker

unread,
Mar 15, 2025, 5:04:52 PMMar 15
to d...@suckless.org
I was kinda surprised suckless surf doesn't have this feature.

It's a surprisingly frequently asked [1] question and a straightforward
way to render HTML.

[1]: see https://unix.stackexchange.com/questions/24931/,
https://stackoverflow.com/questions/3591167/, and https://superuser.com/questions/526327

Cole Young

unread,
Mar 15, 2025, 7:19:57 PMMar 15
to dev mail list, d...@suckless.org
I thought it did have this feature. That’s wild.

> On Mar 15, 2025, at 5:05 PM, Avid Seeker <avids...@disroot.org> wrote:
>
> I was kinda surprised suckless surf doesn't have this feature.

Roberto E. Vargas Caballero

unread,
Mar 20, 2025, 7:08:57 AMMar 20
to d...@suckless.org, Quentin Rameau
Hi,

Quoth Cole Young <co...@young.sh>:
> I thought it did have this feature. That’s wild.
>
> > On Mar 15, 2025, at 5:05 PM, Avid Seeker <avids...@disroot.org> wrote:
> >
> > I was kinda surprised suckless surf doesn't have this feature.
> >
> > It's a surprisingly frequently asked [1] question and a straightforward
> > way to render HTML.

I found some legit use cases for this, and since it was not very hard
to achieve I wrote a small patch that consumes all the input and writes
a temporary file that then it is hijacked into a file:// uri. It is just
a proof of concept, and in case of being added would require also changes
to the man page.

--- 8< ---
From 4d0572544e2301235edfbeae58cd3865413592f0 Mon Sep 17 00:00:00 2001
From: "Roberto E. Vargas Caballero" <k0...@shike2.net>
Date: Thu, 20 Mar 2025 12:01:50 +0100
Subject: [PATCH] Render html from stdin

There are some use cases where consuming the html
from stdin can be convenient. The only possible
way to achieve that is to write the full input
stream and then convert it to a file:// uri.
---
surf.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)

diff --git a/surf.c b/surf.c
index 18d65d7..5663a7e 100644
--- a/surf.c
+++ b/surf.c
@@ -179,6 +179,8 @@ static void spawn(Client *c, const Arg *a);
static void msgext(Client *c, char type, const Arg *a);
static void destroyclient(Client *c);
static void cleanup(void);
+static void readstdin(Arg *a);
+static void removetmp(void);

/* GTK/WebKit */
static WebKitWebView *newview(Client *c, WebKitWebView *rv);
@@ -257,6 +259,7 @@ static const char *useragent;
static Parameter *curconfig;
static int modparams[ParameterLast];
static int spair[2];
+static char *tmpfname;
char *argv0;

static ParamName loadtransient[] = {
@@ -1983,6 +1986,44 @@ clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
spawn(c, &arg);
}

+void
+removetmp(void)
+{
+ remove(tmpfname);
+}
+
+void
+readstdin(Arg *a)
+{
+ FILE *fp;
+ int fd, c, r;
+ char *dir;
+ static char uri[FILENAME_MAX];
+
+ if ((dir = getenv("TMPDIR")) == NULL)
+ dir = "/tmp";
+ r = snprintf(uri, sizeof(uri), "file://%s/%s", dir, "surf.XXXXXX");
+ if (r < 0 || r >= sizeof(uri))
+ die("temporary name too long");
+ tmpfname = uri + sizeof("file://")-1;
+ atexit(removetmp);
+
+ if ((fd = mkstemp(tmpfname)) < 0)
+ die("error creating temporary name");
+
+ if ((fp = fdopen(fd, "w")) == NULL)
+ die("error getting a temporary file pointer");
+
+ while ((c = getchar()) != EOF)
+ putc(c, fp);
+ fflush(fp);
+
+ if (ferror(stdin) || ferror(fp))
+ die("error copying input data to temporary file");
+
+ a->v = uri;
+}
+
int
main(int argc, char *argv[])
{
@@ -2117,6 +2158,9 @@ main(int argc, char *argv[])
else
arg.v = "about:blank";

+ if (strcmp(arg.v, "-") == 0)
+ readstdin(&arg);
+
setup();
c = newclient(NULL);
showview(NULL, c);
--
2.45.3

Regards,


Quentin Rameau

unread,
Mar 21, 2025, 4:24:35 AMMar 21
to d...@suckless.org
> Hi,

Hi,

> > I thought it did have this feature. That’s wild.
> >
> > > On Mar 15, 2025, at 5:05 PM, Avid Seeker <avids...@disroot.org>
> > > wrote:
> > >
> > > I was kinda surprised suckless surf doesn't have this feature.
> > >
> > > It's a surprisingly frequently asked [1] question and a
> > > straightforward way to render HTML.

I'm not sure what's surprising/wild about this,
that's the first time this comes up in ~15 years of surf existence,
apparently none of the other graphical web browsers support it either.

> I found some legit use cases for this, and since it was not very hard
> to achieve I wrote a small patch that consumes all the input and
> writes a temporary file that then it is hijacked into a file:// uri.
> It is just a proof of concept, and in case of being added would
> require also changes to the man page.
>
> There are some use cases where consuming the html
> from stdin can be convenient. The only possible
> way to achieve that is to write the full input
> stream and then convert it to a file:// uri.
> ---

What about /dev/stdin?

Roberto E. Vargas Caballero

unread,
Mar 21, 2025, 5:00:22 AMMar 21
to d...@suckless.org
Hi,

Quoth Quentin Rameau <qu...@fifth.space>:
> What about /dev/stdin?

The thing is that you need a seeakable stream, and it is
unlikely that you can do that with /dev/stdin or directly
reading from stdin.

Regards,


Reply all
Reply to author
Forward
0 new messages