Browse Source

pager: UsePager config option

Serj Kalichev 1 year ago
parent
commit
d0eaf0e5cf
6 changed files with 53 additions and 4 deletions
  1. 19 0
      bin/klish/interactive.c
  2. 2 0
      bin/klish/klish.c
  3. 10 1
      bin/klish/opts.c
  4. 1 0
      bin/klish/private.h
  5. 14 2
      klish.conf
  6. 7 1
      klishd.conf

+ 19 - 0
bin/klish/interactive.c

@@ -33,6 +33,8 @@ static bool_t stdin_cb(faux_eloop_t *eloop, faux_eloop_type_e type,
 static bool_t ktp_sync_auth(ktp_session_t *ktp, int *retcode,
 	faux_error_t *error);
 static void reset_hotkey_table(ctx_t *ctx);
+static bool_t interactive_stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
+	void *user_data);
 
 // Keys
 static bool_t tinyrl_key_enter(tinyrl_t *tinyrl, unsigned char key);
@@ -74,6 +76,9 @@ int klish_interactive_shell(ktp_session_t *ktp, struct options *opts)
 	ctx.opts = opts;
 	faux_bzero(ctx.hotkeys, sizeof(ctx.hotkeys));
 
+	// Replace common stdout callback by interactive-specific one.
+	ktp_session_set_cb(ktp, KTP_SESSION_CB_STDOUT, interactive_stdout_cb, &ctx);
+
 	// Now AUTH command is used only for starting hand-shake and getting
 	// prompt from the server. Generally it must be necessary for
 	// non-interactive session too but for now is not implemented.
@@ -560,3 +565,17 @@ static bool_t ktp_sync_auth(ktp_session_t *ktp, int *retcode,
 
 	return ktp_session_retcode(ktp, retcode);
 }
+
+
+static bool_t interactive_stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
+	void *udata)
+{
+	ctx_t *ctx = (ctx_t *)udata;
+
+	if (write(STDOUT_FILENO, line, len) < 0)
+		return BOOL_FALSE;
+
+	ktp = ktp; // Happy compiler
+
+	return BOOL_TRUE;
+}

+ 2 - 0
bin/klish/klish.c

@@ -94,6 +94,8 @@ int main(int argc, char **argv)
 		fprintf(stderr, "Error: Can't create klish session\n");
 		goto err;
 	}
+	// These callback functions is only for batch mode. Interactive
+	// mode can reassign them.
 	ktp_session_set_cb(ktp, KTP_SESSION_CB_STDOUT, stdout_cb, NULL);
 	ktp_session_set_cb(ktp, KTP_SESSION_CB_STDERR, stderr_cb, NULL);
 

+ 10 - 1
bin/klish/opts.c

@@ -38,6 +38,7 @@ struct options *opts_init(void)
 	opts->cfgfile_userdefined = BOOL_FALSE;
 	opts->unix_socket_path = faux_str_dup(KLISH_DEFAULT_UNIX_SOCKET_PATH);
 	opts->pager = faux_str_dup(DEFAULT_PAGER);
+	opts->pager_enabled = BOOL_TRUE;
 
 	// Don't free command list because elements are the pointers to
 	// command line options and don't need to be freed().
@@ -202,12 +203,20 @@ bool_t config_parse(const char *cfgfile, struct options *opts)
 		opts->unix_socket_path = faux_str_dup(tmp);
 	}
 
-	// DBs
+	// Pager
 	if ((tmp = faux_ini_find(ini, "Pager"))) {
 		faux_str_free(opts->pager);
 		opts->pager = faux_str_dup(tmp);
 	}
 
+	// Use pager: y/n
+	if ((tmp = faux_ini_find(ini, "UsePager"))) {
+		if (strcmp(tmp, "y") == 0)
+			opts->pager_enabled = BOOL_TRUE;
+		else
+			opts->pager_enabled = BOOL_FALSE;
+	}
+
 	faux_ini_free(ini);
 
 	return BOOL_TRUE;

+ 1 - 0
bin/klish/private.h

@@ -17,6 +17,7 @@ struct options {
 	bool_t cfgfile_userdefined;
 	char *unix_socket_path;
 	char *pager;
+	bool_t pager_enabled;
 	bool_t stop_on_error;
 	bool_t dry_run;
 	bool_t quiet;

+ 14 - 2
klish.conf

@@ -1,2 +1,14 @@
-UnixSocketPath=/tmp/klish-unix-socket
-#Pager=/usr/bin/more
+# Template for config file /etc/klish/klish.conf. It's used by klish utility.
+
+# The klishd uses UNIX domain socket to receive connections. It will create an
+# filesystem entry to allow clients to find connection point. By default klish
+# client utility will connect to /tmp/klish-unix-socket.
+#UnixSocketPath=/tmp/klish-unix-socket
+
+# The klish can use external pager for non-interactive commands. By default it
+# will execute "/usr/bin/less -I -F -e -X -K -d" process as a pager.
+#Pager=/usr/bin/less -I -F -e -X -K -d
+
+# External pager is enabled by default. But user can explicitly enable or
+# disable it. Use "y" or "n" values.
+# UsePager=y

+ 7 - 1
klishd.conf

@@ -1,2 +1,8 @@
-UnixSocketPath=/tmp/klish-unix-socket
+# Template for config file /etc/klish/klishd.conf. It's used by klishd daemon.
+
+# The klishd uses UNIX domain socket to receive connections. It will create an
+# filesystem entry to allow clients to find connection point. By default klishd
+# uses /tmp/klish-unix-socket path.
+#UnixSocketPath=/tmp/klish-unix-socket
+
 DBs=libxml2