mksyntax.c revision 36150
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 3. All advertising materials mentioning features or use of this software 171556Srgrimes * must display the following acknowledgement: 181556Srgrimes * This product includes software developed by the University of 191556Srgrimes * California, Berkeley and its contributors. 201556Srgrimes * 4. Neither the name of the University nor the names of its contributors 211556Srgrimes * may be used to endorse or promote products derived from this software 221556Srgrimes * without specific prior written permission. 231556Srgrimes * 241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341556Srgrimes * SUCH DAMAGE. 351556Srgrimes */ 361556Srgrimes 371556Srgrimes#ifndef lint 3820425Sstevestatic char const copyright[] = 391556Srgrimes"@(#) Copyright (c) 1991, 1993\n\ 401556Srgrimes The Regents of the University of California. All rights reserved.\n"; 411556Srgrimes#endif /* not lint */ 421556Srgrimes 431556Srgrimes#ifndef lint 4436150Scharnier#if 0 4536150Scharnierstatic char sccsid[] = "@(#)mksyntax.c 8.2 (Berkeley) 5/4/95"; 4636150Scharnier#endif 4736150Scharnierstatic const char rcsid[] = 4836150Scharnier "$Id$"; 491556Srgrimes#endif /* not lint */ 501556Srgrimes 511556Srgrimes/* 521556Srgrimes * This program creates syntax.h and syntax.c. 531556Srgrimes */ 541556Srgrimes 551556Srgrimes#include <stdio.h> 5617987Speter#include <string.h> 571556Srgrimes#include "parser.h" 581556Srgrimes 591556Srgrimes 601556Srgrimesstruct synclass { 611556Srgrimes char *name; 621556Srgrimes char *comment; 631556Srgrimes}; 641556Srgrimes 651556Srgrimes/* Syntax classes */ 661556Srgrimesstruct synclass synclass[] = { 6717987Speter { "CWORD", "character is nothing special" }, 6817987Speter { "CNL", "newline character" }, 6917987Speter { "CBACK", "a backslash character" }, 7017987Speter { "CSQUOTE", "single quote" }, 7117987Speter { "CDQUOTE", "double quote" }, 7217987Speter { "CENDQUOTE", "a terminating quote" }, 7317987Speter { "CBQUOTE", "backwards single quote" }, 7417987Speter { "CVAR", "a dollar sign" }, 7517987Speter { "CENDVAR", "a '}' character" }, 7617987Speter { "CLP", "a left paren in arithmetic" }, 7717987Speter { "CRP", "a right paren in arithmetic" }, 7817987Speter { "CEOF", "end of file" }, 7917987Speter { "CCTL", "like CWORD, except it must be escaped" }, 8017987Speter { "CSPCL", "these terminate a word" }, 8117987Speter { NULL, NULL } 821556Srgrimes}; 831556Srgrimes 841556Srgrimes 851556Srgrimes/* 861556Srgrimes * Syntax classes for is_ functions. Warning: if you add new classes 871556Srgrimes * you may have to change the definition of the is_in_name macro. 881556Srgrimes */ 891556Srgrimesstruct synclass is_entry[] = { 9017987Speter { "ISDIGIT", "a digit" }, 9117987Speter { "ISUPPER", "an upper case letter" }, 9217987Speter { "ISLOWER", "a lower case letter" }, 9317987Speter { "ISUNDER", "an underscore" }, 9417987Speter { "ISSPECL", "the name of a special parameter" }, 9517987Speter { NULL, NULL } 961556Srgrimes}; 971556Srgrimes 9817987Speterstatic char writer[] = "\ 991556Srgrimes/*\n\ 1001556Srgrimes * This file was generated by the mksyntax program.\n\ 1011556Srgrimes */\n\ 1021556Srgrimes\n"; 1031556Srgrimes 1041556Srgrimes 10517987Speterstatic FILE *cfile; 10617987Speterstatic FILE *hfile; 10717987Speterstatic char *syntax[513]; 10817987Speterstatic int base; 10917987Speterstatic int size; /* number of values which a char variable can have */ 11017987Speterstatic int nbits; /* number of bits in a character */ 11117987Speterstatic int digit_contig;/* true if digits are contiguous */ 1121556Srgrimes 11317987Speterstatic void filltable __P((char *)); 11417987Speterstatic void init __P((void)); 11517987Speterstatic void add __P((char *, char *)); 11617987Speterstatic void print __P((char *)); 11717987Speterstatic void output_type_macros __P((void)); 11817987Speterstatic void digit_convert __P((void)); 1191556Srgrimes 12017987Speterint 12117987Spetermain(argc, argv) 12225905Ssteve int argc __unused; 12325905Ssteve char **argv __unused; 12417987Speter{ 1251556Srgrimes char c; 1261556Srgrimes char d; 1271556Srgrimes int sign; 1281556Srgrimes int i; 1291556Srgrimes char buf[80]; 1301556Srgrimes int pos; 1311556Srgrimes static char digit[] = "0123456789"; 1321556Srgrimes 1331556Srgrimes /* Create output files */ 1341556Srgrimes if ((cfile = fopen("syntax.c", "w")) == NULL) { 1351556Srgrimes perror("syntax.c"); 1361556Srgrimes exit(2); 1371556Srgrimes } 1381556Srgrimes if ((hfile = fopen("syntax.h", "w")) == NULL) { 1391556Srgrimes perror("syntax.h"); 1401556Srgrimes exit(2); 1411556Srgrimes } 1421556Srgrimes fputs(writer, hfile); 1431556Srgrimes fputs(writer, cfile); 1441556Srgrimes 1451556Srgrimes /* Determine the characteristics of chars. */ 1461556Srgrimes c = -1; 1471556Srgrimes if (c < 0) 1481556Srgrimes sign = 1; 1491556Srgrimes else 1501556Srgrimes sign = 0; 1511556Srgrimes for (nbits = 1 ; ; nbits++) { 1521556Srgrimes d = (1 << nbits) - 1; 1531556Srgrimes if (d == c) 1541556Srgrimes break; 1551556Srgrimes } 15620425Ssteve#if 0 1571556Srgrimes printf("%s %d bit chars\n", sign? "signed" : "unsigned", nbits); 15820425Ssteve#endif 1591556Srgrimes if (nbits > 9) { 1601556Srgrimes fputs("Characters can't have more than 9 bits\n", stderr); 1611556Srgrimes exit(2); 1621556Srgrimes } 1631556Srgrimes size = (1 << nbits) + 1; 1641556Srgrimes base = 1; 1651556Srgrimes if (sign) 1661556Srgrimes base += 1 << (nbits - 1); 1671556Srgrimes digit_contig = 1; 1681556Srgrimes for (i = 0 ; i < 10 ; i++) { 1691556Srgrimes if (digit[i] != '0' + i) 1701556Srgrimes digit_contig = 0; 1711556Srgrimes } 1721556Srgrimes 17320425Ssteve fputs("#include <sys/cdefs.h>\n", hfile); 17417525Sache fputs("#include <ctype.h>\n", hfile); 1751556Srgrimes 1761556Srgrimes /* Generate the #define statements in the header file */ 1771556Srgrimes fputs("/* Syntax classes */\n", hfile); 1781556Srgrimes for (i = 0 ; synclass[i].name ; i++) { 1791556Srgrimes sprintf(buf, "#define %s %d", synclass[i].name, i); 1801556Srgrimes fputs(buf, hfile); 18117987Speter for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07) 1821556Srgrimes putc('\t', hfile); 1831556Srgrimes fprintf(hfile, "/* %s */\n", synclass[i].comment); 1841556Srgrimes } 1851556Srgrimes putc('\n', hfile); 1861556Srgrimes fputs("/* Syntax classes for is_ functions */\n", hfile); 1871556Srgrimes for (i = 0 ; is_entry[i].name ; i++) { 1881556Srgrimes sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i); 1891556Srgrimes fputs(buf, hfile); 19017987Speter for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07) 1911556Srgrimes putc('\t', hfile); 1921556Srgrimes fprintf(hfile, "/* %s */\n", is_entry[i].comment); 1931556Srgrimes } 1941556Srgrimes putc('\n', hfile); 1951556Srgrimes fprintf(hfile, "#define SYNBASE %d\n", base); 1961556Srgrimes fprintf(hfile, "#define PEOF %d\n\n", -base); 1971556Srgrimes putc('\n', hfile); 1981556Srgrimes fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile); 1991556Srgrimes fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile); 2001556Srgrimes fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile); 2011556Srgrimes fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile); 2021556Srgrimes putc('\n', hfile); 2031556Srgrimes output_type_macros(); /* is_digit, etc. */ 2041556Srgrimes putc('\n', hfile); 2051556Srgrimes 2061556Srgrimes /* Generate the syntax tables. */ 2071556Srgrimes fputs("#include \"shell.h\"\n", cfile); 2081556Srgrimes fputs("#include \"syntax.h\"\n\n", cfile); 2091556Srgrimes init(); 2101556Srgrimes fputs("/* syntax table used when not in quotes */\n", cfile); 2111556Srgrimes add("\n", "CNL"); 2121556Srgrimes add("\\", "CBACK"); 2131556Srgrimes add("'", "CSQUOTE"); 2141556Srgrimes add("\"", "CDQUOTE"); 2151556Srgrimes add("`", "CBQUOTE"); 2161556Srgrimes add("$", "CVAR"); 2171556Srgrimes add("}", "CENDVAR"); 2181556Srgrimes add("<>();&| \t", "CSPCL"); 2191556Srgrimes print("basesyntax"); 2201556Srgrimes init(); 2211556Srgrimes fputs("\n/* syntax table used when in double quotes */\n", cfile); 2221556Srgrimes add("\n", "CNL"); 2231556Srgrimes add("\\", "CBACK"); 2241556Srgrimes add("\"", "CENDQUOTE"); 2251556Srgrimes add("`", "CBQUOTE"); 2261556Srgrimes add("$", "CVAR"); 2271556Srgrimes add("}", "CENDVAR"); 22817987Speter /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */ 22917987Speter add("!*?[=~:/-", "CCTL"); 2301556Srgrimes print("dqsyntax"); 2311556Srgrimes init(); 2321556Srgrimes fputs("\n/* syntax table used when in single quotes */\n", cfile); 2331556Srgrimes add("\n", "CNL"); 2341556Srgrimes add("'", "CENDQUOTE"); 23517987Speter /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */ 23617987Speter add("!*?[=~:/-", "CCTL"); 2371556Srgrimes print("sqsyntax"); 2381556Srgrimes init(); 2391556Srgrimes fputs("\n/* syntax table used when in arithmetic */\n", cfile); 2401556Srgrimes add("\n", "CNL"); 2411556Srgrimes add("\\", "CBACK"); 2421556Srgrimes add("`", "CBQUOTE"); 2431556Srgrimes add("'", "CSQUOTE"); 2441556Srgrimes add("\"", "CDQUOTE"); 2451556Srgrimes add("$", "CVAR"); 2461556Srgrimes add("}", "CENDVAR"); 2471556Srgrimes add("(", "CLP"); 2481556Srgrimes add(")", "CRP"); 2491556Srgrimes print("arisyntax"); 2501556Srgrimes filltable("0"); 2511556Srgrimes fputs("\n/* character classification table */\n", cfile); 2521556Srgrimes add("0123456789", "ISDIGIT"); 2531556Srgrimes add("abcdefghijklmnopqrstucvwxyz", "ISLOWER"); 2541556Srgrimes add("ABCDEFGHIJKLMNOPQRSTUCVWXYZ", "ISUPPER"); 2551556Srgrimes add("_", "ISUNDER"); 2561556Srgrimes add("#?$!-*@", "ISSPECL"); 2571556Srgrimes print("is_type"); 2581556Srgrimes if (! digit_contig) 2591556Srgrimes digit_convert(); 2601556Srgrimes exit(0); 2611556Srgrimes} 2621556Srgrimes 2631556Srgrimes 2641556Srgrimes 2651556Srgrimes/* 2661556Srgrimes * Clear the syntax table. 2671556Srgrimes */ 2681556Srgrimes 26917987Speterstatic void 2701556Srgrimesfilltable(dftval) 2711556Srgrimes char *dftval; 27217987Speter{ 2731556Srgrimes int i; 2741556Srgrimes 2751556Srgrimes for (i = 0 ; i < size ; i++) 2761556Srgrimes syntax[i] = dftval; 2771556Srgrimes} 2781556Srgrimes 2791556Srgrimes 2801556Srgrimes/* 2811556Srgrimes * Initialize the syntax table with default values. 2821556Srgrimes */ 2831556Srgrimes 28417987Speterstatic void 28517987Speterinit() 28617987Speter{ 2871556Srgrimes filltable("CWORD"); 2881556Srgrimes syntax[0] = "CEOF"; 2891556Srgrimes syntax[base + CTLESC] = "CCTL"; 2901556Srgrimes syntax[base + CTLVAR] = "CCTL"; 2911556Srgrimes syntax[base + CTLENDVAR] = "CCTL"; 2921556Srgrimes syntax[base + CTLBACKQ] = "CCTL"; 2931556Srgrimes syntax[base + CTLBACKQ + CTLQUOTE] = "CCTL"; 2941556Srgrimes syntax[base + CTLARI] = "CCTL"; 2951556Srgrimes syntax[base + CTLENDARI] = "CCTL"; 2961556Srgrimes} 2971556Srgrimes 2981556Srgrimes 2991556Srgrimes/* 3001556Srgrimes * Add entries to the syntax table. 3011556Srgrimes */ 3021556Srgrimes 30317987Speterstatic void 3041556Srgrimesadd(p, type) 3051556Srgrimes char *p, *type; 30617987Speter{ 3071556Srgrimes while (*p) 3081556Srgrimes syntax[*p++ + base] = type; 3091556Srgrimes} 3101556Srgrimes 3111556Srgrimes 3121556Srgrimes 3131556Srgrimes/* 3141556Srgrimes * Output the syntax table. 3151556Srgrimes */ 3161556Srgrimes 31717987Speterstatic void 3181556Srgrimesprint(name) 3191556Srgrimes char *name; 32017987Speter{ 3211556Srgrimes int i; 3221556Srgrimes int col; 3231556Srgrimes 3241556Srgrimes fprintf(hfile, "extern const char %s[];\n", name); 3251556Srgrimes fprintf(cfile, "const char %s[%d] = {\n", name, size); 3261556Srgrimes col = 0; 3271556Srgrimes for (i = 0 ; i < size ; i++) { 3281556Srgrimes if (i == 0) { 3291556Srgrimes fputs(" ", cfile); 3301556Srgrimes } else if ((i & 03) == 0) { 3311556Srgrimes fputs(",\n ", cfile); 3321556Srgrimes col = 0; 3331556Srgrimes } else { 3341556Srgrimes putc(',', cfile); 3351556Srgrimes while (++col < 9 * (i & 03)) 3361556Srgrimes putc(' ', cfile); 3371556Srgrimes } 3381556Srgrimes fputs(syntax[i], cfile); 3391556Srgrimes col += strlen(syntax[i]); 3401556Srgrimes } 3411556Srgrimes fputs("\n};\n", cfile); 3421556Srgrimes} 3431556Srgrimes 3441556Srgrimes 3451556Srgrimes 3461556Srgrimes/* 3471556Srgrimes * Output character classification macros (e.g. is_digit). If digits are 3481556Srgrimes * contiguous, we can test for them quickly. 3491556Srgrimes */ 3501556Srgrimes 35117987Speterstatic char *macro[] = { 3521556Srgrimes "#define is_digit(c)\t((is_type+SYNBASE)[c] & ISDIGIT)", 35317562Sache "#define is_alpha(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && isalpha((unsigned char) (c)))", 35417562Sache "#define is_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalpha((unsigned char) (c))))", 35517562Sache "#define is_in_name(c)\t((c) != PEOF && ((c) < CTLESC || (c) > CTLENDARI) && ((c) == '_' || isalnum((unsigned char) (c))))", 3561556Srgrimes "#define is_special(c)\t((is_type+SYNBASE)[c] & (ISSPECL|ISDIGIT))", 3571556Srgrimes NULL 3581556Srgrimes}; 3591556Srgrimes 36017987Speterstatic void 36120425Ssteveoutput_type_macros() 36217987Speter{ 3631556Srgrimes char **pp; 3641556Srgrimes 3651556Srgrimes if (digit_contig) 3661556Srgrimes macro[0] = "#define is_digit(c)\t((unsigned)((c) - '0') <= 9)"; 3671556Srgrimes for (pp = macro ; *pp ; pp++) 3681556Srgrimes fprintf(hfile, "%s\n", *pp); 3691556Srgrimes if (digit_contig) 3701556Srgrimes fputs("#define digit_val(c)\t((c) - '0')\n", hfile); 3711556Srgrimes else 3721556Srgrimes fputs("#define digit_val(c)\t(digit_value[c])\n", hfile); 3731556Srgrimes} 3741556Srgrimes 3751556Srgrimes 3761556Srgrimes 3771556Srgrimes/* 3781556Srgrimes * Output digit conversion table (if digits are not contiguous). 3791556Srgrimes */ 3801556Srgrimes 38117987Speterstatic void 38220425Sstevedigit_convert() 38317987Speter{ 3841556Srgrimes int maxdigit; 3851556Srgrimes static char digit[] = "0123456789"; 3861556Srgrimes char *p; 3871556Srgrimes int i; 3881556Srgrimes 3891556Srgrimes maxdigit = 0; 3901556Srgrimes for (p = digit ; *p ; p++) 3911556Srgrimes if (*p > maxdigit) 3921556Srgrimes maxdigit = *p; 3931556Srgrimes fputs("extern const char digit_value[];\n", hfile); 3941556Srgrimes fputs("\n\nconst char digit_value[] = {\n", cfile); 3951556Srgrimes for (i = 0 ; i <= maxdigit ; i++) { 3961556Srgrimes for (p = digit ; *p && *p != i ; p++); 3971556Srgrimes if (*p == '\0') 3981556Srgrimes p = digit; 3991556Srgrimes fprintf(cfile, " %d,\n", p - digit); 4001556Srgrimes } 4011556Srgrimes fputs("};\n", cfile); 4021556Srgrimes} 403