xref: /titanic_44/usr/src/lib/libnsl/dial/getargs.c (revision 61961e0f20c7637a3846bb39786bb9dffa91dfb9)
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 2005 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 "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 static int
48 getargs(char *s, char *arps[], int count)
49 {
50 	int i;
51 
52 	for (i = 0; i < count; i++) {
53 		while (*s == ' ' || *s == '\t')
54 			*s++ = '\0';
55 		if (*s == '\n')
56 			*s = '\0';
57 		if (*s == '\0')
58 			break;
59 		arps[i] = s++;
60 		while (*s != '\0' && *s != ' ' && *s != '\t' && *s != '\n')
61 			s++;
62 	}
63 	arps[i] = NULL;
64 	return (i);
65 }
66 
67 /*
68  *      bsfix(args) - remove backslashes from args
69  *
70  *      \123 style strings are collapsed into a single character
71  *	\000 gets mapped into \N for further processing downline.
72  *      \ at end of string is removed
73  *	\t gets replaced by a tab
74  *	\n gets replaced by a newline
75  *	\r gets replaced by a carriage return
76  *	\b gets replaced by a backspace
77  *	\s gets replaced by a blank
78  *	any other unknown \ sequence is left intact for further processing
79  *	downline.
80  */
81 
82 static void
83 bsfix(char **args)
84 {
85 	char *str, *to, *cp;
86 	int num;
87 
88 	for (; *args; args++) {
89 		str = *args;
90 		for (to = str; *str; str++) {
91 			if (*str == '\\') {
92 				if (str[1] == '\0')
93 					break;
94 				switch (*++str) {
95 				case '0':
96 				case '1':
97 				case '2':
98 				case '3':
99 				case '4':
100 				case '5':
101 				case '6':
102 				case '7':
103 					for (num = 0, cp = str;
104 							cp - str < 3; cp++) {
105 						if ('0' <= *cp && *cp <= '7') {
106 							num <<= 3;
107 							num += *cp - '0';
108 						} else
109 						    break;
110 					}
111 					if (num == 0) {
112 						*to++ = '\\';
113 						*to++ = 'N';
114 					} else
115 						*to++ = (char)num;
116 					str = cp-1;
117 					break;
118 
119 				case 't':
120 					*to++ = '\t';
121 					break;
122 
123 				case 's':
124 					*to++ = ' ';
125 					break;
126 
127 				case 'n':
128 					*to++ = '\n';
129 					break;
130 
131 				case 'r':
132 					*to++ = '\r';
133 					break;
134 
135 				case 'b':
136 					*to++ = '\b';
137 					break;
138 
139 				default:
140 					*to++ = '\\';
141 					*to++ = *str;
142 					break;
143 				}
144 			}
145 			else
146 				*to++ = *str;
147 		}
148 		*to = '\0';
149 	}
150 }
151