void command_tr( struct char_buffer* cbuff ) {
	char str_arg1[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg2[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg3[CONFIG_CLI_MAX_CHARACTERS];
	int arg1 = 0;
	int arg2 = 0;

	if(
		next_token( cbuff, str_arg1 ) == 0 ||
		!str_is_integer( str_arg1 ) ||
		next_token( cbuff, str_arg2 ) == 0 ||
		!str_is_integer( str_arg2 ) ||
		next_token( cbuff, str_arg3 ) != 0 ) {
		CommandOutput( "[TR]\tInvalid Arguments" );
		return;
	}

	arg1 = str_atoi( str_arg1 );
	arg2 = str_atoi( str_arg2 );

	if( arg2 < 0 || arg2 > 14 ) {
		CommandOutput( "[TR]\tInvalid Train Speed" );
		return;
	}


	if( SetTrainSpeed( arg1, arg2 ) != -1 ) {
		CommandOutput( "[TR]\tSetting Train %d to Speed %d", arg1, arg2 );
	} else {
		CommandOutput( "[TR]\tERROR: Train %d has not been added", arg1 );
	}
}
void command_zombtest( struct char_buffer* cbuff ) {
	char str_arg1[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg2[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg3[CONFIG_CLI_MAX_CHARACTERS];
	int block_size = 0;
	int num_blocks = 0;

	if(
		next_token( cbuff, str_arg1 ) == 0 ||
		!str_is_integer( str_arg1 ) ||
		next_token( cbuff, str_arg2 ) == 0 ||
		!str_is_integer( str_arg2 ) ||
		next_token( cbuff, str_arg3 ) != 0 ) {
		CommandOutput( "[ZOMB]\tInvalid Arguments" );
		return;
	}

	block_size = str_atoi( str_arg1 );
	num_blocks = str_atoi( str_arg2 );

	CommandOutput( "[ZOMB] STARTING ZOMBIE RECLEMATION TEST [%d blocks of size %d]", num_blocks, block_size );
	struct two_number_message init_msg;
	int zombtesttid = Create( PRIORITY_TEST_ZOMB, &task_test_zombie_reclaim );
	init_msg.num1 = block_size;
	init_msg.num2 = num_blocks;
	Send( zombtesttid, (char *)&init_msg, sizeof (init_msg), (char *)0, 0 );
}
示例#3
0
文件: context.c 项目: yaraki/forsh
Error *context_interpret(Context *context, const char *str)
{
	Value *value;
	if (context->defining_variable) {  // 変数定義
		context->defining_variable = FALSE;
		value = value_new_symbol(str);
		if (NULL == value) {  // エラー (変数名不正など)
			return error_new(IllegalVariableError, NULL);
		}
		map_put(context->map, value_symbol_name(value), value);
	} else if (str_is_integer(str)) {  // 整数
		value = value_new_integer_str(str);
		stack_push(context->stack, value);
	} else if (0 == strcmp(str, "VARIABLE")) {  // 変数定義の開始
		context->defining_variable = TRUE;
	} else if (NULL != (value = context_resolve(context, str))) {  // シンボル
		ForshFunc *func;
		switch (value->type) {
		case TYPE_FUNCTION:
			func = value_function(value);
			return func(context->stack);
		default:
			stack_push(context->stack, value);
			break;
		}
	} else {
		fprintf(stderr, "Failed to interpret: %s\n", str);
	}
	return NULL;
}
void command_sos( struct char_buffer* cbuff ) {
	char str_arg1[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg2[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg3[CONFIG_CLI_MAX_CHARACTERS];

	if(
		next_token( cbuff, str_arg1 ) == 0 ||
		!str_is_integer( str_arg1 ) ||
		next_token( cbuff, str_arg2 ) == 0 ||
		next_token( cbuff, str_arg3 ) != 0 ) {
		CommandOutput( "[SOS]\tInvalid Arguments" );
		return;
	}

	int train_num = str_atoi( str_arg1 );

	int sensor_num = 0;
	if( !sensor_str2num( str_arg2, &sensor_num ) ) {
		CommandOutput( "[SOS]\tInvalid Sensor Name" );
		return;
	}

	if( SOSTrain( train_num, sensor_num ) != -1 ) {
		CommandOutput( "[SOS]\tStopping Train [%d] on Trigger of Sensor [%s] aka [%d]", train_num, str_arg2, sensor_num );
	} else {
		CommandOutput( "[SOS]\tERROR: Train %d has not been added", train_num );
	}
}
void command_st( struct char_buffer* cbuff ) {
	char str_arg1[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg2[CONFIG_CLI_MAX_CHARACTERS];
	int arg1 = 0;

	if(
		next_token( cbuff, str_arg1 ) == 0 ||
		!str_is_integer( str_arg1 ) ||
		next_token( cbuff, str_arg2 ) != 0 ) {
		CommandOutput( "[ST]\tInvalid Arguments" );
		return;
	}

	arg1 = str_atoi( str_arg1 );

	//check the switch number
	if( switch_index( arg1 ) == -1 ) {
		CommandOutput( "[ST]\tInvalid Switch Number" );
		return;
	}

	int status = SwitchStatus( arg1 );
	//display the state that the switch was last switched into
	if( status == SWITCH_CURVED ) {
		CommandOutput( "[ST]\tSwitch %d is Curved", arg1 );
	} else if( status == SWITCH_STRAIGHT ) {
		CommandOutput( "[ST]\tSwitch %d is Straight", arg1 );
	} else if( status == SWITCH_UNKNOWN ) {
		CommandOutput( "[ST]\tSwitch %d is in an Unknown state", arg1 );
	} else {
		CommandOutput( "[ST]\t%d", status );
	}
}
void command_route( struct char_buffer* cbuff, struct track_node * track_data ) {
	char str_arg1[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg2[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg3[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg4[CONFIG_CLI_MAX_CHARACTERS];
	int destination = 0, offset = 0;

	int train_num;
	struct train_route_request_message reqmsg;
	struct empty_message rpl;
	int tid = WhoIs( "traindispatch" );


	if(
		next_token( cbuff, str_arg1 ) == 0 ||
		!str_is_integer( str_arg1 ) ||
		next_token( cbuff, str_arg2 ) == 0) {
		CommandOutput( "[ROUTE]\tInvalid Arguments" );
		return;
	}
	if( next_token( cbuff, str_arg3 ) != 0 ) {
		offset = str_atoi( str_arg3 );
	}
	if(	next_token( cbuff, str_arg4 ) != 0 ) {
		CommandOutput( "[ROUTE]\tInvalid Arguments" );
		return;
	}

	train_num = str_atoi( str_arg1 );
	if( !track_str2num( track_data, str_arg2, &destination ) ) {
		CommandOutput( "[ROUTE]\tInvalid Landmark Name" );
		return;
	}


	reqmsg.message_type = TRAIN_ROUTE_REQUEST_MESSAGE;
	reqmsg.train = train_num;
	reqmsg.pos1.node = -1;
	reqmsg.pos1.offset = 0;
	reqmsg.pos2.node = destination;
	reqmsg.pos2.offset = offset;

	Send( tid, (char *)&reqmsg, sizeof (reqmsg), (char *)&rpl, sizeof (rpl) );

	CommandOutput( "[ROUTE] Routing Train %d to %s + %d mm", train_num, track_data[destination].name, offset );

}
void command_tdir( struct char_buffer* cbuff ) {
	char str_arg1[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg2[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg3[CONFIG_CLI_MAX_CHARACTERS];
	int arg1 = 0;
	int arg2 = 0;

	if(
		next_token( cbuff, str_arg1 ) == 0 ||
		!str_is_integer( str_arg1 ) ||
		next_token( cbuff, str_arg2 ) == 0 ||
		str_arg2[0] == '\0' ||
		str_arg2[1] != '\0' ||
		next_token( cbuff, str_arg3 ) != 0 ) {
		CommandOutput( "[TRDIR]\tInvalid Arguments" );
		return;
	}

	arg1 = str_atoi( str_arg1 );

	if( str_arg2[0] != 'f' && str_arg2[0] != 'b' ) {
		CommandOutput( "[TRDIR]\tInvalid Direction" );
		return;
	}

	if( str_arg2[0] == 'f' ) {
		arg2 = DIRECTION_FORWARD;
	} else {
		arg2 = DIRECTION_BACKWARD;
	}

	//TODO: send direction to train


	if( SetTrainDirection( arg1, arg2 ) != -1 ) {
		switch( arg2 ) {
			case DIRECTION_FORWARD:
				CommandOutput( "[TRDIR]\tSetting Train %d to Direction Forward", arg1 );
				break;
			default:
				CommandOutput( "[TRDIR]\tSetting Train %d to Direction Backward", arg1 );
				break;
		}
	} else {
		CommandOutput( "[TR]\tERROR: Train %d has not been added", arg1 );
	}
}
示例#8
0
文件: proc.c 项目: regit/nufw
/**
 * Load program cache
 */
void prg_cache_load()
{
	char path_process[PATH_MAX];
	char path_fd[PATH_MAX];
	DIR *dirproc = NULL;
	DIR *dirfd = NULL;
	struct dirent *file;

	if (prg_cache_loaded)
		return;
	prg_cache_loaded = 1;

	/* open directory "/proc" */
	dirproc = opendir("/proc");
	if (dirproc == NULL)
		panic("Fail to open /proc directory!");

	while ((file = readdir(dirproc)) != NULL) {
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
		if (file->d_type != DT_DIR)
			continue;
#endif
		if (!str_is_integer(file->d_name))
			continue;

		/* create path like "/proc/123" */
		if (!secure_snprintf
		    (path_process, sizeof(path_process), "/proc/%s",
		     file->d_name))
			continue;

		/* create path like "/proc/123/fd" */
		if (!secure_snprintf
		    (path_fd, sizeof(path_fd), "%s/fd", path_process))
			continue;

		/* open directory like "/proc/123/fd" */
		dirfd = opendir(path_fd);
		if (dirfd != NULL) {
			prg_cache_load_sub(dirfd, path_process, path_fd);
			closedir(dirfd);
		}
	}
	closedir(dirproc);
}
示例#9
0
int main (int argc, char *argv[]) {

  if (argc < 3 || argc < 2 || argc < 1) {
    show_usage(argv[0], TRUE, -1);
  }

  if (file_exists(argv[1]) != 1) {
    show_message("File not found.", TRUE, -2);
  }

  const char* cfilename   = argv[1];
  const char* cfieldname  = argv[2];

  json_object* jobj       = json_object_from_file(cfilename);
  json_object* jlast;

  int i;
  jlast = jobj;
  for(i = 2; i < argc; i++) {
    //printf("arg %d = %s \n", i, argv[i]);

    //argument is referencing an array item by index
    if (json_object_is_array(jlast) && str_is_integer(argv[i])) {
      jlast = json_object_array_get_idx(jlast, atol(argv[i]));
      continue;
    }

    if (json_object_has_item(jlast, argv[i])) {
      jlast = get_object_item(jlast, argv[i]);
    } else {
      jlast = NULL;
    }
  }

  //item was not found
  if (jlast == NULL) {
    printf("%s", "");
    exit(1);
  }

  //print string value or JSON representation of the object
  printf("%s", json_object_get_string(jlast));

  exit(0);
}
void command_sw( struct char_buffer* cbuff ) {
	char str_arg1[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg2[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg3[CONFIG_CLI_MAX_CHARACTERS];
	int arg1 = 0;
	char arg2;

	if(
		next_token( cbuff, str_arg1 ) == 0 ||
		!str_is_integer( str_arg1 ) ||
		next_token( cbuff, str_arg2 ) == 0 ||
		str_arg2[0] == '\0' ||
		str_arg2[1] != '\0' ||
		next_token( cbuff, str_arg3 ) != 0 ) {
		CommandOutput( "[SW]\tInvalid Arguments" );
		return;
	}

	arg1 = str_atoi( str_arg1 );

	//check the switch number
	if( switch_index( arg1 ) == -1 ) {
		CommandOutput( "[SW]\tInvalid Switch Number" );
		return;
	}

	//check the switch state
	if( str_arg2[0] != 's' && str_arg2[0] != 'c' ) {
		CommandOutput( "[SW]\tInvalid Switch State" );
		return;
	}

	if( str_arg2[0] == 'c' ) {
		arg2 = (char)SWITCH_CURVED;
		CommandOutput( "[SW]\tSwitching %d To Curved", arg1 );
	} else {
		arg2 = (char)SWITCH_STRAIGHT;
		CommandOutput( "[SW]\tSwitching %d To Straight", arg1 );
	}

	SetSwitch( arg1, arg2 );
}
void command_delay( struct char_buffer* cbuff ) {
	char str_arg1[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg2[CONFIG_CLI_MAX_CHARACTERS];
	int arg1 = 0;

	if(
		next_token( cbuff, str_arg1 ) == 0 ||
		!str_is_integer( str_arg1 ) ||
		next_token( cbuff, str_arg2 ) != 0 ) {
		CommandOutput( "[DELAY]\tInvalid Arguments" );
		return;
	}

	arg1 = str_atoi( str_arg1 );

	//if(arg1 > 1000){ arg1 = 1000; }

	CommandOutput( "[DELAY]\tDelaying For %d", arg1 );
	Delay( arg1 );
}
void command_add( struct char_buffer* cbuff, int track ) {
	char str_arg1[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg2[CONFIG_CLI_MAX_CHARACTERS];
	int arg1 = 0;

	if(
		next_token( cbuff, str_arg1 ) == 0 ||
		!str_is_integer( str_arg1 ) ||
		next_token( cbuff, str_arg2 ) != 0 ) {
		CommandOutput( "[ADD]\tInvalid Arguments" );
		return;
	}

	arg1 = str_atoi( str_arg1 );

	if( track == TRACK_UNINITIALIZED ) {
		CommandOutput( "[ADD]\tERROR: No track has been selected" );
		return;
	}

	CommandOutput( "[ADD]\tAdding Train %d", arg1 );
	AddTrain( arg1, track );
}
void command_rv( struct char_buffer* cbuff ) {
	char str_arg1[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg2[CONFIG_CLI_MAX_CHARACTERS];
	int arg1 = 0;

	if(
		next_token( cbuff, str_arg1 ) == 0 ||
		!str_is_integer( str_arg1 ) ||
		next_token( cbuff, str_arg2 ) != 0 ) {
		CommandOutput( "[RV]\tInvalid Arguments" );
		return;
	}

	arg1 = str_atoi( str_arg1 );

	if( ReverseTrain( arg1 ) != -1 ) {
		CommandOutput( "[RV]\tReversing Train %d", arg1 );
	} else {
		CommandOutput( "[RV]\tERROR: Train %d has not been added", arg1 );
	}


}
void command_looptest( struct char_buffer* cbuff ) {
	char str_arg1[CONFIG_CLI_MAX_CHARACTERS];
	char str_arg2[CONFIG_CLI_MAX_CHARACTERS];
	int arg1 = 0;

	if(
		next_token( cbuff, str_arg1 ) == 0 ||
		!str_is_integer( str_arg1 ) ||
		next_token( cbuff, str_arg2 ) != 0 ) {
		CommandOutput( "[LOOPTEST]\tInvalid Arguments" );
		return;
	}

	arg1 = str_atoi( str_arg1 );

	//check if priority is valid
	if( arg1 < 0 || arg1 >= PRIORITY_NUM_PRIORITIES ) {
		CommandOutput( "[LOOPTEST]\tInvalid Priority" );
		return;
	}

	CommandOutput( "[LOOPTEST]\tStarting Loop Test" );
	Create( arg1, &task_test_loop_time );
}