2 rxpd_buffer.c - regex policy daemon
5 2007, Christian Thaeter <ct@pipapo.org>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 rxpd_buffer_init (struct rxpd_buffer* self, struct rxpd_connection* conn)
28 self->state = RXPD_OK;
29 self->eol = self->eob = self->buffer;
30 self->buffer [4095] = '\0';
36 rxpd_buffer_readline (struct rxpd_buffer* self)
38 int fd = self->conn->fd;
40 if (self->eol < self->eob)
42 //there was a line pending, shift buffer left
43 memmove (self->buffer, self->eol+1, self->eob - self->eol - 1);
44 self->eob = (char*)(self->eob - (self->eol - self->buffer + 1));
45 self->eol = self->buffer;
51 // find next newline, terminate string there
52 for (char* i = self->buffer; i < self->eob; ++i)
58 // have line, return it
59 return (self->eob == self->buffer) ? NULL : self->buffer;
63 // else we have to read
64 if (self->state == RXPD_OK)
69 r = pth_read (fd, self->eob, 4095 - (self->eob - self->buffer));
71 while (r == -1 && errno == EINTR);
77 shutdown (fd, SHUT_RD);
78 self->state = RXPD_EOF;
83 self->state = RXPD_ERROR;
85 } while (1); // TODO while (!buffer overfulls)
91 rxpd_buffer_write(int fd, short event, void* ptr)
93 struct rxpd_buffer* self = (struct rxpd_buffer*) ptr;
95 ssize_t n = write(int fd, const void *buf, size_t count);
101 rxpd_buffer_printf (struct rxpd_buffer* self, const char* fmt, ...)
103 // for now we do a blocking write, needs to be fixed some day, timeout!
106 int n = vsnprintf (self->buffer, 4096, fmt, ap);
109 pth_write (self->conn->fd, self->buffer, n);