1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
27 * token stream routines
33 #define FLG_RESTORE 01 /* restore string on close */
34 #define FLG_NEWLINE 02 /* return newline token next */
36 typedef struct Tok_s
/* token stream state */
40 char* end
; /* end ('\0') of last token */
41 struct Tok_s
* nxt
; /* next in free list */
43 char chr
; /* replace *end with this */
47 static Tok_t
* freelist
;
50 * open a new token stream on s
51 * if f==0 then string is not restored
55 tokopen(register char* s
, int f
)
60 freelist
= freelist
->ptr
.nxt
;
61 else if (!(p
= newof(0, Tok_t
, 1, 0)))
63 p
->chr
= *(p
->ptr
.end
= s
);
64 p
->flg
= f
? FLG_RESTORE
: 0;
69 * close a token stream
70 * restore the string to its original state
76 register Tok_t
* p
= (Tok_t
*)u
;
78 if (p
->flg
== FLG_RESTORE
&& *p
->ptr
.end
!= p
->chr
)
80 p
->ptr
.nxt
= freelist
;
85 * return next space separated token
86 * "\n" is returned as a token
87 * 0 returned when no tokens remain
88 * "..." and '...' quotes are honored with \ escapes
94 register Tok_t
* p
= (Tok_t
*)u
;
101 * restore string on each call
126 while (*s
== ' ' || *s
== '\t')
136 * find the end of this token
157 p
->flg
= FLG_NEWLINE
;
173 p
->chr
= *(p
->ptr
.end
= r
);