adji

Adji's a Decisive and Joyful Internet browser
git clone https://noxz.tech/git/adji.git
Log | Files | Tags | LICENSE

commit: 2b4aa08d7c74cfd182331f1b209307784a5e21cc
parent: f09c15a89e7c20d23d658d26eec707a064895ee8
author: Chris Noxz <chris@noxz.tech>
date:   Fri, 7 Apr 2023 18:27:02 +0200
add feature to render from stdin

If the instance is independent, stdin is treated as HTML content and rendered
accordingly. The rendered page won't survive a reload, as the content from
stdin isn't stored anywhere. Implementations using data-URIs were considered
and rejected due to resulting in unnecessary bloat, as well as prompting issues
with too large URIs possibly being stored in state files. This feature could
have been implemented for IPC setups as well. However, if that kind of feature
is needed, it is much simpler to just use a temporary file (using mktemp) for
storage, which can then be opened in the browser.
Madji.118++++++++----
Mbrowser.c31++++++++++++++++++--
Mbrowser.h1+
3 files changed, 43 insertions(+), 7 deletions(-)
diff --git a/adji.1 b/adji.1
@@ -5,8 +5,9 @@ adji \- Adji's a Decisive and Joyful Internet browser
 .
 .SH SYNOPSIS
 .B adji
-.RB [ \-I ]
+.RB [ \-I " [" \- ]]
 .IR "" [ URI ...]
+.IR "" [ FILE ...]
 .
 .SH DESCRIPTION
 .B adji
@@ -25,12 +26,19 @@ Along with the standard arguments for GTK+ 3 (see
 is equipped with the following options:
 .TP
 .B -I
-Implements an independent instance without any ipc configuration.
+Implements an independent instance without any IPC configuration.
 .P
-If no URI are provided, the value of the enviornment variable
+If no URIs are provided, the value of the enviornment variable
 .B ADJI_HOME_URI
-will be opened which defaults to \(lqabout:blank\(rq. Otherwise, any number of
-URIs may follow these options.
+will be opened, which defaults to \(lqabout:blank\(rq. If the first URI is '-',
+.B adji
+will only read and interpret
+.B STDIN
+as HTML content. However, the content will only be processed when running as an
+independent instance. Content from
+.B STDIN
+won't survive a page reload either. If that is needed, temporary files should
+instead be utilized. Otherwise, any number of URIs may follow these options.
 .
 .SH ENVIRONMENT
 .B adji
diff --git a/browser.c b/browser.c
@@ -723,6 +723,30 @@ load_state(void)
 	free(u);
 }
 
+void
+load_stdin()
+{
+	int                     c;                  /* character holder */
+	GString                *s = g_string_new(NULL);
+
+	/* read until end of stream */
+	while ((c = getc(stdin)) != EOF)
+		g_string_append_c(s, (gchar)c);
+
+	/* ignore content of stdin if instance is not independent, as there is no
+	 * uri to send over IPC anyway */
+	if (gl.ipc != IPC_NONE)
+		fprintf(stderr, __NAME__": stdin detected, use with -I instead\n");
+	else
+		/* load the html content, which won't survive a reload */
+		webkit_web_view_load_html(
+		    WEBKIT_WEB_VIEW(client_create(NULL, NULL, TRUE, TRUE)->wv),
+		    s->str,
+		    NULL
+		);
+	g_string_free(s, TRUE);
+}
+
 void
 load_user_styles(WebKitWebView *web_view)
 {
@@ -1388,7 +1412,7 @@ main(int argc, char **argv)
 			gl.ipc = IPC_NONE;
 			break;
 		default:
-			die("Usage: " __NAME__ " [-I] [URI ...]\n");
+			die("Usage: " __NAME__ " [-I [-]] [URI ...] [FILE ...]\n");
 		}
 
 	ipc_setup();
@@ -1396,9 +1420,12 @@ main(int argc, char **argv)
 	main_window_setup();
 	load_state();
 
-	/* load a default home uri if no clients or arguments exist  */
+	/* load a default home uri if no clients and no arguments exist  */
 	if (optind >= argc && gl.clients == 0)
 		client_create(CFG_S(HomeUri), NULL, TRUE, TRUE);
+	/* load stdin if first argument is '-' */
+	if (optind < argc && strcmp(argv[optind], "-") == 0)
+		load_stdin();
 	/* load remaining command line arguments as uris into new clients */
 	else if (optind < argc)
 		for (i = optind; i < argc; i++)
diff --git a/browser.h b/browser.h
@@ -211,6 +211,7 @@ static gboolean key_web_view(struct Client *, GdkEvent *);
 static void load_changed(struct Client *, WebKitLoadEvent);
 static void load_configuration(void);
 static void load_state(void);
+static void load_stdin(void);
 static void load_user_styles(WebKitWebView *);
 static void main_window_setup(void);
 static void open_external(struct Client *);