xref: /titanic_52/usr/src/cmd/ttymon/tmparse.c (revision 6185db853e024a486ff8837e6784dd290d866112)
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        <stdio.h>
29 #include	<ctype.h>
30 
31 /*
32  *	getword	- extract one token from the string
33  *		- token delimiter is white space if getall is FALSE
34  *		- token delimiter is ':' or '\0' if getall is TRUE
35  */
36 char *
37 getword(ptr, size, getall)
38 register char *ptr;	/* pointer to the string to be scanned     */
39 int 	 *size;		/* *size = number of characters scanned	   */
40 int 	 getall;	/* if TRUE, get all char until ':' or '\0' */
41 {
42 	register char *optr,c;
43 	char quoted();
44 	static char word[BUFSIZ];
45 	int qsize;
46 
47 	*size = 0;
48 	if (!getall) {
49 		/* Skip all white spaces */
50 		while (isspace(*ptr)) {
51 			(*size)++;
52 			ptr++;
53 		}
54 	}
55 
56 	/* Put all characters from here to next white space or ':' or '\0' */
57 	/* into the word, up to the size of the word. */
58 	for (optr= word,*optr='\0';
59 		*ptr != '\0' && *ptr != ':'; ptr++,(*size)++) {
60 		if (!getall) {
61 			if (isspace(*ptr))
62 				break;
63 		}
64 
65 		/* If the character is quoted, analyze it. */
66 		if (*ptr == '\\') {
67 			c = quoted(ptr,&qsize);
68 			(*size) += qsize;
69 			ptr += qsize;
70 		} else c = *ptr;
71 
72 		/* If there is room, add this character to the word. */
73 		if (optr < &word[BUFSIZ] ) *optr++ = c;
74 	}
75 
76 	/* skip trailing blanks if any*/
77 	while (isspace(*ptr)) {
78 		(*size)++;
79 		ptr++;
80 	}
81 
82 	/* Make sure the line is null terminated. */
83 	*optr++ = '\0';
84 	return(word);
85 }
86 
87 /*	"quoted" takes a quoted character, starting at the quote	*/
88 /*	character, and returns a single character plus the size of	*/
89 /*	the quote string.  "quoted" recognizes the following as		*/
90 /*	special, \n,\r,\v,\t,\b,\f as well as the \nnn notation.	*/
91 char
92 quoted(ptr,qsize)
93 char *ptr;
94 int *qsize;
95 {
96 	register char c,*rptr;
97 	register int i;
98 
99 	rptr = ptr;
100 	switch(*++rptr) {
101 	case 'n':
102 		c = '\n';
103 		break;
104 	case 'r':
105 		c = '\r';
106 		break;
107 	case 'v':
108 		c = '\013';
109 		break;
110 	case 'b':
111 		c = '\b';
112 		break;
113 	case 't':
114 		c = '\t';
115 		break;
116 	case 'f':
117 		c = '\f';
118 		break;
119 	case ':':
120 		c = ':';
121 		break;
122 	default:
123 
124 /* If this is a numeric string, take up to three characters of */
125 /* it as the value of the quoted character. */
126 		if (*rptr >= '0' && *rptr <= '7') {
127 			for (i=0,c=0; i < 3;i++) {
128 				c = c*8 + (*rptr - '0');
129 				if (*++rptr < '0' || *rptr > '7') break;
130 			}
131 			rptr--;
132 
133 /* If the character following the '\\' is a NULL, back up the */
134 /* ptr so that the NULL won't be missed.  The sequence */
135 /* backslash null is essentually illegal. */
136 		} else if (*rptr == '\0') {
137 			c = '\0';
138 			rptr--;
139 
140 		/* In all other cases the quoting does nothing. */
141 		} else c = *rptr;
142 		break;
143 	}
144 
145 	/* Compute the size of the quoted character. */
146 	(*qsize) = rptr - ptr;
147 	return(c);
148 }
149 
150