xref: /titanic_50/usr/src/lib/libast/common/vec/vecload.c (revision e58a33b62cd4c9a6815fd752ce58b5f389289da1)
1 /***********************************************************************
2 *                                                                      *
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                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * Glenn Fowler
25  * AT&T Bell Laboratories
26  *
27  * string vector load support
28  */
29 
30 #include <ast.h>
31 #include <vecargs.h>
32 
33 /*
34  * load a string vector from lines in buf
35  * buf may be modified on return
36  *
37  * each line in buf is treated as a new vector element
38  * lines with # as first char are comments
39  * \ as the last char joins consecutive lines
40  *
41  * the vector ends with a 0 sentinel
42  *
43  * the string array pointer is returned
44  */
45 
46 char**
47 vecload(char* buf)
48 {
49 	register char*	s;
50 	register int	n;
51 	register char**	p;
52 	char**		vec;
53 
54 	vec = 0;
55 	n = (*buf == '#') ? -1 : 0;
56 	for (s = buf;; s++)
57 	{
58 		if (*s == '\n')
59 		{
60 			if (s > buf && *(s - 1) == '\\') *(s - 1) = *s = ' ';
61 			else
62 			{
63 				*s = 0;
64 				if (*(s + 1) != '#')
65 				{
66 					n++;
67 					if (!*(s + 1)) break;
68 				}
69 			}
70 		}
71 		else if (!*s)
72 		{
73 			n++;
74 			break;
75 		}
76 	}
77 	if (n < 0) n = 0;
78 	if (p = newof(0, char*, n + 3, 0))
79 	{
80 		*p++ = s = buf;
81 		vec = ++p;
82 		if (n > 0) for (;;)
83 		{
84 			if (*s != '#')
85 			{
86 				*p++ = s;
87 				if (--n <= 0) break;
88 			}
89 			while (*s) s++;
90 			s++;
91 		}
92 		*p = 0;
93 		*(vec - 1) = (char*)p;
94 	}
95 	return(vec);
96 }
97