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