This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsage-frontend.cc
445 lines (379 loc) · 11.6 KB
/
sage-frontend.cc
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*-----------------------------------------------------------------------------
* ADOM Sage - frontend for ADOM
*
* Copyright (c) 2002 Joshua Kelley; see LICENSE for details
*
* Loader for ADOM and for the frontend
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <getopt.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string>
#include <cstring>
using namespace std;
char *optarg;
int optind, opterr, optopt;
const char *usage =
"Usage: adom-sage [OPTION]... -- [PARAMETERS TO ADOM]...\n"
" -a --adom-path=PATH Path to adom binary.\n"
" -d --debug-level=x Debug level (a number).\n"
" -h, --help Print this screen.\n"
" -l, --logfile=PATH Path to log file (only used if nonzero debug level).\n"
" -p, --passive Run passively (logs without modifying gameplay).\n"
" -s, --sage-path=PATH Path to adom-sage.so.\n"
" -v, --version Display version info.\n"
" -- End of options to Sage; further options are for ADOM";
const char *version =
"ADOM Sage version " "0.9.24" " - frontend for ADOM\n"
"ADOM is (C) Copyright 1994-2013 Thomas Biskup.\n"
"ADOM Sage is Copyright (c) 2002 Joshua Kelley.\n"
"ADOM Sage is Copyright (c) 2012-2013 Christopher Henning.\n";
struct option longopts[] =
{
{"passive", 0, NULL, 'p'},
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'v'},
{"logfile", 1, NULL, 'l'},
{"debug-level", 1, NULL, 'd'},
{"adom-path", 1, NULL, 'a'},
{"sage-path", 1, NULL, 's'},
{0, 0, 0, 0}
};
// Searches through path (a colon-separated list of directories) for filename.
// Destroys path in the process. Returns the directory containing filename.
// Requiring a dynamically-allocated, hard-to-free path parameter is sloppy,
// but we don't run for long enough to care.
char *search_path (const char *filename, char *path)
{
string full_path;
struct stat buf;
char *s;
while ((s = strchr(path, ':')) != NULL)
{
*s = '\0';
full_path = path;
full_path += "/";
full_path += filename;
if (stat(full_path.c_str(), &buf) == 0)
{
return path;
}
path = s + 1;
}
full_path = path;
full_path += "/";
full_path += filename;
if (stat(full_path.c_str(), &buf) == 0)
{
return path;
}
return NULL;
}
// Tries to locate filename in one of four locations:
// 1. Current contents of file_path (if not empty)
// 2. The base_path
// 3. One of the directories from the colon-separated list in search_dirs
// 4. Bare filename (last-resort approach)
// file_path contains the result on exit
void locate_file(const char *filename, string &file_path, string &base_path,
const char *search_dirs)
{
if (file_path.empty())
{
if (!base_path.empty())
{
file_path = base_path + filename;
}
else
{
// Search through PATH to find file if necessary
char *work_path = NULL;
char *search_copy = strdup(search_dirs);
if (search_path != NULL)
{
work_path = search_path(filename, search_copy);
}
if (work_path != NULL)
{
file_path = work_path;
file_path += "/";
file_path += filename;
}
else
{
file_path = filename;
}
free(search_copy);
}
}
}
int main (int argc, char *argv[])
{
int result, i;
string base_path, sage_path, adom_path, libc_path, curses_path;
char *env_path;
char **adom_argv;
// Process options
opterr = 1;
while (1)
{
result = getopt_long(argc, argv, "a:d:l:hps:v", longopts, NULL);
if (result == -1)
{
break;
}
switch (result)
{
case 'a': // Adom path
adom_path = optarg;
break;
case 'd': // Debug level
result = setenv("SAGE_DEBUG", optarg, 1);
break;
case 'h': // Help
puts(usage);
exit(0);
break;
case 'l': // Log path
result = setenv("SAGE_LOGFILE", optarg, 1);
break;
case 'p': // Passive option
result = setenv("SAGE_PASSIVE", "1", 1);
break;
case 's': // Sage path
sage_path = optarg;
break;
case 'v': // Version
puts(version);
exit(0);
default:
puts(usage);
exit(1);
break;
}
if (result == -1)
{
printf("adom-sage: Unable to set config variables\n");
exit(1);
}
}
// Determine base path of this program
if (strchr(argv[0], '/'))
{
base_path = dirname(argv[0]); // NOTE this may modify argv[0]
base_path += "/";
}
else
{
base_path = "";
}
env_path = getenv("PATH");
locate_file("adom", adom_path, base_path, env_path);
locate_file("adom-sage.so", sage_path, base_path, env_path);
// Determine paths to shared libraries
if (libc_path.empty() || curses_path.empty())
{
FILE *stream;
string cmdline;
char buffer[255], *library, *arrow, *libpath;
cmdline = "ldd ";
cmdline += adom_path;
stream = popen(cmdline.c_str(), "r");
if (stream != NULL)
{
while (!feof(stream))
{
if (fgets(buffer, sizeof(buffer), stream) == NULL)
{
break;
}
library = strtok(buffer, " \t");
if (library == NULL)
{
continue;
}
arrow = strtok(NULL, " \t");
if ((arrow == NULL) || (strcmp(arrow, "=>") != 0))
{
continue;
}
libpath = strtok(NULL, " \t");
if (libpath == NULL)
{
continue;
}
if (strcmp(library, "libncurses.so.5") == 0)
{
curses_path = libpath;
}
if (strcmp(library, "libc.so.6") == 0)
{
libc_path = libpath;
}
}
pclose(stream);
}
}
if (curses_path.empty() || libc_path.empty())
{
printf("ADOM Sage: Can't get library info from ADOM, using compile-time defaults.\n");
unsetenv("CURSES_PATH");
unsetenv("LIBC_PATH");
}
else
{
result = setenv("CURSES_PATH", curses_path.c_str(), 1);
if (result != -1)
{
result = setenv("LIBC_PATH", libc_path.c_str(), 1);
}
if (result == -1)
{
printf("adom-sage: Unable to initialize library information\n");
exit(1);
}
}
// Prepare to load library
char *preload = getenv("LD_PRELOAD");
if (preload == NULL)
{
result = setenv("LD_PRELOAD", sage_path.c_str(), 1);
}
else
{
string new_preload(preload);
new_preload += ' ';
new_preload += sage_path;
result = setenv("LD_PRELOAD", new_preload.c_str(), 1);
}
if (result == -1)
{
printf("adom-sage: Unable to load ADOM Sage\n");
exit(1);
}
// Try to get info on ADOM executable
struct stat buf;
result = stat(adom_path.c_str(), &buf);
if (result != -1) // Ignore errors (perhaps unwisely)
{
// Check if ADOM is setuid; warn if it is
if (buf.st_mode & (S_ISUID | S_ISGID))
{
printf("WARNING: ADOM is installed setuid or setgid, so ADOM Sage\n");
printf("probably won't work.\n\n");
printf("[PRESS ENTER TO CONTINUE]");
getchar();
}
// Try to determine ADOM's version while we're at it
const char *version;
switch (buf.st_size)
{
case 2388548:
version = "100";
break;
case 2556118:
version = "110";
break;
case 2452608:
version = "111";
break;
case 2240572: // 32-bit Debian
version = "1203";
break;
case 2246972: // 32-bit Debian
version = "1204";
break;
case 2250044: // 32-bit Debian
version = "1205";
break;
case 2267132: // 32-bit Debian
version = "1206";
break;
case 2301084: // 32-bit Debian
version = "1207";
break;
case 2315196: // 32-bit Debian
version = "1208";
break;
case 2383036: // 32-bit Debian
version = "1209";
break;
case 2379196: // 32-bit Debian
version = "12010";
break;
case 2378908: // 32-bit Debian
version = "12011";
break;
case 2444220: // 32-bit Debian
version = "12012";
break;
case 2444316: // 32-bit Debian
version = "12013";
break;
case 2457248: // 32-bit Debian non-noteye
version = "12014";
break;
case 2471612: // 32-bit Debian non-noteye
version = "12016";
break;
case 2490700: // 32-bit Debian non-noteye
version = "12017";
break;
case 2492396: // 32-bit Debian non-noteye
version = "12018";
break;
case 2495376: // 32-bit Debian non-noteye
version = "12019";
break;
case 2495248: // 32-bit Debian non-noteye
version = "12020";
break;
case 2648272: // 32-bit Debian non-noteye
version = "12021";
break;
case 2656496: // 32-bit Debian non-noteye
version = "12022";
break;
case 2660656: // 32-bit Debian non-noteye
version = "12023";
break;
case 2772272: // 32-bit Debian non-noteye
version = "12048";
break;
case 2773744: // 32-bit Debian non-noteye
version = "12049";
break;
// case 2774352: // 32-bit Debian non-noteye
// version = "12050";
// break;
case 2846748: // 32-bit Debian non-noteye
version = "12051";
break;
case 2850716: // 32-bit Debian non-noteye
version = "12055";
break;
case 2898792: // 32-bit Debian non-noteye
version = "12059";
break;
default:
version = "0";
break;
}
setenv("ADOM_VERSION", version, 1);
}
// Run ADOM
adom_argv = new char*[argc - optind + 2];
adom_argv[0] = strdup(adom_path.c_str());
for (i = 0; i <= argc - optind; i++)
{
adom_argv[1+i] = argv[optind+i];
}
adom_argv[i] = NULL;
execvp(adom_path.c_str(), adom_argv);
perror("adom-sage: Unable to run ADOM");
exit(1);
}