xref: /illumos-gate/usr/src/cmd/lp/lib/lp/getlist.c (revision 00208914)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*00208914SGary Mills 
23*00208914SGary Mills /*
24*00208914SGary Mills  *	Copyright 2015 Gary Mills
25*00208914SGary Mills  */
26*00208914SGary Mills 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "string.h"
347c478bd9Sstevel@tonic-gate #include "errno.h"
357c478bd9Sstevel@tonic-gate #include "stdlib.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "lp.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
407c478bd9Sstevel@tonic-gate static char		*unq_strdup ( char * , char * );
417c478bd9Sstevel@tonic-gate #else
427c478bd9Sstevel@tonic-gate static char		*unq_strdup();
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /**
467c478bd9Sstevel@tonic-gate  ** getlist() - CONSTRUCT LIST FROM STRING
477c478bd9Sstevel@tonic-gate  **/
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Any number of characters from "ws", or a single
517c478bd9Sstevel@tonic-gate  * character from "hardsep", can separate items in the list.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate char **
557c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
getlist(char * str,char * ws,char * hardsep)567c478bd9Sstevel@tonic-gate getlist (
577c478bd9Sstevel@tonic-gate 	char *			str,
587c478bd9Sstevel@tonic-gate 	char *			ws,
597c478bd9Sstevel@tonic-gate 	char *			hardsep
607c478bd9Sstevel@tonic-gate )
617c478bd9Sstevel@tonic-gate #else
627c478bd9Sstevel@tonic-gate getlist (str, ws, hardsep)
637c478bd9Sstevel@tonic-gate 	register char		*str,
647c478bd9Sstevel@tonic-gate 				*ws;
657c478bd9Sstevel@tonic-gate 	char			*hardsep;
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	register char		**list,
697c478bd9Sstevel@tonic-gate 				*p,
707c478bd9Sstevel@tonic-gate 				*sep,
717c478bd9Sstevel@tonic-gate 				c;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	int			n,
747c478bd9Sstevel@tonic-gate 				len;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	char			buf[10];
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 
79*00208914SGary Mills 	char			*copy,
80*00208914SGary Mills 				*begin;
81*00208914SGary Mills 
827c478bd9Sstevel@tonic-gate 	if (!str || !*str)
837c478bd9Sstevel@tonic-gate 		return (0);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	/*
867c478bd9Sstevel@tonic-gate 	 * Construct in "sep" the full list of characters that
877c478bd9Sstevel@tonic-gate 	 * can separate items in the list. Avoid a "malloc()"
887c478bd9Sstevel@tonic-gate 	 * if possible.
897c478bd9Sstevel@tonic-gate 	 */
907c478bd9Sstevel@tonic-gate 	len = strlen(ws) + strlen(hardsep) + 1;
917c478bd9Sstevel@tonic-gate 	if (len > sizeof(buf)) {
927c478bd9Sstevel@tonic-gate 		if (!(sep = Malloc(len))) {
937c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
947c478bd9Sstevel@tonic-gate 			return (0);
957c478bd9Sstevel@tonic-gate 		}
967c478bd9Sstevel@tonic-gate 	} else
977c478bd9Sstevel@tonic-gate 		sep = buf;
987c478bd9Sstevel@tonic-gate 	strcpy (sep, hardsep);
997c478bd9Sstevel@tonic-gate 	strcat (sep, ws);
1007c478bd9Sstevel@tonic-gate 
101*00208914SGary Mills 	/*
102*00208914SGary Mills 	 * Copy the input string because getlist() sometimes writes to it.
103*00208914SGary Mills 	 */
104*00208914SGary Mills 	if (!(begin = Strdup(str))) {
105*00208914SGary Mills 		errno = ENOMEM;
106*00208914SGary Mills 		return (0);
107*00208914SGary Mills 	}
108*00208914SGary Mills 	copy = begin;
109*00208914SGary Mills 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * Skip leading white-space.
1127c478bd9Sstevel@tonic-gate 	 */
113*00208914SGary Mills 	copy += strspn(copy, ws);
114*00208914SGary Mills 	if (!*copy) {
115*00208914SGary Mills 		Free (begin);
1167c478bd9Sstevel@tonic-gate 		return (0);
117*00208914SGary Mills 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	/*
1207c478bd9Sstevel@tonic-gate 	 * Strip trailing white-space.
1217c478bd9Sstevel@tonic-gate 	 */
122*00208914SGary Mills 	p = strchr(copy, '\0');
123*00208914SGary Mills 	while (--p != copy && strchr(ws, *p))
1247c478bd9Sstevel@tonic-gate 		;
1257c478bd9Sstevel@tonic-gate 	*++p = 0;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/*
1287c478bd9Sstevel@tonic-gate 	 * Pass 1: Count the number of items in the list.
1297c478bd9Sstevel@tonic-gate 	 */
130*00208914SGary Mills 	for (n = 0, p = copy; *p; ) {
1317c478bd9Sstevel@tonic-gate 		if ((c = *p++) == '\\')
1327c478bd9Sstevel@tonic-gate 			p++;
1337c478bd9Sstevel@tonic-gate 		else
1347c478bd9Sstevel@tonic-gate 			if (strchr(sep, c)) {
1357c478bd9Sstevel@tonic-gate 				n++;
1367c478bd9Sstevel@tonic-gate 				p += strspn(p, ws);
1377c478bd9Sstevel@tonic-gate 				if (
1387c478bd9Sstevel@tonic-gate 					!strchr(hardsep, c)
1397c478bd9Sstevel@tonic-gate 				     && strchr(hardsep, *p)
1407c478bd9Sstevel@tonic-gate 				) {
1417c478bd9Sstevel@tonic-gate 					p++;
1427c478bd9Sstevel@tonic-gate 					p += strspn(p, ws);
1437c478bd9Sstevel@tonic-gate 				}
1447c478bd9Sstevel@tonic-gate 			}
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/*
1487c478bd9Sstevel@tonic-gate 	 * Pass 2: Create the list.
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/*
1527c478bd9Sstevel@tonic-gate 	 * Pass 1 counted the number of list separaters, so
1537c478bd9Sstevel@tonic-gate 	 * add 2 to the count (includes 1 for terminating null).
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	if (!(list = (char **)Malloc((n+2) * sizeof(char *)))) {
1567c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1577c478bd9Sstevel@tonic-gate 		goto Done;
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	/*
1617c478bd9Sstevel@tonic-gate 	 * This loop will copy all but the last item.
1627c478bd9Sstevel@tonic-gate 	 */
163*00208914SGary Mills 	for (n = 0, p = copy; *p; )
1647c478bd9Sstevel@tonic-gate 		if ((c = *p++) == '\\')
1657c478bd9Sstevel@tonic-gate 			p++;
1667c478bd9Sstevel@tonic-gate 		else
1677c478bd9Sstevel@tonic-gate 			if (strchr(sep, c)) {
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 				p[-1] = 0;
170*00208914SGary Mills 				list[n++] = unq_strdup(copy, sep);
1717c478bd9Sstevel@tonic-gate 				p[-1] = c;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 				p += strspn(p, ws);
1747c478bd9Sstevel@tonic-gate 				if (
1757c478bd9Sstevel@tonic-gate 					!strchr(hardsep, c)
1767c478bd9Sstevel@tonic-gate 				     && strchr(hardsep, *p)
1777c478bd9Sstevel@tonic-gate 				) {
1787c478bd9Sstevel@tonic-gate 					p++;
1797c478bd9Sstevel@tonic-gate 					p += strspn(p, ws);
1807c478bd9Sstevel@tonic-gate 				}
181*00208914SGary Mills 				copy = p;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 			}
1847c478bd9Sstevel@tonic-gate 
185*00208914SGary Mills 	list[n++] = unq_strdup(copy, sep);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	list[n] = 0;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate Done:	if (sep != buf)
1907c478bd9Sstevel@tonic-gate 		Free (sep);
191*00208914SGary Mills 	Free (begin);
1927c478bd9Sstevel@tonic-gate 	return (list);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /**
1967c478bd9Sstevel@tonic-gate  ** unq_strdup()
1977c478bd9Sstevel@tonic-gate  **/
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static char *
2007c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
unq_strdup(char * str,char * sep)2017c478bd9Sstevel@tonic-gate unq_strdup (
2027c478bd9Sstevel@tonic-gate 	char *			str,
2037c478bd9Sstevel@tonic-gate 	char *			sep
2047c478bd9Sstevel@tonic-gate )
2057c478bd9Sstevel@tonic-gate #else
2067c478bd9Sstevel@tonic-gate unq_strdup (str, sep)
2077c478bd9Sstevel@tonic-gate 	char			*str,
2087c478bd9Sstevel@tonic-gate 				*sep;
2097c478bd9Sstevel@tonic-gate #endif
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	register int		len	= 0;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	register char		*p,
2147c478bd9Sstevel@tonic-gate 				*q,
2157c478bd9Sstevel@tonic-gate 				*ret;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	for (p = str; *p; p++)
2197c478bd9Sstevel@tonic-gate 		if (*p != '\\' || !p[1] || !strchr(sep, p[1]))
2207c478bd9Sstevel@tonic-gate 			len++;
2217c478bd9Sstevel@tonic-gate 	if (!(q = ret = Malloc(len + 1)))
2227c478bd9Sstevel@tonic-gate 		return (0);
2237c478bd9Sstevel@tonic-gate 	for (p = str; *p; p++)
2247c478bd9Sstevel@tonic-gate 		if (*p != '\\' || !p[1] || !strchr(sep, p[1]))
2257c478bd9Sstevel@tonic-gate 			*q++ = *p;
2267c478bd9Sstevel@tonic-gate 	*q = 0;
2277c478bd9Sstevel@tonic-gate 	return (ret);
2287c478bd9Sstevel@tonic-gate }
229