/* Return the number of words in 'buf'. A word is defined as a * sequence of characters not containing any of the characters in * 'delimeters'. */ int count_args(const char buf[], const char delimeters[]) { int i = 0; bool prev_was_delim; bool cur_is_delim = true; int argc = 0; while (buf[i] != '\0') { prev_was_delim = cur_is_delim; cur_is_delim = exists_in(buf[i], delimeters); argc += (prev_was_delim && !cur_is_delim); ++i; } return argc; }
void process_input(char *input){ char *stringp = (char *)malloc(sizeof(input)); strcpy(stringp, input); const char *delim = "|"; char *service; //stringp is updated to point to the next token service = strtok(stringp, delim); int service_index = exists_in(service, list_of_services, NO_OF_SERVICES); if(service_index != -1){ char *client_fifo = strtok(NULL, delim); printf("Client %s requested for service %s\n", client_fifo, service); int client_fifo_fd = open(client_fifo, O_RDWR); client_fifos_fds[client_fd_index++] = client_fifo_fd; int pid = fork(); if (pid == 0){ // int in = client_fifo_fd; int out = client_fifo_fd; // if (in != 0){ // dup2(in, 0); // close(in); // } // child will read from the pipe of the previous process (int in) if (out != 1){ dup2(out,1); close(out); } // child will write to the pipe execl(service, service, (char*)0); } } else{ printf("Service does not exist\n"); } memset(input, 0, sizeof(input)); }
void process_input(char *input){ char *stringp = (char *)malloc(sizeof(input)); strcpy(stringp, input); const char *delim = "|"; char *service; //stringp is updated to point to the next token service = strtok(stringp, delim); int service_index = exists_in(service, list_of_services, NO_OF_SERVICES); if(service_index != -1){ char *client_fifo = strtok(NULL, delim); printf("Client %s requested for service %s\n", client_fifo, service); // client_fifos_fds[client_fd_index++] = client_fifo_fd; // generating the service fifo to send back to the client. char* servicefifo = (char *)malloc(sizeof(char)* 100); strcpy(servicefifo, service); strcat(servicefifo, "fifo|\n"); // sending the service fifo back to the client int server_to_client = open(client_fifo, O_WRONLY); int w = write(server_to_client, servicefifo, sizeof(servicefifo)); print_error(w, "write to fifo failed"); fflush(stdout); } else{ printf("Service does not exist\n"); } memset(input, 0, sizeof(input)); }
void main_game::run(float dt) { game_base::run(dt); // world position for interactions world_mouse_pos = camera.viewport_to_world(mouse->position, screen_rect); nebular_background->run(camera); current_level->run(dt); if (state == game_state::game_base) { bullet_particle_system.upload(dt, screen_rect); sparks_particle_system.upload(dt, screen_rect); ship_sys.run(dt, screen_rect); ship_sys.calc_screen_pos(camera, screen_rect); background_dust->run(camera, screen_rect); waypoint_visualizer->run(); trails_renderer->run(camera); if (ship_sys.size() > 0) { auto button_container = (ui::ui_container*)ui.named_elements["ship_button_container"]; uint32_t current_button_count = 0; for (auto button_it = button_container->begin(); button_it != button_container->end(); ++button_it) { current_button_count++; } if (current_button_count < ship_sys.size()) { for (uint32_t i = 0; i < ship_sys.size() - current_button_count; ++i) { auto new_selection_button = new ship_selection_button(button_container); } } auto current_button = (ship_selection_button*)*button_container->begin(); for (auto ship_it = ship_sys.begin(); ship_it != ship_sys.end(); ++ship_it) { auto screen_pos = (*ship_it)->screen_position; current_button->set_margin_left(floor(screen_pos.x - 8.f)); current_button->set_margin_top(floor(screen_pos.y - 8.f)); if ((*ship_it)->team == 0) { if (exists_in(teams[0].selected_ships, *ship_it)) { current_button->set_selection_state(ship_selection_button_state::selected); } else { current_button->set_selection_state(ship_selection_button_state::player); } } else { current_button->set_selection_state(ship_selection_button_state::enemy); } current_button->assc_ship = *ship_it; current_button->mouse_button_released(ui::get_ui_handler(&main_game::ship_selection_button_mouse_released, this)); current_button = (ship_selection_button*)current_button->get_next_leaf(); } while (current_button) { current_button->set_visibility(false); current_button = (ship_selection_button*)current_button->get_next_leaf(); } button_container->arrange_layout(viewport); } // camera movements if (mouse->wheel_y > 0) { camera.zoom_in(dt); } if (mouse->wheel_y < 0) { camera.zoom_out(dt); } if (mouse->is_down(mouse_buttons::middle)) { camera.move(mouse->delta.reflect_y()); } if (mouse->was_pressed(mouse_buttons::left)) { // enabled selection rect if (!ui.render_context.is_drawn_pixel(world_mouse_pos)) { selection_anchor = world_mouse_pos; is_selection_active = true; selection_renderer->show(); } } if (is_selection_active) { if (!ui.render_context.is_drawn_pixel(world_mouse_pos)) { teams[0].previous_selected_ships.clear(); teams[0].selected_ships.swap(teams[0].previous_selected_ships); // create selection rect selection_rect.position.x = min(selection_anchor.x, world_mouse_pos.x); selection_rect.position.y = min(selection_anchor.y, world_mouse_pos.y); selection_rect.size.width = abs(selection_anchor.x - world_mouse_pos.x); selection_rect.size.height = abs(selection_anchor.y - world_mouse_pos.y); selection_renderer->set_rect(selection_rect); // find ships inside selection rect rect ship_rect; for (auto& current_ship : teams[0].ships) { ship_rect.position.x = current_ship->transform.position.x - current_ship->dimension.size.width * 0.5f; ship_rect.position.y = current_ship->transform.position.y - current_ship->dimension.size.height * 0.5f; ship_rect.size.width = current_ship->dimension.size.width; ship_rect.size.height = current_ship->dimension.size.height; if (selection_rect.intersects(ship_rect)) { teams[0].selected_ships.push_back(current_ship); } } // check if some ship was selected for (auto selected_ship : teams[0].selected_ships) { if (!exists_in(teams[0].previous_selected_ships, selected_ship)) { selected_ship->select(); } } // check if some ship was unselected for (auto prev_selected_ship : teams[0].previous_selected_ships) { if (!exists_in(teams[0].selected_ships, prev_selected_ship)) { prev_selected_ship->unselect(); } } } } if (mouse->was_released(mouse_buttons::left)) { // disabled selection rect is_selection_active = false; selection_renderer->hide(); } if (mouse->was_pressed(mouse_buttons::right)) { if (ui.is_drawn_pixel(mouse->position)) { } else { // order selected ships to fly to mouse position waypoint new_waypoint; new_waypoint.type = waypoint_type::fly_to_position; new_waypoint.position_target = world_mouse_pos; add_waypoint_to_selection(new_waypoint); } } // damp screen shake amplitude screen_shake.set_amplitude(screen_shake.get_amplitude() * exp(-4.0f * dt)); } if (state == game_state::loading) { if (!loader.is_finished()) { ((ui_label*)ui.named_elements["loading_label"])->set_text(std::to_string((int)(loader.get_progress() * 100.f))); loader.load(hub, 10.f); loading_squares->run(loader.get_progress()); } else { change_game_state(game_state::game_base); } } }