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
|
/*****
* parser.cc
* Tom Prince 2004/01/10
*
*****/
#include <fstream>
#include <sstream>
#include <cstring>
#include <fcntl.h>
#include <algorithm>
#include "common.h"
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_LIBCURL
#include <curl/curl.h>
#endif
#include "interact.h"
#include "locate.h"
#include "errormsg.h"
#include "parser.h"
#include "util.h"
// The lexical analysis and parsing functions used by parseFile.
void setlexer(size_t (*input) (char* bif, size_t max_size), string filename);
extern int yyparse(void);
extern int yydebug;
extern int yy_flex_debug;
extern bool lexerEOF();
extern void reportEOF();
extern bool hangup;
namespace parser {
namespace yy { // Lexers
std::streambuf *sbuf = NULL;
size_t stream_input(char *buf, size_t max_size)
{
return sbuf ? sbuf->sgetn(buf,max_size) : 0;
}
} // namespace yy
void debug(bool state)
{
// For debugging the machine-generated lexer and parser.
yy_flex_debug = yydebug = state;
}
namespace {
void error(const string& filename)
{
em.sync();
em << "error: could not load module '" << filename << "'\n";
em.sync();
throw handled_error();
}
}
absyntax::file *doParse(size_t (*input) (char* bif, size_t max_size),
const string& filename, bool extendable=false)
{
setlexer(input,filename);
absyntax::file *root = yyparse() == 0 ? absyntax::root : 0;
absyntax::root = 0;
yy::sbuf = 0;
if (!root) {
if (lexerEOF()) {
if (extendable) {
return 0;
} else {
// Have the lexer report the error.
reportEOF();
}
}
em.error(nullPos);
if(!interact::interactive)
error(filename);
else
throw handled_error();
}
return root;
}
absyntax::file *parseStdin()
{
debug(false);
yy::sbuf = cin.rdbuf();
return doParse(yy::stream_input,"-");
}
bool isURL(const string& filename)
{
#ifdef HAVE_LIBCURL
return filename.find("://") != string::npos;
#else
return false;
#endif
}
absyntax::file *parseFile(const string& filename,
const char *nameOfAction)
{
#ifdef HAVE_LIBCURL
if(isURL(filename))
return parseURL(filename,nameOfAction);
#endif
if(filename == "-")
return parseStdin();
string file = settings::locateFile(filename);
if(file.empty())
error(filename);
if(nameOfAction && settings::verbose > 1)
cerr << nameOfAction << " " << filename << " from " << file << endl;
debug(false);
std::filebuf filebuf;
if(!filebuf.open(file.c_str(),std::ios::in))
error(filename);
#ifdef HAVE_SYS_STAT_H
// Check that the file is not a directory.
static struct stat buf;
if(stat(file.c_str(),&buf) == 0) {
if(S_ISDIR(buf.st_mode))
error(filename);
}
#endif
// Check that the file can actually be read.
try {
filebuf.sgetc();
} catch (...) {
error(filename);
}
yy::sbuf = &filebuf;
return doParse(yy::stream_input,file);
}
absyntax::file *parseString(const string& code,
const string& filename,
bool extendable)
{
debug(false);
stringbuf buf(code);
yy::sbuf = &buf;
return doParse(yy::stream_input,filename,extendable);
}
#ifdef HAVE_LIBCURL
size_t curlCallback(char *data, size_t size, size_t n, stringstream& buf)
{
size_t Size=size*n;
buf.write(data,Size);
return Size;
}
int curlProgress(void *, curl_off_t, curl_off_t, curl_off_t, curl_off_t)
{
return errorstream::interrupt ? -1 : 0;
}
bool readURL(stringstream& buf, const string& filename)
{
CURL *curl=curl_easy_init();
if(settings::verbose > 3)
curl_easy_setopt(curl,CURLOPT_VERBOSE,true);
#ifdef __MSDOS__
string cert=settings::getSetting<string>("sysdir")+settings::dirsep+
"ca-bundle.crt";
curl_easy_setopt(curl,CURLOPT_CAINFO,cert.c_str());
#endif
curl_easy_setopt(curl,CURLOPT_URL,filename.c_str());
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,curlCallback);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,&buf);
curl_easy_setopt(curl,CURLOPT_NOPROGRESS,0);
curl_easy_setopt(curl,CURLOPT_XFERINFOFUNCTION,curlProgress);
CURLcode res=curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res != CURLE_OK) {
cerr << curl_easy_strerror(res) << endl;
return false;
}
string s=buf.str();
return !s.empty() && s != "404: Not Found";
}
absyntax::file *parseURL(const string& filename,
const char *nameOfAction)
{
stringstream code;
if(!readURL(code,filename))
error(filename);
if(nameOfAction && settings::verbose > 1)
cerr << nameOfAction << " " << filename << endl;
debug(false);
yy::sbuf=code.rdbuf();
return doParse(yy::stream_input,filename);
}
#endif
} // namespace parser
|