static void raptor_write_string_iostream_finish(void *user_data) { struct raptor_write_string_iostream_context* con; size_t len; void *str = NULL; con = (struct raptor_write_string_iostream_context*)user_data; len = raptor_stringbuffer_length(con->sb); *con->string_p = NULL; if(con->length_p) *con->length_p = len; str = (void*)con->malloc_handler(len+1); if(str) { if(len) raptor_stringbuffer_copy_to_string(con->sb, (unsigned char*)str, len+1); else *(char*)str='\0'; *con->string_p = str; } if(!str && con->length_p) *con->length_p = 0; raptor_free_stringbuffer(con->sb); RAPTOR_FREE(raptor_write_string_iostream_context, con); return; }
/* * rasqal_new_iostream_from_stringbuffer: * @world: raptor world * @sb: stringbuffer * * INTERNAL - create a new iostream reading from a stringbuffer. * * The stringbuffer @sb becomes owned by the iostream * * This is intended to be replaced by * raptor_new_iostream_from_stringbuffer() in a newer raptor release. * * Return value: new #raptor_iostream object or NULL on failure **/ raptor_iostream* rasqal_new_iostream_from_stringbuffer(raptor_world *raptor_world_ptr, raptor_stringbuffer* sb) { struct rasqal_read_stringbuffer_iostream_context* con; const raptor_iostream_handler* handler; if(!sb) return NULL; handler = &rasqal_iostream_read_stringbuffer_handler; con = RASQAL_CALLOC(struct rasqal_read_stringbuffer_iostream_context*, 1, sizeof(*con)); if(!con) { raptor_free_stringbuffer(sb); return NULL; } con->sb = sb; con->string = raptor_stringbuffer_as_string(sb); con->length = raptor_stringbuffer_length(sb); return raptor_new_iostream_from_handler(raptor_world_ptr, con, handler); }
/** * raptor_free_xml_element: * @element: XML Element * * Destructor - destroy a raptor_xml_element object. **/ void raptor_free_xml_element(raptor_xml_element *element) { unsigned int i; if(!element) return; for(i = 0; i < element->attribute_count; i++) if(element->attributes[i]) raptor_free_qname(element->attributes[i]); if(element->attributes) RAPTOR_FREE(raptor_qname_array, element->attributes); if(element->content_cdata_sb) raptor_free_stringbuffer(element->content_cdata_sb); if(element->base_uri) raptor_free_uri(element->base_uri); if(element->xml_language) RAPTOR_FREE(char*, element->xml_language); raptor_free_qname(element->name); if(element->declared_nspaces) raptor_free_sequence(element->declared_nspaces); RAPTOR_FREE(raptor_element, element); }
static void rasqal_read_stringbuffer_iostream_finish(void *user_data) { struct rasqal_read_stringbuffer_iostream_context* con; con = (struct rasqal_read_stringbuffer_iostream_context*)user_data; if(con->sb) raptor_free_stringbuffer(con->sb); RASQAL_FREE(rasqal_read_stringbuffer_iostream_context, con); return; }
static void rasqal_builtin_agg_expression_execute_finish(void* user_data) { rasqal_builtin_agg_expression_execute* b; b = (rasqal_builtin_agg_expression_execute*)user_data; if(b->l) rasqal_free_literal(b->l); if(b->sb) raptor_free_stringbuffer(b->sb); RASQAL_FREE(rasqal_builtin_agg_expression_execute, b); }
/** * raptor_www_fetch_to_string: * @www: raptor_www object * @uri: raptor_uri to retrieve * @string_p: pointer to location to hold string * @length_p: pointer to location to hold length of string (or NULL) * @malloc_handler: pointer to malloc() to use to make string (or NULL) * * Start a WWW content retrieval for the given URI, returning the data in a new string. * * If @malloc_handler is null, raptor will allocate it using it's * own memory allocator. *string_p is set to NULL on failure (and * *length_p to 0 if length_p is not NULL). * * Return value: non-0 on failure **/ RAPTOR_EXTERN_C int raptor_www_fetch_to_string(raptor_www *www, raptor_uri *uri, void **string_p, size_t *length_p, raptor_data_malloc_handler const malloc_handler) { raptor_stringbuffer *sb = NULL; void *str = NULL; raptor_www_write_bytes_handler saved_write_bytes; void *saved_write_bytes_userdata; sb = raptor_new_stringbuffer(); if(!sb) return 1; if(length_p) *length_p=0; saved_write_bytes = www->write_bytes; saved_write_bytes_userdata = www->write_bytes_userdata; raptor_www_set_write_bytes_handler(www, raptor_www_fetch_to_string_write_bytes, sb); if(raptor_www_fetch(www, uri)) str = NULL; else { size_t len = raptor_stringbuffer_length(sb); if(len) { str = (void*)malloc_handler(len+1); if(str) { raptor_stringbuffer_copy_to_string(sb, (unsigned char*)str, len+1); *string_p=str; if(length_p) *length_p=len; } } } if(sb) raptor_free_stringbuffer(sb); raptor_www_set_write_bytes_handler(www, saved_write_bytes, saved_write_bytes_userdata); return (str == NULL); }
static int rasqal_builtin_agg_expression_execute_reset(void* user_data) { rasqal_builtin_agg_expression_execute* b; b = (rasqal_builtin_agg_expression_execute*)user_data; b->count = 0; b->error = 0; if(b->l) { rasqal_free_literal(b->l); b->l = 0; } if(b->sb) { raptor_free_stringbuffer(b->sb); b->sb = raptor_new_stringbuffer(); if(!b) return 1; } return 0; }
static unsigned char* check_query_read_file_string(const char* filename, const char* label, size_t* len_p) { raptor_stringbuffer *sb; size_t len; FILE *fh = NULL; unsigned char* string = NULL; unsigned char* buffer = NULL; sb = raptor_new_stringbuffer(); if(!sb) return NULL; fh = fopen(filename, "r"); if(!fh) { fprintf(stderr, "%s: %s '%s' open failed - %s", program, label, filename, strerror(errno)); goto tidy; } buffer = (unsigned char*)malloc(FILE_READ_BUF_SIZE); if(!buffer) goto tidy; while(!feof(fh)) { size_t read_len; read_len = fread((char*)buffer, 1, FILE_READ_BUF_SIZE, fh); if(read_len > 0) raptor_stringbuffer_append_counted_string(sb, buffer, read_len, 1); if(read_len < FILE_READ_BUF_SIZE) { if(ferror(fh)) { fprintf(stderr, "%s: file '%s' read failed - %s\n", program, filename, strerror(errno)); goto tidy; } break; } } len = raptor_stringbuffer_length(sb); string = (unsigned char*)malloc(len + 1); if(string) { raptor_stringbuffer_copy_to_string(sb, string, len); if(len_p) *len_p = len; } tidy: if(buffer) free(buffer); if(fh) fclose(fh); if(sb) raptor_free_stringbuffer(sb); return string; }
static void raptor_libxml_xmlStructuredError_handler_common(raptor_world *world, raptor_locator *locator, xmlErrorPtr err) { raptor_stringbuffer* sb; char *nmsg; raptor_log_level level = RAPTOR_LOG_LEVEL_ERROR; if(err == NULL || err->code == XML_ERR_OK || err->level == XML_ERR_NONE) return; /* Do not warn about things with no location */ if(err->level == XML_ERR_WARNING && !err->file) return; /* XML fatal errors never cause an abort */ if(err->level == XML_ERR_FATAL) err->level = XML_ERR_ERROR; sb = raptor_new_stringbuffer(); if(err->domain != XML_FROM_HTML) raptor_stringbuffer_append_counted_string(sb, (const unsigned char*)"XML ", 4, 1); if(err->domain != XML_FROM_NONE && err->domain < XML_LAST_DL) { const unsigned char* label; label = (const unsigned char*)raptor_libxml_domain_labels[(int)err->domain]; raptor_stringbuffer_append_string(sb, label, 1); raptor_stringbuffer_append_counted_string(sb, (const unsigned char*)" ", 1, 1); } if(err->level == XML_ERR_WARNING) raptor_stringbuffer_append_counted_string(sb, (const unsigned char*)"warning: ", 9, 1); else /* XML_ERR_ERROR or XML_ERR_FATAL */ raptor_stringbuffer_append_counted_string(sb, (const unsigned char*)"error: ", 7, 1); if(err->message) { unsigned char* msg; size_t len; msg = (unsigned char*)err->message; len= strlen((const char*)msg); if(len && msg[len-1] == '\n') msg[--len]='\0'; raptor_stringbuffer_append_counted_string(sb, msg, len, 1); } #if LIBXML_VERSION >= 20618 /* 2005-02-13 - v2.6.18 */ /* str1 has the detailed HTTP error */ if(err->domain == XML_FROM_HTTP && err->str1) { unsigned char* msg; size_t len; msg = (unsigned char*)err->str1; len= strlen((const char*)msg); if(len && msg[len-1] == '\n') msg[--len]='\0'; raptor_stringbuffer_append_counted_string(sb, (const unsigned char*)" - ", 3, 1); raptor_stringbuffer_append_counted_string(sb, msg, len, 1); } #endif /* When err->domain == XML_FROM_XPATH then err->int1 is * the offset into err->str1, the line with the error */ if(err->domain == XML_FROM_XPATH && err->str1) { raptor_stringbuffer_append_counted_string(sb, (const unsigned char*)" in ", 4, 1); raptor_stringbuffer_append_string(sb, (const unsigned char*)err->str1, 1); } nmsg = (char*)raptor_stringbuffer_as_string(sb); if(err->level == XML_ERR_FATAL) level = RAPTOR_LOG_LEVEL_FATAL; else if(err->level == XML_ERR_ERROR) level = RAPTOR_LOG_LEVEL_ERROR; else level = RAPTOR_LOG_LEVEL_WARN; raptor_log_error(world, level, locator, nmsg); raptor_free_stringbuffer(sb); }
/** * rasqal_service_execute: * @svc: rasqal service * * Execute a rasqal sparql protocol service * * Return value: query results or NULL on failure */ rasqal_query_results* rasqal_service_execute(rasqal_service* svc) { rasqal_query_results* results = NULL; unsigned char* result_string = NULL; size_t result_length; raptor_iostream* read_iostr = NULL; raptor_uri* read_base_uri = NULL; rasqal_variables_table* vars_table = NULL; rasqal_query_results_formatter* read_formatter = NULL; raptor_uri* retrieval_uri = NULL; raptor_stringbuffer* uri_sb = NULL; size_t len; unsigned char* str; raptor_world* raptor_world_ptr = rasqal_world_get_raptor(svc->world); if(!svc->www) { svc->www = raptor_new_www(raptor_world_ptr); if(!svc->www) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create WWW"); goto error; } } svc->started = 0; svc->final_uri = NULL; svc->sb = raptor_new_stringbuffer(); svc->content_type = NULL; if(svc->format) raptor_www_set_http_accept(svc->www, svc->format); else raptor_www_set_http_accept(svc->www, DEFAULT_FORMAT); raptor_www_set_write_bytes_handler(svc->www, rasqal_service_write_bytes, svc); raptor_www_set_content_type_handler(svc->www, rasqal_service_content_type_handler, svc); /* Construct a URI to retrieve following SPARQL protocol HTTP * binding from concatenation of * * 1. service_uri * 2. '?' * 3. "query=" query_string * 4. "&default-graph-uri=" background graph URI if any * 5. "&named-graph-uri=" named graph URI for all named graphs * with URI-escaping of the values */ uri_sb = raptor_new_stringbuffer(); if(!uri_sb) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create stringbuffer"); goto error; } str = raptor_uri_as_counted_string(svc->service_uri, &len); raptor_stringbuffer_append_counted_string(uri_sb, str, len, 1); raptor_stringbuffer_append_counted_string(uri_sb, (const unsigned char*)"?", 1, 1); if(svc->query_string) { raptor_stringbuffer_append_counted_string(uri_sb, (const unsigned char*)"query=", 6, 1); raptor_stringbuffer_append_uri_escaped_counted_string(uri_sb, svc->query_string, svc->query_string_len, 1); } if(svc->data_graphs) { rasqal_data_graph* dg; int i; int bg_graph_count; for(i = 0, bg_graph_count = 0; (dg = (rasqal_data_graph*)raptor_sequence_get_at(svc->data_graphs, i)); i++) { unsigned char* graph_str; size_t graph_len; raptor_uri* graph_uri; if(dg->flags & RASQAL_DATA_GRAPH_BACKGROUND) { if(bg_graph_count++) { if(bg_graph_count == 2) { /* Warn once, only when the second BG is seen */ rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_WARN, NULL, "Attempted to add use background graphs"); } /* always skip after first BG */ continue; } raptor_stringbuffer_append_counted_string(uri_sb, (const unsigned char*)"&default-graph-uri=", 19, 1); graph_uri = dg->uri; } else { raptor_stringbuffer_append_counted_string(uri_sb, (const unsigned char*)"&named-graph-uri=", 17, 1); graph_uri = dg->name_uri; } graph_str = raptor_uri_as_counted_string(graph_uri, &graph_len); raptor_stringbuffer_append_uri_escaped_counted_string(uri_sb, (const char*)graph_str, graph_len, 1); } } str = raptor_stringbuffer_as_string(uri_sb); retrieval_uri = raptor_new_uri(raptor_world_ptr, str); if(!retrieval_uri) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create retrieval URI %s", raptor_uri_as_string(retrieval_uri)); goto error; } raptor_free_stringbuffer(uri_sb); uri_sb = NULL; if(raptor_www_fetch(svc->www, retrieval_uri)) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to fetch retrieval URI %s", raptor_uri_as_string(retrieval_uri)); goto error; } vars_table = rasqal_new_variables_table(svc->world); if(!vars_table) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create variables table"); goto error; } results = rasqal_new_query_results(svc->world, NULL, RASQAL_QUERY_RESULTS_BINDINGS, vars_table); /* (results takes a reference/copy to vars_table) */ rasqal_free_variables_table(vars_table); vars_table = NULL; if(!results) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create query results"); goto error; } result_length = raptor_stringbuffer_length(svc->sb); result_string = raptor_stringbuffer_as_string(svc->sb); read_iostr = raptor_new_iostream_from_string(raptor_world_ptr, result_string, result_length); if(!read_iostr) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create iostream from string"); rasqal_free_query_results(results); results = NULL; goto error; } read_base_uri = svc->final_uri ? svc->final_uri : svc->service_uri; read_formatter = rasqal_new_query_results_formatter(svc->world, /* format name */ NULL, svc->content_type, /* format URI */ NULL); if(!read_formatter) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create query formatter for type %s", svc->content_type); rasqal_free_query_results(results); results = NULL; goto error; } if(rasqal_query_results_formatter_read(svc->world, read_iostr, read_formatter, results, read_base_uri)) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to read from query formatter"); rasqal_free_query_results(results); results = NULL; goto error; } error: if(retrieval_uri) raptor_free_uri(retrieval_uri); if(uri_sb) raptor_free_stringbuffer(uri_sb); if(read_formatter) rasqal_free_query_results_formatter(read_formatter); if(read_iostr) raptor_free_iostream(read_iostr); if(vars_table) rasqal_free_variables_table(vars_table); if(svc->final_uri) { raptor_free_uri(svc->final_uri); svc->final_uri = NULL; } if(svc->content_type) { RASQAL_FREE(cstring, svc->content_type); svc->content_type = NULL; } if(svc->sb) { raptor_free_stringbuffer(svc->sb); svc->sb = NULL; } return results; }
int main(int argc, char *argv[]) { int query_from_string = 0; unsigned char *query_string = NULL; unsigned char *uri_string = NULL; int free_uri_string = 0; unsigned char *base_uri_string = NULL; rasqal_query *rq = NULL; rasqal_query_results *results; const char *ql_name = "sparql"; char *ql_uri = NULL; int rc = 0; raptor_uri *uri = NULL; raptor_uri *base_uri = NULL; char *filename = NULL; char *p; int usage = 0; int help = 0; int quiet = 0; int count = 0; int dryrun = 0; raptor_sequence* data_graphs = NULL; const char *result_format = NULL; query_output_format output_format = QUERY_OUTPUT_UNKNOWN; rasqal_feature query_feature = (rasqal_feature)-1; int query_feature_value= -1; unsigned char* query_feature_string_value = NULL; rasqal_world *world; raptor_world* raptor_world_ptr = NULL; #ifdef RASQAL_INTERNAL int store_results = -1; #endif char* data_graph_parser_name = NULL; raptor_iostream* iostr = NULL; const unsigned char* service_uri_string = 0; raptor_uri* service_uri = NULL; program = argv[0]; if((p = strrchr(program, '/'))) program = p + 1; else if((p = strrchr(program, '\\'))) program = p + 1; argv[0] = program; world = rasqal_new_world(); if(!world || rasqal_world_open(world)) { fprintf(stderr, "%s: rasqal_world init failed\n", program); return(1); } raptor_world_ptr = rasqal_world_get_raptor(world); rasqal_world_set_log_handler(world, world, roqet_log_handler); #ifdef STORE_RESULTS_FLAG /* This is for debugging only */ if(1) { char* sr = getenv("RASQAL_DEBUG_STORE_RESULTS"); if(sr) store_results = atoi(sr); } #endif while (!usage && !help) { int c; #ifdef HAVE_GETOPT_LONG int option_index = 0; c = getopt_long (argc, argv, GETOPT_STRING, long_options, &option_index); #else c = getopt (argc, argv, GETOPT_STRING); #endif if (c == -1) break; switch (c) { case 0: case '?': /* getopt() - unknown option */ usage = 1; break; case 'c': count = 1; break; case 'd': output_format = QUERY_OUTPUT_UNKNOWN; if(optarg) { int i; for(i = 1; i <= QUERY_OUTPUT_LAST; i++) if(!strcmp(optarg, query_output_format_labels[i][0])) { output_format = (query_output_format)i; break; } } if(output_format == QUERY_OUTPUT_UNKNOWN) { int i; fprintf(stderr, "%s: invalid argument `%s' for `" HELP_ARG(d, dump-query) "'\n", program, optarg); for(i = 1; i <= QUERY_OUTPUT_LAST; i++) fprintf(stderr, " %-12s for %s\n", query_output_format_labels[i][0], query_output_format_labels[i][1]); usage = 1; } break; case 'e': if(optarg) { query_string = (unsigned char*)optarg; query_from_string = 1; } break; case 'f': if(optarg) { if(!strcmp(optarg, "help")) { int i; fprintf(stderr, "%s: Valid query features are:\n", program); for(i = 0; i < (int)rasqal_get_feature_count(); i++) { const char *feature_name; const char *feature_label; if(!rasqal_features_enumerate(world, (rasqal_feature)i, &feature_name, NULL, &feature_label)) { const char *feature_type; feature_type = (rasqal_feature_value_type((rasqal_feature)i) == 0) ? "" : " (string)"; fprintf(stderr, " %-20s %s%s\n", feature_name, feature_label, feature_type); } } fputs("Features are set with `" HELP_ARG(f, feature) " FEATURE=VALUE or `-f FEATURE'\nand take a decimal integer VALUE except where noted, defaulting to 1 if omitted.\n", stderr); rasqal_free_world(world); exit(0); } else { int i; size_t arg_len = strlen(optarg); for(i = 0; i < (int)rasqal_get_feature_count(); i++) { const char *feature_name; size_t len; if(rasqal_features_enumerate(world, (rasqal_feature)i, &feature_name, NULL, NULL)) continue; len = strlen(feature_name); if(!strncmp(optarg, feature_name, len)) { query_feature = (rasqal_feature)i; if(rasqal_feature_value_type(query_feature) == 0) { if(len < arg_len && optarg[len] == '=') query_feature_value=atoi(&optarg[len + 1]); else if(len == arg_len) query_feature_value = 1; } else { if(len < arg_len && optarg[len] == '=') query_feature_string_value = (unsigned char*)&optarg[len + 1]; else if(len == arg_len) query_feature_string_value = (unsigned char*)""; } break; } } if(query_feature_value < 0 && !query_feature_string_value) { fprintf(stderr, "%s: invalid argument `%s' for `" HELP_ARG(f, feature) "'\nTry '%s " HELP_ARG(f, feature) " help' for a list of valid features\n", program, optarg, program); usage = 1; } } } break; case 'F': if(optarg) { if(!raptor_world_is_parser_name(raptor_world_ptr, optarg)) { fprintf(stderr, "%s: invalid parser name `%s' for `" HELP_ARG(F, format) "'\n\n", program, optarg); usage = 1; } else { data_graph_parser_name = optarg; } } break; case 'h': help = 1; break; case 'n': dryrun = 1; break; case 'p': if(optarg) service_uri_string = (const unsigned char*)optarg; break; case 'r': if(optarg) result_format = optarg; break; case 'i': if(rasqal_language_name_check(world, optarg)) ql_name = optarg; else { int i; fprintf(stderr, "%s: invalid argument `%s' for `" HELP_ARG(i, input) "'\n", program, optarg); fprintf(stderr, "Valid arguments are:\n"); for(i = 0; 1; i++) { const raptor_syntax_description* desc; desc = rasqal_world_get_query_language_description(world, i); if(desc == NULL) break; fprintf(stderr, " %-18s for %s\n", desc->names[0], desc->label); } usage = 1; } break; case 'q': quiet = 1; break; case 's': case 'D': case 'G': if(optarg) { rasqal_data_graph *dg = NULL; rasqal_data_graph_flags type; type = (c == 's' || c == 'G') ? RASQAL_DATA_GRAPH_NAMED : RASQAL_DATA_GRAPH_BACKGROUND; if(!strcmp((const char*)optarg, "-")) { /* stdin: use an iostream not a URI data graph */ unsigned char* source_uri_string; raptor_uri* iostr_base_uri = NULL; raptor_uri* graph_name = NULL; /* FIXME - get base URI from somewhere else */ source_uri_string = (unsigned char*)"file:///dev/stdin"; iostr_base_uri = raptor_new_uri(raptor_world_ptr, source_uri_string); if(iostr_base_uri) { iostr = raptor_new_iostream_from_file_handle(raptor_world_ptr, stdin); if(iostr) dg = rasqal_new_data_graph_from_iostream(world, iostr, iostr_base_uri, graph_name, type, NULL, data_graph_parser_name, NULL); } if(base_uri) raptor_free_uri(base_uri); } else if(!access((const char*)optarg, R_OK)) { /* file: use URI */ unsigned char* source_uri_string; raptor_uri* source_uri; raptor_uri* graph_name = NULL; source_uri_string = raptor_uri_filename_to_uri_string((const char*)optarg); source_uri = raptor_new_uri(raptor_world_ptr, source_uri_string); raptor_free_memory(source_uri_string); if(type == RASQAL_DATA_GRAPH_NAMED) graph_name = source_uri; if(source_uri) dg = rasqal_new_data_graph_from_uri(world, source_uri, graph_name, type, NULL, data_graph_parser_name, NULL); if(source_uri) raptor_free_uri(source_uri); } else { raptor_uri* source_uri; raptor_uri* graph_name = NULL; /* URI: use URI */ source_uri = raptor_new_uri(raptor_world_ptr, (const unsigned char*)optarg); if(type == RASQAL_DATA_GRAPH_NAMED) graph_name = source_uri; if(source_uri) dg = rasqal_new_data_graph_from_uri(world, source_uri, graph_name, type, NULL, data_graph_parser_name, NULL); if(source_uri) raptor_free_uri(source_uri); } if(!dg) { fprintf(stderr, "%s: Failed to create data graph for `%s'\n", program, optarg); return(1); } if(!data_graphs) { data_graphs = raptor_new_sequence((raptor_data_free_handler)rasqal_free_data_graph, NULL); if(!data_graphs) { fprintf(stderr, "%s: Failed to create data graphs sequence\n", program); return(1); } } raptor_sequence_push(data_graphs, dg); } break; case 'W': if(optarg) warning_level = atoi(optarg); else warning_level = 0; rasqal_world_set_warning_level(world, warning_level); break; case 'E': ignore_errors = 1; break; case 'v': fputs(rasqal_version_string, stdout); fputc('\n', stdout); rasqal_free_world(world); exit(0); #ifdef STORE_RESULTS_FLAG case STORE_RESULTS_FLAG: store_results = (!strcmp(optarg, "yes") || !strcmp(optarg, "YES")); break; #endif } } if(!help && !usage) { if(service_uri_string) { if(optind != argc && optind != argc-1) usage = 2; /* Title and usage */ } else if(query_string) { if(optind != argc && optind != argc-1) usage = 2; /* Title and usage */ } else { if(optind != argc-1 && optind != argc-2) usage = 2; /* Title and usage */ } } if(usage) { if(usage > 1) { fprintf(stderr, title_format_string, rasqal_version_string); fputs("Rasqal home page: ", stderr); fputs(rasqal_home_url_string, stderr); fputc('\n', stderr); fputs(rasqal_copyright_string, stderr); fputs("\nLicense: ", stderr); fputs(rasqal_license_string, stderr); fputs("\n\n", stderr); } fprintf(stderr, "Try `%s " HELP_ARG(h, help) "' for more information.\n", program); rasqal_free_world(world); exit(1); } if(help) { int i; printf(title_format_string, rasqal_version_string); puts("Run an RDF query giving variable bindings or RDF triples."); printf("Usage: %s [OPTIONS] <query URI> [base URI]\n", program); printf(" %s [OPTIONS] -e <query string> [base URI]\n", program); printf(" %s [OPTIONS] -p <SPARQL protocol service URI> -e <query string> [base URI]\n\n", program); fputs(rasqal_copyright_string, stdout); fputs("\nLicense: ", stdout); puts(rasqal_license_string); fputs("Rasqal home page: ", stdout); puts(rasqal_home_url_string); puts("\nNormal operation is to execute the query retrieved from URI <query URI>"); puts("and print the results in a simple text format."); puts("\nMain options:"); puts(HELP_TEXT("e", "exec QUERY ", "Execute QUERY string instead of <query URI>")); puts(HELP_TEXT("p", "protocol URI ", "Execute QUERY against a SPARQL protocol service URI")); puts(HELP_TEXT("i", "input LANGUAGE ", "Set query language name to one of:")); for(i = 0; 1; i++) { const raptor_syntax_description* desc; desc = rasqal_world_get_query_language_description(world, i); if(!desc) break; printf(" %-15s %s", desc->names[0], desc->label); if(!i) puts(" (default)"); else putchar('\n'); } puts(HELP_TEXT("r", "results FORMAT ", "Set query results output format to one of:")); puts(" For variable bindings and boolean results:"); puts(" simple A simple text format (default)"); for(i = 0; 1; i++) { const raptor_syntax_description* desc; desc = rasqal_world_get_query_results_format_description(world, i); if(!desc) break; if(desc->flags & RASQAL_QUERY_RESULTS_FORMAT_FLAG_WRITER) printf(" %-10s %s\n", desc->names[0], desc->label); } puts(" For RDF graph results:"); for(i = 0; 1; i++) { const raptor_syntax_description *desc; desc = raptor_world_get_parser_description(raptor_world_ptr, i); if(!desc) break; printf(" %-15s %s", desc->names[0], desc->label); if(!i) puts(" (default)"); else putchar('\n'); } puts("\nAdditional options:"); puts(HELP_TEXT("c", "count ", "Count triples - no output")); puts(HELP_TEXT("d FORMAT", "dump-query FORMAT", HELP_PAD "Print the parsed query out in FORMAT:")); puts(HELP_TEXT("D URI", "data URI ", "RDF data source URI")); for(i = 1; i <= QUERY_OUTPUT_LAST; i++) printf(" %-15s %s\n", query_output_format_labels[i][0], query_output_format_labels[i][1]); puts(HELP_TEXT("E", "ignore-errors ", "Ignore error messages")); puts(HELP_TEXT("f FEATURE(=VALUE)", "feature FEATURE(=VALUE)", HELP_PAD "Set query features" HELP_PAD "Use `-f help' for a list of valid features")); puts(HELP_TEXT("F NAME", "format NAME ", "Set data source format name (default: guess)")); puts(HELP_TEXT("G URI", "named URI ", "RDF named graph data source URI")); puts(HELP_TEXT("h", "help ", "Print this help, then exit")); puts(HELP_TEXT("n", "dryrun ", "Prepare but do not run the query")); puts(HELP_TEXT("q", "quiet ", "No extra information messages")); puts(HELP_TEXT("s URI", "source URI ", "Same as `-G URI'")); puts(HELP_TEXT("v", "version ", "Print the Rasqal version")); puts(HELP_TEXT("w", "walk-query ", "Print query. Same as '-d structure'")); puts(HELP_TEXT("W LEVEL", "warnings LEVEL", HELP_PAD "Set warning message LEVEL from 0: none to 100: all")); #ifdef STORE_RESULTS_FLAG puts(HELP_TEXT_LONG("store-results BOOL", "DEBUG: Set store results yes/no BOOL")); #endif puts("\nReport bugs to http://bugs.librdf.org/"); rasqal_free_world(world); exit(0); } if(service_uri_string) { service_uri = raptor_new_uri(raptor_world_ptr, service_uri_string); if(optind == argc-1) { base_uri_string = (unsigned char*)argv[optind]; } } else if(query_string) { if(optind == argc-1) base_uri_string = (unsigned char*)argv[optind]; } else { if(optind == argc-1) uri_string = (unsigned char*)argv[optind]; else { uri_string = (unsigned char*)argv[optind++]; base_uri_string = (unsigned char*)argv[optind]; } /* If uri_string is "path-to-file", turn it into a file: URI */ if(!strcmp((const char*)uri_string, "-")) { if(!base_uri_string) { fprintf(stderr, "%s: A Base URI is required when reading from standard input.\n", program); return(1); } uri_string = NULL; } else if(!access((const char*)uri_string, R_OK)) { filename = (char*)uri_string; uri_string = raptor_uri_filename_to_uri_string(filename); free_uri_string = 1; } if(uri_string) { uri = raptor_new_uri(raptor_world_ptr, uri_string); if(!uri) { fprintf(stderr, "%s: Failed to create URI for %s\n", program, uri_string); return(1); } } else uri = NULL; /* stdin */ } if(!base_uri_string) { if(uri) base_uri = raptor_uri_copy(uri); } else base_uri = raptor_new_uri(raptor_world_ptr, base_uri_string); if(base_uri_string && !base_uri) { fprintf(stderr, "%s: Failed to create URI for %s\n", program, base_uri_string); return(1); } if(service_uri_string) { /* NOP - nothing to do here */ } else if(query_string) { /* NOP - already got it */ } else if(!uri_string) { size_t read_len; query_string = (unsigned char*)calloc(FILE_READ_BUF_SIZE, 1); read_len = fread(query_string, FILE_READ_BUF_SIZE, 1, stdin); if(read_len < FILE_READ_BUF_SIZE) { if(ferror(stdin)) fprintf(stderr, "%s: query string stdin read failed - %s\n", program, strerror(errno)); else fprintf(stderr, "%s: query string stdin read failed - no error\n", program); rc = 1; goto tidy_setup; } query_from_string = 0; } else if(filename) { raptor_stringbuffer *sb = raptor_new_stringbuffer(); size_t len; FILE *fh; unsigned char* buffer; fh = fopen(filename, "r"); if(!fh) { fprintf(stderr, "%s: file '%s' open failed - %s", program, filename, strerror(errno)); rc = 1; goto tidy_setup; } buffer = (unsigned char*)malloc(FILE_READ_BUF_SIZE); while(!feof(fh)) { size_t read_len; read_len = fread((char*)buffer, 1, FILE_READ_BUF_SIZE, fh); if(read_len > 0) raptor_stringbuffer_append_counted_string(sb, buffer, read_len, 1); if(read_len < FILE_READ_BUF_SIZE) { if(ferror(fh)) { fprintf(stderr, "%s: file '%s' read failed - %s\n", program, filename, strerror(errno)); free(buffer); fclose(fh); rc = 1; goto tidy_setup; } break; } } free(buffer); fclose(fh); len = raptor_stringbuffer_length(sb); query_string = (unsigned char*)malloc(len + 1); raptor_stringbuffer_copy_to_string(sb, query_string, len); raptor_free_stringbuffer(sb); query_from_string = 0; } else { raptor_www *www; www = raptor_new_www(raptor_world_ptr); if(www) { raptor_www_fetch_to_string(www, uri, (void**)&query_string, NULL, malloc); raptor_free_www(www); } if(!query_string || error_count) { fprintf(stderr, "%s: Retrieving query at URI '%s' failed\n", program, uri_string); rc = 1; goto tidy_setup; } query_from_string = 0; } if(!quiet) { if(service_uri) { fprintf(stderr, "%s: Calling SPARQL service at URI %s", program, service_uri_string); if(query_string) fprintf(stderr, " with query '%s'", query_string); if(base_uri_string) fprintf(stderr, " with base URI %s\n", base_uri_string); fputc('\n', stderr); } else if(query_from_string) { if(base_uri_string) fprintf(stderr, "%s: Running query '%s' with base URI %s\n", program, query_string, base_uri_string); else fprintf(stderr, "%s: Running query '%s'\n", program, query_string); } else if(filename) { if(base_uri_string) fprintf(stderr, "%s: Querying from file %s with base URI %s\n", program, filename, base_uri_string); else fprintf(stderr, "%s: Querying from file %s\n", program, filename); } else if(uri_string) { if(base_uri_string) fprintf(stderr, "%s: Querying URI %s with base URI %s\n", program, uri_string, base_uri_string); else fprintf(stderr, "%s: Querying URI %s\n", program, uri_string); } } if(service_uri) { /* Execute query remotely */ if(!dryrun) results = roqet_call_sparql_service(world, service_uri, query_string, data_graphs, /* service_format */ NULL); } else { /* Execute query in this query engine (from URI or from -e QUERY) */ rq = roqet_init_query(world, ql_name, ql_uri, query_string, base_uri, query_feature, query_feature_value, query_feature_string_value, store_results, data_graphs); if(!rq) { rc = 1; goto tidy_query; } if(output_format != QUERY_OUTPUT_UNKNOWN && !quiet) roqet_print_query(rq, raptor_world_ptr, output_format, base_uri); if(!dryrun) { results = rasqal_query_execute(rq); } } /* No results from dryrun */ if(dryrun) goto tidy_query; if(!results) { fprintf(stderr, "%s: Query execution failed\n", program); rc = 1; goto tidy_query; } if(rasqal_query_results_is_bindings(results)) { if(result_format) rc = print_formatted_query_results(world, results, raptor_world_ptr, stdout, result_format, base_uri, quiet); else print_bindings_result_simple(results, stdout, quiet, count); } else if(rasqal_query_results_is_boolean(results)) { if(result_format) rc = print_formatted_query_results(world, results, raptor_world_ptr, stdout, result_format, base_uri, quiet); else print_boolean_result_simple(results, stdout, quiet); } else if(rasqal_query_results_is_graph(results)) { if(!result_format) result_format = DEFAULT_GRAPH_FORMAT; rc = print_graph_result(rq, results, raptor_world_ptr, stdout, result_format, base_uri, quiet); } else { fprintf(stderr, "%s: Query returned unknown result format\n", program); rc = 1; } rasqal_free_query_results(results); tidy_query: if(!query_from_string) free(query_string); if(rq) rasqal_free_query(rq); tidy_setup: if(data_graphs) raptor_free_sequence(data_graphs); if(base_uri) raptor_free_uri(base_uri); if(uri) raptor_free_uri(uri); if(free_uri_string) raptor_free_memory(uri_string); if(iostr) raptor_free_iostream(iostr); if(service_uri) raptor_free_uri(service_uri); rasqal_free_world(world); if(error_count && !ignore_errors) return 1; if(warning_count && warning_level != 0) return 2; return (rc); }
void raptor_libxml_xmlStructuredErrorFunc(void *user_data, xmlErrorPtr err) { raptor_error_handlers* error_handlers=(raptor_error_handlers*)user_data; raptor_stringbuffer* sb; char *nmsg; raptor_message_handler handler=NULL; void* handler_data=NULL; raptor_log_level level=RAPTOR_LOG_LEVEL_ERROR; if(err == NULL || err->code == XML_ERR_OK || err->level == XML_ERR_NONE) return; /* Do not warn about things with no location */ if(err->level == XML_ERR_WARNING && !err->file) return; /* XML fatal errors never cause an abort */ if(err->level == XML_ERR_FATAL) err->level= XML_ERR_ERROR; sb=raptor_new_stringbuffer(); if(err->domain != XML_FROM_HTML) raptor_stringbuffer_append_counted_string(sb, (const unsigned char*)"XML ", 4, 1); if(err->domain != XML_FROM_NONE && err->domain < XML_LAST_DL) { const unsigned char* label; label=(const unsigned char*)raptor_libxml_domain_labels[(int)err->domain]; raptor_stringbuffer_append_string(sb, label, 1); raptor_stringbuffer_append_counted_string(sb, (const unsigned char*)" ", 1, 1); } if(err->level == XML_ERR_WARNING) raptor_stringbuffer_append_counted_string(sb, (const unsigned char*)"warning: ", 9, 1); else /* XML_ERR_ERROR or XML_ERR_FATAL */ raptor_stringbuffer_append_counted_string(sb, (const unsigned char*)"error: ", 7, 1); if(err->message) { unsigned char* msg; size_t len; msg=(unsigned char*)err->message; len= strlen((const char*)msg); if(len && msg[len-1] == '\n') msg[--len]='\0'; raptor_stringbuffer_append_counted_string(sb, msg, len, 1); } #if LIBXML_VERSION >= 20618 /* 2005-02-13 - v2.6.18 */ /* str1 has the detailed HTTP error */ if(err->domain == XML_FROM_HTTP && err->str1) { unsigned char* msg; size_t len; msg=(unsigned char*)err->str1; len= strlen((const char*)msg); if(len && msg[len-1] == '\n') msg[--len]='\0'; raptor_stringbuffer_append_counted_string(sb, (const unsigned char*)" - ", 3, 1); raptor_stringbuffer_append_counted_string(sb, msg, len, 1); } #endif /* When err->domain == XML_FROM_XPATH then err->int1 is * the offset into err->str1, the line with the error */ if(err->domain == XML_FROM_XPATH && err->str1) { raptor_stringbuffer_append_counted_string(sb, (const unsigned char*)" in ", 4, 1); raptor_stringbuffer_append_string(sb, (const unsigned char*)err->str1, 1); } if(error_handlers) { if(error_handlers->magic != RAPTOR_ERROR_HANDLER_MAGIC) { #ifdef RAPTOR_DEBUG if(1) /* FIXME */ RAPTOR_DEBUG2("Received bogus error_handlers pointer %p\n", error_handlers); else RAPTOR_FATAL2("Received bogus error_handlers pointer %p\n", error_handlers); #endif error_handlers=NULL; } } nmsg=(char*)raptor_stringbuffer_as_string(sb); if(err->level == XML_ERR_FATAL) level=RAPTOR_LOG_LEVEL_FATAL; else if(err->level == XML_ERR_ERROR) level=RAPTOR_LOG_LEVEL_ERROR; else level=RAPTOR_LOG_LEVEL_WARNING; if(error_handlers && level <= error_handlers->last_log_level) { handler=error_handlers->handlers[level].handler; handler_data=error_handlers->handlers[level].user_data; } raptor_log_error(level, handler, handler_data, (error_handlers ? error_handlers->locator : NULL), nmsg); raptor_free_stringbuffer(sb); }