summaryrefslogtreecommitdiffstats
path: root/eBones/compile_et/compile_et.c
blob: 25be70bf252f0e68ad79cc19258f6a0508ee6254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 *
 * Copyright 1986, 1987 by MIT Student Information Processing Board
 * For copyright info, see "Copyright.SIPB".
 *
 *	$Id: compile_et.c,v 1.2 1994/07/19 19:21:24 g89r4222 Exp $
 */

#include <stdio.h>
#include <sys/file.h>
#include <strings.h>
#include <sys/param.h>

static char copyright[] = "Copyright 1987 by MIT Student Information Processing Board";

extern char *gensym();
extern char *current_token;
extern int table_number, current;
char buffer[BUFSIZ];
char *table_name = (char *)NULL;
FILE *hfile, *cfile;
     
/* C library */
extern char *malloc();
extern int errno;
     
/* lex stuff */
extern FILE *yyin;
extern int yylineno;
     
/* pathnames */
char c_file[MAXPATHLEN];	/* temporary file */
char h_file[MAXPATHLEN];	/* output */
char o_file[MAXPATHLEN];	/* output */
char et_file[MAXPATHLEN];	/* input */

main(argc, argv)
     int argc;
     char **argv;
{
     register char *p;
     int n_flag = 0, debug = 0;
     
     while (argc > 2) {
	  register char *arg, ch;
	  arg = argv[--argc];
	  if (strlen(arg) != 2 || arg[0] != '-')
	       goto usage;
	  ch = arg[1];
	  if (ch == 'n')
	       n_flag++;
	  else if (ch == 'd')
	       debug++;
	  else
	       goto usage;
     }

     if (argc != 2) {
     usage:
	  fprintf(stderr, "Usage:  %s et_file [-n]\n", argv[0]);
	  exit(1);
     }
     
     strcpy(et_file, argv[1]);
     p = rindex(et_file, '/');
     if (p == (char *)NULL)
	  p = et_file;
     else
	  p++;
     p = rindex(p, '.');
     if (!strcmp(p, ".et"))
	  *++p = '\0';
     else {
	  if (!p)
	       p = et_file;
	  while (*p)
	       p++;
	  *p++ = '.';
	  *p = '\0';
     }
     /* p points at null where suffix should be */
     strcpy(p, "et.c");
     strcpy(c_file, et_file);
     p[0] = 'h';
     p[1] = '\0';
     strcpy(h_file, et_file);
     p[0] = 'o';
     strcpy(o_file, et_file);
     p[0] = 'e';
     p[1] = 't';
     p[2] = '\0';

     yyin = fopen(et_file, "r");
     if (!yyin) {
	  perror(et_file);
	  exit(1);
     }
     
     hfile = fopen(h_file, "w");
     if (hfile == (FILE *)NULL) {
	  perror(h_file);
	  exit(1);
     }
     
     cfile = fopen(c_file, "w");
     if (cfile == (FILE *)NULL) {
	  perror("Can't open temp file");
	  exit(1);
     }
     
     /* parse it */
     fputs("#define NULL 0\n", cfile);
     fputs("static char *_et[] = {\n", cfile);
     
     yyparse();
     fclose(yyin);		/* bye bye input file */
     
     fputs("\t(char *)0\n};\n", cfile);
     fputs("extern int init_error_table();\n\n", cfile);
     fprintf(cfile, "int %s_err_base = %d;\n\n", table_name, table_number);
     fprintf(cfile, "int\ninit_%s_err_tbl()\n", table_name);
     fprintf(cfile, "{\n\treturn(init_error_table(_et, %d, %d));\n}\n",
	     table_number, current);
     fclose(cfile);
     
     fputs("extern int init_", hfile);
     fputs(table_name, hfile);
     fputs("_err_tbl();\nextern int ", hfile);
     fputs(table_name, hfile);
     fputs("_err_base;\n", hfile);
     fclose(hfile);		/* bye bye hfile */
     
     if (n_flag)
	  exit(0);

     if (!fork()) {
	  p = rindex(c_file, '/');
	  if (p) {
	       *p++ = '\0';
	       chdir(c_file);
	  }
	  else
	       p = c_file;
	  execlp("cc", "cc", "-c", "-R", "-O", p, 0);
	  perror("cc");
	  exit(1);
     }
     else wait(0);

     if (!debug)
	  (void) unlink(c_file);
     /* make it .o file name */
     c_file[strlen(c_file)-1] = 'o';
     if (!fork()) {
	  execlp("cp", "cp", c_file, o_file, 0);
	  perror("cp");
	  exit(1);
     }
     else wait(0);
     if (!debug)
	  (void) unlink(c_file);

     exit(0);
}

yyerror(s)
     char *s;
{
     fputs(s, stderr);
     fprintf(stderr, "\nLine number %d; last token was '%s'\n",
	     yylineno, current_token);
}
OpenPOWER on IntegriCloud