xref: /titanic_53/usr/src/cmd/bnu/getprm.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1995 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "uucp.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #define LQUOTE	'('
36*7c478bd9Sstevel@tonic-gate #define RQUOTE ')'
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate static char *bal();
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  * get next parameter from s
42*7c478bd9Sstevel@tonic-gate  *	s	-> string to scan
43*7c478bd9Sstevel@tonic-gate  *	whsp	-> pointer to use to return leading whitespace
44*7c478bd9Sstevel@tonic-gate  *	prm	-> pointer to use to return token
45*7c478bd9Sstevel@tonic-gate  * return:
46*7c478bd9Sstevel@tonic-gate  *	 s	-> pointer to next character
47*7c478bd9Sstevel@tonic-gate  *		NULL at end
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate char *
50*7c478bd9Sstevel@tonic-gate getprm(s, whsp, prm)
51*7c478bd9Sstevel@tonic-gate register char *s, *whsp, *prm;
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	register char *c;
54*7c478bd9Sstevel@tonic-gate 	char rightq;		/* the right quote character */
55*7c478bd9Sstevel@tonic-gate 	char *beginning;
56*7c478bd9Sstevel@tonic-gate 	wchar_t	ch;
57*7c478bd9Sstevel@tonic-gate 	int	width;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	beginning = prm;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	while ((width = mbtowc(&ch, s, MB_CUR_MAX)) &&
62*7c478bd9Sstevel@tonic-gate 	    iswspace(ch) || (ch == '\n')) {
63*7c478bd9Sstevel@tonic-gate 		if (whsp != (char *) NULL)
64*7c478bd9Sstevel@tonic-gate 			while (width--)
65*7c478bd9Sstevel@tonic-gate 				*whsp++ = *s++;
66*7c478bd9Sstevel@tonic-gate 		else
67*7c478bd9Sstevel@tonic-gate 			s += width;
68*7c478bd9Sstevel@tonic-gate 	}
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	if ( whsp != (char *) NULL )
71*7c478bd9Sstevel@tonic-gate 		*whsp = '\0';
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	while ((width = mbtowc(&ch, s, MB_CUR_MAX)) && ch) {
74*7c478bd9Sstevel@tonic-gate 		if (iswspace(ch) || ch == '\n' || ch == '\0') {
75*7c478bd9Sstevel@tonic-gate 			*prm = '\0';
76*7c478bd9Sstevel@tonic-gate 			return(prm == beginning ? NULL : s);
77*7c478bd9Sstevel@tonic-gate 		}
78*7c478bd9Sstevel@tonic-gate 		switch (ch) {
79*7c478bd9Sstevel@tonic-gate 		case '>':
80*7c478bd9Sstevel@tonic-gate 			if ((prm == beginning + 1) && (*beginning == '2'))
81*7c478bd9Sstevel@tonic-gate 				*prm++ = *s++;
82*7c478bd9Sstevel@tonic-gate 			if ((prm == beginning + 1) && (*beginning == '1'))
83*7c478bd9Sstevel@tonic-gate 				*beginning = *s++;
84*7c478bd9Sstevel@tonic-gate 			if (prm == beginning) {
85*7c478bd9Sstevel@tonic-gate 				width = mbtowc(&ch, s+1, MB_CUR_MAX);
86*7c478bd9Sstevel@tonic-gate 				if ((ch == '>') || (ch == '&'))
87*7c478bd9Sstevel@tonic-gate 					*prm++ = *s++;
88*7c478bd9Sstevel@tonic-gate 				*prm++ = *s++;
89*7c478bd9Sstevel@tonic-gate 			}
90*7c478bd9Sstevel@tonic-gate 			*prm = '\0';
91*7c478bd9Sstevel@tonic-gate 			return(s);
92*7c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
93*7c478bd9Sstevel@tonic-gate 			break;
94*7c478bd9Sstevel@tonic-gate 		case '<':
95*7c478bd9Sstevel@tonic-gate 			if ((prm == beginning + 1) && (*beginning == '0'))
96*7c478bd9Sstevel@tonic-gate 				*beginning = *s++;
97*7c478bd9Sstevel@tonic-gate 			if (prm == beginning) {
98*7c478bd9Sstevel@tonic-gate 				width = mbtowc(&ch, s+1, MB_CUR_MAX);
99*7c478bd9Sstevel@tonic-gate 				if (ch == '<') {
100*7c478bd9Sstevel@tonic-gate 					*prm++ = *s++;
101*7c478bd9Sstevel@tonic-gate 					*prm++ = *s++;
102*7c478bd9Sstevel@tonic-gate 					*prm = '\0';
103*7c478bd9Sstevel@tonic-gate 					return;
104*7c478bd9Sstevel@tonic-gate 				}
105*7c478bd9Sstevel@tonic-gate 				*prm++ = *s++;
106*7c478bd9Sstevel@tonic-gate 			}
107*7c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
108*7c478bd9Sstevel@tonic-gate 		case '|':
109*7c478bd9Sstevel@tonic-gate 		case ';':
110*7c478bd9Sstevel@tonic-gate 		case '&':
111*7c478bd9Sstevel@tonic-gate 		case '^':
112*7c478bd9Sstevel@tonic-gate 		case '\\':
113*7c478bd9Sstevel@tonic-gate 			if (prm == beginning)
114*7c478bd9Sstevel@tonic-gate 				*prm++ = *s++;
115*7c478bd9Sstevel@tonic-gate 			*prm = '\0';
116*7c478bd9Sstevel@tonic-gate 			return(s);
117*7c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
118*7c478bd9Sstevel@tonic-gate 			break;
119*7c478bd9Sstevel@tonic-gate 		case '\'':
120*7c478bd9Sstevel@tonic-gate 		case '(':
121*7c478bd9Sstevel@tonic-gate 		case '`':
122*7c478bd9Sstevel@tonic-gate 		case '"':
123*7c478bd9Sstevel@tonic-gate 			if (prm == beginning) {
124*7c478bd9Sstevel@tonic-gate 				rightq = ( *s == '(' ? ')' : *s );
125*7c478bd9Sstevel@tonic-gate 				c = bal(s, rightq);
126*7c478bd9Sstevel@tonic-gate 				(void) strncpy(prm, s, c-s+1);
127*7c478bd9Sstevel@tonic-gate 				prm += c - s + 1;
128*7c478bd9Sstevel@tonic-gate 				if ( *(s=c) == rightq)
129*7c478bd9Sstevel@tonic-gate 					s++;
130*7c478bd9Sstevel@tonic-gate 			}
131*7c478bd9Sstevel@tonic-gate 			*prm = '\0';
132*7c478bd9Sstevel@tonic-gate 			return(s);
133*7c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
134*7c478bd9Sstevel@tonic-gate 			break;
135*7c478bd9Sstevel@tonic-gate 		default:
136*7c478bd9Sstevel@tonic-gate 			while (width--)
137*7c478bd9Sstevel@tonic-gate 				*prm++ = *s++;
138*7c478bd9Sstevel@tonic-gate 		}
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	*prm = '\0';
142*7c478bd9Sstevel@tonic-gate 	return(prm == beginning ? NULL : s);
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate /*
146*7c478bd9Sstevel@tonic-gate  * bal - get balanced quoted string
147*7c478bd9Sstevel@tonic-gate  *
148*7c478bd9Sstevel@tonic-gate  * s - input string
149*7c478bd9Sstevel@tonic-gate  * r - right quote
150*7c478bd9Sstevel@tonic-gate  * Note: *s is the left quote
151*7c478bd9Sstevel@tonic-gate  * return:
152*7c478bd9Sstevel@tonic-gate  *  pointer to the end of the quoted string
153*7c478bd9Sstevel@tonic-gate  * Note:
154*7c478bd9Sstevel@tonic-gate  *	If the string is not balanced, it returns a pointer to the
155*7c478bd9Sstevel@tonic-gate  *	end of the string.
156*7c478bd9Sstevel@tonic-gate  */
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate static char *
159*7c478bd9Sstevel@tonic-gate bal(s, r)
160*7c478bd9Sstevel@tonic-gate register char *s;
161*7c478bd9Sstevel@tonic-gate char r;
162*7c478bd9Sstevel@tonic-gate {
163*7c478bd9Sstevel@tonic-gate 	int	width;
164*7c478bd9Sstevel@tonic-gate 	wchar_t	ch;
165*7c478bd9Sstevel@tonic-gate 	short count = 1;
166*7c478bd9Sstevel@tonic-gate 	char l;		/* left quote character */
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	for (l = *s++; *s; s+=width) {
169*7c478bd9Sstevel@tonic-gate 	    width = mbtowc(&ch, s, MB_CUR_MAX);
170*7c478bd9Sstevel@tonic-gate 	    if (*s == r) {
171*7c478bd9Sstevel@tonic-gate 		if (--count == 0)
172*7c478bd9Sstevel@tonic-gate 		    break;	/* this is the balanced end */
173*7c478bd9Sstevel@tonic-gate 	    }
174*7c478bd9Sstevel@tonic-gate 	    else if (*s == l)
175*7c478bd9Sstevel@tonic-gate 		count++;
176*7c478bd9Sstevel@tonic-gate 	}
177*7c478bd9Sstevel@tonic-gate 	return(s);
178*7c478bd9Sstevel@tonic-gate }
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate /*
181*7c478bd9Sstevel@tonic-gate  * split - split the name into parts:
182*7c478bd9Sstevel@tonic-gate  *	arg  - original string
183*7c478bd9Sstevel@tonic-gate  *	sys  - leading system name
184*7c478bd9Sstevel@tonic-gate  *	fwd  - intermediate destinations, if not NULL, otherwise
185*7c478bd9Sstevel@tonic-gate  *		only split into two parts.
186*7c478bd9Sstevel@tonic-gate  *	file - filename part
187*7c478bd9Sstevel@tonic-gate  */
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate int
190*7c478bd9Sstevel@tonic-gate split(arg, sys, fwd, file)
191*7c478bd9Sstevel@tonic-gate char *arg, *sys, *fwd, *file;
192*7c478bd9Sstevel@tonic-gate {
193*7c478bd9Sstevel@tonic-gate     register wchar_t *cl, *cr, *n;
194*7c478bd9Sstevel@tonic-gate     int retval = 0;
195*7c478bd9Sstevel@tonic-gate     wchar_t	wcbuf[MAXFULLNAME];
196*7c478bd9Sstevel@tonic-gate     wchar_t	tmpbuf[MAXFULLNAME];
197*7c478bd9Sstevel@tonic-gate     wchar_t	myname[MAXFULLNAME];
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate     *sys = *file = NULLCHAR;
200*7c478bd9Sstevel@tonic-gate     if ( fwd != (char *) NULL )
201*7c478bd9Sstevel@tonic-gate 	*fwd = NULLCHAR;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate     /* uux can use parentheses for output file names */
204*7c478bd9Sstevel@tonic-gate     /* we'll check here until  we can move it to uux */
205*7c478bd9Sstevel@tonic-gate     if (EQUALS(Progname,"uux") && (*arg == LQUOTE)) {
206*7c478bd9Sstevel@tonic-gate 	char *c;
207*7c478bd9Sstevel@tonic-gate 	c = bal(arg++, RQUOTE);
208*7c478bd9Sstevel@tonic-gate 	(void) strncpy(file, arg, c-arg);
209*7c478bd9Sstevel@tonic-gate 	file[c-arg] = NULLCHAR;
210*7c478bd9Sstevel@tonic-gate 	return(retval);
211*7c478bd9Sstevel@tonic-gate 	}
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate     mbstowcs(myname, Myname, MAXFULLNAME);
215*7c478bd9Sstevel@tonic-gate     mbstowcs(wcbuf, arg, MAXFULLNAME);
216*7c478bd9Sstevel@tonic-gate     for (n=wcbuf ;; n=cl+1) {
217*7c478bd9Sstevel@tonic-gate 	cl = wcschr(n, (wchar_t)'!');
218*7c478bd9Sstevel@tonic-gate 	if (cl == NULL) {
219*7c478bd9Sstevel@tonic-gate 	    /* no ! in n */
220*7c478bd9Sstevel@tonic-gate 	    (void) wcstombs(file, n, MAXFULLNAME);
221*7c478bd9Sstevel@tonic-gate 	    return(retval);
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	retval = 1;
225*7c478bd9Sstevel@tonic-gate 	if (cl == n)	/* leading ! */
226*7c478bd9Sstevel@tonic-gate 	    continue;
227*7c478bd9Sstevel@tonic-gate 	if (WEQUALSN(myname, n, cl - n) && myname[cl - n] == NULLCHAR)
228*7c478bd9Sstevel@tonic-gate 	    continue;
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	(void) wcsncpy(tmpbuf, n, cl-n);
231*7c478bd9Sstevel@tonic-gate 	tmpbuf[cl-n] = NULLCHAR;
232*7c478bd9Sstevel@tonic-gate 	(void) wcstombs(sys, tmpbuf, MAXFULLNAME);
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	if (fwd != (char *) NULL) {
235*7c478bd9Sstevel@tonic-gate 	    if (cl != (cr = wcsrchr(n, (wchar_t)'!'))) {
236*7c478bd9Sstevel@tonic-gate 		/*  more than one ! */
237*7c478bd9Sstevel@tonic-gate 		wcsncpy(tmpbuf, cl+1, cr-cl-1);
238*7c478bd9Sstevel@tonic-gate 		tmpbuf[cr-cl-1] = NULL;
239*7c478bd9Sstevel@tonic-gate 		(void) wcstombs(fwd, tmpbuf, MAXFULLNAME);
240*7c478bd9Sstevel@tonic-gate 	    }
241*7c478bd9Sstevel@tonic-gate 	} else {
242*7c478bd9Sstevel@tonic-gate 	    cr = cl;
243*7c478bd9Sstevel@tonic-gate 	}
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	(void) wcstombs(file, cr+1, MAXFULLNAME);
246*7c478bd9Sstevel@tonic-gate 	return(retval);
247*7c478bd9Sstevel@tonic-gate     }
248*7c478bd9Sstevel@tonic-gate     /*NOTREACHED*/
249*7c478bd9Sstevel@tonic-gate }
250*7c478bd9Sstevel@tonic-gate 
251