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 $	*/
28 __RCSID("$MirOS: src/bin/mksh/shf.c,v 1.61 2013/07/21 18:36:03 tg Exp $");
40 static int shf_fillbuf(struct shf *);
41 static int shf_emptybuf(struct shf *, int);
48 struct shf *
51 struct shf *shf;
58 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
59 shf->areap = ATEMP;
60 shf->buf = (unsigned char *)&shf[1];
61 shf->bsize = bsize;
62 shf->flags = SHF_ALLOCS;
68 afree(shf, shf->areap);
79 afree(shf, shf->areap);
89 return (shf_reopen(fd, sflags, shf));
125 /* Set up the shf structure for a file descriptor. Doesn't fail. */
126 struct shf *
127 shf_fdopen(int fd, int sflags, struct shf *shf)
134 if (shf) {
136 shf->buf = alloc(bsize, ATEMP);
139 shf->buf = NULL;
141 shf = alloc(sizeof(struct shf) + bsize, ATEMP);
142 shf->buf = (unsigned char *)&shf[1];
145 shf->areap = ATEMP;
146 shf->fd = fd;
147 shf->rp = shf->wp = shf->buf;
148 shf->rnleft = 0;
149 shf->rbsize = bsize;
150 shf->wnleft = 0; /* force call to shf_emptybuf() */
151 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
152 shf->flags = sflags;
153 shf->errnosv = 0;
154 shf->bsize = bsize;
157 return (shf);
160 /* Set up an existing shf (and buffer) to use the given fd */
161 struct shf *
162 shf_reopen(int fd, int sflags, struct shf *shf)
169 if (!shf || !shf->buf || shf->bsize < bsize)
170 internal_errorf("%s: %s", "shf_reopen", "bad shf/buf/bsize");
172 /* assumes shf->buf and shf->bsize already set up */
173 shf->fd = fd;
174 shf->rp = shf->wp = shf->buf;
175 shf->rnleft = 0;
176 shf->rbsize = bsize;
177 shf->wnleft = 0; /* force call to shf_emptybuf() */
178 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
179 shf->flags = (shf->flags & (SHF_ALLOCS | SHF_ALLOCB)) | sflags;
180 shf->errnosv = 0;
183 return (shf);
189 * bytes that can be written. If shf is not NULL, it is filled in and
190 * returned, if it is NULL, shf is allocated. If writing and buf is NULL
195 struct shf *
196 shf_sopen(char *buf, ssize_t bsize, int sflags, struct shf *shf)
202 if (!shf) {
203 shf = alloc(sizeof(struct shf), ATEMP);
206 shf->areap = ATEMP;
211 buf = alloc(bsize, shf->areap);
213 shf->fd = -1;
214 shf->buf = shf->rp = shf->wp = (unsigned char *)buf;
215 shf->rnleft = bsize;
216 shf->rbsize = bsize;
217 shf->wnleft = bsize - 1; /* space for a '\0' */
218 shf->wbsize = bsize;
219 shf->flags = sflags | SHF_STRING;
220 shf->errnosv = 0;
221 shf->bsize = bsize;
223 return (shf);
226 /* Flush and close file descriptor, free the shf structure */
228 shf_close(struct shf *shf)
232 if (shf->fd >= 0) {
233 ret = shf_flush(shf);
234 if (close(shf->fd) < 0)
237 if (shf->flags & SHF_ALLOCS)
238 afree(shf, shf->areap);
239 else if (shf->flags & SHF_ALLOCB)
240 afree(shf->buf, shf->areap);
247 shf_fdclose(struct shf *shf)
251 if (shf->fd >= 0) {
252 ret = shf_flush(shf);
253 if (close(shf->fd) < 0)
255 shf->rnleft = 0;
256 shf->rp = shf->buf;
257 shf->wnleft = 0;
258 shf->fd = -1;
266 * returns a pointer to the string and frees shf if it was allocated
270 shf_sclose(struct shf *shf)
272 unsigned char *s = shf->buf;
275 if (shf->flags & SHF_WR) {
276 shf->wnleft++;
277 shf_putc('\0', shf);
279 if (shf->flags & SHF_ALLOCS)
280 afree(shf, shf->areap);
289 shf_flush(struct shf *shf)
291 if (shf->flags & SHF_STRING)
292 return ((shf->flags & SHF_WR) ? EOF : 0);
294 if (shf->fd < 0)
297 if (shf->flags & SHF_ERROR) {
298 errno = shf->errnosv;
302 if (shf->flags & SHF_READING) {
303 shf->flags &= ~(SHF_EOF | SHF_READING);
304 if (shf->rnleft > 0) {
305 lseek(shf->fd, (off_t)-shf->rnleft, SEEK_CUR);
306 shf->rnleft = 0;
307 shf->rp = shf->buf;
310 } else if (shf->flags & SHF_WRITING)
311 return (shf_emptybuf(shf, 0));
321 shf_emptybuf(struct shf *shf, int flags)
325 if (!(shf->flags & SHF_STRING) && shf->fd < 0)
328 if (shf->flags & SHF_ERROR) {
329 errno = shf->errnosv;
333 if (shf->flags & SHF_READING) {
337 ret = shf_flush(shf);
338 shf->flags &= ~SHF_READING;
340 if (shf->flags & SHF_STRING) {
345 * SHF_ALLOCB is set... (changing the shf pointer could
348 if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC) ||
349 !(shf->flags & SHF_ALLOCB))
352 nbuf = aresize2(shf->buf, 2, shf->wbsize, shf->areap);
353 shf->rp = nbuf + (shf->rp - shf->buf);
354 shf->wp = nbuf + (shf->wp - shf->buf);
355 shf->rbsize += shf->wbsize;
356 shf->wnleft += shf->wbsize;
357 shf->wbsize <<= 1;
358 shf->buf = nbuf;
360 if (shf->flags & SHF_WRITING) {
361 ssize_t n, ntowrite = shf->wp - shf->buf;
362 unsigned char *buf = shf->buf;
365 n = write(shf->fd, buf, ntowrite);
368 !(shf->flags & SHF_INTERRUPT))
370 shf->flags |= SHF_ERROR;
371 shf->errnosv = errno;
372 shf->wnleft = 0;
373 if (buf != shf->buf) {
378 memmove(shf->buf, buf,
380 shf->wp = shf->buf + ntowrite;
388 shf->wp = shf->buf;
389 shf->wnleft = 0;
390 shf->flags &= ~SHF_WRITING;
394 shf->wp = shf->buf;
395 shf->wnleft = shf->wbsize;
397 shf->flags |= SHF_WRITING;
404 shf_fillbuf(struct shf *shf)
408 if (shf->flags & SHF_STRING)
411 if (shf->fd < 0)
414 if (shf->flags & (SHF_EOF | SHF_ERROR)) {
415 if (shf->flags & SHF_ERROR)
416 errno = shf->errnosv;
420 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
423 shf->flags |= SHF_READING;
425 shf->rp = shf->buf;
427 n = blocking_read(shf->fd, (char *)shf->buf, shf->rbsize);
428 if (n < 0 && errno == EINTR && !(shf->flags & SHF_INTERRUPT))
433 shf->flags |= SHF_ERROR;
434 shf->errnosv = errno;
435 shf->rnleft = 0;
436 shf->rp = shf->buf;
439 if ((shf->rnleft = n) == 0)
440 shf->flags |= SHF_EOF;
445 * Read a buffer from shf. Returns the number of bytes read into buf, if
450 shf_read(char *buf, ssize_t bsize, struct shf *shf)
454 if (!(shf->flags & SHF_RD))
455 internal_errorf("%s: flags 0x%X", "shf_read", shf->flags);
461 if (shf->rnleft == 0 &&
462 (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
464 ncopy = shf->rnleft;
467 memcpy(buf, shf->rp, ncopy);
470 shf->rp += ncopy;
471 shf->rnleft -= ncopy;
474 return (orig_bsize == bsize ? (shf_error(shf) ? EOF : 0) :
485 shf_getse(char *buf, ssize_t bsize, struct shf *shf)
491 if (!(shf->flags & SHF_RD))
492 internal_errorf("%s: flags 0x%X", "shf_getse", shf->flags);
500 if (shf->rnleft == 0) {
501 if (shf_fillbuf(shf) == EOF)
503 if (shf->rnleft == 0) {
508 end = (unsigned char *)memchr((char *)shf->rp, '\n',
509 shf->rnleft);
510 ncopy = end ? end - shf->rp + 1 : shf->rnleft;
513 memcpy(buf, (char *) shf->rp, ncopy);
514 shf->rp += ncopy;
515 shf->rnleft -= ncopy;
525 shf_getchar(struct shf *shf)
527 if (!(shf->flags & SHF_RD))
528 internal_errorf("%s: flags 0x%X", "shf_getchar", shf->flags);
530 if (shf->rnleft == 0 && (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
532 --shf->rnleft;
533 return (*shf->rp++);
541 shf_ungetc(int c, struct shf *shf)
543 if (!(shf->flags & SHF_RD))
544 internal_errorf("%s: flags 0x%X", "shf_ungetc", shf->flags);
546 if ((shf->flags & SHF_ERROR) || c == EOF ||
547 (shf->rp == shf->buf && shf->rnleft))
550 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
553 if (shf->rp == shf->buf)
554 shf->rp = shf->buf + shf->rbsize;
555 if (shf->flags & SHF_STRING) {
560 if ((int)(shf->rp[-1]) != c)
562 shf->flags &= ~SHF_EOF;
563 shf->rp--;
564 shf->rnleft++;
567 shf->flags &= ~SHF_EOF;
568 *--(shf->rp) = c;
569 shf->rnleft++;
578 shf_putchar(int c, struct shf *shf)
580 if (!(shf->flags & SHF_WR))
581 internal_errorf("%s: flags 0x%X", "shf_putchar", shf->flags);
586 if (shf->flags & SHF_UNBUF) {
590 if (shf->fd < 0)
592 if (shf->flags & SHF_ERROR) {
593 errno = shf->errnosv;
596 while ((n = write(shf->fd, &cc, 1)) != 1)
599 !(shf->flags & SHF_INTERRUPT))
601 shf->flags |= SHF_ERROR;
602 shf->errnosv = errno;
607 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == EOF)
609 shf->wnleft--;
610 *shf->wp++ = c;
621 shf_puts(const char *s, struct shf *shf)
626 return (shf_write(s, strlen(s), shf));
631 shf_write(const char *buf, ssize_t nbytes, struct shf *shf)
635 if (!(shf->flags & SHF_WR))
636 internal_errorf("%s: flags 0x%X", "shf_write", shf->flags);
642 if ((ncopy = shf->wnleft) &&
643 (shf->wp != shf->buf || nbytes < shf->wnleft)) {
646 memcpy(shf->wp, buf, ncopy);
649 shf->wp += ncopy;
650 shf->wnleft -= ncopy;
653 if (shf->flags & SHF_STRING) {
655 while (nbytes > shf->wnleft)
656 if (shf_emptybuf(shf, EB_GROW) == EOF)
661 if (shf_emptybuf(shf, EB_GROW) == EOF)
664 if (nbytes > shf->wbsize) {
666 if (shf->wbsize)
667 shf->wbsize;
670 n = write(shf->fd, buf, ncopy);
673 !(shf->flags & SHF_INTERRUPT))
675 shf->flags |= SHF_ERROR;
676 shf->errnosv = errno;
677 shf->wnleft = 0;
692 memcpy(shf->wp, buf, nbytes);
693 shf->wp += nbytes;
694 shf->wnleft -= nbytes;
702 shf_fprintf(struct shf *shf, const char *fmt, ...)
708 n = shf_vfprintf(shf, fmt, args);
717 struct shf shf;
725 shf_sopen(buf, bsize, SHF_WR, &shf);
727 n = shf_vfprintf(&shf, fmt, args);
730 shf_sclose(&shf);
737 struct shf shf;
740 shf_sopen(NULL, 0, SHF_WR|SHF_DYNAMIC, &shf);
742 shf_vfprintf(&shf, fmt, args);
745 return (shf_sclose(&shf));
762 shf_vfprintf(struct shf *shf, const char *fmt, va_list args)
786 shf_putc(c, shf);
992 print_value_quoted(shf, s);
1026 shf_putc(*s, shf);
1031 shf_putc(*s, shf);
1036 shf_putc(*s, shf);
1048 shf_putc(c, shf);
1061 shf_putc(*s, shf);
1067 shf_putc(c, shf);
1071 return (shf_error(shf) ? EOF : nwritten);
1076 shf_getc(struct shf *shf)
1078 return (shf_getc_i(shf));
1082 shf_putc(int c, struct shf *shf)
1084 return (shf_putc_i(c, shf));