Пример #1
0
void write_vec_test()
{
    cbuf_t out;
    u_int8_t small_buf[10];
    u_int16_t i16;

    printf("write_vec_test1: binary BEGIN\n");
    cbuf_init(&out, small_buf, sizeof(small_buf), 0, CBUF_FLAG_BINARY);
    for (i16 = 1; i16 <= 20; i16++)
	cbuf_write(&out, &i16, sizeof(i16));
    cbuf_seg_add(&out);    
    for (i16 = 21; i16 <= 40; i16++)
	cbuf_write(&out, &i16, sizeof(i16));
    cbuf_seg_add(&out);    
    for (i16 = 41; i16 <= 60; i16++)
	cbuf_write(&out, &i16, sizeof(i16));
    cbuf_reset(&out, 0);
    print_u16(&out, 60);
    cbuf_print(&out, "out_i16");
    cbuf_final(&out);
    printf("write_vec_test1: binary END\n");    


    printf("write_vec_test2: BEGIN\n");
    cbuf_init(&out, small_buf, sizeof(small_buf), 0, 0);
    for (i16 = 1; i16 <= 20; i16++)
	cbuf_write(&out, &i16, sizeof(i16));
    cbuf_seg_add(&out);    
    for (i16 = 21; i16 <= 40; i16++)
	cbuf_write(&out, &i16, sizeof(i16));
    cbuf_seg_add(&out);    
    for (i16 = 41; i16 <= 60; i16++)
	cbuf_write(&out, &i16, sizeof(i16));
    cbuf_seg_add(&out);    
    for (i16 = 61; i16 <= 80; i16++)
	cbuf_write(&out, &i16, sizeof(i16));
    cbuf_seg_add(&out);    
    for (i16 = 81; i16 <= 100; i16++)
	cbuf_write(&out, &i16, sizeof(i16));
    cbuf_reset(&out, 0);
    print_u16(&out, 100);
    cbuf_print(&out, "out_i16");
    cbuf_final(&out);
    printf("write_vec_test2: END\n");    
}
Пример #2
0
struct dpl_conf_ctx *
dpl_conf_new(dpl_conf_cb_func_t cb_func,
             void *cb_arg)
{
  struct dpl_conf_ctx *ctx;

  ctx = malloc(sizeof (*ctx));
  if (NULL == ctx)
    return NULL;

  memset(ctx, 0, sizeof (*ctx));

  ctx->backslash = 0;
  ctx->comment = 0;
  ctx->quote = 0;
  ctx->cb_func = cb_func;
  ctx->cb_arg = cb_arg;

  cbuf_reset(&ctx->var_cbuf);
  cbuf_reset(&ctx->value_cbuf);
  ctx->cur_cbuf = &ctx->var_cbuf;

  return ctx;
}
Пример #3
0
dpl_status_t
dpl_conf_parse(struct dpl_conf_ctx *ctx,
               char *buf,
               int len)
{
  int i, ret;

  i = 0;
  while (i < len)
    {
      char c;

      c = buf[i];

      if (ctx->comment)
	{
	  if (c == '\n')
	    ctx->comment = 0;
	  else
	    i++;

	  continue ;
	}

      if (ctx->backslash)
	{
	  if (c == 'n')
	    c = '\n';
	  else
	    if (c == 'r')
	      c = '\r';
	    else
	      if (c == 't')
		c = '\t';

          ret = cbuf_add_char(ctx->cur_cbuf, c);
          if (-1 == ret)
            return DPL_FAILURE;

	  ctx->backslash = 0;
	  goto cont;
	}

      if (c == '\\')
	{
	  ctx->backslash = 1;
	  goto cont;
	}

      if (ctx->quote)
	{
	  if (c == '"')
	    ctx->quote = 0;
	  else
            {
              ret = cbuf_add_char(ctx->cur_cbuf, c);
              if (-1 == ret)
                return DPL_FAILURE;
            }

	  goto cont;
	}

      if (c == '"')
	{
	  ctx->quote = 1;
	  goto cont;
	}

      if (c == '#')
	{
	  ctx->comment = 1;
	  goto cont;
	}

      if (ctx->cur_cbuf != &ctx->value_cbuf)
	if (c == '=')
	  {
	    ctx->cur_cbuf = &ctx->value_cbuf;
	    goto cont;
	  }

      if (c == ' ' || c == '\t')
	goto cont;

      if (c == '\n' || c == ';')
	{
	  ret = ctx->cb_func(ctx->cb_arg,
                             ctx->var_cbuf.buf,
                             ctx->value_cbuf.buf);
          if (-1 == ret)
            return DPL_FAILURE;

	  cbuf_reset(&ctx->var_cbuf);
	  cbuf_reset(&ctx->value_cbuf);

	  ctx->cur_cbuf = &ctx->var_cbuf;
	  goto cont;
	}

      ret = cbuf_add_char(ctx->cur_cbuf, c);
      if (-1 == ret)
        return DPL_FAILURE;

    cont:
      i++;
    }

  return DPL_SUCCESS;
}
Пример #4
0
void write_buffer_test()
{
    cbuf_t out;
    u_int8_t small_buf[10];
    u_int8_t i8;
    u_int16_t i16;

    printf("write_buffer_test small_buf: BEGIN\n");
    cbuf_init(&out, small_buf, sizeof(small_buf), 0, 0);
    for (i8 = 1; i8 <= 10; i8++)
	cbuf_write(&out, &i8, sizeof(i8));
    cbuf_reset(&out, 0);
    print_u8(&out, 10);
    cbuf_print(&out, "out_i8");
    cbuf_final(&out);
    printf("write_buffer_test: small_buf: END\n");

    printf("write_buffer_test alloc_buf: BEGIN\n");
    cbuf_init(&out, small_buf, sizeof(small_buf), 0, 0);
    for (i8 = 1; i8 <= 20; i8++)
	cbuf_write(&out, &i8, sizeof(i8));
    cbuf_reset(&out, 0);
    print_u8(&out, 20);
    cbuf_print(&out, "out_i8");
    cbuf_final(&out);
    printf("write_buffer_test: alloc_buf: END\n");

    printf("write_buffer_test realloc_buf: BEGIN\n");
    cbuf_init(&out, small_buf, sizeof(small_buf), 0, 0);
    cbuf_print(&out, "out_i16");
    for (i16 = 1; i16 <= 200; i16++)
	cbuf_write(&out, &i16, sizeof(i16));
    cbuf_reset(&out, 0);
    print_u16(&out, 200);
    cbuf_print(&out, "out_i16");
    cbuf_final(&out);
    printf("write_buffer_test: realloc_buf: END\n");

    // the same with empty inital buffer
    printf("write_buffer_test realloc_buf2: BEGIN\n");
    cbuf_init(&out, 0, 0, 0, 0);
    cbuf_print(&out, "out_i16");
    for (i16 = 1; i16 <= 200; i16++)
	cbuf_write(&out, &i16, sizeof(i16));
    cbuf_reset(&out, 0);
    print_u16(&out, 200);
    cbuf_print(&out, "out_i16");
    cbuf_final(&out);
    printf("write_buffer_test: realloc_buf2: END\n");

    printf("write_buffer_test binary_realloc_buf: BEGIN\n");
    cbuf_init(&out, small_buf, sizeof(small_buf), 0, CBUF_FLAG_BINARY);
    cbuf_print(&out, "out_i16");
    for (i16 = 1; i16 <= 200; i16++)
	cbuf_write(&out, &i16, sizeof(i16));
    cbuf_reset(&out, 0);
    print_u16(&out, 200);
    cbuf_print(&out, "out_i16");
    cbuf_final(&out);
    printf("write_buffer_test: binary_realloc_buf: END\n");

    

}
Пример #5
0
/*! Copy one configuration object to antother
 *
 * Works for objects that are items ina yang list with a keyname, eg as:
 *   list sender{ 
 *      key name;	
 *	leaf name{...
 *
 * @param[in]  h    CLICON handle
 * @param[in]  cvv  Vector of variables from CLIgen command-line
 * @param[in]  argv Vector: <db>, <xpath>, <field>, <fromvar>, <tovar>
 * Explanation of argv fields:
 *  db:     Database name, eg candidate|tmp|startup
 *  xpath:  XPATH expression with exactly two %s pointing to field and from name
 *  field:  Name of list key, eg name
 *  fromvar:Name of variable containing name of object to copy from (given by xpath)
 *  tovar:  Name of variable containing name of object to copy to.
 * @code
 * cli spec:
 *  copy snd <n1:string> to <n2:string>, cli_copy_config("candidate", "/sender[%s='%s']", "from", "n1", "n2");
 * cli command:
 *  copy snd from to to
 * @endcode
 */
int
cli_copy_config(clicon_handle h, 
		cvec         *cvv, 
		cvec         *argv)
{
    int          retval = -1;
    char        *db;
    cxobj       *x1 = NULL; 
    cxobj       *x2 = NULL; 
    cxobj       *x;
    char        *xpath;
    int          i;
    int          j;
    cbuf        *cb = NULL;
    char        *keyname;
    char        *fromvar;
    cg_var      *fromcv;
    char        *fromname = NULL;
    char        *tovar;
    cg_var      *tocv;
    char        *toname;
    cxobj       *xerr;

    if (cvec_len(argv) != 5){
	clicon_err(OE_PLUGIN, 0, "Requires four elements: <db> <xpath> <keyname> <from> <to>");
	goto done;
    }
    /* First argv argument: Database */
    db = cv_string_get(cvec_i(argv, 0));
    /* Second argv argument: xpath */
    xpath = cv_string_get(cvec_i(argv, 1));
    /* Third argv argument: name of keyname */
    keyname = cv_string_get(cvec_i(argv, 2));
    /* Fourth argv argument: from variable */
    fromvar = cv_string_get(cvec_i(argv, 3));
    /* Fifth argv argument: to variable */
    tovar = cv_string_get(cvec_i(argv, 4));
    
    /* Get from variable -> cv -> from name */
    if ((fromcv = cvec_find(cvv, fromvar)) == NULL){
	clicon_err(OE_PLUGIN, 0, "fromvar '%s' not found in cligen var list", fromvar);	
	goto done;
    }
    /* Get from name from cv */
    fromname = cv_string_get(fromcv);
    /* Create xpath */
    if ((cb = cbuf_new()) == NULL){
	clicon_err(OE_PLUGIN, errno, "cbuf_new");	
	goto done;
    }
    /* Sanity check that xpath contains exactly two %s, ie [%s='%s'] */
    j = 0;
    for (i=0; i<strlen(xpath); i++){
	if (xpath[i] == '%')
	    j++;
    }
    if (j != 2){
	clicon_err(OE_PLUGIN, 0, "xpath '%s' does not have two '%%'", xpath);	
	goto done;
    }
    cprintf(cb, xpath, keyname, fromname);	
    /* Get from object configuration and store in x1 */
    if (clicon_rpc_get_config(h, db, cbuf_get(cb), &x1) < 0)
	goto done;
    if ((xerr = xpath_first(x1, "/rpc-error")) != NULL){
	clicon_rpc_generate_error("Get configuration", xerr);
	goto done;
    }

    /* Get to variable -> cv -> to name */
    if ((tocv = cvec_find(cvv, tovar)) == NULL){
	clicon_err(OE_PLUGIN, 0, "tovar '%s' not found in cligen var list", tovar);
	goto done;
    }
    toname = cv_string_get(tocv);
    /* Create copy xml tree x2 */
    if ((x2 = xml_new("new", NULL, NULL)) == NULL)
	goto done;
    if (xml_copy(x1, x2) < 0)
	goto done;
    xml_name_set(x2, "config");
    cprintf(cb, "/%s", keyname);	
    if ((x = xpath_first(x2, "%s", cbuf_get(cb))) == NULL){
	clicon_err(OE_PLUGIN, 0, "Field %s not found in copy tree", keyname);
	goto done;
    }
    x = xml_find(x, "body");
    xml_value_set(x, toname);
    /* resuse cb */
    cbuf_reset(cb);
    /* create xml copy tree and merge it with database configuration */
    clicon_xml2cbuf(cb, x2, 0, 0);
    if (clicon_rpc_edit_config(h, db, OP_MERGE, cbuf_get(cb)) < 0)
	goto done;
    retval = 0;
 done:
    if (cb)
	cbuf_free(cb);
    if (x1 != NULL)
	xml_free(x1);
    if (x2 != NULL)
	xml_free(x2);
    return retval;
}