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*80ab886dSwesolows * Common Development and Distribution License (the "License"). 6*80ab886dSwesolows * 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 */ 21*80ab886dSwesolows 227c478bd9Sstevel@tonic-gate /* 23*80ab886dSwesolows * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate * 267c478bd9Sstevel@tonic-gate * escmain.c -- main routine for esc, the eversholt compiler 277c478bd9Sstevel@tonic-gate * 287c478bd9Sstevel@tonic-gate * argument processing and the general flow through all the other 297c478bd9Sstevel@tonic-gate * modules is driven by this file. 307c478bd9Sstevel@tonic-gate */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include <stdio.h> 357c478bd9Sstevel@tonic-gate #include <string.h> 367c478bd9Sstevel@tonic-gate #ifdef sun 377c478bd9Sstevel@tonic-gate #include <stdlib.h> 387c478bd9Sstevel@tonic-gate #else 397c478bd9Sstevel@tonic-gate #include <getopt.h> 407c478bd9Sstevel@tonic-gate #endif /* sun */ 417c478bd9Sstevel@tonic-gate #include "out.h" 427c478bd9Sstevel@tonic-gate #include "stats.h" 437c478bd9Sstevel@tonic-gate #include "alloc.h" 447c478bd9Sstevel@tonic-gate #include "stable.h" 457c478bd9Sstevel@tonic-gate #include "literals.h" 467c478bd9Sstevel@tonic-gate #include "lut.h" 477c478bd9Sstevel@tonic-gate #include "esclex.h" 487c478bd9Sstevel@tonic-gate #include "eftwrite.h" 497c478bd9Sstevel@tonic-gate #include "ptree.h" 507c478bd9Sstevel@tonic-gate #include "tree.h" 517c478bd9Sstevel@tonic-gate #include "check.h" 527c478bd9Sstevel@tonic-gate #include "version.h" 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate /* stuff exported by yacc-generated parsers */ 557c478bd9Sstevel@tonic-gate extern void yyparse(void); 567c478bd9Sstevel@tonic-gate extern int yydebug; 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate /* 597c478bd9Sstevel@tonic-gate * This external definition has to be here. If we put it in literals.h 607c478bd9Sstevel@tonic-gate * lint complains about the declaration not being used within the block 617c478bd9Sstevel@tonic-gate * when compiling literals.c. 627c478bd9Sstevel@tonic-gate */ 637c478bd9Sstevel@tonic-gate extern void literals_init(void); 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate static const char *Usage = 667c478bd9Sstevel@tonic-gate "[-SYdghpqvy] [-Dname[=def]] [-I dir] [-Uname] [-o outfile] esc-files..."; 677c478bd9Sstevel@tonic-gate static const char *Help = 687c478bd9Sstevel@tonic-gate "\tinput files are run through cpp and concatenated.\n" 697c478bd9Sstevel@tonic-gate "\t-D name[=def] Pass to cpp\n" 707c478bd9Sstevel@tonic-gate "\t-I dir Pass to cpp\n" 717c478bd9Sstevel@tonic-gate "\t-S Print stats for compiler memory usage, etc.\n" 727c478bd9Sstevel@tonic-gate "\t-U name Pass to cpp\n" 737c478bd9Sstevel@tonic-gate "\t-Y Enable parser debug output\n" 747c478bd9Sstevel@tonic-gate "\t-d Enable general debug output\n" 757c478bd9Sstevel@tonic-gate "\t-g Print generated iterators (use with -p)\n" 767c478bd9Sstevel@tonic-gate "\t-h Print this help message\n" 777c478bd9Sstevel@tonic-gate "\t-o outfile Emit compiled EFT to \"outfile\"\n" 787c478bd9Sstevel@tonic-gate "\t-p Print complete parse tree\n" 797c478bd9Sstevel@tonic-gate "\t-q Quiet mode: suppress warnings\n" 807c478bd9Sstevel@tonic-gate "\t-v Enable verbose output\n" 817c478bd9Sstevel@tonic-gate "\t-y Enable lexer debug output"; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate int Debug; 847c478bd9Sstevel@tonic-gate int Verbose; 857c478bd9Sstevel@tonic-gate int Warn = 1; /* the esc compiler should issue language warnings */ 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate extern int Pchildgen; /* flag to ptree for printing generated iterators */ 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate #define MAXARGS 8192 907c478bd9Sstevel@tonic-gate char Args[MAXARGS]; 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate #define MAXCPPARGS 4000 937c478bd9Sstevel@tonic-gate static char Cppargs[MAXCPPARGS]; 947c478bd9Sstevel@tonic-gate 95*80ab886dSwesolows int 967c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 977c478bd9Sstevel@tonic-gate { 987c478bd9Sstevel@tonic-gate char flagbuf[] = " -D"; 997c478bd9Sstevel@tonic-gate char **av; 1007c478bd9Sstevel@tonic-gate int c; 1017c478bd9Sstevel@tonic-gate int stats = 0; 1027c478bd9Sstevel@tonic-gate int lexecho = 0; 1037c478bd9Sstevel@tonic-gate const char *outfile = NULL; 1047c478bd9Sstevel@tonic-gate int count; 1057c478bd9Sstevel@tonic-gate int i; 1067c478bd9Sstevel@tonic-gate int pflag = 0; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate alloc_init(); 1097c478bd9Sstevel@tonic-gate out_init(argv[0]); 1107c478bd9Sstevel@tonic-gate stats_init(1); /* extended stats always enabled for esc */ 1117c478bd9Sstevel@tonic-gate stable_init(0); 1127c478bd9Sstevel@tonic-gate literals_init(); 1137c478bd9Sstevel@tonic-gate lut_init(); 1147c478bd9Sstevel@tonic-gate tree_init(); 1157c478bd9Sstevel@tonic-gate eftwrite_init(); 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* built a best effort summary of args for eftwrite() */ 1187c478bd9Sstevel@tonic-gate count = 0; 1197c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) { 1207c478bd9Sstevel@tonic-gate char *ptr = argv[i]; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate if (count < MAXARGS - 1) 1237c478bd9Sstevel@tonic-gate Args[count++] = ' '; 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate while (count < MAXARGS - 1 && *ptr) 1267c478bd9Sstevel@tonic-gate Args[count++] = *ptr++; 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate Args[count] = '\0'; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "D:I:SU:Ydgho:pqvy")) != EOF) { 1347c478bd9Sstevel@tonic-gate switch (c) { 1357c478bd9Sstevel@tonic-gate case 'D': 1367c478bd9Sstevel@tonic-gate case 'I': 1377c478bd9Sstevel@tonic-gate case 'U': 1387c478bd9Sstevel@tonic-gate if (strlen(optarg) + strlen(Cppargs) + 4 >= MAXCPPARGS) 1397c478bd9Sstevel@tonic-gate out(O_DIE, "cpp args too long (max %d bytes)", 1407c478bd9Sstevel@tonic-gate MAXCPPARGS); 1417c478bd9Sstevel@tonic-gate flagbuf[2] = c; 1427c478bd9Sstevel@tonic-gate (void) strcat(Cppargs, flagbuf); 1437c478bd9Sstevel@tonic-gate (void) strcat(Cppargs, optarg); 1447c478bd9Sstevel@tonic-gate break; 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate case 'S': 1477c478bd9Sstevel@tonic-gate stats++; 1487c478bd9Sstevel@tonic-gate break; 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate case 'Y': 1517c478bd9Sstevel@tonic-gate yydebug++; 1527c478bd9Sstevel@tonic-gate break; 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate case 'd': 1557c478bd9Sstevel@tonic-gate Debug++; 1567c478bd9Sstevel@tonic-gate break; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate case 'g': 1597c478bd9Sstevel@tonic-gate Pchildgen++; 1607c478bd9Sstevel@tonic-gate break; 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate case 'h': 1637c478bd9Sstevel@tonic-gate case '?': 1647c478bd9Sstevel@tonic-gate out(O_PROG, "eversholt compiler version %d.%d", 1657c478bd9Sstevel@tonic-gate VERSION_MAJOR, VERSION_MINOR); 1667c478bd9Sstevel@tonic-gate out(O_DIE|O_USAGE, "%s\n%s", Usage, Help); 1677c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate case 'o': 1707c478bd9Sstevel@tonic-gate outfile = optarg; 1717c478bd9Sstevel@tonic-gate break; 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate case 'p': 1747c478bd9Sstevel@tonic-gate pflag++; 1757c478bd9Sstevel@tonic-gate break; 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate case 'q': 1787c478bd9Sstevel@tonic-gate Warn = 0; 1797c478bd9Sstevel@tonic-gate break; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate case 'v': 1827c478bd9Sstevel@tonic-gate Verbose++; 1837c478bd9Sstevel@tonic-gate break; 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate case 'y': 1867c478bd9Sstevel@tonic-gate lexecho++; 1877c478bd9Sstevel@tonic-gate break; 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate default: 1907c478bd9Sstevel@tonic-gate out(O_DIE|O_USAGE, Usage); 1917c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate out(O_PROG|O_VERB, "eversholt compiler version %d.%d", 1967c478bd9Sstevel@tonic-gate VERSION_MAJOR, VERSION_MINOR); 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate argc -= optind; 1997c478bd9Sstevel@tonic-gate av = &argv[optind]; 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate if (argc < 1) { 2027c478bd9Sstevel@tonic-gate out(O_ERR, "no esc source files given"); 2037c478bd9Sstevel@tonic-gate out(O_DIE|O_USAGE, Usage); 2047c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate lex_init(av, Cppargs, lexecho); 2087c478bd9Sstevel@tonic-gate check_init(); 2097c478bd9Sstevel@tonic-gate yyparse(); 2107c478bd9Sstevel@tonic-gate (void) lex_fini(); 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate tree_report(); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate if (count = out_errcount()) 2157c478bd9Sstevel@tonic-gate out(O_DIE, "%d language error%s encountered, exiting.", 2167c478bd9Sstevel@tonic-gate OUTS(count)); 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate if (outfile) 2197c478bd9Sstevel@tonic-gate eftwrite(outfile); 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate if (pflag) 2227c478bd9Sstevel@tonic-gate ptree_name_iter(O_OK, tree_root(NULL)); 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate if (stats) { 2257c478bd9Sstevel@tonic-gate out(O_OK, "Stats:"); 2267c478bd9Sstevel@tonic-gate stats_publish(); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate out_exit(0); 2307c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 231*80ab886dSwesolows return (0); 2327c478bd9Sstevel@tonic-gate } 233