1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #include "sfhdr.h"
23da2e3ebdSchin
24da2e3ebdSchin /* Push back one byte to a given SF_READ stream
25da2e3ebdSchin **
26da2e3ebdSchin ** Written by Kiem-Phong Vo.
27da2e3ebdSchin */
28da2e3ebdSchin #if __STD_C
_uexcept(Sfio_t * f,int type,Void_t * val,Sfdisc_t * disc)297c2fbfb3SApril Chin static int _uexcept(Sfio_t* f, int type, Void_t* val, Sfdisc_t* disc)
30da2e3ebdSchin #else
31da2e3ebdSchin static int _uexcept(f,type,val,disc)
327c2fbfb3SApril Chin Sfio_t *f;
337c2fbfb3SApril Chin int type;
34da2e3ebdSchin Void_t* val;
357c2fbfb3SApril Chin Sfdisc_t *disc;
36da2e3ebdSchin #endif
37da2e3ebdSchin {
38da2e3ebdSchin NOTUSED(val);
39da2e3ebdSchin
40da2e3ebdSchin /* hmm! This should never happen */
41da2e3ebdSchin if(disc != _Sfudisc)
42da2e3ebdSchin return -1;
43da2e3ebdSchin
44da2e3ebdSchin /* close the unget stream */
45da2e3ebdSchin if(type != SF_CLOSING)
46da2e3ebdSchin (void)sfclose((*_Sfstack)(f,NIL(Sfio_t*)));
47da2e3ebdSchin
48da2e3ebdSchin return 1;
49da2e3ebdSchin }
50da2e3ebdSchin
51da2e3ebdSchin #if __STD_C
sfungetc(Sfio_t * f,int c)527c2fbfb3SApril Chin int sfungetc(Sfio_t* f, int c)
53da2e3ebdSchin #else
54da2e3ebdSchin int sfungetc(f,c)
557c2fbfb3SApril Chin Sfio_t* f; /* push back one byte to this stream */
567c2fbfb3SApril Chin int c; /* the value to be pushed back */
57da2e3ebdSchin #endif
58da2e3ebdSchin {
59da2e3ebdSchin reg Sfio_t* uf;
607c2fbfb3SApril Chin SFMTXDECL(f);
61da2e3ebdSchin
627c2fbfb3SApril Chin SFMTXENTER(f, -1)
63da2e3ebdSchin
64da2e3ebdSchin if(c < 0 || (f->mode != SF_READ && _sfmode(f,SF_READ,0) < 0))
65da2e3ebdSchin SFMTXRETURN(f, -1);
66da2e3ebdSchin SFLOCK(f,0);
67da2e3ebdSchin
68da2e3ebdSchin /* fast handling of the typical unget */
69da2e3ebdSchin if(f->next > f->data && f->next[-1] == (uchar)c)
70da2e3ebdSchin { f->next -= 1;
71da2e3ebdSchin goto done;
72da2e3ebdSchin }
73da2e3ebdSchin
74da2e3ebdSchin /* make a string stream for unget characters */
75da2e3ebdSchin if(f->disc != _Sfudisc)
76da2e3ebdSchin { if(!(uf = sfnew(NIL(Sfio_t*),NIL(char*),(size_t)SF_UNBOUND,
77da2e3ebdSchin -1,SF_STRING|SF_READ)))
78da2e3ebdSchin { c = -1;
79da2e3ebdSchin goto done;
80da2e3ebdSchin }
81da2e3ebdSchin _Sfudisc->exceptf = _uexcept;
82da2e3ebdSchin sfdisc(uf,_Sfudisc);
83da2e3ebdSchin SFOPEN(f,0); (void)sfstack(f,uf); SFLOCK(f,0);
84da2e3ebdSchin }
85da2e3ebdSchin
86da2e3ebdSchin /* space for data */
87da2e3ebdSchin if(f->next == f->data)
88da2e3ebdSchin { reg uchar* data;
89da2e3ebdSchin if(f->size < 0)
90da2e3ebdSchin f->size = 0;
91da2e3ebdSchin if(!(data = (uchar*)malloc(f->size+16)))
92da2e3ebdSchin { c = -1;
93da2e3ebdSchin goto done;
94da2e3ebdSchin }
95da2e3ebdSchin f->flags |= SF_MALLOC;
96da2e3ebdSchin if(f->data)
97da2e3ebdSchin memcpy((char*)(data+16),(char*)f->data,f->size);
98da2e3ebdSchin f->size += 16;
99da2e3ebdSchin f->data = data;
100da2e3ebdSchin f->next = data+16;
101da2e3ebdSchin f->endb = data+f->size;
102da2e3ebdSchin }
103da2e3ebdSchin
104da2e3ebdSchin *--f->next = (uchar)c;
105da2e3ebdSchin done:
106da2e3ebdSchin SFOPEN(f,0);
107da2e3ebdSchin SFMTXRETURN(f, c);
108da2e3ebdSchin }
109