This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpl.c
345 lines (296 loc) · 7.49 KB
/
tpl.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
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
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <errno.h>
#include <locale.h>
#include <unistd.h>
#include "history.h"
#include "trealla.h"
#ifdef _WIN32
#include <windows.h>
#include <io.h>
#define snprintf _snprintf
#define isatty _isatty
#define msleep Sleep
#else
#include <sys/stat.h>
#include <sys/types.h>
#ifndef __wasi__
#include <sys/wait.h>
#endif
#define msleep(ms) \
{ \
struct timespec tv; \
tv.tv_sec = (ms) / 1000; \
tv.tv_nsec = ((ms) % 1000) * 1000 * 1000; \
nanosleep(&tv, &tv); \
}
#endif
void sigfn(int s)
{
signal(SIGINT, &sigfn);
g_tpl_interrupt = s;
}
#ifndef __wasi__
static int daemonize(int argc, char *argv[])
{
char path[1024];
path[0] = '\0';
int watchdog = 0;
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-w") || !strcmp(argv[i], "--watchdog"))
watchdog = 1;
else if (!strncmp(argv[i], "--cd=", 5))
strcpy(path, argv[i] + 5);
}
#ifdef _WIN32
char cmd[1024], args[1024 * 8];
args[0] = 0;
strcpy(cmd, argv[0]);
strcat(cmd, ".exe");
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--daemon"))
continue;
if (!args[0])
strcat(args, " ");
strcat(args, "\"");
strcat(args, argv[i]);
strcat(args, "\"");
}
STARTUPINFO startInfo = {0};
PROCESS_INFORMATION process = {0};
startInfo.cb = sizeof(startInfo);
startInfo.dwFlags = STARTF_USESHOWWINDOW;
startInfo.wShowWindow = SW_HIDE;
if (!CreateProcessA(
(LPSTR)cmd, // application name
(LPSTR)args, // command line arguments
NULL, // process attributes
NULL, // thread attributes
FALSE, // inherit (file) handles
DETACHED_PROCESS, // Detach
NULL, // environment
(path[0] ? path : NULL), // current directory
&startInfo, // startup info
&process) // process information
) {
fprintf(stderr, "Error: creation of the process failed\n");
return 1;
}
return 0;
#else
pid_t pid;
if ((pid = fork()) < 0) // Error
return -1;
else if (pid != 0) // Parent
return 0;
if (watchdog)
signal(SIGCHLD, SIG_IGN);
while (watchdog) {
pid_t pid;
if ((pid = fork()) < 0) // Error
return -1;
else if (pid != 0) { // Parent
if (watchdog) {
int status;
wait(&status);
msleep(1000);
}
else
return 0;
}
else // Child
break;
}
if (path[0])
if (chdir(path) < 0)
fprintf(stderr, "Error: can't chdir(%s)\n", path);
setsid();
umask(0);
close(2);
close(1);
close(0);
return 1;
#endif
}
#endif
int main(int ac, char *av[])
{
setlocale(LC_ALL, "");
setlocale(LC_NUMERIC, "C");
const char *homedir;
g_argv0 = av[0];
if ((homedir = getenv("HOME")) == NULL)
homedir = ".";
char histfile[1024];
snprintf(histfile, sizeof(histfile), "%s/%s", homedir, ".tpl_history");
convert_path(histfile);
//bool did_load = false;
int i, do_goal = 0, do_lib = 0;
int version = 0, daemon = 0;
bool ns = false, no_res = false;
void *pl = pl_create();
if (!pl) {
fprintf(stderr, "Failed to create the prolog system: %s\n", strerror(errno));
return 1;
}
set_opt(pl, 1);
for (i = 1; i < ac; i++) {
if (!strcmp(av[i], "--")) {
g_ac = ac;
g_avc = ++i;
g_av = av;
break;
}
if (!strcmp(av[i], "-h") || !strcmp(av[i], "--help")) {
version = 2;
} else if (!strcmp(av[i], "-v") || !strcmp(av[i], "--version")) {
version = 1;
} else if (!strcmp(av[i], "-q") || !strcmp(av[i], "--quiet")) {
set_quiet(pl);
} else if (!strcmp(av[i], "-O0") || !strcmp(av[i], "--noopt"))
set_opt(pl, 0);
else if (!strcmp(av[i], "-t") || !strcmp(av[i], "--trace"))
set_trace(pl);
else if (!strcmp(av[i], "--ns"))
ns = true;
else if (!strcmp(av[i], "-d") || !strcmp(av[i], "--daemon"))
daemon = 1;
}
#ifndef __wasi__
if (daemon) {
if (!daemonize(ac, av)) {
pl_destroy(pl);
return 0;
}
} else {
signal(SIGINT, &sigfn);
#ifndef _WIN32
signal(SIGALRM, &sigfn);
#endif
}
#endif
#if !defined(_WIN32) && !defined(__wasi__)
signal(SIGPIPE, SIG_IGN);
#endif
const char *goal = NULL;
for (i = 1; i < ac; i++) {
if (!strcmp(av[i], "--"))
break;
#if 0
if ((av[i][0] == '-') && did_load) {
fprintf(stderr, "Error: options entered after files\n");
pl_destroy(pl);
return 0;
}
#endif
if (!strcmp(av[i], "--consult")) {
#ifdef _WIN32
if (!pl_consult_fp(pl, stdin, ".\\")) {
#else
if (!pl_consult_fp(pl, stdin, "./")) {
#endif
pl_destroy(pl);
return 1;
}
} else if (!strcmp(av[i], "--library")) {
do_goal = 0;
do_lib = 1;
} else if (!strcmp(av[i], "-f") || !strcmp(av[i], "-l") || !strcmp(av[i], "--file") || !strcmp(av[i], "--consult-file")) {
if (!strcmp(av[i], "-f"))
no_res = true;
do_lib = do_goal = 0;
} else if (!strcmp(av[i], "-g") || !strcmp(av[i], "-e") || !strcmp(av[i], "--goal") || !strcmp(av[i], "--query-goal")) {
do_lib = 0;
do_goal = 1;
} else if (av[i][0] == '-') {
continue;
} else if (do_lib) {
g_tpl_lib = strdup(av[i]);
convert_path(g_tpl_lib);
do_lib = 0;
} else if (do_goal) {
do_goal = 0;
goal = av[i];
} else {
if (!pl_consult(pl, av[i])) {
fprintf(stderr, "Error: error(existence_error(source_sink,'%s'),consult/1)\n", av[i]);
pl_destroy(pl);
return 1;
}
}
}
if (!no_res && !version)
#ifdef _WIN32
pl_consult(pl, "~\\.tplrc");
#else
pl_consult(pl, "~/.tplrc");
#endif
if (goal) {
if (!pl_eval(pl, goal)) {
int halt_code = get_halt_code(pl);
pl_destroy(pl);
return halt_code;
}
}
if (get_halt(pl) || ns) {
int halt_code = get_halt_code(pl);
pl_destroy(pl);
return halt_code;
}
if (version)
printf("Trealla Prolog (c) Infradig 2020-2022, %s\n", g_version);
if (version == 2) {
fprintf(stdout, "Usage:\n");
fprintf(stdout, " tpl [options] [files] [-- args]\n");
fprintf(stdout, "Options:\n");
fprintf(stdout, " -f file\t\t- load file (~/.tplrc not loaded)\n");
fprintf(stdout, " -l file\t\t- load file (~/.tplrc) loaded\n");
fprintf(stdout, " -g goal\t\t- query goal (only used once)\n");
fprintf(stdout, " --library path\t- alt to TPL_LIBRARY_PATH env variable\n");
fprintf(stdout, " -v, --version\t\t- print version info and exit\n");
fprintf(stdout, " -h, --help\t\t- print help info and exit\n");
fprintf(stdout, " -O0, --noopt\t\t- turn off optimization\n");
fprintf(stdout, " -q, --quiet\t\t- quiet mode\n");
fprintf(stdout, " -t, --trace\t\t- trace mode\n");
fprintf(stdout, " -d, --daemon\t\t- daemonize\n");
fprintf(stdout, " -w, --watchdog\t- create watchdog\n");
fprintf(stdout, " --consult\t\t- consult from STDIN\n");
}
if (version) {
pl_destroy(pl);
return 0;
}
if (isatty(0))
history_load(histfile);
char *line;
while ((line = history_readline_eol("?- ", '.')) != NULL) {
const char *src = line;
while (isspace(*src))
src++;
if (!*src || (*src == '\n')) {
free(line);
continue;
}
g_tpl_interrupt = 0;
pl_eval(pl, src);
free(line);
if (get_halt(pl))
break;
if (!did_dump_vars(pl)) {
if (get_redo(pl))
printf(" ");
else
printf(" ");
printf("%s.\n", get_status(pl) ? "true" : "false");
}
}
if (isatty(0))
history_save();
int halt_code = get_halt_code(pl);
pl_destroy(pl);
return halt_code;
}