xref: /titanic_52/usr/src/cmd/bnu/getargs.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 1998 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 /*
36*7c478bd9Sstevel@tonic-gate  * generate a vector of pointers (arps) to the
37*7c478bd9Sstevel@tonic-gate  * substrings in string "s".
38*7c478bd9Sstevel@tonic-gate  * Each substring is separated by blanks and/or tabs.
39*7c478bd9Sstevel@tonic-gate  *	s	-> string to analyze -- s GETS MODIFIED
40*7c478bd9Sstevel@tonic-gate  *	arps	-> array of pointers -- count + 1 pointers
41*7c478bd9Sstevel@tonic-gate  *	count	-> max number of fields
42*7c478bd9Sstevel@tonic-gate  * returns:
43*7c478bd9Sstevel@tonic-gate  *	i	-> # of subfields
44*7c478bd9Sstevel@tonic-gate  *	arps[i] = NULL
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate GLOBAL int
48*7c478bd9Sstevel@tonic-gate getargs(s, arps, count)
49*7c478bd9Sstevel@tonic-gate register char *s, *arps[];
50*7c478bd9Sstevel@tonic-gate register int count;
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 	register int i;
53*7c478bd9Sstevel@tonic-gate 	char	*prev;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	prev = _uu_setlocale(LC_ALL, "C");
56*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
57*7c478bd9Sstevel@tonic-gate 		while (*s == ' ' || *s == '\t')
58*7c478bd9Sstevel@tonic-gate 			*s++ = '\0';
59*7c478bd9Sstevel@tonic-gate 		if (*s == '\n')
60*7c478bd9Sstevel@tonic-gate 			*s = '\0';
61*7c478bd9Sstevel@tonic-gate 		if (*s == '\0')
62*7c478bd9Sstevel@tonic-gate 			break;
63*7c478bd9Sstevel@tonic-gate 		arps[i] = s++;
64*7c478bd9Sstevel@tonic-gate 		while (*s != '\0' && *s != ' '
65*7c478bd9Sstevel@tonic-gate 			&& *s != '\t' && *s != '\n')
66*7c478bd9Sstevel@tonic-gate 				s++;
67*7c478bd9Sstevel@tonic-gate 	}
68*7c478bd9Sstevel@tonic-gate 	arps[i] = NULL;
69*7c478bd9Sstevel@tonic-gate 	(void) _uu_resetlocale(LC_ALL, prev);
70*7c478bd9Sstevel@tonic-gate 	return(i);
71*7c478bd9Sstevel@tonic-gate }
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate /*
74*7c478bd9Sstevel@tonic-gate  *      bsfix(args) - remove backslashes from args
75*7c478bd9Sstevel@tonic-gate  *
76*7c478bd9Sstevel@tonic-gate  *      \123 style strings are collapsed into a single character
77*7c478bd9Sstevel@tonic-gate  *	\000 gets mapped into \N for further processing downline.
78*7c478bd9Sstevel@tonic-gate  *      \ at end of string is removed
79*7c478bd9Sstevel@tonic-gate  *	\t gets replaced by a tab
80*7c478bd9Sstevel@tonic-gate  *	\n gets replaced by a newline
81*7c478bd9Sstevel@tonic-gate  *	\r gets replaced by a carriage return
82*7c478bd9Sstevel@tonic-gate  *	\b gets replaced by a backspace
83*7c478bd9Sstevel@tonic-gate  *	\s gets replaced by a blank
84*7c478bd9Sstevel@tonic-gate  *	any other unknown \ sequence is left intact for further processing
85*7c478bd9Sstevel@tonic-gate  *	downline.
86*7c478bd9Sstevel@tonic-gate  */
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate GLOBAL void
89*7c478bd9Sstevel@tonic-gate bsfix (args)
90*7c478bd9Sstevel@tonic-gate char **args;
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	register char *str, *to, *cp;
93*7c478bd9Sstevel@tonic-gate 	register int num;
94*7c478bd9Sstevel@tonic-gate 	char	*prev;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	prev = _uu_setlocale(LC_ALL, "C");
97*7c478bd9Sstevel@tonic-gate 	for (; *args; args++) {
98*7c478bd9Sstevel@tonic-gate 		str = *args;
99*7c478bd9Sstevel@tonic-gate 		for (to = str; *str; str++) {
100*7c478bd9Sstevel@tonic-gate 			if (*str == '\\') {
101*7c478bd9Sstevel@tonic-gate 				if (str[1] == '\0')
102*7c478bd9Sstevel@tonic-gate 					break;
103*7c478bd9Sstevel@tonic-gate 				switch (*++str) {
104*7c478bd9Sstevel@tonic-gate 				case '0':
105*7c478bd9Sstevel@tonic-gate 				case '1':
106*7c478bd9Sstevel@tonic-gate 				case '2':
107*7c478bd9Sstevel@tonic-gate 				case '3':
108*7c478bd9Sstevel@tonic-gate 				case '4':
109*7c478bd9Sstevel@tonic-gate 				case '5':
110*7c478bd9Sstevel@tonic-gate 				case '6':
111*7c478bd9Sstevel@tonic-gate 				case '7':
112*7c478bd9Sstevel@tonic-gate 					for ( num = 0, cp = str
113*7c478bd9Sstevel@tonic-gate 					    ; cp - str < 3
114*7c478bd9Sstevel@tonic-gate 					    ; cp++
115*7c478bd9Sstevel@tonic-gate 					    ) {
116*7c478bd9Sstevel@tonic-gate 						if ('0' <= *cp && *cp <= '7') {
117*7c478bd9Sstevel@tonic-gate 							num <<= 3;
118*7c478bd9Sstevel@tonic-gate 							num += *cp - '0';
119*7c478bd9Sstevel@tonic-gate 						}
120*7c478bd9Sstevel@tonic-gate 						else
121*7c478bd9Sstevel@tonic-gate 						    break;
122*7c478bd9Sstevel@tonic-gate 					}
123*7c478bd9Sstevel@tonic-gate 					if (num == 0) {
124*7c478bd9Sstevel@tonic-gate 						*to++ = '\\';
125*7c478bd9Sstevel@tonic-gate 						*to++ = 'N';
126*7c478bd9Sstevel@tonic-gate 					} else
127*7c478bd9Sstevel@tonic-gate 						*to++ = (char) num;
128*7c478bd9Sstevel@tonic-gate 					str = cp-1;
129*7c478bd9Sstevel@tonic-gate 					break;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 				case 't':
132*7c478bd9Sstevel@tonic-gate 					*to++ = '\t';
133*7c478bd9Sstevel@tonic-gate 					break;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 				case 's':
136*7c478bd9Sstevel@tonic-gate 					*to++ = ' ';
137*7c478bd9Sstevel@tonic-gate 					break;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 				case 'n':
140*7c478bd9Sstevel@tonic-gate 					*to++ = '\n';
141*7c478bd9Sstevel@tonic-gate 					break;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 				case 'r':
144*7c478bd9Sstevel@tonic-gate 					*to++ = '\r';
145*7c478bd9Sstevel@tonic-gate 					break;
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 				case 'b':
148*7c478bd9Sstevel@tonic-gate 					*to++ = '\b';
149*7c478bd9Sstevel@tonic-gate 					break;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 				default:
152*7c478bd9Sstevel@tonic-gate 					*to++ = '\\';
153*7c478bd9Sstevel@tonic-gate 					*to++ = *str;
154*7c478bd9Sstevel@tonic-gate 					break;
155*7c478bd9Sstevel@tonic-gate 				}
156*7c478bd9Sstevel@tonic-gate 			}
157*7c478bd9Sstevel@tonic-gate 			else
158*7c478bd9Sstevel@tonic-gate 				*to++ = *str;
159*7c478bd9Sstevel@tonic-gate 		}
160*7c478bd9Sstevel@tonic-gate 		*to = '\0';
161*7c478bd9Sstevel@tonic-gate 	}
162*7c478bd9Sstevel@tonic-gate 	(void) _uu_resetlocale(LC_ALL, prev);
163*7c478bd9Sstevel@tonic-gate 	return;
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate /*
167*7c478bd9Sstevel@tonic-gate ** This routine is used so that the current
168*7c478bd9Sstevel@tonic-gate ** locale can be saved and then restored.
169*7c478bd9Sstevel@tonic-gate */
170*7c478bd9Sstevel@tonic-gate char *
171*7c478bd9Sstevel@tonic-gate _uu_setlocale(int category, char *locale)
172*7c478bd9Sstevel@tonic-gate {
173*7c478bd9Sstevel@tonic-gate 	char *tmp, *ret;
174*7c478bd9Sstevel@tonic-gate 	int len;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	/* get current locale */
177*7c478bd9Sstevel@tonic-gate 	if ((tmp = setlocale(category, NULL)) == NULL)
178*7c478bd9Sstevel@tonic-gate 		return (NULL);
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	/* allocate space for the current locale and copy it */
181*7c478bd9Sstevel@tonic-gate 	len = strlen(tmp) + 1;
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	if ((ret = malloc(len)) == NULL)
184*7c478bd9Sstevel@tonic-gate 		return ((char *) 0);
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	strncpy(ret, tmp, len);
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	/* now set the new locale */
189*7c478bd9Sstevel@tonic-gate 	if (setlocale(category, locale) == NULL) {
190*7c478bd9Sstevel@tonic-gate 		free(ret);
191*7c478bd9Sstevel@tonic-gate 		return ((char *) 0);
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 	return (ret);
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate /*
197*7c478bd9Sstevel@tonic-gate ** Reset the previous locale, free memory
198*7c478bd9Sstevel@tonic-gate */
199*7c478bd9Sstevel@tonic-gate void
200*7c478bd9Sstevel@tonic-gate _uu_resetlocale(int category, char *locale)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	if (locale == NULL)
203*7c478bd9Sstevel@tonic-gate 		return;
204*7c478bd9Sstevel@tonic-gate 	(void) setlocale(category, locale);
205*7c478bd9Sstevel@tonic-gate 	free(locale);
206*7c478bd9Sstevel@tonic-gate }
207