Home | History | Annotate | Download | only in src

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.97 2018/01/14 01:28:16 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;
535 int c = shf_getc(shf);
539 shf_ungetc(c, shf);
548 shf_getchar(struct shf *shf)
550 if (!(shf->flags & SHF_RD))
552 (unsigned int)shf->flags);
554 if (shf->rnleft == 0 && (shf_fillbuf(shf) == -1 || shf->rnleft == 0))
556 --shf->rnleft;
557 return (ord(*shf->rp++));
565 shf_ungetc(int c, struct shf *shf)
567 if (!(shf->flags & SHF_RD))
569 (unsigned int)shf->flags);
571 if ((shf->flags & SHF_ERROR) || c == -1 ||
572 (shf->rp == shf->buf && shf->rnleft))
575 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == -1)
578 if (shf->rp == shf->buf)
579 shf->rp = shf->buf + shf->rbsize;
580 if (shf->flags & SHF_STRING) {
585 if ((int)(shf->rp[-1]) != c)
587 shf->flags &= ~SHF_EOF;
588 shf->rp--;
589 shf->rnleft++;
592 shf->flags &= ~SHF_EOF;
593 *--(shf->rp) = c;
594 shf->rnleft++;
603 shf_putchar(int c, struct shf *shf)
605 if (!(shf->flags & SHF_WR))
607 (unsigned int)shf->flags);
612 if (shf->flags & SHF_UNBUF) {
616 if (shf->fd < 0)
618 if (shf->flags & SHF_ERROR) {
619 errno = shf->errnosv;
622 while ((n = write(shf->fd, &cc, 1)) != 1)
625 !(shf->flags & SHF_INTERRUPT))
627 shf->flags |= SHF_ERROR;
628 shf->errnosv = errno;
633 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == -1)
635 shf->wnleft--;
636 *shf->wp++ = c;
647 shf_puts(const char *s, struct shf *shf)
652 return (shf_write(s, strlen(s), shf));
657 shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
661 if (!(shf->flags & SHF_WR))
663 (unsigned int)shf->flags);
669 if ((ncopy = shf->wnleft) &&
670 (shf->wp != shf->buf || nbytes < shf->wnleft)) {
673 memcpy(shf->wp, buf, ncopy);
676 shf->wp += ncopy;
677 shf
680 if (shf->flags & SHF_STRING) {
682 while (nbytes > shf->wnleft)
683 if (shf_emptybuf(shf, EB_GROW) == -1)
688 if (shf_emptybuf(shf, EB_GROW) == -1)
691 if (nbytes > shf->wbsize) {
693 if (shf->wbsize)
694 ncopy -= nbytes % shf->wbsize;
697 n = write(shf->fd, buf, ncopy);
700 !(shf->flags & SHF_INTERRUPT))
702 shf->flags |= SHF_ERROR;
703 shf->errnosv = errno;
704 shf->wnleft = 0;
719 memcpy(shf->wp, buf, nbytes);
720 shf->wp += nbytes;
721 shf->wnleft -= nbytes;
729 shf_fprintf(struct shf *shf, const char *fmt, ...)
735 n = shf_vfprintf(shf, fmt, args);
744 struct shf shf;
752 shf_sopen(buf, bsize, SHF_WR, &shf);
754 n = shf_vfprintf(&shf, fmt, args);
757 shf_sclose(&shf);
764 struct shf shf;
767 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
769 shf_vfprintf(&shf, fmt, args);
772 return (shf_sclose(&shf));
789 shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
808 shf_putc(c, shf);
1003 print_value_quoted(shf, s);
1035 shf_putc(*s, shf);
1040 shf_putc(*s, shf);
1045 shf_putc(*s, shf);
1056 shf_putc(c, shf);
1066 shf_putc(*s++, shf);
1070 shf_putc(c, shf);
1073 return (shf_error(shf) ? -1 : nwritten);
1078 shf_getc(struct shf *shf)
1080 return (shf_getc_i(shf));
1084 shf_putc(int c, struct shf *shf)
1086 return (shf_putc_i(c, shf));