xref: /illumos-gate/usr/src/lib/libnsl/dial/getargs.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include "mt.h"
34 #include "uucp.h"
35 
36 /*
37  * generate a vector of pointers (arps) to the
38  * substrings in string "s".
39  * Each substring is separated by blanks and/or tabs.
40  *	s	-> string to analyze -- s GETS MODIFIED
41  *	arps	-> array of pointers -- count + 1 pointers
42  *	count	-> max number of fields
43  * returns:
44  *	i	-> # of subfields
45  *	arps[i] = NULL
46  */
47 
48 static int
49 getargs(char *s, char *arps[], int count)
50 {
51 	int i;
52 
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 != ' ' && *s != '\t' && *s != '\n')
62 			s++;
63 	}
64 	arps[i] = NULL;
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 static void
84 bsfix(char **args)
85 {
86 	char *str, *to, *cp;
87 	int num;
88 
89 	for (; *args; args++) {
90 		str = *args;
91 		for (to = str; *str; str++) {
92 			if (*str == '\\') {
93 				if (str[1] == '\0')
94 					break;
95 				switch (*++str) {
96 				case '0':
97 				case '1':
98 				case '2':
99 				case '3':
100 				case '4':
101 				case '5':
102 				case '6':
103 				case '7':
104 					for (num = 0, cp = str;
105 							cp - str < 3; cp++) {
106 						if ('0' <= *cp && *cp <= '7') {
107 							num <<= 3;
108 							num += *cp - '0';
109 						} else
110 						    break;
111 					}
112 					if (num == 0) {
113 						*to++ = '\\';
114 						*to++ = 'N';
115 					} else
116 						*to++ = (char)num;
117 					str = cp-1;
118 					break;
119 
120 				case 't':
121 					*to++ = '\t';
122 					break;
123 
124 				case 's':
125 					*to++ = ' ';
126 					break;
127 
128 				case 'n':
129 					*to++ = '\n';
130 					break;
131 
132 				case 'r':
133 					*to++ = '\r';
134 					break;
135 
136 				case 'b':
137 					*to++ = '\b';
138 					break;
139 
140 				default:
141 					*to++ = '\\';
142 					*to++ = *str;
143 					break;
144 				}
145 			}
146 			else
147 				*to++ = *str;
148 		}
149 		*to = '\0';
150 	}
151 }
152