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