1994-05-26 06:18:55 +00:00
|
|
|
/*-
|
2017-11-20 19:49:47 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1994-05-26 06:18:55 +00:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Kenneth Almquist.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-26 06:18:55 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-05-01 16:58:57 +00:00
|
|
|
#if 0
|
1994-05-26 06:18:55 +00:00
|
|
|
#ifndef lint
|
1996-12-14 06:20:03 +00:00
|
|
|
static char const copyright[] =
|
1994-05-26 06:18:55 +00:00
|
|
|
"@(#) Copyright (c) 1991, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-05-18 06:44:24 +00:00
|
|
|
static char sccsid[] = "@(#)mksyntax.c 8.2 (Berkeley) 5/4/95";
|
1994-05-26 06:18:55 +00:00
|
|
|
#endif /* not lint */
|
2003-05-01 16:58:57 +00:00
|
|
|
#endif
|
2002-06-30 05:15:05 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This program creates syntax.h and syntax.c.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2001-06-19 15:41:57 +00:00
|
|
|
#include <stdlib.h>
|
1996-09-01 10:22:36 +00:00
|
|
|
#include <string.h>
|
1994-05-26 06:18:55 +00:00
|
|
|
#include "parser.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct synclass {
|
2009-12-27 18:04:05 +00:00
|
|
|
const char *name;
|
|
|
|
const char *comment;
|
1994-05-26 06:18:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Syntax classes */
|
2015-03-03 21:21:43 +00:00
|
|
|
static const struct synclass synclass[] = {
|
1996-09-01 10:22:36 +00:00
|
|
|
{ "CWORD", "character is nothing special" },
|
|
|
|
{ "CNL", "newline character" },
|
2018-05-21 21:52:48 +00:00
|
|
|
{ "CQNL", "newline character in quotes" },
|
1996-09-01 10:22:36 +00:00
|
|
|
{ "CBACK", "a backslash character" },
|
2011-05-05 20:55:55 +00:00
|
|
|
{ "CSBACK", "a backslash character in single quotes" },
|
1996-09-01 10:22:36 +00:00
|
|
|
{ "CSQUOTE", "single quote" },
|
|
|
|
{ "CDQUOTE", "double quote" },
|
|
|
|
{ "CENDQUOTE", "a terminating quote" },
|
|
|
|
{ "CBQUOTE", "backwards single quote" },
|
|
|
|
{ "CVAR", "a dollar sign" },
|
|
|
|
{ "CENDVAR", "a '}' character" },
|
|
|
|
{ "CLP", "a left paren in arithmetic" },
|
|
|
|
{ "CRP", "a right paren in arithmetic" },
|
|
|
|
{ "CEOF", "end of file" },
|
|
|
|
{ "CCTL", "like CWORD, except it must be escaped" },
|
|
|
|
{ "CSPCL", "these terminate a word" },
|
2010-10-24 22:25:38 +00:00
|
|
|
{ "CIGN", "character should be ignored" },
|
1996-09-01 10:22:36 +00:00
|
|
|
{ NULL, NULL }
|
1994-05-26 06:18:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Syntax classes for is_ functions. Warning: if you add new classes
|
|
|
|
* you may have to change the definition of the is_in_name macro.
|
|
|
|
*/
|
2015-03-03 21:21:43 +00:00
|
|
|
static const struct synclass is_entry[] = {
|
1996-09-01 10:22:36 +00:00
|
|
|
{ "ISDIGIT", "a digit" },
|
|
|
|
{ "ISUPPER", "an upper case letter" },
|
|
|
|
{ "ISLOWER", "a lower case letter" },
|
|
|
|
{ "ISUNDER", "an underscore" },
|
|
|
|
{ "ISSPECL", "the name of a special parameter" },
|
|
|
|
{ NULL, NULL }
|
1994-05-26 06:18:55 +00:00
|
|
|
};
|
|
|
|
|
2015-03-03 21:21:43 +00:00
|
|
|
static const char writer[] = "\
|
1994-05-26 06:18:55 +00:00
|
|
|
/*\n\
|
|
|
|
* This file was generated by the mksyntax program.\n\
|
|
|
|
*/\n\
|
|
|
|
\n";
|
|
|
|
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
static FILE *cfile;
|
|
|
|
static FILE *hfile;
|
|
|
|
|
2013-02-07 22:42:33 +00:00
|
|
|
static void add_default(void);
|
|
|
|
static void finish(void);
|
|
|
|
static void init(const char *);
|
2009-12-27 18:04:05 +00:00
|
|
|
static void add(const char *, const char *);
|
2002-02-02 06:50:57 +00:00
|
|
|
static void output_type_macros(void);
|
1996-09-01 10:22:36 +00:00
|
|
|
|
|
|
|
int
|
2002-02-02 06:50:57 +00:00
|
|
|
main(int argc __unused, char **argv __unused)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
1994-05-26 06:18:55 +00:00
|
|
|
int i;
|
|
|
|
char buf[80];
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
/* Create output files */
|
|
|
|
if ((cfile = fopen("syntax.c", "w")) == NULL) {
|
|
|
|
perror("syntax.c");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
if ((hfile = fopen("syntax.h", "w")) == NULL) {
|
|
|
|
perror("syntax.h");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
fputs(writer, hfile);
|
|
|
|
fputs(writer, cfile);
|
|
|
|
|
|
|
|
fputs("#include <sys/cdefs.h>\n", hfile);
|
2013-02-07 22:42:33 +00:00
|
|
|
fputs("#include <limits.h>\n\n", hfile);
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
/* Generate the #define statements in the header file */
|
|
|
|
fputs("/* Syntax classes */\n", hfile);
|
|
|
|
for (i = 0 ; synclass[i].name ; i++) {
|
|
|
|
sprintf(buf, "#define %s %d", synclass[i].name, i);
|
|
|
|
fputs(buf, hfile);
|
1996-09-01 10:22:36 +00:00
|
|
|
for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
|
1994-05-26 06:18:55 +00:00
|
|
|
putc('\t', hfile);
|
|
|
|
fprintf(hfile, "/* %s */\n", synclass[i].comment);
|
|
|
|
}
|
|
|
|
putc('\n', hfile);
|
|
|
|
fputs("/* Syntax classes for is_ functions */\n", hfile);
|
|
|
|
for (i = 0 ; is_entry[i].name ; i++) {
|
|
|
|
sprintf(buf, "#define %s %#o", is_entry[i].name, 1 << i);
|
|
|
|
fputs(buf, hfile);
|
1996-09-01 10:22:36 +00:00
|
|
|
for (pos = strlen(buf) ; pos < 32 ; pos = (pos + 8) & ~07)
|
1994-05-26 06:18:55 +00:00
|
|
|
putc('\t', hfile);
|
|
|
|
fprintf(hfile, "/* %s */\n", is_entry[i].comment);
|
|
|
|
}
|
|
|
|
putc('\n', hfile);
|
2013-02-07 22:42:33 +00:00
|
|
|
fputs("#define SYNBASE (1 - CHAR_MIN)\n", hfile);
|
|
|
|
fputs("#define PEOF -SYNBASE\n\n", hfile);
|
1994-05-26 06:18:55 +00:00
|
|
|
putc('\n', hfile);
|
|
|
|
fputs("#define BASESYNTAX (basesyntax + SYNBASE)\n", hfile);
|
|
|
|
fputs("#define DQSYNTAX (dqsyntax + SYNBASE)\n", hfile);
|
|
|
|
fputs("#define SQSYNTAX (sqsyntax + SYNBASE)\n", hfile);
|
|
|
|
fputs("#define ARISYNTAX (arisyntax + SYNBASE)\n", hfile);
|
|
|
|
putc('\n', hfile);
|
|
|
|
output_type_macros(); /* is_digit, etc. */
|
|
|
|
putc('\n', hfile);
|
|
|
|
|
|
|
|
/* Generate the syntax tables. */
|
2013-02-07 22:42:33 +00:00
|
|
|
fputs("#include \"parser.h\"\n", cfile);
|
1994-05-26 06:18:55 +00:00
|
|
|
fputs("#include \"shell.h\"\n", cfile);
|
|
|
|
fputs("#include \"syntax.h\"\n\n", cfile);
|
2013-02-07 22:42:33 +00:00
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
fputs("/* syntax table used when not in quotes */\n", cfile);
|
2013-02-07 22:42:33 +00:00
|
|
|
init("basesyntax");
|
|
|
|
add_default();
|
1994-05-26 06:18:55 +00:00
|
|
|
add("\n", "CNL");
|
|
|
|
add("\\", "CBACK");
|
|
|
|
add("'", "CSQUOTE");
|
|
|
|
add("\"", "CDQUOTE");
|
|
|
|
add("`", "CBQUOTE");
|
|
|
|
add("$", "CVAR");
|
|
|
|
add("}", "CENDVAR");
|
|
|
|
add("<>();&| \t", "CSPCL");
|
2013-02-07 22:42:33 +00:00
|
|
|
finish();
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
fputs("\n/* syntax table used when in double quotes */\n", cfile);
|
2013-02-07 22:42:33 +00:00
|
|
|
init("dqsyntax");
|
|
|
|
add_default();
|
2018-05-21 21:52:48 +00:00
|
|
|
add("\n", "CQNL");
|
1994-05-26 06:18:55 +00:00
|
|
|
add("\\", "CBACK");
|
|
|
|
add("\"", "CENDQUOTE");
|
|
|
|
add("`", "CBQUOTE");
|
|
|
|
add("$", "CVAR");
|
|
|
|
add("}", "CENDVAR");
|
2011-12-28 23:51:17 +00:00
|
|
|
/* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
|
|
|
|
add("!*?[]=~:/-^", "CCTL");
|
2013-02-07 22:42:33 +00:00
|
|
|
finish();
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
fputs("\n/* syntax table used when in single quotes */\n", cfile);
|
2013-02-07 22:42:33 +00:00
|
|
|
init("sqsyntax");
|
|
|
|
add_default();
|
2018-05-21 21:52:48 +00:00
|
|
|
add("\n", "CQNL");
|
2011-05-05 20:55:55 +00:00
|
|
|
add("\\", "CSBACK");
|
1994-05-26 06:18:55 +00:00
|
|
|
add("'", "CENDQUOTE");
|
2011-12-28 23:51:17 +00:00
|
|
|
/* ':/' for tilde expansion, '-^]' for [a\-x] pattern ranges */
|
|
|
|
add("!*?[]=~:/-^", "CCTL");
|
2013-02-07 22:42:33 +00:00
|
|
|
finish();
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
fputs("\n/* syntax table used when in arithmetic */\n", cfile);
|
2013-02-07 22:42:33 +00:00
|
|
|
init("arisyntax");
|
|
|
|
add_default();
|
2018-05-21 21:52:48 +00:00
|
|
|
add("\n", "CQNL");
|
1994-05-26 06:18:55 +00:00
|
|
|
add("\\", "CBACK");
|
|
|
|
add("`", "CBQUOTE");
|
2010-10-24 22:25:38 +00:00
|
|
|
add("\"", "CIGN");
|
1994-05-26 06:18:55 +00:00
|
|
|
add("$", "CVAR");
|
|
|
|
add("}", "CENDVAR");
|
|
|
|
add("(", "CLP");
|
|
|
|
add(")", "CRP");
|
2013-02-07 22:42:33 +00:00
|
|
|
finish();
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
fputs("\n/* character classification table */\n", cfile);
|
2013-02-07 22:42:33 +00:00
|
|
|
init("is_type");
|
1994-05-26 06:18:55 +00:00
|
|
|
add("0123456789", "ISDIGIT");
|
2010-08-08 21:04:27 +00:00
|
|
|
add("abcdefghijklmnopqrstuvwxyz", "ISLOWER");
|
|
|
|
add("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ISUPPER");
|
1994-05-26 06:18:55 +00:00
|
|
|
add("_", "ISUNDER");
|
|
|
|
add("#?$!-*@", "ISSPECL");
|
2013-02-07 22:42:33 +00:00
|
|
|
finish();
|
|
|
|
|
1994-05-26 06:18:55 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-02-07 22:42:33 +00:00
|
|
|
* Output the header and declaration of a syntax table.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
static void
|
2013-02-07 22:42:33 +00:00
|
|
|
init(const char *name)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2013-02-07 22:42:33 +00:00
|
|
|
fprintf(hfile, "extern const char %s[];\n", name);
|
|
|
|
fprintf(cfile, "const char %s[SYNBASE + CHAR_MAX + 1] = {\n", name);
|
|
|
|
}
|
1994-05-26 06:18:55 +00:00
|
|
|
|
2013-02-07 22:42:33 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
add_one(const char *key, const char *type)
|
|
|
|
{
|
|
|
|
fprintf(cfile, "\t[SYNBASE + %s] = %s,\n", key, type);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-02-07 22:42:33 +00:00
|
|
|
* Add default values to the syntax table.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
static void
|
2013-02-07 22:42:33 +00:00
|
|
|
add_default(void)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2013-02-07 22:42:33 +00:00
|
|
|
add_one("PEOF", "CEOF");
|
|
|
|
add_one("CTLESC", "CCTL");
|
|
|
|
add_one("CTLVAR", "CCTL");
|
|
|
|
add_one("CTLENDVAR", "CCTL");
|
|
|
|
add_one("CTLBACKQ", "CCTL");
|
|
|
|
add_one("CTLBACKQ + CTLQUOTE", "CCTL");
|
|
|
|
add_one("CTLARI", "CCTL");
|
|
|
|
add_one("CTLENDARI", "CCTL");
|
|
|
|
add_one("CTLQUOTEMARK", "CCTL");
|
|
|
|
add_one("CTLQUOTEEND", "CCTL");
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-02-07 22:42:33 +00:00
|
|
|
* Output the footer of a syntax table.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
static void
|
2013-02-07 22:42:33 +00:00
|
|
|
finish(void)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2013-02-07 22:42:33 +00:00
|
|
|
fputs("};\n", cfile);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-02-07 22:42:33 +00:00
|
|
|
* Add entries to the syntax table.
|
1994-05-26 06:18:55 +00:00
|
|
|
*/
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
static void
|
2013-02-07 22:42:33 +00:00
|
|
|
add(const char *p, const char *type)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2013-02-07 22:42:33 +00:00
|
|
|
for (; *p; ++p) {
|
|
|
|
char c = *p;
|
|
|
|
switch (c) {
|
|
|
|
case '\t': c = 't'; break;
|
|
|
|
case '\n': c = 'n'; break;
|
|
|
|
case '\'': c = '\''; break;
|
|
|
|
case '\\': c = '\\'; break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(cfile, "\t[SYNBASE + '%c'] = %s,\n", c, type);
|
|
|
|
continue;
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
2013-02-07 22:42:33 +00:00
|
|
|
fprintf(cfile, "\t[SYNBASE + '\\%c'] = %s,\n", c, type);
|
1994-05-26 06:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Output character classification macros (e.g. is_digit). If digits are
|
|
|
|
* contiguous, we can test for them quickly.
|
|
|
|
*/
|
|
|
|
|
2009-12-27 18:04:05 +00:00
|
|
|
static const char *macro[] = {
|
2013-02-05 22:54:09 +00:00
|
|
|
"#define is_digit(c)\t((unsigned int)((c) - '0') <= 9)",
|
2005-08-13 15:47:13 +00:00
|
|
|
"#define is_eof(c)\t((c) == PEOF)",
|
2010-11-20 14:30:28 +00:00
|
|
|
"#define is_alpha(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER))",
|
|
|
|
"#define is_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER))",
|
|
|
|
"#define is_in_name(c)\t((is_type+SYNBASE)[(int)c] & (ISUPPER|ISLOWER|ISUNDER|ISDIGIT))",
|
2010-09-03 22:13:54 +00:00
|
|
|
"#define is_special(c)\t((is_type+SYNBASE)[(int)c] & (ISSPECL|ISDIGIT))",
|
2013-02-05 22:54:09 +00:00
|
|
|
"#define digit_val(c)\t((c) - '0')",
|
1994-05-26 06:18:55 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
1996-09-01 10:22:36 +00:00
|
|
|
static void
|
2002-02-02 06:50:57 +00:00
|
|
|
output_type_macros(void)
|
1996-09-01 10:22:36 +00:00
|
|
|
{
|
2009-12-27 18:04:05 +00:00
|
|
|
const char **pp;
|
1994-05-26 06:18:55 +00:00
|
|
|
|
|
|
|
for (pp = macro ; *pp ; pp++)
|
|
|
|
fprintf(hfile, "%s\n", *pp);
|
|
|
|
}
|