xref: /illumos-gate/usr/src/cmd/rpcgen/rpc_scan.c (revision a2f144d1)
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
5*a2f144d1SJordan Brown  * Common Development and Distribution License (the "License").
6*a2f144d1SJordan Brown  * 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
2061961e0fSrobinson  */
2161961e0fSrobinson 
2261961e0fSrobinson /*
23*a2f144d1SJordan Brown  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
307c478bd9Sstevel@tonic-gate  * The Regents of the University of California
317c478bd9Sstevel@tonic-gate  * All Rights Reserved
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
347c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
357c478bd9Sstevel@tonic-gate  * contributors.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
3961961e0fSrobinson  * rpc_scan.c, Scanner for the RPC protocol compiler
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/wait.h>
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <ctype.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
4661961e0fSrobinson #include <strings.h>
477c478bd9Sstevel@tonic-gate #include "rpc_scan.h"
487c478bd9Sstevel@tonic-gate #include "rpc_parse.h"
497c478bd9Sstevel@tonic-gate #include "rpc_util.h"
507c478bd9Sstevel@tonic-gate 
5161961e0fSrobinson #define	startcomment(where)	(where[0] == '/' && where[1] == '*')
5261961e0fSrobinson #define	endcomment(where)	(where[-1] == '*' && where[0] == '/')
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static int pushed = 0;	/* is a token pushed */
557c478bd9Sstevel@tonic-gate static token lasttok;	/* last token, if pushed */
567c478bd9Sstevel@tonic-gate 
5761961e0fSrobinson static void unget_token(token *);
5861961e0fSrobinson static void findstrconst(char **, char **);
5961961e0fSrobinson static void findchrconst(char **, char **);
6061961e0fSrobinson static void findconst(char **, char **);
6161961e0fSrobinson static void findkind(char **, token *);
6261961e0fSrobinson static int cppline(char *);
6361961e0fSrobinson static int directive(char *);
6461961e0fSrobinson static void printdirective(char *);
6561961e0fSrobinson static void docppline(char *, int *, char **);
6661961e0fSrobinson 
677c478bd9Sstevel@tonic-gate /*
6861961e0fSrobinson  * scan expecting 1 given token
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate void
scan(tok_kind expect,token * tokp)7161961e0fSrobinson scan(tok_kind expect, token *tokp)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	get_token(tokp);
7461961e0fSrobinson 	if (tokp->kind != expect)
757c478bd9Sstevel@tonic-gate 		expected1(expect);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
7961961e0fSrobinson  * scan expecting any of the 2 given tokens
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate void
scan2(tok_kind expect1,tok_kind expect2,token * tokp)8261961e0fSrobinson scan2(tok_kind expect1, tok_kind expect2, token *tokp)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	get_token(tokp);
8561961e0fSrobinson 	if (tokp->kind != expect1 && tokp->kind != expect2)
867c478bd9Sstevel@tonic-gate 		expected2(expect1, expect2);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
9061961e0fSrobinson  * scan expecting any of the 3 given token
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate void
scan3(tok_kind expect1,tok_kind expect2,tok_kind expect3,token * tokp)9361961e0fSrobinson scan3(tok_kind expect1, tok_kind expect2, tok_kind expect3, token *tokp)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	get_token(tokp);
9661961e0fSrobinson 	if (tokp->kind != expect1 && tokp->kind != expect2 &&
97*a2f144d1SJordan Brown 	    tokp->kind != expect3)
987c478bd9Sstevel@tonic-gate 		expected3(expect1, expect2, expect3);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
10261961e0fSrobinson  * scan expecting a constant, possibly symbolic
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate void
scan_num(token * tokp)10561961e0fSrobinson scan_num(token *tokp)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	get_token(tokp);
1087c478bd9Sstevel@tonic-gate 	switch (tokp->kind) {
1097c478bd9Sstevel@tonic-gate 	case TOK_IDENT:
1107c478bd9Sstevel@tonic-gate 		break;
1117c478bd9Sstevel@tonic-gate 	default:
1127c478bd9Sstevel@tonic-gate 		error("constant or identifier expected");
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
11761961e0fSrobinson  * Peek at the next token
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate void
peek(token * tokp)12061961e0fSrobinson peek(token *tokp)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	get_token(tokp);
1237c478bd9Sstevel@tonic-gate 	unget_token(tokp);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
12761961e0fSrobinson  * Peek at the next token and scan it if it matches what you expect
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate int
peekscan(tok_kind expect,token * tokp)13061961e0fSrobinson peekscan(tok_kind expect, token *tokp)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	peek(tokp);
1337c478bd9Sstevel@tonic-gate 	if (tokp->kind == expect) {
1347c478bd9Sstevel@tonic-gate 		get_token(tokp);
1357c478bd9Sstevel@tonic-gate 		return (1);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	return (0);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
14161961e0fSrobinson  * Get the next token, printing out any directive that are encountered.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate void
get_token(token * tokp)14461961e0fSrobinson get_token(token *tokp)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	int commenting;
1477c478bd9Sstevel@tonic-gate 	int stat = 0;
14861961e0fSrobinson 
1497c478bd9Sstevel@tonic-gate 	if (pushed) {
1507c478bd9Sstevel@tonic-gate 		pushed = 0;
1517c478bd9Sstevel@tonic-gate 		*tokp = lasttok;
1527c478bd9Sstevel@tonic-gate 		return;
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 	commenting = 0;
1557c478bd9Sstevel@tonic-gate 	for (;;) {
1567c478bd9Sstevel@tonic-gate 		if (*where == 0) {
1577c478bd9Sstevel@tonic-gate 			for (;;) {
1587c478bd9Sstevel@tonic-gate 				if (!fgets(curline, MAXLINESIZE, fin)) {
1597c478bd9Sstevel@tonic-gate 					tokp->kind = TOK_EOF;
16061961e0fSrobinson 					/*
16161961e0fSrobinson 					 * now check if cpp returned
16261961e0fSrobinson 					 * non NULL value
16361961e0fSrobinson 					 */
16461961e0fSrobinson 					(void) waitpid(childpid, &stat,
165*a2f144d1SJordan Brown 					    WUNTRACED);
1667c478bd9Sstevel@tonic-gate 					if (stat > 0) {
1677c478bd9Sstevel@tonic-gate 					/* Set return value from rpcgen */
1687c478bd9Sstevel@tonic-gate 						nonfatalerrors = stat >> 8;
1697c478bd9Sstevel@tonic-gate 					}
1707c478bd9Sstevel@tonic-gate 					*where = 0;
1717c478bd9Sstevel@tonic-gate 					return;
1727c478bd9Sstevel@tonic-gate 				}
1737c478bd9Sstevel@tonic-gate 				linenum++;
1747c478bd9Sstevel@tonic-gate 				if (commenting) {
1757c478bd9Sstevel@tonic-gate 					break;
1767c478bd9Sstevel@tonic-gate 				} else if (cppline(curline)) {
17761961e0fSrobinson 					docppline(curline, &linenum,
178*a2f144d1SJordan Brown 					    &infilename);
1797c478bd9Sstevel@tonic-gate 				} else if (directive(curline)) {
1807c478bd9Sstevel@tonic-gate 					printdirective(curline);
1817c478bd9Sstevel@tonic-gate 				} else {
1827c478bd9Sstevel@tonic-gate 					break;
1837c478bd9Sstevel@tonic-gate 				}
1847c478bd9Sstevel@tonic-gate 			}
1857c478bd9Sstevel@tonic-gate 			where = curline;
1867c478bd9Sstevel@tonic-gate 		} else if (isspace(*where)) {
1877c478bd9Sstevel@tonic-gate 			while (isspace(*where)) {
1887c478bd9Sstevel@tonic-gate 				where++;	/* eat */
1897c478bd9Sstevel@tonic-gate 			}
1907c478bd9Sstevel@tonic-gate 		} else if (commenting) {
1917c478bd9Sstevel@tonic-gate 			for (where++; *where; where++) {
1927c478bd9Sstevel@tonic-gate 				if (endcomment(where)) {
1937c478bd9Sstevel@tonic-gate 					where++;
1947c478bd9Sstevel@tonic-gate 					commenting--;
1957c478bd9Sstevel@tonic-gate 					break;
1967c478bd9Sstevel@tonic-gate 				}
1977c478bd9Sstevel@tonic-gate 			}
1987c478bd9Sstevel@tonic-gate 		} else if (startcomment(where)) {
1997c478bd9Sstevel@tonic-gate 			where += 2;
2007c478bd9Sstevel@tonic-gate 			commenting++;
2017c478bd9Sstevel@tonic-gate 		} else {
2027c478bd9Sstevel@tonic-gate 			break;
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/*
20761961e0fSrobinson 	 * 'where' is not whitespace, comment or directive Must be a token!
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 	switch (*where) {
2107c478bd9Sstevel@tonic-gate 	case ':':
2117c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_COLON;
2127c478bd9Sstevel@tonic-gate 		where++;
2137c478bd9Sstevel@tonic-gate 		break;
2147c478bd9Sstevel@tonic-gate 	case ';':
2157c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_SEMICOLON;
2167c478bd9Sstevel@tonic-gate 		where++;
2177c478bd9Sstevel@tonic-gate 		break;
2187c478bd9Sstevel@tonic-gate 	case ',':
2197c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_COMMA;
2207c478bd9Sstevel@tonic-gate 		where++;
2217c478bd9Sstevel@tonic-gate 		break;
2227c478bd9Sstevel@tonic-gate 	case '=':
2237c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_EQUAL;
2247c478bd9Sstevel@tonic-gate 		where++;
2257c478bd9Sstevel@tonic-gate 		break;
2267c478bd9Sstevel@tonic-gate 	case '*':
2277c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_STAR;
2287c478bd9Sstevel@tonic-gate 		where++;
2297c478bd9Sstevel@tonic-gate 		break;
2307c478bd9Sstevel@tonic-gate 	case '[':
2317c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_LBRACKET;
2327c478bd9Sstevel@tonic-gate 		where++;
2337c478bd9Sstevel@tonic-gate 		break;
2347c478bd9Sstevel@tonic-gate 	case ']':
2357c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_RBRACKET;
2367c478bd9Sstevel@tonic-gate 		where++;
2377c478bd9Sstevel@tonic-gate 		break;
2387c478bd9Sstevel@tonic-gate 	case '{':
2397c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_LBRACE;
2407c478bd9Sstevel@tonic-gate 		where++;
2417c478bd9Sstevel@tonic-gate 		break;
2427c478bd9Sstevel@tonic-gate 	case '}':
2437c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_RBRACE;
2447c478bd9Sstevel@tonic-gate 		where++;
2457c478bd9Sstevel@tonic-gate 		break;
2467c478bd9Sstevel@tonic-gate 	case '(':
2477c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_LPAREN;
2487c478bd9Sstevel@tonic-gate 		where++;
2497c478bd9Sstevel@tonic-gate 		break;
2507c478bd9Sstevel@tonic-gate 	case ')':
2517c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_RPAREN;
2527c478bd9Sstevel@tonic-gate 		where++;
2537c478bd9Sstevel@tonic-gate 		break;
2547c478bd9Sstevel@tonic-gate 	case '<':
2557c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_LANGLE;
2567c478bd9Sstevel@tonic-gate 		where++;
2577c478bd9Sstevel@tonic-gate 		break;
2587c478bd9Sstevel@tonic-gate 	case '>':
2597c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_RANGLE;
2607c478bd9Sstevel@tonic-gate 		where++;
2617c478bd9Sstevel@tonic-gate 		break;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	case '"':
2647c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_STRCONST;
2657c478bd9Sstevel@tonic-gate 		findstrconst(&where, &tokp->str);
2667c478bd9Sstevel@tonic-gate 		break;
2677c478bd9Sstevel@tonic-gate 	case '\'':
2687c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_CHARCONST;
2697c478bd9Sstevel@tonic-gate 		findchrconst(&where, &tokp->str);
2707c478bd9Sstevel@tonic-gate 		break;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	case '-':
2737c478bd9Sstevel@tonic-gate 	case '0':
2747c478bd9Sstevel@tonic-gate 	case '1':
2757c478bd9Sstevel@tonic-gate 	case '2':
2767c478bd9Sstevel@tonic-gate 	case '3':
2777c478bd9Sstevel@tonic-gate 	case '4':
2787c478bd9Sstevel@tonic-gate 	case '5':
2797c478bd9Sstevel@tonic-gate 	case '6':
2807c478bd9Sstevel@tonic-gate 	case '7':
2817c478bd9Sstevel@tonic-gate 	case '8':
2827c478bd9Sstevel@tonic-gate 	case '9':
2837c478bd9Sstevel@tonic-gate 		tokp->kind = TOK_IDENT;
2847c478bd9Sstevel@tonic-gate 		findconst(&where, &tokp->str);
2857c478bd9Sstevel@tonic-gate 		break;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	default:
2887c478bd9Sstevel@tonic-gate 		if (!(isalpha(*where) || *where == '_')) {
2897c478bd9Sstevel@tonic-gate 			char buf[100];
2907c478bd9Sstevel@tonic-gate 			char *p;
29161961e0fSrobinson 			size_t blen;
2927c478bd9Sstevel@tonic-gate 
29361961e0fSrobinson 			(void) snprintf(buf, sizeof (buf),
294*a2f144d1SJordan Brown 			    "illegal character in file: ");
29561961e0fSrobinson 			blen = strlen(buf);
29661961e0fSrobinson 			p = buf + blen;
2977c478bd9Sstevel@tonic-gate 			if (isprint(*where)) {
29861961e0fSrobinson 				(void) snprintf(p, sizeof (buf) - blen,
299*a2f144d1SJordan Brown 				    "%c", *where);
3007c478bd9Sstevel@tonic-gate 			} else {
30161961e0fSrobinson 				(void) snprintf(p, sizeof (buf) - blen,
302*a2f144d1SJordan Brown 				    "%d", *where);
3037c478bd9Sstevel@tonic-gate 			}
3047c478bd9Sstevel@tonic-gate 			error(buf);
3057c478bd9Sstevel@tonic-gate 		}
3067c478bd9Sstevel@tonic-gate 		findkind(&where, tokp);
3077c478bd9Sstevel@tonic-gate 		break;
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
31161961e0fSrobinson static void
unget_token(token * tokp)31261961e0fSrobinson unget_token(token *tokp)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	lasttok = *tokp;
3157c478bd9Sstevel@tonic-gate 	pushed = 1;
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
31861961e0fSrobinson static void
findstrconst(char ** str,char ** val)31961961e0fSrobinson findstrconst(char **str, char **val)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	char *p;
3227c478bd9Sstevel@tonic-gate 	int size;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	p = *str;
3257c478bd9Sstevel@tonic-gate 	do {
32661961e0fSrobinson 		p++;
3277c478bd9Sstevel@tonic-gate 	} while (*p && *p != '"');
3287c478bd9Sstevel@tonic-gate 	if (*p == 0) {
3297c478bd9Sstevel@tonic-gate 		error("unterminated string constant");
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 	p++;
3327c478bd9Sstevel@tonic-gate 	size = p - *str;
33361961e0fSrobinson 	*val = malloc(size + 1);
3347c478bd9Sstevel@tonic-gate 	(void) strncpy(*val, *str, size);
3357c478bd9Sstevel@tonic-gate 	(*val)[size] = 0;
3367c478bd9Sstevel@tonic-gate 	*str = p;
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
33961961e0fSrobinson static void
findchrconst(char ** str,char ** val)34061961e0fSrobinson findchrconst(char **str, char **val)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	char *p;
3437c478bd9Sstevel@tonic-gate 	int size;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	p = *str;
3467c478bd9Sstevel@tonic-gate 	do {
34761961e0fSrobinson 		p++;
3487c478bd9Sstevel@tonic-gate 	} while (*p && *p != '\'');
34961961e0fSrobinson 	if (*p == 0)
3507c478bd9Sstevel@tonic-gate 		error("unterminated string constant");
3517c478bd9Sstevel@tonic-gate 	p++;
3527c478bd9Sstevel@tonic-gate 	size = p - *str;
35361961e0fSrobinson 	if (size != 3)
3547c478bd9Sstevel@tonic-gate 		error("empty char string");
35561961e0fSrobinson 	*val = malloc(size + 1);
3567c478bd9Sstevel@tonic-gate 	(void) strncpy(*val, *str, size);
3577c478bd9Sstevel@tonic-gate 	(*val)[size] = 0;
3587c478bd9Sstevel@tonic-gate 	*str = p;
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
36161961e0fSrobinson static void
findconst(char ** str,char ** val)36261961e0fSrobinson findconst(char **str, char **val)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	char *p;
3657c478bd9Sstevel@tonic-gate 	int size;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	p = *str;
3687c478bd9Sstevel@tonic-gate 	if (*p == '0' && *(p + 1) == 'x') {
3697c478bd9Sstevel@tonic-gate 		p++;
3707c478bd9Sstevel@tonic-gate 		do {
3717c478bd9Sstevel@tonic-gate 			p++;
3727c478bd9Sstevel@tonic-gate 		} while (isxdigit(*p));
3737c478bd9Sstevel@tonic-gate 	} else {
3747c478bd9Sstevel@tonic-gate 		do {
3757c478bd9Sstevel@tonic-gate 			p++;
3767c478bd9Sstevel@tonic-gate 		} while (isdigit(*p));
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 	size = p - *str;
37961961e0fSrobinson 	*val = malloc(size + 1);
3807c478bd9Sstevel@tonic-gate 	(void) strncpy(*val, *str, size);
3817c478bd9Sstevel@tonic-gate 	(*val)[size] = 0;
3827c478bd9Sstevel@tonic-gate 	*str = p;
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate static token symbols[] = {
38661961e0fSrobinson 			{TOK_CONST, "const"},
38761961e0fSrobinson 			{TOK_UNION, "union"},
38861961e0fSrobinson 			{TOK_SWITCH, "switch"},
38961961e0fSrobinson 			{TOK_CASE, "case"},
39061961e0fSrobinson 			{TOK_DEFAULT, "default"},
39161961e0fSrobinson 			{TOK_STRUCT, "struct"},
39261961e0fSrobinson 			{TOK_TYPEDEF, "typedef"},
39361961e0fSrobinson 			{TOK_ENUM, "enum"},
39461961e0fSrobinson 			{TOK_OPAQUE, "opaque"},
39561961e0fSrobinson 			{TOK_BOOL, "bool"},
39661961e0fSrobinson 			{TOK_VOID, "void"},
39761961e0fSrobinson 			{TOK_ONEWAY, "oneway"},
39861961e0fSrobinson 			{TOK_CHAR, "char"},
39961961e0fSrobinson 			{TOK_INT, "int"},
40061961e0fSrobinson 			{TOK_UNSIGNED, "unsigned"},
40161961e0fSrobinson 			{TOK_SHORT, "short"},
40261961e0fSrobinson 			{TOK_LONG, "long"},
40361961e0fSrobinson 			{TOK_HYPER, "hyper"},
40461961e0fSrobinson 			{TOK_FLOAT, "float"},
40561961e0fSrobinson 			{TOK_DOUBLE, "double"},
40661961e0fSrobinson 			{TOK_QUAD, "quadruple"},
40761961e0fSrobinson 			{TOK_STRING, "string"},
40861961e0fSrobinson 			{TOK_PROGRAM, "program"},
40961961e0fSrobinson 			{TOK_VERSION, "version"},
41061961e0fSrobinson 			{TOK_EOF, "??????"},
4117c478bd9Sstevel@tonic-gate };
4127c478bd9Sstevel@tonic-gate 
41361961e0fSrobinson static void
findkind(char ** mark,token * tokp)41461961e0fSrobinson findkind(char **mark, token *tokp)
4157c478bd9Sstevel@tonic-gate {
4167c478bd9Sstevel@tonic-gate 	int len;
4177c478bd9Sstevel@tonic-gate 	token *s;
4187c478bd9Sstevel@tonic-gate 	char *str;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	str = *mark;
4217c478bd9Sstevel@tonic-gate 	for (s = symbols; s->kind != TOK_EOF; s++) {
4227c478bd9Sstevel@tonic-gate 		len = strlen(s->str);
4237c478bd9Sstevel@tonic-gate 		if (strncmp(str, s->str, len) == 0) {
4247c478bd9Sstevel@tonic-gate 			if (!isalnum(str[len]) && str[len] != '_') {
4257c478bd9Sstevel@tonic-gate 				tokp->kind = s->kind;
4267c478bd9Sstevel@tonic-gate 				tokp->str = s->str;
4277c478bd9Sstevel@tonic-gate 				*mark = str + len;
4287c478bd9Sstevel@tonic-gate 				return;
4297c478bd9Sstevel@tonic-gate 			}
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 	tokp->kind = TOK_IDENT;
433*a2f144d1SJordan Brown 	for (len = 0; isalnum(str[len]) || str[len] == '_'; len++)
434*a2f144d1SJordan Brown 		/* LOOP */;
43561961e0fSrobinson 	tokp->str = malloc(len + 1);
4367c478bd9Sstevel@tonic-gate 	(void) strncpy(tokp->str, str, len);
4377c478bd9Sstevel@tonic-gate 	tokp->str[len] = 0;
4387c478bd9Sstevel@tonic-gate 	*mark = str + len;
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
44161961e0fSrobinson static int
cppline(char * line)44261961e0fSrobinson cppline(char *line)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	return (line == curline && *line == '#');
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
44761961e0fSrobinson static int
directive(char * line)44861961e0fSrobinson directive(char *line)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate 	return (line == curline && *line == '%');
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
45361961e0fSrobinson static void
printdirective(char * line)45461961e0fSrobinson printdirective(char *line)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	f_print(fout, "%s", line + 1);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
45961961e0fSrobinson static void
docppline(char * line,int * lineno,char ** fname)46061961e0fSrobinson docppline(char *line, int *lineno, char **fname)
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate 	char *file;
4637c478bd9Sstevel@tonic-gate 	int num;
4647c478bd9Sstevel@tonic-gate 	char *p;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	line++;
46761961e0fSrobinson 	while (isspace(*line))
4687c478bd9Sstevel@tonic-gate 		line++;
4697c478bd9Sstevel@tonic-gate 	num = atoi(line);
47061961e0fSrobinson 	while (isdigit(*line))
4717c478bd9Sstevel@tonic-gate 		line++;
47261961e0fSrobinson 	while (isspace(*line))
4737c478bd9Sstevel@tonic-gate 		line++;
47461961e0fSrobinson 	if (*line != '"')
4757c478bd9Sstevel@tonic-gate 		error("preprocessor error");
4767c478bd9Sstevel@tonic-gate 	line++;
47761961e0fSrobinson 	p = file = malloc(strlen(line) + 1);
47861961e0fSrobinson 	while (*line && *line != '"')
4797c478bd9Sstevel@tonic-gate 		*p++ = *line++;
48061961e0fSrobinson 	if (*line == 0)
4817c478bd9Sstevel@tonic-gate 		error("preprocessor error");
4827c478bd9Sstevel@tonic-gate 	*p = 0;
48361961e0fSrobinson 	if (*file == 0)
4847c478bd9Sstevel@tonic-gate 		*fname = NULL;
48561961e0fSrobinson 	else
4867c478bd9Sstevel@tonic-gate 		*fname = file;
4877c478bd9Sstevel@tonic-gate 	*lineno = num - 1;
4887c478bd9Sstevel@tonic-gate }
489