-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun.c
More file actions
187 lines (152 loc) · 3.53 KB
/
run.c
File metadata and controls
187 lines (152 loc) · 3.53 KB
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
#include <stdio.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "as31.h"
/* global variables */
unsigned long lc;
char *asmfile;
int fatal=0, abort_asap=0;
int pass=0;
int dashl=0;
FILE *listing=NULL, *fin=NULL;
int run_as31(const char *infile, int lst, int use_stdout,const char *fmt, const char *arg)
{
char *outfile=NULL, *lstfile=NULL;
const char *extension;
int has_dot_asm=0;
int len, baselen, extlen;
/* first, figure out all the file names */
dashl = lst;
extension = emit_extension(fmt);
extlen = strlen(extension);
len = baselen = strlen(infile);
if (len >= 4 && strcasecmp(infile + len - 4, ".asm") == 0) {
has_dot_asm = 1;
baselen -= 4;
}
asmfile = (char *)malloc(baselen + 5);
strcpy(asmfile, infile);
if (dashl) {
lstfile = (char *)malloc(baselen + 5);
strncpy(lstfile, infile, baselen);
strcpy(lstfile + baselen, ".lst");
}
if (use_stdout) {
outfile = NULL;
} else {
outfile = (char *)malloc(baselen + extlen + 2);
strncpy(outfile, infile, baselen);
*(outfile + baselen) = '.';
strcpy(outfile + baselen + 1, extension);
}
/* now open the files */
fin = freopen(asmfile, "r", stdin);
if (fin == NULL) {
if (!has_dot_asm) {
strcpy(asmfile + baselen, ".asm");
fin = freopen(asmfile, "r", stdin);
}
if (fin == NULL) {
mesg_f("Cannot open input file: %s\n", asmfile);
free(asmfile);
if (outfile) free(outfile);
if (lstfile) free(lstfile);
return -1;
}
}
if (dashl) {
listing = fopen(lstfile,"w");
if( listing == NULL ) {
mesg_f("Cannot open file: %s for writing.\n",
lstfile);
fclose(fin);
free(asmfile);
if (outfile) free(outfile);
if (lstfile) free(lstfile);
return -1;
}
}
/* what happens if this doesn't work */
emitopen(outfile, fmt, arg);
syminit();
clear_location_counter();
fatal = abort_asap = 0;
lineno = 1;
pass=0;
lc = 0;
/*
** P A S S 1
*/
if (!use_stdout) mesg_f("Begin Pass #1\n");
yyparse();
if (fatal) {
mesg_f("Errors in pass1, assembly aborted\n");
} else {
mesg_f("Finished ");
rewind(fin);
lineno = 1;
pass++;
lc = 0;
emitaddr(lc);
/*
** P A S S 2
*/
if (!use_stdout) mesg_f("Begin Pass #2\n");
yyparse();
}
if (fatal) {
mesg_f("Errors in pass2, assembly aborted\n");
}
else{
mesg_f("Finished ");
}
emitclose();
fclose(fin);
if (dashl) fclose(listing);
free(asmfile);
if (outfile) free(outfile);
if (lstfile) free(lstfile);
freesym();
if (fatal) return -1;
return 0;
}
/* the parser, lexer and other stuff that actually do the */
/* assembly will call to these two functions to report any */
/* errors or warning. error() calls exit() in the command */
/* line version, but the abort_asap flag was added, and the */
/* parser check it at the end of every line */
void error(const char *fmt, ...)
{
va_list args;
char buf[2048];
int len;
abort_asap++;
fatal++;
va_start(args, fmt);
len = snprintf(buf, sizeof(buf), "Error, line %d, ", lineno);
len += vsnprintf(buf + len, sizeof(buf) - len, fmt, args);
snprintf(buf + len, sizeof(buf) - len, ".\n");
mesg(buf);
}
void warn(const char *fmt, ...)
{
va_list args;
char buf[2048];
int len;
fatal++;
va_start(args, fmt);
len = snprintf(buf, sizeof(buf), "Warning, line %d, ", lineno);
len += vsnprintf(buf + len, sizeof(buf) - len, fmt, args);
snprintf(buf + len, sizeof(buf) - len, ".\n");
mesg(buf);
}
void mesg_f(const char *fmt, ...)
{
va_list args;
char buf[2048];
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
mesg(buf);
}