xref: /onnv-gate/usr/src/lib/libast/common/vec/vecload.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 Bell Laboratories
264887Schin  *
274887Schin  * string vector load support
284887Schin  */
294887Schin 
304887Schin #include <ast.h>
314887Schin #include <vecargs.h>
324887Schin 
334887Schin /*
344887Schin  * load a string vector from lines in buf
354887Schin  * buf may be modified on return
364887Schin  *
374887Schin  * each line in buf is treated as a new vector element
384887Schin  * lines with # as first char are comments
394887Schin  * \ as the last char joins consecutive lines
404887Schin  *
414887Schin  * the vector ends with a 0 sentinel
424887Schin  *
434887Schin  * the string array pointer is returned
444887Schin  */
454887Schin 
464887Schin char**
vecload(char * buf)474887Schin vecload(char* buf)
484887Schin {
494887Schin 	register char*	s;
504887Schin 	register int	n;
514887Schin 	register char**	p;
524887Schin 	char**		vec;
534887Schin 
544887Schin 	vec = 0;
554887Schin 	n = (*buf == '#') ? -1 : 0;
564887Schin 	for (s = buf;; s++)
574887Schin 	{
584887Schin 		if (*s == '\n')
594887Schin 		{
604887Schin 			if (s > buf && *(s - 1) == '\\') *(s - 1) = *s = ' ';
614887Schin 			else
624887Schin 			{
634887Schin 				*s = 0;
644887Schin 				if (*(s + 1) != '#')
654887Schin 				{
664887Schin 					n++;
674887Schin 					if (!*(s + 1)) break;
684887Schin 				}
694887Schin 			}
704887Schin 		}
714887Schin 		else if (!*s)
724887Schin 		{
734887Schin 			n++;
744887Schin 			break;
754887Schin 		}
764887Schin 	}
774887Schin 	if (n < 0) n = 0;
784887Schin 	if (p = newof(0, char*, n + 3, 0))
794887Schin 	{
804887Schin 		*p++ = s = buf;
814887Schin 		vec = ++p;
824887Schin 		if (n > 0) for (;;)
834887Schin 		{
844887Schin 			if (*s != '#')
854887Schin 			{
864887Schin 				*p++ = s;
874887Schin 				if (--n <= 0) break;
884887Schin 			}
894887Schin 			while (*s) s++;
904887Schin 			s++;
914887Schin 		}
924887Schin 		*p = 0;
934887Schin 		*(vec - 1) = (char*)p;
944887Schin 	}
954887Schin 	return(vec);
964887Schin }
97