1da2e3ebchin/***********************************************************************
2da2e3ebchin*                                                                      *
3da2e3ebchin*               This software is part of the ast package               *
43e14f97Roger A. Faulkner*          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5da2e3ebchin*                      and is licensed under the                       *
6da2e3ebchin*                  Common Public License, Version 1.0                  *
77c2fbfbApril Chin*                    by AT&T Intellectual Property                     *
8da2e3ebchin*                                                                      *
9da2e3ebchin*                A copy of the License is available at                 *
10da2e3ebchin*            http://www.opensource.org/licenses/cpl1.0.txt             *
11da2e3ebchin*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12da2e3ebchin*                                                                      *
13da2e3ebchin*              Information and Software Systems Research               *
14da2e3ebchin*                            AT&T Research                             *
15da2e3ebchin*                           Florham Park NJ                            *
16da2e3ebchin*                                                                      *
17da2e3ebchin*                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebchin*                  David Korn <dgk@research.att.com>                   *
19da2e3ebchin*                   Phong Vo <kpv@research.att.com>                    *
20da2e3ebchin*                                                                      *
21da2e3ebchin***********************************************************************/
22da2e3ebchin#include	"sfhdr.h"
23da2e3ebchin
24da2e3ebchin/*	Invoke event handlers for a stream
25da2e3ebchin**
26da2e3ebchin**	Written by Kiem-Phong Vo.
27da2e3ebchin*/
28da2e3ebchin
29da2e3ebchin#if __STD_C
30da2e3ebchinstatic int _sfraiseall(int type, Void_t* data)
31da2e3ebchin#else
32da2e3ebchinstatic int _sfraiseall(type, data)
33da2e3ebchinint	type;	/* type of event	*/
34da2e3ebchinVoid_t*	data;	/* associated data	*/
35da2e3ebchin#endif
36da2e3ebchin{
37da2e3ebchin	Sfio_t		*f;
38da2e3ebchin	Sfpool_t	*p, *next;
39da2e3ebchin	int		n, rv;
40da2e3ebchin
41da2e3ebchin	rv = 0;
42da2e3ebchin	for(p = &_Sfpool; p; p = next)
43da2e3ebchin	{
44da2e3ebchin		for(next = p->next; next; next = next->next)
45da2e3ebchin			if(next->n_sf > 0)
46da2e3ebchin				break;
47da2e3ebchin		for(n = 0; n < p->n_sf; ++n)
48da2e3ebchin		{	f = p->sf[n];
49da2e3ebchin			if(sfraise(f, type, data) < 0)
50da2e3ebchin				rv -= 1;
51da2e3ebchin		}
52da2e3ebchin	}
53da2e3ebchin	return rv;
54da2e3ebchin}
55da2e3ebchin
56da2e3ebchin#if __STD_C
57da2e3ebchinint sfraise(Sfio_t* f, int type, Void_t* data)
58da2e3ebchin#else
59da2e3ebchinint sfraise(f, type, data)
60da2e3ebchinSfio_t*	f;	/* stream		*/
61da2e3ebchinint	type;	/* type of event	*/
62da2e3ebchinVoid_t*	data;	/* associated data	*/
63da2e3ebchin#endif
64da2e3ebchin{
65da2e3ebchin	reg Sfdisc_t	*disc, *next, *d;
66da2e3ebchin	reg int		local, rv;
677c2fbfbApril Chin	SFMTXDECL(f);
68da2e3ebchin
69da2e3ebchin	if(!f)
70da2e3ebchin		return _sfraiseall(type,data);
71da2e3ebchin
727c2fbfbApril Chin	SFMTXENTER(f, -1);
73da2e3ebchin
74da2e3ebchin	GETLOCAL(f,local);
75da2e3ebchin	if(!SFKILLED(f) &&
76da2e3ebchin	   !(local &&
77da2e3ebchin	     (type == SF_NEW || type == SF_CLOSING ||
78da2e3ebchin	      type == SF_FINAL || type == SF_ATEXIT)) &&
79da2e3ebchin	   SFMODE(f,local) != (f->mode&SF_RDWR) && _sfmode(f,0,local) < 0)
80da2e3ebchin		SFMTXRETURN(f, -1);
81da2e3ebchin	SFLOCK(f,local);
82da2e3ebchin
83da2e3ebchin	for(disc = f->disc; disc; )
84da2e3ebchin	{	next = disc->disc;
85da2e3ebchin		if(type == SF_FINAL)
86da2e3ebchin			f->disc = next;
87da2e3ebchin
88da2e3ebchin		if(disc->exceptf)
89da2e3ebchin		{	SFOPEN(f,0);
90da2e3ebchin			if((rv = (*disc->exceptf)(f,type,data,disc)) != 0 )
91da2e3ebchin				SFMTXRETURN(f, rv);
92da2e3ebchin			SFLOCK(f,0);
93da2e3ebchin		}
94da2e3ebchin
95da2e3ebchin		if((disc = next) )
96da2e3ebchin		{	/* make sure that "next" hasn't been popped */
97da2e3ebchin			for(d = f->disc; d; d = d->disc)
98da2e3ebchin				if(d == disc)
99da2e3ebchin					break;
100da2e3ebchin			if(!d)
101da2e3ebchin				disc = f->disc;
102da2e3ebchin		}
103da2e3ebchin	}
104da2e3ebchin
105da2e3ebchin	SFOPEN(f,local);
106da2e3ebchin	SFMTXRETURN(f, 0);
107da2e3ebchin}
108