Esempio n. 1
0
bool Output::execute()
{
	init_proc();
	//The post-increment operator is slower than the pre-increment, don't use it unless you have a reason
	/* Also using a for to increment the counter_ doesn't work, not entirely sure why but it seems
	 * the increment is not executed when we return to the function after the yield().
	 */
	while (counter_ < 10)
	{
		std::cout << name_ << " is executing for the " << ++counter_ << " time" << std::endl;
		if (sleep_)
			sleep(1);
		else
			yield();
	}
	end_proc();
	return false;
}
Esempio n. 2
0
/*
 * This is the top level function for generating the tk script.
 */
void dump_tk_script( struct kconfig * scfg )
{
    int menu_depth;
    int menu_num [64];
    int imenu, i;
    int top_level_num = 0;
    struct kconfig * cfg;
    struct kconfig * cfg1 = NULL;
    const char * name = "No Name";

    /*
     * Mark begin and end of each menu so I can omit submenus when walking
     * over a parent menu.
     */
    tot_menu_num = 0;
    menu_depth   = 0;
    menu_num [0] = 0;

    for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
    {
	switch ( cfg->token )
	{
	default:
	    break;

	case token_mainmenu_name:
	    name = cfg->label;
	    break;

	case token_mainmenu_option:
	    if ( ++menu_depth >= 64 )
		{ fprintf( stderr, "menus too deep\n" ); exit( 1 ); }
	    if ( ++tot_menu_num >= 100 )
		{ fprintf( stderr, "too many menus\n" ); exit( 1 ); }
	    menu_num   [menu_depth]   = tot_menu_num;
	    menu_first [tot_menu_num] = cfg;
	    menu_last  [tot_menu_num] = cfg;
	    /*
	     * Note, that menu_number is set to the number of parent 
	     * (upper level) menu.
	     */
	    cfg->menu_number = menu_num[menu_depth - 1];
	    if ( menu_depth == 1 )
		++top_level_num;
	    break;

	case token_endmenu:
	    menu_last [menu_num [menu_depth]] = cfg;
	    /* flatten menus with proper scoping */
	    if ( --menu_depth < 0 )
		{ fprintf( stderr, "unmatched endmenu\n" ); exit( 1 ); }
	    break;

	case token_bool:
	case token_choice_header:
	case token_choice_item:
	case token_comment:
	case token_dep_bool:
	case token_dep_tristate:
	case token_dep_mbool:
	case token_hex:
	case token_int:
	case token_string:
	case token_tristate:
	    cfg->menu_number = menu_num[menu_depth];
	    if ( menu_depth == 0 )
		{ fprintf( stderr, "statement not in menu\n" ); exit( 1 ); }
	    break;

	case token_define_bool:
	case token_define_hex:
	case token_define_int:
	case token_define_string:
	case token_define_tristate:
	case token_unset:
	    cfg->menu_number = menu_num[menu_depth];
	    break;
	}
    }

    /*
     * Generate menus per column setting.
     * There are:
     *   four extra buttons for save/quit/load/store;
     *   one blank button
     *   add two to round up for division
     */
    printf( "set menus_per_column %d\n", (top_level_num + 4 + 1 + 2) / 3 );
    printf( "set total_menus %d\n\n", tot_menu_num );

    printf( "proc toplevel_menu {num} {\n" );
    for ( imenu = 1; imenu <= tot_menu_num; ++imenu )
    {
	int parent = 1;

	if ( menu_first[imenu]->menu_number == 0 )
	    parent = menu_first[imenu]->menu_number;
	else
	    printf( "\tif {$num == %d} then {return %d}\n",
		imenu, menu_first[imenu]->menu_number );
    }
    printf( "\treturn $num\n}\n\n" );

    /*
     * Generate the menus.
     */
    printf( "mainmenu_name \"%s\"\n", name );
    for ( imenu = 1; imenu <= tot_menu_num; ++imenu )
    {
	int menu_line = 0;
	int nr_submenu = imenu;
	int menu_name_omitted = 0;
	int opt_count = 0;

	clear_globalflags();
	start_proc( menu_first[imenu]->label, imenu, 
		!menu_first[imenu]->menu_number );

	for ( cfg = menu_first[imenu]->next; cfg != NULL && cfg != menu_last[imenu]; cfg = cfg->next )
	{
	    switch ( cfg->token )
	    {
	    default:
		break;

	    case token_mainmenu_option:
		while ( menu_first[++nr_submenu]->menu_number > imenu )
		    ;
		cfg->menu_line = menu_line++;
		printf( "\tsubmenu $w.config.f %d %d \"%s\" %d\n",
		    cfg->menu_number, cfg->menu_line, cfg->label, nr_submenu );
		cfg = menu_last[nr_submenu];
		break;

	    case token_comment:
		if ( !cfg->menu_line && !menu_name_omitted )
		{
		    cfg->menu_line = -1;
		    menu_name_omitted = 1;
		}
		else
		{
		    menu_name_omitted = 1;
		    cfg->menu_line = menu_line++;
		    printf( "\tcomment $w.config.f %d %d \"%s\"\n",
			cfg->menu_number, cfg->menu_line, cfg->label );
		}
		break;

	    case token_bool:
		cfg->menu_line = menu_line++;
		printf( "\tbool $w.config.f %d %d \"%s\" %s\n",
		    cfg->menu_number, cfg->menu_line, cfg->label,
		    vartable[cfg->nameindex].name );
		break;

	    case token_choice_header:
		/*
		 * I need the first token_choice_item to pick out the right
		 * help text from Documentation/Configure.help.
		 */
		cfg->menu_line = menu_line++;
		printf( "\tglobal tmpvar_%d\n", -(cfg->nameindex) );
		printf( "\tminimenu $w.config.f %d %d \"%s\" tmpvar_%d %s\n",
		    cfg->menu_number, cfg->menu_line, cfg->label,
		    -(cfg->nameindex), vartable[cfg->next->nameindex].name );
		printf( "\tmenu $w.config.f.x%d.x.menu -tearoffcommand \"menutitle \\\"%s\\\"\"\n",
		    cfg->menu_line, cfg->label );
		cfg1 = cfg;
		opt_count = 0;
		break;

	    case token_choice_item:
		/* note: no menu line; uses choice header menu line */
		printf( "\t$w.config.f.x%d.x.menu add radiobutton -label \"%s\" -variable tmpvar_%d -value \"%s\" -command \"update_active\"\n",
		    cfg1->menu_line, cfg->label, -(cfg1->nameindex),
		    cfg->label );
		opt_count++;
		if ( cfg->next && cfg->next->token != token_choice_item ) {
		    /* last option in the menu */
		    printf( "\tmenusplit $w $w.config.f.x%d.x.menu %d\n",
			cfg1->menu_line, opt_count );
		}
		break;

	    case token_dep_bool:
	    case token_dep_mbool:
		cfg->menu_line = menu_line++;
		printf( "\tdep_bool $w.config.f %d %d \"%s\" %s\n",
		    cfg->menu_number, cfg->menu_line, cfg->label,
		    vartable[cfg->nameindex].name );
		break;

	    case token_dep_tristate:
		cfg->menu_line = menu_line++;
		printf( "\tdep_tristate $w.config.f %d %d \"%s\" %s\n",
		    cfg->menu_number, cfg->menu_line, cfg->label,
		    vartable[cfg->nameindex].name );
		break;

	    case token_hex:
		cfg->menu_line = menu_line++;
		printf( "\thex $w.config.f %d %d \"%s\" %s\n",
		    cfg->menu_number, cfg->menu_line, cfg->label,
		    vartable[cfg->nameindex].name );
		break;

	    case token_int:
		cfg->menu_line = menu_line++;
		printf( "\tint $w.config.f %d %d \"%s\" %s\n",
		    cfg->menu_number, cfg->menu_line, cfg->label,
		    vartable[cfg->nameindex].name );
		break;

	    case token_string:
		cfg->menu_line = menu_line++;
		printf( "\tistring $w.config.f %d %d \"%s\" %s\n",
		    cfg->menu_number, cfg->menu_line, cfg->label,
		    vartable[cfg->nameindex].name );
		break;

	    case token_tristate:
		cfg->menu_line = menu_line++;
		printf( "\ttristate $w.config.f %d %d \"%s\" %s\n",
		    cfg->menu_number, cfg->menu_line, cfg->label,
		    vartable[cfg->nameindex].name );
		break;
	    }
	}

	end_proc( scfg, imenu );
    }

    /*
     * The top level menu also needs an update function.  When we update a
     * submenu, we may need to disable one or more of the submenus on
     * the top level menu, and this procedure will ensure that things are
     * correct.
     */
    clear_globalflags();
    printf( "proc update_mainmenu {}  {\n" );
    for ( imenu = 1; imenu <= tot_menu_num; imenu++ )
    {
	if ( menu_first[imenu]->cond != NULL && menu_first[imenu]->menu_number == 0 )
	    generate_if( menu_first[imenu], menu_first[imenu]->cond, imenu, -1 );
    }
    printf( "}\n\n\n" );

    clear_globalflags();
    /*
     * Generate code to load the default settings into the variables.
     * The script in tail.tk will attempt to load .config,
     * which may override these settings, but that's OK.
     */
    for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
    {
	switch ( cfg->token )
	{
	default:
	    break;

	case token_bool:
	case token_choice_item:
	case token_dep_bool:
	case token_dep_tristate:
	case token_dep_mbool:
	case token_tristate:
	    if ( ! vartable[cfg->nameindex].global_written )
	    {
		printf( "set %s 0\n", vartable[cfg->nameindex].name );
		vartable[cfg->nameindex].global_written = 1;
	    }
	    break;

	case token_choice_header:
	    printf( "set tmpvar_%d \"(not set)\"\n", -(cfg->nameindex) );
	    break;

	case token_hex:
	case token_int:
	    if ( ! vartable[cfg->nameindex].global_written )
	    {
		printf( "set %s %s\n", vartable[cfg->nameindex].name, cfg->value ? cfg->value : "0" );
		vartable[cfg->nameindex].global_written = 1;
	    }
	    break;

	case token_string:
	    if ( ! vartable[cfg->nameindex].global_written )
	    {
		printf( "set %s \"%s\"\n", vartable[cfg->nameindex].name, cfg->value );
		vartable[cfg->nameindex].global_written = 1;
	    }
	    break;
	}
    }

    /*
     * Define to an empty value all other variables (which are never defined)
     */
    for ( i = 1; i <= max_varnum; i++ )
    {
	if ( ! vartable[i].global_written
	&&   strncmp( vartable[i].name, "CONSTANT_", 9 ) )
	    printf( "set %s 4\n", vartable[i].name );
    }

    /*
     * Generate a function to write all of the variables to a file.
     */
    printf( "proc writeconfig {file1 file2} {\n" );
    printf( "\tset cfg [open $file1 w]\n" );
    printf( "\tset autocfg [open $file2 w]\n" );
    printf( "\tset notmod 1\n" );
    printf( "\tset notset 0\n" );
    printf( "\tputs $cfg \"#\"\n");
    printf( "\tputs $cfg \"# Automatically generated make config: don't edit\"\n");
    printf( "\tputs $cfg \"#\"\n" );

    printf( "\tputs $autocfg \"/*\"\n" );
    printf( "\tputs $autocfg \" * Automatically generated C config: don't edit\"\n" );
    printf( "\tputs $autocfg \" */\"\n" );
    printf( "\tputs $autocfg \"#define AUTOCONF_INCLUDED\"\n" );

    clear_globalflags();
    for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
    {
	switch ( cfg->token )
	{
	default:
	    break;

	case token_bool:
	case token_choice_header:
	case token_comment:
	case token_define_bool:
	case token_define_hex:
	case token_define_int:
	case token_define_string:
	case token_define_tristate:
	case token_dep_bool:
	case token_dep_tristate:
	case token_dep_mbool:
	case token_hex:
	case token_int:
	case token_string:
	case token_tristate:
	    generate_writeconfig( cfg );
	    break;
	}
    }
    printf( "\tclose $cfg\n" );
    printf( "\tclose $autocfg\n" );
    printf( "}\n\n\n" );

    /*
     * Generate a simple function that updates the master choice
     * variable depending upon what values were loaded from a .config
     * file.  
     */
    printf( "proc clear_choices { } {\n" );
    for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
    {
	if ( cfg->token == token_choice_header )
	{
	    for ( cfg1  = cfg->next; 
		  cfg1 != NULL && cfg1->token == token_choice_item;
		  cfg1  = cfg1->next )
	    {
		printf( "\tglobal %s; set %s 0\n",
		    vartable[cfg1->nameindex].name,
		    vartable[cfg1->nameindex].name );
	    }
	}
    }
    printf( "}\n\n\n" );

    printf( "proc update_choices { } {\n" );
    for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
    {
	if ( cfg->token == token_choice_header )
	{
	    printf( "\tglobal tmpvar_%d\n", -(cfg->nameindex) );
	    printf("\tset tmpvar_%d \"%s\"\n", -(cfg->nameindex), cfg->value);
	    for ( cfg1  = cfg->next; 
		  cfg1 != NULL && cfg1->token == token_choice_item;
		  cfg1  = cfg1->next )
	    {
		printf( "\tglobal %s\n", vartable[cfg1->nameindex].name );
		printf( "\tif { $%s == 1 } then { set tmpvar_%d \"%s\" }\n",
		    vartable[cfg1->nameindex].name,
		    -(cfg->nameindex), cfg1->label );
	    }
	}
    }
    printf( "}\n\n\n" );

    generate_update_var( scfg, 0 );

    /*
     * That's it.  We are done.  The output of this file will have header.tk
     * prepended and tail.tk appended to create an executable wish script.
     */
}