Home | History | Annotate | Download | only in ksh

Lines Matching defs:shf

1 /*	$NetBSD: shf.c,v 1.16 2024/02/05 21:20:09 andvar Exp $	*/
9 __RCSID("$NetBSD: shf.c,v 1.16 2024/02/05 21:20:09 andvar Exp $");
28 static int shf_fillbuf ARGS((struct shf *shf));
29 static int shf_emptybuf ARGS((struct shf *shf, int flags));
35 struct shf *
42 struct shf *shf;
47 shf = (struct shf *) alloc(sizeof(struct shf) + bsize, ATEMP);
48 shf->areap = ATEMP;
49 shf->buf = (unsigned char *) &shf[1];
50 shf->bsize = bsize;
51 shf->flags = SHF_ALLOCS;
56 afree(shf, shf->areap);
65 afree(shf, shf->areap);
75 return shf_reopen(fd, sflags, shf);
78 /* Set up the shf structure for a file descriptor. Doesn't fail. */
79 struct shf *
80 shf_fdopen(fd, sflags, shf)
83 struct shf *shf;
105 if (shf) {
107 shf->buf = (unsigned char *) alloc(bsize, ATEMP);
110 shf->buf = (unsigned char *) 0;
112 shf = (struct shf *) alloc(sizeof(struct shf) + bsize, ATEMP);
113 shf->buf = (unsigned char *) &shf[1];
116 shf->areap = ATEMP;
117 shf->fd = fd;
118 shf->rp = shf->wp = shf->buf;
119 shf->rnleft = 0;
120 shf->rbsize = bsize;
121 shf->wnleft = 0; /* force call to shf_emptybuf() */
122 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
123 shf->flags = sflags;
124 shf->errno_ = 0;
125 shf->bsize = bsize;
128 return shf;
131 /* Set up an existing shf (and buffer) to use the given fd */
132 struct shf *
133 shf_reopen(fd, sflags, shf)
136 struct shf *shf;
157 if (!shf || !shf->buf || shf->bsize < bsize)
158 internal_errorf(1, "shf_reopen: bad shf/buf/bsize");
160 /* assumes shf->buf and shf->bsize already set up */
161 shf->fd = fd;
162 shf->rp = shf->wp = shf->buf;
163 shf->rnleft = 0;
164 shf->rbsize = bsize;
165 shf->wnleft = 0; /* force call to shf_emptybuf() */
166 shf->wbsize = sflags & SHF_UNBUF ? 0 : bsize;
167 shf->flags = (shf->flags & (SHF_ALLOCS | SHF_ALLOCB)) | sflags;
168 shf->errno_ = 0;
171 return shf;
176 * bytes that can be written. If shf is not null, it is filled in and
177 * returned, if it is null, shf is allocated. If writing and buf is null
182 struct shf *
183 shf_sopen(buf, bsize, sflags, shf)
187 struct shf *shf;
194 if (!shf) {
195 shf = (struct shf *) alloc(sizeof(struct shf), ATEMP);
198 shf->areap = ATEMP;
203 buf = alloc(bsize, shf->areap);
205 shf->fd = -1;
206 shf->buf = shf->rp = shf->wp = (unsigned char *) buf;
207 shf->rnleft = bsize;
208 shf->rbsize = bsize;
209 shf->wnleft = bsize - 1; /* space for a '\0' */
210 shf->wbsize = bsize;
211 shf->flags = sflags | SHF_STRING;
212 shf->errno_ = 0;
213 shf->bsize = bsize;
215 return shf;
218 /* Flush and close file descriptor, free the shf structure */
220 shf_close(shf)
221 struct shf *shf;
225 if (shf->fd >= 0) {
226 ret = shf_flush(shf);
227 if (close(shf->fd) < 0)
230 if (shf->flags & SHF_ALLOCS)
231 afree(shf, shf->areap);
232 else if (shf->flags & SHF_ALLOCB)
233 afree(shf->buf, shf->areap);
240 shf_fdclose(shf)
241 struct shf *shf;
245 if (shf->fd >= 0) {
246 ret = shf_flush(shf);
247 if (close(shf->fd) < 0)
249 shf->rnleft = 0;
250 shf->rp = shf->buf;
251 shf->wnleft = 0;
252 shf->fd = -1;
259 * returns a pointer to the string and frees shf if it was allocated
263 shf_sclose(shf)
264 struct shf *shf;
266 unsigned char *s = shf->buf;
269 if (shf->flags & SHF_WR) {
270 shf->wnleft++;
271 shf_putc('\0', shf);
273 if (shf->flags & SHF_ALLOCS)
274 afree(shf, shf->areap);
280 shf_finish(shf)
281 struct shf *shf;
285 if (shf->fd >= 0)
286 ret = shf_flush(shf);
287 if (shf->flags & SHF_ALLOCS)
288 afree(shf, shf->areap);
289 else if (shf->flags & SHF_ALLOCB)
290 afree(shf->buf, shf->areap);
299 shf_flush(shf)
300 struct shf *shf;
302 if (shf->flags & SHF_STRING)
303 return (shf->flags & SHF_WR) ? EOF : 0;
305 if (shf->fd < 0)
308 if (shf->flags & SHF_ERROR) {
309 errno = shf->errno_;
313 if (shf->flags & SHF_READING) {
314 shf->flags &= ~(SHF_EOF | SHF_READING);
315 if (shf->rnleft > 0) {
316 lseek(shf->fd, (off_t) -shf->rnleft, 1);
317 shf->rnleft = 0;
318 shf->rp = shf->buf;
321 } else if (shf->flags & SHF_WRITING)
322 return shf_emptybuf(shf, 0);
331 shf_emptybuf(shf, flags)
332 struct shf *shf;
337 if (!(shf->flags & SHF_STRING) && shf->fd < 0)
340 if (shf->flags & SHF_ERROR) {
341 errno = shf->errno_;
345 if (shf->flags & SHF_READING) {
348 ret = shf_flush(shf);
349 shf->flags &= ~SHF_READING;
351 if (shf->flags & SHF_STRING) {
355 * is set... (changing the shf pointer could cause problems)
357 if (!(flags & EB_GROW) || !(shf->flags & SHF_DYNAMIC)
358 || !(shf->flags & SHF_ALLOCB))
361 nbuf = (unsigned char *) aresize(shf->buf, shf->wbsize * 2,
362 shf->areap);
363 shf->rp = nbuf + (shf->rp - shf->buf);
364 shf->wp = nbuf + (shf->wp - shf->buf);
365 shf->rbsize += shf->wbsize;
366 shf->wnleft += shf->wbsize;
367 shf->wbsize *= 2;
368 shf->buf = nbuf;
370 if (shf->flags & SHF_WRITING) {
371 int ntowrite = shf->wp - shf->buf;
372 unsigned char *buf = shf->buf;
376 n = write(shf->fd, buf, ntowrite);
379 && !(shf->flags & SHF_INTERRUPT))
381 shf->flags |= SHF_ERROR;
382 shf->errno_ = errno;
383 shf->wnleft = 0;
384 if (buf != shf->buf) {
387 memmove(shf->buf, buf,
389 shf->wp = shf->buf + ntowrite;
397 shf->wp = shf->buf;
398 shf->wnleft = 0;
399 shf->flags &= ~SHF_WRITING;
403 shf->wp = shf->buf;
404 shf->wnleft = shf->wbsize;
406 shf->flags |= SHF_WRITING;
413 shf_fillbuf(shf)
414 struct shf *shf;
416 if (shf->flags & SHF_STRING)
419 if (shf->fd < 0)
422 if (shf->flags & (SHF_EOF | SHF_ERROR)) {
423 if (shf->flags & SHF_ERROR)
424 errno = shf->errno_;
428 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
431 shf->flags |= SHF_READING;
433 shf->rp = shf->buf;
435 shf->rnleft = blocking_read(shf->fd, (char *) shf->buf,
436 shf->rbsize);
437 if (shf->rnleft < 0 && errno == EINTR
438 && !(shf->flags & SHF_INTERRUPT))
442 if (shf->rnleft <= 0) {
443 if (shf->rnleft < 0) {
444 shf->flags |= SHF_ERROR;
445 shf->errno_ = errno;
446 shf->rnleft = 0;
447 shf->rp = shf->buf;
450 shf->flags |= SHF_EOF;
460 shf_seek(shf, where, from)
461 struct shf *shf;
465 if (shf->fd < 0) {
470 if (shf->flags & SHF_ERROR) {
471 errno = shf->errno_;
475 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
478 if (shf->flags & SHF_READING) {
481 -where >= shf->rbsize - shf->rnleft :
482 where < shf->rnleft)) {
483 shf->rnleft -= where;
484 shf->rp += where;
487 shf->rnleft = 0;
488 shf->rp = shf->buf;
491 shf->flags &= ~(SHF_EOF | SHF_READING | SHF_WRITING);
493 if (lseek(shf->fd, where, from) < 0) {
494 shf->errno_ = errno;
495 shf->flags |= SHF_ERROR;
503 /* Read a buffer from shf. Returns the number of bytes read into buf,
508 shf_read(buf, bsize, shf)
511 struct shf *shf;
516 if (!(shf->flags & SHF_RD))
517 internal_errorf(1, "shf_read: flags %x", shf->flags);
523 if (shf->rnleft == 0
524 && (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
526 ncopy = shf->rnleft;
529 memcpy(buf, shf->rp, ncopy);
532 shf->rp += ncopy;
533 shf->rnleft -= ncopy;
536 return orig_bsize == bsize ? (shf_error(shf) ? EOF : 0)
545 shf_getse(buf, bsize, shf)
548 struct shf *shf;
554 if (!(shf->flags & SHF_RD))
555 internal_errorf(1, "shf_getse: flags %x", shf->flags);
562 if (shf->rnleft == 0) {
563 if (shf_fillbuf(shf) == EOF)
565 if (shf->rnleft == 0) {
570 end = (unsigned char *) memchr((char *) shf->rp, '\n',
571 shf->rnleft);
572 ncopy = end ? end - shf->rp + 1 : shf->rnleft;
575 memcpy(buf, (char *) shf->rp, ncopy);
576 shf->rp += ncopy;
577 shf->rnleft -= ncopy;
588 shf_getchar(shf)
589 struct shf *shf;
591 if (!(shf->flags & SHF_RD))
592 internal_errorf(1, "shf_getchar: flags %x", shf->flags);
594 if (shf->rnleft == 0 && (shf_fillbuf(shf) == EOF || shf->rnleft == 0))
596 --shf->rnleft;
597 return *shf->rp++;
604 shf_ungetc(c, shf)
606 struct shf *shf;
608 if (!(shf->flags & SHF_RD))
609 internal_errorf(1, "shf_ungetc: flags %x", shf->flags);
611 if ((shf->flags & SHF_ERROR) || c == EOF
612 || (shf->rp == shf->buf && shf->rnleft))
615 if ((shf->flags & SHF_WRITING) && shf_emptybuf(shf, EB_READSW) == EOF)
618 if (shf->rp == shf->buf)
619 shf->rp = shf->buf + shf->rbsize;
620 if (shf->flags & SHF_STRING) {
624 if (shf->rp[-1] != c)
626 shf->flags &= ~SHF_EOF;
627 shf->rp--;
628 shf->rnleft++;
631 shf->flags &= ~SHF_EOF;
632 *--(shf->rp) = c;
633 shf->rnleft++;
641 shf_putchar(c, shf)
643 struct shf *shf;
645 if (!(shf->flags & SHF_WR))
646 internal_errorf(1, "shf_putchar: flags %x", shf->flags);
651 if (shf->flags & SHF_UNBUF) {
655 if (shf->fd < 0)
657 if (shf->flags & SHF_ERROR) {
658 errno = shf->errno_;
661 while ((n = write(shf->fd, &cc, 1)) != 1)
664 && !(shf->flags & SHF_INTERRUPT))
666 shf->flags |= SHF_ERROR;
667 shf->errno_ = errno;
672 if (shf->wnleft == 0 && shf_emptybuf(shf, EB_GROW) == EOF)
674 shf->wnleft--;
675 *shf->wp++ = c;
685 shf_puts(s, shf)
687 struct shf *shf;
692 return shf_write(s, strlen(s), shf);
697 shf_write(buf, nbytes, shf)
700 struct shf *shf;
706 if (!(shf->flags & SHF_WR))
707 internal_errorf(1, "shf_write: flags %x", shf->flags);
713 if ((ncopy = shf->wnleft)
714 && (shf->wp != shf->buf || nbytes < shf->wnleft))
718 memcpy(shf->wp, buf, ncopy);
721 shf->wp += ncopy;
722 shf->wnleft -= ncopy;
726 if (shf_emptybuf(shf, EB_GROW) == EOF)
728 if (nbytes > shf->wbsize) {
730 if (shf->wbsize)
731 ncopy -= nbytes % shf->wbsize;
734 n = write(shf->fd, buf, ncopy);
737 && !(shf->flags & SHF_INTERRUPT))
739 shf->flags |= SHF_ERROR;
740 shf->errno_ = errno;
741 shf->wnleft = 0;
751 memcpy(shf->wp, buf, nbytes);
752 shf->wp += nbytes;
753 shf->wnleft -= nbytes;
761 shf_fprintf(struct shf *shf, const char *fmt, ...)
767 n = shf_vfprintf(shf, fmt, args);
776 struct shf shf;
784 shf_sopen(buf, bsize, SHF_WR, &shf);
786 n = shf_vfprintf(&shf, fmt, args);
788 shf_sclose(&shf); /* null terminates */
795 struct shf shf;
798 shf_sopen((char *) 0, 0, SHF_WR|SHF_DYNAMIC, &shf);
800 shf_vfprintf(&shf, fmt, args);
802 return shf_sclose(&shf); /* null terminates */
863 shf_vfprintf(shf, fmt, args)
864 struct shf *shf;
896 shf_putc(c, shf);
1232 shf_putc(*s, shf);
1237 shf_putc(*s, shf);
1243 shf_putc(*s, shf);
1255 shf_putc(c, shf);
1265 shf_putc(*s, shf);
1270 shf_putc(c, shf);
1274 return shf_error(shf) ? EOF : nwritten;