Esempio n. 1
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. 2
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;
}