/* * ------------------------------------------------------------------------ * * "rcqpCmd_query(SEXP inMother, SEXP inChild, SEXP inQuery)" -- * * * * ------------------------------------------------------------------------ */ SEXP rcqpCmd_query(SEXP inMother, SEXP inChild, SEXP inQuery) { SEXP result = R_NilValue; char *child, *mother, *query, *c, *sc; if (!isString(inMother) || length(inMother) != 1) error("invalid corpus name"); if (!isString(inChild) || length(inChild) != 1) error("invalid subcorpus name"); if (!isString(inQuery) || length(inQuery) != 1) error("invalid query name"); PROTECT(inMother); PROTECT(inChild); PROTECT(inQuery); mother = (char*)CHAR(STRING_ELT(inMother,0)); child = (char*)CHAR(STRING_ELT(inChild,0)); query = (char*)CHAR(STRING_ELT(inQuery,0)); if (!split_subcorpus_spec(mother, &c, &sc)) { rcqp_error_code(cqi_errno); } else { char *cqp_query; int len = strlen(child) + strlen(query) + 10; cqp_query = (char *) cl_malloc(len); if (!check_subcorpus_name(child) || !cqi_activate_corpus(mother)) { rcqp_error_code(cqi_errno); } else { query_lock = floor(1e9 * cl_runif()) + 1; /* activate query lock mode with random key */ if (rcqp_query_has_semicolon(query)) { sprintf(cqp_query, "%s = %s", child, query); } else { sprintf(cqp_query, "%s = %s;", child, query); } if (!cqp_parse_string(cqp_query)) { rcqp_error_code(CQI_CQP_ERROR_GENERAL); /* should be changed to detailed error messages */ } else { char * full_child; CorpusList * childcl; full_child = combine_subcorpus_spec(c, child); /* c is the 'physical' part of the mother corpus */ childcl = cqi_find_corpus(full_child); if ((childcl) == NULL) { rcqp_error_code(CQI_CQP_ERROR_GENERAL); } free(full_child); } query_lock = 0; /* deactivate query lock mode */ } free(cqp_query); } free(c); free(sc); UNPROTECT(3); return result; }
void do_cqi_cqp_query(void) { char *child, *mother, *query, *c, *sc; mother = cqi_read_string(); child = cqi_read_string(); query = cqi_read_string(); if (server_debug) Rprintf( "CQi: CQI_CQP_QUERY('%s', '%s', '%s')\n", mother, child, query); if (!split_subcorpus_spec(mother, &c, &sc)) { cqi_command(cqi_errno); } else { char *cqp_query; int len = strlen(child) + strlen(query) + 10; cqp_query = (char *) cl_malloc(len); if (!check_subcorpus_name(child) || !cqi_activate_corpus(mother)) { cqi_command(cqi_errno); } else { query_lock = floor(1e9 * cl_runif()) + 1; /* activate query lock mode with random key */ Rprintf("CQPSERVER: query_lock = %d\n", query_lock); if (query_has_semicolon(query)) sprintf(cqp_query, "%s = %s", child, query); else sprintf(cqp_query, "%s = %s;", child, query); if (!cqp_parse_string(cqp_query)) cqi_command(CQI_CQP_ERROR_GENERAL); /* should be changed to detailed error messages */ else { char *full_child; CorpusList *childcl; full_child = combine_subcorpus_spec(c, child); /* c is the 'physical' part of the mother corpus */ childcl = cqi_find_corpus(full_child); if ((childcl) == NULL) cqi_command(CQI_CQP_ERROR_GENERAL); else { if (server_log) { Rprintf("'%s' ran the following query on %s\n", user, mother); Rprintf("\t%s\n", cqp_query); Rprintf("and got %d matches.\n", childcl->size); } cqi_command(CQI_STATUS_OK); } free(full_child); } query_lock = 0; /* deactivate query lock mode */ } free(cqp_query); } free(c); free(sc); }
/* * ------------------------------------------------------------------------ * * "rcqpCmd_cqpCmd(SEXP inCommand)" -- * * * * ------------------------------------------------------------------------ */ SEXP rcqpCmd_cqpCmd(SEXP inCommand) { SEXP result = R_NilValue; char *command; if (!isString(inCommand) || length(inCommand) != 1) error("invalid command argument type or length"); PROTECT(inCommand); command = (char*)CHAR(STRING_ELT(inCommand,0)); //query_lock = floor(1e9 * cl_runif()) + 1; /* activate query lock mode with random key */ query_lock = 0; /* deactivate query lock mode */ if (!cqp_parse_string(command)) { rcqp_error_code(CQI_CQP_ERROR_GENERAL); /* should be changed to detailed error messages */ } query_lock = 0; /* deactivate query lock mode */ UNPROTECT(1); return result; }
/** this function replaces cqp_parse_file(stdin,0) if we're using GNU Readline */ void readline_main(void) { char prompt[CL_MAX_LINE_LENGTH]; char *input = NULL; /* activate CQP's custom completion function */ rl_attempted_completion_function = cqp_custom_completion; /* configuration: don't break tokens on $, so word lists work correctly (everything else corresponds to readline defaults) */ rl_completer_word_break_characters = " \t\n\"\\'`@><=;|&{("; /* if CQP history file is specified, read history from file */ if (cqp_history_file != NULL) { /* ignore errors; it's probably just that the history file doesn't exist yet */ read_history(cqp_history_file); } /* == the line input loop == */ while (!exit_cqp) { if (input != NULL) { free(input); input = NULL; } if (highlighting) { printf(get_typeface_escape('n')); /* work around 'bug' in less which may not switch off display attributes when user exits */ fflush(stdout); } if (silent) { input = readline(NULL); } else { if (current_corpus != NULL) { /* don't use terminal colours for the prompt because they mess up readline's formatting */ if (STREQ(current_corpus->name, current_corpus->mother_name)) sprintf(prompt, "%s> ", current_corpus->name); else sprintf(prompt, "%s:%s[%d]> ", current_corpus->mother_name, current_corpus->name, current_corpus->size); } else sprintf(prompt, "[no corpus]> "); input = readline(prompt); } if (input != NULL) { input = ensure_semicolon(input); /* add semicolon at end of line if missing (also replaces ws-only lines by "") */ if (*input) add_history(input); /* add input line to history (unless it's an empty line) */ cqp_parse_string(input); /* parse & execute query */ } else { exit_cqp = True; /* NULL means we've had an EOF character */ } /* reinstall signal handler if necessary */ if (! signal_handler_is_installed) install_signal_handler(); } if (save_on_exit) save_unsaved_subcorpora(); if (!silent) { printf("\nDone. Share and enjoy!\n"); } }