File: tbuff.c

package info (click to toggle)
vile 9.6m-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,196 kB
  • ctags: 8,459
  • sloc: ansic: 90,876; lex: 9,514; sh: 3,223; cpp: 3,137; perl: 2,928; makefile: 785; awk: 276
file content (662 lines) | stat: -rw-r--r-- 11,884 bytes parent folder | download
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
 *	tbuff.c
 *
 *	Manage dynamic temporary buffers.
 *	Note that some temp-buffs are never freed, for speed
 *
 *	To do:	add 'tb_ins()' and 'tb_del()' to support cursor-level command
 *		editing.
 *
 * $Header: /usr/build/vile/vile/RCS/tbuff.c,v 1.68 2007/12/31 19:49:19 tom Exp $
 *
 */

#include "estruct.h"
#include "edef.h"

#define	NCHUNK	NLINE

#define ERROR_LEN 6		/* sizeof(ERROR) */

/*******(testing)************************************************************/
#if NO_LEAKS
typedef struct _tb_list {
    struct _tb_list *link;
    TBUFF *buff;
} TB_LIST;

static TB_LIST *all_tbuffs;

#define	AllocatedBuffer(q)	tb_remember(q)
#define	FreedBuffer(q)		tb_forget(q)

static void
tb_remember(TBUFF *p)
{
    TB_LIST *q;

    if ((q = typealloc(TB_LIST)) != 0) {
	q->buff = p;
	q->link = all_tbuffs;
	all_tbuffs = q;
    }
}

static void
tb_forget(TBUFF *p)
{
    TB_LIST *q, *r;

    beginDisplay();
    for (q = all_tbuffs, r = 0; q != 0; r = q, q = q->link) {
	if (q->buff == p) {
	    if (r != 0)
		r->link = q->link;
	    else
		all_tbuffs = q->link;
	    free(q);
	    break;
	}
    }
    endofDisplay();
}

void
tb_leaks(void)
{
    while (all_tbuffs != 0) {
	TBUFF *q = all_tbuffs->buff;
	FreedBuffer(q);
	tb_free(&q);
    }
}

#else
#define	AllocatedBuffer(q)
#define	FreedBuffer(q)
#endif

#if OPT_TRACE
static void
ValidateTBUFF(TBUFF *p, int lineno)
{
    int ok = TRUE;

    if (p != 0) {
	if (p->tb_errs) {
	    if ((p->tb_size != 0 || p->tb_used != 0) || (p->tb_data != 0))
		ok = FALSE;
	} else {
	    if ((p->tb_size != 0 || p->tb_used != 0) && (p->tb_data == 0))
		ok = FALSE;
	    if ((p->tb_size == 0 && p->tb_used == 0) && (p->tb_data != 0))
		ok = FALSE;
	}
	if (!ok) {
	    TRACE(("%s @%d: inconsistent TBUFF %p(errs %d, size %d, used %d, data %p)\n",
		   __FILE__, lineno,
		   p,
		   p->tb_errs,
		   (int) p->tb_size,
		   (int) p->tb_used,
		   p->tb_data));
	}
    }
}
#define valid_tbuff(p)		ValidateTBUFF(p, __LINE__)
#else
#define valid_tbuff(p)		/* nothing */
#endif /* OPT_TRACE */

/*******(initialization)*****************************************************/

/*
 * ensure that the given temp-buff has as much space as specified
 */
TBUFF *
tb_alloc(TBUFF **p, size_t n)
{
    TBUFF *q = *p;

    beginDisplay();
    valid_tbuff(q);

    /* any nonzero-size will do, but nonzero it must be */
    if (n < sizeof(TBUFF)) {
	n = sizeof(TBUFF);
    }

    if (q == 0) {
	if ((q = typealloc(TBUFF)) != 0) {
	    if ((q->tb_data = typeallocn(char, q->tb_size = n)) != 0) {
		q->tb_used = 0;
		q->tb_last = 0;
		q->tb_endc = esc_c;
		q->tb_data[0] = 0;	/* appease Purify */
		q->tb_errs = FALSE;
		AllocatedBuffer(q);
	    } else {
		tb_free(&q);
	    }
	}
    } else if (!isTB_ERRS(q) && (n >= q->tb_size || q->tb_data == 0)) {
	q->tb_data = typereallocn(char, q->tb_data, q->tb_size = (n * 2));
	if (q->tb_data == 0)
	    tb_free(&q);
    }
    endofDisplay();
    return (*p = q);
}

/*
 * (re)initialize a temp-buff
 */
TBUFF *
tb_init(TBUFF **p, int c)
{
    TBUFF *q = *p;
    if (q == 0)
	q = tb_alloc(p, NCHUNK);
    if (q != 0) {
	q->tb_used = 0;
	q->tb_last = 0;
	q->tb_endc = c;		/* code to return if no-more-data */
	q->tb_errs = FALSE;
    }
    return (*p = q);
}

/*
 * Set a temp-buff to error-state
 */
TBUFF *
tb_error(TBUFF **p)
{
    TBUFF *result;

    beginDisplay();
    if ((result = tb_init(p, EOS)) != 0) {
	result->tb_size = 0;
	result->tb_used = 0;
	FreeAndNull(result->tb_data);
	result->tb_errs = TRUE;
    }
    endofDisplay();
    return result;
}

/*
 * deallocate a temp-buff
 */
void
tb_free(TBUFF **p)
{
    TBUFF *q = *p;

    beginDisplay();
    valid_tbuff(q);
    if (q != 0) {
	FreedBuffer(q);
	FreeIfNeeded(q->tb_data);
	free(q);
    }
    *p = 0;
    endofDisplay();
}

/*******(storage)************************************************************/

/*
 * put a character c at the nth position of the temp-buff
 */
TBUFF *
tb_put(TBUFF **p, size_t n, int c)
{
    TBUFF *q = *p;

    if (!isTB_ERRS(q)) {
	if ((q = tb_alloc(p, n + 1)) != 0) {
	    q->tb_data[n] = (char) c;
	    q->tb_used = n + 1;
	}
    }
    valid_tbuff(q);
    return (*p = q);
}

#if VILE_NEEDED
/*
 * stuff the nth character into the temp-buff -- assumes space already there
 *  it's sort of the opposite of tb_peek
 */
void
tb_stuff(TBUFF *p, int c)
{
    if (p != 0) {
	if (p->tb_last < p->tb_used)
	    p->tb_data[p->tb_last] = c;
	else
	    p->tb_endc = c;
    }
}
#endif

/*
 * append a character to the temp-buff
 */
TBUFF *
tb_append(TBUFF **p, int c)
{
    TBUFF *q = *p;
    size_t n = (q != 0) ? q->tb_used : 0;

    return tb_put(p, n, c);
}

/*
 * insert a character into the temp-buff
 */
TBUFF *
tb_insert(TBUFF **p, size_t n, int c)
{
    TBUFF *q = *p;

    if (!isTB_ERRS(q)) {
	size_t m = tb_length(*p);

	q = tb_append(p, c);
	if (q != 0 && n < m) {
	    while (n < m) {
		q->tb_data[m] = q->tb_data[m - 1];
		m--;
	    }
	    q->tb_data[m] = (char) c;
	}
    }
    return (*p = q);
}

/*
 * Copy one temp-buff to another
 */
TBUFF *
tb_copy(TBUFF **d, TBUFF *s)
{
    TBUFF *p = *d;

    if (isTB_ERRS(p)) {
	p = tb_error(d);
    } else {
	if (s != 0) {
	    if ((p = tb_init(d, s->tb_endc)) != 0)
		p = tb_bappend(d, tb_values(s), tb_length(s));
	} else {
	    p = tb_init(d, esc_c);
	}
    }
    return (*d = p);
}

/*
 * append a binary data to the temp-buff
 */
TBUFF *
tb_bappend(TBUFF **p, const char *s, size_t len)
{
    TBUFF *q = *p;

    if (!isTB_ERRS(*p)) {
	size_t n = (q != 0) ? q->tb_used : 0;

	if (s == error_val) {
	    tb_error(&q);
	} else if ((q = tb_alloc(p, n + len)) != 0) {
	    if (len != 0) {
		if (q->tb_data + n != s)
		    memcpy(q->tb_data + n, s, len);
		q->tb_used = n + len;
	    }
	}
    }
    valid_tbuff(q);
    return q;
}

/*
 * append a string to the temp-buff
 */
TBUFF *
tb_sappend(TBUFF **p, const char *s)
{
    if (s != 0)
	(void) tb_bappend(p, s, strlen(s));
    return *p;
}

/*
 * append a string to the temp-buff, assume there may be a null on the end of
 * target which is removed.
 */
TBUFF *
tb_sappend0(TBUFF **p, const char *s)
{
    if (s != 0) {
	TBUFF *q = *p;
	if (q != 0
	    && !isTB_ERRS(q)
	    && q->tb_used > 0
	    && q->tb_data[q->tb_used - 1] == EOS) {
	    q->tb_used--;
	}
	tb_bappend(p, s, strlen(s));
	tb_append(p, EOS);
    }
    return *p;
}

/*
 * copy a string to the temp-buff, including a null
 */
TBUFF *
tb_scopy(TBUFF **p, const char *s)
{
    (void) tb_init(p, EOS);
    (void) tb_sappend(p, s);
    return tb_append(p, EOS);
}

/*
 * Construct a TBUFF from a null-terminated string, omitting its null.
 */
TBUFF *
tb_string(const char *s)
{
    TBUFF *p = 0;
    (void) tb_init(&p, EOS);
    return tb_bappend(&p, s, strlen(s));
}

/*******(retrieval)************************************************************/

/*
 * get the nth character from the temp-buff
 */
int
tb_get(TBUFF *p, size_t n)
{
    int c = esc_c;

    if (p != 0
	&& !isTB_ERRS(p))
	c = (n < p->tb_used) ? p->tb_data[n] : p->tb_endc;

    return char2int(c);
}

/*
 * undo the last 'tb_put'
 */
void
tb_unput(TBUFF *p)
{
    if (p != 0
	&& !isTB_ERRS(p)
	&& p->tb_used != 0)
	p->tb_used -= 1;
    valid_tbuff(p);
}

/*******(iterators)************************************************************/

#if VILE_NEEDED
/*
 * Reset the iteration-count
 */
void
tb_first(TBUFF *p)
{
    if (p != 0)
	p->tb_last = 0;
}
#endif

#if DISP_X11
/*
 * Returns true iff the iteration-count has not gone past the end of temp-buff.
 */
int
tb_more(TBUFF *p)
{
    return (p != 0 && !isTB_ERRS(p)) ? (p->tb_last < p->tb_used) : FALSE;
}

/*
 * get the next character from the temp-buff
 */
int
tb_next(TBUFF *p)
{
    if (p != 0 && !isTB_ERRS(p))
	return tb_get(p, p->tb_last++);
    return esc_c;
}
#endif

#if VILE_NEEDED
/*
 * undo a tb_next
 */
void
tb_unnext(TBUFF *p)
{
    if (p == 0 || isTB_ERRS(p))
	return;
    if (p->tb_last > 0)
	p->tb_last--;
    valid_tbuff(p);
}

/*
 * get the next character from the temp-buff w/o incrementing index
 */
int
tb_peek(TBUFF *p)
{
    if (p != 0 && !isTB_ERRS(p))
	return tb_get(p, p->tb_last);
    return esc_c;
}
#endif /* NEEDED */

/*******(evaluation)***********************************************************/
/*
 * Strip single- or double-quotes from the content, adjusting the size.
 */
TBUFF *
tb_dequote(TBUFF **p)
{
    char *value = tb_values(*p);

    TRACE2(("tb_dequote %s\n", tb_visible(*p)));
    if (value == 0) {
	TRACE2(("...empty\n"));
    } else if (value != error_val) {
	int escaped = FALSE;
	UINT delim = CharOf(value[0]);
	UINT j, k, ch;
	size_t have = tb_length(*p);
	size_t used = have;

	if (delim == SQUOTE) {
	    for (j = k = 0; k < have; ++k) {
		value[j] = value[k];
		if (CharOf(value[j]) == delim) {
		    --used;
		} else {
		    ++j;
		}
	    }
	    (*p)->tb_used = used;
	    TRACE2(("...tb_dequote 1: %s\n", tb_visible(*p)));
	} else if (delim == DQUOTE) {
	    for (j = 0, k = 1; k < have; ++k) {
		ch = CharOf(value[j] = value[k]);
		if (escaped) {
		    escaped = FALSE;
		    ++j;
		} else if (ch == BACKSLASH) {
		    escaped = TRUE;
		    --used;
		} else if (ch == delim) {
		    --used;
		} else {
		    ++j;
		}
	    }
	    (*p)->tb_used = used;
	    TRACE2(("...tb_dequote 2: %s\n", tb_visible(*p)));
	} else {
	    TRACE2(("...tb_dequote OOPS: %s\n", tb_visible(*p)));
	}
    }
    valid_tbuff(*p);
    return *p;
}

/*
 * Quote the content.  We prefer single-quotes since they're simpler to read.
 */
TBUFF *
tb_enquote(TBUFF **p)
{
    char *value = tb_values(*p);

    TRACE2(("tb_enquote %s\n", tb_visible(*p)));
    if (value == 0) {
	TRACE2(("...empty\n"));
    } else if (value != error_val && tb_length(*p)) {
	UINT j;
	size_t have = tb_length(*p) - 1;
	size_t need = 2 + have;
	int delim;

	/* decide which delimiter we can use */
	delim = SQUOTE;
	for (j = 0; j < have; ++j) {
	    if (value[j] == SQUOTE) {
		delim = DQUOTE;
		break;
	    }
	}

	/* if we can use single-quotes, there is no need for backslashes */
	if (delim == DQUOTE) {
	    for (j = 0; j < have; ++j) {
		if (value[j] == BACKSLASH || value[j] == DQUOTE)
		    ++need;
	    }
	}

	tb_alloc(p, need + 1);
	(*p)->tb_used = need + 1;

	value = tb_values(*p);
	value[need] = EOS;
	value[need - 1] = (char) delim;
	for (j = 0; j < have; ++j) {
	    size_t i = have - j - 1;
	    size_t k = need - j - 2;
	    UINT ch = CharOf(value[k] = value[i]);
	    if (delim == DQUOTE && (ch == DQUOTE || ch == BACKSLASH)) {
		--need;
		value[k - 1] = BACKSLASH;
	    }
	}
	value[0] = (char) delim;
	TRACE2(("...tb_enquote %s\n", tb_visible(*p)));
    }
    valid_tbuff(*p);
    return *p;
}

/*******(bulk-data)************************************************************/

/*
 * returns a pointer to data, assumes it is one long string
 */
char *
tb_values(TBUFF *p)
{
    char *result = 0;

    if (p != 0) {
	if (isTB_ERRS(p)) {
	    result = error_val;
	} else {
	    result = p->tb_data;
	}
    }
    valid_tbuff(p);
    return result;
}

/*
 * returns the length of the data
 */
size_t
tb_length(TBUFF *p)
{
    size_t result = 0;

    if (p != 0) {
	if (isTB_ERRS(p)) {
	    result = ERROR_LEN;
	} else if (p->tb_data != 0) {
	    result = p->tb_used;
	}
    }
    valid_tbuff(p);
    return result;
}

/*
 * returns the length of the data, assuming it is terminated by a trailing null
 * which we do not want to count.
 */
int
tb_length0(TBUFF *p)
{
    int result = (int) tb_length(p);

    /* FIXME: we should have an assertion here */
    if (result)
	--result;
    return result;
}

/*
 * Set the length of the buffer, increasing it if needed, and accounting for
 * errors.
 */
void
tb_setlen(TBUFF **p, int n)
{
    size_t len;

    if (p != 0) {
	if (!isTB_ERRS(*p)) {
	    if (n < 0) {
		char *value = tb_values(*p);
		if (value != 0)
		    len = strlen(value) + 1;
		else
		    len = 0;
	    } else {
		len = n;
		tb_alloc(p, len);
	    }
	    (*p)->tb_used = len;
	}
	valid_tbuff(*p);
    }
}