A tiling window manager

receive_command: replace the 1-buffer, 1-char read loop with recv_unix()

This re-uses the code factored out from send_command() to receive the
sent command in multiple chunks into a dynamic heap buffer. Now we are
able to receive arbitrarily sized commands that span multiple BUFSZs.
This restores a regression from the original Ratpoison.

Example: 'sdorfehs -c "echo $(ncal 2024)"' will print the full year's
calendar as a message, it's over 2k bytes, so it will span 3 buffer
quanta (BUFSZ == 1024 at time of writing).

authored by

Scott Mcdermott and committed by jcs.org 153f4ce5 17489f14

+7 -19
+7 -19
communications.c
··· 211 211 receive_command(void) 212 212 { 213 213 cmdret *cmd_ret; 214 - char cmd[BUFSZ] = { 0 }, c; 215 - char *result, *rcmd; 214 + char *result, *rcmd, *cmd; 216 215 int cl, len = 0, interactive = 0; 217 216 218 217 PRINT_DEBUG(("have connection waiting on command socket\n")); ··· 222 221 return; 223 222 } 224 223 225 - while (len <= sizeof(cmd)) { 226 - if (len == sizeof(cmd)) { 227 - warn("%s: bogus command length", __func__); 228 - close(cl); 229 - return; 230 - } 231 - 232 - if (read(cl, &c, 1) == 1) { 233 - cmd[len] = c; 234 - if (len++ && c == '\0') 235 - break; 236 - } else if (errno != EAGAIN) { 237 - PRINT_DEBUG(("bad read result on control socket: %s\n", 238 - strerror(errno))); 239 - break; 240 - } 224 + len = recv_unix(cl, &cmd) 225 + if (cmd[len] != '\0') { 226 + /* should not be possible, TODO remove */ 227 + warnx("%s\n", "last byte of sent command not null"); 228 + cmd[len] = '\0'; 241 229 } 242 - 243 230 interactive = cmd[0]; 244 231 rcmd = cmd + 1; 245 232 ··· 267 254 268 255 PRINT_DEBUG(("receive_command: write finished, closing\n")); 269 256 257 + free(cmd); 270 258 close(cl); 271 259 }