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 /* 397c478bd9Sstevel@tonic-gate * 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 517c478bd9Sstevel@tonic-gate #define startcomment(where) (where[0] == '/' && where[1] == '*') 527c478bd9Sstevel@tonic-gate #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 /* 687c478bd9Sstevel@tonic-gate * scan expecting 1 given token 697c478bd9Sstevel@tonic-gate */ 707c478bd9Sstevel@tonic-gate void 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 /* 797c478bd9Sstevel@tonic-gate * scan expecting any of the 2 given tokens 807c478bd9Sstevel@tonic-gate */ 817c478bd9Sstevel@tonic-gate void 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 /* 907c478bd9Sstevel@tonic-gate * scan expecting any of the 3 given token 917c478bd9Sstevel@tonic-gate */ 927c478bd9Sstevel@tonic-gate void 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 && 9761961e0fSrobinson tokp->kind != expect3) 987c478bd9Sstevel@tonic-gate expected3(expect1, expect2, expect3); 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * scan expecting a constant, possibly symbolic 1037c478bd9Sstevel@tonic-gate */ 1047c478bd9Sstevel@tonic-gate void 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 /* 1177c478bd9Sstevel@tonic-gate * Peek at the next token 1187c478bd9Sstevel@tonic-gate */ 1197c478bd9Sstevel@tonic-gate void 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 /* 1277c478bd9Sstevel@tonic-gate * Peek at the next token and scan it if it matches what you expect 1287c478bd9Sstevel@tonic-gate */ 1297c478bd9Sstevel@tonic-gate int 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 /* 1417c478bd9Sstevel@tonic-gate * Get the next token, printing out any directive that are encountered. 1427c478bd9Sstevel@tonic-gate */ 1437c478bd9Sstevel@tonic-gate void 14461961e0fSrobinson get_token(token *tokp) 1457c478bd9Sstevel@tonic-gate { 1467c478bd9Sstevel@tonic-gate int commenting; 1477c478bd9Sstevel@tonic-gate int stat = 0; 1487c478bd9Sstevel@tonic-gate 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, 16561961e0fSrobinson 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)) { 1777c478bd9Sstevel@tonic-gate docppline(curline, &linenum, 1787c478bd9Sstevel@tonic-gate &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 /* 2077c478bd9Sstevel@tonic-gate * '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), 29461961e0fSrobinson "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, 29961961e0fSrobinson "%c", *where); 3007c478bd9Sstevel@tonic-gate } else { 30161961e0fSrobinson (void) snprintf(p, sizeof (buf) - blen, 30261961e0fSrobinson "%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 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 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 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 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[] = { 3867c478bd9Sstevel@tonic-gate {TOK_CONST, "const"}, 3877c478bd9Sstevel@tonic-gate {TOK_UNION, "union"}, 3887c478bd9Sstevel@tonic-gate {TOK_SWITCH, "switch"}, 3897c478bd9Sstevel@tonic-gate {TOK_CASE, "case"}, 3907c478bd9Sstevel@tonic-gate {TOK_DEFAULT, "default"}, 3917c478bd9Sstevel@tonic-gate {TOK_STRUCT, "struct"}, 3927c478bd9Sstevel@tonic-gate {TOK_TYPEDEF, "typedef"}, 3937c478bd9Sstevel@tonic-gate {TOK_ENUM, "enum"}, 3947c478bd9Sstevel@tonic-gate {TOK_OPAQUE, "opaque"}, 3957c478bd9Sstevel@tonic-gate {TOK_BOOL, "bool"}, 3967c478bd9Sstevel@tonic-gate {TOK_VOID, "void"}, 3977c478bd9Sstevel@tonic-gate {TOK_ONEWAY, "oneway"}, 3987c478bd9Sstevel@tonic-gate {TOK_CHAR, "char"}, 3997c478bd9Sstevel@tonic-gate {TOK_INT, "int"}, 4007c478bd9Sstevel@tonic-gate {TOK_UNSIGNED, "unsigned"}, 4017c478bd9Sstevel@tonic-gate {TOK_SHORT, "short"}, 4027c478bd9Sstevel@tonic-gate {TOK_LONG, "long"}, 4037c478bd9Sstevel@tonic-gate {TOK_HYPER, "hyper"}, 4047c478bd9Sstevel@tonic-gate {TOK_FLOAT, "float"}, 4057c478bd9Sstevel@tonic-gate {TOK_DOUBLE, "double"}, 4067c478bd9Sstevel@tonic-gate {TOK_QUAD, "quadruple"}, 4077c478bd9Sstevel@tonic-gate {TOK_STRING, "string"}, 4087c478bd9Sstevel@tonic-gate {TOK_PROGRAM, "program"}, 4097c478bd9Sstevel@tonic-gate {TOK_VERSION, "version"}, 4107c478bd9Sstevel@tonic-gate {TOK_EOF, "??????"}, 4117c478bd9Sstevel@tonic-gate }; 4127c478bd9Sstevel@tonic-gate 41361961e0fSrobinson static void 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 44261961e0fSrobinson cppline(char *line) 4437c478bd9Sstevel@tonic-gate { 4447c478bd9Sstevel@tonic-gate return (line == curline && *line == '#'); 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate 44761961e0fSrobinson static int 44861961e0fSrobinson directive(char *line) 4497c478bd9Sstevel@tonic-gate { 4507c478bd9Sstevel@tonic-gate return (line == curline && *line == '%'); 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate 45361961e0fSrobinson static void 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 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