sfdisc.c revision 7c2fbfb345896881c631598ee3852ce9ce33fb07
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* Copyright (c) 1985-2008 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Glenn Fowler <gsf@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Phong Vo <kpv@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* Add a new discipline to the discipline stack. Each discipline
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** provides alternative I/O functions that are analogues of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** system calls.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** When the application fills or flushes the stream buffer, data
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** will be processed through discipline functions. A case deserving
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** consideration is stacking a discipline onto a read stream. Each
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** discipline operation implies buffer synchronization so the stream
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** buffer should be empty. However, a read stream representing an
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** unseekable device (eg, a pipe) may not be synchronizable. In that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** case, any buffered data must then be fed to the new discipline
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** to preserve data processing semantics. This is done by creating
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** a temporary discipline to cache such buffered data and feed
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** them to the new discipline when its readf() asks for new data.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** Care must then be taken to remove this temporary discipline
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** when it runs out of cached data.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** Written by Kiem-Phong Vo
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintypedef struct _dccache_s
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int _dccaexcept(Sfio_t* f, int type, Void_t* val, Sfdisc_t* disc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic ssize_t _dccaread(Sfio_t* f, Void_t* buf, size_t size, Sfdisc_t* disc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!f) /* bad stream */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* make sure that this is on the discipline stack */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* read from available data */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((dcca->data += sz) >= dcca->endb) /* free empty cache */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin if((Sfio_t*)disc == f) /* special case to get the top discipline */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((f->flags&SF_READ) && f->proc && (f->mode&SF_WRITE) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { /* make sure in read mode to check for read-ahead data */
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin { if((f->mode&SF_RDWR) != f->mode && _sfmode(f,0,0) < 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* disallow popping while there is cached data */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!disc && f->disc && f->disc->disc && f->disc->disc->readf == _dccaread )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* synchronize before switching to a new discipline */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* if there is buffered data, ask app before proceeding */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(((f->mode&SF_WRITE) && (n = f->next-f->data) > 0) ||
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { int rv = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(rv == 0 && f->disc && f->disc->exceptf) /* ask current discipline */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(rv == 0 && disc && disc->exceptf) /* ask discipline being pushed */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* trick the new discipline into processing already buffered data */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(!(dcca = (Dccache_t*)malloc(sizeof(Dccache_t)+n)) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* move buffered data into the temp discipline */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* save old readf, writef, and seekf to see if stream need reinit */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { /* popping, warn the being popped discipline */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!(d = f->disc) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { /* pushing, warn being pushed discipline */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { /* loop to handle the case where d may pop itself */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(d && d->exceptf)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin } while(d != f->disc);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* make sure we are not creating an infinite loop */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin for(; d; d = d->disc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* set new disc */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { /* this stream may have to be reinitialized */