summaryrefslogtreecommitdiff
path: root/usr.bin/compile_et/compile_et.c
diff options
context:
space:
mode:
authorHans Insulander <hin@cvs.openbsd.org>2000-02-19 12:07:20 +0000
committerHans Insulander <hin@cvs.openbsd.org>2000-02-19 12:07:20 +0000
commitb569828a4448d7136edb08499c3d8a6a22fa2b92 (patch)
treeedb24d47432ae777fa1ce5e7267a44fadef5dea2 /usr.bin/compile_et/compile_et.c
parent62184336d5b01f0b7bcbe408e3ea4a9a7d65cc57 (diff)
Update to a newer version from KTH-Kerberos.
(most work by art@)
Diffstat (limited to 'usr.bin/compile_et/compile_et.c')
-rw-r--r--usr.bin/compile_et/compile_et.c449
1 files changed, 190 insertions, 259 deletions
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();
}