Browse Source

Client can print server notifications

Serj Kalichev 5 months ago
parent
commit
c318f22353
4 changed files with 56 additions and 3 deletions
  1. 29 0
      bin/klish/klish.c
  2. 23 0
      klish/ktp/ktp_session.c
  3. 3 3
      klish/ktp/ktpd_session.c
  4. 1 0
      klish/ktp_session.h

+ 29 - 0
bin/klish/klish.c

@@ -76,6 +76,7 @@ static bool_t cmd_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
 static bool_t cmd_incompleted_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
 static bool_t completion_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
 static bool_t help_ack_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
+static bool_t notification_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata);
 
 // Eloop callbacks
 //static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
@@ -224,6 +225,8 @@ int main(int argc, char **argv)
 	ktp_session_set_cb(ktp, KTP_SESSION_CB_COMPLETION_ACK,
 		completion_ack_cb, &ctx);
 	ktp_session_set_cb(ktp, KTP_SESSION_CB_HELP_ACK, help_ack_cb, &ctx);
+	ktp_session_set_cb(ktp, KTP_SESSION_CB_NOTIFICATION,
+		notification_cb, &ctx);
 
 	// Commands from cmdline
 	if (ctx.mode == MODE_CMDLINE) {
@@ -1100,6 +1103,32 @@ static bool_t stdout_cb(ktp_session_t *ktp, const char *line, size_t len,
 }
 
 
+bool_t notification_cb(ktp_session_t *ktp, const faux_msg_t *msg, void *udata)
+{
+	char *str = NULL;
+	ctx_t *ctx = (ctx_t *)udata;
+
+	str = faux_msg_get_str_param_by_type(msg, KTP_PARAM_ERROR);
+	if (!str)
+		return BOOL_TRUE;
+
+	if (ctx->mode == MODE_INTERACTIVE) {
+		tinyrl_multi_crlf(ctx->tinyrl);
+		tinyrl_reset_line_state(ctx->tinyrl);
+	}
+	fprintf(stderr, "Note: %s\n", str);
+	fflush(stderr);
+	if (ctx->mode == MODE_INTERACTIVE)
+		tinyrl_redisplay(ctx->tinyrl);
+
+	faux_str_free(str);
+
+	ktp = ktp; // Happy compiler
+
+	return BOOL_TRUE;
+}
+
+
 static void signal_handler_empty(int signo)
 {
 	signo = signo; // Happy compiler

+ 23 - 0
klish/ktp/ktp_session.c

@@ -497,6 +497,26 @@ static bool_t ktp_session_process_help_ack(ktp_session_t *ktp, const faux_msg_t
 }
 
 
+static bool_t ktp_session_process_notification(ktp_session_t *ktp, const faux_msg_t *msg)
+{
+	assert(ktp);
+	assert(msg);
+
+	// Get exit flag from message
+	if (KTP_STATUS_IS_EXIT(faux_msg_get_status(msg)))
+		ktp->done = BOOL_TRUE;
+
+	// Execute external callback
+	if (ktp->cb[KTP_SESSION_CB_NOTIFICATION].fn)
+		((ktp_session_event_cb_fn)
+			ktp->cb[KTP_SESSION_CB_NOTIFICATION].fn)(
+			ktp, msg,
+			ktp->cb[KTP_SESSION_CB_NOTIFICATION].udata);
+
+	return BOOL_TRUE;
+}
+
+
 /*
 static bool_t ktp_session_process_exit(ktp_session_t *ktp, const faux_msg_t *msg)
 {
@@ -571,6 +591,9 @@ static bool_t ktp_session_dispatch(ktp_session_t *ktp, faux_msg_t *msg)
 		}
 		rc = ktp_session_process_stderr(ktp, msg);
 		break;
+	case KTP_NOTIFICATION:
+		rc = ktp_session_process_notification(ktp, msg);
+		break;
 	default:
 		syslog(LOG_WARNING, "Unsupported command: 0x%04x\n", cmd); // Ignore
 		break;

+ 3 - 3
klish/ktp/ktpd_session.c

@@ -1257,21 +1257,21 @@ static bool_t ktpd_session_dispatch(ktpd_session_t *ktpd, faux_msg_t *msg)
 		break;
 	case KTP_STDIN_CLOSE:
 		if (ktpd->state != KTPD_SESSION_STATE_WAIT_FOR_PROCESS) {
-			err = "No active command is running (closing stdin)";
+//			err = "No active command is running (closing stdin)";
 			break;
 		}
 		ktpd_session_process_stdin_close(ktpd, msg);
 		break;
 	case KTP_STDOUT_CLOSE:
 		if (ktpd->state != KTPD_SESSION_STATE_WAIT_FOR_PROCESS) {
-			err = "No active command is running (closing stdout)";
+//			err = "No active command is running (closing stdout)";
 			break;
 		}
 		ktpd_session_process_stdout_close(ktpd, msg);
 		break;
 	case KTP_STDERR_CLOSE:
 		if (ktpd->state != KTPD_SESSION_STATE_WAIT_FOR_PROCESS) {
-			err = "No active command is running (closing stderr)";
+//			err = "No active command is running (closing stderr)";
 			break;
 		}
 		ktpd_session_process_stderr_close(ktpd, msg);

+ 1 - 0
klish/ktp_session.h

@@ -65,6 +65,7 @@ typedef enum {
 	KTP_SESSION_CB_COMPLETION_ACK,
 	KTP_SESSION_CB_HELP_ACK,
 	KTP_SESSION_CB_EXIT,
+	KTP_SESSION_CB_NOTIFICATION,
 	KTP_SESSION_CB_MAX,
 } ktp_session_cb_e;