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