xref: /illumos-gate/usr/src/lib/libnsl/dial/getargs.c (revision 7c478bd9)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
27 
28 #include "uucp.h"
29 #include <rpc/trace.h>
30 
31 /*
32  * generate a vector of pointers (arps) to the
33  * substrings in string "s".
34  * Each substring is separated by blanks and/or tabs.
35  *	s	-> string to analyze -- s GETS MODIFIED
36  *	arps	-> array of pointers -- count + 1 pointers
37  *	count	-> max number of fields
38  * returns:
39  *	i	-> # of subfields
40  *	arps[i] = NULL
41  */
42 
43 GLOBAL int
44 getargs(s, arps, count)
45 register char *s, *arps[];
46 register int count;
47 {
48 	register int i;
49 
50 	trace2(TR_getargs, 0, count);
51 	for (i = 0; i < count; i++) {
52 		while (*s == ' ' || *s == '\t')
53 			*s++ = '\0';
54 		if (*s == '\n')
55 			*s = '\0';
56 		if (*s == '\0')
57 			break;
58 		arps[i] = s++;
59 		while (*s != '\0' && *s != ' '
60 			&& *s != '\t' && *s != '\n')
61 				s++;
62 	}
63 	arps[i] = NULL;
64 	trace1(TR_getargs, 1);
65 	return (i);
66 }
67 
68 /*
69  *      bsfix(args) - remove backslashes from args
70  *
71  *      \123 style strings are collapsed into a single character
72  *	\000 gets mapped into \N for further processing downline.
73  *      \ at end of string is removed
74  *	\t gets replaced by a tab
75  *	\n gets replaced by a newline
76  *	\r gets replaced by a carriage return
77  *	\b gets replaced by a backspace
78  *	\s gets replaced by a blank
79  *	any other unknown \ sequence is left intact for further processing
80  *	downline.
81  */
82 
83 GLOBAL void
84 bsfix (args)
85 char **args;
86 {
87 	register char *str, *to, *cp;
88 	register int num;
89 
90 	trace1(TR_bsfix, 0);
91 	for (; *args; args++) {
92 		str = *args;
93 		for (to = str; *str; str++) {
94 			if (*str == '\\') {
95 				if (str[1] == '\0')
96 					break;
97 				switch (*++str) {
98 				case '0':
99 				case '1':
100 				case '2':
101 				case '3':
102 				case '4':
103 				case '5':
104 				case '6':
105 				case '7':
106 					for ( num = 0, cp = str
107 					    ; cp - str < 3
108 					    ; cp++
109 					    ) {
110 						if ('0' <= *cp && *cp <= '7') {
111 							num <<= 3;
112 							num += *cp - '0';
113 						}
114 						else
115 						    break;
116 					}
117 					if (num == 0) {
118 						*to++ = '\\';
119 						*to++ = 'N';
120 					} else
121 						*to++ = (char) num;
122 					str = cp-1;
123 					break;
124 
125 				case 't':
126 					*to++ = '\t';
127 					break;
128 
129 				case 's':
130 					*to++ = ' ';
131 					break;
132 
133 				case 'n':
134 					*to++ = '\n';
135 					break;
136 
137 				case 'r':
138 					*to++ = '\r';
139 					break;
140 
141 				case 'b':
142 					*to++ = '\b';
143 					break;
144 
145 				default:
146 					*to++ = '\\';
147 					*to++ = *str;
148 					break;
149 				}
150 			}
151 			else
152 				*to++ = *str;
153 		}
154 		*to = '\0';
155 	}
156 	trace1(TR_bsfix, 1);
157 	return;
158 }
159