-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuf.c
898 lines (698 loc) · 17.2 KB
/
buf.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
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
/*
* Copyright (c) 2023 Logan Ryan McLintock
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Buffer module */
#include "toucanlib.h"
#define READ_BLOCK_SIZE BUFSIZ
#define INIT_BUF_SIZE 512
/* ###################################################################### */
struct ibuf *init_ibuf(size_t n)
{
struct ibuf *b = NULL;
if ((b = calloc(1, sizeof(struct ibuf))) == NULL)
mgoto(error);
if ((b->a = calloc(n, 1)) == NULL)
mgoto(error);
b->i = 0;
b->n = n;
return b;
error:
free_ibuf(b);
return NULL;
}
int free_ibuf(struct ibuf *b)
{
int ret = 0;
struct ibuf *t;
while (b != NULL) {
t = b->next;
if (b->nm != NULL)
free(b->nm);
if (b->fp != NULL && b->fp != stdin)
if (fclose(b->fp))
ret = GEN_ERROR; /* Continue */
free(b->a);
free(b);
b = t;
}
return ret;
}
static int grow_ibuf(struct ibuf *b, size_t will_use)
{
char *t;
size_t new_n;
if (aof(b->n, will_use, SIZE_MAX))
mreturn(GEN_ERROR);
new_n = b->n + will_use;
if (mof(new_n, 2, SIZE_MAX))
mreturn(GEN_ERROR);
new_n *= 2;
if ((t = realloc(b->a, new_n)) == NULL)
mreturn(GEN_ERROR);
b->a = t;
b->n = new_n;
return 0;
}
int unget_ch(struct ibuf *b, char ch)
{
if (b->i == b->n && grow_ibuf(b, 1))
mreturn(GEN_ERROR);
*(b->a + b->i) = ch;
++b->i;
return 0;
}
int unget_str(struct ibuf *b, const char *str)
{
size_t len, j;
char *p;
len = strlen(str);
if (!len)
return 0;
if (len > b->n - b->i && grow_ibuf(b, len))
mreturn(GEN_ERROR);
p = b->a + b->i + len - 1;
j = len;
while (j) {
*p = *str;
--p;
++str;
--j;
}
b->i += len;
return 0;
}
int unget_stream(struct ibuf **b, FILE *fp, const char *nm)
{
/*
* Creates a new struct head. *b can be NULL.
* Upon error, fp is closed by caller.
*/
struct ibuf *t = NULL;
if ((t = init_ibuf(INIT_BUF_SIZE)) == NULL)
mreturn(GEN_ERROR);
if ((t->nm = strdup(nm)) == NULL) {
free_ibuf(t);
mreturn(GEN_ERROR);
}
/* Success */
t->fp = fp;
t->rn = 1;
/* Link in front */
t->next = *b;
*b = t;
return 0;
}
int unget_file(struct ibuf **b, const char *fn)
{
FILE *fp = NULL;
if ((fp = fopen(fn, "rb")) == NULL)
mreturn(GEN_ERROR);
if (unget_stream(b, fp, fn)) {
fclose(fp);
mreturn(GEN_ERROR);
}
return 0;
}
int append_stream(struct ibuf **b, FILE *fp, const char *nm)
{
/* Links a new stream in at the tail of the list. *b can be NULL. */
struct ibuf *t = NULL;
struct ibuf *w = NULL;
if ((t = init_ibuf(INIT_BUF_SIZE)) == NULL)
mreturn(GEN_ERROR);
if ((t->nm = strdup(nm)) == NULL) {
free_ibuf(t);
mreturn(GEN_ERROR);
}
/* Success */
t->fp = fp;
t->rn = 1;
/* Link at end */
if (*b != NULL) {
w = *b;
while (w->next != NULL)
w = w->next;
w->next = t;
} else {
*b = t;
}
return 0;
}
int append_file(struct ibuf **b, const char *fn)
{
FILE *fp = NULL;
if ((fp = fopen(fn, "rb")) == NULL)
mreturn(GEN_ERROR);
if (append_stream(b, fp, fn)) {
fclose(fp);
mreturn(GEN_ERROR);
}
return 0;
}
int get_ch(struct ibuf **input, char *ch)
{
struct ibuf *t = NULL;
int x;
top:
if ((*input)->i) {
--(*input)->i;
*ch = *((*input)->a + (*input)->i);
return 0;
}
if ((*input)->fp != NULL) {
if ((x = getc((*input)->fp)) == EOF) {
if (ferror((*input)->fp))
mreturn(GEN_ERROR);
else if (feof((*input)->fp)) {
if ((*input)->next != NULL) {
t = (*input)->next;
/* Isolate old head */
(*input)->next = NULL;
if (free_ibuf(*input))
mreturn(GEN_ERROR);
/* Update head */
*input = t;
goto top;
} else {
/*
* Need to close the stream, as some systems block
* waiting for input after the first EOF is read,
* instead of automatically repeating the EOF status
* on each subsequent read after the first EOF char.
*/
if (fclose((*input)->fp))
mreturn(GEN_ERROR);
(*input)->fp = NULL;
return EOF;
}
}
} else {
if ((*input)->incr_rn) {
++(*input)->rn;
(*input)->incr_rn = 0;
}
if (x == '\n')
(*input)->incr_rn = 1;
*ch = x;
return 0;
}
}
return EOF;
}
int eat_whitespace(struct ibuf **input)
{
int r;
char ch;
while (1) {
r = get_ch(input, &ch);
if (r == GEN_ERROR)
mreturn(GEN_ERROR);
else if (r == EOF)
break;
if (!(isspace(ch) || ch == '\0')) {
if (unget_ch(*input, ch))
mreturn(GEN_ERROR);
break;
}
}
return 0;
}
int delete_to_nl(struct ibuf **input)
{
/* Delete to (and including) the next newline character */
int r;
char ch;
while (1) {
r = get_ch(input, &ch);
if (r == GEN_ERROR)
mreturn(GEN_ERROR);
else if (r == EOF)
break;
if (ch == '\n')
break;
}
return 0;
}
int eat_str_if_match(struct ibuf **input, const char *str)
{
/*
* Checks for str at the start of input and eats it if there is a match.
* Cannot check the buffer directly as stdin may not have been read yet.
* Returns MATCH, NO_MATCH, or GEN_ERROR.
* (EOF is considered as NO_MATCH).
*/
int r;
char x, ch;
size_t i;
if (str == NULL)
return NO_MATCH;
i = 0;
while (1) {
x = *(str + i);
if (x == '\0')
break;
r = get_ch(input, &ch);
if (r == GEN_ERROR)
mreturn(GEN_ERROR);
else if (r == EOF)
goto no_match;
if (x != ch) {
if (unget_ch(*input, ch))
mreturn(GEN_ERROR);
goto no_match;
}
++i;
}
return MATCH;
no_match:
/* Return the read characters */
while (i) {
if (unget_ch(*input, *(str + i - 1)))
mreturn(GEN_ERROR);
--i;
}
return NO_MATCH;
}
int get_word(struct ibuf **input, struct obuf *token, int interpret_hex)
{
int r;
char ch, type;
int second_ch;
token->i = 0;
if ((r = get_ch(input, &ch)))
return r;
if (put_ch(token, ch))
mreturn(GEN_ERROR);
if (isdigit(ch))
type = 'd'; /* Decimal (or octal) number */
else if (isalpha(ch) || ch == '_') /* First char cannot be a digit */
type = 'w'; /* Word (valid variable or macro name) */
else
goto end; /* Send a single char */
second_ch = 1;
while (1) {
r = get_ch(input, &ch);
if (r == GEN_ERROR)
mreturn(GEN_ERROR);
else if (r == EOF) /* Ignore, as not the first char */
goto end;
if (interpret_hex && second_ch && type == 'd'
&& (ch == 'x' || ch == 'X'))
type = 'h'; /* Hexadecimal number */
/* More of the same type. Words can include digits here. */
if ((type == 'd' && isdigit(ch))
|| (type == 'w' && (isalnum(ch) || ch == '_'))
|| (type == 'h' && (second_ch || isxdigit(ch)))) {
if (put_ch(token, ch))
mreturn(GEN_ERROR);
} else {
if (unget_ch(*input, ch))
mreturn(GEN_ERROR);
goto end;
}
second_ch = 0;
}
end:
if (put_ch(token, '\0')) { /* Terminate string */
mreturn(GEN_ERROR);
}
return 0;
}
/* ###################################################################### */
struct obuf *init_obuf(size_t n)
{
struct obuf *b = NULL;
if ((b = calloc(1, sizeof(struct obuf))) == NULL)
mgoto(error);
if ((b->a = calloc(n, 1)) == NULL)
mgoto(error);
b->i = 0;
b->n = n;
return b;
error:
free_obuf(b);
return NULL;
}
void free_obuf(struct obuf *b)
{
if (b != NULL) {
free(b->a);
free(b);
}
}
void free_obuf_exterior(struct obuf *b)
{
free(b);
}
static int grow_obuf(struct obuf *b, size_t will_use)
{
char *t;
size_t new_n;
if (aof(b->n, will_use, SIZE_MAX))
mreturn(GEN_ERROR);
new_n = b->n + will_use;
if (mof(new_n, 2, SIZE_MAX))
mreturn(GEN_ERROR);
new_n *= 2;
if ((t = realloc(b->a, new_n)) == NULL)
mreturn(GEN_ERROR);
b->a = t;
b->n = new_n;
return 0;
}
int put_ch(struct obuf *b, char ch)
{
if (b->i == b->n && grow_obuf(b, 1))
mreturn(GEN_ERROR);
*(b->a + b->i) = ch;
++b->i;
return 0;
}
int put_str(struct obuf *b, const char *str)
{
size_t i_backup = b->i;
char ch;
while ((ch = *str++) != '\0') {
if (b->i == b->n && grow_obuf(b, 1)) {
b->i = i_backup; /* Restore */
mreturn(GEN_ERROR);
}
*(b->a + b->i++) = ch;
}
return 0;
}
int put_mem(struct obuf *b, const char *mem, size_t mem_len)
{
if (mem_len > b->n - b->i && grow_obuf(b, mem_len))
mreturn(GEN_ERROR);
memcpy(b->a + b->i, mem, mem_len);
b->i += mem_len;
return 0;
}
int put_obuf(struct obuf *b, struct obuf *t)
{
/* Empties t onto the end of b */
if (put_mem(b, t->a, t->i))
mreturn(GEN_ERROR);
t->i = 0;
return 0;
}
int put_file(struct obuf *b, const char *fn)
{
int ret = GEN_ERROR;
FILE *fp = NULL;
size_t fs;
if (fn == NULL || *fn == '\0')
mreturn(GEN_ERROR);
if ((fp = fopen(fn, "rb")) == NULL) {
ret = GEN_ERROR;
mgoto(clean_up);
}
if (get_file_size(fn, &fs)) {
ret = GEN_ERROR;
mgoto(clean_up);
}
if (!fs) {
ret = GEN_ERROR;
mgoto(done);
}
if (fs > b->n - b->i && grow_obuf(b, fs)) {
ret = GEN_ERROR;
mgoto(clean_up);
}
if (fread(b->a + b->i, 1, fs, fp) != fs) {
ret = GEN_ERROR;
mgoto(clean_up);
}
b->i += fs;
done:
ret = 0;
clean_up:
if (fp != NULL)
if (fclose(fp))
ret = GEN_ERROR;
return ret;
}
int put_stream(struct obuf *b, FILE *fp)
{
size_t i_backup, rs;
if (fp == NULL)
mreturn(GEN_ERROR);
i_backup = b->i;
while (1) {
if (READ_BLOCK_SIZE > b->n - b->i && grow_obuf(b, READ_BLOCK_SIZE))
mgoto(error);
rs = fread(b->a + b->i, 1, READ_BLOCK_SIZE, fp);
b->i += rs;
if (rs != READ_BLOCK_SIZE) {
if (feof(fp) && !ferror(fp))
break;
else
mgoto(error);
}
}
return 0;
error:
b->i = i_backup;
return GEN_ERROR;
}
int write_obuf(struct obuf *b, const char *fn, int append)
{
/* Empties b to file fn */
FILE *fp;
if (fn == NULL || *fn == '\0')
mreturn(GEN_ERROR);
if ((fp = fopen_w(fn, append)) == NULL)
mreturn(GEN_ERROR);
if (fwrite(b->a, 1, b->i, fp) != b->i) {
fclose(fp);
mreturn(GEN_ERROR);
}
if (fclose(fp))
mreturn(GEN_ERROR);
b->i = 0;
return 0;
}
int flush_obuf(struct obuf *b, int tty_output)
{
size_t i;
char ch;
if (!b->i)
return 0;
if (tty_output) {
for (i = 0; i < b->i; ++i) {
ch = *(b->a + i);
if (isprint(ch) || ch == '\n')
putchar(ch);
else if (ch >= 1 && ch <= 26)
printf("^%c", 'A' + ch - 1);
else
switch (ch) {
case 0:
printf("^@");
break;
case 27:
printf("^[");
break;
case 28:
printf("^\\");
break;
case 29:
printf("^]");
break;
case 30:
printf("^^");
break;
case 31:
printf("^_");
break;
case 127:
printf("^?");
break;
default:
printf("\\x%02X", (unsigned char) ch);
break;
}
}
} else {
if (fwrite(b->a, 1, b->i, stdout) != b->i)
mreturn(GEN_ERROR);
}
if (fflush(stdout))
mreturn(GEN_ERROR);
b->i = 0;
return 0;
}
char *obuf_to_str(struct obuf **b)
{
char *str;
if (put_ch(*b, '\0'))
mreturn(NULL);
/* Success */
str = (*b)->a;
free(*b);
*b = NULL;
return str;
}
/* ###################################################################### */
struct lbuf *init_lbuf(size_t n)
{
struct lbuf *b = NULL;
if ((b = calloc(1, sizeof(struct lbuf))) == NULL)
mgoto(error);
if (mof(n, sizeof(long), SIZE_MAX))
mgoto(error);
if ((b->a = calloc(n, sizeof(long))) == NULL)
mgoto(error);
b->i = 0;
b->n = n;
return b;
error:
free_lbuf(b);
return NULL;
}
void free_lbuf(struct lbuf *b)
{
if (b != NULL) {
free(b->a);
free(b);
}
}
static int grow_lbuf(struct lbuf *b, size_t will_use)
{
long *t;
size_t new_n;
if (aof(b->n, will_use, SIZE_MAX))
mreturn(GEN_ERROR);
new_n = b->n + will_use;
if (mof(new_n, 2, SIZE_MAX))
mreturn(GEN_ERROR);
new_n *= 2;
if (mof(new_n, sizeof(long), SIZE_MAX))
mreturn(GEN_ERROR);
if ((t = realloc(b->a, new_n * sizeof(long))) == NULL)
mreturn(GEN_ERROR);
b->a = t;
b->n = new_n;
return 0;
}
int add_l(struct lbuf *b, long x)
{
if (b->i == b->n && grow_lbuf(b, 1))
mreturn(GEN_ERROR);
*(b->a + b->i) = x;
++b->i;
return 0;
}
/* ###################################################################### */
struct sbuf *init_sbuf(size_t n)
{
struct sbuf *b = NULL;
if ((b = calloc(1, sizeof(struct sbuf))) == NULL)
mgoto(error);
if (mof(n, sizeof(size_t), SIZE_MAX))
mgoto(error);
if ((b->a = calloc(n, sizeof(size_t))) == NULL)
mgoto(error);
b->i = 0;
b->n = n;
return b;
error:
free_sbuf(b);
return NULL;
}
void free_sbuf(struct sbuf *b)
{
if (b != NULL) {
free(b->a);
free(b);
}
}
static int grow_sbuf(struct sbuf *b, size_t will_use)
{
size_t *t;
size_t new_n;
if (aof(b->n, will_use, SIZE_MAX))
mreturn(GEN_ERROR);
new_n = b->n + will_use;
if (mof(new_n, 2, SIZE_MAX))
mreturn(GEN_ERROR);
new_n *= 2;
if (mof(new_n, sizeof(size_t), SIZE_MAX))
mreturn(GEN_ERROR);
if ((t = realloc(b->a, new_n * sizeof(size_t))) == NULL)
mreturn(GEN_ERROR);
b->a = t;
b->n = new_n;
return 0;
}
int add_s(struct sbuf *b, size_t x)
{
if (b->i == b->n && grow_sbuf(b, 1))
mreturn(GEN_ERROR);
*(b->a + b->i) = x;
++b->i;
return 0;
}
/* ###################################################################### */
struct pbuf *init_pbuf(size_t n)
{
struct pbuf *b = NULL;
if ((b = calloc(1, sizeof(struct pbuf))) == NULL)
mgoto(error);
if (mof(n, sizeof(void *), SIZE_MAX))
mgoto(error);
if ((b->a = calloc(n, sizeof(void *))) == NULL)
mgoto(error);
b->i = 0;
b->n = n;
return b;
error:
free_pbuf(b);
return NULL;
}
void free_pbuf(struct pbuf *b)
{
if (b != NULL) {
free(b->a);
free(b);
}
}
static int grow_pbuf(struct pbuf *b, size_t will_use)
{
void **t;
size_t new_n;
if (aof(b->n, will_use, SIZE_MAX))
mreturn(GEN_ERROR);
new_n = b->n + will_use;
if (mof(new_n, 2, SIZE_MAX))
mreturn(GEN_ERROR);
new_n *= 2;
if (mof(new_n, sizeof(void *), SIZE_MAX))
mreturn(GEN_ERROR);
if ((t = realloc(b->a, new_n * sizeof(void *))) == NULL)
mreturn(GEN_ERROR);
b->a = t;
b->n = new_n;
return 0;
}
int add_p(struct pbuf *b, void *x)
{
if (b->i == b->n && grow_pbuf(b, 1))
mreturn(GEN_ERROR);
*(b->a + b->i) = x;
++b->i;
return 0;
}