1 /*
2  *
3  * Copyright 1986, 1987 by MIT Student Information Processing Board
4  *
5  * For copyright info, see "mit-sipb-copyright.h".
6  *
7  */
8 
9 #include <stdio.h>
10 #include <sys/file.h>
11 #include <strings.h>
12 #include <sys/param.h>
13 #include "mit-sipb-copyright.h"
14 
15 static char copyright[] = "Copyright 1987 by MIT Student Information Processing Board";
16 
17 extern char *gensym();
18 extern char *current_token;
19 extern int table_number, current;
20 char buffer[BUFSIZ];
21 char *table_name = (char *)NULL;
22 FILE *hfile, *cfile;
23 
24 /* C library */
25 extern int errno;
26 
27 /* lex stuff */
28 extern FILE *yyin;
29 extern int yylineno;
30 
31 /* pathnames */
32 char c_file[MAXPATHLEN];	/* temporary file */
33 char h_file[MAXPATHLEN];	/* output */
34 char o_file[MAXPATHLEN];	/* output */
35 char et_file[MAXPATHLEN];	/* input */
36 
37 main(argc, argv)
38      int argc;
39      char **argv;
40 {
41      register char *p;
42      int n_flag = 0, debug = 0;
43 
44      while (argc > 2) {
45 	  register char *arg, ch;
46 	  arg = argv[--argc];
47 	  if (strlen(arg) != 2 || arg[0] != '-')
48 	       goto usage;
49 	  ch = arg[1];
50 	  if (ch == 'n')
51 	       n_flag++;
52 	  else if (ch == 'd')
53 	       debug++;
54 	  else
55 	       goto usage;
56      }
57 
58      if (argc != 2) {
59      usage:
60 	  fprintf(stderr, "Usage:  %s et_file [-n]\n", argv[0]);
61 	  exit(1);
62      }
63 
64      strcpy(et_file, argv[1]);
65      p = rindex(et_file, '/');
66      if (p == (char *)NULL)
67 	  p = et_file;
68      else
69 	  p++;
70      p = rindex(p, '.');
71      if (!strcmp(p, ".et"))
72 	  *++p = '\0';
73      else {
74 	  if (!p)
75 	       p = et_file;
76 	  while (*p)
77 	       p++;
78 	  *p++ = '.';
79 	  *p = '\0';
80      }
81      /* p points at null where suffix should be */
82      strcpy(p, "et.c");
83      strcpy(c_file, et_file);
84      p[0] = 'h';
85      p[1] = '\0';
86      strcpy(h_file, et_file);
87      p[0] = 'o';
88      strcpy(o_file, et_file);
89      p[0] = 'e';
90      p[1] = 't';
91      p[2] = '\0';
92 
93      yyin = fopen(et_file, "r");
94      if (!yyin) {
95 	  perror(et_file);
96 	  exit(1);
97      }
98 
99      hfile = fopen(h_file, "w");
100      if (hfile == (FILE *)NULL) {
101 	  perror(h_file);
102 	  exit(1);
103      }
104 
105      cfile = fopen(c_file, "w");
106      if (cfile == (FILE *)NULL) {
107 	  perror("Can't open temp file");
108 	  exit(1);
109      }
110 
111      /* parse it */
112      fputs("#define NULL 0\n", cfile);
113      fputs("static char *_et[] = {\n", cfile);
114 
115      yyparse();
116      fclose(yyin);		/* bye bye input file */
117 
118      fputs("\t(char *)0\n};\n", cfile);
119      fputs("extern int init_error_table();\n\n", cfile);
120      fprintf(cfile, "int %s_err_base = %d;\n\n", table_name, table_number);
121      fprintf(cfile, "int\ninit_%s_err_tbl()\n", table_name);
122      fprintf(cfile, "{\n\treturn(init_error_table(_et, %d, %d));\n}\n",
123 	     table_number, current);
124      fclose(cfile);
125 
126      fputs("extern int init_", hfile);
127      fputs(table_name, hfile);
128      fputs("_err_tbl();\nextern int ", hfile);
129      fputs(table_name, hfile);
130      fputs("_err_base;\n", hfile);
131      fclose(hfile);		/* bye bye hfile */
132 
133      if (n_flag)
134 	  exit(0);
135 
136      if (!fork()) {
137 	  p = rindex(c_file, '/');
138 	  if (p) {
139 	       *p++ = '\0';
140 	       chdir(c_file);
141 	  }
142 	  else
143 	       p = c_file;
144 	  execlp("cc", "cc", "-c", "-R", "-O", p, 0);
145 	  perror("cc");
146 	  exit(1);
147      }
148      else wait(0);
149 
150      if (!debug)
151 	  (void) unlink(c_file);
152      /* make it .o file name */
153      c_file[strlen(c_file)-1] = 'o';
154      if (!fork()) {
155 	  execlp("cp", "cp", c_file, o_file, 0);
156 	  perror("cp");
157 	  exit(1);
158      }
159      else wait(0);
160      if (!debug)
161 	  (void) unlink(c_file);
162 
163      exit(0);
164 }
165 
166 yyerror(s)
167      char *s;
168 {
169      fputs(s, stderr);
170      fprintf(stderr, "\nLine number %d; last token was '%s'\n",
171 	     yylineno, current_token);
172 }
173