Lines Matching refs:shf

1 /*	$OpenBSD: shf.c,v 1.16 2013/04/19 17:36:09 millert Exp $	*/
30 __RCSID("$MirOS: src/bin/mksh/shf.c,v 1.102 2020/06/22 17:11:03 tg Exp $");
42 static int shf_fillbuf(struct shf *);
43 static int shf_emptybuf(struct shf *, int);
50 struct shf *
53 struct shf *shf;
60 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
61 shf->areap = ATEMP;
62 shf->buf = (unsigned char *)&shf[1];
63 shf->bsize = bsize;
64 shf->flags = SHF_ALLOCS;
70 afree(shf, shf->areap);
81 afree(shf, shf->areap);
91 return (shf_reopen(fd, sflags, shf));
127 /* Set up the shf structure for a file descriptor. Doesn't fail. */
128 struct shf *
129 shf_fdopen(int fd, int sflags, struct shf *shf)
136 if (shf) {
138 shf->buf = alloc(bsize, ATEMP);
141 shf->buf = NULL;
143 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
144 shf->buf = (unsigned char *)&shf[1];
147 shf->areap = ATEMP;
148 shf->fd = fd;
149 shf->rp = shf->wp = shf->buf;
150 shf->rnleft = 0;
151 shf->rbsize = bsize;
152 shf->wnleft = 0; /* force call to shf_emptybuf() */
153 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
154 shf->flags = sflags;
155 shf->errnosv = 0;
156 shf->bsize = bsize;
159 return (shf);
162 /* Set up an existing shf (and buffer) to use the given fd */
163 struct shf *
164 shf_reopen(int fd, int sflags, struct shf *shf)
171 if (!shf || !shf->buf || shf->bsize < bsize)
174 /* assumes shf->buf and shf->bsize already set up */
175 shf->fd = fd;
176 shf->rp = shf->wp = shf->buf;
177 shf->rnleft = 0;
178 shf->rbsize = bsize;
179 shf->wnleft = 0; /* force call to shf_emptybuf() */
180 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
181 shf->flags = (shf->flags & (SHF_ALLOCS | SHF_ALLOCB)) | sflags;
182 shf->errnosv = 0;
185 return (shf);
191 * bytes that can be written. If shf is not NULL, it is filled in and
192 * returned, if it is NULL, shf is allocated. If writing and buf is NULL
197 struct shf *
198 shf_sopen(char *buf, ssize_t bsize, int sflags, struct shf *shf)
205 if (!shf) {
206 shf = alloc(sizeof(struct shf), ATEMP);
209 shf->areap = ATEMP;
214 buf = alloc(bsize, shf->areap);
216 shf->fd = -1;
217 shf->buf = shf->rp = shf->wp = (unsigned char *)buf;
218 shf->rnleft = bsize;
219 shf->rbsize = bsize;
220 shf->wnleft = bsize - 1; /* space for a '\0' */
221 shf->wbsize = bsize;
222 shf->flags = sflags | SHF_STRING;
223 shf->errnosv = 0;
224 shf->bsize = bsize;
226 return (shf);
229 /* Flush and close file descriptor, free the shf structure */
231 shf_close(struct shf *shf)
235 if (shf->fd >= 0) {
236 ret = shf_flush(shf);
237 if (close(shf->fd) < 0)
240 if (shf->flags & SHF_ALLOCS)
241 afree(shf, shf->areap);
242 else if (shf->flags & SHF_ALLOCB)
243 afree(shf->buf, shf->areap);
250 shf_fdclose(struct shf *shf)
254 if (shf->fd >= 0) {
255 ret = shf_flush(shf);
256 if (close(shf->fd) < 0)
258 shf->rnleft = 0;
259 shf->rp = shf->buf;
260 shf->wnleft = 0;
261 shf->fd = -1;
269 * returns a pointer to the string and frees shf if it was allocated
273 shf_sclose(struct shf *shf)
275 unsigned char *s = shf->buf;
278 if (shf->flags & SHF_WR) {
279 shf->wnleft++;
280 shf_putc('\0', shf);
282 if (shf->flags & SHF_ALLOCS)
283 afree(shf, shf->areap);
292 shf_flush(struct shf *shf)
296 if (shf->flags & SHF_STRING)
297 rv = (shf->flags & SHF_WR) ? -1 : 0;
298 else if (shf->fd < 0)
300 else if (shf->flags & SHF_ERROR) {
301 errno = shf->errnosv;
303 } else if (shf->flags & SHF_READING) {
304 shf->flags &= ~(SHF_EOF | SHF_READING);
305 if (shf->rnleft > 0) {
306 if (lseek(shf->fd, (off_t)-shf->rnleft,
308 shf->flags |= SHF_ERROR;
309 shf->errnosv = errno;
312 shf->rnleft = 0;
313 shf->rp = shf->buf;
315 } else if (shf->flags & SHF_WRITING)
316 rv = shf_emptybuf(shf, 0);
326 shf_emptybuf(struct shf *shf, int flags)
330 if (!(shf->flags & SHF_STRING) && shf->fd < 0)
333 if (shf->flags & SHF_ERROR) {
334 errno = shf->errnosv;
338 if (shf->flags & SHF_READING) {
342 ret = shf_flush(shf);
343 shf->flags &= ~SHF_READING;
345 if (shf->flags & SHF_STRING) {
350 * SHF_ALLOCB is set... (changing the shf pointer could
353 if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
354 !(shf->flags & SHF_ALLOCB))
357 nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
358 shf->rp = nbuf + (shf->rp - shf->buf);
359 shf->wp = nbuf + (shf->wp - shf->buf);
360 shf->rbsize += shf->wbsize;
361 shf->wnleft += shf->wbsize;
362 shf->wbsize <<= 1;
363 shf->buf = nbuf;
365 if (shf->flags & SHF_WRITING) {
366 ssize_t n, ntowrite = shf->wp - shf->buf;
367 unsigned char *buf = shf->buf;
370 n = write(shf->fd, buf, ntowrite);
373 !(shf->flags & SHF_INTERRUPT))
375 shf->flags |= SHF_ERROR;
376 shf->errnosv = errno;
377 shf->wnleft = 0;
378 if (buf != shf->buf) {
383 memmove(shf->buf, buf,
385 shf->wp = shf->buf + ntowrite;
393 shf->wp = shf->buf;
394 shf->wnleft = 0;
395 shf->flags &= ~SHF_WRITING;
399 shf->wp = shf->buf;
400 shf->wnleft = shf->wbsize;
402 shf->flags |= SHF_WRITING;
409 shf_fillbuf(struct shf *shf)
413 if (shf->flags & SHF_STRING)
416 if (shf->fd < 0)
419 if (shf->flags & (SHF_EOF | SHF_ERROR)) {
420 if (shf->flags & SHF_ERROR)
421 errno = shf->errnosv;
425 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
428 shf->flags |= SHF_READING;
430 shf->rp = shf->buf;
432 n = blocking_read(shf->fd, (char *)shf->buf, shf->rbsize);
433 if (n < 0 && errno == EINTR && !(shf->flags & SHF_INTERRUPT))
438 shf->flags |= SHF_ERROR;
439 shf->errnosv = errno;
440 shf->rnleft = 0;
441 shf->rp = shf->buf;
444 if ((shf->rnleft = n) == 0)
445 shf->flags |= SHF_EOF;
450 * Read a buffer from shf. Returns the number of bytes read into buf, if
455 shf_read(char *buf, ssize_t bsize, struct shf *shf)
459 if (!(shf->flags & SHF_RD))
461 (unsigned int)shf->flags);
467 if (shf->rnleft == 0 &&
468 (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
470 ncopy = shf->rnleft;
473 memcpy(buf, shf->rp, ncopy);
476 shf->rp += ncopy;
477 shf->rnleft -= ncopy;
480 return (orig_bsize == bsize ? (shf_error(shf) ? -1 : 0) :
491 shf_getse(char *buf, ssize_t bsize, struct shf *shf)
497 if (!(shf->flags & SHF_RD))
499 (unsigned int)shf->flags);
507 if (shf->rnleft == 0) {
508 if (shf_fillbuf(shf) == -1)
510 if (shf->rnleft == 0) {
515 end = (unsigned char *)memchr((char *)shf->rp, '\n',
516 shf->rnleft);
517 ncopy = end ? end - shf->rp + 1 : shf->rnleft;
520 memcpy(buf, (char *) shf->rp, ncopy);
521 shf->rp += ncopy;
522 shf->rnleft -= ncopy;
536 int c = shf_getc(shf);
540 shf_ungetc(c, shf);
549 shf_getchar(struct shf *shf)
551 if (!(shf->flags & SHF_RD))
553 (unsigned int)shf->flags);
555 if (shf->rnleft == 0 && (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
557 --shf->rnleft;
558 return (ord(*shf->rp++));
566 shf_ungetc(int c, struct shf *shf)
568 if (!(shf->flags & SHF_RD))
570 (unsigned int)shf->flags);
572 if ((shf->flags & SHF_ERROR) || c == -1 ||
573 (shf->rp == shf->buf && shf->rnleft))
576 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
579 if (shf->rp == shf->buf)
580 shf->rp = shf->buf + shf->rbsize;
581 if (shf->flags & SHF_STRING) {
586 if ((int)(shf->rp[-1]) != c)
588 shf->flags &= ~SHF_EOF;
589 shf->rp--;
590 shf->rnleft++;
593 shf->flags &= ~SHF_EOF;
594 *--(shf->rp) = c;
595 shf->rnleft++;
604 shf_putchar(int c, struct shf *shf)
606 if (!(shf->flags & SHF_WR))
608 (unsigned int)shf->flags);
613 if (shf->flags & SHF_UNBUF) {
617 if (shf->fd < 0)
619 if (shf->flags & SHF_ERROR) {
620 errno = shf->errnosv;
623 while ((n = write(shf->fd, &cc, 1)) != 1)
626 !(shf->flags & SHF_INTERRUPT))
628 shf->flags |= SHF_ERROR;
629 shf->errnosv = errno;
634 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == -1)
636 shf->wnleft--;
637 *shf->wp++ = c;
648 shf_puts(const char *s, struct shf *shf)
653 return (shf_write(s, strlen(s), shf));
658 shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
662 if (!(shf->flags & SHF_WR))
664 (unsigned int)shf->flags);
670 if ((ncopy = shf->wnleft) &&
671 (shf->wp != shf->buf || nbytes < shf->wnleft)) {
674 memcpy(shf->wp, buf, ncopy);
677 shf->wp += ncopy;
678 shf->wnleft -= ncopy;
681 if (shf->flags & SHF_STRING) {
683 while (nbytes > shf->wnleft)
684 if (shf_emptybuf(shf, EB_GROW) == -1)
689 if (shf_emptybuf(shf, EB_GROW) == -1)
692 if (nbytes > shf->wbsize) {
694 if (shf->wbsize)
695 ncopy -= nbytes % shf->wbsize;
698 n = write(shf->fd, buf, ncopy);
701 !(shf->flags & SHF_INTERRUPT))
703 shf->flags |= SHF_ERROR;
704 shf->errnosv = errno;
705 shf->wnleft = 0;
720 memcpy(shf->wp, buf, nbytes);
721 shf->wp += nbytes;
722 shf->wnleft -= nbytes;
730 shf_fprintf(struct shf *shf, const char *fmt, ...)
736 n = shf_vfprintf(shf, fmt, args);
745 struct shf shf;
753 shf_sopen(buf, bsize, SHF_WR, &shf);
755 n = shf_vfprintf(&shf, fmt, args);
758 shf_sclose(&shf);
765 struct shf shf;
768 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
770 shf_vfprintf(&shf, fmt, args);
773 return (shf_sclose(&shf));
790 shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
809 shf_putc(c, shf);
1004 print_value_quoted(shf, s);
1036 shf_putc(*s, shf);
1041 shf_putc(*s, shf);
1046 shf_putc(*s, shf);
1057 shf_putc(c, shf);
1067 shf_putc(*s++, shf);
1071 shf_putc(c, shf);
1074 return (shf_error(shf) ? -1 : nwritten);
1079 shf_getc(struct shf *shf)
1081 return (shf_getc_i(shf));
1085 shf_putc(int c, struct shf *shf)
1087 return (shf_putc_i(c, shf));