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