Exemplo n.º 1
0
static void parse_file(char *file)
{
    int c;
    int pgp_part = 0;
    struct dictionary *d = dict;

    f = fopen(file, "r");
    if (f == NULL)
        die("Unable to load dice wordlist");

    for (;;)
    {
        c = fgetc(f);

        if (c == EOF)
            break;
        if (isspace(c))
            continue;
        if (c == '-')
        {
            pgp_part = read_pgp_tag();
            if (pgp_part < 0)
                die("Malformed PGP message in dice wordlist");

            continue;
        }

        /* We are in the message body */
        if (pgp_part == 0)
        {
            ungetc(c, f);
            if (parse_keyval(d++) < 0)
                die("Malformed word list");
        }
    }
    fclose(f);

}
Exemplo n.º 2
0
static void
log_send(struct evkeyvalq *output_headers, struct evbuffer *res_buf,
         thd_data *thd, struct evkeyvalq *get_args)
{
  uint64_t millisec;
  int threshold, limit;
  const char *callback, *types, *query, *client_id, *target_name,
             *learn_target_name;

  parse_keyval(get_args, &query, &types, &client_id, &target_name,
               &learn_target_name, &callback, &millisec, &threshold, &limit);

  /* send data to learn client */
  if (thd->zmq_sock && millisec && client_id && query && learn_target_name) {
    char c;
    size_t l;
    msgpack_packer pk;
    msgpack_sbuffer sbuf;
    int cnt, submit_flag = 0;

    msgpack_sbuffer_init(&sbuf);
    msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);

    cnt = 4;
    if (types && !strcmp(types, "submit")) {
      cnt++;
      types = NULL;
      submit_flag = 1;
    }
    msgpack_pack_map(&pk, cnt);

    c = 'i';
    msgpack_pack_raw(&pk, 1);
    msgpack_pack_raw_body(&pk, &c, 1);
    l = strlen(client_id);
    msgpack_pack_raw(&pk, l);
    msgpack_pack_raw_body(&pk, client_id, l);

    c = 'q';
    msgpack_pack_raw(&pk, 1);
    msgpack_pack_raw_body(&pk, &c, 1);
    l = strlen(query);
    msgpack_pack_raw(&pk, l);
    msgpack_pack_raw_body(&pk, query, l);

    c = 's';
    msgpack_pack_raw(&pk, 1);
    msgpack_pack_raw_body(&pk, &c, 1);
    msgpack_pack_uint64(&pk, millisec);

    c = 'l';
    msgpack_pack_raw(&pk, 1);
    msgpack_pack_raw_body(&pk, &c, 1);
    l = strlen(learn_target_name);
    msgpack_pack_raw(&pk, l);
    msgpack_pack_raw_body(&pk, learn_target_name, l);

    if (submit_flag) {
      c = 't';
      msgpack_pack_raw(&pk, 1);
      msgpack_pack_raw_body(&pk, &c, 1);
      msgpack_pack_true(&pk);
    }
    {
      zmq_msg_t msg;
      if (!zmq_msg_init_size(&msg, sbuf.size)) {
        memcpy((void *)zmq_msg_data(&msg), sbuf.data, sbuf.size);
        if (zmq_send(thd->zmq_sock, &msg, 0)) {
          print_error("zmq_send() error");
        }
        zmq_msg_close(&msg);
      }
    }
    msgpack_sbuffer_destroy(&sbuf);
  }
  /* make result */
  {
    int content_length;
    if (callback) {
      evhttp_add_header(output_headers,
                        "Content-Type", "text/javascript; charset=UTF-8");
      content_length = strlen(callback);
      evbuffer_add(res_buf, callback, content_length);
      evbuffer_add(res_buf, "(", 1);
      content_length += suggest_result(res_buf, types, query, target_name,
                                       threshold, limit,
                                       &(thd->cmd_buf), thd->ctx) + 3;
      evbuffer_add(res_buf, ");", 2);
    } else {
      evhttp_add_header(output_headers,
                        "Content-Type", "application/json; charset=UTF-8");
      content_length = suggest_result(res_buf, types, query, target_name,
                                      threshold, limit,
                                      &(thd->cmd_buf), thd->ctx);
    }
    if (content_length >= 0) {
      char num_buf[16];
      snprintf(num_buf, 16, "%d", content_length);
      evhttp_add_header(output_headers, "Content-Length", num_buf);
    }
  }
}
Exemplo n.º 3
0
int
initialize_context(struct ds_context *ctx, int ac, char *av[])
{
	int errors = 0;
	int opt;
	char mode;

	if ((ctx == NULL) || (ac < 2)) {
		errors++;
	}

	if (!errors) {
		ctx->filename = NULL;
		ctx->psize = PMEMOBJ_MIN_POOL;
		ctx->newpool = 0;
		ctx->pop = NULL;
		ctx->fileio = false;
		ctx->fmode = 0666;
		ctx->mode = 0;
		ctx->fd = -1;
	}

	if (!errors) {
		while ((opt = getopt(ac, av, "s:m:n:")) != -1) {
			switch (opt) {
			case 'm':
				mode = optarg[0];
				if (mode == 'f') {
					ctx->mode |= FILL;
				} else if (mode == 'd') {
					ctx->mode |= DUMP;
				} else if (mode == 'g') {
					ctx->mode |= GRAPH;
				} else if (mode == 'i') {
					ctx->mode |= INSERT;
					parse_keyval(ctx, av[optind], INSERT);
					optind++;
				} else if (mode == 's') {
					ctx->mode |= SEARCH;
					parse_keyval(ctx, av[optind], SEARCH);
					optind++;
				} else if (mode == 'r') {
					ctx->mode |= REMOVE;
					parse_keyval(ctx, av[optind], REMOVE);
					optind++;
				} else {
					errors++;
				}
				break;
			case 'n': {
				long insertions;
				insertions = strtol(optarg, NULL, 0);
				if (insertions > 0 && insertions < LONG_MAX) {
					ctx->insertions = insertions;
				}
				break;
			}
			case 's': {
				long poolsize;
				poolsize = strtol(optarg, NULL, 0);
				if (poolsize >= PMEMOBJ_MIN_POOL) {
					ctx->psize = poolsize;
				}
				break;
			}
			default:
				errors++;
				break;
			}
		}
	}

	if (!errors) {
		ctx->filename = strdup(av[optind]);
	}

	return errors;
}