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