xref: /titanic_50/usr/src/cmd/ttymon/tmparse.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include        <stdio.h>
29*7c478bd9Sstevel@tonic-gate #include	<ctype.h>
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*
32*7c478bd9Sstevel@tonic-gate  *	getword	- extract one token from the string
33*7c478bd9Sstevel@tonic-gate  *		- token delimiter is white space if getall is FALSE
34*7c478bd9Sstevel@tonic-gate  *		- token delimiter is ':' or '\0' if getall is TRUE
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate char *
getword(ptr,size,getall)37*7c478bd9Sstevel@tonic-gate getword(ptr, size, getall)
38*7c478bd9Sstevel@tonic-gate register char *ptr;	/* pointer to the string to be scanned     */
39*7c478bd9Sstevel@tonic-gate int 	 *size;		/* *size = number of characters scanned	   */
40*7c478bd9Sstevel@tonic-gate int 	 getall;	/* if TRUE, get all char until ':' or '\0' */
41*7c478bd9Sstevel@tonic-gate {
42*7c478bd9Sstevel@tonic-gate 	register char *optr,c;
43*7c478bd9Sstevel@tonic-gate 	char quoted();
44*7c478bd9Sstevel@tonic-gate 	static char word[BUFSIZ];
45*7c478bd9Sstevel@tonic-gate 	int qsize;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	*size = 0;
48*7c478bd9Sstevel@tonic-gate 	if (!getall) {
49*7c478bd9Sstevel@tonic-gate 		/* Skip all white spaces */
50*7c478bd9Sstevel@tonic-gate 		while (isspace(*ptr)) {
51*7c478bd9Sstevel@tonic-gate 			(*size)++;
52*7c478bd9Sstevel@tonic-gate 			ptr++;
53*7c478bd9Sstevel@tonic-gate 		}
54*7c478bd9Sstevel@tonic-gate 	}
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	/* Put all characters from here to next white space or ':' or '\0' */
57*7c478bd9Sstevel@tonic-gate 	/* into the word, up to the size of the word. */
58*7c478bd9Sstevel@tonic-gate 	for (optr= word,*optr='\0';
59*7c478bd9Sstevel@tonic-gate 		*ptr != '\0' && *ptr != ':'; ptr++,(*size)++) {
60*7c478bd9Sstevel@tonic-gate 		if (!getall) {
61*7c478bd9Sstevel@tonic-gate 			if (isspace(*ptr))
62*7c478bd9Sstevel@tonic-gate 				break;
63*7c478bd9Sstevel@tonic-gate 		}
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 		/* If the character is quoted, analyze it. */
66*7c478bd9Sstevel@tonic-gate 		if (*ptr == '\\') {
67*7c478bd9Sstevel@tonic-gate 			c = quoted(ptr,&qsize);
68*7c478bd9Sstevel@tonic-gate 			(*size) += qsize;
69*7c478bd9Sstevel@tonic-gate 			ptr += qsize;
70*7c478bd9Sstevel@tonic-gate 		} else c = *ptr;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 		/* If there is room, add this character to the word. */
73*7c478bd9Sstevel@tonic-gate 		if (optr < &word[BUFSIZ] ) *optr++ = c;
74*7c478bd9Sstevel@tonic-gate 	}
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	/* skip trailing blanks if any*/
77*7c478bd9Sstevel@tonic-gate 	while (isspace(*ptr)) {
78*7c478bd9Sstevel@tonic-gate 		(*size)++;
79*7c478bd9Sstevel@tonic-gate 		ptr++;
80*7c478bd9Sstevel@tonic-gate 	}
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	/* Make sure the line is null terminated. */
83*7c478bd9Sstevel@tonic-gate 	*optr++ = '\0';
84*7c478bd9Sstevel@tonic-gate 	return(word);
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate /*	"quoted" takes a quoted character, starting at the quote	*/
88*7c478bd9Sstevel@tonic-gate /*	character, and returns a single character plus the size of	*/
89*7c478bd9Sstevel@tonic-gate /*	the quote string.  "quoted" recognizes the following as		*/
90*7c478bd9Sstevel@tonic-gate /*	special, \n,\r,\v,\t,\b,\f as well as the \nnn notation.	*/
91*7c478bd9Sstevel@tonic-gate char
quoted(ptr,qsize)92*7c478bd9Sstevel@tonic-gate quoted(ptr,qsize)
93*7c478bd9Sstevel@tonic-gate char *ptr;
94*7c478bd9Sstevel@tonic-gate int *qsize;
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	register char c,*rptr;
97*7c478bd9Sstevel@tonic-gate 	register int i;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	rptr = ptr;
100*7c478bd9Sstevel@tonic-gate 	switch(*++rptr) {
101*7c478bd9Sstevel@tonic-gate 	case 'n':
102*7c478bd9Sstevel@tonic-gate 		c = '\n';
103*7c478bd9Sstevel@tonic-gate 		break;
104*7c478bd9Sstevel@tonic-gate 	case 'r':
105*7c478bd9Sstevel@tonic-gate 		c = '\r';
106*7c478bd9Sstevel@tonic-gate 		break;
107*7c478bd9Sstevel@tonic-gate 	case 'v':
108*7c478bd9Sstevel@tonic-gate 		c = '\013';
109*7c478bd9Sstevel@tonic-gate 		break;
110*7c478bd9Sstevel@tonic-gate 	case 'b':
111*7c478bd9Sstevel@tonic-gate 		c = '\b';
112*7c478bd9Sstevel@tonic-gate 		break;
113*7c478bd9Sstevel@tonic-gate 	case 't':
114*7c478bd9Sstevel@tonic-gate 		c = '\t';
115*7c478bd9Sstevel@tonic-gate 		break;
116*7c478bd9Sstevel@tonic-gate 	case 'f':
117*7c478bd9Sstevel@tonic-gate 		c = '\f';
118*7c478bd9Sstevel@tonic-gate 		break;
119*7c478bd9Sstevel@tonic-gate 	case ':':
120*7c478bd9Sstevel@tonic-gate 		c = ':';
121*7c478bd9Sstevel@tonic-gate 		break;
122*7c478bd9Sstevel@tonic-gate 	default:
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate /* If this is a numeric string, take up to three characters of */
125*7c478bd9Sstevel@tonic-gate /* it as the value of the quoted character. */
126*7c478bd9Sstevel@tonic-gate 		if (*rptr >= '0' && *rptr <= '7') {
127*7c478bd9Sstevel@tonic-gate 			for (i=0,c=0; i < 3;i++) {
128*7c478bd9Sstevel@tonic-gate 				c = c*8 + (*rptr - '0');
129*7c478bd9Sstevel@tonic-gate 				if (*++rptr < '0' || *rptr > '7') break;
130*7c478bd9Sstevel@tonic-gate 			}
131*7c478bd9Sstevel@tonic-gate 			rptr--;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate /* If the character following the '\\' is a NULL, back up the */
134*7c478bd9Sstevel@tonic-gate /* ptr so that the NULL won't be missed.  The sequence */
135*7c478bd9Sstevel@tonic-gate /* backslash null is essentually illegal. */
136*7c478bd9Sstevel@tonic-gate 		} else if (*rptr == '\0') {
137*7c478bd9Sstevel@tonic-gate 			c = '\0';
138*7c478bd9Sstevel@tonic-gate 			rptr--;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 		/* In all other cases the quoting does nothing. */
141*7c478bd9Sstevel@tonic-gate 		} else c = *rptr;
142*7c478bd9Sstevel@tonic-gate 		break;
143*7c478bd9Sstevel@tonic-gate 	}
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	/* Compute the size of the quoted character. */
146*7c478bd9Sstevel@tonic-gate 	(*qsize) = rptr - ptr;
147*7c478bd9Sstevel@tonic-gate 	return(c);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate 
150