-
Notifications
You must be signed in to change notification settings - Fork 35
/
shf.c
2177 lines (2003 loc) · 52 KB
/
shf.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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $OpenBSD: shf.c,v 1.16 2013/04/19 17:36:09 millert Exp $ */
/*-
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2011,
* 2012, 2013, 2015, 2016, 2017, 2018, 2019, 2021,
* 2022, 2023
* mirabilos <[email protected]>
* Copyright (c) 2015
* Daniel Richard G. <[email protected]>
*
* Provided that these terms and disclaimer and all copyright notices
* are retained or reproduced in an accompanying document, permission
* is granted to deal in this work without restriction, including un-
* limited rights to use, publicly perform, distribute, sell, modify,
* merge, give away, or sublicence.
*
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
* the utmost extent permitted by applicable law, neither express nor
* implied; without malicious intent or gross negligence. In no event
* may a licensor, author or contributor be held liable for indirect,
* direct, other damage, loss, or other issues arising in any way out
* of dealing in the work, even if advised of the possibility of such
* damage or existence of a defect, except proven that it results out
* of said person's immediate fault when using the work as intended.
*-
* Use %zX instead of %p and floating point isn't supported at all.
*/
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.134 2023/03/19 23:31:29 tg Exp $");
/* flags to shf_emptybuf() */
#define EB_READSW 0x01 /* about to switch to reading */
#define EB_GROW 0x02 /* grow buffer if necessary (STRING+DYNAMIC) */
/*
* Replacement stdio routines. Stdio is too flakey on too many machines
* to be useful when you have multiple processes using the same underlying
* file descriptors.
*/
static int shf_fillbuf(struct shf *);
static int shf_emptybuf(struct shf *, int);
/*
* Open a file. First three args are for open(), last arg is flags for
* this package. Returns NULL if file could not be opened, or if a dup
* fails.
*/
struct shf *
shf_open(const char *name, int oflags, int mode, int sflags)
{
struct shf *shf;
ssize_t bsize =
/* at most 512 */
sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
int fd, eno;
/* Done before open so if alloca fails, fd won't be lost. */
shf = alloc1(sizeof(struct shf), bsize, ATEMP);
shf->areap = ATEMP;
shf->buf = (unsigned char *)&shf[1];
shf->bsize = bsize;
shf->flags = SHF_ALLOCS;
/* Rest filled in by reopen. */
fd = binopen3(name, oflags, mode);
if (fd < 0) {
eno = errno;
afree(shf, shf->areap);
errno = eno;
return (NULL);
}
if ((sflags & SHF_MAPHI) && fd < FDBASE) {
int nfd;
nfd = fcntl(fd, F_DUPFD, FDBASE);
eno = errno;
close(fd);
if (nfd < 0) {
afree(shf, shf->areap);
errno = eno;
return (NULL);
}
fd = nfd;
}
sflags &= ~SHF_ACCMODE;
sflags |= (oflags & O_ACCMODE) == O_RDONLY ? SHF_RD :
((oflags & O_ACCMODE) == O_WRONLY ? SHF_WR : SHF_RDWR);
return (shf_reopen(fd, sflags, shf));
}
/* helper function for shf_fdopen and shf_reopen */
/* pre-initio() *sflagsp=SHF_WR */
static void
shf_open_hlp(int fd, int *sflagsp, const char *where)
{
int sflags = *sflagsp;
/* use fcntl() to figure out correct read/write flags */
if (sflags & SHF_GETFL) {
int flags = fcntl(fd, F_GETFL, 0);
if (flags < 0)
/* will get an error on first read/write */
sflags |= SHF_RDWR;
else {
switch (flags & O_ACCMODE) {
case O_RDONLY:
sflags |= SHF_RD;
break;
case O_WRONLY:
sflags |= SHF_WR;
break;
case O_RDWR:
sflags |= SHF_RDWR;
break;
}
}
*sflagsp = sflags;
}
if (!(sflags & (SHF_RD | SHF_WR)))
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tbad_flags, where, sflags);
}
/* Set up the shf structure for a file descriptor. Doesn't fail. */
/* pre-initio() sflags=SHF_WR */
struct shf *
shf_fdopen(int fd, int sflags, struct shf *shf)
{
ssize_t bsize =
/* at most 512 */
(sflags & SHF_UNBUF) ? ((sflags & SHF_RD) ? 1 : 0) : SHF_BSIZE;
shf_open_hlp(fd, &sflags, "shf_fdopen");
if (shf) {
if (bsize) {
shf->buf = alloc(bsize, ATEMP);
sflags |= SHF_ALLOCB;
} else
shf->buf = NULL;
} else {
unsigned char *cp;
cp = alloc1(sizeof(struct shf), bsize, ATEMP);
shf = (void *)cp;
shf->buf = cp + sizeof(struct shf);
sflags |= SHF_ALLOCS;
}
shf->areap = ATEMP;
shf->fd = fd;
shf->rp = shf->wp = shf->buf;
shf->rnleft = 0;
shf->rbsize = bsize;
shf->wnleft = 0; /* force call to shf_emptybuf() */
shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
shf->flags = sflags;
shf->errnosv = 0;
shf->bsize = bsize;
if ((sflags & SHF_CLEXEC) && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
kwarnf0(KWF_INTERNAL | KWF_WARNING, Tcloexec_failed, "set", fd);
return (shf);
}
/* Set up an existing shf (and buffer) to use the given fd */
struct shf *
shf_reopen(int fd, int sflags, struct shf *shf)
{
ssize_t bsize =
/* at most 512 */
sflags & SHF_UNBUF ? (sflags & SHF_RD ? 1 : 0) : SHF_BSIZE;
shf_open_hlp(fd, &sflags, "shf_reopen");
if (!shf->buf || shf->bsize < bsize)
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tbad_buf, "shf_reopen", (size_t)shf->buf, shf->bsize);
/* assumes shf->buf and shf->bsize already set up */
shf->fd = fd;
shf->rp = shf->wp = shf->buf;
shf->rnleft = 0;
shf->rbsize = bsize;
shf->wnleft = 0; /* force call to shf_emptybuf() */
shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
shf->flags = (shf->flags & (SHF_ALLOCS | SHF_ALLOCB)) | sflags;
shf->errnosv = 0;
if ((sflags & SHF_CLEXEC) && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
kwarnf0(KWF_INTERNAL | KWF_WARNING, Tcloexec_failed, "set", fd);
return (shf);
}
/*
* Open a string for reading or writing. If reading, bsize is the number
* of bytes that can be read. If writing, bsize is the maximum number of
* bytes that can be written. If shf is not NULL, it is filled in and
* returned, if it is NULL, shf is allocated. If writing and buf is NULL
* and SHF_DYNAMIC is set, the buffer is allocated (if bsize > 0, it is
* used for the initial size). Doesn't fail.
* When writing, a byte is reserved for a trailing NUL - see shf_sclose().
*/
struct shf *
shf_sopen(char *buf, ssize_t bsize, int sflags, struct shf *shf)
{
if (!((sflags & SHF_RD) ^ (sflags & SHF_WR)))
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tbad_flags, "shf_sopen", sflags);
if (!shf) {
shf = alloc(sizeof(struct shf), ATEMP);
sflags |= SHF_ALLOCS;
}
shf->areap = ATEMP;
if (!buf && (sflags & SHF_WR) && (sflags & SHF_DYNAMIC)) {
if (bsize <= 0)
bsize = 64;
sflags |= SHF_ALLOCB;
buf = alloc(bsize, shf->areap);
}
shf->fd = -1;
shf->buf = shf->rp = shf->wp = (unsigned char *)buf;
shf->rnleft = bsize;
shf->rbsize = bsize;
shf->wnleft = bsize - 1; /* space for a '\0' */
shf->wbsize = bsize;
shf->flags = sflags | SHF_STRING;
shf->errnosv = 0;
shf->bsize = bsize;
return (shf);
}
/* Open a string for dynamic writing, using already-allocated buffer */
struct shf *
shf_sreopen(char *buf, ssize_t bsize, Area *ap, struct shf *oshf)
{
struct shf *shf;
shf = shf_sopen(buf, bsize, SHF_WR | SHF_DYNAMIC, oshf);
shf->areap = ap;
shf->flags |= SHF_ALLOCB;
return (shf);
}
/* Check whether the string can grow to take n bytes, close it up otherwise */
int
shf_scheck_grow(ssize_t n, struct shf *shf)
{
if (!(shf->flags & SHF_WR))
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tbad_flags, "shf_scheck", shf->flags);
/* if n < 0 we lose in the macro already */
/* nōn-string can always grow flushing */
if (!(shf->flags & SHF_STRING))
return (0);
while (shf->wnleft < n)
if (shf_emptybuf(shf, EB_GROW) == -1)
break;
if (shf->wnleft < n) {
/* block subsequent writes as we truncate here */
shf->wnleft = 0;
return (1);
}
return (0);
}
/* Flush and close file descriptor, free the shf structure */
int
shf_close(struct shf *shf)
{
int ret = 0;
if (shf->fd >= 0) {
ret = shf_flush(shf);
if (close(shf->fd) < 0)
ret = -1;
}
if (shf->flags & SHF_ALLOCS)
afree(shf, shf->areap);
else if (shf->flags & SHF_ALLOCB)
afree(shf->buf, shf->areap);
return (ret);
}
/* Flush and close file descriptor, don't free file structure */
int
shf_fdclose(struct shf *shf)
{
int ret = 0;
if (shf->fd >= 0) {
ret = shf_flush(shf);
if (close(shf->fd) < 0)
ret = -1;
shf->rnleft = 0;
shf->rp = shf->buf;
shf->wnleft = 0;
shf->fd = -1;
}
return (ret);
}
/*
* Close a string - if it was opened for writing, it is NUL terminated;
* returns a pointer to the string and frees shf if it was allocated
* (does not free string if it was allocated).
*/
char *
shf_sclose(struct shf *shf)
{
unsigned char *s = shf->buf;
/* NUL terminate */
if (shf->flags & SHF_WR)
*shf->wp = '\0';
if (shf->flags & SHF_ALLOCS)
afree(shf, shf->areap);
return ((char *)s);
}
/*
* Un-read what has been read but not examined, or write what has been
* buffered. Returns 0 for success, -1 for (write) error.
*/
int
shf_flush(struct shf *shf)
{
int rv = 0;
if (shf->flags & SHF_STRING)
rv = (shf->flags & SHF_WR) ? -1 : 0;
else if (shf->fd < 0)
kerrf(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_TWOMSG | KWF_NOERRNO,
"shf_flush", "no fd");
else if (shf->flags & SHF_ERROR) {
errno = shf->errnosv;
rv = -1;
} else if (shf->flags & SHF_READING) {
shf->flags &= ~(SHF_EOF | SHF_READING);
if (shf->rnleft > 0) {
if (lseek(shf->fd, (off_t)-shf->rnleft,
SEEK_CUR) == -1) {
shf->flags |= SHF_ERROR;
shf->errnosv = errno;
rv = -1;
}
shf->rnleft = 0;
shf->rp = shf->buf;
}
} else if (shf->flags & SHF_WRITING)
rv = shf_emptybuf(shf, 0);
return (rv);
}
/*
* Write out any buffered data. If currently reading, flushes the read
* buffer. Returns 0 for success, -1 for (write) error.
*/
static int
shf_emptybuf(struct shf *shf, int flags)
{
int ret = 0;
if (!(shf->flags & SHF_STRING) && shf->fd < 0)
kerrf(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_TWOMSG | KWF_NOERRNO,
"shf_emptybuf", "no fd");
if (shf->flags & SHF_ERROR) {
errno = shf->errnosv;
return (-1);
}
if (shf->flags & SHF_READING) {
if (flags & EB_READSW)
/* doesn't happen */
return (0);
ret = shf_flush(shf);
shf->flags &= ~SHF_READING;
}
if (shf->flags & SHF_STRING) {
size_t rp, wp;
/*
* Note that we assume SHF_ALLOCS is not set if
* SHF_ALLOCB is set... (changing the shf pointer could
* cause problems)
*/
if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
!(shf->flags & SHF_ALLOCB))
return (-1);
/* allocate more space for buffer */
rp = shf->rp - shf->buf;
wp = shf->wp - shf->buf;
shf->buf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
shf->rp = shf->buf + rp;
shf->wp = shf->buf + wp;
shf->rbsize += shf->wbsize;
shf->wnleft += shf->wbsize;
shf->wbsize <<= 1;
} else {
if (shf->flags & SHF_WRITING) {
ssize_t n, ntowrite = shf->wp - shf->buf;
unsigned char *buf = shf->buf;
while (ntowrite > 0) {
n = write(shf->fd, buf, ntowrite);
if (n < 0) {
if (errno == EINTR &&
!(shf->flags & SHF_INTERRUPT))
continue;
shf->flags |= SHF_ERROR;
shf->errnosv = errno;
shf->wnleft = 0;
if (buf != shf->buf) {
/*
* allow a second flush
* to work
*/
memmove(shf->buf, buf,
ntowrite);
shf->wp = shf->buf + ntowrite;
/* restore errno for caller */
errno = shf->errnosv;
}
return (-1);
}
buf += n;
ntowrite -= n;
}
if (flags & EB_READSW) {
shf->wp = shf->buf;
shf->wnleft = 0;
shf->flags &= ~SHF_WRITING;
return (0);
}
}
shf->wp = shf->buf;
shf->wnleft = shf->wbsize;
}
shf->flags |= SHF_WRITING;
return (ret);
}
/* Fill up a read buffer. Returns -1 for a read error, 0 otherwise. */
static int
shf_fillbuf(struct shf *shf)
{
ssize_t n;
if (shf->flags & SHF_STRING)
return (0);
if (shf->fd < 0)
kerrf(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_TWOMSG | KWF_NOERRNO,
"shf_fillbuf", "no fd");
if (shf->flags & (SHF_EOF | SHF_ERROR)) {
if (shf->flags & SHF_ERROR)
errno = shf->errnosv;
return (-1);
}
if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
return (-1);
shf->flags |= SHF_READING;
shf->rp = shf->buf;
while (/* CONSTCOND */ 1) {
n = blocking_read(shf->fd, (char *)shf->buf, shf->rbsize);
if (n < 0 && errno == EINTR && !(shf->flags & SHF_INTERRUPT))
continue;
break;
}
if (n < 0) {
shf->flags |= SHF_ERROR;
shf->errnosv = errno;
shf->rnleft = 0;
shf->rp = shf->buf;
return (-1);
}
if ((shf->rnleft = n) == 0)
shf->flags |= SHF_EOF;
return (0);
}
/*
* Read a buffer from shf. Returns the number of bytes read into buf, if
* no bytes were read, returns 0 if end of file was seen, -1 if a read
* error occurred.
*/
ssize_t
shf_read(char *buf, ssize_t bsize, struct shf *shf)
{
ssize_t ncopy, orig_bsize = bsize;
if (!(shf->flags & SHF_RD))
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tbad_flags, Tshf_read, shf->flags);
if (bsize <= 0)
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tbad_buf, Tshf_read, (size_t)buf, bsize);
while (bsize > 0) {
if (shf->rnleft == 0 &&
(shf_fillbuf(shf) == -1 || shf->rnleft == 0))
break;
ncopy = shf->rnleft;
if (ncopy > bsize)
ncopy = bsize;
memcpy(buf, shf->rp, ncopy);
buf += ncopy;
bsize -= ncopy;
shf->rp += ncopy;
shf->rnleft -= ncopy;
}
/* Note: fread(3S) returns 0 for errors - this doesn't */
return (orig_bsize == bsize ? (shf_error(shf) ? -1 : 0) :
orig_bsize - bsize);
}
/*
* Read up to a newline or -1. The newline is put in buf; buf is always
* NUL terminated. Returns NULL on read error or if nothing was read
* before end of file, returns a pointer to the NUL byte in buf
* otherwise.
*/
char *
shf_getse(char *buf, ssize_t bsize, struct shf *shf)
{
unsigned char *end;
ssize_t ncopy;
char *orig_buf = buf;
if (!(shf->flags & SHF_RD))
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tbad_flags, "shf_getse", shf->flags);
if (bsize <= 0)
return (NULL);
/* save room for NUL */
--bsize;
do {
if (shf->rnleft == 0) {
if (shf_fillbuf(shf) == -1)
return (NULL);
if (shf->rnleft == 0) {
*buf = '\0';
return (buf == orig_buf ? NULL : buf);
}
}
end = (unsigned char *)memchr((char *)shf->rp, '\n',
shf->rnleft);
ncopy = end ? end - shf->rp + 1 : shf->rnleft;
if (ncopy > bsize)
ncopy = bsize;
memcpy(buf, shf->rp, ncopy);
shf->rp += ncopy;
shf->rnleft -= ncopy;
buf += ncopy;
bsize -= ncopy;
#ifdef MKSH_WITH_TEXTMODE
if (buf > orig_buf + 1 && ord(buf[-2]) == ORD('\r') &&
ord(buf[-1]) == ORD('\n')) {
buf--;
bsize++;
buf[-1] = '\n';
}
#endif
} while (!end && bsize);
#ifdef MKSH_WITH_TEXTMODE
if (!bsize && ord(buf[-1]) == ORD('\r')) {
int c = shf_getc(shf);
if (ord(c) == ORD('\n'))
buf[-1] = '\n';
else if (c != -1)
shf_ungetc(c, shf);
}
#endif
*buf = '\0';
return (buf);
}
/* Returns the char read. Returns -1 for error and end of file. */
int
shf_getchar(struct shf *shf)
{
if (!(shf->flags & SHF_RD))
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tbad_flags, "shf_getchar", shf->flags);
if (shf->rnleft == 0 && (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
return (-1);
--shf->rnleft;
return (ord(*shf->rp++));
}
/*
* Put a character back in the input stream. Returns the character if
* successful, -1 if there is no room.
*/
int
shf_ungetc(int c, struct shf *shf)
{
if (!(shf->flags & SHF_RD))
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tbad_flags, "shf_ungetc", shf->flags);
if ((shf->flags & SHF_ERROR) || c == -1 ||
(shf->rp == shf->buf && shf->rnleft))
return (-1);
if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
return (-1);
if (shf->rp == shf->buf)
shf->rp = shf->buf + shf->rbsize;
if (shf->flags & SHF_STRING) {
/*
* Can unget what was read, but not something different;
* we don't want to modify a string.
*/
if ((int)(shf->rp[-1]) != c)
return (-1);
shf->flags &= ~SHF_EOF;
shf->rp--;
shf->rnleft++;
return (c);
}
shf->flags &= ~SHF_EOF;
*--(shf->rp) = c;
shf->rnleft++;
return (c);
}
/*
* Write a character. Returns the character if successful, -1 if the
* char could not be written.
*/
int
shf_putchar(int c, struct shf *shf)
{
if (!(shf->flags & SHF_WR))
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tbad_flags, "shf_putchar", shf->flags);
if (c == -1)
return (-1);
if (shf->flags & SHF_UNBUF) {
unsigned char cc = (unsigned char)c;
ssize_t n;
if (shf->fd < 0)
kerrf(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_TWOMSG | KWF_NOERRNO,
"shf_putchar", "no fd");
if (shf->flags & SHF_ERROR) {
errno = shf->errnosv;
return (-1);
}
while ((n = write(shf->fd, &cc, 1)) != 1)
if (n < 0) {
if (errno == EINTR &&
!(shf->flags & SHF_INTERRUPT))
continue;
shf->flags |= SHF_ERROR;
shf->errnosv = errno;
return (-1);
}
} else {
/* Flush deals with strings and sticky errors */
if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == -1)
return (-1);
shf->wnleft--;
*shf->wp++ = c;
}
return (c);
}
/*
* Write a string. Returns the length of the string if successful,
* less if truncated, and -1 if the string could not be written.
*/
ssize_t
shf_putsv(const char *s, struct shf *shf)
{
if (!s)
return (-1);
return (shf_write(s, strlen(s), shf));
}
/*
* Write a buffer. Returns nbytes if successful, less if truncated
* (outputting to string only), and -1 if there is an error.
*/
ssize_t
shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
{
ssize_t n, ncopy, orig_nbytes = nbytes;
if (!(shf->flags & SHF_WR))
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tbad_flags, Tshf_write, shf->flags);
if (nbytes < 0)
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tbad_buf, Tshf_write, (size_t)buf, nbytes);
/* don't buffer if buffer is empty and we're writing a large amount */
if ((ncopy = shf->wnleft) &&
(shf->wp != shf->buf || nbytes < shf->wnleft)) {
if (ncopy > nbytes)
ncopy = nbytes;
memcpy(shf->wp, buf, ncopy);
nbytes -= ncopy;
buf += ncopy;
shf->wp += ncopy;
shf->wnleft -= ncopy;
}
if (nbytes > 0) {
if (shf->flags & SHF_STRING) {
/* resize buffer until there's enough space left */
while (nbytes > shf->wnleft)
if (shf_emptybuf(shf, EB_GROW) == -1) {
/* truncate if possible */
if (shf->wnleft == 0)
return (-1);
nbytes = shf->wnleft;
break;
}
/* then write everything into the buffer */
} else {
/* flush deals with sticky errors */
if (shf_emptybuf(shf, EB_GROW) == -1)
return (-1);
/* write chunks larger than window size directly */
if (nbytes > shf->wbsize) {
ncopy = nbytes;
if (shf->wbsize)
ncopy -= nbytes % shf->wbsize;
nbytes -= ncopy;
while (ncopy > 0) {
n = write(shf->fd, buf, ncopy);
if (n < 0) {
if (errno == EINTR &&
!(shf->flags & SHF_INTERRUPT))
continue;
shf->flags |= SHF_ERROR;
shf->errnosv = errno;
shf->wnleft = 0;
/*
* Note: fwrite(3) returns 0
* for errors - this doesn't
*/
return (-1);
}
buf += n;
ncopy -= n;
}
}
/* ... and buffer the rest */
}
if (nbytes > 0) {
/* write remaining bytes to buffer */
memcpy(shf->wp, buf, nbytes);
shf->wp += nbytes;
shf->wnleft -= nbytes;
}
}
return (orig_nbytes);
}
ssize_t
shf_fprintf(struct shf *shf, const char *fmt, ...)
{
va_list args;
ssize_t n;
va_start(args, fmt);
n = shf_vfprintf(shf, fmt, args);
va_end(args);
return (n);
}
ssize_t
shf_snprintf(char *buf, ssize_t bsize, const char *fmt, ...)
{
struct shf shf;
va_list args;
ssize_t n;
if (!buf || bsize <= 0)
kerrf0(KWF_INTERNAL | KWF_ERR(0xFF) | KWF_NOERRNO,
Tbad_buf, "shf_snprintf", (size_t)buf, bsize);
shf_sopen(buf, bsize, SHF_WR, &shf);
va_start(args, fmt);
n = shf_vfprintf(&shf, fmt, args);
va_end(args);
/* NUL terminates */
shf_sclose(&shf);
return (n);
}
char *
shf_smprintf(const char *fmt, ...)
{
struct shf shf;
va_list args;
shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
va_start(args, fmt);
shf_vfprintf(&shf, fmt, args);
va_end(args);
/* NUL terminates */
return (shf_sclose(&shf));
}
/* pre-initio() */
char *
kslfmt(ksl number, kui flags, char *numbuf)
{
/* easy for positive number */
if (number >= 0)
return (kulfmt((kul)number, flags, numbuf));
/* negative signed quantity */
if (!IS(flags, FM_TYPE, FL_SGN)) {
/* uh-oh, output a signed quantity unsignedly */
return (kulfmt(mbiA_S2U(kul, ksl, number), flags, numbuf));
}
return (kulfmt(mbiA_S2M(kul, ksl, number), flags | FL_NEG, numbuf));
}
/* pre-initio() */
char *
kulfmt(kul number, kui flags, char *numbuf)
{
char *cp;
cp = numbuf + NUMBUFSZ;
*--cp = '\0';
switch (flags & FM_TYPE) {
case FL_OCT:
do {
*--cp = digits_lc[number & 07UL];
number >>= 3;
} while (number);
if (HAS(flags, FL_HASH) && ord(*cp) != ORD('0'))
*--cp = '0';
break;
case FL_HEX: {
const char *digits;
digits = HAS(flags, FL_UCASE) ? digits_uc : digits_lc;
do {
*--cp = digits[number & 0xFUL];
number >>= 4;
} while (number);
if (HAS(flags, FL_HASH)) {
*--cp = IS(flags, FL_UPPER, FL_UPPER) ? 'X' : 'x';
*--cp = '0';
}
break;
}
default:
do {
*--cp = digits_lc[number % 10UL];
number /= 10UL;
} while (number);
if (IS(flags, FM_TYPE, FL_SGN)) {
if (HAS(flags, FL_NEG))
*--cp = '-';
else if (HAS(flags, FL_PLUS))
*--cp = '+';
else if (HAS(flags, FL_BLANK))
*--cp = ' ';
}
break;
}
return (cp);
}
#ifdef MKSH_HAVE_HUGE
/* pre-initio() */
char *
ksHfmt(ksH number, kui flags, char *numbuf)
{
/* easy for positive number */
if (number >= 0)
return (kuHfmt((kuH)number, flags, numbuf));
/* negative signed quantity */
if (!IS(flags, FM_TYPE, FL_SGN)) {
/* uh-oh, output a signed quantity unsignedly */
return (kuHfmt(mbiA_S2U(kuH, ksH, number), flags, numbuf));
}
return (kuHfmt(mbiA_S2M(kuH, ksH, number), flags | FL_NEG, numbuf));
}
/* pre-initio() */
char *
kuHfmt(kuH number, kui flags, char *numbuf)
{
char *cp;
cp = numbuf + NUMBUFSZ;
*--cp = '\0';
switch (flags & FM_TYPE) {
case FL_OCT:
do {
*--cp = digits_lc[number & 07UL];
number >>= 3;
} while (number);
if (HAS(flags, FL_HASH) && ord(*cp) != ORD('0'))
*--cp = '0';
break;
case FL_HEX: {
const char *digits;
digits = HAS(flags, FL_UCASE) ? digits_uc : digits_lc;
do {
*--cp = digits[number & 0xFUL];
number >>= 4;
} while (number);
if (HAS(flags, FL_HASH)) {
*--cp = IS(flags, FL_UPPER, FL_UPPER) ? 'X' : 'x';
*--cp = '0';
}
break;
}
default:
do {
*--cp = digits_lc[number % 10UL];
number /= 10UL;
} while (number);
if (IS(flags, FM_TYPE, FL_SGN)) {
if (HAS(flags, FL_NEG))
*--cp = '-';
else if (HAS(flags, FL_PLUS))
*--cp = '+';
else if (HAS(flags, FL_BLANK))
*--cp = ' ';
}
break;
}
return (cp);
}
#endif
ssize_t
shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
{
char numbuf[NUMBUFSZ];
const char *s;
char c;
int tmp = 0, flags;
size_t field, precision, len;
/* this stuff for dealing with the buffer */
ssize_t nwritten = 0;
/* for width determination */
const char *lp, *np;
#define VA(type) va_arg(args, type)
if (!fmt)
return (0);
while ((c = *fmt++)) {
if (c != '%') {
shf_putc(c, shf);
nwritten++;
continue;
}
/*
* This will accept flags/fields in any order - not just
* the order specified in printf(3), but this is the way