Lines Matching defs:shf
1 /* $OpenBSD: shf.c,v 1.15 2006/04/02 00:48:33 deraadt Exp $ */
27 __RCSID("$MirOS: src/bin/mksh/shf.c,v 1.56 2013/01/01 03:32:44 tg Exp $");
39 static int shf_fillbuf(struct shf *);
40 static int shf_emptybuf(struct shf *, int);
47 struct shf *
50 struct shf *shf;
57 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
58 shf->areap = ATEMP;
59 shf->buf = (unsigned char *)&shf[1];
60 shf->bsize = bsize;
61 shf->flags = SHF_ALLOCS;
66 afree(shf, shf->areap);
75 afree(shf, shf->areap);
84 return (shf_reopen(fd, sflags, shf));
120 /* Set up the shf structure for a file descriptor. Doesn't fail. */
121 struct shf *
122 shf_fdopen(int fd, int sflags, struct shf *shf)
129 if (shf) {
131 shf->buf = alloc(bsize, ATEMP);
134 shf->buf = NULL;
136 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
137 shf->buf = (unsigned char *)&shf[1];
140 shf->areap = ATEMP;
141 shf->fd = fd;
142 shf->rp = shf->wp = shf->buf;
143 shf->rnleft = 0;
144 shf->rbsize = bsize;
145 shf->wnleft = 0; /* force call to shf_emptybuf() */
146 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
147 shf->flags = sflags;
148 shf->errnosv = 0;
149 shf->bsize = bsize;
152 return (shf);
155 /* Set up an existing shf (and buffer) to use the given fd */
156 struct shf *
157 shf_reopen(int fd, int sflags, struct shf *shf)
164 if (!shf || !shf->buf || shf->bsize < bsize)
165 internal_errorf("%s: %s", "shf_reopen", "bad shf/buf/bsize");
167 /* assumes shf->buf and shf->bsize already set up */
168 shf->fd = fd;
169 shf->rp = shf->wp = shf->buf;
170 shf->rnleft = 0;
171 shf->rbsize = bsize;
172 shf->wnleft = 0; /* force call to shf_emptybuf() */
173 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
174 shf->flags = (shf->flags & (SHF_ALLOCS | SHF_ALLOCB)) | sflags;
175 shf->errnosv = 0;
178 return (shf);
184 * bytes that can be written. If shf is not NULL, it is filled in and
185 * returned, if it is NULL, shf is allocated. If writing and buf is NULL
190 struct shf *
191 shf_sopen(char *buf, ssize_t bsize, int sflags, struct shf *shf)
197 if (!shf) {
198 shf = alloc(sizeof(struct shf), ATEMP);
201 shf->areap = ATEMP;
206 buf = alloc(bsize, shf->areap);
208 shf->fd = -1;
209 shf->buf = shf->rp = shf->wp = (unsigned char *)buf;
210 shf->rnleft = bsize;
211 shf->rbsize = bsize;
212 shf->wnleft = bsize - 1; /* space for a '\0' */
213 shf->wbsize = bsize;
214 shf->flags = sflags | SHF_STRING;
215 shf->errnosv = 0;
216 shf->bsize = bsize;
218 return (shf);
221 /* Flush and close file descriptor, free the shf structure */
223 shf_close(struct shf *shf)
227 if (shf->fd >= 0) {
228 ret = shf_flush(shf);
229 if (close(shf->fd) < 0)
232 if (shf->flags & SHF_ALLOCS)
233 afree(shf, shf->areap);
234 else if (shf->flags & SHF_ALLOCB)
235 afree(shf->buf, shf->areap);
242 shf_fdclose(struct shf *shf)
246 if (shf->fd >= 0) {
247 ret = shf_flush(shf);
248 if (close(shf->fd) < 0)
250 shf->rnleft = 0;
251 shf->rp = shf->buf;
252 shf->wnleft = 0;
253 shf->fd = -1;
261 * returns a pointer to the string and frees shf if it was allocated
265 shf_sclose(struct shf *shf)
267 unsigned char *s = shf->buf;
270 if (shf->flags & SHF_WR) {
271 shf->wnleft++;
272 shf_putc('\0', shf);
274 if (shf->flags & SHF_ALLOCS)
275 afree(shf, shf->areap);
284 shf_flush(struct shf *shf)
286 if (shf->flags & SHF_STRING)
287 return ((shf->flags & SHF_WR) ? EOF : 0);
289 if (shf->fd < 0)
292 if (shf->flags & SHF_ERROR) {
293 errno = shf->errnosv;
297 if (shf->flags & SHF_READING) {
298 shf->flags &= ~(SHF_EOF | SHF_READING);
299 if (shf->rnleft > 0) {
300 lseek(shf->fd, (off_t)-shf->rnleft, SEEK_CUR);
301 shf->rnleft = 0;
302 shf->rp = shf->buf;
305 } else if (shf->flags & SHF_WRITING)
306 return (shf_emptybuf(shf, 0));
316 shf_emptybuf(struct shf *shf, int flags)
320 if (!(shf->flags & SHF_STRING) && shf->fd < 0)
323 if (shf->flags & SHF_ERROR) {
324 errno = shf->errnosv;
328 if (shf->flags & SHF_READING) {
332 ret = shf_flush(shf);
333 shf->flags &= ~SHF_READING;
335 if (shf->flags & SHF_STRING) {
340 * SHF_ALLOCB is set... (changing the shf pointer could
343 if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
344 !(shf->flags & SHF_ALLOCB))
347 nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
348 shf->rp = nbuf + (shf->rp - shf->buf);
349 shf->wp = nbuf + (shf->wp - shf->buf);
350 shf->rbsize += shf->wbsize;
351 shf->wnleft += shf->wbsize;
352 shf->wbsize <<= 1;
353 shf->buf = nbuf;
355 if (shf->flags & SHF_WRITING) {
356 ssize_t n, ntowrite = shf->wp - shf->buf;
357 unsigned char *buf = shf->buf;
360 n = write(shf->fd, buf, ntowrite);
363 !(shf->flags & SHF_INTERRUPT))
365 shf->flags |= SHF_ERROR;
366 shf->errnosv = errno;
367 shf->wnleft = 0;
368 if (buf != shf->buf) {
373 memmove(shf->buf, buf,
375 shf->wp = shf->buf + ntowrite;
383 shf->wp = shf->buf;
384 shf->wnleft = 0;
385 shf->flags &= ~SHF_WRITING;
389 shf->wp = shf->buf;
390 shf->wnleft = shf->wbsize;
392 shf->flags |= SHF_WRITING;
399 shf_fillbuf(struct shf *shf)
403 if (shf->flags & SHF_STRING)
406 if (shf->fd < 0)
409 if (shf->flags & (SHF_EOF | SHF_ERROR)) {
410 if (shf->flags & SHF_ERROR)
411 errno = shf->errnosv;
415 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
418 shf->flags |= SHF_READING;
420 shf->rp = shf->buf;
422 n = blocking_read(shf->fd, (char *)shf->buf, shf->rbsize);
423 if (n < 0 && errno == EINTR && !(shf->flags & SHF_INTERRUPT))
428 shf->flags |= SHF_ERROR;
429 shf->errnosv = errno;
430 shf->rnleft = 0;
431 shf->rp = shf->buf;
434 if ((shf->rnleft = n) == 0)
435 shf->flags |= SHF_EOF;
440 * Read a buffer from shf. Returns the number of bytes read into buf, if
445 shf_read(char *buf, ssize_t bsize, struct shf *shf)
449 if (!(shf->flags & SHF_RD))
450 internal_errorf("%s: flags 0x%X", "shf_read", shf->flags);
456 if (shf->rnleft == 0 &&
457 (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
459 ncopy = shf->rnleft;
462 memcpy(buf, shf->rp, ncopy);
465 shf->rp += ncopy;
466 shf->rnleft -= ncopy;
469 return (orig_bsize == bsize ? (shf_error(shf) ? EOF : 0) :
480 shf_getse(char *buf, ssize_t bsize, struct shf *shf)
486 if (!(shf->flags & SHF_RD))
487 internal_errorf("%s: flags 0x%X", "shf_getse", shf->flags);
495 if (shf->rnleft == 0) {
496 if (shf_fillbuf(shf) == EOF)
498 if (shf->rnleft == 0) {
503 end = (unsigned char *)memchr((char *)shf->rp, '\n',
504 shf->rnleft);
505 ncopy = end ? end - shf->rp + 1 : shf->rnleft;
508 memcpy(buf, (char *) shf->rp, ncopy);
509 shf->rp += ncopy;
510 shf->rnleft -= ncopy;
520 shf_getchar(struct shf *shf)
522 if (!(shf->flags & SHF_RD))
523 internal_errorf("%s: flags 0x%X", "shf_getchar", shf->flags);
525 if (shf->rnleft == 0 && (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
527 --shf->rnleft;
528 return (*shf->rp++);
536 shf_ungetc(int c, struct shf *shf)
538 if (!(shf->flags & SHF_RD))
539 internal_errorf("%s: flags 0x%X", "shf_ungetc", shf->flags);
541 if ((shf->flags & SHF_ERROR) || c == EOF ||
542 (shf->rp == shf->buf && shf->rnleft))
545 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
548 if (shf->rp == shf->buf)
549 shf->rp = shf->buf + shf->rbsize;
550 if (shf->flags & SHF_STRING) {
555 if ((int)(shf->rp[-1]) != c)
557 shf->flags &= ~SHF_EOF;
558 shf->rp--;
559 shf->rnleft++;
562 shf->flags &= ~SHF_EOF;
563 *--(shf->rp) = c;
564 shf->rnleft++;
573 shf_putchar(int c, struct shf *shf)
575 if (!(shf->flags & SHF_WR))
576 internal_errorf("%s: flags 0x%X", "shf_putchar", shf->flags);
581 if (shf->flags & SHF_UNBUF) {
585 if (shf->fd < 0)
587 if (shf->flags & SHF_ERROR) {
588 errno = shf->errnosv;
591 while ((n = write(shf->fd, &cc, 1)) != 1)
594 !(shf->flags & SHF_INTERRUPT))
596 shf->flags |= SHF_ERROR;
597 shf->errnosv = errno;
602 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == EOF)
604 shf->wnleft--;
605 *shf->wp++ = c;
616 shf_puts(const char *s, struct shf *shf)
621 return (shf_write(s, strlen(s), shf));
626 shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
630 if (!(shf->flags & SHF_WR))
631 internal_errorf("%s: flags 0x%X", "shf_write", shf->flags);
637 if ((ncopy = shf->wnleft) &&
638 (shf->wp != shf->buf || nbytes < shf->wnleft)) {
641 memcpy(shf->wp, buf, ncopy);
644 shf->wp += ncopy;
645 shf->wnleft -= ncopy;
648 if (shf->flags & SHF_STRING) {
650 while (nbytes > shf->wnleft)
651 if (shf_emptybuf(shf, EB_GROW) == EOF)
656 if (shf_emptybuf(shf, EB_GROW) == EOF)
659 if (nbytes > shf->wbsize) {
661 if (shf->wbsize)
662 ncopy -= nbytes % shf->wbsize;
665 n = write(shf->fd, buf, ncopy);
668 !(shf->flags & SHF_INTERRUPT))
670 shf->flags |= SHF_ERROR;
671 shf->errnosv = errno;
672 shf->wnleft = 0;
687 memcpy(shf->wp, buf, nbytes);
688 shf->wp += nbytes;
689 shf->wnleft -= nbytes;
697 shf_fprintf(struct shf *shf, const char *fmt, ...)
703 n = shf_vfprintf(shf, fmt, args);
712 struct shf shf;
720 shf_sopen(buf, bsize, SHF_WR, &shf);
722 n = shf_vfprintf(&shf, fmt, args);
725 shf_sclose(&shf);
732 struct shf shf;
735 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
737 shf_vfprintf(&shf, fmt, args);
740 return (shf_sclose(&shf));
759 shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
783 shf_putc(c, shf);
1019 shf_putc(*s, shf);
1024 shf_putc(*s, shf);
1029 shf_putc(*s, shf);
1041 shf_putc(c, shf);
1054 shf_putc(*s, shf);
1060 shf_putc(c, shf);
1064 return (shf_error(shf) ? EOF : nwritten);
1069 shf_getc(struct shf *shf)
1071 return (shf_getc_i(shf));
1075 shf_putc(int c, struct shf *shf)
1077 return (shf_putc_i(c, shf));