-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscottcom.c
214 lines (168 loc) · 3.71 KB
/
scottcom.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* ScottCom -- Compiles adventures into the Scott Adams format.
* Copyright (C) 1985-1996 Bjorn Gustavsson
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: scottcom.c,v 1.2 1996/10/17 04:01:57 bjorn Exp $
*
*/
#include "scottcom.h"
#include <ctype.h>
char acknowledge[] = "***** Compiled by ScottCom, v " VERSION " *****";
char *progname = "scottcom"; /* for error messages */
int lineno = 1;
int fatalerr = NO;
int verbose = NO;
char *infile, *outfile;
FILE *finp;
int try_index;
byte *try_level[MAX_TRY_DEPTH+1];
char *err_sbuf;
Action** action; /* Pointer to action blocks for each verb.
* Each pointer will point to the last block
* in the chain, and the last block will
* back to the first. */
/*
* Function pointers for the output format.
*/
unsigned (*translate)(unsigned code);
void (*titlestr)(char*);
void (*begaction)(int, int);
void (*endaction)(void);
void (*begcatch)(void);
void (*endcatch)(void);
void (*action0)(unsigned);
void (*action1)(unsigned, unsigned);
void (*action2)(unsigned, unsigned, unsigned);
void (*dump)(void);
char *stripexten(char *name);
void work_filename(char *fn);
int main(int argc, char *argv[])
{
err_sbuf = malloc(512);
fprintf(stderr, "ScottCom, version "VERSION"\n");
ti99_init();
while (argc > 1 && argv[1][0] == '-')
{
switch (tolower(argv[1][1]))
{
case 'v': /* -v: Verbose */
verbose = YES;
break;
}
argc--;
argv++;
}
init();
if (argc == 1)
{
execerror("source file name must be specified.\n"
"\n"
"Syntax : {-v} source_file\n\n"
"-v\tVerbose.\n"
);
}
work_filename(argv[1]);
printf("Source File = %s\n", infile);
printf("Output File = %s\n", outfile);
if ((finp = fopen(infile, "r")) == NULL)
execerror("cannot open source file");
initprint(outfile);
yyparse();
fclose(finp);
stopprint();
if (fatalerr) {
remove(outfile);
exit(1);
}
exit(0);
}
void work_filename(char *fn)
{
char *f1, *f2;
int ff1;
ff1 = 0;
f1 = getexten(fn);
if(f1 == NULL)
{
f1 = malloc(strlen(fn) + strlen(".apl") + 2);
strcpy(f1, fn);
strcat(f1, ".apl");
ff1 = 1;
}
else
{
f1 = fn;
}
f2 = newexten(f1, ".fiad");
infile = strdup(f1);
outfile = strdup(f2);
if(ff1==1)
free(f1);
free(f2);
}
void execerror(char *s)
{
fprintf(stderr, "%s: %s\n", progname, s);
exit(1);
}
/*
* Return pointer to extension or null
*/
char *getexten(char *name)
{
char *p;
p = strchr(name, '.');
if(p!=NULL)
{
char *z;
z = p;
while(p!=NULL)
{
z = p;
p = strchr(z+1, '.');
}
return z;
}
return NULL;
}
char *newexten(char *name, char *ext) /* return pointer to name with new extension */
{
char *p;
char *xnew;
xnew = malloc(strlen(name) + strlen(ext) + 2);
strcpy(xnew, name);
p = strchr(xnew, 0x0);
p-=1;
while(p > xnew && *p!='.')
p-=1;
if(*p=='.')
{
strcpy(p, ext);
}
else
{
strcat(xnew, ext);
}
return xnew;
}
void _Assert(const char* file, unsigned line)
{
fflush(stdout);
fprintf(stderr, "\nAssertion failed: %s, line %u\n", file, line);
fflush(stderr);
abort();
}