Esempio n. 1
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;
}
Esempio n. 2
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;
}
Esempio n. 3
0
/*! Take a pattern and expand all <variables> with option 'choice' or 'expand'. 
 * The pattern is expanded by examining the objects they point 
 * to: those objects that are expand or choice variables
 * (eg <string expand:foo>) are transformed into a set of new commands
 * with a reference point back to the original.
 * @param[in]  h       cligen handle
 * @param[in]  ptr     original parse-tree consisting of a vector of cligen objects
 * @param[in]  hide    Respect hide setting of commands (dont show)
 * @param[in]  expandvar Set if VARS should be expanded, eg ? <tab>
 * @param[out] ptn     shadow parse-tree initially an empty pointer, its value is returned.
 */
int
pt_expand_2(cligen_handle h, 
	    parse_tree   *ptr, 
	    cvec         *cvv,
	    int           hide,
	    int           expandvar,
	    parse_tree   *ptn)
{
    int          i;
    cg_obj      *co;
    cg_obj      *parent = NULL;
    int          retval = -1;

    ptn->pt_len = 0;
    ptn->pt_vec = NULL;
    if (ptr->pt_vec == NULL)
	return 0;
    for (i=0; i<ptr->pt_len; i++){ /* Build ptn (new) from ptr (orig) */
	if ((co = ptr->pt_vec[i]) != NULL){
	    if (co_value_set(co, NULL) < 0)
		goto done;
	    if (hide && co->co_hide)
		continue;
	    /*
	     * Choice variable - Insert the static choices as commands in place
	     * of the variable
	     */
	    if (co->co_type == CO_VARIABLE && co->co_choice != NULL){
		if (pt_expand_choice(co, ptn, parent) < 0)
		    goto done;
	    }
	    /* Expand variable - call expand callback and insert expanded
	     * commands in place of the variable
	     */
	    else if (co->co_type == CO_VARIABLE && 
		     co->co_expandv_fn != NULL){
#ifdef EXPAND_ONLY_INTERACTIVE
		/* If I add conditional here, you need to explicitly have a
		 * a "free" variable expression, not just expands.
		 * eg (<v:int expand_dbvar()>|<v:int>)
		 * If I put the conditional in the if-statement above you
		 * may then get two variables and ambiguous command
		 * MAYBE you could see if there are any other same variables on
		 * this iteration and if not add it?
		 */
		if (expandvar)
#endif
		    if (pt_expand_fnv(h, co, cvv, ptn, parent) < 0)
			goto done;
	    }
	    else{
		/* Copy vector element */
		pt_realloc(ptn);

		/* Copy original cg_obj to shadow list*/
		if (co_expand_sub(co, parent, 
				  &ptn->pt_vec[ptn->pt_len-1]) < 0)
		    goto done;
		/* Reference old cg_obj */
		//		  con = ptn->pt_vec[ptn->pt_len-1];
	    }
	}
	else{ 
	    pt_realloc(ptn); /* empty child */
	}
    } /* for */
    cligen_parsetree_sort(*ptn, 1);
    if (cligen_logsyntax(h) > 0){
	fprintf(stderr, "%s:\n", __FUNCTION__);
	cligen_print(stderr, *ptn, 0);
    }
    retval = 0;
 done:
    return retval;
}
Esempio n. 4
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;
}