4 FlushSReadlineInfo(SReadlineInfo
*srl
)
6 /* Discards any input left in the current buffer,
7 * and resets the buffer and its pointer.
9 srl
->bufSize
= srl
->bufSizeMax
;
10 memset(srl
->buf
, 0, srl
->bufSize
);
11 srl
->bufLim
= srl
->buf
+ srl
->bufSizeMax
;
13 /* This line sets the buffer pointer
14 * so that the first thing to do is reset and fill the buffer
17 srl
->bufPtr
= srl
->bufLim
;
18 } /* FlushSReadlineInfo */
24 InitSReadlineInfo(SReadlineInfo
*srl
, int fd
, char *buf
, size_t bsize
, int tlen
, int requireEOLN
)
28 bsize
= 512; /* Pointless, otherwise. */
29 buf
= (char *) malloc(bsize
);
36 memset(buf
, 0, bsize
);
39 srl
->bufSizeMax
= bsize
;
40 srl
->bufLim
= srl
->buf
+ bsize
;
42 srl
->timeoutLen
= tlen
;
43 srl
->requireEOLN
= requireEOLN
;
45 /* This line sets the buffer pointer
46 * so that the first thing to do is reset and fill the buffer
49 srl
->bufPtr
= srl
->bufLim
;
51 } /* InitSReadlineInfo */
57 DisposeSReadlineInfo(SReadlineInfo
*srl
)
59 memset(srl
->buf
, 0, srl
->bufSizeMax
);
62 memset(srl
, 0, sizeof(SReadlineInfo
));
64 /* Note: it does not close(srl->fd). */
65 } /* DisposeSReadlineInfo */
70 /* Returns the number of bytes read, including the newline which is
71 * also appended to the buffer. If you don't want that newline,
72 * set buf[nread - 1] = '\0', if nread > 0.
76 SReadline(SReadlineInfo
*srl
, char *const linebuf
, size_t linebufsize
)
90 dstlim
= dst
+ linebufsize
- 1; /* Leave room for NUL. */
92 requireEOLN
= srl
->requireEOLN
;
96 return (-1); /* Buffer too small. */
99 if ((requireEOLN
== 0) && (dst
>= dstlim
))
101 if (src
>= srl
->bufLim
) {
102 /* Fill the buffer. */
104 /* Probable DOS -- return now and give you an
105 * opportunity to handle bogus input.
109 nr
= SRead(srl
->fd
, srl
->buf
, srl
->bufSizeMax
, srl
->timeoutLen
, 0);
118 srl
->bufPtr
= src
= srl
->buf
;
119 srl
->bufLim
= srl
->buf
+ nr
;
124 } else if (*src
== '\r') {
126 /* If the next character is a \n that is valid,
127 * otherwise treat a stray \r as an illegal character.
129 if ((src
< srl
->bufLim
) && (*src
!= '\n'))
148 if ((requireEOLN
!= 0) && (dst
!= linebuf
) && (dst
[-1] != '\n'))
150 if ((requireEOLN
!= 0) && (dst
== linebuf
) && (illegals
> 0))
153 len
= (int) (dst
- linebuf
);