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