-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface-cli.cpp
343 lines (316 loc) · 9.06 KB
/
interface-cli.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#undef printf//I compile Asar with -Dprintf=DO_NOT_USE -Dputs=DO_NOT_USE to make sure that no debug codes sneak past. They're legitime in here, though.
#undef puts
#ifdef INTERFACE_CLI
#include "asar.h"
#include "scapegoat.hpp"
#include "libstr.h"
#include "libcon.h"
#include "libsmw.h"
#include <stdio.h>
#ifdef TIMELIMIT
# if defined(linux)
# include <sys/resource.h>
# include <signal.h>
# elif defined(_WIN32)
//WARNING: The Windows equivalent of SIGXCPU, job limits, is very poorly suited for short-running
// tasks like this; it's only checked approximately every seven seconds on the machine I tested on,
// and it kills the process instantly once this happens. (Additionally, due to an implementation
// quirk, it'll bug up if you ask for anything above about seven minutes, so don't do that.)
# include <windows.h>
# else
# error Time limits not configured for this OS.
# endif
#endif
//specialized for Linux and Windows for performance - it will still work on anything that only provides libc though
#if defined(linux)
#include <sys/stat.h>
bool file_exists(const char * filename)
{
struct stat st;
return (!stat(filename, &st));
}
#elif defined(_WIN32)
#include <windows.h>
bool file_exists(const char * filename)
{
return (GetFileAttributes(filename)!=INVALID_FILE_ATTRIBUTES);
}
#else
bool file_exists(const char * filename)
{
FILE * f=fopen(filename, "rb");
if (f) fclose(f);
return f;
}
#endif
extern bool errored;
//extern int aborterrors;
extern bool checksum;
extern const char asarver[];
//extern char romtitle[30];
//extern bool stdlib;
string dir(char const *name);
string getdecor();
extern const char * thisfilename;
extern lightweight_map<string, string> defines;//these two are useless for cli, but they may be useful for other stuff
extern lightweight_map<string, unsigned int> labels;
void assemblefile(const char * filename, bool toplevel);
extern const char * thisfilename;
extern int thisline;
extern const char * thisblock;
void print(const char * str)
{
puts(str);
}
FILE * errloc=stderr;
static int errnum=0;
template<typename t> void error(int neededpass, const char * e_)
{
try
{
errored=true;
if (pass==neededpass)
{
errnum++;
fputs(S getdecor()+"error: "+e_+(thisblock?(S" ["+thisblock+"]"):"")+"\n", errloc);
if (errnum==20+1) error<errfatal>(pass, "Over 20 errors detected. Aborting.");
}
t err;
throw err;
}
catch (errnull&) {}
}
void (*shutupgcc1)(int, const char*)=error<errnull>;
void (*shutupgcc2)(int, const char*)=error<errblock>;
void (*shutupgcc3)(int, const char*)=error<errline>;
void (*shutupgcc4)(int, const char*)=error<errfatal>;
void initmathcore();
void initstuff();
void finishpass();
void reseteverything();
bool werror=false;
bool warned=false;
void warn(const char * e_)
{
fputs(S getdecor()+"warning: "+e_+"\n", errloc);
warned=true;
}
#ifdef TIMELIMIT
#if defined(linux)
void onsigxcpu(int ignored)
{
error<errnull>(pass, "Time limit exceeded.");
exit(1);
}
#elif defined(_WIN32)
//null
#endif
#endif
bool setmapper();
int main(int argc, char * argv[])
{
#ifdef TIMELIMIT
#if defined(linux)
rlimit lim;
lim.rlim_cur=TIMELIMIT;
lim.rlim_max=RLIM_INFINITY;
setrlimit(RLIMIT_CPU, &lim);
signal(SIGXCPU, onsigxcpu);
#elif defined(_WIN32)
HANDLE hjob=CreateJobObject(NULL, NULL);
AssignProcessToJobObject(hjob, GetCurrentProcess());
JOBOBJECT_BASIC_LIMIT_INFORMATION jbli;
jbli.LimitFlags=JOB_OBJECT_LIMIT_PROCESS_TIME;
jbli.PerProcessUserTimeLimit.LowPart=10*1000*1000*TIMELIMIT;
jbli.PerProcessUserTimeLimit.HighPart=0;
SetInformationJobObject(hjob, JobObjectBasicLimitInformation, &jbli, sizeof(jbli));
#endif
#endif
#define pause(sev) do { if (pause>=pause_##sev) libcon_pause(); } while(0)
enum {
pause_no,
pause_err,
pause_warn,
pause_yes,
} pause=pause_no;
try
{
initmathcore();
string version=S"Asar "+dec(asarver_maj)+"."+dec(asarver_min)+((asarver_bug>=10 || asarver_min>=10)?".":"")+
dec(asarver_bug)+(asarver_beta?"pre":"")+", by Alcaro";
char * myname=argv[0];
if (strrchr(myname, '/')) myname=strrchr(myname, '/')+1;
//char * dot=strrchr(myname, '.');
//if (dot) *dot='\0';
if (!strncasecmp(myname, "xkas", strlen("xkas"))) errloc=stdout;
//if (dot) *dot='.';
libcon_init(argc, argv,
"[options] patch [ROM]\n"
"options can be zero or more of the following:\n"
" -nocheck (disable verifying ROM title; note that it )\n"
" -pause={no, err, warn, yes}\n"
" -verbose\n"
" -v or -version\n"
" -werror\n"
);
bool ignoreerrors=false;
string par;
bool verbose=libcon_interactive;
while (par=libcon_option())
{
if (par=="-werror") werror=true;
else if (par=="-nocheck") ignoreerrors=true;
else if (par=="-verbose") verbose=true;
else if (par=="-v" || par=="-version")
{
puts(version);
return 0;
}
else if (par=="-pause" || !strncmp(par, "-pause=", strlen("-pause=")))
{
if (par=="-pause") pause=pause_yes;
else if (par=="-pause=no") pause=pause_no;
else if (par=="-pause=err") pause=pause_err;
else if (par=="-pause=warn") pause=pause_warn;
else if (par=="-pause=yes") pause=pause_yes;
else libcon_badusage();
}
else libcon_badusage();
}
if (verbose)
{
puts(version);
}
string asmname=libcon_require_filename("Enter patch name:");
string romname=libcon_optional_filename("Enter ROM name:", NULL);
//char * outname=libcon_optional_filename("Enter output ROM name:", NULL);
libcon_end();
if (!strchr(asmname, '.') && !file_exists(asmname)) asmname+=".asm";
if (!romname)
{
char * romnametmp=(char*)malloc(sizeof(char)*256);
strcpy(romnametmp, asmname);
if (strrchr(romnametmp, '.')) *strrchr(romnametmp, '.')=0;
if (file_exists(S romnametmp+".sfc")) romname=S romnametmp+".sfc";
else if (file_exists(S romnametmp+".smc")) romname=S romnametmp+".smc";
else romname=S romnametmp+".sfc";
}
else if (!strchr(romname, '.') && !file_exists(romname))
{
if (file_exists(S romname+".sfc")) romname+=".sfc";
else if (file_exists(S romname+".smc")) romname+=".smc";
}
if (!file_exists(romname))
{
FILE * f=fopen(romname, "wb");
if (!f)
{
error<errfatal>(pass, "Couldn't create ROM.");
}
fclose(f);
}
if (!openrom(romname, false))
{
thisfilename=NULL;
error<errnull>(pass, openromerror);
pause(err);
return 1;
}
//check if the ROM title and checksum looks sane
if (romlen>=32768 && !ignoreerrors)
{
bool validtitle=setmapper();
if (!validtitle)
{
string title;
for (int i=0;i<21;i++)
{
char c=romdata[snestopc(0x00FFC0)];
if (c==7) c=14;
if (c==8) c=27;//to not generate more hard-to-print characters than needed
if (c==9) c=26;//random characters are picked in accordance with the charset Windows-1252, but they should be garbage in all charsets
if (c=='\r') c=17;
if (c=='\n') c=25;
if (c=='\0') c=155;
title+=c;
}
if (libcon_interactive)
{
if (!libcon_question_bool(S"Warning: The ROM title appears to be \""+title+"\", which looks like garbage. "
"Is this your ROM title? (Note that inproperly answering \"yes\" will crash your ROM.)", false))
{
puts("Assembling aborted. snespurify should be able to fix your ROM.");
return 1;
}
}
else
{
puts(S"Error: The ROM title appears to be \""+title+"\", which looks like garbage. "
"If this is the ROM title, add -nocheck to the command line options. If the ROM title is something else, use snespurify on your ROM.");
pause(err);
return 1;
}
}
}
romdata_r=(unsigned char*)malloc(romlen);
romlen_r=romlen;
memcpy((void*)romdata_r, romdata, romlen);//recently allocated, dead
for (pass=0;pass<3;pass++)
{
//pass 1: find which bank all labels are in, for label optimizations
// freespaces are listed as above 0xFFFFFF, to find if it's in the ROM or if it's dynamic
//pass 2: find where exactly all labels are
//pass 3: assemble it all
initstuff();
assemblefile(asmname, true);
finishpass();
}
if (werror && warned) error<errnull>(pass, "One or more warnings was detected with werror on.");
if (checksum) fixchecksum();
//if (pcpos>romlen) romlen=pcpos;
if (errored)
{
puts("Errors were detected while assembling the patch. Assembling aborted. Your ROM has not been modified.");
closerom(false);
reseteverything();
pause(err);
return 1;
}
if (warned)
{
if (libcon_interactive)
{
if (!libcon_question_bool("One or more warnings were detected while assembling the patch. "
"Do you want insert the patch anyways? (Default: yes)", true))
{
puts("ROM left unchanged.");
closerom(false);
reseteverything();
return 1;
}
}
else
{
if (verbose) puts("Assembling completed, but one or more warnings were detected.");
pause(warn);
}
}
else
{
if (verbose) puts("Assembling completed without problems.");
pause(yes);
}
closerom();
reseteverything();
}
catch(errfatal&)
{
puts("A fatal error was detected while assembling the patch. Assembling aborted. Your ROM has not been modified.");
closerom(false);
reseteverything();
pause(err);
return 1;
}
return 0;
}
#endif