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