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
|
/***** Autogenerated from runhistory.in; changes will be overwritten *****/
#line 1 "runtimebase.in"
/*****
* runtimebase.in
* Andy Hammerlindl 2009/07/28
*
* Common declarations needed for all code-generating .in files.
*
*****/
#line 1 "runhistory.in"
/*****
* runhistory.in
*
* Runtime functions for history operations.
*
*****/
#line 1 "runtimebase.in"
#include "stack.h"
#include "types.h"
#include "builtin.h"
#include "entry.h"
#include "errormsg.h"
#include "array.h"
#include "triple.h"
#include "callable.h"
#include "opsymbols.h"
using vm::stack;
using vm::error;
using vm::array;
using vm::read;
using vm::callable;
using types::formal;
using types::function;
using camp::triple;
#define PRIMITIVE(name,Name,asyName) using types::prim##Name;
#include <primitives.h>
#undef PRIMITIVE
typedef double real;
void unused(void *);
namespace run {
array *copyArray(array *a);
array *copyArray2(array *a);
array *copyArray3(array *a);
double *copyTripleArray2Components(array *a, bool square=true, size_t dim2=0,
GCPlacement placement=NoGC);
}
function *realRealFunction();
#define CURRENTPEN processData().currentpen
#line 12 "runhistory.in"
#include "array.h"
#include "mathop.h"
#include "builtin.h"
using namespace camp;
using namespace settings;
using namespace vm;
using namespace run;
typedef array stringarray;
using types::stringArray;
#if defined(HAVE_LIBREADLINE) && defined(HAVE_LIBCURSES)
#include <readline/readline.h>
#include <readline/history.h>
struct historyState {
bool store;
HISTORY_STATE state;
};
typedef mem::map<CONST string, historyState> historyMap_t;
historyMap_t historyMap;
static HISTORY_STATE history_save;
// Store a deep copy of the current readline history in dest.
void store_history(HISTORY_STATE *dest)
{
HISTORY_STATE *src=history_get_history_state();
if(src) {
*dest=*src;
for(Int i=0; i < src->length; ++i)
dest->entries[i]=src->entries[i];
free(src);
}
}
stringarray* get_history(Int n)
{
int N=intcast(n);
if(N <= 0) N=history_length;
else N=Min(N,history_length);
array *a=new array((size_t) N);
int offset=history_length-N+1;
for(int i=0; i < N; ++i) {
HIST_ENTRY *last=history_get(offset+i);
string s=last ? last->line : "";
(*a)[i]=s;
}
return a;
}
string historyfilename(const string &name)
{
return historyname+"_"+name;
}
#endif
namespace run {
extern string emptystring;
#if defined(HAVE_LIBREADLINE) && defined(HAVE_LIBCURSES)
void init_readline(bool tabcompletion)
{
rl_bind_key('\t',tabcompletion ? rl_complete : rl_insert);
}
#endif
void cleanup()
{
#if defined(HAVE_LIBREADLINE) && defined(HAVE_LIBCURSES)
store_history(&history_save);
int nlines=intcast(getSetting<Int>("historylines"));
for(historyMap_t::iterator h=historyMap.begin(); h != historyMap.end();
++h) {
history_set_history_state(&h->second.state);
stifle_history(nlines);
if(h->second.store) write_history(historyfilename(h->first).c_str());
}
history_set_history_state(&history_save);
#endif
#ifdef HAVE_LIBGSL
trans::GSLrngFree();
#endif
}
}
// Autogenerated routines:
#ifndef NOSYM
#include "runhistory.symbols.h"
#endif
namespace run {
// Return the last n lines of the history named name.
#line 106 "runhistory.in"
// stringarray* history(string name, Int n=1);
void gen_runhistory0(stack *Stack)
{
Int n=vm::pop<Int>(Stack,1);
string name=vm::pop<string>(Stack);
#line 107 "runhistory.in"
#if defined(HAVE_LIBREADLINE) && defined(HAVE_LIBCURSES)
bool newhistory=historyMap.find(name) == historyMap.end();
string filename;
if(newhistory) {
filename=historyfilename(name);
std::ifstream exists(filename.c_str());
if(!exists) {Stack->push<stringarray*>(new array(0)); return;}
}
store_history(&history_save);
HISTORY_STATE& history=historyMap[name].state;
history_set_history_state(&history);
if(newhistory)
read_history(filename.c_str());
array *a=get_history(n);
store_history(&history);
history_set_history_state(&history_save);
{Stack->push<stringarray*>(a); return;}
#else
unused(&n);
{Stack->push<stringarray*>(new array(0)); return;}
#endif
}
// Return the last n lines of the interactive history.
#line 139 "runhistory.in"
// stringarray* history(Int n=0);
void gen_runhistory1(stack *Stack)
{
Int n=vm::pop<Int>(Stack,0);
#line 140 "runhistory.in"
#if defined(HAVE_LIBREADLINE) && defined(HAVE_LIBCURSES)
{Stack->push<stringarray*>(get_history(n)); return;}
#else
unused(&n);
{Stack->push<stringarray*>(new array(0)); return;}
#endif
}
// Prompt for a string using prompt, the GNU readline library, and a
// local history named name.
#line 151 "runhistory.in"
// string readline(string prompt=emptystring, string name=emptystring, bool tabcompletion=false);
void gen_runhistory2(stack *Stack)
{
bool tabcompletion=vm::pop<bool>(Stack,false);
string name=vm::pop<string>(Stack,emptystring);
string prompt=vm::pop<string>(Stack,emptystring);
#line 153 "runhistory.in"
if(!(isatty(STDIN_FILENO) || getSetting<bool>("interactive")))
{Stack->push<string>(emptystring); return;}
#if defined(HAVE_LIBREADLINE) && defined(HAVE_LIBCURSES)
init_readline(tabcompletion);
store_history(&history_save);
bool newhistory=historyMap.find(name) == historyMap.end();
historyState& h=historyMap[name];
HISTORY_STATE& history=h.state;
history_set_history_state(&history);
if(newhistory)
read_history(historyfilename(name).c_str());
static char *line=NULL;
/* Return the memory to the free pool
if the buffer has already been allocated. */
if(line) {
free(line);
line=NULL;
}
/* Get a line from the user. */
line=readline(prompt.c_str());
if(!line) cout << endl;
history_set_history_state(&history_save);
{Stack->push<string>(line ? string(line) : emptystring); return;}
#else
cout << prompt;
string s;
getline(cin,s);
unused(&tabcompletion); // Avoid unused variable warning message.
{Stack->push<string>(s); return;}
#endif
}
// Save a string in a local history named name.
// If store=true, store the local history in the file historyfilename(name).
#line 195 "runhistory.in"
// void saveline(string name, string value, bool store=true);
void gen_runhistory3(stack *Stack)
{
bool store=vm::pop<bool>(Stack,true);
string value=vm::pop<string>(Stack);
string name=vm::pop<string>(Stack);
#line 196 "runhistory.in"
#if defined(HAVE_LIBREADLINE) && defined(HAVE_LIBCURSES)
store_history(&history_save);
bool newhistory=historyMap.find(name) == historyMap.end();
historyState& h=historyMap[name];
h.store=store;
HISTORY_STATE& history=h.state;
history_set_history_state(&history);
if(newhistory)
read_history(historyfilename(name).c_str());
if(value != "") {
add_history(value.c_str());
if(store) {
std::ofstream hout(historyfilename(name).c_str(),std::ios::app);
hout << value << endl;
}
}
store_history(&history);
history_set_history_state(&history_save);
#else
unused(&store);
#endif
}
} // namespace run
namespace trans {
void gen_runhistory_venv(venv &ve)
{
#line 105 "runhistory.in"
addFunc(ve, run::gen_runhistory0, stringArray(), SYM(history), formal(primString() , SYM(name), false, false), formal(primInt(), SYM(n), true, false));
#line 138 "runhistory.in"
addFunc(ve, run::gen_runhistory1, stringArray(), SYM(history), formal(primInt(), SYM(n), true, false));
#line 149 "runhistory.in"
addFunc(ve, run::gen_runhistory2, primString() , SYM(readline), formal(primString() , SYM(prompt), true, false), formal(primString() , SYM(name), true, false), formal(primBoolean(), SYM(tabcompletion), true, false));
#line 193 "runhistory.in"
addFunc(ve, run::gen_runhistory3, primVoid(), SYM(saveline), formal(primString() , SYM(name), false, false), formal(primString() , SYM(value), false, false), formal(primBoolean(), SYM(store), true, false));
}
} // namespace trans
|