From 1e04b22833597acae79dbe479a22c6cc84266e9e Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 7 Nov 2019 18:25:56 +0000 Subject: [PATCH] Fix `mosquitto_pub -l` not sending the final line of stdin This would happen if the final line did not end with a new line. Closes #1473. Thanks to majekw. --- ChangeLog.txt | 2 ++ client/pub_client.c | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 22edb4c6..231458ff 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -24,6 +24,8 @@ Client library: Clients: - Fix duplicate cfg definition in rr_client. Closes #1453. - Fix `mosquitto_pub -l` hang when stdin stream ends. Closes #1448. +- Fix `mosquitto_pub -l` not sending the final line of stdin if it does not + end with a new line. Closes #1473. Build: - Added `CLIENT_STATIC_LDADD` to makefile builds to allow more libraries to be diff --git a/client/pub_client.c b/client/pub_client.c index 217b6ce9..993e8128 100644 --- a/client/pub_client.c +++ b/client/pub_client.c @@ -223,7 +223,7 @@ int pub_shared_init(void) int pub_stdin_line_loop(struct mosquitto *mosq) { char *buf2; - int buf_len_actual; + int buf_len_actual = 0; int pos; int rc = MOSQ_ERR_SUCCESS; int read_len; @@ -240,6 +240,7 @@ int pub_stdin_line_loop(struct mosquitto *mosq) if(line_buf[buf_len_actual-1] == '\n'){ line_buf[buf_len_actual-1] = '\0'; rc = my_publish(mosq, &mid_sent, cfg.topic, buf_len_actual-1, line_buf, cfg.qos, cfg.retain); + pos = 0; if(rc){ err_printf(&cfg, "Error: Publish returned %d, disconnecting.\n", rc); mosquitto_disconnect_v5(mosq, MQTT_RC_DISCONNECT_WITH_WILL_MSG, cfg.disconnect_props); @@ -257,6 +258,13 @@ int pub_stdin_line_loop(struct mosquitto *mosq) line_buf = buf2; } } + if(pos != 0){ + rc = my_publish(mosq, &mid_sent, cfg.topic, buf_len_actual, line_buf, cfg.qos, cfg.retain); + if(rc){ + err_printf(&cfg, "Error: Publish returned %d, disconnecting.\n", rc); + mosquitto_disconnect_v5(mosq, MQTT_RC_DISCONNECT_WITH_WILL_MSG, cfg.disconnect_props); + } + } if(feof(stdin)){ if(mid_sent == -1){ /* Empty file */