xref: /titanic_50/usr/src/cmd/sgs/lex/common/yyless.c (revision b3700b074e637f8c6991b70754c88a2cfffb246b)
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) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <sys/euc.h>
34 #include <stdlib.h>
35 #include <widec.h>
36 #include <limits.h>
37 #include <inttypes.h>
38 #include <unistd.h>
39 
40 #pragma weak yyprevious
41 extern int yyprevious;
42 
43 #ifndef JLSLEX
44 #define	CHR    char
45 
46 #pragma weak yyinput
47 #pragma weak yyleng
48 #pragma weak yyunput
49 #pragma weak yytext
50 extern CHR yytext[];
51 
52 #define	YYTEXT yytext
53 #define	YYLENG yyleng
54 #define	YYINPUT yyinput
55 #define	YYUNPUT yyunput
56 #define	YYOUTPUT yyoutput
57 #endif
58 
59 #ifdef WOPTION
60 #define	CHR    wchar_t
61 
62 #pragma weak yyinput
63 #pragma weak yyleng
64 #pragma weak yyunput
65 #pragma weak yytext
66 extern CHR yytext[];
67 
68 #define	YYTEXT yytext
69 #define	YYLENG yyleng
70 #define	YYINPUT yyinput
71 #define	YYUNPUT yyunput
72 #define	YYOUTPUT yyoutput
73 #endif
74 
75 #ifdef EOPTION
76 #define	CHR    wchar_t
77 
78 #pragma weak yyleng
79 extern int yyleng;
80 #pragma weak yytext
81 extern CHR yytext[];
82 #pragma weak yywinput
83 #pragma weak yywleng
84 #pragma weak yywunput
85 #pragma weak yywtext
86 extern CHR yywtext[];
87 
88 #define	YYTEXT yywtext
89 #define	YYLENG yywleng
90 #define	YYINPUT yywinput
91 #define	YYUNPUT yywunput
92 #define	YYOUTPUT yywoutput
93 #endif
94 
95 extern int YYLENG;
96 #if defined(__STDC__)
97     extern void YYUNPUT(int);
98 #endif
99 
100 #if defined(__cplusplus) || defined(__STDC__)
101 /* XCU4: type of yyless() changes to int */
102 int
103 yyless(int x)
104 #else
105 yyless(x)
106 int x;
107 #endif
108 {
109 	register CHR *lastch, *ptr;
110 
111 	lastch = YYTEXT+YYLENG;
112 	if (x >= 0 && x <= YYLENG)
113 		ptr = x + YYTEXT;
114 	else {
115 #ifdef	_LP64
116 		static int seen = 0;
117 
118 		if (!seen) {
119 			(void) write(2,
120 				    "warning: yyless pointer arg truncated\n",
121 				    39);
122 			seen = 1;
123 		}
124 #endif	/* _LP64 */
125 	/*
126 	 * The cast on the next line papers over an unconscionable nonportable
127 	 * glitch to allow the caller to hand the function a pointer instead of
128 	 * an integer and hope that it gets figured out properly.  But it's
129 	 * that way on all systems.
130 	 */
131 		ptr = (CHR *)(intptr_t)x;
132 	}
133 	while (lastch > ptr)
134 		YYUNPUT(*--lastch);
135 	*lastch = 0;
136 	if (ptr > YYTEXT)
137 		yyprevious = *--lastch;
138 	YYLENG = ptr-YYTEXT;
139 #ifdef EOPTION
140 	yyleng = wcstombs((char *)yytext, YYTEXT, YYLENG*MB_LEN_MAX);
141 #endif
142 	return (0);
143 }
144