-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFLEXBUF.C
400 lines (358 loc) · 6.93 KB
/
FLEXBUF.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* Copyright (c) 1990,1991,1992 Chris and John Downey */
#ifndef lint
static char *sccsid = "@(#)flexbuf.c 2.2 (Chris & John Downey) 8/27/92";
#endif
/***
* program name:
xvi
* function:
PD version of UNIX "vi" editor, with extensions.
* module name:
flexbuf.c
* module function:
Routines for Flexbufs (variable-length FIFO queues).
Some of the access routines are implemented as macros in xvi.h.
* history:
STEVIE - ST Editor for VI Enthusiasts, Version 3.10
Originally by Tim Thompson (twitch!tjt)
Extensive modifications by Tony Andrews (onecom!wldrdg!tony)
Heavily modified by Chris & John Downey
***/
#include "xvi.h"
#define FLEXEXTRA 64
/*
* Append a single character to a Flexbuf. Return FALSE if we've run
* out of space.
*
* Note that the f->fxb_chars array is not necessarily null-terminated.
*/
bool_t
flexaddch(f, ch)
register Flexbuf *f;
int ch;
{
if (flexempty(f))
f->fxb_rcnt = f->fxb_wcnt = 0;
if (f->fxb_wcnt >= f->fxb_max) {
if (f->fxb_max == 0) {
if ((f->fxb_chars = alloc(FLEXEXTRA)) == NULL) {
return FALSE;
} else {
f->fxb_max = FLEXEXTRA;
}
} else {
unsigned newsize = f->fxb_wcnt + FLEXEXTRA;
if ((f->fxb_chars = realloc(f->fxb_chars, newsize)) == NULL) {
f->fxb_wcnt = f->fxb_max = 0;
return FALSE;
} else {
f->fxb_max = newsize;
}
}
}
f->fxb_chars[f->fxb_wcnt++] = ch;
return TRUE;
}
/*
* Return contents of a Flexbuf as a null-terminated string.
*/
char *
flexgetstr(f)
Flexbuf *f;
{
if (!flexempty(f) && flexaddch(f, '\0')) {
--f->fxb_wcnt;
return &f->fxb_chars[f->fxb_rcnt];
} else {
return "";
}
}
/*
* Remove the first character from a Flexbuf and return it.
*/
int
flexpopch(f)
Flexbuf *f;
{
return flexempty(f) ?
0 : (unsigned char) f->fxb_chars[f->fxb_rcnt++];
}
/*
* Free storage belonging to a Flexbuf.
*/
void
flexdelete(f)
Flexbuf *f;
{
if (f->fxb_max > 0) {
(void) free(f->fxb_chars);
f->fxb_wcnt = f->fxb_max = 0;
}
}
/*
* The following routines provide for appending formatted data to a
* Flexbuf using a subset of the format specifiers accepted by
* printf(3). The specifiers we understand are currently
*
* %c %d %ld %lu %s %u
*
* Field width, precision & left justification can also be specified
* as for printf().
*
* The main advantage of this is that we don't have to worry about the
* end of the destination array being overwritten, as we do with
* sprintf(3).
*/
static unsigned width;
static unsigned prec;
static bool_t ljust;
/*
* Append a string to a Flexbuf, truncating the string & adding filler
* characters as specified by the width, prec & ljust variables.
*
* The precision specifier gives the maximum field width.
*/
static bool_t
strformat(f, p)
Flexbuf *f;
register char *p;
{
register int c;
if (width != 0 && !ljust) {
register unsigned len;
len = strlen(p);
if (prec != 0 && prec < len)
len = prec;
while (width > len) {
if (!flexaddch(f, ' '))
return FALSE;
--width;
}
}
while ((c = *p++) != '\0') {
if (!flexaddch(f, c))
return FALSE;
if (width != 0)
--width;
if (prec != 0) {
if (--prec == 0) {
break;
}
}
}
while (width != 0) {
if (!flexaddch(f, ' '))
return FALSE;
--width;
}
return TRUE;
}
/*
* Given a binary long integer, convert it to a decimal number &
* append it to the end of a Flexbuf.
*
* The precision specifier gives the minimum number of decimal digits.
*/
static bool_t
numformat(f, n, uflag)
Flexbuf *f;
long n;
bool_t uflag;
{
register char *s;
register unsigned len;
if (n == 0) {
/*
* Special case.
*/
s = "0";
len = 1;
} else {
static char dstr[sizeof (long) * 3 + 2];
register unsigned long un;
int neg;
* (s = &dstr[sizeof dstr - 1]) = '\0';
if (!uflag && n < 0) {
neg = 1;
un = -n;
} else {
neg = 0;
un = n;
}
while (un > 0 && s > &dstr[1]) {
*--s = (char)((un % 10) + '0');
un /= 10;
}
if (neg)
*--s = '-';
len = &dstr[sizeof dstr - 1] - s;
}
while (width > len && width > prec) {
if (!flexaddch(f, ' '))
return FALSE;
--width;
}
while (prec > len) {
if (!flexaddch(f, '0'))
return FALSE;
--prec;
if (width > 0)
--width;
}
prec = 0;
return strformat(f, s);
}
/*
* Main formatting routine.
*/
bool_t
vformat
#ifdef __STDC__
(Flexbuf *f, register char *format, register va_list argp)
#else
(f, format, argp)
Flexbuf *f;
register char *format;
register va_list argp;
#endif
{
register int c;
while ((c = *format++) != '\0') {
if (c == '%') {
static char cstr[2];
bool_t lnflag;
bool_t zflag;
lnflag = FALSE;
ljust = FALSE;
width = 0;
prec = 0;
zflag = FALSE;
if ((c = *format++) == '-') {
ljust = TRUE;
c = *format++;
}
/*
* Get width specifier.
*/
if (c == '0') {
if (ljust)
/*
* It doesn't make sense to
* have a left-justified
* numeric field with zero
* padding.
*/
return FALSE;
zflag = TRUE;
c = *format++;
}
while (c && is_digit(c)) {
if (width != 0)
width *= 10;
width += (c - '0');
c = *format++;
}
if (c == '.') {
/*
* Get precision specifier.
*/
while ((c = *format++) != '\0' && is_digit(c)) {
if (prec != 0)
prec *= 10;
prec += (c - '0');
}
}
switch (c) {
case '%':
cstr[0] = c;
if (!strformat(f, cstr))
return FALSE;
continue;
case 'c':
cstr[0] = va_arg(argp, int);
if (!strformat(f, cstr))
return FALSE;
continue;
case 'l':
switch (c = *format++) {
case 'd':
case 'u':
lnflag = TRUE;
break;
default:
/*
* Syntax error.
*/
return FALSE;
}
/* fall through ... */
case 'd':
case 'u':
{
long n;
if (lnflag)
{
n = (c == 'u' ?
(long) va_arg(argp, unsigned long) :
va_arg(argp, long));
} else {
n = (c == 'u' ?
(long) va_arg(argp, unsigned int) :
(long) va_arg(argp, int));
}
/*
* For integers, the precision
* specifier gives the minimum number
* of decimal digits.
*/
if (zflag && prec < width) {
prec = width;
width = 0;
}
if (!numformat(f, n, (c == 'u')))
return FALSE;
continue;
}
case 's':
{
char *sp;
if ((sp = va_arg(argp, char *)) == NULL ||
!strformat(f, sp)) {
return FALSE;
}
continue;
}
default:
/*
* Syntax error.
*/
return FALSE;
}
} else if (!flexaddch(f, c)) {
return FALSE;
}
}
return TRUE;
}
/*
* Front end callable with a variable number of arguments.
*/
/*VARARGS2*/
bool_t
lformat
#ifdef __STDC__
(Flexbuf *f, char *format, ...)
#else
(f, format, va_alist)
Flexbuf *f;
char *format;
va_dcl
#endif
{
va_list argp;
bool_t retval;
VA_START(argp, format);
retval = vformat(f, format, argp);
va_end(argp);
return retval;
}