Skip to content

Commit

Permalink
Fix mosquitto_pub -l not sending the final line of stdin
Browse files Browse the repository at this point in the history
This would happen if the final line did not end with a new line.

Closes #1473. Thanks to majekw.
  • Loading branch information
ralight committed Nov 7, 2019
1 parent 05171b2 commit 1e04b22
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ChangeLog.txt
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion client/pub_client.c
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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 */
Expand Down

0 comments on commit 1e04b22

Please sign in to comment.