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 * return an Sfio_t* to a file or string that
29 * splices \\n to single lines
30 * checks for "..." and '...' spanning newlines
31 * drops #...\n comments
33 * if <arg> is a file and first line matches
34 * #!!! <level> <message> !!!
35 * then error(<lev>,"%s: %s",<arg>,<msg>) called
37 * NOTE: seek disabled and string disciplines cannot be composed
38 * quoted \n translated to \r
58 spliceline(Sfio_t
* s
, int op
, void* val
, Sfdisc_t
* ad
)
60 Splice_t
* d
= (Splice_t
*)ad
;
81 if (!(buf
= sfgetr(d
->sp
, '\n', 0)) && !(buf
= sfgetr(d
->sp
, '\n', -1)))
87 if (n
> 1 && buf
[n
- 2] == '\\')
108 if ((c
= *b
++) == '\\')
114 if (c
== '\'' || c
== '"')
116 else if (c
== '#' && (b
== (buf
+ 1) || (c
= *(b
- 2)) == ' ' || c
== '\t'))
118 if (buf
[n
- 1] != '\n')
123 else if (n
= b
- buf
- 1)
131 if (!j
&& buf
[n
- 1] != '\n' && (s
->_flags
& SF_STRING
))
133 if (q
&& buf
[n
- 1] == '\n')
147 * open a stream to parse lines
149 * flags: 0 arg: open Sfio_t*
150 * flags: SF_READ arg: file name
151 * flags: SF_STRING arg: null terminated char*
153 * if line!=0 then it points to a line count that starts at 0
154 * and is incremented for each input line
158 tokline(const char* arg
, int flags
, int* line
)
168 if (!(d
= newof(0, Splice_t
, 1, 0)))
170 if (!(s
= sfopen(NiL
, NiL
, "s")))
175 if (!(flags
& (SF_STRING
|SF_READ
)))
177 else if (!(f
= sfopen(NiL
, arg
, (flags
& SF_STRING
) ? "s" : "r")))
183 else if ((p
= sfreserve(f
, 0, 0)) && sfvalue(f
) > 11 && strmatch(p
, "#!!! +([-0-9]) *([!\n]) !!!\n*") && (e
= strchr(p
, '\n')))
185 flags
= strtol(p
+ 5, &p
, 10);
186 error(flags
, "%s:%-.*s", arg
, e
- p
- 4, p
);
188 d
->disc
.exceptf
= spliceline
;
190 *(d
->line
= line
? line
: &hidden
) = 0;
191 sfdisc(s
, (Sfdisc_t
*)d
);