-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamlsh.c
492 lines (447 loc) · 10.8 KB
/
camlsh.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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* Camlsh - a camllight wrapper to improve shell-mode usage.
* Copyright (C) 2012 Romain Labolle <[email protected]>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*
*
* Compile : gcc -o camlsh camlsh.c -lreadline
* Usage : camlsh [options] [--] [camlrun options]
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <getopt.h>
#include <wordexp.h>
#include <regex.h>
#include <sys/types.h>
#include <readline/readline.h>
#include <readline/history.h>
static struct option long_options[] = {
{"color", required_argument, NULL, 'c'},
{"noargs", no_argument, NULL, 'n'},
{"camlrun", required_argument, NULL, 'r'},
{"camltop", required_argument, NULL, 't'},
{"stdlib", required_argument, NULL, 'l'},
{"open", required_argument, NULL, 'o'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
typedef struct list{
char *hd;
struct list *tl;
} list;
void error(char *s)
{
perror(s);
exit(1);
}
void* xmalloc(size_t num)
{
void *new = calloc(1, num);
if (!new)
exit(1);
return new;
}
void xfree(void *p)
{
if(p)
free(p);
p=NULL;
}
char* xstrdup(const char *str)
{
if (str)
return strcpy(xmalloc(strlen(str) + 1), str);
return NULL;
}
char* xstrndup(const char *str, size_t n)
{
if (str)
return strncpy(xmalloc(n+1), str, n);
return NULL;
}
void usage(char **argv)
{
printf("usage: %s [options] [--] [camlrun options]\n", argv[0]);
printf("Options:\n"
"\t-c|--color n\t\tSet color code for error messages.0 to disable.\n"
"\t\t\t\tSee VT100 colors attributes for more informations. Default: 31 (red).\n"
"\t-o|--open file.ml\tRun file.ml and give back console access to the use.\n"
"\t\t\t\tUsefull for testing purpose.\n"
"\t-t|--camltop <path>\tSet camltop path. This should be a custom camltop,\n"
"\t\t\t\tcompiled with toplevel_input_prompt = \"\\014\" and error_prompt = \"\\021>\".\n"
"\t\t\t\tDefault to ./camltop, ~/.camlsh/camltop or */lib/caml-light/caml{sh,}top\n"
"\t-l|--stdlib <path>\tSet Caml stdlib path for camltop (can be standard one).\n"
"\t\t\t\tDefault to /usr/lib/caml-light/ or /usr/local/lib/caml-light/\n"
"\t-r|--camlrun <path>\tSet path to camlrun. Default: camlrun (from PATH).\n"
"\t-n|--noargs\t\tDon't pass default arguments to camlrun, only [camlrun options] are passed.\n"
"\t-h|--help\t\tDisplay this message.\n"
);
exit(0);
}
char *expand_first(char *filename)
{
char *result;
wordexp_t exp_result;
if(0!=(wordexp(filename, &exp_result, 0)))
return NULL;
wordexp(filename, &exp_result, 0);
result = xstrdup(exp_result.we_wordv[0]);
wordfree(&exp_result);
return result;
}
list *expand(char *filename)
{
wordexp_t exp_result;
list *files=NULL, *tmp=NULL;
int i;
if(0!=(wordexp(filename, &exp_result, 0)))
return NULL;
for (i=exp_result.we_wordc-1; i >= 0 ; i--)
{
tmp = xmalloc(sizeof(list));
tmp->hd = xstrdup(exp_result.we_wordv[i]);
tmp->tl = files;
files = tmp;
}
wordfree(&exp_result);
return files;
}
char *find_camlstdlib(void)
{
if(access("/usr/lib/caml-light/", R_OK) == 0)
return "/usr/lib/caml-light/";
if(access("/usr/local/lib/caml-light/", R_OK) == 0)
return "/usr/local/lib/caml-light/";
error("Cannot find caml stdlibs");
}
char *find_camltop(void)
{
char *filename;
if(access("./camltop", R_OK) == 0)
return "./camltop";
filename = expand_first("~/.camlsh/camltop");
if(access(filename, R_OK) == 0)
return filename;
xfree(filename);
if(access("/usr/lib/caml-light/camlshtop", R_OK) == 0)
return "/usr/lib/caml-light/camlshtop";
if(access("/usr/local/lib/caml-light/camlshtop", R_OK) == 0)
return "/usr/local/lib/caml-light/camlshtop";
if(access("/usr/lib/caml-light/camltop", R_OK) == 0)
return "/usr/lib/caml-light/camltop";
if(access("/usr/local/lib/caml-light/camltop", R_OK) == 0)
return "/usr/local/lib/caml-light/camltop";
error("Cannot find camltop");
}
void camlbackend(int in[2], int out[2], int argc, char *argv[])
{
int c, i, option_index=0, noargs=0;
char *camlrun = NULL;
char *camltop = NULL;
char *stdlib = NULL;
char **camlargv = NULL;
/* Close stdin, stdout, stderr */
close(0);
close(1);
close(2);
/* make our pipes, our new stdin,stdout and stderr */
dup2(in[0],0);
dup2(out[1],1);
dup2(out[1],2);
/* Close unused ends of the pipes */
close(in[1]);
close(out[0]);
/* Parse command line arguments */
opterr=0;
while ((c = getopt_long(argc, argv, "c:nr:t:l:o:h", long_options, &option_index)) != -1)
{
switch (c)
{
case 'c':
case 'o':
break;
case 'n':
noargs = 1;
break;
case 'r':
camlrun = optarg;
break;
case 't':
camltop = optarg;
break;
case 'l':
stdlib = optarg;
break;
case '?':
case 'h':
exit(0);
break; /* Yes, I know... */
default:
printf ("?? getopt returned character code 0%o ??\n", c);
}
}
camlargv = xmalloc(sizeof(char*)*( 1 + (noargs?0:3) + (argc - optind) + 1)); /* "camlrun" + ("/path/camltop" + "-stdlib" + "/path/stdlib/") + extras + NULL */
if(camlrun == NULL)
camlrun = "camlrun";
camlargv[0] = camlrun;
i=1;
if(!noargs)
{
if(camltop)
camlargv[1] = camltop;
else
camlargv[1] = find_camltop();
camlargv[2] = "-stdlib";
if(stdlib)
camlargv[3] = stdlib;
else
camlargv[3] = find_camlstdlib();
i=4;
}
while (optind < argc)
{
camlargv[i++] = argv[optind++];
}
camlargv[i] = (char*)NULL;
/* Over-write the child process with camllight */
execvp(camlrun, camlargv);
error("Could not exec camllight");
}
void camlfrontend(int in[2], int out[2], int argc, char *argv[])
{
int o=0, i=0, n=0, option_index=0, com=0, sqt=0, dqt=0, inlen=0, color=31;
char c=0;
char *input=NULL;
char *str=NULL;
char *histfile=NULL;
char prompt[3] = "# ";
char *set_color=NULL, *reset_color=NULL;
FILE *output=NULL;
list *includes=NULL, *tmp=NULL;
FILE *inc=NULL;
regex_t regex;
size_t nmatch = 2;
regmatch_t pmatch[2];
/* Close unused ends of the pipes */
close(in[0]);
close(out[1]);
/* Open caml output for reading */
output = fdopen(out[0], "rb");
if(output == NULL)
error("Could not open pipe");
opterr=0;
while ((o = getopt_long(argc, argv, "c:nr:t:l:o:h", long_options, &option_index)) != -1)
{
switch (o)
{
case 'c':
color = strtol(optarg, &optarg+strlen(optarg), 10);
break;
case 'o':
if(includes == NULL)
includes = expand(optarg);
else
{
tmp = includes;
while(tmp->tl != NULL)
tmp = tmp->tl;
tmp->tl = expand(optarg);
}
break;
case 'n':
case 'r':
case 't':
case 'l':
break;
case 'h':
usage(argv);
return;
case '?':
if (optopt == 'c' || optopt == 'r' || optopt == 't' || optopt == 'l' || optopt == 'o')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
usage(argv);
return;
default:
printf ("?? getopt returned character code 0%o ??\n", o);
}
}
if ( (1 <= color && color <=8) || (30 <= color && color <=37) || (40 <= color && color <=47))
{
set_color = xmalloc(sizeof(char)*6);
snprintf(set_color, 6, "\033[%dm", color);
reset_color = "\033[m";
}
else
set_color = reset_color = "";
while((c = getc(output))!=(char)14)
{
if(c == EOF)
return;
putchar(c);
}
if(0 != (regcomp(®ex, "^(\\*include \\(.*\\)\\*)$", 0)))
error("regcomp() failed");
histfile = expand_first("~/.camlsh/history");
read_history(histfile);
rl_bind_key ('\t', rl_insert);
for(;;)
{
if(includes != NULL && inc == NULL)
{
inc=fopen(includes->hd, "rb");
if(inc)
rl_instream = inc;
else
{
if(includes->hd != NULL)
xfree(includes->hd);
tmp = includes;
includes = includes->tl;
xfree(tmp);
if(includes == NULL)
{
rl_instream = stdin;
continue;
}
}
}
printf(reset_color);
input = readline(prompt);
if (!input)
{
if(includes != NULL && inc)
{
rl_instream = stdin;
fclose(inc);
inc = NULL;
if(includes->hd != NULL)
xfree(includes->hd);
tmp = includes;
includes = includes->tl;
xfree(tmp);
putchar('\n');
continue;
}
else
break;
}
inlen = strlen(input);
if(inlen == 0)
{
xfree(input);
continue;
}
if(regexec(®ex, input, nmatch, pmatch, 0)==0)
{
str = xstrndup(&input[pmatch[1].rm_so], pmatch[1].rm_eo-pmatch[1].rm_so);
if(includes == NULL)
includes = expand(str);
else
{
tmp = includes;
while(tmp->tl != NULL)
tmp = tmp->tl;
tmp->tl = expand(str);
}
xfree(str);
}
add_history(input);
write(in[1], input, inlen);
write(in[1], "\n", 1);
prompt[0]='\0';
n=0;
for(i=0;i<inlen-1;i++)
{
if(!sqt && !com && input[i] == '"' && (i==0 || input[i-1]!='\\'))
{
dqt ^= 1;
continue;
}
if(!dqt && !com && input[i] == '\'' && (i==0 || input[i-1]!='\\'))
{
sqt ^= 1;
continue;
}
if(!dqt && !sqt && input[i] == '(' && input[i+1] == '*')
{
com++;
i++;
continue;
}
if(!dqt && !sqt && input[i] == '*' && input[i+1] == ')' && com)
{
com--;
i++;
continue;
}
if(!dqt && !sqt && !com && input[i] == ';' && input[i+1] == ';')
{
n++;
i++;
if(i==inlen-1)
prompt[0]='#';
}
}
if(!sqt && !com && input[i] == '"' && (i==0 || input[i-1]!='\\'))
dqt ^= 1;
if(!dqt && !com && input[i] == '\'' && (i==0 || input[i-1]!='\\'))
sqt ^= 1;
while(n>0)
{
c = getc(output);
if(c == EOF)
return;
else if(c == (char)14)
n--;
else if(c == (char)21)
printf(set_color);
else
putchar(c);
}
xfree(input);
}
putchar('\n');
mkdir(expand_first("~/.camlsh"));
write_history(histfile);
xfree(histfile);
regfree(®ex);
close(in[1]);
close(out[0]);
return;
}
int main(int argc, char *argv[])
{
int in[2], out[2], pid, status;
/* In a pipe, xx[0] is for reading, xx[1] is for writing */
if (pipe(in) < 0) error("pipe in");
if (pipe(out) < 0) error("pipe out");
pid=fork();
if (pid == -1)
error("Could not fork");
else if (pid == 0)
camlbackend(in, out, argc, argv);
else
camlfrontend(in, out, argc, argv);
if(waitpid(pid, &status, 0)==-1)
error("waitpid()");
exit(status>>8);
}