xref: /illumos-gate/usr/src/cmd/bnu/getprm.c (revision 462be471)
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  */
227c478bd9Sstevel@tonic-gate /*
23*462be471Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "uucp.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #define LQUOTE	'('
367c478bd9Sstevel@tonic-gate #define RQUOTE ')'
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static char *bal();
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * get next parameter from s
427c478bd9Sstevel@tonic-gate  *	s	-> string to scan
437c478bd9Sstevel@tonic-gate  *	whsp	-> pointer to use to return leading whitespace
447c478bd9Sstevel@tonic-gate  *	prm	-> pointer to use to return token
457c478bd9Sstevel@tonic-gate  * return:
467c478bd9Sstevel@tonic-gate  *	 s	-> pointer to next character
477c478bd9Sstevel@tonic-gate  *		NULL at end
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate char *
507c478bd9Sstevel@tonic-gate getprm(s, whsp, prm)
51*462be471Sceastha char *s, *whsp, *prm;
527c478bd9Sstevel@tonic-gate {
53*462be471Sceastha 	char *c;
547c478bd9Sstevel@tonic-gate 	char rightq;		/* the right quote character */
557c478bd9Sstevel@tonic-gate 	char *beginning;
567c478bd9Sstevel@tonic-gate 	wchar_t	ch;
577c478bd9Sstevel@tonic-gate 	int	width;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	beginning = prm;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	while ((width = mbtowc(&ch, s, MB_CUR_MAX)) &&
627c478bd9Sstevel@tonic-gate 	    iswspace(ch) || (ch == '\n')) {
637c478bd9Sstevel@tonic-gate 		if (whsp != (char *) NULL)
647c478bd9Sstevel@tonic-gate 			while (width--)
657c478bd9Sstevel@tonic-gate 				*whsp++ = *s++;
667c478bd9Sstevel@tonic-gate 		else
677c478bd9Sstevel@tonic-gate 			s += width;
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	if ( whsp != (char *) NULL )
717c478bd9Sstevel@tonic-gate 		*whsp = '\0';
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	while ((width = mbtowc(&ch, s, MB_CUR_MAX)) && ch) {
747c478bd9Sstevel@tonic-gate 		if (iswspace(ch) || ch == '\n' || ch == '\0') {
757c478bd9Sstevel@tonic-gate 			*prm = '\0';
767c478bd9Sstevel@tonic-gate 			return(prm == beginning ? NULL : s);
777c478bd9Sstevel@tonic-gate 		}
787c478bd9Sstevel@tonic-gate 		switch (ch) {
797c478bd9Sstevel@tonic-gate 		case '>':
807c478bd9Sstevel@tonic-gate 			if ((prm == beginning + 1) && (*beginning == '2'))
817c478bd9Sstevel@tonic-gate 				*prm++ = *s++;
827c478bd9Sstevel@tonic-gate 			if ((prm == beginning + 1) && (*beginning == '1'))
837c478bd9Sstevel@tonic-gate 				*beginning = *s++;
847c478bd9Sstevel@tonic-gate 			if (prm == beginning) {
857c478bd9Sstevel@tonic-gate 				width = mbtowc(&ch, s+1, MB_CUR_MAX);
867c478bd9Sstevel@tonic-gate 				if ((ch == '>') || (ch == '&'))
877c478bd9Sstevel@tonic-gate 					*prm++ = *s++;
887c478bd9Sstevel@tonic-gate 				*prm++ = *s++;
897c478bd9Sstevel@tonic-gate 			}
907c478bd9Sstevel@tonic-gate 			*prm = '\0';
917c478bd9Sstevel@tonic-gate 			return(s);
927c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
937c478bd9Sstevel@tonic-gate 			break;
947c478bd9Sstevel@tonic-gate 		case '<':
957c478bd9Sstevel@tonic-gate 			if ((prm == beginning + 1) && (*beginning == '0'))
967c478bd9Sstevel@tonic-gate 				*beginning = *s++;
977c478bd9Sstevel@tonic-gate 			if (prm == beginning) {
987c478bd9Sstevel@tonic-gate 				width = mbtowc(&ch, s+1, MB_CUR_MAX);
997c478bd9Sstevel@tonic-gate 				if (ch == '<') {
1007c478bd9Sstevel@tonic-gate 					*prm++ = *s++;
1017c478bd9Sstevel@tonic-gate 					*prm++ = *s++;
1027c478bd9Sstevel@tonic-gate 					*prm = '\0';
103*462be471Sceastha 					return (s);
1047c478bd9Sstevel@tonic-gate 				}
1057c478bd9Sstevel@tonic-gate 				*prm++ = *s++;
1067c478bd9Sstevel@tonic-gate 			}
1077c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
1087c478bd9Sstevel@tonic-gate 		case '|':
1097c478bd9Sstevel@tonic-gate 		case ';':
1107c478bd9Sstevel@tonic-gate 		case '&':
1117c478bd9Sstevel@tonic-gate 		case '^':
1127c478bd9Sstevel@tonic-gate 		case '\\':
1137c478bd9Sstevel@tonic-gate 			if (prm == beginning)
1147c478bd9Sstevel@tonic-gate 				*prm++ = *s++;
1157c478bd9Sstevel@tonic-gate 			*prm = '\0';
1167c478bd9Sstevel@tonic-gate 			return(s);
1177c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
1187c478bd9Sstevel@tonic-gate 			break;
1197c478bd9Sstevel@tonic-gate 		case '\'':
1207c478bd9Sstevel@tonic-gate 		case '(':
1217c478bd9Sstevel@tonic-gate 		case '`':
1227c478bd9Sstevel@tonic-gate 		case '"':
1237c478bd9Sstevel@tonic-gate 			if (prm == beginning) {
1247c478bd9Sstevel@tonic-gate 				rightq = ( *s == '(' ? ')' : *s );
1257c478bd9Sstevel@tonic-gate 				c = bal(s, rightq);
1267c478bd9Sstevel@tonic-gate 				(void) strncpy(prm, s, c-s+1);
1277c478bd9Sstevel@tonic-gate 				prm += c - s + 1;
1287c478bd9Sstevel@tonic-gate 				if ( *(s=c) == rightq)
1297c478bd9Sstevel@tonic-gate 					s++;
1307c478bd9Sstevel@tonic-gate 			}
1317c478bd9Sstevel@tonic-gate 			*prm = '\0';
1327c478bd9Sstevel@tonic-gate 			return(s);
1337c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
1347c478bd9Sstevel@tonic-gate 			break;
1357c478bd9Sstevel@tonic-gate 		default:
1367c478bd9Sstevel@tonic-gate 			while (width--)
1377c478bd9Sstevel@tonic-gate 				*prm++ = *s++;
1387c478bd9Sstevel@tonic-gate 		}
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	*prm = '\0';
1427c478bd9Sstevel@tonic-gate 	return(prm == beginning ? NULL : s);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * bal - get balanced quoted string
1477c478bd9Sstevel@tonic-gate  *
1487c478bd9Sstevel@tonic-gate  * s - input string
1497c478bd9Sstevel@tonic-gate  * r - right quote
1507c478bd9Sstevel@tonic-gate  * Note: *s is the left quote
1517c478bd9Sstevel@tonic-gate  * return:
1527c478bd9Sstevel@tonic-gate  *  pointer to the end of the quoted string
1537c478bd9Sstevel@tonic-gate  * Note:
1547c478bd9Sstevel@tonic-gate  *	If the string is not balanced, it returns a pointer to the
1557c478bd9Sstevel@tonic-gate  *	end of the string.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static char *
1597c478bd9Sstevel@tonic-gate bal(s, r)
160*462be471Sceastha char *s;
1617c478bd9Sstevel@tonic-gate char r;
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	int	width;
1647c478bd9Sstevel@tonic-gate 	wchar_t	ch;
1657c478bd9Sstevel@tonic-gate 	short count = 1;
1667c478bd9Sstevel@tonic-gate 	char l;		/* left quote character */
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	for (l = *s++; *s; s+=width) {
1697c478bd9Sstevel@tonic-gate 	    width = mbtowc(&ch, s, MB_CUR_MAX);
1707c478bd9Sstevel@tonic-gate 	    if (*s == r) {
1717c478bd9Sstevel@tonic-gate 		if (--count == 0)
1727c478bd9Sstevel@tonic-gate 		    break;	/* this is the balanced end */
1737c478bd9Sstevel@tonic-gate 	    }
1747c478bd9Sstevel@tonic-gate 	    else if (*s == l)
1757c478bd9Sstevel@tonic-gate 		count++;
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 	return(s);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * split - split the name into parts:
1827c478bd9Sstevel@tonic-gate  *	arg  - original string
1837c478bd9Sstevel@tonic-gate  *	sys  - leading system name
1847c478bd9Sstevel@tonic-gate  *	fwd  - intermediate destinations, if not NULL, otherwise
1857c478bd9Sstevel@tonic-gate  *		only split into two parts.
1867c478bd9Sstevel@tonic-gate  *	file - filename part
1877c478bd9Sstevel@tonic-gate  */
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate int
1907c478bd9Sstevel@tonic-gate split(arg, sys, fwd, file)
1917c478bd9Sstevel@tonic-gate char *arg, *sys, *fwd, *file;
1927c478bd9Sstevel@tonic-gate {
193*462be471Sceastha     wchar_t *cl, *cr, *n;
1947c478bd9Sstevel@tonic-gate     int retval = 0;
1957c478bd9Sstevel@tonic-gate     wchar_t	wcbuf[MAXFULLNAME];
1967c478bd9Sstevel@tonic-gate     wchar_t	tmpbuf[MAXFULLNAME];
1977c478bd9Sstevel@tonic-gate     wchar_t	myname[MAXFULLNAME];
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate     *sys = *file = NULLCHAR;
2007c478bd9Sstevel@tonic-gate     if ( fwd != (char *) NULL )
2017c478bd9Sstevel@tonic-gate 	*fwd = NULLCHAR;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate     /* uux can use parentheses for output file names */
2047c478bd9Sstevel@tonic-gate     /* we'll check here until  we can move it to uux */
2057c478bd9Sstevel@tonic-gate     if (EQUALS(Progname,"uux") && (*arg == LQUOTE)) {
2067c478bd9Sstevel@tonic-gate 	char *c;
2077c478bd9Sstevel@tonic-gate 	c = bal(arg++, RQUOTE);
2087c478bd9Sstevel@tonic-gate 	(void) strncpy(file, arg, c-arg);
2097c478bd9Sstevel@tonic-gate 	file[c-arg] = NULLCHAR;
2107c478bd9Sstevel@tonic-gate 	return(retval);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate     mbstowcs(myname, Myname, MAXFULLNAME);
2157c478bd9Sstevel@tonic-gate     mbstowcs(wcbuf, arg, MAXFULLNAME);
2167c478bd9Sstevel@tonic-gate     for (n=wcbuf ;; n=cl+1) {
2177c478bd9Sstevel@tonic-gate 	cl = wcschr(n, (wchar_t)'!');
2187c478bd9Sstevel@tonic-gate 	if (cl == NULL) {
2197c478bd9Sstevel@tonic-gate 	    /* no ! in n */
2207c478bd9Sstevel@tonic-gate 	    (void) wcstombs(file, n, MAXFULLNAME);
2217c478bd9Sstevel@tonic-gate 	    return(retval);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	retval = 1;
2257c478bd9Sstevel@tonic-gate 	if (cl == n)	/* leading ! */
2267c478bd9Sstevel@tonic-gate 	    continue;
2277c478bd9Sstevel@tonic-gate 	if (WEQUALSN(myname, n, cl - n) && myname[cl - n] == NULLCHAR)
2287c478bd9Sstevel@tonic-gate 	    continue;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	(void) wcsncpy(tmpbuf, n, cl-n);
2317c478bd9Sstevel@tonic-gate 	tmpbuf[cl-n] = NULLCHAR;
2327c478bd9Sstevel@tonic-gate 	(void) wcstombs(sys, tmpbuf, MAXFULLNAME);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (fwd != (char *) NULL) {
2357c478bd9Sstevel@tonic-gate 	    if (cl != (cr = wcsrchr(n, (wchar_t)'!'))) {
2367c478bd9Sstevel@tonic-gate 		/*  more than one ! */
2377c478bd9Sstevel@tonic-gate 		wcsncpy(tmpbuf, cl+1, cr-cl-1);
2387c478bd9Sstevel@tonic-gate 		tmpbuf[cr-cl-1] = NULL;
2397c478bd9Sstevel@tonic-gate 		(void) wcstombs(fwd, tmpbuf, MAXFULLNAME);
2407c478bd9Sstevel@tonic-gate 	    }
2417c478bd9Sstevel@tonic-gate 	} else {
2427c478bd9Sstevel@tonic-gate 	    cr = cl;
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	(void) wcstombs(file, cr+1, MAXFULLNAME);
2467c478bd9Sstevel@tonic-gate 	return(retval);
2477c478bd9Sstevel@tonic-gate     }
2487c478bd9Sstevel@tonic-gate     /*NOTREACHED*/
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
251