buf.c (1671B)
1 /* Copyright 2021-2023 Jacob R. Edwards 2 * 3 * ap -- audio player 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <https://www.gnu.org/licenses/>. 17 */ 18 19 #include <sys/socket.h> 20 21 #include <ctype.h> 22 #include <limits.h> 23 #include <poll.h> 24 #include <stddef.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #include "aps.h" 29 #include "log.h" 30 31 int 32 aps_bufword(struct aps *aps, int fd, char *word) 33 { 34 return bufword(aps->clients[fd].response, word); 35 } 36 37 int 38 aps_bufline(struct aps *aps, int fd, char *line) 39 { 40 return bufline(aps->clients[fd].response, line); 41 } 42 43 int 44 aps_bufstatus(struct aps *aps, int fd, char *error) 45 { 46 size_t errlen; 47 struct buf *buf; 48 49 buf = aps->clients[fd].response; 50 if (bufline(buf, NULL)) 51 return 1; 52 53 if (!error) { 54 if (bufappend(buf, "ok\n", 3)) 55 return 1; 56 aps->pfds[fd].events = POLLOUT; 57 return 0; 58 } 59 60 errlen = strlen(error); 61 if (bufenlarge(buf, 7 + errlen + 1)) 62 return 1; 63 bufappend(buf, "error: ", 7); 64 bufappend(buf, error, errlen); 65 bufappend(buf, "\n", 1); 66 aps->pfds[fd].events = POLLOUT; 67 68 aps_log(aps, fd, INFO, logsubjects[LogCommand], "error: %s", error); 69 return 0; 70 }