Example #1
0
/**
 * Main function for the cqpserver app.
 */
int
main(int argc, char *argv[])
{
  int cmd;

  which_app = cqpserver;

  /* TODO: shouldn't these come AFTER initialize_cqp(), as that function may overwrite these values with defaults?
   * or maybe I've missed some subtlety here....*/
  silent = 1; 
  paging = autoshow = auto_save = 0;

  if (!initialize_cqp(argc, argv)) {
   Rprintf( "CQPserver: ERROR Couldn't initialise CQP engine.\n");
    rcqp_receive_error(1);
  }
  cqiserver_welcome();

  if (localhost) {
    add_host_to_list("127.0.0.1"); /* in -L mode, connections from localhost are automatically accepted  */
  }

  if (0 < accept_connection(server_port)) {
    if (server_log)
     Rprintf("CQPserver: Connected. Waiting for CONNECT request.\n");
  }
  else {
   Rprintf( "CQPserver: ERROR Connection failed.\n");
    rcqp_receive_error(1);
  }

  /* establish CQi connection: wait for CONNECT request */
  cmd = cqi_read_command();
  if (cmd != CQI_CTRL_CONNECT) {
    if (server_log)
     Rprintf("CQPserver: Connection refused.\n");
    cqiserver_wrong_command_error(cmd);
  }
  user = cqi_read_string();
  passwd = cqi_read_string();
  if (server_log)
   Rprintf("CQPserver: CONNECT  user = '******'  passwd = '%s'  pid = %d\n", user, passwd, (int)getpid());

  /* check password here (always required !!) */
  if (!authenticate_user(user, passwd)) {
   Rprintf("CQPserver: Wrong username or password. Connection refused.\n"); /* TODO shouldn't this be to stderr as it is not conditional on server_log? */
    cqi_command(CQI_ERROR_CONNECT_REFUSED);
  }
  else {
    cqi_command(CQI_STATUS_CONNECT_OK);

    /* re-randomize for query lock key generation */
    cl_randomize();

    /* check which corpora the user is granted access to */
    {
      CorpusList *cl = FirstCorpusFromList();
      while (cl != NULL) {
        if (!check_grant(user, cl->name))
          dropcorpus(cl);
        cl = NextCorpusFromList(cl);
      }
    }

    /* start command interpreter loop */
    interpreter();

    if (server_log)
     Rprintf("CQPserver: User '%s' has logged off.\n", user);
  }

  /* connection terminated; clean up and exit */
 Rprintf("CQPserver: Exit. (pid = %d)\n", (int)getpid());

  /* TODO should we check cqp_error_status as in the main cqp app? */
  return 0;
}
Example #2
0
/**
 * Main function for the interactive CQP program.
 *
 * Doesn't do much except call the initialisation function,
 * and then one of the loop-and-parse-input functions.
 *
 * @param argc  Number of commandline arguments.
 * @param argv  Pointer to commandline arguments.
 * @return      Return value to OS.
 */
int
main(int argc, char *argv[])
{

  which_app = cqp;

  if (!initialize_cqp(argc, argv)) {
    fprintf(stderr, "Can't initialize CQP\n");
    exit(1);
  }

  /* Test ANSI colours (if CQP was invoked with -C switch) */
  if (use_colour) {
#ifndef __MINGW__
    char *blue = get_colour_escape('b', 1);
    char *green = get_colour_escape('g', 1);
    char *red = get_colour_escape('r', 1);
    char *pink = get_colour_escape('p', 1);
    char *cyanBack = get_colour_escape('c', 0);
    char *greenBack = get_colour_escape('g', 0);
    char *yellowBack = get_colour_escape('y', 0);
    char *bold = get_typeface_escape('b');
    char *underline = get_typeface_escape('u');
    char *standout = get_typeface_escape('s');
    char *normal = get_typeface_escape('n');
    char sc_colour[256];
    int i, j;

    printf("%s%sWelcome%s to %s%sC%s%sQ%s%sP%s -- ", green, bold, normal, red, bold, pink, bold, blue, bold, normal);
    printf("the %s Colourful %s Query %s Processor %s.\n", yellowBack, greenBack, cyanBack, normal);

    for (i = 3; i <= 4; i++) {
      printf("[");
      for (j = 0; j < 8; j++) {
        sprintf(sc_colour, "\x1B[0;%d%dm", i,j);
        printf("%d%d: %sN%s%sB%s%sU%s%sS%s  ",
               i, j,
               sc_colour,
               sc_colour, bold,
               sc_colour, underline,
               sc_colour, standout,
               normal);
      }
      printf("]\n");
    }
#else
    fprintf(stderr, "We're sorry, CQP's Colourful Mode is not available under Windows.\n");
    fprintf(stderr, "CQP will continue as normal without it...\n");
    use_colour = 0;
#endif
  } /* endif use_colour */

  install_signal_handler();

  if (child_process) {
    printf("CQP version " VERSION "\n");
    fflush(stdout);
  }

  if (batchmode) {
    if (batchfd == NULL)
      fprintf(stderr, "Can't open batch file\n");
    else
      cqp_parse_file(batchfd, 1);
  }
  else {
#ifdef USE_READLINE
    if (use_readline)
      readline_main();
    else
#endif /* USE_READLINE */
      cqp_parse_file(stdin, 0);
  }

  if (macro_debug)
    macro_statistics();

  return (cqp_error_status == 0 ? 0 : 1);
}