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