-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlod-util.c
457 lines (431 loc) · 10.9 KB
/
lod-util.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
/* Author: Mo McRoberts <[email protected]>
*
* Copyright (c) 2014-2016 BBC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <curl/curl.h>
#include <histedit.h>
#include "liblod.h"
#define LOD_FETCH_ALWAYS 0
#define LOD_FETCH_ABSENT 1
#define LOD_FETCH_NEVER 2
#define LOD_FETCH_MODE 0x000f
#define LOD_FETCH_FLAGS 0xf000
#define LOD_FETCH_PRIMARYTOPIC 0x1000
static LODCONTEXT *context;
static int mode, flags;
static int resolve_uri(const char *uri, int mode);
static int process_command(const char *command);
static librdf_serializer *get_serializer(void);
static int perform_query(const char *query);
static char *prompt(EditLine *el);
int
main(int argc, char **argv)
{
int index, num;
const char *buf;
char *linebuf, *p, *t;
EditLine *el;
History *hist;
HistEvent ev;
size_t len, lblen;
mode = LOD_FETCH_ABSENT;
if(curl_global_init(CURL_GLOBAL_ALL))
{
fprintf(stderr, "%s: failed to initialise libcurl\n", argv[0]);
return 1;
}
index = 1;
linebuf = NULL;
lblen = 0;
printf("%s: a test harness for liblod\n", argv[0]);
printf("Enter a URI to resolve, or .COMMAND (type '.help' for a list of commands)\n\n");
hist = history_init();
history(hist, &ev, H_SETSIZE, 100);
el = el_init(argv[0], stdin, stdout, stderr);
el_set(el, EL_EDITOR, "emacs");
el_set(el, EL_SIGNAL, 1);
el_set(el, EL_PROMPT, prompt);
el_set(el, EL_HIST, history, hist);
el_source(el, NULL);
while(1)
{
/* While there are URIs specified on the command-line, process them */
if(index < argc)
{
t = argv[index];
index++;
}
else
{
buf = el_gets(el, &num);
if(!buf || !num)
{
break;
}
while(*buf && isspace(*buf))
{
buf++;
}
if(!*buf)
{
continue;
}
len = strlen(buf);
if(len > lblen)
{
p = (char *) realloc(linebuf, len + 1);
if(!p)
{
fprintf(stderr, "%s: failed to resize line buffer: %s\n", argv[0], strerror(errno));
exit(EXIT_FAILURE);
}
linebuf = p;
lblen = len;
}
strcpy(linebuf, buf);
t = strchr(linebuf, 0);
t--;
while(t >= linebuf && isspace(*t))
{
*t = 0;
t--;
}
history(hist, &ev, H_ENTER, linebuf);
if(linebuf[0] == '.')
{
if(process_command(&(linebuf[1])))
{
break;
}
continue;
}
}
if(!context)
{
context = lod_create();
if(!context)
{
fprintf(stderr, "failed to create context: %s\n", strerror(errno));
return 1;
}
}
resolve_uri(linebuf, mode | flags);
}
if(context)
{
lod_destroy(context);
}
history_end(hist);
el_end(el);
free(linebuf);
return 0;
}
static int
resolve_uri(const char *uri, int mode)
{
LODINSTANCE *instance;
librdf_serializer *serializer;
librdf_stream *stream;
const char *s;
instance = NULL;
switch(mode & LOD_FETCH_MODE)
{
case LOD_FETCH_NEVER:
instance = lod_locate(context, uri);
break;
case LOD_FETCH_ABSENT:
instance = lod_resolve(context, uri);
break;
case LOD_FETCH_ALWAYS:
instance = lod_fetch(context, uri);
}
if(!instance)
{
if(lod_error(context))
{
fprintf(stderr, "failed to resolve <%s>: %s\n", uri, lod_errmsg(context));
return -1;
}
printf("# subject is not present in dataset\n");
}
else
{
stream = lod_instance_stream(instance);
serializer = get_serializer();
librdf_serializer_serialize_stream_to_file_handle(serializer, stdout, NULL, stream);
librdf_free_serializer(serializer);
librdf_free_stream(stream);
printf("# subject URI is <%s>\n", librdf_uri_as_string(lod_instance_uri(instance)));
}
if((s = lod_subject(context)))
{
printf("# request URI was <%s>\n", s);
}
if((s = lod_document(context)))
{
printf("# document URI was <%s>\n", s);
}
lod_instance_destroy(instance);
return 0;
}
static int
process_command(const char *command)
{
librdf_world *world;
librdf_serializer *serializer;
librdf_model *model;
librdf_node *node, *subj, *predicate;
librdf_stream *stream;
librdf_statement *query, *st;
librdf_uri *uri;
const char *doc;
char *s;
if(!strcmp(command, "quit") || !strcmp(command, "exit"))
{
return 1;
}
if(!strcmp(command, "reset"))
{
if(context)
{
lod_destroy(context);
context = NULL;
}
return 0;
}
if(!strcmp(command, "dump"))
{
if(context)
{
serializer = get_serializer();
model = lod_model(context);
librdf_serializer_serialize_model_to_file_handle(serializer, stdout, NULL, model);
librdf_free_serializer(serializer);
}
return 0;
}
if(!strcmp(command, "doc"))
{
if(!context)
{
fprintf(stderr, "cannot print document triples because no document has been fetched yet\n");
return 0;
}
doc = lod_document(context);
if(!doc)
{
fprintf(stderr, "cannot print document triples because no document has been fetched yet\n");
return 0;
}
s = strdup(doc);
resolve_uri(s, LOD_FETCH_NEVER);
free(s);
return 0;
}
if(!strcmp(command, "primary"))
{
if(!context)
{
fprintf(stderr, "cannot print primary topic triples because no document has been fetched yet\n");
return 0;
}
doc = lod_document(context);
if(!doc)
{
fprintf(stderr, "cannot print primary topic triples because no document has been fetched yet\n");
return 0;
}
world = lod_world(context);
model = lod_model(context);
subj = librdf_new_node_from_uri_string(world, (const unsigned char *) doc);
predicate = librdf_new_node_from_uri_string(world, (const unsigned char *) "http://xmlns.com/foaf/0.1/primaryTopic");
query = librdf_new_statement_from_nodes(world, subj, predicate, NULL);
stream = librdf_model_find_statements(model, query);
if(librdf_stream_end(stream))
{
fprintf(stderr, "failed to locate a foaf:primaryTopic predicate associated with document URI <%s>\n", doc);
librdf_free_statement(query);
librdf_free_stream(stream);
return 0;
}
st = librdf_stream_get_object(stream);
node = librdf_statement_get_object(st);
uri = librdf_node_get_uri(node);
resolve_uri((const char *) librdf_uri_as_string(uri), LOD_FETCH_NEVER);
librdf_free_statement(query);
librdf_free_stream(stream);
return 0;
}
if(!strcmp(command, "help"))
{
printf("Available commands:\n\n"
" .help Print this message\n"
" .quit or .exit End lod-util session\n"
" .reset Discard the current context\n"
" .dump Print all of the triples in the current context\n"
" .doc Print the triples relating to the most recently-fetched\n"
" document\n"
" .primary Print the triples relating to the primary topic of the\n"
" most recently-fetched document\n"
" .fetch [MODE] Print or set the fetch mode\n"
" .follow Toggle whether foaf:primaryTopic will be\n"
" followed if encountered\n"
"\n");
return 0;
}
if(!strcmp(command, "fetch") || !strncmp(command, "fetch ", 6))
{
command += 5;
while(isspace(*command))
{
command++;
}
if(!*command)
{
switch(mode)
{
case LOD_FETCH_NEVER:
printf("will never fetch\n");
break;
case LOD_FETCH_ALWAYS:
printf("will always fetch\n");
break;
case LOD_FETCH_ABSENT:
printf("will conditionally fetch if URI is not already a subject in the context\n");
break;
default:
break;
}
return 0;
}
if(!strcmp(command, "never"))
{
printf("fetching disabled\n");
mode = LOD_FETCH_NEVER;
return 0;
}
if(!strcmp(command, "always"))
{
printf("fetching enabled (always fetch)\n");
mode = LOD_FETCH_ALWAYS;
return 0;
}
if(!strcmp(command, "conditional") || !strcmp(command, "cond") || !strcmp(command, "absent"))
{
printf("fetching conditionally enabled (fetch if needed)\n");
mode = LOD_FETCH_ABSENT;
return 0;
}
fprintf(stderr, "unrecognised fetch mode: '%s'\n", command);
fprintf(stderr, "Usage: .mode always|never|cond[itional]\n");
return 0;
}
if(!strcmp(command, "follow"))
{
if(flags & LOD_FETCH_PRIMARYTOPIC)
{
flags &= ~LOD_FETCH_PRIMARYTOPIC;
printf("will not follow foaf:primaryTopic\n");
}
else
{
flags |= LOD_FETCH_PRIMARYTOPIC;
printf("will follow foaf:primaryTopic\n");
}
return 0;
}
if(!strcmp(command, "q") || !strncmp(command, "q ", 2))
{
command++;
while(isspace(*command))
{
command++;
}
if(!*command)
{
fprintf(stderr, "Usage: .q SPARQL-QUERY\n");
return 0;
}
return perform_query(command);
}
fprintf(stderr, "unrecognised command: .%s\n", command);
return 0;
}
static librdf_serializer *
get_serializer(void)
{
librdf_world *world;
librdf_serializer *serializer;
librdf_uri *uri;
world = lod_world(context);
serializer = librdf_new_serializer(world, "turtle", NULL, NULL);
if(!serializer)
{
fprintf(stderr, "failed to create serializer: %s\n", lod_errmsg(context));
exit(1);
}
uri = librdf_new_uri(world, (const unsigned char *) "http://www.w3.org/2000/01/rdf-schema#");
librdf_serializer_set_namespace(serializer, uri, "rdfs");
librdf_free_uri(uri);
uri = librdf_new_uri(world, (const unsigned char *) "http://www.w3.org/2001/XMLSchema#");
librdf_serializer_set_namespace(serializer, uri, "xsd");
librdf_free_uri(uri);
uri = librdf_new_uri(world, (const unsigned char *) "http://purl.org/dc/terms/");
librdf_serializer_set_namespace(serializer, uri, "dct");
librdf_free_uri(uri);
uri = librdf_new_uri(world, (const unsigned char *) "http://purl.org/dc/elements/1.1/");
librdf_serializer_set_namespace(serializer, uri, "dc");
librdf_free_uri(uri);
uri = librdf_new_uri(world, (const unsigned char *) "http://xmlns.com/foaf/0.1/");
librdf_serializer_set_namespace(serializer, uri, "foaf");
librdf_free_uri(uri);
return serializer;
}
static int
perform_query(const char *query)
{
librdf_world *world;
librdf_model *model;
librdf_query *q;
librdf_query_results *res;
world = lod_world(context);
model = lod_model(context);
q = librdf_new_query(world, "sparql", NULL, (const unsigned char *) query, NULL);
res = librdf_query_execute(q, model);
if(!res)
{
fprintf(stderr, "failed to execute query: %s\n", lod_errmsg(context));
librdf_free_query(q);
return 0;
}
librdf_query_results_to_file_handle2(res, stdout, "table", NULL, NULL, NULL);
librdf_free_query_results(res);
librdf_free_query(q);
return 0;
}
static char *
prompt(EditLine *el)
{
(void) el;
return "> ";
}