xref: /illumos-gate/usr/src/cmd/sgs/m4/common/m4.h (revision 447603b5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54c56998aSRich Burridge  * Common Development and Distribution License (the "License").
64c56998aSRich Burridge  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
264c56998aSRich Burridge  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
29*447603b5SGary Mills /*
30*447603b5SGary Mills  * Copyright (c) 2011 Gary Mills
31*447603b5SGary Mills  */
32*447603b5SGary Mills 
334c56998aSRich Burridge #ifndef	_M4_H
344c56998aSRich Burridge #define	_M4_H
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include	<ctype.h>
377c478bd9Sstevel@tonic-gate #include	<stdio.h>
387c478bd9Sstevel@tonic-gate #include	<stdlib.h>
397c478bd9Sstevel@tonic-gate #include	<string.h>
40*447603b5SGary Mills #include	<errno.h>
417c478bd9Sstevel@tonic-gate #include	<locale.h>
427c478bd9Sstevel@tonic-gate #include	<limits.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include	<wchar.h>
457c478bd9Sstevel@tonic-gate #include	<wctype.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	EOS	((wchar_t)0)
487c478bd9Sstevel@tonic-gate #define	MAXSYM	5
497c478bd9Sstevel@tonic-gate #define	PUSH	1
507c478bd9Sstevel@tonic-gate #define	NOPUSH	0
517c478bd9Sstevel@tonic-gate #define	OK	0
527c478bd9Sstevel@tonic-gate #define	NOT_OK	1
537c478bd9Sstevel@tonic-gate 
544c56998aSRich Burridge /* Used in m4.c and m4ext.c */
554c56998aSRich Burridge #define	DEF_HSHSIZE	199	/* default hash table size (prime). */
564c56998aSRich Burridge #define	DEF_BUFSIZE	4096	/* default pushback & arg text buffers size */
574c56998aSRich Burridge #define	DEF_STKSIZE	100	/* default call stack size */
584c56998aSRich Burridge #define	DEF_TOKSIZE	512	/* default token buffer size */
594c56998aSRich Burridge 
607c478bd9Sstevel@tonic-gate #define	BUILTIN		0x40000000
617c478bd9Sstevel@tonic-gate #define	INVALID_CHAR	0x80000000
627c478bd9Sstevel@tonic-gate #define	builtin(x)	((x) | BUILTIN)
637c478bd9Sstevel@tonic-gate #define	builtin_idx(x)	((x) & (wchar_t)~BUILTIN)
647c478bd9Sstevel@tonic-gate #define	is_builtin(x)	((x) != WEOF && ((x) & BUILTIN))
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * Since we have expanded char to wchar_t, large vaule(has BUILTIN set)
687c478bd9Sstevel@tonic-gate  * can be given to the ctype macros. First check BUILTIN, and return
697c478bd9Sstevel@tonic-gate  * FALSE if it was set. EOF/WEOF will be in this case.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate #define	is_alpha(x)	(!is_builtin(x) && \
727c478bd9Sstevel@tonic-gate 				(wide ? iswalpha(x) : isalpha(x)))
737c478bd9Sstevel@tonic-gate #define	is_alnum(x)	(!is_builtin(x) && \
747c478bd9Sstevel@tonic-gate 				(wide ? iswalnum(x) : isalnum(x)))
757c478bd9Sstevel@tonic-gate #define	is_space(x)	(!is_builtin(x) && \
767c478bd9Sstevel@tonic-gate 				(wide ? iswspace(x) : isspace(x)))
777c478bd9Sstevel@tonic-gate #define	is_digit(x)	(!is_builtin(x) && iswascii(x) && isdigit(x))
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate struct bs {
817c478bd9Sstevel@tonic-gate 	void	(*bfunc)(wchar_t **, int);
827c478bd9Sstevel@tonic-gate 	wchar_t	*bname;
837c478bd9Sstevel@tonic-gate };
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate struct	call {
867c478bd9Sstevel@tonic-gate 	wchar_t	**argp;
877c478bd9Sstevel@tonic-gate 	int	plev;
887c478bd9Sstevel@tonic-gate };
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate struct	nlist {
917c478bd9Sstevel@tonic-gate 	wchar_t	*name;
927c478bd9Sstevel@tonic-gate 	wchar_t	*def;
937c478bd9Sstevel@tonic-gate 	char	tflag;
947c478bd9Sstevel@tonic-gate 	struct	nlist *next;
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate struct Wrap {
987c478bd9Sstevel@tonic-gate 	wchar_t *wrapstr;
997c478bd9Sstevel@tonic-gate 	struct Wrap *nxt;
1007c478bd9Sstevel@tonic-gate };
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate typedef struct {
1037c478bd9Sstevel@tonic-gate 	unsigned char buffer[MB_LEN_MAX + 1];
1047c478bd9Sstevel@tonic-gate 	char nbytes;
1057c478bd9Sstevel@tonic-gate } ibuf_t;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate extern FILE	*cf;
1087c478bd9Sstevel@tonic-gate extern FILE	*ifile[];
1097c478bd9Sstevel@tonic-gate extern FILE	*ofile[];
1107c478bd9Sstevel@tonic-gate extern FILE	*xfopen(char *, char *);
1117c478bd9Sstevel@tonic-gate extern wchar_t	**Ap;
1127c478bd9Sstevel@tonic-gate extern wchar_t	**argstk;
1137c478bd9Sstevel@tonic-gate extern wchar_t	*astklm;
1147c478bd9Sstevel@tonic-gate extern void	*xmalloc(size_t);
1157c478bd9Sstevel@tonic-gate extern char	*fname[];
1167c478bd9Sstevel@tonic-gate extern wchar_t	*ibuf;
1177c478bd9Sstevel@tonic-gate extern wchar_t	*ibuflm;
1187c478bd9Sstevel@tonic-gate extern wchar_t	*ip;
1197c478bd9Sstevel@tonic-gate extern wchar_t	*ipflr;
1207c478bd9Sstevel@tonic-gate extern wchar_t	*ipstk[10];
1217c478bd9Sstevel@tonic-gate extern wchar_t	*obuf;
1227c478bd9Sstevel@tonic-gate extern wchar_t	*obuflm;
1237c478bd9Sstevel@tonic-gate extern wchar_t	*op;
1247c478bd9Sstevel@tonic-gate extern char	*procnam;
1257c478bd9Sstevel@tonic-gate extern char	*tempfile;
1267c478bd9Sstevel@tonic-gate extern wchar_t	*token;
1277c478bd9Sstevel@tonic-gate extern wchar_t	*toklm;
1287c478bd9Sstevel@tonic-gate extern wchar_t	C;
1297c478bd9Sstevel@tonic-gate extern wchar_t	getchr();
1307c478bd9Sstevel@tonic-gate extern wchar_t	lcom[];
1317c478bd9Sstevel@tonic-gate extern wchar_t	lquote[];
1327c478bd9Sstevel@tonic-gate extern wchar_t	nullstr[];
1337c478bd9Sstevel@tonic-gate extern wchar_t	rcom[];
1347c478bd9Sstevel@tonic-gate extern wchar_t	rquote[];
1357c478bd9Sstevel@tonic-gate extern int	bufsize;
1367c478bd9Sstevel@tonic-gate extern int	fline[];
1377c478bd9Sstevel@tonic-gate extern int	hshsize;
1387c478bd9Sstevel@tonic-gate extern unsigned int	hshval;
1397c478bd9Sstevel@tonic-gate extern int	ifx;
1407c478bd9Sstevel@tonic-gate extern int	nflag;
1417c478bd9Sstevel@tonic-gate extern int	ofx;
1427c478bd9Sstevel@tonic-gate extern int	sflag;
1437c478bd9Sstevel@tonic-gate extern int	stksize;
1447c478bd9Sstevel@tonic-gate extern int	sysrval;
1457c478bd9Sstevel@tonic-gate extern int	toksize;
1467c478bd9Sstevel@tonic-gate extern int	trace;
1477c478bd9Sstevel@tonic-gate extern int	exitstat;
1487c478bd9Sstevel@tonic-gate extern long	ctol(wchar_t *);
1497c478bd9Sstevel@tonic-gate extern struct bs	barray[];
1507c478bd9Sstevel@tonic-gate extern struct call	*Cp;
1517c478bd9Sstevel@tonic-gate extern struct call	*callst;
1527c478bd9Sstevel@tonic-gate extern struct nlist	**hshtab;
1537c478bd9Sstevel@tonic-gate extern void	install();
1547c478bd9Sstevel@tonic-gate extern struct nlist	*lookup();
1557c478bd9Sstevel@tonic-gate extern struct Wrap	*wrapstart;
1567c478bd9Sstevel@tonic-gate extern int	wide;
1577c478bd9Sstevel@tonic-gate extern ibuf_t	ibuffer[];
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate extern void setfname(char *);
1607c478bd9Sstevel@tonic-gate extern void pbstr(wchar_t *);
1617c478bd9Sstevel@tonic-gate extern void pbnum(long);
1627c478bd9Sstevel@tonic-gate extern void pbnbr(long, int, int);
1637c478bd9Sstevel@tonic-gate extern void undiv(int, int);
1647c478bd9Sstevel@tonic-gate extern void delexit(int, int);
1657c478bd9Sstevel@tonic-gate extern void error(char *);
1667c478bd9Sstevel@tonic-gate extern int min(int, int);
1677c478bd9Sstevel@tonic-gate extern void putbak(wchar_t);
1687c478bd9Sstevel@tonic-gate extern void stkchr(wchar_t);
169*447603b5SGary Mills extern void errorf(char *, char *);
1707c478bd9Sstevel@tonic-gate extern void error2(char *, int);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate extern wchar_t *wstrdup(wchar_t *);
1737c478bd9Sstevel@tonic-gate extern int wstoi(wchar_t *);
1747c478bd9Sstevel@tonic-gate extern char *wstr2str(wchar_t *, int);
1757c478bd9Sstevel@tonic-gate extern wchar_t *str2wstr(char *, int);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate extern void dodef(wchar_t **, int);
1787c478bd9Sstevel@tonic-gate extern void doundef(wchar_t **, int);
1797c478bd9Sstevel@tonic-gate extern int undef(wchar_t *);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * macros for performance reason.
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate #define	putbak(c)	\
1857c478bd9Sstevel@tonic-gate 	if (ip < ibuflm)	\
1867c478bd9Sstevel@tonic-gate 		*ip++ = (c);	\
1877c478bd9Sstevel@tonic-gate 	else	\
1887c478bd9Sstevel@tonic-gate 		error2(gettext("pushed back more than %d chars"), bufsize)
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate #define	stkchr(c)	\
1917c478bd9Sstevel@tonic-gate 	if (op < obuflm)	\
1927c478bd9Sstevel@tonic-gate 		*op++ = (c);	\
1937c478bd9Sstevel@tonic-gate 	else	\
1947c478bd9Sstevel@tonic-gate 		error2(gettext("more than %d chars of argument text"), bufsize)
1954c56998aSRich Burridge 
1964c56998aSRich Burridge #endif	/* _M4_H */
197