-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrrbuf.c
243 lines (222 loc) · 6.23 KB
/
crrbuf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* $ crrbuf.h $
*
* Author: Tomi Ollila -- too ät iki piste fi
*
* Copyright (c) 2018 Tomi Ollila
* All rights reserved
*
* Created: Tue 11 Nov 2008 21:04:29 EET too
* Last modified: Wed 19 May 2021 21:01:08 +0300 too
*/
/* SPDX-License-Identifier: BSD-2-Clause */
#include "more-warnings.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include "crrbuf.h"
//#define DEBUG_RB 1
#define DEBUG_RB 0
#if DEBUG_RB
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <fcntl.h>
static void dbprintf(const char * format, ...)
{
char buf[1024]; unsigned int l; va_list ap;
//int fd = open("rb_debug", O_WRONLY|O_CREAT|O_APPEND, 0644);
//if (fd < 0) abort();
va_start(ap, format);
l = vsnprintf(buf, sizeof buf, format, ap);
va_end(ap);
write(1, buf, l > sizeof buf? sizeof buf: l);
//write(fd, buf, l > sizeof buf? sizeof buf: l);
//close(fd);
}
#define d1(x) dbprintf x
#define d0(x) do {} while (0)
#else
#define d1(x) do {} while (0)
#define d0(x) do {} while (0)
#endif // DEBUG_RB
#define null ((void *)0)
struct _CrRBuf {
uint32_t max; /* +1 */
uint32_t next;
uint32_t lastnl; /* +1 */
uint16_t bufsize;
char sizecrossed; // no longer used, works as pad
unsigned char prevchar;
unsigned char buf[];
};
/* initial implementation -- byte at a time */
/* fails only on out-of-mem */
CrRBuf * crrbuf_create(int bufsize)
{
/**/ if (bufsize < 16) bufsize = 16;
else if (bufsize > 16384) bufsize = 16384;
/* bufsize have to be power of 2 (otherwise problem when integer wraps) */
else if ((bufsize & (bufsize - 1)) != 0) {
bufsize = 1 << (32 - __builtin_clz((unsigned int)bufsize));
}
CrRBuf * rb = malloc(sizeof *rb + bufsize);
if (rb == null)
return null;
d1(("* bufsize: %d\n", bufsize));
memset(rb, 0, sizeof *rb);
rb->bufsize = (uint16_t)bufsize;
crrbuf_append(rb, (const unsigned char *)"\r\n", 2);
return rb;
}
/*
* crrbuf_append() adds given text data to the buffer, handling CRLF, LFCR
* and finally plain CR specially. This way output that writes to one line
* continuously (using CR to move cursor back at the beginning of line)
* does not use all buffer space. This provides somewhat good experience
* without more knowledge of control chars and escape sequences
* (and, especially, without copying data around).
* In addition to that the following 'characters' are not buffered:
* 0x07: (audible?) bell -- minimum effort noise cancellation
*/
void crrbuf_append(CrRBuf * rb, const unsigned char * s, int len)
{
while (len-- > 0) {
unsigned char c = *s++;
switch (c) {
case '\n':
d0(("xx %d ", rb->next));
if ((int32_t)(rb->max - rb->next) > 0)
rb->next = rb->max;
d0(("// %d ", rb->next));
if (rb->prevchar == '\r') {
rb->buf[rb->next++ % rb->bufsize] = '\r';
c = 0;
}
rb->buf[rb->next++ % rb->bufsize] = '\n';
rb->lastnl = rb->next;
break;
case '\r':
if (rb->prevchar == '\n') {
rb->buf[rb->next++ % rb->bufsize] = '\r';
c = 0;
}
else {
if ((int32_t)(rb->next - rb->max) > 0)
rb->max = rb->next;
rb->next = rb->lastnl;
}
break;
// not logging:
case 0x07: // bell
break;
#if 0
/* trying BS handling broke e.g. ^H ESC [ K sequence badly.
* w/ some effort handling that stream of octets at rb->max
* could be handled, but it is SMOP, and probably not last */
case 0x08: /* backspace */
if ((int32_t)(rb->next - rb->lastnl) > 0)
// should we also do some rb->max handling?
rb->next--;
break;
#endif
default:
d0(("%d ", rb->next));
rb->buf[rb->next++ % rb->bufsize] = c;
}
rb->prevchar = c;
}
if ((int32_t)(rb->next - rb->max) > 0)
rb->max = rb->next;
// it is highly unlikely that when rb->max wraps at 4GiB
// the data is requested, more likely sizecrossed is set
// but backward movements moves rb->next back...
// (actually, this comment may be invalid...)
// if (rb->sizecrossed == 0 && rb->max > rb->bufsize)
// rb->sizecrossed = 1;
}
int crrbuf_data(CrRBuf * rb, struct iovec iov[2])
{
uint32_t umax;
// if last input char was \r, add it to output. a hack
// common use case (with progress bars) is to move cursor
// at the beginning of line after data written...
d0(("max %d next %d lastnl %d\n", rb->max, rb->next, rb->lastnl));
if ((int32_t)(rb->max - rb->next) > 0 && rb->next == rb->lastnl) {
umax = rb->max + 1;
rb->buf[rb->max % rb->bufsize] = '\r';
}
else
umax = rb->max;
uint16_t mix = umax % rb->bufsize;
#if DEBUG_RB && 0
uint16_t bs = rb->bufsize;
d1(("\r\n umax %d (%d) <- mix"
"\r\n next %d (%d) "
"\r\n lastnl %d (%d)"
"\r\n last chars: %d %d %d %d %d %d %d %d"
"\r\n", umax, umax % bs,
rb->next, rb->next % bs, /**/ rb->lastnl, rb->lastnl % bs,
rb->buf[(umax - 8) % bs], rb->buf[(umax - 7) % bs],
rb->buf[(umax - 6) % bs], rb->buf[(umax - 5) % bs],
rb->buf[(umax - 4) % bs], rb->buf[(umax - 3) % bs],
rb->buf[(umax - 2) % bs], rb->buf[(umax - 1) % bs]));
#endif /* DEBUG_RB */
//if (rb->sizecrossed && mix != 0) {
if (umax > rb->bufsize && mix != 0) {
iov[0].iov_base = rb->buf + mix;
iov[0].iov_len = rb->bufsize - mix;
iov[1].iov_base = rb->buf;
iov[1].iov_len = mix;
return 2;
}
else {
iov[0].iov_base = rb->buf;
iov[0].iov_len = mix ? mix: rb->bufsize;
return 1;
}
}
#if defined(TEST) && TEST
#include <unistd.h>
#define WriteCS(f, s) write((f), ("" s ""), sizeof (s) - 1)
int main(int argc, char * argv[])
{
int rbsiz = 64;
if (argc > 1)
rbsiz = atoi(argv[1]);
CrRBuf * rb = crrbuf_create(rbsiz);
unsigned char buf[24];
while (1) {
int len = read(0, buf, sizeof buf);
if (len <= 0) exit(-len);
WriteCS(1, "input: "); write(1, buf, len);
#if 1
for (int i = 0; i < len; i++) {
// manual test haxes...
switch (buf[i]) {
case ',': buf[i] = '\r'; break;
// in next two, usecase is to change last character
case '.': buf[i+1] = '/'; break; // for \n avoidance
case '-': buf[i+1] = '\r'; break; // trailing \r
}
}
#endif
crrbuf_append(rb, buf, len);
WriteCS(1, "================\n");
struct iovec iov[2];
int iovcnt = crrbuf_data(rb, iov);
writev(1, iov, iovcnt);
WriteCS(1, ",,\n");
//WriteCS(1, ",,,,,,,,,,,,,,,,\n");
}
return 10; // not reached //
}
#endif
/*
* Local variables:
* mode: c
* c-file-style: "linux"
* tab-width: 8
* End:
*/