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
, int fd
)
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 if (self
->eol
< self
->eob
)
40 //there was a line pending, shift buffer left
41 memmove (self
->buffer
, self
->eol
+1, self
->eob
- self
->eol
- 1);
42 self
->eob
= (char*)(self
->eob
- (self
->eol
- self
->buffer
+ 1));
43 self
->eol
= self
->buffer
;
48 // find next newline, terminate string there
49 for (char* i
= self
->buffer
; i
< self
->eob
; ++i
)
55 // have line, return it
56 return (self
->eob
== self
->buffer
) ? NULL
: self
->buffer
;
60 // else we have to read
61 if (self
->state
== RXPD_OK
)
66 r
= pth_read (self
->fd
, self
->eob
, 4095 - (self
->eob
- self
->buffer
));
68 while (r
== -1 && errno
== EINTR
);
74 shutdown (self
->fd
, SHUT_RD
);
75 self
->state
= RXPD_EOF
;
80 self
->state
= RXPD_ERROR
;
82 } while (self
->state
== RXPD_OK
); // TODO while (!buffer overfulls)
88 rxpd_buffer_printf (struct rxpd_buffer
* self
, const char* fmt
, ...)
92 int n
= vsnprintf (self
->buffer
, 4096, fmt
, ap
);
95 pth_write (self
->fd
, self
->buffer
, n
);