Ejemplo n.º 1
0
/*! Transform string variables to commands
 * Expansion of choice or expand takes a variable (<expand> <choice>)
 * and transform them to a set of commands: <string>...<string>
 * @param[in]  co        The variable to transform to a command
 * @param[in]  cmd       Command name
 * @param[out] helptext  Helptext of command
 */
int
transform_var_to_cmd(cg_obj *co, 
		     char   *cmd, 
		     char   *helptext)
{
    if (co->co_command)
	free(co->co_command);
    co->co_command = cmd; 
    if (helptext){
	if (co->co_help)
	    free(co->co_help);
	co->co_help = helptext; 
    }
    if (co->co_expandv_fn)
	co->co_expandv_fn = NULL;
    if (co->co_expand_fn_str){
	free(co->co_expand_fn_str);
	co->co_expand_fn_str = NULL;
    }
    if (co->co_expand_fn_vec){
	cvec_free(co->co_expand_fn_vec);
	co->co_expand_fn_vec = NULL;
    }
    if (co->co_translate_fn_str){
	free(co->co_translate_fn_str);
	co->co_translate_fn_str = NULL;
    }
    if (co->co_show){
	free(co->co_show);
	co->co_show = NULL;
    }
    if (co->co_rangecvv_low){
	cvec_free(co->co_rangecvv_low);
	co->co_rangecvv_low = NULL;
    }
    if (co->co_rangecvv_upp){
	cvec_free(co->co_rangecvv_upp);
	co->co_rangecvv_upp = NULL;
    }
    if (co->co_choice){
	free(co->co_choice);
	co->co_choice = NULL;
    }
    if (co->co_regex){
	cvec_free(co->co_regex);
	co->co_regex = NULL;
    }
    co->co_type = CO_COMMAND;
    return 0;
}
Ejemplo n.º 2
0
/*! Call expand callback and insert expanded commands in place of variable
 * variable argument callback variant
 * @see pt_expand_fn
 */
static int
pt_expand_fnv(cligen_handle h, 
	      cg_obj       *co,     
	      cvec         *cvv,
	      parse_tree   *ptn,
	      cg_obj       *parent)
{
    int     retval = -1;
    cvec   *commands = cvec_new(0);
    cvec   *helptexts = cvec_new(0);
    cg_var *cv = NULL;
    char   *helpstr;
    cg_obj *con;
    int     i;

    if ((*co->co_expandv_fn)(
			     cligen_userhandle(h)?cligen_userhandle(h):h, 
			     co->co_expand_fn_str, 
			     cvv,
			     co->co_expand_fn_vec,
			     commands, 
			     helptexts) < 0)
	goto done;
    i = 0;
    while ((cv = cvec_each(commands, cv)) != NULL) {
	if (i < cvec_len(helptexts)){
	    helpstr = strdup(cv_string_get(cvec_i(helptexts, i)));
	}
	else
	    helpstr = NULL;
	i++;
	pt_realloc(ptn);
	if (co_expand_sub(co, parent, 
			  &ptn->pt_vec[ptn->pt_len-1]) < 0)
	    goto done;
	con = ptn->pt_vec[ptn->pt_len-1];
	if (transform_var_to_cmd(con, 
				 strdup(cv_string_get(cv)),
				 helpstr) < 0)
	    goto done;
    }
    if (commands)
	cvec_free(commands);
    if (helptexts)
	cvec_free(helptexts);
    retval = 0;
 done:
    return retval;

}
Ejemplo n.º 3
0
/**
 @brief    密行列AをHessenberg型行列Bに相似変換する.
 @param[in]  n    正方行列A,Bのサイズ
 @param[in]  A    行列
 @param[in]  LDA  Aの第1次元
 @param[out] B    相似変換により得られたHessenberg型行列
 @param[in]  LDB  Bの第1次元
 */
void chsnbrg_simtr(int n, cmulti **B, int LDB, cmulti **A, int LDA)
{
  int i,prec=53;
  rmulti *alpha=NULL;
  cmulti **h=NULL;
  // allocate
  prec=cmat_get_prec_max(n,n,B,LDB);
  alpha=rallocate_prec(prec);
  h=cvec_allocate_prec(n,prec);
  // copy
  cmat_copy(n,n,B,LDB,A,LDA);
  // compute
  for(i=0; i<n-2; i++){
    // Householder vector
    chouseholder_vec(n,i+1,h,alpha,&COL(B,i,LDB));
    // similarity transformation
    chouseholder_right(n,n,B,LDB,B,LDB,i+1,h,alpha);
    chouseholder_left(n,n,B,LDB,B,LDB,i+1,h,alpha);
    // set lower part as zeros
    cvec_set_zeros(n-2-i,&MAT(B,i+2,i,LDB));
  }
  // done
  alpha=rfree(alpha);
  h=cvec_free(n,h);
}
Ejemplo n.º 4
0
static PyObject *
_cli_set(PyObject *args)
{
    PyObject *handle;
    PyObject *vars;
    PyObject *arg;
    PyObject *Cv;
    PyObject *Vr;
    cg_var *cv;
    cvec *vr = NULL;
    int retval = -1;
    clicon_handle h;

    if (!PyArg_ParseTuple(args, "OOO", &handle, &vars, &arg))
        return NULL;
    
    h = PyCapsule_GetPointer(handle, NULL);

    if ((Vr = PyObject_CallMethod(vars, "__cvec_from_Cvec", NULL)) == NULL)
      return NULL;
    vr = PyCapsule_GetPointer(Vr, NULL);
    Py_XDECREF(Vr);

    if ((Cv = PyObject_CallMethod(arg, "__cv", NULL)) == NULL)
      return NULL;
    cv = PyCapsule_GetPointer(Cv, NULL);
    Py_XDECREF(Cv);

    retval = cli_set(h, vr, cv);
    cvec_free(vr);

    return PyLong_FromLong(retval);
}
Ejemplo n.º 5
0
void func_cvec_p_resize(func_t *f, int n)
{
  if(func_ptype(f)!=FUNC_P_CVEC){ FUNC_ERROR_ARG1("func_cvec_p_resize",f); }
  if(f->p.cvec==NULL){ func_cvec_p_new(f); }
  if(n>0){
    f->p.cvec->x=cvec_free(f->p.cvec->n,f->p.cvec->x);
    f->p.cvec->n=n;
    f->p.cvec->x=(cmulti**)cvec_allocate(n);
  }
}
Ejemplo n.º 6
0
void func_cvec_p_del(func_t *f)
{
  if(func_ptype(f)!=FUNC_P_CVEC){ FUNC_ERROR_ARG1("func_cvec_p_del",f); }
  if(f->p.cvec!=NULL){
    f->p.cvec->x=cvec_free(f->p.cvec->n,f->p.cvec->x);
    free(f->p.cvec);
    f->p.cvec=NULL;
    f->ptype=FUNC_P_NULL;
  }
}
Ejemplo n.º 7
0
/*! Create a new vector, initialize the first element to the contents of 'var'
 *
 * @param[in] var      cg_var to clone and add to vector
 * @retval    cvec     allocated cvec
 */
cvec *
cvec_from_var(cg_var *cv)
{
    cvec   *newvec = NULL;
    cg_var *tail = NULL;

    if (cv && (newvec = cvec_new(0))) {
        if ((tail = cvec_append_var(newvec, cv)) == NULL) {
            cvec_free(newvec);
            newvec = NULL;
        }
    }
    return newvec;
}
Ejemplo n.º 8
0
/**
 @brief    多倍長Hessenbergに相似変換する(mステップで終了)
 @details  Aは書き換え,cmat型
 @param[in]  n    行列サイズ
 @param[in]  A    行列
 @param[in]  LDA 
 @param[in]  step 相似変換ステップ数(m-2が上限)
 @param[out] A    hessenberg行列
 */
void chsnbrg_simtr_step(int n, cmulti **A, int LDA, int step)
{
  int prec,i;
  rmulti *alpha=NULL;
  cmulti **h=NULL;
  if(step<=0 || step>(n-2)){ printf("ERROR! Necessary step =< n-2 \n"); exit(0); }
  // allocate
  prec=cmat_get_prec_max(n,n,A,LDA);
  alpha=rallocate_prec(prec);
  h=cvec_allocate_prec(n,prec);
  // compute
  for(i=0; i<step; i++){
    chouseholder_vec(n,i+1,h,alpha,&COL(A,i,LDA));
    chouseholder_right(n,n,A,LDA,A,LDA,i+1,h,alpha);
    chouseholder_left(n,n,A,LDA,A,LDA,i+1,h,alpha);
  }
  // done
  alpha=rfree(alpha);
  h=cvec_free(n,h);
}
Ejemplo n.º 9
0
/* Main */
int
main(int argc, char *argv[])
{
    cligen_handle   h;
    int             retval = -1;
    parse_tree     *pt;
    FILE           *f = stdin;
    char           *argv0 = argv[0];
    char           *filename=NULL;
    cvec           *globals;   /* global variables from syntax */
    char           *str;

    argv++;argc--;
    for (;(argc>0)&& *argv; argc--, argv++){
        if (**argv != '-')
            break;
        (*argv)++;
        if (strlen(*argv)==0)
            usage(argv0);
        switch(**argv) {
        case 'h': /* help */
            usage(argv0); /* usage exits */
            break;
        case 'f' : 
            argc--;argv++;
            filename = *argv;
            if ((f = fopen(filename, "r")) == NULL){
		fprintf(stderr, "fopen %s: %s\n", filename, strerror(errno));
                exit(1);
            }
            break;
        default:
            usage(argv0);
            break;
        }
  }
    if ((h = cligen_init()) == NULL)
        goto done;    
    if ((globals = cvec_new(0)) == NULL)
	goto done;
    if (cligen_parse_file(h, f, filename, NULL, globals) < 0)
        goto done;
    pt = NULL;
    while ((pt = cligen_tree_each(h, pt)) != NULL) {
	if (cligen_callback_str2fn(*pt, str2fn, NULL) < 0) /* map functions */
	    goto done;
	if (cligen_expand_str2fn(*pt, str2fn_exp, NULL) < 0)
	    goto done;
    }
    if ((str = cvec_find_str(globals, "prompt")) != NULL)
        cligen_prompt_set(h, str);
    if ((str = cvec_find_str(globals, "comment")) != NULL)
        cligen_comment_set(h, *str);
    if ((str = cvec_find_str(globals, "tabmode")) != NULL)
	cligen_tabmode_set(h, strcmp(str,"long") == 0);
    cvec_free(globals);
    pt = NULL;
    while ((pt = cligen_tree_each(h, pt)) != NULL) {
	printf("Syntax:\n");
	cligen_print(stdout, *pt, 0);
    }
    fflush(stdout);

    if (cligen_loop(h) < 0)
	goto done;
    retval = 0;
  done:
    fclose(f);
    cligen_exit(h);
    return retval;
}
Ejemplo n.º 10
0
/* Main */
int
main(int argc, char *argv[])
{
    int         retval = -1;
    parse_tree *pt;
    FILE       *f = stdin;
    char       *argv0 = argv[0];
    char       *filename=NULL;
    cvec       *globals;   /* global variables from syntax */
    cligen_handle  h;
    char       *str;
    int         quit = 0;
    int         print_syntax = 0;

    argv++;argc--;
    for (;(argc>0)&& *argv; argc--, argv++){
	if (**argv != '-')
	    break;
	(*argv)++;
	if (strlen(*argv)==0)
	    usage(argv0);
	switch(**argv) {
	case 'h': /* help */
	    usage(argv0); /* usage exits */
	    break;
	case 'q': /* quit directly */
	    quit++;
	    break;
	case 'p': /* print syntax */
	    print_syntax++;
	    break;
	case 'f' : 
	    argc--;argv++;
	    filename = *argv;
	    if ((f = fopen(filename, "r")) == NULL){
		fprintf(stderr, "fopen(%s): %s\n", filename, strerror(errno));
		exit(1);
	    }
	    break;
	default:
	    usage(argv0);
	    break;
	}
  }
    if ((h = cligen_init()) == NULL)
	goto done;    
    cligen_lexicalorder_set(h, 1);
    cligen_ignorecase_set(h, 1);
//    cligen_parse_debug(1);
    if ((globals = cvec_new(0)) == NULL)
	goto done;
    if (cligen_parse_file(h, f, filename?filename:"stdin", NULL, globals) < 0)
	goto done;
    pt = cligen_tree_i(h, 0); 

    /* map functions */
    if (pt && cligen_callbackv_str2fn(*pt, str2fn, NULL) < 0)     
	goto done;
    if ((str = cvec_find_str(globals, "prompt")) != NULL)
	cligen_prompt_set(h, str);
    if ((str = cvec_find_str(globals, "tabmode")) != NULL)
	if (strcmp(str,"long") == 0)
	    cligen_tabmode_set(h, CLIGEN_TABMODE_COLUMNS);
    if ((str = cvec_find_str(globals, "comment")) != NULL)
	cligen_comment_set(h, *str);
    if ((str = cvec_find_str(globals, "mode")) != NULL)
	cligen_tree_active_set(h, str);
    cvec_free(globals);

    if (print_syntax){
	cligen_print(stdout, *pt, 0);
	fflush(stdout);
    }
    if (quit)
	goto done;
    if (cligen_loop(h) < 0)
	goto done;
    retval = 0;
  done:
    fclose(f);
    if (h)
	cligen_exit(h);
    return retval;
}
Ejemplo n.º 11
0
/*
 * generic_validate
 *
 * key values are checked for validity independent of user-defined callbacks
 * They are checked as follows:
 * 1. If no value and default value defined, add it.
 * 2. If no value and mandatory flag set in spec, report error.
 * 3. Validate value versus spec, and report error if no match. Currently only int ranges and
 *    string regexp checked.
 */
static int
generic_validate(clicon_handle h, char *dbname, const struct dbdiff *dd)
{
    int             i, j;
    char           *key;
    cvec           *cvec = NULL;
    cg_var         *cv;
    cg_varspec     *cs;
    int             retval = -1;
    cg_obj         *co;
    cg_obj         *cov;
    parse_tree     *dbspec_co;
    char           *reason = NULL;
    parse_tree     *pt;

    if ((dbspec_co = clicon_dbspec_pt(h)) == NULL)
	goto done;
    /* dd->df_ents[].dfe_key1 (running),  
       dd->df_ents[].dfe_key2 (candidate) */
    for (i = 0; i < dd->df_nr; i++) {
	if ((key = dd->df_ents[i].dfe_key2) == NULL)
	    continue;
	if ((co = key2spec_co(dbspec_co, key)) == NULL)
	    continue;
	/* read variable list from db */
	if ((cvec = dbkey2cvec(dbname, key)) == NULL) 
	    goto done;
	/* Loop over all co:s children (spec) and check if actual values
	   in db(cv) satisfies them */
	pt = &co->co_pt;
	for (j=0; j<pt->pt_len; j++){
	    if ((cov = pt->pt_vec[j]) == NULL)
		continue;
	    if (cov->co_type == CO_VARIABLE){

		/* There is no db-value, but dbspec has default value */
		if ((cv = dbspec_default_get(cov)) != NULL &&
		    cvec_find(cvec, cov->co_command) == NULL){
		    cv_flag_set(cv, V_DEFAULT); /* mark it as default XXX not survive DB */
		    /* add default value to cvec */
		    if (cvec_add_cv(cvec, cv) < 0){
			clicon_err(OE_CFG, 0, "cvec_add_cv");
			goto done;
		    }
		    /* Write to database */
		    if (cvec2dbkey(dbname, key, cvec) < 0)
			goto done;
		}
		else
		if (!dbspec_optional_get(cov) && cvec_find(cvec, cov->co_command) == NULL){
		    clicon_err(OE_CFG, 0, 
			       "key %s: Missing mandatory variable: %s\n",
			       key, cov->co_command);
		    goto done;
		}
	    }
	}
	cv = NULL;
	/* Loop over all actual db/cv:s och check their validity */	
	while ((cv = cvec_each(cvec, cv))) {
	    if ((cov = co_find_one(*pt, cv_name_get(cv))) == NULL){
		clicon_err(OE_CFG, 0, "key %s: variable %s not found in co-spec", 
			   key, cv_name_get(cv));
		goto done;
	    }
	    if ((cs = co2varspec(cov)) == NULL)
		continue;
	    if (cv_validate(cv, cs, &reason) != 1){ /* We ignore errors */
		clicon_err(OE_DB, 0, 
			   "key %s: validation of %s failed\n",
			   key, cov->co_command);
		goto done;
	    }

	}
	if (cvec){
	    cvec_free(cvec);
	    cvec = NULL;
	}
    } /* for */
    retval = 0;
  done:
    if (cvec)
	cvec_free(cvec);
    if (reason)
	free(reason);
    return retval;
}
Ejemplo n.º 12
0
/* Main */
int
main(int argc, char *argv[])
{
    int         retval = -1;
    parse_tree  pt = {0,};
    int         callback_ret = 0;
    FILE       *f = stdin;
    char       *argv0 = argv[0];
    char       *line;
    char       *filename=NULL;
    cvec       *globals;   /* global variables from syntax */
    cligen_handle  h;
    char       *str;
    char       *treename;
    int         quit = 0;
    int         print_syntax = 0;

    argv++;argc--;
    for (;(argc>0)&& *argv; argc--, argv++){
	if (**argv != '-')
	    break;
	(*argv)++;
	if (strlen(*argv)==0)
	    usage(argv0);
	switch(**argv) {
	case 'h': /* help */
	    usage(argv0); /* usage exits */
	    break;
	case 'q': /* quit directly */
	    quit++;
	    break;
	case 'p': /* print syntax */
	    print_syntax++;
	    break;
	case 'f' : 
	    argc--;argv++;
	    filename = *argv;
	    if ((f = fopen(filename, "r")) == NULL){
		fprintf(stderr, "fopen(%s): %s\n", filename, strerror(errno));
		exit(1);
	    }
	    break;
	default:
	    usage(argv0);
	    break;
	}
  }
    if ((h = cligen_init()) == NULL)
	goto done;    
    cligen_lexicalorder_set(h, 1);
    cligen_ignorecase_set(h, 1);
//    cligen_parse_debug(1);
    if ((globals = cvec_new(0)) == NULL)
	goto done;
    if (cligen_parse_file(h, f, filename?filename:"stdin", &pt, globals) < 0)
	goto done;

    /* map functions */
    if (cligen_callback_str2fn(pt, str2fn, NULL) < 0)     
	goto done;
//    if (cligen_expand_register(pt, cli_expand_fn) < 0)     
//	goto done;
    if (cligen_expand_str2fn(pt, (expand_str2fn_t *)expand_str2fn, NULL) < 0)     
	goto done;
    if ((str = cvec_find_str(globals, "prompt")) != NULL)
	cligen_prompt_set(h, str);
    if ((str = cvec_find_str(globals, "tabmode")) != NULL)
	cligen_tabmode_set(h, strcmp(str,"long") == 0);
    if ((str = cvec_find_str(globals, "comment")) != NULL)
	cligen_comment_set(h, *str);
    if ((treename = cvec_find_str(globals, "name")) == NULL)
	treename = "tree0";
    cligen_tree_active_set(h, treename); 
    cligen_tree_add(h, treename, pt); 
    cvec_free(globals);
    if (quit)
	goto done;
    if (print_syntax){
	printf("Syntax:\n");
	cligen_print(stdout, pt, 0);
	fflush(stdout);
    }

    /* Run the CLI command interpreter */
    while (!cligen_exiting(h)){
	switch (cliread_eval(h, &line, &callback_ret)){
	case CG_EOF: /* eof */
	    goto done;
	    break;
	case CG_ERROR: /* cligen match errors */
	    printf("CLI read error\n");
	    goto done;
	case CG_NOMATCH: /* no match */
	    printf("CLI syntax error in: \"%s\": %s\n", line, cligen_nomatch(h));
	    break;
	case CG_MATCH: /* unique match */
	    if (callback_ret < 0)
		printf("CLI callback error\n");
	    break;
	default: /* multiple matches */
	    printf("Ambigous command\n");
	    break;
	}
    }
    retval = 0;
  done:
    fclose(f);
    cligen_exit(h);
    return retval;
}