-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErlangRuntime.mm
201 lines (187 loc) · 7.42 KB
/
ErlangRuntime.mm
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
//
// ErlangRuntime.mm
// ErlangBBLM
//
// Created by Paul Guyot on 29/10/2014.
//
//
#import "ErlangRuntime.h"
namespace com_semiocast_bblm_erlang {
// http://erlang.org/doc/reference_manual/data_types.html#id76876
static unichar EscapeChar(unichar c) {
unichar r;
switch (c) {
case 'b':
r = '\b';
break;
case 'd':
r = 127; // DEL
break;
case 'e':
r = '\e';
break;
case 'f':
r = '\f';
break;
case 'n':
r = '\n';
break;
case 'r':
r = '\r';
break;
case 's':
r = ' ';
break;
case 't':
r = '\t';
break;
case 'v':
r = '\v';
break;
/*
// UNIMPLEMENTED
\XYZ, \YZ, \Z character with octal representation XYZ, YZ or Z
\xXY character with hexadecimal representation XY
\x{X...} character with hexadecimal representation; X... is one or more hexadecimal characters
\^a...\^z
\^A...\^Z control A to control Z
*/
default:
// \' single quote
// \" double quote
// \\ backslash
r = c;
}
return r;
}
NSString* ParseErlangString(NSString* expression) {
NSString* result = nil;
NSUInteger exprLen = [expression length];
if (exprLen >= 2) {
if ([expression characterAtIndex:0] == '"' && [expression characterAtIndex: exprLen - 1] == '"') {
NSUInteger indexEnd = [expression length] - 1;
NSUInteger len = indexEnd - 1;
NSUInteger indexExpr;
NSUInteger indexBuffer;
unichar* strBuffer = (unichar*) malloc(sizeof(unichar) * len);
for (indexBuffer = 0, indexExpr = 1; indexExpr < indexEnd; indexExpr++, indexBuffer++) {
unichar c = [expression characterAtIndex:indexExpr];
if (c == '\\') {
indexExpr++;
len--;
c = [expression characterAtIndex:indexExpr];
strBuffer[indexBuffer] = EscapeChar(c);
} else {
strBuffer[indexBuffer] = c;
}
}
result = [NSString stringWithCharacters:strBuffer length:len];
free(strBuffer);
} else if ([expression characterAtIndex:0] == '[') {
NSScanner* scanner = [NSScanner scannerWithString:expression];
// Skip leading bracket
unichar* strBuffer = (unichar*) malloc(sizeof(unichar) * (exprLen - 2) / 2);
NSUInteger len = 0;
[scanner scanString:@"[" intoString:nil];
while (true) {
int c;
BOOL r = [scanner scanInt:&c];
if (r == NO) {
// Expect the end bracket.
r = [scanner scanString: @"]" intoString: nil];
if (r == YES) {
result = [NSString stringWithCharacters:strBuffer length:len];
}
free(strBuffer);
break;
}
strBuffer[len] = c;
len++;
[scanner scanString: @"," intoString: nil];
}
}
}
return result;
}
///
/// Convert a symbol to a man page.
/// Actually sanitize the string by only keeping [_a-zA-Z0-9-]
/// Stop if any other character is encountered, including :
/// (thus module:function will lookup for module).
NSString* SymbolToManPage(NSString* symbol) {
NSUInteger len = [symbol length];
NSUInteger indexIn;
NSUInteger indexOut = 0;
unichar* strBuffer = (unichar*) malloc(sizeof(unichar) * len);
for (indexIn = 0; indexIn < len; indexIn++) {
unichar c = [symbol characterAtIndex:indexIn];
if ((c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| (c >= '0' && c <= '9')
|| (c == '_')
|| (c == '-')) {
strBuffer[indexOut] = c;
indexOut++;
} else {
break;
}
}
NSString* result = [NSString stringWithCharacters:strBuffer length:indexOut];
free(strBuffer);
return result;
}
///
/// Try to fetch the man page for a given symbol.
///
NSString* ErlangManPage(NSString* page) {
NSPipe* stdoutPipe = [NSPipe pipe];
NSFileHandle* stdoutFile = stdoutPipe.fileHandleForReading;
NSString* erlManCommand = [NSString stringWithFormat:@"erl -man %@ > /dev/null 2>&1 || exit 1; erl -man %@ | col -bx", page, page];
NSTask *task = [NSTask new];
NSDictionary *environmentDict = [[NSProcessInfo processInfo] environment];
NSString *shellString = [environmentDict objectForKey:@"SHELL"];
task.launchPath = shellString;
task.arguments = @[@"-c", erlManCommand];
task.standardOutput = stdoutPipe;
[task launch];
NSData *data = [stdoutFile readDataToEndOfFile];
[task waitUntilExit];
[stdoutFile closeFile];
NSString* output = nil;
if ([task terminationStatus] == 0) {
output = [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
}
[task terminate];
[task release];
return output;
}
///
/// Try to eval an Erlang expression, get the result.
/// The result is serialized with io:format("~p", [Expr]).
/// Instead of passing expression on command line (using -eval argument), we get Erlang VM to parse stdin.
/// This avoids parametrized text passed to /bin/sh
///
#define ERL_EVAL_COMMAND_LINE @"erl -eval 'error_logger:tty(false), {ok, Expr, _} = io:parse_erl_exprs([]), Result = (catch begin {value, R, _} = erl_eval:exprs(Expr, []), R end), io:format(\"~p\", [Result]), init:stop().' -noshell"
NSString* EvalErlang(NSString* expression) {
NSPipe* stdoutPipe = [NSPipe pipe];
NSFileHandle* stdoutFile = stdoutPipe.fileHandleForReading;
NSPipe* stdinPipe = [NSPipe pipe];
NSFileHandle* stdinFile = stdinPipe.fileHandleForWriting;
NSTask *task = [NSTask new];
NSDictionary *environmentDict = [[NSProcessInfo processInfo] environment];
NSString *shellString = [environmentDict objectForKey:@"SHELL"];
task.launchPath = shellString;
task.arguments = @[@"-c", ERL_EVAL_COMMAND_LINE];
task.standardOutput = stdoutPipe;
task.standardInput = stdinPipe;
[task launch];
[stdinFile writeData:[expression dataUsingEncoding:NSUTF8StringEncoding]];
[stdinFile closeFile];
NSData *data = [stdoutFile readDataToEndOfFile];
[stdoutFile closeFile];
NSString* output = [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
[task terminate];
[task release];
return output;
}
}