diff options
author | Hans Insulander <hin@cvs.openbsd.org> | 2000-02-19 12:07:20 +0000 |
---|---|---|
committer | Hans Insulander <hin@cvs.openbsd.org> | 2000-02-19 12:07:20 +0000 |
commit | b569828a4448d7136edb08499c3d8a6a22fa2b92 (patch) | |
tree | edb24d47432ae777fa1ce5e7267a44fadef5dea2 | |
parent | 62184336d5b01f0b7bcbe408e3ea4a9a7d65cc57 (diff) |
Update to a newer version from KTH-Kerberos.
(most work by art@)
-rw-r--r-- | usr.bin/compile_et/Makefile | 9 | ||||
-rw-r--r-- | usr.bin/compile_et/compile_et.1 | 28 | ||||
-rw-r--r-- | usr.bin/compile_et/compile_et.c | 449 | ||||
-rw-r--r-- | usr.bin/compile_et/compile_et.h | 83 | ||||
-rw-r--r-- | usr.bin/compile_et/compiler.h | 22 | ||||
-rw-r--r-- | usr.bin/compile_et/error_table.y | 373 | ||||
-rw-r--r-- | usr.bin/compile_et/et_lex.lex.l | 138 |
7 files changed, 559 insertions, 543 deletions
diff --git a/usr.bin/compile_et/Makefile b/usr.bin/compile_et/Makefile index db4a7b14ee8..a2eff189709 100644 --- a/usr.bin/compile_et/Makefile +++ b/usr.bin/compile_et/Makefile @@ -1,11 +1,14 @@ -# $OpenBSD: Makefile,v 1.1 1996/11/11 05:06:33 downsj Exp $ +# $OpenBSD: Makefile,v 1.2 2000/02/19 12:07:17 hin Exp $ PROG= compile_et -SRCS= compile_et.c error_table.y #et_lex.lex.l +SRCS= compile_et.c error_table.c et_lex.lex.l CFLAGS+=-I. -I${.CURDIR} -I${.CURDIR}/../../lib/libcom_err DPADD= ${LIBL} LDADD= -ll -CLEANFILES=error_table.c y.tab.h et_lex.lex.c +CLEANFILES=error_table.h error_table.c y.tab.h et_lex.lex.c + +error_table.h error_table.c: error_table.y + ${YACC} -d -o error_table.c ${.CURDIR}/error_table.y beforedepend: et_lex.lex.c error_table.o: et_lex.lex.c diff --git a/usr.bin/compile_et/compile_et.1 b/usr.bin/compile_et/compile_et.1 index eefe9bdf576..dead7fa6735 100644 --- a/usr.bin/compile_et/compile_et.1 +++ b/usr.bin/compile_et/compile_et.1 @@ -1,8 +1,8 @@ .\" Copyright (c) 1988 Massachusetts Institute of Technology, .\" Student Information Processing Board. All rights reserved. .\" -.\" $OpenBSD: compile_et.1,v 1.7 2000/01/03 00:18:05 aaron Exp $ -.\" $Header: /cvs/OpenBSD/src/usr.bin/compile_et/compile_et.1,v 1.7 2000/01/03 00:18:05 aaron Exp $ +.\" $OpenBSD: compile_et.1,v 1.8 2000/02/19 12:07:18 hin Exp $ +.\" $Header: /cvs/OpenBSD/src/usr.bin/compile_et/compile_et.1,v 1.8 2000/02/19 12:07:18 hin Exp $ .\" .Dd November 22, 1988 .Dt COMPILE_ET 1 @@ -21,23 +21,41 @@ a C source file suitable for use with the library. .Pp The source file name must end with a suffix of ``.et''; the file -consists of a declaration supplying the name (up to four characters -long) of the error-code table: +consists of headers and declarations supplying the name (up to four characters +long) of the error-code table. + +The file may have these declarations: + +.Em id string + +where string is a revision control system tag. + +.Em prefix string + +where string is prepended to all error codes specified in the file. .Em error_table name -followed by up to 256 entries of the form: +which specifies the name of the error table. + +.Em index number + +sets the assigned error number for the next entry. .Em error_code name , " .Em string " +declares an error code called name with description name. Upto +256 error codes are allowed. + and a final .Em end to indicate the end of the table. + .Pp The name of the table is used to construct the name of a subroutine .Em initialize_XXXX_error_table diff --git a/usr.bin/compile_et/compile_et.c b/usr.bin/compile_et/compile_et.c index c8a08eb103b..d7954f6421f 100644 --- a/usr.bin/compile_et/compile_et.c +++ b/usr.bin/compile_et/compile_et.c @@ -1,291 +1,222 @@ -/* $OpenBSD: compile_et.c,v 1.4 1998/05/13 17:54:20 art Exp $ */ - +/* $OpenBSD: compile_et.c,v 1.5 2000/02/19 12:07:18 hin Exp $ */ /* - * Copyright 1986, 1987, 1988 - * by MIT Student Information Processing Board. + * Copyright (c) 1998, 1999 Kungliga Tekniska Högskolan + * (Royal Institute of Technology, Stockholm, Sweden). + * All rights reserved. + * + * 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. + * + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Kungliga Tekniska + * Högskolan and its contributors. + * + * 4. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. * - * For copyright info, see "mit-sipb-copyright.h". + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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. */ -#include <stdio.h> -#include <sys/types.h> -#include <sys/file.h> -#include <string.h> -#include <sys/param.h> -#include "compiler.h" +#undef ROKEN_RENAME +#include "compile_et.h" -#ifndef __STDC__ -#define const -#endif +/* RCSID("$KTH: compile_et.c,v 1.12 1999/04/01 09:13:52 joda Exp $"); */ -#ifndef lint -static const char copyright[] = - "Copyright 1987,1988 by MIT Student Information Processing Board"; +#include <err.h> +#include "error_table.h" -static const char rcsid_compile_et_c[] = - "$Id: compile_et.c,v 1.4 1998/05/13 17:54:20 art Exp $"; -#endif +int numerror; +extern FILE *yyin; -extern char *gensym(); -extern char *current_token; -extern int table_number, current; -char buffer[BUFSIZ]; -char *table_name = NULL; -FILE *hfile, *cfile; +extern void yyparse(void); -/* C library */ -extern char *malloc(); -extern int errno; +long base; +int number; +char *prefix; +char *id_str; -/* lex stuff */ -extern FILE *yyin; -extern unsigned lineno; +char name[128]; +char Basename[128]; + +#ifdef YYDEBUG +extern int yydebug = 1; +#endif -char * xmalloc (size) unsigned int size; { - char * p = malloc (size); - if (!p) { - perror (whoami); - exit (1); +char *filename; +char hfn[128]; +char cfn[128]; + +struct error_code *codes = NULL; + +static int +generate_c(void) +{ + int n; + struct error_code *ec; + + FILE *c_file = fopen(cfn, "w"); + if(c_file == NULL) + return 1; + + fprintf(c_file, "/* Generated from %s */\n", filename); + if(id_str) + fprintf(c_file, "/* %s */\n", id_str); + fprintf(c_file, "\n"); + fprintf(c_file, "#include <stddef.h>\n"); + fprintf(c_file, "#include <com_err.h>\n"); + fprintf(c_file, "#include \"%s\"\n", hfn); + fprintf(c_file, "\n"); + + fprintf(c_file, "static const char *text[] = {\n"); + + for(ec = codes, n = 0; ec; ec = ec->next, n++) { + while(n < ec->number) { + fprintf(c_file, "\t/* %03d */ \"Reserved %s error (%d)\",\n", + n, name, n); + n++; + + } + fprintf(c_file, "\t/* %03d */ \"%s\",\n", ec->number, ec->string); } - return p; -} -static int check_arg (str_list, arg) char const *const *str_list, *arg; { - while (*str_list) - if (!strcmp(arg, *str_list++)) - return 1; + fprintf(c_file, "\tNULL\n"); + fprintf(c_file, "};\n"); + fprintf(c_file, "\n"); + fprintf(c_file, + "void initialize_%s_error_table_r(struct et_list **list)\n", + name); + fprintf(c_file, "{\n"); + fprintf(c_file, + " initialize_error_table_r(list, text, " + "%s_num_errors, ERROR_TABLE_BASE_%s);\n", name, name); + fprintf(c_file, "}\n"); + fprintf(c_file, "\n"); + fprintf(c_file, "void initialize_%s_error_table(void)\n", name); + fprintf(c_file, "{\n"); + fprintf(c_file, + " init_error_table(text, ERROR_TABLE_BASE_%s, " + "%s_num_errors);\n", name, name); + fprintf(c_file, "}\n"); + + fclose(c_file); return 0; } -static const char *const debug_args[] = { - "d", - "debug", - 0, -}; - -static const char *const lang_args[] = { - "lang", - "language", - 0, -}; - -static const char *const language_names[] = { - "C", - "K&R C", - "C++", - 0, -}; - -static const char * const c_src_prolog[] = { - "static const char * const text[] = {\n", - 0, -}; - -static const char * const krc_src_prolog[] = { - "#ifdef __STDC__\n", - "#define NOARGS void\n", - "#else\n", - "#define NOARGS\n", - "#define const\n", - "#endif\n\n", - "static const char * const text[] = {\n", - 0, -}; +static int +generate_h(void) +{ + struct error_code *ec; + char fn[128]; + FILE *h_file = fopen(hfn, "w"); + char *p; + + if(h_file == NULL) + return 1; + + snprintf(fn, sizeof(fn), "__%s__", hfn); + for(p = fn; *p; p++) + if(!isalnum((unsigned char)*p)) + *p = '_'; + + fprintf(h_file, "/* Generated from %s */\n", filename); + if(id_str) + fprintf(h_file, "/* %s */\n", id_str); + fprintf(h_file, "\n"); + fprintf(h_file, "#ifndef %s\n", fn); + fprintf(h_file, "#define %s\n", fn); + fprintf(h_file, "\n"); + fprintf(h_file, "#include <com_err.h>\n"); + fprintf(h_file, "\n"); + fprintf(h_file, + "void initialize_%s_error_table_r(struct et_list **);\n", + name); + fprintf(h_file, "\n"); + fprintf(h_file, "void initialize_%s_error_table(void);\n", name); + fprintf(h_file, "#define init_%s_err_tbl initialize_%s_error_table\n", + name, name); + fprintf(h_file, "\n"); + fprintf(h_file, "typedef enum %s_error_number{\n", name); + fprintf(h_file, "\tERROR_TABLE_BASE_%s = %ld,\n", name, base); + fprintf(h_file, "\t%s_err_base = %ld,\n", name, base); + + for(ec = codes; ec; ec = ec->next) { + fprintf(h_file, "\t%s = %ld,\n", ec->name, base + ec->number); + } -static const char *const struct_def[] = { - "struct error_table {\n", - " char const * const * msgs;\n", - " long base;\n", - " int n_msgs;\n", - "};\n", - "struct et_list {\n", - " struct et_list *next;\n", - " const struct error_table * table;\n", - "};\n", - "extern struct et_list *_et_list;\n", - "\n", 0, -}; + fprintf(h_file, "\t%s_num_errors = %d\n", name, number); + fprintf(h_file, "} %s_error_number;\n", name); + fprintf(h_file, "\n"); + fprintf(h_file, "#endif /* %s */\n", fn); -static const char warning[] = - "/*\n * %s:\n * This file is automatically generated; please do not edit it.\n */\n"; -/* pathnames */ -char c_file[MAXPATHLEN]; /* output file */ -char h_file[MAXPATHLEN]; /* output */ + fclose(h_file); + return 0; +} -static void usage () { - fprintf (stderr, "usage: %s ERROR_TABLE\n", - whoami); - exit (1); +static int +generate(void) +{ + return generate_c() || generate_h(); } -static void dup_err (type, one, two) char const *type, *one, *two; { - fprintf (stderr, "%s: multiple %s specified: `%s' and `%s'\n", - whoami, type, one, two); - usage (); +static void +usage(int code) +{ + extern char *__progname; + fprintf(stderr, "Usage: %s <file>\n", __progname); + exit(1); } -int main (argc, argv) int argc; char **argv; { - char *p, *ename; - int len; - char const * const *cpp; - int got_language = 0; +int +main(int argc, char **argv) +{ + char *p; - /* argument parsing */ - debug = 0; - filename = 0; - whoami = argv[0]; - p = strrchr (whoami, '/'); - if (p) - whoami = p+1; - while (argv++, --argc) { - char *arg = *argv; - if (arg[0] != '-') { - if (filename) - dup_err ("filenames", filename, arg); - filename = arg; - } - else { - arg++; - if (check_arg (debug_args, arg)) - debug++; - else if (check_arg (lang_args, arg)) { - got_language++; - arg = *++argv, argc--; - if (!arg) - usage (); - if (language) - dup_err ("languanges", language_names[(int)language], arg); -#define check_lang(x,v) else if (!strcasecmp(arg,x)) language = v - check_lang ("c", lang_C); - check_lang ("ansi_c", lang_C); - check_lang ("ansi-c", lang_C); - check_lang ("krc", lang_KRC); - check_lang ("kr_c", lang_KRC); - check_lang ("kr-c", lang_KRC); - check_lang ("k&r-c", lang_KRC); - check_lang ("k&r_c", lang_KRC); - check_lang ("c++", lang_CPP); - check_lang ("cplusplus", lang_CPP); - check_lang ("c-plus-plus", lang_CPP); -#undef check_lang - else { - fprintf (stderr, "%s: unknown language name `%s'\n", - whoami, arg); - fprintf (stderr, "\tpick one of: C K&R-C\n"); - exit (1); - } - } - else { - fprintf (stderr, "%s: unknown control argument -`%s'\n", - whoami, arg); - usage (); - } - } - } - if (!filename) - usage (); - if (!got_language) - language = lang_KRC; - else if (language == lang_CPP) { - fprintf (stderr, "%s: Sorry, C++ support is not yet finished.\n", - whoami); - exit (1); - } + if(argc != 2) + usage(1); - p = xmalloc (strlen (filename) + 5); - strcpy (p, filename); - filename = p; + filename = argv[1]; + yyin = fopen(filename, "r"); + if(yyin == NULL) + err(1, "%s", filename); + + p = strrchr(filename, '/'); - if (p == NULL) - p = filename; - else + if(p) p++; - ename = p; - len = strlen (ename); - p += len - 3; - if (strcmp (p, ".et")) - p += 3; - *p++ = '.'; - /* now p points to where "et" suffix should start */ - /* generate new filenames */ - strcpy (p, "c"); - strcpy (c_file, ename); - *p = 'h'; - strcpy (h_file, ename); - strcpy (p, "et"); - - yyin = fopen(filename, "r"); - if (!yyin) { - perror(filename); - exit(1); - } - - hfile = fopen(h_file, "w"); - if (hfile == NULL) { - perror(h_file); - exit(1); - } - fprintf (hfile, warning, h_file); - - cfile = fopen(c_file, "w"); - if (cfile == NULL) { - perror(c_file); - exit(1); - } - fprintf (cfile, warning, c_file); - - /* prologue */ - if (language == lang_C) - cpp = c_src_prolog; - else if (language == lang_KRC) - cpp = krc_src_prolog; else - abort (); - while (*cpp) - fputs (*cpp++, cfile); - - /* parse it */ + p = filename; + strncpy(Basename, p, sizeof(Basename)); + Basename[sizeof(Basename) - 1] = '\0'; + + Basename[strcspn(Basename, ".")] = '\0'; + + snprintf(hfn, sizeof(hfn), "%s.h", Basename); + snprintf(cfn, sizeof(cfn), "%s.c", Basename); + yyparse(); - fclose(yyin); /* bye bye input file */ - - fputs (" 0\n};\n\n", cfile); - for (cpp = struct_def; *cpp; cpp++) - fputs (*cpp, cfile); - fprintf(cfile, - "static const struct error_table et = { text, %d, %d };\n\n", - table_number, current); - fputs("static struct et_list link = { 0, 0 };\n\n", - cfile); - fprintf(cfile, "void initialize_%s_error_table (%s) {\n", - table_name, (language == lang_C) ? "void" : "NOARGS"); - fputs(" if (!link.table) {\n", cfile); - fputs(" link.next = _et_list;\n", cfile); - fputs(" link.table = &et;\n", cfile); - fputs(" _et_list = &link;\n", cfile); - fputs(" }\n", cfile); - fputs("}\n", cfile); - fclose(cfile); - - fprintf (hfile, "extern void initialize_%s_error_table ();\n", - table_name); - fprintf (hfile, "#define ERROR_TABLE_BASE_%s (%dL)\n", - table_name, table_number); - /* compatibility... */ - fprintf (hfile, "\n/* for compatibility with older versions... */\n"); - fprintf (hfile, "#define init_%s_err_tbl initialize_%s_error_table\n", - table_name, table_name); - fprintf (hfile, "#define %s_err_base ERROR_TABLE_BASE_%s\n", table_name, - table_name); - fclose(hfile); /* bye bye include file */ + if(numerror) + return 1; - return 0; -} - -int yyerror(s) char *s; { - fputs(s, stderr); - fprintf(stderr, "\nLine number %d; last token was '%s'\n", - lineno, current_token); - - return 0; + return generate(); } diff --git a/usr.bin/compile_et/compile_et.h b/usr.bin/compile_et/compile_et.h new file mode 100644 index 00000000000..332ce2f984d --- /dev/null +++ b/usr.bin/compile_et/compile_et.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 1998 Kungliga Tekniska Högskolan + * (Royal Institute of Technology, Stockholm, Sweden). + * All rights reserved. + * + * 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. + * + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Kungliga Tekniska + * Högskolan and its contributors. + * + * 4. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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. + */ + +/* $KTH: compile_et.h,v 1.3 1998/11/22 09:39:46 assar Exp $ */ + +#ifndef __COMPILE_ET_H__ +#define __COMPILE_ET_H__ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdarg.h> +#include <ctype.h> + +extern long base; +extern int number; +extern char *prefix; +extern char name[128]; +extern char *id_str; +extern char *filename; +extern int numerror; + +struct error_code { + unsigned number; + char *name; + char *string; + struct error_code *next, **tail; +}; + +extern struct error_code *codes; + +#define APPEND(L, V) \ +do { \ + if((L) == NULL) { \ + (L) = (V); \ + (L)->tail = &(V)->next; \ + (L)->next = NULL; \ + }else{ \ + *(L)->tail = (V); \ + (L)->tail = &(V)->next; \ + } \ +}while(0) + +#endif /* __COMPILE_ET_H__ */ diff --git a/usr.bin/compile_et/compiler.h b/usr.bin/compile_et/compiler.h deleted file mode 100644 index d8bce211a1c..00000000000 --- a/usr.bin/compile_et/compiler.h +++ /dev/null @@ -1,22 +0,0 @@ -/* $OpenBSD: compiler.h,v 1.1 1996/11/11 05:06:34 downsj Exp $ */ - -/* - * definitions common to the source files of the error table compiler - */ - -#ifndef __STDC__ -/* loser */ -#undef const -#define const -#endif - -enum lang { - lang_C, /* ANSI C (default) */ - lang_KRC, /* C: ANSI + K&R */ - lang_CPP /* C++ */ -}; - -int debug; /* dump debugging info? */ -char *filename; /* error table source */ -enum lang language; -const char *whoami; diff --git a/usr.bin/compile_et/error_table.y b/usr.bin/compile_et/error_table.y index 2d32ca87e08..93ed55f7555 100644 --- a/usr.bin/compile_et/error_table.y +++ b/usr.bin/compile_et/error_table.y @@ -1,264 +1,171 @@ %{ -/* $OpenBSD: error_table.y,v 1.2 1998/05/13 17:54:21 art Exp $ */ - -/*- - * Copyright 1987, 1988 by the Student Information Processing Board - * of the Massachusetts Institute of Technology +/* + * Copyright (c) 1998, 1999 Kungliga Tekniska Högskolan + * (Royal Institute of Technology, Stockholm, Sweden). + * All rights reserved. + * + * 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. * - * Permission to use, copy, modify, and distribute this software - * and its documentation for any purpose and without fee is - * hereby granted, provided that the above copyright notice - * appear in all copies and that both that copyright notice and - * this permission notice appear in supporting documentation, - * and that the names of M.I.T. and the M.I.T. S.I.P.B. not be - * used in advertising or publicity pertaining to distribution - * of the software without specific, written prior permission. - * M.I.T. and the M.I.T. S.I.P.B. make no representations about - * the suitability of this software for any purpose. It is - * provided "as is" without express or implied warranty. + * 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. + * + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Kungliga Tekniska + * Högskolan and its contributors. + * + * 4. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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. */ -#include <stdio.h> -#include <stdlib.h> -char *str_concat(), *ds(), *quote(); -char *current_token = (char *)NULL; -extern char *table_name; +#include "compile_et.h" +/* RCSID("$KTH: parse.y,v 1.9 1999/07/04 14:54:58 assar Exp $"); */ + +void yyerror (char *s); +long name2number(const char *str); +void error_message(char *, ...); + +extern char *yytext; + +/* This is for bison */ + +#if !defined(alloca) && !defined(HAVE_ALLOCA) +#define alloca(x) malloc(x) +#endif + %} + %union { - char *dynstr; + char *string; + int number; } -%token ERROR_TABLE ERROR_CODE_ENTRY END -%token <dynstr> STRING QUOTED_STRING -%type <dynstr> ec_name description table_id -%{ -%} -%start error_table +%token ET INDEX PREFIX EC ID END +%token <string> STRING +%token <number> NUMBER + %% -error_table : ERROR_TABLE table_id error_codes END - { table_name = ds($2); - current_token = table_name; - put_ecs(); } +file : /* */ + | header statements ; -table_id : STRING - { current_token = $1; - set_table_num($1); - $$ = $1; } +header : id et + | et ; -error_codes : error_codes ec_entry - | ec_entry +id : ID STRING + { + id_str = $2; + } ; -ec_entry : ERROR_CODE_ENTRY ec_name ',' description - { add_ec($2, $4); - free($2); - free($4); } - | ERROR_CODE_ENTRY ec_name '=' STRING ',' description - { add_ec_val($2, $4, $6); - free($2); - free($4); - free($6); - } +et : ET STRING + { + base = name2number($2); + strncpy(name, $2, sizeof(name)); + name[sizeof(name) - 1] = '\0'; + free($2); + } + | ET STRING STRING + { + base = name2number($2); + strncpy(name, $3, sizeof(name)); + name[sizeof(name) - 1] = '\0'; + free($2); + free($3); + } ; -ec_name : STRING - { $$ = ds($1); - current_token = $$; } +statements : statement + | statements statement ; -description : QUOTED_STRING - { $$ = ds($1); - current_token = $$; } +statement : INDEX NUMBER + { + number = $2; + } + | PREFIX STRING + { + prefix = realloc(prefix, strlen($2) + 2); + strcpy(prefix, $2); + strcat(prefix, "_"); + free($2); + } + | PREFIX + { + prefix = realloc(prefix, 1); + *prefix = '\0'; + } + | EC STRING ',' STRING + { + struct error_code *ec = malloc(sizeof(*ec)); + + ec->next = NULL; + ec->number = number; + if(prefix && *prefix != '\0') { + asprintf (&ec->name, "%s%s", prefix, $2); + free($2); + } else + ec->name = $2; + ec->string = $4; + APPEND(codes, ec); + number++; + } + | END + { + YYACCEPT; + } ; %% -/* - * Copyright 1987, 1988 by the Student Information Processing Board - * of the Massachusetts Institute of Technology - * - * Permission to use, copy, modify, and distribute this software - * and its documentation for any purpose and without fee is - * hereby granted, provided that the above copyright notice - * appear in all copies and that both that copyright notice and - * this permission notice appear in supporting documentation, - * and that the names of M.I.T. and the M.I.T. S.I.P.B. not be - * used in advertising or publicity pertaining to distribution - * of the software without specific, written prior permission. - * M.I.T. and the M.I.T. S.I.P.B. make no representations about - * the suitability of this software for any purpose. It is - * provided "as is" without express or implied warranty. - */ - -#include <string.h> -#include <assert.h> -#include <ctype.h> -#include <sys/types.h> -#include <sys/time.h> -#include "error_table.h" - -#ifndef lint -static char const rcsid_error_table_y[] = - "$Id: error_table.y,v 1.2 1998/05/13 17:54:21 art Exp $"; -#endif - -#include "et_lex.lex.c" - -extern FILE *hfile, *cfile; - -static long gensym_n = 0; -char * -gensym(x) - char const *x; -{ - char *symbol; - if (!gensym_n) { - struct timeval tv; - struct timezone tzp; - gettimeofday(&tv, &tzp); - gensym_n = (tv.tv_sec%10000)*100 + tv.tv_usec/10000; - } - symbol = (char *)malloc(32 * sizeof(char)); - gensym_n++; - snprintf(symbol, 32 * sizeof(char), "et%ld", gensym_n); - return(symbol); -} - -char * -ds(string) - char const *string; -{ - char *rv; - rv = (char *)malloc(strlen(string)+1); - strcpy(rv, string); - return(rv); -} - -char * -quote(string) - char const *string; -{ - char *rv; - rv = (char *)malloc(strlen(string)+3); - strcpy(rv, "\""); - strcat(rv, string); - strcat(rv, "\""); - return(rv); -} - -int table_number; -int current = 0; -char **error_codes = (char **)NULL; - -void -add_ec(name, description) - char const *name, *description; -{ - fprintf(cfile, "\t\"%s\",\n", description); - if (error_codes == (char **)NULL) { - error_codes = (char **)malloc(sizeof(char *)); - *error_codes = (char *)NULL; - } - error_codes = (char **)realloc((char *)error_codes, - (current + 2)*sizeof(char *)); - error_codes[current++] = ds(name); - error_codes[current] = (char *)NULL; -} - -void -add_ec_val(name, val, description) - char const *name, *val, *description; -{ - const int ncurrent = atoi(val); - if (ncurrent < current) { - printf("Error code %s (%d) out of order", name, - current); - return; - } - - while (ncurrent > current) - fputs("\t(char *)NULL,\n", cfile), current++; - - fprintf(cfile, "\t\"%s\",\n", description); - if (error_codes == (char **)NULL) { - error_codes = (char **)malloc(sizeof(char *)); - *error_codes = (char *)NULL; - } - error_codes = (char **)realloc((char *)error_codes, - (current + 2)*sizeof(char *)); - error_codes[current++] = ds(name); - error_codes[current] = (char *)NULL; -} -void -put_ecs() +long +name2number(const char *str) { - int i; - for (i = 0; i < current; i++) { - if (error_codes[i] != (char *)NULL) - fprintf(hfile, "#define %-40s (%d)\n", - error_codes[i], table_number + i); + const char *p; + long base = 0; + const char *x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz0123456789_"; + if(strlen(str) > 4) { + yyerror("table name too long"); + return 0; + } + for(p = str; *p; p++){ + char *q = strchr(x, *p); + if(q == NULL) { + yyerror("invalid character in table name"); + return 0; } -} - -/* - * char_to_num -- maps letters and numbers into a small numbering space - * uppercase -> 1-26 - * lowercase -> 27-52 - * digits -> 53-62 - * underscore-> 63 - */ - -static const char char_set[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"; - -int char_to_num(c) - char c; -{ - const char *where; - int diff; - - where = strchr (char_set, c); - if (where) { - diff = where - char_set + 1; - assert (diff < (1 << ERRCODE_RANGE)); - return diff; - } - else if (isprint (c)) - fprintf (stderr, - "Illegal character `%c' in error table name\n", - c); - else - fprintf (stderr, - "Illegal character %03o in error table name\n", - c); - exit (1); + base = (base << 6) + (q - x) + 1; + } + base <<= 8; + if(base > 0x7fffffff) + base = -(0xffffffff - base + 1); + return base; } void -set_table_num(string) - char *string; +yyerror (char *s) { - if (char_to_num (string[0]) > char_to_num ('z')) { - fprintf (stderr, "%s%s%s%s", - "First character of error table name must be ", - "a letter; name ``", - string, "'' rejected\n"); - exit (1); - } - if (strlen(string) > 4) { - fprintf(stderr, "Table name %s too long, truncated ", - string); - string[4] = '\0'; - fprintf(stderr, "to %s\n", string); - } - while (*string != '\0') { - table_number = (table_number << BITS_PER_CHAR) - + char_to_num(*string); - string++; - } - table_number = table_number << ERRCODE_RANGE; + error_message ("%s\n", s); } - diff --git a/usr.bin/compile_et/et_lex.lex.l b/usr.bin/compile_et/et_lex.lex.l index e25e18328a1..b2f0a95d315 100644 --- a/usr.bin/compile_et/et_lex.lex.l +++ b/usr.bin/compile_et/et_lex.lex.l @@ -1,31 +1,127 @@ %{ - unsigned lineno = 1; -%} +/* + * Copyright (c) 1998 Kungliga Tekniska Högskolan + * (Royal Institute of Technology, Stockholm, Sweden). + * All rights reserved. + * + * 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. + * + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Kungliga Tekniska + * Högskolan and its contributors. + * + * 4. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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. + */ -PC [^\"] -AN [A-Z_a-z0-9] -%% +/* + * This is to handle the definition of this symbol in some AIX + * headers, which will conflict with the definition that lex will + * generate for it. It's only a problem for AIX lex. + */ -error_table return ERROR_TABLE; -et return ERROR_TABLE; -error_code return ERROR_CODE_ENTRY; -ec return ERROR_CODE_ENTRY; -end return END; +#undef ECHO -[\t ] ; -\n ++lineno; +#include "compile_et.h" +#include "error_table.h" -\"{PC}*\" { register char *p; yylval.dynstr = ds(yytext+1); - if ((p = strrchr(yylval.dynstr, '"')) != NULL) *p = '\0'; - return QUOTED_STRING; - } +/* RCSID("$KTH: lex.l,v 1.4 1998/11/20 05:58:52 assar Exp $"); */ -{AN}* { yylval.dynstr = ds(yytext); return STRING; } +static unsigned lineno = 1; +void error_message(char *, ...); +int getstring(void); -#.*\n ++lineno; +%} -. { return (*yytext); } + +%% +et { return ET; } +error_table { return ET; } +ec { return EC; } +error_code { return EC; } +prefix { return PREFIX; } +index { return INDEX; } +id { return ID; } +end { return END; } +[0-9]+ { yylval.number = atoi(yytext); return NUMBER; } +#[^\n]* ; +[ \t] ; +\n { lineno++; } +\" { return getstring(); } +[a-zA-Z0-9_]+ { yylval.string = strdup(yytext); return STRING; } +. { return *yytext; } %% -#ifndef lint -static char rcsid_et_lex_lex_l[] = "$Id: et_lex.lex.l,v 1.2 1998/05/13 17:54:22 art Exp $"; + +#ifndef yywrap /* XXX */ +int +yywrap () +{ + return 1; +} #endif + +int +getstring(void) +{ + char x[128]; + int i = 0; + int c; + int quote = 0; + while((c = input()) != EOF){ + if(quote) { + x[i++] = c; + quote = 0; + continue; + } + if(c == '\n'){ + error_message("unterminated string"); + lineno++; + break; + } + if(c == '\\'){ + quote++; + continue; + } + if(c == '\"') + break; + x[i++] = c; + } + x[i] = '\0'; + yylval.string = strdup(x); + return STRING; +} + +void +error_message (char *format, ...) +{ + va_list args; + + va_start (args, format); + fprintf (stderr, "%s:%d:", filename, lineno); + vfprintf (stderr, format, args); + va_end (args); + numerror++; +} |