xref: /onnv-gate/usr/src/lib/libast/common/string/tokline.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin  * Glenn Fowler
254887Schin  * AT&T Research
264887Schin  *
274887Schin  * return an Sfio_t* to a file or string that
284887Schin  *
294887Schin  *	splices \\n to single lines
304887Schin  *	checks for "..." and '...' spanning newlines
314887Schin  *	drops #...\n comments
324887Schin  *
334887Schin  * if <arg> is a file and first line matches
344887Schin  *	#!!! <level> <message> !!!
354887Schin  * then error(<lev>,"%s: %s",<arg>,<msg>) called
364887Schin  *
374887Schin  * NOTE: seek disabled and string disciplines cannot be composed
384887Schin  *	 quoted \n translated to \r
394887Schin  */
404887Schin 
414887Schin #include <ast.h>
424887Schin #include <error.h>
434887Schin #include <tok.h>
444887Schin 
454887Schin typedef struct
464887Schin {
474887Schin 	Sfdisc_t	disc;
484887Schin 	Sfio_t*		sp;
494887Schin 	int		quote;
504887Schin 	int*		line;
514887Schin } Splice_t;
524887Schin 
534887Schin /*
544887Schin  * the splicer
554887Schin  */
564887Schin 
574887Schin static int
spliceline(Sfio_t * s,int op,void * val,Sfdisc_t * ad)584887Schin spliceline(Sfio_t* s, int op, void* val, Sfdisc_t* ad)
594887Schin {
604887Schin 	Splice_t*	d = (Splice_t*)ad;
614887Schin 	register char*	b;
624887Schin 	register int	c;
634887Schin 	register int	n;
644887Schin 	register int	q;
654887Schin 	register int	j;
664887Schin 	register char*	e;
674887Schin 	char*		buf;
684887Schin 
694887Schin 	NoP(val);
704887Schin 	switch (op)
714887Schin 	{
724887Schin 	case SF_CLOSING:
734887Schin 		sfclose(d->sp);
744887Schin 		return 0;
754887Schin 	case SF_DPOP:
764887Schin 		free(d);
774887Schin 		return 0;
784887Schin 	case SF_READ:
794887Schin 		do
804887Schin 		{
814887Schin 			if (!(buf = sfgetr(d->sp, '\n', 0)) && !(buf = sfgetr(d->sp, '\n', -1)))
824887Schin 				return 0;
834887Schin 			n = sfvalue(d->sp);
844887Schin 			q = d->quote;
854887Schin 			j = 0;
864887Schin 			(*d->line)++;
874887Schin 			if (n > 1 && buf[n - 2] == '\\')
884887Schin 			{
894887Schin 				j = 1;
904887Schin 				n -= 2;
914887Schin 				if (q == '#')
924887Schin 				{
934887Schin 					n = 0;
944887Schin 					continue;
954887Schin 				}
964887Schin 			}
974887Schin 			else if (q == '#')
984887Schin 			{
994887Schin 				q = 0;
1004887Schin 				n = 0;
1014887Schin 				continue;
1024887Schin 			}
1034887Schin 			if (n > 0)
1044887Schin 			{
1054887Schin 				e = (b = buf) + n;
1064887Schin 				while (b < e)
1074887Schin 				{
1084887Schin 					if ((c = *b++) == '\\')
1094887Schin 						b++;
1104887Schin 					else if (c == q)
1114887Schin 						q = 0;
1124887Schin 					else if (!q)
1134887Schin 					{
1144887Schin 						if (c == '\'' || c == '"')
1154887Schin 							q = c;
1164887Schin 						else if (c == '#' && (b == (buf + 1) || (c = *(b - 2)) == ' ' || c == '\t'))
1174887Schin 						{
1184887Schin 							if (buf[n - 1] != '\n')
1194887Schin 							{
1204887Schin 								q = '#';
1214887Schin 								n = b - buf - 2;
1224887Schin 							}
1234887Schin 							else if (n = b - buf - 1)
1244887Schin 								buf[n - 1] = '\n';
1254887Schin 							break;
1264887Schin 						}
1274887Schin 					}
1284887Schin 				}
1294887Schin 				if (n > 0)
1304887Schin 				{
1314887Schin 					if (!j && buf[n - 1] != '\n' && (s->_flags & SF_STRING))
1324887Schin 						buf[n++] = '\n';
1334887Schin 					if (q && buf[n - 1] == '\n')
1344887Schin 						buf[n - 1] = '\r';
1354887Schin 				}
1364887Schin 			}
1374887Schin 		} while (n <= 0);
1384887Schin 		sfsetbuf(s, buf, n);
1394887Schin 		d->quote = q;
1404887Schin 		return 1;
1414887Schin 	default:
1424887Schin 		return 0;
1434887Schin 	}
1444887Schin }
1454887Schin 
1464887Schin /*
1474887Schin  * open a stream to parse lines
1484887Schin  *
1494887Schin  *	flags: 0		arg: open Sfio_t*
1504887Schin  *	flags: SF_READ		arg: file name
1514887Schin  *	flags: SF_STRING	arg: null terminated char*
1524887Schin  *
1534887Schin  * if line!=0 then it points to a line count that starts at 0
1544887Schin  * and is incremented for each input line
1554887Schin  */
1564887Schin 
1574887Schin Sfio_t*
tokline(const char * arg,int flags,int * line)1584887Schin tokline(const char* arg, int flags, int* line)
1594887Schin {
1604887Schin 	Sfio_t*		f;
1614887Schin 	Sfio_t*		s;
1624887Schin 	Splice_t*	d;
1634887Schin 	char*		p;
1644887Schin 	char*		e;
1654887Schin 
1664887Schin 	static int	hidden;
1674887Schin 
1684887Schin 	if (!(d = newof(0, Splice_t, 1, 0)))
1694887Schin 		return 0;
1704887Schin 	if (!(s = sfopen(NiL, NiL, "s")))
1714887Schin 	{
1724887Schin 		free(d);
1734887Schin 		return 0;
1744887Schin 	}
1754887Schin 	if (!(flags & (SF_STRING|SF_READ)))
1764887Schin 		f = (Sfio_t*)arg;
1774887Schin 	else if (!(f = sfopen(NiL, arg, (flags & SF_STRING) ? "s" : "r")))
1784887Schin 	{
1794887Schin 		free(d);
1804887Schin 		sfclose(s);
1814887Schin 		return 0;
1824887Schin 	}
1834887Schin 	else if ((p = sfreserve(f, 0, 0)) && sfvalue(f) > 11 && strmatch(p, "#!!! +([-0-9]) *([!\n]) !!!\n*") && (e = strchr(p, '\n')))
1844887Schin 	{
1854887Schin 		flags = strtol(p + 5, &p, 10);
1864887Schin 		error(flags, "%s:%-.*s", arg, e - p - 4, p);
1874887Schin 	}
1884887Schin 	d->disc.exceptf = spliceline;
1894887Schin 	d->sp = f;
1904887Schin 	*(d->line = line ? line : &hidden) = 0;
1914887Schin 	sfdisc(s, (Sfdisc_t*)d);
1924887Schin 	return s;
1934887Schin }
194