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